Home | History | Annotate | Download | only in libcopybit
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * Copyright (c) 2010 - 2013, The Linux Foundation. All rights reserved.
      4  *
      5  * Not a Contribution, Apache license notifications and license are retained
      6  * for attribution purposes only.
      7  *
      8  * Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *      http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 #include <cutils/log.h>
     22 
     23 #include <linux/msm_mdp.h>
     24 #include <linux/fb.h>
     25 
     26 #include <stdint.h>
     27 #include <string.h>
     28 #include <unistd.h>
     29 #include <errno.h>
     30 #include <fcntl.h>
     31 
     32 #include <sys/ioctl.h>
     33 #include <sys/types.h>
     34 #include <sys/mman.h>
     35 
     36 #include <copybit.h>
     37 
     38 #include "gralloc_priv.h"
     39 #include "software_converter.h"
     40 
     41 #define DEBUG_MDP_ERRORS 1
     42 
     43 /******************************************************************************/
     44 
     45 #define MAX_SCALE_FACTOR    (4)
     46 #define MAX_DIMENSION       (4096)
     47 
     48 /******************************************************************************/
     49 struct blitReq{
     50     struct  mdp_buf_sync sync;
     51     uint32_t count;
     52     struct mdp_blit_req req[10];
     53 };
     54 
     55 /** State information for each device instance */
     56 struct copybit_context_t {
     57     struct copybit_device_t device;
     58     int     mFD;
     59     uint8_t mAlpha;
     60     int     mFlags;
     61     bool    mBlitToFB;
     62     int     acqFence[MDP_MAX_FENCE_FD];
     63     int     relFence;
     64     struct  mdp_buf_sync sync;
     65     struct  blitReq list;
     66 };
     67 
     68 /**
     69  * Common hardware methods
     70  */
     71 
     72 static int open_copybit(const struct hw_module_t* module, const char* name,
     73                         struct hw_device_t** device);
     74 
     75 static struct hw_module_methods_t copybit_module_methods = {
     76 open:  open_copybit
     77 };
     78 
     79 /*
     80  * The COPYBIT Module
     81  */
     82 struct copybit_module_t HAL_MODULE_INFO_SYM = {
     83 common: {
     84 tag: HARDWARE_MODULE_TAG,
     85      version_major: 1,
     86      version_minor: 0,
     87      id: COPYBIT_HARDWARE_MODULE_ID,
     88      name: "QCT MSM7K COPYBIT Module",
     89      author: "Google, Inc.",
     90      methods: &copybit_module_methods
     91         }
     92 };
     93 
     94 /******************************************************************************/
     95 
     96 /** min of int a, b */
     97 static inline int min(int a, int b) {
     98     return (a<b) ? a : b;
     99 }
    100 
    101 /** max of int a, b */
    102 static inline int max(int a, int b) {
    103     return (a>b) ? a : b;
    104 }
    105 
    106 /** scale each parameter by mul/div. Assume div isn't 0 */
    107 static inline void MULDIV(uint32_t *a, uint32_t *b, int mul, int div) {
    108     if (mul != div) {
    109         *a = (mul * *a) / div;
    110         *b = (mul * *b) / div;
    111     }
    112 }
    113 
    114 /** Determine the intersection of lhs & rhs store in out */
    115 static void intersect(struct copybit_rect_t *out,
    116                       const struct copybit_rect_t *lhs,
    117                       const struct copybit_rect_t *rhs) {
    118     out->l = max(lhs->l, rhs->l);
    119     out->t = max(lhs->t, rhs->t);
    120     out->r = min(lhs->r, rhs->r);
    121     out->b = min(lhs->b, rhs->b);
    122 }
    123 
    124 /** convert COPYBIT_FORMAT to MDP format */
    125 static int get_format(int format) {
    126     switch (format) {
    127         case HAL_PIXEL_FORMAT_RGB_565:       return MDP_RGB_565;
    128         case HAL_PIXEL_FORMAT_RGBX_8888:     return MDP_RGBX_8888;
    129         case HAL_PIXEL_FORMAT_BGRX_8888:     return MDP_BGRX_8888;
    130         case HAL_PIXEL_FORMAT_RGB_888:       return MDP_RGB_888;
    131         case HAL_PIXEL_FORMAT_RGBA_8888:     return MDP_RGBA_8888;
    132         case HAL_PIXEL_FORMAT_BGRA_8888:     return MDP_BGRA_8888;
    133         case HAL_PIXEL_FORMAT_YCrCb_422_I:   return MDP_YCRYCB_H2V1;
    134         case HAL_PIXEL_FORMAT_YCbCr_422_I:   return MDP_YCBYCR_H2V1;
    135         case HAL_PIXEL_FORMAT_YCrCb_422_SP:  return MDP_Y_CRCB_H2V1;
    136         case HAL_PIXEL_FORMAT_YCrCb_420_SP:  return MDP_Y_CRCB_H2V2;
    137         case HAL_PIXEL_FORMAT_YCbCr_422_SP:  return MDP_Y_CBCR_H2V1;
    138         case HAL_PIXEL_FORMAT_YCbCr_420_SP:  return MDP_Y_CBCR_H2V2;
    139         case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
    140         case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
    141         case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
    142     }
    143     return -1;
    144 }
    145 
    146 /** convert from copybit image to mdp image structure */
    147 static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
    148 {
    149     private_handle_t* hnd = (private_handle_t*)rhs->handle;
    150     if(hnd == NULL){
    151         ALOGE("copybit: Invalid handle");
    152         return;
    153     }
    154     img->width      = rhs->w;
    155     img->height     = rhs->h;
    156     img->format     = get_format(rhs->format);
    157     img->offset     = hnd->offset;
    158     img->memory_id  = hnd->fd;
    159 }
    160 /** setup rectangles */
    161 static void set_rects(struct copybit_context_t *dev,
    162                       struct mdp_blit_req *e,
    163                       const struct copybit_rect_t *dst,
    164                       const struct copybit_rect_t *src,
    165                       const struct copybit_rect_t *scissor,
    166                       uint32_t horiz_padding,
    167                       uint32_t vert_padding) {
    168     struct copybit_rect_t clip;
    169     intersect(&clip, scissor, dst);
    170 
    171     e->dst_rect.x  = clip.l;
    172     e->dst_rect.y  = clip.t;
    173     e->dst_rect.w  = clip.r - clip.l;
    174     e->dst_rect.h  = clip.b - clip.t;
    175 
    176     uint32_t W, H, delta_x, delta_y;
    177     if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
    178         delta_x = (clip.t - dst->t);
    179         delta_y = (dst->r - clip.r);
    180         e->src_rect.w = (clip.b - clip.t);
    181         e->src_rect.h = (clip.r - clip.l);
    182         W = dst->b - dst->t;
    183         H = dst->r - dst->l;
    184     } else {
    185         delta_x  = (clip.l - dst->l);
    186         delta_y  = (clip.t - dst->t);
    187         e->src_rect.w  = (clip.r - clip.l);
    188         e->src_rect.h  = (clip.b - clip.t);
    189         W = dst->r - dst->l;
    190         H = dst->b - dst->t;
    191     }
    192 
    193     MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
    194     MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
    195 
    196     e->src_rect.x = delta_x + src->l;
    197     e->src_rect.y = delta_y + src->t;
    198 
    199     if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
    200         if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
    201             e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
    202         }else{
    203             e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
    204         }
    205     }
    206 
    207     if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
    208         if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
    209             e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
    210         }else{
    211             e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
    212         }
    213     }
    214 }
    215 
    216 /** setup mdp request */
    217 static void set_infos(struct copybit_context_t *dev,
    218                       struct mdp_blit_req *req, int flags)
    219 {
    220     req->alpha = dev->mAlpha;
    221     req->transp_mask = MDP_TRANSP_NOP;
    222     req->flags = dev->mFlags | flags;
    223     // check if we are blitting to f/b
    224     if (COPYBIT_ENABLE == dev->mBlitToFB) {
    225         req->flags |= MDP_MEMORY_ID_TYPE_FB;
    226     }
    227 #if defined(COPYBIT_QSD8K)
    228     req->flags |= MDP_BLEND_FG_PREMULT;
    229 #endif
    230 }
    231 
    232 /** copy the bits */
    233 static int msm_copybit(struct copybit_context_t *dev, void const *list)
    234 {
    235     int err;
    236     if (dev->relFence != -1) {
    237         close(dev->relFence);
    238         dev->relFence = -1;
    239     }
    240     err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
    241                     (struct mdp_async_blit_req_list const*)list);
    242     ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
    243     if (err == 0) {
    244         return 0;
    245     } else {
    246 #if DEBUG_MDP_ERRORS
    247         struct mdp_async_blit_req_list const* l =
    248             (struct mdp_async_blit_req_list const*)list;
    249         for (unsigned int i=0 ; i<l->count ; i++) {
    250             ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
    251                   "    dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
    252                   "    flags=%08x"
    253                   ,
    254                   i,
    255                   l->req[i].src.width,
    256                   l->req[i].src.height,
    257                   l->req[i].src.format,
    258                   l->req[i].src_rect.x,
    259                   l->req[i].src_rect.y,
    260                   l->req[i].src_rect.w,
    261                   l->req[i].src_rect.h,
    262                   l->req[i].dst.width,
    263                   l->req[i].dst.height,
    264                   l->req[i].dst.format,
    265                   l->req[i].dst_rect.x,
    266                   l->req[i].dst_rect.y,
    267                   l->req[i].dst_rect.w,
    268                   l->req[i].dst_rect.h,
    269                   l->req[i].flags
    270                  );
    271         }
    272 #endif
    273         return -errno;
    274     }
    275 }
    276 
    277 /*****************************************************************************/
    278 
    279 /** Set a parameter to value */
    280 static int set_parameter_copybit(
    281     struct copybit_device_t *dev,
    282     int name,
    283     int value)
    284 {
    285     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    286     int status = 0;
    287     if (ctx) {
    288         switch(name) {
    289             case COPYBIT_ROTATION_DEG:
    290                 switch (value) {
    291                     case 0:
    292                         ctx->mFlags &= ~0x7;
    293                         break;
    294                     case 90:
    295                         ctx->mFlags &= ~0x7;
    296                         ctx->mFlags |= MDP_ROT_90;
    297                         break;
    298                     case 180:
    299                         ctx->mFlags &= ~0x7;
    300                         ctx->mFlags |= MDP_ROT_180;
    301                         break;
    302                     case 270:
    303                         ctx->mFlags &= ~0x7;
    304                         ctx->mFlags |= MDP_ROT_270;
    305                         break;
    306                     default:
    307                         ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
    308                         status = -EINVAL;
    309                         break;
    310                 }
    311                 break;
    312             case COPYBIT_PLANE_ALPHA:
    313                 if (value < 0)      value = MDP_ALPHA_NOP;
    314                 if (value >= 256)   value = 255;
    315                 ctx->mAlpha = value;
    316                 break;
    317             case COPYBIT_DITHER:
    318                 if (value == COPYBIT_ENABLE) {
    319                     ctx->mFlags |= MDP_DITHER;
    320                 } else if (value == COPYBIT_DISABLE) {
    321                     ctx->mFlags &= ~MDP_DITHER;
    322                 }
    323                 break;
    324             case COPYBIT_BLUR:
    325                 if (value == COPYBIT_ENABLE) {
    326                     ctx->mFlags |= MDP_BLUR;
    327                 } else if (value == COPYBIT_DISABLE) {
    328                     ctx->mFlags &= ~MDP_BLUR;
    329                 }
    330                 break;
    331             case COPYBIT_BLEND_MODE:
    332                 if(value == COPYBIT_BLENDING_PREMULT) {
    333                     ctx->mFlags |= MDP_BLEND_FG_PREMULT;
    334                 } else {
    335                     ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
    336                 }
    337                 break;
    338             case COPYBIT_TRANSFORM:
    339                 ctx->mFlags &= ~0x7;
    340                 ctx->mFlags |= value & 0x7;
    341                 break;
    342             case COPYBIT_BLIT_TO_FRAMEBUFFER:
    343                 if (COPYBIT_ENABLE == value) {
    344                     ctx->mBlitToFB = value;
    345                 } else if (COPYBIT_DISABLE == value) {
    346                     ctx->mBlitToFB = value;
    347                 } else {
    348                     ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
    349                             __FUNCTION__, value);
    350                 }
    351                 break;
    352             case COPYBIT_FG_LAYER:
    353                 if(value == COPYBIT_ENABLE) {
    354                      ctx->mFlags |= MDP_IS_FG;
    355                 } else if (value == COPYBIT_DISABLE) {
    356                     ctx->mFlags &= ~MDP_IS_FG;
    357                 }
    358                 break ;
    359             default:
    360                 status = -EINVAL;
    361                 break;
    362         }
    363     } else {
    364         status = -EINVAL;
    365     }
    366     return status;
    367 }
    368 
    369 /** Get a static info value */
    370 static int get(struct copybit_device_t *dev, int name)
    371 {
    372     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    373     int value;
    374     if (ctx) {
    375         switch(name) {
    376             case COPYBIT_MINIFICATION_LIMIT:
    377                 value = MAX_SCALE_FACTOR;
    378                 break;
    379             case COPYBIT_MAGNIFICATION_LIMIT:
    380                 value = MAX_SCALE_FACTOR;
    381                 break;
    382             case COPYBIT_SCALING_FRAC_BITS:
    383                 value = 32;
    384                 break;
    385             case COPYBIT_ROTATION_STEP_DEG:
    386                 value = 90;
    387                 break;
    388             default:
    389                 value = -EINVAL;
    390         }
    391     } else {
    392         value = -EINVAL;
    393     }
    394     return value;
    395 }
    396 
    397 static int set_sync_copybit(struct copybit_device_t *dev,
    398     int acquireFenceFd)
    399 {
    400     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    401     if (acquireFenceFd != -1) {
    402         if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
    403             ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
    404         } else {
    405             int ret = -EINVAL;
    406             struct blitReq *list = &ctx->list;
    407 
    408             // Since fence is full kick off what is already in the list
    409             ret = msm_copybit(ctx, list);
    410             if (ret < 0) {
    411                 ALOGE("%s: Blit call failed", __FUNCTION__);
    412                 return -EINVAL;
    413             }
    414             list->count = 0;
    415             list->sync.acq_fen_fd_cnt = 0;
    416             ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
    417         }
    418     }
    419     return 0;
    420 }
    421 
    422 /** do a stretch blit type operation */
    423 static int stretch_copybit(
    424     struct copybit_device_t *dev,
    425     struct copybit_image_t const *dst,
    426     struct copybit_image_t const *src,
    427     struct copybit_rect_t const *dst_rect,
    428     struct copybit_rect_t const *src_rect,
    429     struct copybit_region_t const *region)
    430 {
    431     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    432     struct blitReq *list;
    433     int status = 0;
    434     private_handle_t *yv12_handle = NULL;
    435 
    436     if (ctx) {
    437         list = &ctx->list;
    438 
    439         if (ctx->mAlpha < 255) {
    440             switch (src->format) {
    441                 // we don't support plane alpha with RGBA formats
    442                 case HAL_PIXEL_FORMAT_RGBA_8888:
    443                 case HAL_PIXEL_FORMAT_BGRA_8888:
    444                     ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
    445                            src->format);
    446                     return -EINVAL;
    447             }
    448         }
    449 
    450         if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
    451             src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
    452             // this is always invalid
    453             ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
    454                    __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
    455 
    456             return -EINVAL;
    457         }
    458 
    459         if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
    460             ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
    461             return -EINVAL;
    462         }
    463 
    464         if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
    465             ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
    466             return -EINVAL;
    467         }
    468 
    469         if(src->format ==  HAL_PIXEL_FORMAT_YV12) {
    470             int usage =
    471             GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
    472             if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
    473                                   src->format, usage)){
    474                 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
    475                     (const_cast<copybit_image_t *>(src))->format =
    476                         HAL_PIXEL_FORMAT_YCrCb_420_SP;
    477                     (const_cast<copybit_image_t *>(src))->handle =
    478                         yv12_handle;
    479                     (const_cast<copybit_image_t *>(src))->base =
    480                         (void *)yv12_handle->base;
    481                 }
    482                 else{
    483                     ALOGE("Error copybit conversion from yv12 failed");
    484                     if(yv12_handle)
    485                         free_buffer(yv12_handle);
    486                     return -EINVAL;
    487                 }
    488             }
    489             else{
    490                 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
    491                 return -EINVAL;
    492             }
    493         }
    494         const uint32_t maxCount = sizeof(list->req)/sizeof(list->req[0]);
    495         const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
    496         struct copybit_rect_t clip;
    497         status = 0;
    498         while ((status == 0) && region->next(region, &clip)) {
    499             intersect(&clip, &bounds, &clip);
    500             mdp_blit_req* req = &list->req[list->count];
    501             int flags = 0;
    502 
    503             private_handle_t* src_hnd = (private_handle_t*)src->handle;
    504             if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
    505                 flags |=  MDP_BLIT_NON_CACHED;
    506             }
    507 
    508             set_infos(ctx, req, flags);
    509             set_image(&req->dst, dst);
    510             set_image(&req->src, src);
    511             set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
    512 
    513             if (req->src_rect.w<=0 || req->src_rect.h<=0)
    514                 continue;
    515 
    516             if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
    517                 continue;
    518 
    519             if (++list->count == maxCount) {
    520                 status = msm_copybit(ctx, list);
    521                 list->sync.acq_fen_fd_cnt = 0;
    522                 list->count = 0;
    523             }
    524         }
    525         if(yv12_handle) {
    526             //Before freeing the buffer we need buffer passed through blit call
    527             if (list->count != 0) {
    528                 status = msm_copybit(ctx, list);
    529                 list->sync.acq_fen_fd_cnt = 0;
    530                 list->count = 0;
    531             }
    532             free_buffer(yv12_handle);
    533         }
    534     } else {
    535         ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
    536         status = -EINVAL;
    537     }
    538     return status;
    539 }
    540 
    541 /** Perform a blit type operation */
    542 static int blit_copybit(
    543     struct copybit_device_t *dev,
    544     struct copybit_image_t const *dst,
    545     struct copybit_image_t const *src,
    546     struct copybit_region_t const *region)
    547 {
    548     struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
    549     struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
    550     return stretch_copybit(dev, dst, src, &dr, &sr, region);
    551 }
    552 
    553 static int finish_copybit(struct copybit_device_t *dev)
    554 {
    555     // NOP for MDP copybit
    556     return 0;
    557 }
    558 static int clear_copybit(struct copybit_device_t *dev,
    559                          struct copybit_image_t const *buf,
    560                          struct copybit_rect_t *rect)
    561 {
    562     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    563     uint32_t color = 0; // black color
    564 
    565     if (!ctx) {
    566         ALOGE ("%s: Invalid copybit context", __FUNCTION__);
    567         return -EINVAL;
    568     }
    569 
    570     struct blitReq list1;
    571     memset((char *)&list1 , 0 ,sizeof (struct blitReq) );
    572     list1.count = 1;
    573     int rel_fen_fd = -1;
    574     int my_tmp_get_fence = -1;
    575 
    576     list1.sync.acq_fen_fd  =  ctx->acqFence;
    577     list1.sync.rel_fen_fd  =  &my_tmp_get_fence;
    578     list1.sync.acq_fen_fd_cnt = ctx->list.sync.acq_fen_fd_cnt;
    579     mdp_blit_req* req = &list1.req[0];
    580 
    581     if(!req) {
    582         ALOGE ("%s : Invalid request", __FUNCTION__);
    583         return -EINVAL;
    584     }
    585 
    586     set_image(&req->dst, buf);
    587     set_image(&req->src, buf);
    588 
    589     if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > req->dst.width ||
    590        rect->t < 0 || (uint32_t)(rect->b - rect->t) > req->dst.height) {
    591        ALOGE ("%s : Invalid rect : src_rect l %d t %d r %d b %d",\
    592        __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
    593        return -EINVAL;
    594     }
    595 
    596     req->dst_rect.x  = rect->l;
    597     req->dst_rect.y  = rect->t;
    598     req->dst_rect.w  = rect->r - rect->l;
    599     req->dst_rect.h  = rect->b - rect->t;
    600 
    601     req->src_rect = req->dst_rect;
    602 
    603     req->const_color.b = (uint32_t)((color >> 16) & 0xff);
    604     req->const_color.g = (uint32_t)((color >> 8) & 0xff);
    605     req->const_color.r = (uint32_t)((color >> 0) & 0xff);
    606     req->const_color.alpha = MDP_ALPHA_NOP;
    607 
    608     req->transp_mask = MDP_TRANSP_NOP;
    609     req->flags = MDP_SOLID_FILL | MDP_MEMORY_ID_TYPE_FB | MDP_BLEND_FG_PREMULT;
    610     int status = msm_copybit(ctx, &list1);
    611 
    612     ctx->list.sync.acq_fen_fd_cnt = 0;
    613     if (my_tmp_get_fence !=  -1)
    614         close(my_tmp_get_fence);
    615 
    616     return status;
    617 }
    618 
    619 /** Fill the rect on dst with RGBA color **/
    620 static int fill_color(struct copybit_device_t *dev,
    621                       struct copybit_image_t const *dst,
    622                       struct copybit_rect_t const *rect,
    623                       uint32_t color)
    624 {
    625     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    626     if (!ctx) {
    627         ALOGE("%s: Invalid copybit context", __FUNCTION__);
    628         return -EINVAL;
    629     }
    630 
    631     if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
    632         ALOGE("%s: Invalid DST w=%d h=%d", __FUNCTION__, dst->w, dst->h);
    633         return -EINVAL;
    634     }
    635 
    636     if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > dst->w ||
    637         rect->t < 0 || (uint32_t)(rect->b - rect->t) > dst->h) {
    638         ALOGE("%s: Invalid destination rect: l=%d t=%d r=%d b=%d",
    639                 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
    640         return -EINVAL;
    641     }
    642 
    643     int status = 0;
    644     struct blitReq* list = &ctx->list;
    645     mdp_blit_req* req = &list->req[list->count++];
    646     set_infos(ctx, req, MDP_SOLID_FILL);
    647     set_image(&req->src, dst);
    648     set_image(&req->dst, dst);
    649 
    650     req->dst_rect.x = rect->l;
    651     req->dst_rect.y = rect->t;
    652     req->dst_rect.w = rect->r - rect->l;
    653     req->dst_rect.h = rect->b - rect->t;
    654     req->src_rect = req->dst_rect;
    655 
    656     req->const_color.r = (uint32_t)((color >> 0) & 0xff);
    657     req->const_color.g = (uint32_t)((color >> 8) & 0xff);
    658     req->const_color.b = (uint32_t)((color >> 16) & 0xff);
    659     req->const_color.alpha = (uint32_t)((color >> 24) & 0xff);
    660 
    661     if (list->count == sizeof(list->req)/sizeof(list->req[0])) {
    662         status = msm_copybit(ctx, list);
    663         list->sync.acq_fen_fd_cnt = 0;
    664         list->count = 0;
    665     }
    666     return status;
    667 }
    668 
    669 /*****************************************************************************/
    670 
    671 /** Close the copybit device */
    672 static int close_copybit(struct hw_device_t *dev)
    673 {
    674     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    675     if (ctx) {
    676         close(ctx->mFD);
    677         free(ctx);
    678     }
    679     return 0;
    680 }
    681 
    682 static int flush_get_fence(struct copybit_device_t *dev, int* fd)
    683 {
    684     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
    685     struct blitReq *list = &ctx->list;
    686     int ret = -EINVAL;
    687 
    688     if (list->count) {
    689         ret = msm_copybit(ctx, list);
    690         if (ret < 0)
    691             ALOGE("%s: Blit call failed", __FUNCTION__);
    692         list->count = 0;
    693     }
    694     *fd = ctx->relFence;
    695     list->sync.acq_fen_fd_cnt = 0;
    696     ctx->relFence = -1;
    697     return ret;
    698 }
    699 
    700 /** Open a new instance of a copybit device using name */
    701 static int open_copybit(const struct hw_module_t* module, const char* name,
    702                         struct hw_device_t** device)
    703 {
    704     int status = -EINVAL;
    705     copybit_context_t *ctx;
    706     ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
    707     memset(ctx, 0, sizeof(*ctx));
    708 
    709     ctx->device.common.tag = HARDWARE_DEVICE_TAG;
    710     ctx->device.common.version = 1;
    711     ctx->device.common.module = const_cast<hw_module_t*>(module);
    712     ctx->device.common.close = close_copybit;
    713     ctx->device.set_parameter = set_parameter_copybit;
    714     ctx->device.get = get;
    715     ctx->device.blit = blit_copybit;
    716     ctx->device.set_sync = set_sync_copybit;
    717     ctx->device.stretch = stretch_copybit;
    718     ctx->device.finish = finish_copybit;
    719     ctx->device.fill_color = fill_color;
    720     ctx->device.flush_get_fence = flush_get_fence;
    721     ctx->device.clear = clear_copybit;
    722     ctx->mAlpha = MDP_ALPHA_NOP;
    723     ctx->mFlags = 0;
    724     ctx->sync.flags = 0;
    725     ctx->relFence = -1;
    726     for (int i=0; i < MDP_MAX_FENCE_FD; i++) {
    727         ctx->acqFence[i] = -1;
    728     }
    729     ctx->sync.acq_fen_fd = ctx->acqFence;
    730     ctx->sync.rel_fen_fd = &ctx->relFence;
    731     ctx->list.count = 0;
    732     ctx->list.sync.acq_fen_fd_cnt = 0;
    733     ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
    734     ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
    735     ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
    736     if (ctx->mFD < 0) {
    737         status = errno;
    738         ALOGE("Error opening frame buffer errno=%d (%s)",
    739               status, strerror(status));
    740         status = -status;
    741     } else {
    742         status = 0;
    743         *device = &ctx->device.common;
    744     }
    745     return status;
    746 }
    747