【转载】HAL Copybit 2D加速实例

来源:互联网 发布:手机cpu超频软件 编辑:程序博客网 时间:2024/06/13 16:17

[S3C6410]Android 2D加速模块Copybit源码

=================================================================================

/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#define LOG_TAG "copybit"

#include <cutils/log.h>

#include <linux/fb.h>

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>

#include <hardware/copybit.h>
#include <hardware/hardware.h>

#include <linux/android_pmem.h>

#include "gralloc_priv.h"
#include "s3c_g2d.h"

#define MAX_DIMENSION 2040

#ifdef COPYBIT_DEBUG
#define DBOUT(arg,...) /
LOGI(arg,__VA_ARGS__)
#endif

#define DEBUG_G2D_ERRORS 0

//static int pmem_offset = 0;
#define PMEM_OFFSET 0x56000000 //16MB /dev/pmem ossfet

/** State information for each device instance */

struct copybit_context_t {
    struct copybit_device_t device;

    int fd;
int mem_fd;
char *mem_addr;

    int rotation;
    int alpha;
    int dither;
    int transform;

};

/**
* Common hardware methods
*/

static int open_copybit(const struct hw_module_t* module, const char* name,
        struct hw_device_t** device);

static struct hw_module_methods_t copybit_module_methods = {
    open: open_copybit
};

/*
* The COPYBIT Module
*/
struct copybit_module_t HAL_MODULE_INFO_SYM = {
    common: {
        tag: HARDWARE_MODULE_TAG,
        version_major: 1,
        version_minor: 0,
        id: COPYBIT_HARDWARE_MODULE_ID,
        name: "Ky6410 COPYBIT Module",
        author: "Rockie Cheng.",
        methods: &copybit_module_methods
    }
};

/*****************************************************************************/

/** min of int a, b */
static inline int min(int a, int b) {
    return (a<b) ? a : b;
}

/** max of int a, b */
static inline int max(int a, int b) {
    return (a>b) ? a : b;
}

/** scale each parameter by mul/div. Assume div isn't 0 */
static inline void MULDIV(uint32_t *a, uint32_t *b, int mul, int div) {
    if (mul != div) {
        *a = (mul * *a) / div;
        *b = (mul * *b) / div;
    }
}

/** Determine the intersection of lhs & rhs store in out */
static void intersect(struct copybit_rect_t *out,
                      const struct copybit_rect_t *lhs,
                      const struct copybit_rect_t *rhs) {
    out->l = max(lhs->l, rhs->l);
    out->t = max(lhs->t, rhs->t);
    out->r = min(lhs->r, rhs->r);
    out->b = min(lhs->b, rhs->b);
}

/** convert COPYBIT_FORMAT to G2D format */
static int get_format(int format) {

    switch (format)
{
    case COPYBIT_FORMAT_RGB_565:       return G2D_RGB16;
    case COPYBIT_FORMAT_RGBX_8888:     return G2D_RGBX32;
    case COPYBIT_FORMAT_RGB_888:       return G2D_RGB16;
    case COPYBIT_FORMAT_RGBA_8888:     return G2D_RGBA32;
    case COPYBIT_FORMAT_BGRA_8888:     return G2D_ARGB32;
    //case COPYBIT_FORMAT_YCbCr_422_SP: return -1;
    //case COPYBIT_FORMAT_YCbCr_420_SP: return -1;
default :
    LOGE("Copybit HAL unsupport format ! ,format is 0x%x",format);
    return -1;
}

}

static uint32_t get_rotate(int transform)
{
int g2d_rotate = S3C_G2D_ROTATOR_0;

switch(transform)
{
   /* flip source image horizontally */
   case COPYBIT_TRANSFORM_FLIP_H:
    g2d_rotate = S3C_G2D_ROTATOR_X_FLIP;
    break;
   
   /* flip source image vertically */
   case COPYBIT_TRANSFORM_FLIP_V:
    g2d_rotate = S3C_G2D_ROTATOR_Y_FLIP;
    break;

   /* rotate source image 90 degres */
   case COPYBIT_TRANSFORM_ROT_90:
    g2d_rotate = S3C_G2D_ROTATOR_90;
    break;
  
   /* rotate source image 180 degres */
   case COPYBIT_TRANSFORM_ROT_180:
    g2d_rotate = S3C_G2D_ROTATOR_180;
    break;

   /* rotate source image 270 degres */
   case COPYBIT_TRANSFORM_ROT_270:
    g2d_rotate = S3C_G2D_ROTATOR_270;
    break;
}
return g2d_rotate;
}

/** convert from copybit image to g2d image structure */
static void set_image(struct g2d_img *img, const struct copybit_image_t *rhs)
{
    private_handle_t* hnd = (private_handle_t*)rhs->handle;
    img->width      = rhs->w;
    img->height     = rhs->h;
    img->format     = get_format(rhs->format);
    img->offset     = hnd->offset;

    img->memory_id = hnd->fd;

}
/** setup rectangles */
static void set_rects(struct copybit_context_t *dev,
                      struct g2d_blit_req *e,
                      const struct copybit_rect_t *dst,
                      const struct copybit_rect_t *src,
                      const struct copybit_rect_t *scissor) {
   struct copybit_rect_t clip;
    intersect(&clip, scissor, dst);

    uint32_t W, H;
#if 0
if(dev->transform == 0 )
{
    e->src_rect.x = (clip.l - dst->l) + src->l;
        e->src_rect.y = (clip.t - dst->t) + src->t;
        e->src_rect.w = (clip.r - clip.l);
        e->src_rect.h = (clip.b - clip.t);
        W = dst->r - dst->l;
        H = dst->b - dst->t;
}    
else
{
    e->src_rect.x = (clip.t - dst->t) + src->t;
        e->src_rect.y = (dst->r - clip.r) + src->l;
        e->src_rect.w = (clip.b - clip.t);
        e->src_rect.h = (clip.r - clip.l);
        W = dst->b - dst->t;
        H = dst->r - dst->l;
}
#endif
         
        e->dst_rect.x = clip.l;
       e->dst_rect.y = clip.t;
        e->dst_rect.w = clip.r - clip.l ;
        e->dst_rect.h = clip.b - clip.t ;

switch(dev->transform)
{
        /*rotate source image 90 degres*/
   case 4:
    e->src_rect.x = (clip.t - dst->t) + src->t ;
        e->src_rect.y = (dst->r - clip.r) + src->l ;
        e->src_rect.w = (clip.b - clip.t) ;
        e->src_rect.h = (clip.r - clip.l) ;
        W = dst->b - dst->t;
        H = dst->r - dst->l;
   break;

   /*rotate source image 180 degres*/
   case 3:
    e->src_rect.x = (dst->r - clip.r) + src->l;
    e->src_rect.y = (dst->b - clip.b) + src->t;
        e->src_rect.w = (clip.r - clip.l) - 1;
        e->src_rect.h = (clip.b - clip.t) - 1;   
        W = dst->r - dst->l;
        H = dst->b - dst->t;  
   break;

   /*rotate source image 270 degres*/
   case 7:
       e->src_rect.x = (dst->b - clip.b) + src->t;
    e->src_rect.y = (clip.l - dst->l) + src->l;
        e->src_rect.w = (clip.b - clip.t) ;
        e->src_rect.h = (clip.r - clip.l) ;
        W = dst->b - dst->t;
        H = dst->r - dst->l;

     break;
       
       /*have no rotation*/
   case 0:
   default:
        e->src_rect.x = (clip.l - dst->l) + src->l;
        e->src_rect.y = (clip.t - dst->t) + src->t;
        e->src_rect.w = (clip.r - clip.l) ;
        e->src_rect.h = (clip.b - clip.t) ;
        W = dst->r - dst->l;
        H = dst->b - dst->t;

   break;
    }
    MULDIV(&e->src_rect.x, &e->src_rect.w, src->r - src->l, W);
    MULDIV(&e->src_rect.y, &e->src_rect.h, src->b - src->t, H);

}

/** setup g2d request */
static void set_infos(struct copybit_context_t *dev, struct g2d_blit_req *req)
{
    req->alpha = dev->alpha;
    req->transform = get_rotate(dev->transform);
}

/** copy the bits */
static int g2d_copybit(struct copybit_context_t *dev, g2d_blit_req const *blit)
{
   struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
   int err;
s3c_g2d_params *params;  

params = (s3c_g2d_params *)malloc(sizeof(s3c_g2d_params));
memset(params, 0, sizeof(s3c_g2d_params));

struct fb_fix_screeninfo finfo;

if( ioctl(blit->src.memory_id, FBIOGET_FSCREENINFO, &finfo) < 0) //not fb ,is pmem!
{
   params->src_base_addr = PMEM_OFFSET + blit->src.offset;  
}
else //is fb!
{
   params->src_base_addr = finfo.smem_start + blit->src.offset;
   //LOGE("fb smem start is 0x%x ,offset is 0x%x",finfo.smem_start,blit->src.offset );
}

params->bpp        = blit->src.format;

if (ctx->alpha != 255)
{
   params->alpha_mode     = 1;
   params->alpha_val      = ctx->alpha;
}
else
{
   params->alpha_mode     = 0;
   params->alpha_val      = 0;
}

params->color_key_mode = 0;
params->color_key_val = 0;
params->src_full_width = blit->src.width;
params->src_full_height = blit->src.height;

if( ioctl(blit->dst.memory_id, FBIOGET_FSCREENINFO, &finfo) < 0) //not fb ,is pmem!
{
   params->dst_base_addr = PMEM_OFFSET + blit->dst.offset;  
}
else //is fb!
{
   params->dst_base_addr = finfo.smem_start + blit->dst.offset;
   //LOGE("fb smem start is 0x%x ,offset is 0x%x",finfo.smem_start,blit->src.offset );
}

params->dst_full_width = blit->dst.width;
params->dst_full_height = blit->dst.height;

params->src_start_x     = blit->src_rect.x;
params->src_start_y     = blit->src_rect.y;
params->src_work_width = blit->src_rect.w - 1;
params->src_work_height = blit->src_rect.h - 1;
   
params->dst_start_x     = blit->dst_rect.x;
params->dst_start_y     = blit->dst_rect.y;
params->dst_work_width = blit->dst_rect.w - 1;
params->dst_work_height = blit->dst_rect.h - 1;


//initialize clipping window
params->cw_x1 = params->dst_start_x;
params->cw_y1 = params->dst_start_y;
params->cw_x2 = params->dst_start_x+params->dst_work_width;
params->cw_y2 = params->dst_start_y+params->dst_work_height;

int nRot = S3C_G2D_ROTATOR_0;
switch(ctx->transform)
{
   case 0:
    nRot = S3C_G2D_ROTATOR_0;

    break;
   case 4:
    nRot = S3C_G2D_ROTATOR_90;
   
    params->src_start_x     = blit->src_rect.x;
    params->src_start_y     = blit->src_rect.y;
    params->src_work_width = blit->src_rect.w ;
    params->src_work_height = blit->src_rect.h;
   
    params->dst_start_x     = blit->dst_rect.x + 1;
    params->dst_start_y     = blit->dst_rect.y;
    params->dst_work_width = blit->dst_rect.w ;
    params->dst_work_height = blit->dst_rect.h ;
   
    break;
   case 3:
    nRot = S3C_G2D_ROTATOR_180;
    break;
   case 7:
    nRot = S3C_G2D_ROTATOR_270;
    break;
   default:
    nRot = S3C_G2D_ROTATOR_0;
    break;   
}
   err = ioctl(dev->fd, nRot, params);

    LOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));

#if DEBUG_G2D_ERRORS
            LOGD("    src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}/n"
                 "    dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}/n"
                    ,
                    blit->src.width,
                    blit->src.height,
                    blit->src.format,
                    blit->src_rect.x,
                    blit->src_rect.y,
                    blit->src_rect.w,
                    blit->src_rect.h,
                    blit->dst.width,
                    blit->dst.height,
                    blit->dst.format,
                    blit->dst_rect.x,
                    blit->dst_rect.y,
                    blit->dst_rect.w,
                    blit->dst_rect.h
                 );         
#endif

end:
free(params);
return 0;

}

/*****************************************************************************/

/** Set a parameter to value */
static int set_parameter_copybit(struct copybit_device_t *dev, int name, int value)
{
    struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
int status=0;

   switch (name)
{
    case COPYBIT_ROTATION_DEG:
       //LOGI("in copybit set_parameter_copybit: name=%d value=%d/n",name,value);
    ctx->rotation = value;
    if(!((value==0)||(value==90)||(value==270)))
    {
     LOGE("In valid rotation degree/n");   
     status=-EINVAL;       
    }
        break;

    case COPYBIT_PLANE_ALPHA:
    if(value<0) value=0;
    if(value>255) value=255;
        ctx->alpha = value;
        break;

    case COPYBIT_DITHER:
        ctx->dither = value;
        break;

    case COPYBIT_TRANSFORM:
        ctx->transform = value;
        break;

    default:
       LOGE("name of the set parameter not implemented/n");
        return -EINVAL;
    }
    return status;

}

/** Get a static info value */
static int get(struct copybit_device_t *dev, int name)
{
    struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    int value;
//LOGI("in copybit get: name=%d/n",name);
    if (ctx) {
        switch(name) {
        case COPYBIT_MINIFICATION_LIMIT:
            value = 4;
            break;
        case COPYBIT_MAGNIFICATION_LIMIT:
            value = 4;
            break;
        case COPYBIT_SCALING_FRAC_BITS:
            value = 32;
            break;
        case COPYBIT_ROTATION_STEP_DEG:
            value = 90;
            break;
        default:
            value = -EINVAL;
        }
    } else {
        value = -EINVAL;
    }
    return value;
}

/** do a stretch blit type operation */
static int stretch_copybit(
        struct copybit_device_t *dev,
        struct copybit_image_t const *dst,
        struct copybit_image_t const *src,
        struct copybit_rect_t const *dst_rect,
        struct copybit_rect_t const *src_rect,
        struct copybit_region_t const *region)
{
    struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    int status = 0;
struct g2d_blit_req blit_req;

    if (ctx)
{
        struct {
            uint32_t count;
            struct g2d_blit_req req[12];
        } list;

      int count;
        struct copybit_rect_t clip;
        const struct copybit_rect_t bounds = { 0, 0, dst->w, dst->h };

        if (ctx->alpha < 255)
      {
            switch (src->format)
     {
                // we don't support plane alpha with RGBA formats
                case COPYBIT_FORMAT_RGBA_8888:
                case COPYBIT_FORMAT_BGRA_8888:
                case COPYBIT_FORMAT_RGBA_5551:
                case COPYBIT_FORMAT_RGBA_4444:
                    return -EINVAL;
             }
              }

        if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
            src_rect->t < 0 || (uint32_t)src_rect->b > src->h)
     return -EINVAL;

        if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION)
            return -EINVAL;

        if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION)
            return -EINVAL;

        while ((status == 0) && region->next(region, &clip))
    {
     //LOGI("clip:l=%d r=%d t=%d b=%d/n",clip.l,clip.r,clip.t,clip.b);
            intersect(&clip, &bounds, &clip);
           
            set_infos(ctx, &blit_req);//set blit_req's alpha & transform
     set_image(&blit_req.dst, dst);//set blit_req's img
     set_image(&blit_req.src, src);
     set_rects(ctx, &blit_req, dst_rect, src_rect, &clip);

     status = g2d_copybit(ctx, &blit_req);
    }
    }
else
{
        status = -EINVAL;
}
    return status;
}

/** Perform a blit type operation */
static int blit_copybit(
        struct copybit_device_t *dev,
        struct copybit_image_t const *dst,
        struct copybit_image_t const *src,
        struct copybit_region_t const *region)
{
    struct copybit_rect_t dr = { 0, 0, dst->w, dst->h };
    struct copybit_rect_t sr = { 0, 0, src->w, src->h };
    return stretch_copybit(dev, dst, src, &dr, &sr, region);
}

/*****************************************************************************/

/** Close the copybit device */
static int close_copybit(struct hw_device_t *dev)
{
    struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    if (ctx) {
        close(ctx->fd);
        free(ctx);
    }
    return 0;
}

/** Open a new instance of a copybit device using name */
static int open_copybit(const struct hw_module_t* module, const char* name,
        struct hw_device_t** device)
{
    int status = -EINVAL;

    copybit_context_t *ctx;
    ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
    memset(ctx, 0, sizeof(*ctx));

    ctx->device.common.tag = HARDWARE_DEVICE_TAG;
    ctx->device.common.version = 1;
    ctx->device.common.module = const_cast<hw_module_t*>(module);
    ctx->device.common.close = close_copybit;
    ctx->device.set_parameter = set_parameter_copybit;
    ctx->device.get = get;
    ctx->device.blit = blit_copybit;
    ctx->device.stretch = stretch_copybit;

ctx->alpha = 0xff;
ctx->rotation = 0;
ctx->transform = 0;

    ctx->fd = open(S3C_G2D_DEV_NAME, O_RDWR, 0);
   
    if (ctx->fd < 0)
{
        status = errno;
        LOGE("Error opening frame buffer errno=%d (%s)", status, strerror(status));
        status = -status;
   }

    *device = &ctx->device.common;
status = 0;

    return status;
}

原创粉丝点击