blob: bd9fc1aa895e35d25e20df5661a3cad0cf9f4717 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05303 * Copyright (c) 2010 - 2014, The Linux Foundation. All rights reserved.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08004 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
Naseer Ahmed29a26812012-06-14 00:56:20 -07007 *
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
Naseer Ahmed29a26812012-06-14 00:56:20 -070021#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"
Sushil Chauhan0265c472014-10-01 16:39:02 -070040#include <qdMetaData.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070041
42#define DEBUG_MDP_ERRORS 1
43
44/******************************************************************************/
45
Naseer Ahmed29a26812012-06-14 00:56:20 -070046#define MAX_SCALE_FACTOR (4)
47#define MAX_DIMENSION (4096)
Naseer Ahmed29a26812012-06-14 00:56:20 -070048
49/******************************************************************************/
Terence Hampsonadf47302013-05-23 12:21:02 -040050struct blitReq{
51 struct mdp_buf_sync sync;
52 uint32_t count;
53 struct mdp_blit_req req[10];
54};
Naseer Ahmed29a26812012-06-14 00:56:20 -070055
56/** State information for each device instance */
57struct copybit_context_t {
58 struct copybit_device_t device;
59 int mFD;
60 uint8_t mAlpha;
61 int mFlags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070062 bool mBlitToFB;
Terence Hampsonadf47302013-05-23 12:21:02 -040063 int acqFence[MDP_MAX_FENCE_FD];
64 int relFence;
65 struct mdp_buf_sync sync;
66 struct blitReq list;
Naseer Ahmed29a26812012-06-14 00:56:20 -070067};
68
69/**
70 * Common hardware methods
71 */
72
73static int open_copybit(const struct hw_module_t* module, const char* name,
74 struct hw_device_t** device);
75
76static struct hw_module_methods_t copybit_module_methods = {
77open: open_copybit
78};
79
80/*
81 * The COPYBIT Module
82 */
83struct copybit_module_t HAL_MODULE_INFO_SYM = {
84common: {
85tag: HARDWARE_MODULE_TAG,
86 version_major: 1,
87 version_minor: 0,
88 id: COPYBIT_HARDWARE_MODULE_ID,
89 name: "QCT MSM7K COPYBIT Module",
90 author: "Google, Inc.",
91 methods: &copybit_module_methods
92 }
93};
94
95/******************************************************************************/
96
97/** min of int a, b */
98static inline int min(int a, int b) {
99 return (a<b) ? a : b;
100}
101
102/** max of int a, b */
103static inline int max(int a, int b) {
104 return (a>b) ? a : b;
105}
106
107/** scale each parameter by mul/div. Assume div isn't 0 */
108static inline void MULDIV(uint32_t *a, uint32_t *b, int mul, int div) {
109 if (mul != div) {
110 *a = (mul * *a) / div;
111 *b = (mul * *b) / div;
112 }
113}
114
115/** Determine the intersection of lhs & rhs store in out */
116static void intersect(struct copybit_rect_t *out,
117 const struct copybit_rect_t *lhs,
118 const struct copybit_rect_t *rhs) {
119 out->l = max(lhs->l, rhs->l);
120 out->t = max(lhs->t, rhs->t);
121 out->r = min(lhs->r, rhs->r);
122 out->b = min(lhs->b, rhs->b);
123}
124
125/** convert COPYBIT_FORMAT to MDP format */
126static int get_format(int format) {
127 switch (format) {
128 case HAL_PIXEL_FORMAT_RGB_565: return MDP_RGB_565;
129 case HAL_PIXEL_FORMAT_RGBX_8888: return MDP_RGBX_8888;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700130 case HAL_PIXEL_FORMAT_BGRX_8888: return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 case HAL_PIXEL_FORMAT_RGB_888: return MDP_RGB_888;
132 case HAL_PIXEL_FORMAT_RGBA_8888: return MDP_RGBA_8888;
133 case HAL_PIXEL_FORMAT_BGRA_8888: return MDP_BGRA_8888;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700134 case HAL_PIXEL_FORMAT_YCrCb_422_I: return MDP_YCRYCB_H2V1;
135 case HAL_PIXEL_FORMAT_YCbCr_422_I: return MDP_YCBYCR_H2V1;
Raj kamale77f4ec2013-07-03 23:57:50 +0530136 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CRCB_H2V1;
137 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CRCB_H2V2;
138 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CBCR_H2V1;
139 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Radhika Ranjan Soni21175f92013-07-12 17:32:15 +0530141 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800142 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143 }
144 return -1;
145}
146
147/** convert from copybit image to mdp image structure */
148static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
149{
150 private_handle_t* hnd = (private_handle_t*)rhs->handle;
151 if(hnd == NULL){
152 ALOGE("copybit: Invalid handle");
153 return;
154 }
155 img->width = rhs->w;
156 img->height = rhs->h;
157 img->format = get_format(rhs->format);
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530158 img->offset = (uint32_t)hnd->offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700159 img->memory_id = hnd->fd;
160}
161/** setup rectangles */
162static void set_rects(struct copybit_context_t *dev,
163 struct mdp_blit_req *e,
164 const struct copybit_rect_t *dst,
165 const struct copybit_rect_t *src,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530166 const struct copybit_rect_t *scissor) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700167 struct copybit_rect_t clip;
168 intersect(&clip, scissor, dst);
169
170 e->dst_rect.x = clip.l;
171 e->dst_rect.y = clip.t;
172 e->dst_rect.w = clip.r - clip.l;
173 e->dst_rect.h = clip.b - clip.t;
174
175 uint32_t W, H, delta_x, delta_y;
176 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
177 delta_x = (clip.t - dst->t);
178 delta_y = (dst->r - clip.r);
179 e->src_rect.w = (clip.b - clip.t);
180 e->src_rect.h = (clip.r - clip.l);
181 W = dst->b - dst->t;
182 H = dst->r - dst->l;
183 } else {
184 delta_x = (clip.l - dst->l);
185 delta_y = (clip.t - dst->t);
186 e->src_rect.w = (clip.r - clip.l);
187 e->src_rect.h = (clip.b - clip.t);
188 W = dst->r - dst->l;
189 H = dst->b - dst->t;
190 }
191
192 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
193 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
194
195 e->src_rect.x = delta_x + src->l;
196 e->src_rect.y = delta_y + src->t;
197
198 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
199 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
200 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
201 }else{
202 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
203 }
204 }
205
206 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
207 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
208 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
209 }else{
210 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
211 }
212 }
213}
214
215/** setup mdp request */
216static void set_infos(struct copybit_context_t *dev,
217 struct mdp_blit_req *req, int flags)
218{
219 req->alpha = dev->mAlpha;
220 req->transp_mask = MDP_TRANSP_NOP;
221 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700222 // check if we are blitting to f/b
223 if (COPYBIT_ENABLE == dev->mBlitToFB) {
224 req->flags |= MDP_MEMORY_ID_TYPE_FB;
225 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700226#if defined(COPYBIT_QSD8K)
227 req->flags |= MDP_BLEND_FG_PREMULT;
228#endif
229}
230
231/** copy the bits */
232static int msm_copybit(struct copybit_context_t *dev, void const *list)
233{
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400234 int err;
235 if (dev->relFence != -1) {
236 close(dev->relFence);
237 dev->relFence = -1;
238 }
239 err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
Terence Hampsonadf47302013-05-23 12:21:02 -0400240 (struct mdp_async_blit_req_list const*)list);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700241 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
242 if (err == 0) {
243 return 0;
244 } else {
245#if DEBUG_MDP_ERRORS
Terence Hampsonadf47302013-05-23 12:21:02 -0400246 struct mdp_async_blit_req_list const* l =
247 (struct mdp_async_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700248 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700249 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
250 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700251 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700252 ,
253 i,
254 l->req[i].src.width,
255 l->req[i].src.height,
256 l->req[i].src.format,
257 l->req[i].src_rect.x,
258 l->req[i].src_rect.y,
259 l->req[i].src_rect.w,
260 l->req[i].src_rect.h,
261 l->req[i].dst.width,
262 l->req[i].dst.height,
263 l->req[i].dst.format,
264 l->req[i].dst_rect.x,
265 l->req[i].dst_rect.y,
266 l->req[i].dst_rect.w,
267 l->req[i].dst_rect.h,
268 l->req[i].flags
269 );
270 }
271#endif
272 return -errno;
273 }
274}
275
276/*****************************************************************************/
277
278/** Set a parameter to value */
279static int set_parameter_copybit(
280 struct copybit_device_t *dev,
281 int name,
282 int value)
283{
284 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
285 int status = 0;
286 if (ctx) {
287 switch(name) {
288 case COPYBIT_ROTATION_DEG:
289 switch (value) {
290 case 0:
291 ctx->mFlags &= ~0x7;
292 break;
293 case 90:
294 ctx->mFlags &= ~0x7;
295 ctx->mFlags |= MDP_ROT_90;
296 break;
297 case 180:
298 ctx->mFlags &= ~0x7;
299 ctx->mFlags |= MDP_ROT_180;
300 break;
301 case 270:
302 ctx->mFlags &= ~0x7;
303 ctx->mFlags |= MDP_ROT_270;
304 break;
305 default:
306 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
307 status = -EINVAL;
308 break;
309 }
310 break;
311 case COPYBIT_PLANE_ALPHA:
312 if (value < 0) value = MDP_ALPHA_NOP;
313 if (value >= 256) value = 255;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530314 ctx->mAlpha = (uint8_t)value;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700315 break;
316 case COPYBIT_DITHER:
317 if (value == COPYBIT_ENABLE) {
318 ctx->mFlags |= MDP_DITHER;
319 } else if (value == COPYBIT_DISABLE) {
320 ctx->mFlags &= ~MDP_DITHER;
321 }
322 break;
323 case COPYBIT_BLUR:
324 if (value == COPYBIT_ENABLE) {
325 ctx->mFlags |= MDP_BLUR;
326 } else if (value == COPYBIT_DISABLE) {
327 ctx->mFlags &= ~MDP_BLUR;
328 }
329 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800330 case COPYBIT_BLEND_MODE:
331 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800333 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700334 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
335 }
336 break;
337 case COPYBIT_TRANSFORM:
338 ctx->mFlags &= ~0x7;
339 ctx->mFlags |= value & 0x7;
340 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700341 case COPYBIT_BLIT_TO_FRAMEBUFFER:
342 if (COPYBIT_ENABLE == value) {
343 ctx->mBlitToFB = value;
344 } else if (COPYBIT_DISABLE == value) {
345 ctx->mBlitToFB = value;
346 } else {
347 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
348 __FUNCTION__, value);
349 }
350 break;
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530351 case COPYBIT_FG_LAYER:
352 if(value == COPYBIT_ENABLE) {
353 ctx->mFlags |= MDP_IS_FG;
354 } else if (value == COPYBIT_DISABLE) {
355 ctx->mFlags &= ~MDP_IS_FG;
356 }
357 break ;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358 default:
359 status = -EINVAL;
360 break;
361 }
362 } else {
363 status = -EINVAL;
364 }
365 return status;
366}
367
368/** Get a static info value */
369static int get(struct copybit_device_t *dev, int name)
370{
371 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
372 int value;
373 if (ctx) {
374 switch(name) {
375 case COPYBIT_MINIFICATION_LIMIT:
376 value = MAX_SCALE_FACTOR;
377 break;
378 case COPYBIT_MAGNIFICATION_LIMIT:
379 value = MAX_SCALE_FACTOR;
380 break;
381 case COPYBIT_SCALING_FRAC_BITS:
382 value = 32;
383 break;
384 case COPYBIT_ROTATION_STEP_DEG:
385 value = 90;
386 break;
387 default:
388 value = -EINVAL;
389 }
390 } else {
391 value = -EINVAL;
392 }
393 return value;
394}
395
Terence Hampsonadf47302013-05-23 12:21:02 -0400396static int set_sync_copybit(struct copybit_device_t *dev,
397 int acquireFenceFd)
398{
399 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
400 if (acquireFenceFd != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400401 if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
402 ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400403 } else {
404 int ret = -EINVAL;
405 struct blitReq *list = &ctx->list;
406
407 // Since fence is full kick off what is already in the list
408 ret = msm_copybit(ctx, list);
409 if (ret < 0) {
410 ALOGE("%s: Blit call failed", __FUNCTION__);
411 return -EINVAL;
412 }
413 list->count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400414 list->sync.acq_fen_fd_cnt = 0;
415 ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400416 }
417 }
418 return 0;
419}
420
Naseer Ahmed29a26812012-06-14 00:56:20 -0700421/** do a stretch blit type operation */
422static int stretch_copybit(
423 struct copybit_device_t *dev,
424 struct copybit_image_t const *dst,
425 struct copybit_image_t const *src,
426 struct copybit_rect_t const *dst_rect,
427 struct copybit_rect_t const *src_rect,
428 struct copybit_region_t const *region)
429{
430 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Terence Hampsonadf47302013-05-23 12:21:02 -0400431 struct blitReq *list;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700432 int status = 0;
433 private_handle_t *yv12_handle = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700434
Terence Hampsonadf47302013-05-23 12:21:02 -0400435 if (ctx) {
436 list = &ctx->list;
437
Naseer Ahmed29a26812012-06-14 00:56:20 -0700438 if (ctx->mAlpha < 255) {
439 switch (src->format) {
440 // we don't support plane alpha with RGBA formats
441 case HAL_PIXEL_FORMAT_RGBA_8888:
442 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700443 case HAL_PIXEL_FORMAT_RGBA_5551:
444 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700445 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
446 src->format);
447 return -EINVAL;
448 }
449 }
450
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800451 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
452 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700453 // this is always invalid
454 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
455 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
456
457 return -EINVAL;
458 }
459
460 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
461 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
462 return -EINVAL;
463 }
464
465 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
466 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
467 return -EINVAL;
468 }
469
470 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800471 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700472 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700473 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
474 src->format, usage)){
475 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
476 (const_cast<copybit_image_t *>(src))->format =
477 HAL_PIXEL_FORMAT_YCrCb_420_SP;
478 (const_cast<copybit_image_t *>(src))->handle =
479 yv12_handle;
480 (const_cast<copybit_image_t *>(src))->base =
481 (void *)yv12_handle->base;
482 }
483 else{
484 ALOGE("Error copybit conversion from yv12 failed");
485 if(yv12_handle)
486 free_buffer(yv12_handle);
487 return -EINVAL;
488 }
489 }
490 else{
491 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
492 return -EINVAL;
493 }
494 }
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530495 const uint32_t maxCount =
496 (uint32_t)(sizeof(list->req)/sizeof(list->req[0]));
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700497 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700498 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700499 status = 0;
500 while ((status == 0) && region->next(region, &clip)) {
501 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400502 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700503 int flags = 0;
504
505 private_handle_t* src_hnd = (private_handle_t*)src->handle;
506 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
507 flags |= MDP_BLIT_NON_CACHED;
508 }
509
Sushil Chauhan0265c472014-10-01 16:39:02 -0700510 // Set Color Space for MDP to configure CSC matrix
511 req->color_space = ITU_R_601;
512 MetaData_t *metadata = (MetaData_t *)src_hnd->base_metadata;
513 if (metadata && (metadata->operation & UPDATE_COLOR_SPACE)) {
514 req->color_space = metadata->colorSpace;
515 }
516
Naseer Ahmed29a26812012-06-14 00:56:20 -0700517 set_infos(ctx, req, flags);
518 set_image(&req->dst, dst);
519 set_image(&req->src, src);
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530520 set_rects(ctx, req, dst_rect, src_rect, &clip);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700521
522 if (req->src_rect.w<=0 || req->src_rect.h<=0)
523 continue;
524
525 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
526 continue;
527
Terence Hampsonadf47302013-05-23 12:21:02 -0400528 if (++list->count == maxCount) {
529 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400530 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400531 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700532 }
533 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400534 if(yv12_handle) {
535 //Before freeing the buffer we need buffer passed through blit call
536 if (list->count != 0) {
537 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400538 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400539 list->count = 0;
540 }
541 free_buffer(yv12_handle);
542 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700543 } else {
544 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
545 status = -EINVAL;
546 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700547 return status;
548}
549
550/** Perform a blit type operation */
551static int blit_copybit(
552 struct copybit_device_t *dev,
553 struct copybit_image_t const *dst,
554 struct copybit_image_t const *src,
555 struct copybit_region_t const *region)
556{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700557 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
558 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700559 return stretch_copybit(dev, dst, src, &dr, &sr, region);
560}
561
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800562static int finish_copybit(struct copybit_device_t *dev)
563{
564 // NOP for MDP copybit
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530565 if(!dev)
566 return -EINVAL;
567
Terence Hampson0124cc72013-04-18 23:45:56 -0700568 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800569}
Ramakant Singh613e3572013-10-17 15:25:14 +0530570static int clear_copybit(struct copybit_device_t *dev,
571 struct copybit_image_t const *buf,
572 struct copybit_rect_t *rect)
573{
574 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
575 uint32_t color = 0; // black color
576
577 if (!ctx) {
578 ALOGE ("%s: Invalid copybit context", __FUNCTION__);
579 return -EINVAL;
580 }
581
582 struct blitReq list1;
583 memset((char *)&list1 , 0 ,sizeof (struct blitReq) );
584 list1.count = 1;
585 int rel_fen_fd = -1;
586 int my_tmp_get_fence = -1;
587
Terence Hampsonffbcf432013-12-30 17:18:33 -0500588 list1.sync.acq_fen_fd = ctx->acqFence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530589 list1.sync.rel_fen_fd = &my_tmp_get_fence;
Terence Hampsonffbcf432013-12-30 17:18:33 -0500590 list1.sync.acq_fen_fd_cnt = ctx->list.sync.acq_fen_fd_cnt;
Ramakant Singh613e3572013-10-17 15:25:14 +0530591 mdp_blit_req* req = &list1.req[0];
592
593 if(!req) {
594 ALOGE ("%s : Invalid request", __FUNCTION__);
595 return -EINVAL;
596 }
597
598 set_image(&req->dst, buf);
599 set_image(&req->src, buf);
600
601 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > req->dst.width ||
602 rect->t < 0 || (uint32_t)(rect->b - rect->t) > req->dst.height) {
603 ALOGE ("%s : Invalid rect : src_rect l %d t %d r %d b %d",\
604 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
605 return -EINVAL;
606 }
607
608 req->dst_rect.x = rect->l;
609 req->dst_rect.y = rect->t;
610 req->dst_rect.w = rect->r - rect->l;
611 req->dst_rect.h = rect->b - rect->t;
612
613 req->src_rect = req->dst_rect;
614
615 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
616 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
617 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
618 req->const_color.alpha = MDP_ALPHA_NOP;
619
620 req->transp_mask = MDP_TRANSP_NOP;
621 req->flags = MDP_SOLID_FILL | MDP_MEMORY_ID_TYPE_FB | MDP_BLEND_FG_PREMULT;
622 int status = msm_copybit(ctx, &list1);
623
Terence Hampsonffbcf432013-12-30 17:18:33 -0500624 ctx->list.sync.acq_fen_fd_cnt = 0;
Ramakant Singh613e3572013-10-17 15:25:14 +0530625 if (my_tmp_get_fence != -1)
626 close(my_tmp_get_fence);
627
628 return status;
629}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800630
Sushil Chauhan943797c2013-10-21 17:35:55 -0700631/** Fill the rect on dst with RGBA color **/
632static int fill_color(struct copybit_device_t *dev,
633 struct copybit_image_t const *dst,
634 struct copybit_rect_t const *rect,
635 uint32_t color)
636{
637 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
638 if (!ctx) {
639 ALOGE("%s: Invalid copybit context", __FUNCTION__);
640 return -EINVAL;
641 }
642
643 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
644 ALOGE("%s: Invalid DST w=%d h=%d", __FUNCTION__, dst->w, dst->h);
645 return -EINVAL;
646 }
647
648 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > dst->w ||
649 rect->t < 0 || (uint32_t)(rect->b - rect->t) > dst->h) {
650 ALOGE("%s: Invalid destination rect: l=%d t=%d r=%d b=%d",
651 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
652 return -EINVAL;
653 }
654
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800655 int status = 0;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700656 struct blitReq* list = &ctx->list;
657 mdp_blit_req* req = &list->req[list->count++];
658 set_infos(ctx, req, MDP_SOLID_FILL);
659 set_image(&req->src, dst);
660 set_image(&req->dst, dst);
661
662 req->dst_rect.x = rect->l;
663 req->dst_rect.y = rect->t;
664 req->dst_rect.w = rect->r - rect->l;
665 req->dst_rect.h = rect->b - rect->t;
666 req->src_rect = req->dst_rect;
667
668 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
669 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
670 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
671 req->const_color.alpha = (uint32_t)((color >> 24) & 0xff);
672
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800673 if (list->count == sizeof(list->req)/sizeof(list->req[0])) {
674 status = msm_copybit(ctx, list);
675 list->sync.acq_fen_fd_cnt = 0;
676 list->count = 0;
677 }
Sushil Chauhan943797c2013-10-21 17:35:55 -0700678 return status;
679}
680
Naseer Ahmed29a26812012-06-14 00:56:20 -0700681/*****************************************************************************/
682
683/** Close the copybit device */
684static int close_copybit(struct hw_device_t *dev)
685{
686 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
687 if (ctx) {
688 close(ctx->mFD);
689 free(ctx);
690 }
691 return 0;
692}
693
Terence Hampson0124cc72013-04-18 23:45:56 -0700694static int flush_get_fence(struct copybit_device_t *dev, int* fd)
695{
Terence Hampsonadf47302013-05-23 12:21:02 -0400696 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
697 struct blitReq *list = &ctx->list;
698 int ret = -EINVAL;
699
700 if (list->count) {
701 ret = msm_copybit(ctx, list);
702 if (ret < 0)
703 ALOGE("%s: Blit call failed", __FUNCTION__);
704 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400705 }
706 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400707 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400708 ctx->relFence = -1;
709 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700710}
711
Naseer Ahmed29a26812012-06-14 00:56:20 -0700712/** Open a new instance of a copybit device using name */
713static int open_copybit(const struct hw_module_t* module, const char* name,
714 struct hw_device_t** device)
715{
716 int status = -EINVAL;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530717
Dileep Kumar Reddi8ec85af2014-06-26 09:43:49 +0530718 if (strcmp(name, COPYBIT_HARDWARE_COPYBIT0)) {
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530719 return COPYBIT_FAILURE;
720 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700721 copybit_context_t *ctx;
722 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
723 memset(ctx, 0, sizeof(*ctx));
724
725 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
726 ctx->device.common.version = 1;
727 ctx->device.common.module = const_cast<hw_module_t*>(module);
728 ctx->device.common.close = close_copybit;
729 ctx->device.set_parameter = set_parameter_copybit;
730 ctx->device.get = get;
731 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400732 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700733 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800734 ctx->device.finish = finish_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700735 ctx->device.fill_color = fill_color;
Terence Hampson0124cc72013-04-18 23:45:56 -0700736 ctx->device.flush_get_fence = flush_get_fence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530737 ctx->device.clear = clear_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700738 ctx->mAlpha = MDP_ALPHA_NOP;
739 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400740 ctx->sync.flags = 0;
Sushil Chauhan9be65422013-12-02 13:27:53 -0800741 ctx->relFence = -1;
742 for (int i=0; i < MDP_MAX_FENCE_FD; i++) {
743 ctx->acqFence[i] = -1;
744 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400745 ctx->sync.acq_fen_fd = ctx->acqFence;
746 ctx->sync.rel_fen_fd = &ctx->relFence;
747 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400748 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400749 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
750 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700751 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
752 if (ctx->mFD < 0) {
753 status = errno;
754 ALOGE("Error opening frame buffer errno=%d (%s)",
755 status, strerror(status));
756 status = -status;
757 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700758 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700759 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700760 }
761 return status;
762}