一个Recovery模式横屏方案

1、创建bootable\recovery\minui\mt_graphic_rotate.h

//
// Created by Larkin.Lai on 2019/9/6.
//

#ifndef ANDROID_GRAPHIC_ROTATE_H
#define ANDROID_GRAPHIC_ROTATE_H
#include "minui/minui.h"

void rotate_canvas_exit(void);
void rotate_canvas_init(GRSurface *gr_draw);
void rotate_surface(GRSurface *dst, GRSurface *src);
GRSurface *rotate_canvas_get(GRSurface *gr_draw);
#endif //ANDROID_GRAPHIC_ROTATE_H

2、创建bootable\recovery\minui\mt_graphic_rotate.cpp

//
// Created by Larkin.Lai on 2019/9/6.
//

#include 
#include 
#include 
#include 

#include 
#include 

#include 
#include 
#include 
#include 

#include 
#include 

#include "minui/minui.h"
#include "graphics.h"

GRSurface __gr_canvas;

GRSurface* gr_canvas = NULL;
int rotate_index=-1;

static void print_surface_info(GRSurface *s, const char *name)
{
    printf("[graphics] %s > Height:%d, Width:%d, PixelBytes:%d, RowBytes:%d, Size:%d, Data: 0x%08" PRIxPTR "\n",
        name, s->height, s->width, s->pixel_bytes, s->row_bytes, s->height* s->row_bytes, (uintptr_t) s->data);
}

#ifndef ANDROID_GRAPHIC_ROTATE_H
#define ANDROID_GRAPHIC_ROTATE_H "undefined"
#endif
static int rotate_config(GRSurface *gr_draw)
{
    if (rotate_index<0)
    {
        if (gr_draw->pixel_bytes != 4) rotate_index=0; // support 4 bytes pixel only
        else if (0 == strncmp(MTK_LCM_PHYSICAL_ROTATION, "90", 2))
        {
            rotate_index=1;
        }
        else if (0 == strncmp(MTK_LCM_PHYSICAL_ROTATION, "180", 3))
        {
            rotate_index=2;
        }

        else if (0 == strncmp(MTK_LCM_PHYSICAL_ROTATION, "270", 3)) rotate_index=3;
        else rotate_index=0;
        printf("[graphics] rotate_config %d %s\n", rotate_index, MTK_LCM_PHYSICAL_ROTATION);
    }
    return 3;//mtk /*修改这个值可以改变图标的方向*/
}

#define swap(x, y, type) {type z; z=x; x=y; y=z;}

// Allocate and setup the canvas object
void rotate_canvas_init(GRSurface *gr_draw)
{
    gr_canvas = &__gr_canvas;
    memcpy(gr_canvas, gr_draw, sizeof(GRSurface));

    // Swap canvas' height and width, if the rotate angle is 90" or 270"
    if (rotate_config(gr_draw)%2) {
        swap(gr_canvas->width, gr_canvas->height, int);
        gr_canvas->row_bytes = gr_canvas->width * gr_canvas->pixel_bytes;
    }

    gr_canvas->data = (unsigned char*) malloc(gr_canvas->height * gr_canvas->row_bytes);
    if (gr_canvas->data == NULL) {
        printf("[graphics] rotate_canvas_init() malloc gr_canvas->data failed\n");
        gr_canvas = NULL;
        return;
    }

    memset(gr_canvas->data,  0, gr_canvas->height * gr_canvas->row_bytes);

    print_surface_info(gr_draw, "gr_draw");
    print_surface_info(gr_canvas, "gr_canvas");
}

// Cleanup the canvas
void rotate_canvas_exit(void)
{
    if (gr_canvas) {
        if (gr_canvas->data)
            free(gr_canvas->data);
        free(gr_canvas);
    }
    gr_canvas=NULL;
}

// Return the canvas object
GRSurface *rotate_canvas_get(GRSurface *gr_draw)
{
    // Initialize the canvas, if it was not exist.
    if (gr_canvas==NULL)
        rotate_canvas_init(gr_draw);
    return gr_canvas;
}

// Surface Rotate Routines
static void rotate_surface_0(GRSurface *dst, GRSurface *src)
{
    memcpy(dst->data, src->data, src->height*src->row_bytes);
}

static void rotate_surface_270(GRSurface *dst, GRSurface *src)
{
    int v, w, h;
    unsigned int *src_pixel;
    unsigned int *dst_pixel;

    for (h=0, v=src->width-1; hheight; h++, v--) {
        for (w=0; wwidth; w++) {
            dst_pixel = (unsigned int *)(dst->data + dst->row_bytes*h);
            src_pixel = (unsigned int *)(src->data + src->row_bytes*w);
            *(dst_pixel+w)=*(src_pixel+v);
        }
    }
}

static void rotate_surface_180(GRSurface *dst, GRSurface *src)
{
    int v, w, k, h;
    unsigned int *src_pixel;
    unsigned int *dst_pixel;

    for (h=0, k=src->height-1; hheight && k>=0 ; h++, k--) {
        dst_pixel = (unsigned int *)(dst->data + dst->row_bytes*h);
        src_pixel = (unsigned int *)(src->data + src->row_bytes*k);
        for (w=0, v=src->width-1; wwidth && v>=0; w++, v--) {
            *(dst_pixel+w)=*(src_pixel+v);
        }
    }
}

static void rotate_surface_90(GRSurface *dst, GRSurface *src)
{
    int w, k, h;
    unsigned int *src_pixel;
    unsigned int *dst_pixel;

    for (h=0; hheight; h++) {
        for (w=0, k=src->height-1; wwidth; w++, k--) {
            dst_pixel = (unsigned int *)(dst->data + dst->row_bytes*h);
            src_pixel = (unsigned int *)(src->data + src->row_bytes*k);
            *(dst_pixel+w)=*(src_pixel+h);
        }
    }
}

typedef void (*rotate_surface_t) (GRSurface *, GRSurface *);

rotate_surface_t rotate_func[4]=
{
    rotate_surface_0,
    rotate_surface_90,
    rotate_surface_180,
    rotate_surface_270
};

// rotate and copy src* surface to dst surface
void rotate_surface(GRSurface *dst, GRSurface *src)
{
    rotate_surface_t rotate;
    rotate=rotate_func[rotate_config(dst)];
    rotate(dst, src);
}

3、bootable\recovery\minui\Android.mk中添加graphic_rotate.cpp

LOCAL_SRC_FILES := \
    events.cpp \
    graphics.cpp \
    graphics_adf.cpp \
    graphics_drm.cpp \
    graphics_fbdev.cpp \
    resources.cpp \
    graphic_rotate.cpp

4、bootable\recovery\minui\graphics_fbdev.cpp

diff --git a/bootable/recovery/minui/graphics_fbdev.cpp b/bootable/recovery/minui/graphics_fbdev.cpp
old mode 100644
new mode 100755
index 746f42a..c1b3dd2
--- a/bootable/recovery/minui/graphics_fbdev.cpp
+++ b/bootable/recovery/minui/graphics_fbdev.cpp
@@ -15,7 +15,7 @@
  */
 
 #include "graphics_fbdev.h"
-
+#include "graphic_rotate.h"
 #include 
 #include 
 #include 
@@ -138,10 +138,11 @@ GRSurface* MinuiBackendFbdev::Init() {
   Blank(true);
   Blank(false);
 
-  return gr_draw;
+  return rotate_canvas_get(gr_draw);
 }
 
 GRSurface* MinuiBackendFbdev::Flip() {
+  rotate_surface(gr_draw, rotate_canvas_get(gr_draw));
   if (double_buffered) {
     // Change gr_draw to point to the buffer currently displayed,
     // then flip the driver so we're displaying the other buffer
@@ -152,13 +153,13 @@ GRSurface* MinuiBackendFbdev::Flip() {
     // Copy from the in-memory surface to the framebuffer.
     memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes);
   }
-  return gr_draw;
+  return rotate_canvas_get(gr_draw);
 }
 
 MinuiBackendFbdev::~MinuiBackendFbdev() {
   close(fb_fd);
   fb_fd = -1;
-
+rotate_canvas_exit();
   if (!double_buffered && gr_draw) {
     free(gr_draw->data);
     free(gr_draw);
-- 
2.7.4

以上实现默认旋转270度。可根据rotate_config方法的返回值旋转其他方向.

你可能感兴趣的:(Android,OS)