免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 974 | 回复: 0
打印 上一主题 下一主题

[20100528]copybit.cpp [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览
/*
 * 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 DEBUG_G2D_ERRORS 0

//#define MEMALLOC_SIZE 0x200000 //2MB for RGBA

#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);

    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;

    uint32_t W, H;
    if (dev->transform != 0) {
        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;
    } else {
        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;
    }
    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));

//pmem_region *region;
//region = (pmem_region *)malloc(sizeof(pmem_region));

struct fb_fix_screeninfo finfo;

//if(blit->src.format != G2D_RGB16)//RGBA or RGBX
//{
// params->src_base_addr = blit->src.memory_id;
// params->pmem_fd = ctx->mem_fd;
//} 
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      = 256-ctx->alpha;
}
else
{
params->alpha_mode     = 0;
params->alpha_val      = 0;
}

//params->src_offset = blit->src.offset;

params->color_key_mode = 0;
params->color_key_val = 0;

params->src_full_width = blit->src.width;
params->src_full_height = blit->src.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;

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->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 = 0;
params->cw_y1 = 0;
params->cw_x2 = 800-1;
params->cw_y2 = 480-1;

//LOGI("src fd is 0x%x , dst fd is 0x%x",blit->src.memory_id ,blit->dst.memory_id);
if ( ctx->transform == 4)
{
err =  ioctl(dev->fd, S3C_G2D_ROTATOR_90, params);
}
else
{
err =  ioctl(dev->fd, S3C_G2D_ROTATOR_0, 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);
//free(region);
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;

    switch (name) 
{
    case COPYBIT_ROTATION_DEG:
        ctx->rotation = value;
        break;

    case COPYBIT_PLANE_ALPHA:
        ctx->alpha = value;
        break;

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

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

    default:
        return -EINVAL;
    }
    return 0;

}

/** 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;
    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;

    //LOGI("stretch : src: w=%d, h=%d  fmt:%d dst: w=%d, h=%d fmt:%d alpha:%d transform:%d  \n", 
// src->w, src->h, src->format, dst->w, dst->h, dst->format, ctx->alpha, ctx->transform);

    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 };

        count = 0;

        while ((status == 0) && region->next(region, &clip)) 
{

            intersect(&clip, &bounds, &clip);
            
            set_infos(ctx, &blit_req);
set_image(&blit_req.dst, dst);
set_image(&blit_req.src, src);
set_rects(ctx, &blit_req, dst_rect, src_rect, &clip);

count ++; 
    
        if ((status == 0) && count > 0) 
{
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);
//  close(ctx->mem_fd);
//  munmap(ctx->mem_addr,  MEMALLOC_SIZE);
        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;
 

// ctx->mem_fd = open("/dev/pmem_adsp", O_RDWR);
// ctx->mem_addr = (char *)mmap(0, MEMALLOC_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, ctx->mem_fd, NULL);

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

    return status;
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP