blob: b182e4c86d0bf6408d1784cfa69f1f2b857b5d8d [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
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530125static bool validateCopybitRect(struct copybit_rect_t *rect) {
126 return ((rect->b > rect->t) && (rect->r > rect->l)) ;
127}
128
Naseer Ahmed29a26812012-06-14 00:56:20 -0700129/** convert COPYBIT_FORMAT to MDP format */
130static int get_format(int format) {
131 switch (format) {
132 case HAL_PIXEL_FORMAT_RGB_565: return MDP_RGB_565;
Ramkumar Radhakrishnanb8eb16d2014-10-09 13:54:07 -0700133 case HAL_PIXEL_FORMAT_RGBA_5551: return MDP_RGBA_5551;
134 case HAL_PIXEL_FORMAT_RGBA_4444: return MDP_RGBA_4444;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700135 case HAL_PIXEL_FORMAT_RGBX_8888: return MDP_RGBX_8888;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700136 case HAL_PIXEL_FORMAT_BGRX_8888: return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700137 case HAL_PIXEL_FORMAT_RGB_888: return MDP_RGB_888;
138 case HAL_PIXEL_FORMAT_RGBA_8888: return MDP_RGBA_8888;
139 case HAL_PIXEL_FORMAT_BGRA_8888: return MDP_BGRA_8888;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700140 case HAL_PIXEL_FORMAT_YCrCb_422_I: return MDP_YCRYCB_H2V1;
141 case HAL_PIXEL_FORMAT_YCbCr_422_I: return MDP_YCBYCR_H2V1;
Raj kamale77f4ec2013-07-03 23:57:50 +0530142 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CRCB_H2V1;
143 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CRCB_H2V2;
144 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CBCR_H2V1;
145 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700146 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Radhika Ranjan Soni21175f92013-07-12 17:32:15 +0530147 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800148 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700149 }
150 return -1;
151}
152
153/** convert from copybit image to mdp image structure */
154static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
155{
156 private_handle_t* hnd = (private_handle_t*)rhs->handle;
157 if(hnd == NULL){
158 ALOGE("copybit: Invalid handle");
159 return;
160 }
161 img->width = rhs->w;
162 img->height = rhs->h;
163 img->format = get_format(rhs->format);
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530164 img->offset = (uint32_t)hnd->offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700165 img->memory_id = hnd->fd;
166}
167/** setup rectangles */
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530168static bool set_rects(struct copybit_context_t *dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700169 struct mdp_blit_req *e,
170 const struct copybit_rect_t *dst,
171 const struct copybit_rect_t *src,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530172 const struct copybit_rect_t *scissor) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173 struct copybit_rect_t clip;
174 intersect(&clip, scissor, dst);
175
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530176 if (!validateCopybitRect(&clip))
177 return false;
178
Naseer Ahmed29a26812012-06-14 00:56:20 -0700179 e->dst_rect.x = clip.l;
180 e->dst_rect.y = clip.t;
181 e->dst_rect.w = clip.r - clip.l;
182 e->dst_rect.h = clip.b - clip.t;
183
184 uint32_t W, H, delta_x, delta_y;
185 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
186 delta_x = (clip.t - dst->t);
187 delta_y = (dst->r - clip.r);
188 e->src_rect.w = (clip.b - clip.t);
189 e->src_rect.h = (clip.r - clip.l);
190 W = dst->b - dst->t;
191 H = dst->r - dst->l;
192 } else {
193 delta_x = (clip.l - dst->l);
194 delta_y = (clip.t - dst->t);
195 e->src_rect.w = (clip.r - clip.l);
196 e->src_rect.h = (clip.b - clip.t);
197 W = dst->r - dst->l;
198 H = dst->b - dst->t;
199 }
200
201 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
202 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
203
204 e->src_rect.x = delta_x + src->l;
205 e->src_rect.y = delta_y + src->t;
206
207 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
208 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
209 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
210 }else{
211 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
212 }
213 }
214
215 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
216 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
217 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
218 }else{
219 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
220 }
221 }
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530222 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700223}
224
225/** setup mdp request */
226static void set_infos(struct copybit_context_t *dev,
227 struct mdp_blit_req *req, int flags)
228{
229 req->alpha = dev->mAlpha;
230 req->transp_mask = MDP_TRANSP_NOP;
231 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700232 // check if we are blitting to f/b
233 if (COPYBIT_ENABLE == dev->mBlitToFB) {
234 req->flags |= MDP_MEMORY_ID_TYPE_FB;
235 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700236#if defined(COPYBIT_QSD8K)
237 req->flags |= MDP_BLEND_FG_PREMULT;
238#endif
239}
240
241/** copy the bits */
242static int msm_copybit(struct copybit_context_t *dev, void const *list)
243{
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400244 int err;
245 if (dev->relFence != -1) {
246 close(dev->relFence);
247 dev->relFence = -1;
248 }
249 err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
Terence Hampsonadf47302013-05-23 12:21:02 -0400250 (struct mdp_async_blit_req_list const*)list);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700251 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
252 if (err == 0) {
253 return 0;
254 } else {
255#if DEBUG_MDP_ERRORS
Terence Hampsonadf47302013-05-23 12:21:02 -0400256 struct mdp_async_blit_req_list const* l =
257 (struct mdp_async_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700258 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700259 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
260 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700261 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700262 ,
263 i,
264 l->req[i].src.width,
265 l->req[i].src.height,
266 l->req[i].src.format,
267 l->req[i].src_rect.x,
268 l->req[i].src_rect.y,
269 l->req[i].src_rect.w,
270 l->req[i].src_rect.h,
271 l->req[i].dst.width,
272 l->req[i].dst.height,
273 l->req[i].dst.format,
274 l->req[i].dst_rect.x,
275 l->req[i].dst_rect.y,
276 l->req[i].dst_rect.w,
277 l->req[i].dst_rect.h,
278 l->req[i].flags
279 );
280 }
281#endif
282 return -errno;
283 }
284}
285
286/*****************************************************************************/
287
288/** Set a parameter to value */
289static int set_parameter_copybit(
290 struct copybit_device_t *dev,
291 int name,
292 int value)
293{
294 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
295 int status = 0;
296 if (ctx) {
297 switch(name) {
298 case COPYBIT_ROTATION_DEG:
299 switch (value) {
300 case 0:
301 ctx->mFlags &= ~0x7;
302 break;
303 case 90:
304 ctx->mFlags &= ~0x7;
305 ctx->mFlags |= MDP_ROT_90;
306 break;
307 case 180:
308 ctx->mFlags &= ~0x7;
309 ctx->mFlags |= MDP_ROT_180;
310 break;
311 case 270:
312 ctx->mFlags &= ~0x7;
313 ctx->mFlags |= MDP_ROT_270;
314 break;
315 default:
316 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
317 status = -EINVAL;
318 break;
319 }
320 break;
321 case COPYBIT_PLANE_ALPHA:
322 if (value < 0) value = MDP_ALPHA_NOP;
323 if (value >= 256) value = 255;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530324 ctx->mAlpha = (uint8_t)value;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 break;
326 case COPYBIT_DITHER:
327 if (value == COPYBIT_ENABLE) {
328 ctx->mFlags |= MDP_DITHER;
329 } else if (value == COPYBIT_DISABLE) {
330 ctx->mFlags &= ~MDP_DITHER;
331 }
332 break;
333 case COPYBIT_BLUR:
334 if (value == COPYBIT_ENABLE) {
335 ctx->mFlags |= MDP_BLUR;
336 } else if (value == COPYBIT_DISABLE) {
337 ctx->mFlags &= ~MDP_BLUR;
338 }
339 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800340 case COPYBIT_BLEND_MODE:
341 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800343 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
345 }
346 break;
347 case COPYBIT_TRANSFORM:
348 ctx->mFlags &= ~0x7;
349 ctx->mFlags |= value & 0x7;
350 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700351 case COPYBIT_BLIT_TO_FRAMEBUFFER:
352 if (COPYBIT_ENABLE == value) {
353 ctx->mBlitToFB = value;
354 } else if (COPYBIT_DISABLE == value) {
355 ctx->mBlitToFB = value;
356 } else {
357 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
358 __FUNCTION__, value);
359 }
360 break;
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530361 case COPYBIT_FG_LAYER:
362 if(value == COPYBIT_ENABLE) {
363 ctx->mFlags |= MDP_IS_FG;
364 } else if (value == COPYBIT_DISABLE) {
365 ctx->mFlags &= ~MDP_IS_FG;
366 }
367 break ;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700368 default:
369 status = -EINVAL;
370 break;
371 }
372 } else {
373 status = -EINVAL;
374 }
375 return status;
376}
377
378/** Get a static info value */
379static int get(struct copybit_device_t *dev, int name)
380{
381 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
382 int value;
383 if (ctx) {
384 switch(name) {
385 case COPYBIT_MINIFICATION_LIMIT:
386 value = MAX_SCALE_FACTOR;
387 break;
388 case COPYBIT_MAGNIFICATION_LIMIT:
389 value = MAX_SCALE_FACTOR;
390 break;
391 case COPYBIT_SCALING_FRAC_BITS:
392 value = 32;
393 break;
394 case COPYBIT_ROTATION_STEP_DEG:
395 value = 90;
396 break;
397 default:
398 value = -EINVAL;
399 }
400 } else {
401 value = -EINVAL;
402 }
403 return value;
404}
405
Terence Hampsonadf47302013-05-23 12:21:02 -0400406static int set_sync_copybit(struct copybit_device_t *dev,
407 int acquireFenceFd)
408{
409 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
410 if (acquireFenceFd != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400411 if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
412 ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400413 } else {
414 int ret = -EINVAL;
415 struct blitReq *list = &ctx->list;
416
417 // Since fence is full kick off what is already in the list
418 ret = msm_copybit(ctx, list);
419 if (ret < 0) {
420 ALOGE("%s: Blit call failed", __FUNCTION__);
421 return -EINVAL;
422 }
423 list->count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400424 list->sync.acq_fen_fd_cnt = 0;
425 ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400426 }
427 }
428 return 0;
429}
430
Naseer Ahmed29a26812012-06-14 00:56:20 -0700431/** do a stretch blit type operation */
432static int stretch_copybit(
433 struct copybit_device_t *dev,
434 struct copybit_image_t const *dst,
435 struct copybit_image_t const *src,
436 struct copybit_rect_t const *dst_rect,
437 struct copybit_rect_t const *src_rect,
438 struct copybit_region_t const *region)
439{
440 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Terence Hampsonadf47302013-05-23 12:21:02 -0400441 struct blitReq *list;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700442 int status = 0;
443 private_handle_t *yv12_handle = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700444
Terence Hampsonadf47302013-05-23 12:21:02 -0400445 if (ctx) {
446 list = &ctx->list;
447
Naseer Ahmed29a26812012-06-14 00:56:20 -0700448 if (ctx->mAlpha < 255) {
449 switch (src->format) {
450 // we don't support plane alpha with RGBA formats
451 case HAL_PIXEL_FORMAT_RGBA_8888:
452 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700453 case HAL_PIXEL_FORMAT_RGBA_5551:
454 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700455 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
456 src->format);
457 return -EINVAL;
458 }
459 }
460
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800461 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
462 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700463 // this is always invalid
464 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
465 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
466
467 return -EINVAL;
468 }
469
470 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
471 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
472 return -EINVAL;
473 }
474
475 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
476 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
477 return -EINVAL;
478 }
479
480 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800481 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700482 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700483 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
484 src->format, usage)){
485 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
486 (const_cast<copybit_image_t *>(src))->format =
487 HAL_PIXEL_FORMAT_YCrCb_420_SP;
488 (const_cast<copybit_image_t *>(src))->handle =
489 yv12_handle;
490 (const_cast<copybit_image_t *>(src))->base =
491 (void *)yv12_handle->base;
492 }
493 else{
494 ALOGE("Error copybit conversion from yv12 failed");
495 if(yv12_handle)
496 free_buffer(yv12_handle);
497 return -EINVAL;
498 }
499 }
500 else{
501 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
502 return -EINVAL;
503 }
504 }
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530505 const uint32_t maxCount =
506 (uint32_t)(sizeof(list->req)/sizeof(list->req[0]));
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700507 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700508 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700509 status = 0;
510 while ((status == 0) && region->next(region, &clip)) {
511 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400512 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700513 int flags = 0;
514
515 private_handle_t* src_hnd = (private_handle_t*)src->handle;
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800516 if(src_hnd != NULL &&
517 (!(src_hnd->flags & private_handle_t::PRIV_FLAGS_CACHED))) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 flags |= MDP_BLIT_NON_CACHED;
519 }
520
Sushil Chauhan0265c472014-10-01 16:39:02 -0700521 // Set Color Space for MDP to configure CSC matrix
522 req->color_space = ITU_R_601;
523 MetaData_t *metadata = (MetaData_t *)src_hnd->base_metadata;
524 if (metadata && (metadata->operation & UPDATE_COLOR_SPACE)) {
525 req->color_space = metadata->colorSpace;
526 }
527
Naseer Ahmed29a26812012-06-14 00:56:20 -0700528 set_infos(ctx, req, flags);
529 set_image(&req->dst, dst);
530 set_image(&req->src, src);
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530531 if (set_rects(ctx, req, dst_rect, src_rect, &clip) == false)
532 continue;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700533
534 if (req->src_rect.w<=0 || req->src_rect.h<=0)
535 continue;
536
537 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
538 continue;
539
Terence Hampsonadf47302013-05-23 12:21:02 -0400540 if (++list->count == maxCount) {
541 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400542 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400543 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700544 }
545 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400546 if(yv12_handle) {
547 //Before freeing the buffer we need buffer passed through blit call
548 if (list->count != 0) {
549 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400550 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400551 list->count = 0;
552 }
553 free_buffer(yv12_handle);
554 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700555 } else {
556 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
557 status = -EINVAL;
558 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700559 return status;
560}
561
562/** Perform a blit type operation */
563static int blit_copybit(
564 struct copybit_device_t *dev,
565 struct copybit_image_t const *dst,
566 struct copybit_image_t const *src,
567 struct copybit_region_t const *region)
568{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700569 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
570 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700571 return stretch_copybit(dev, dst, src, &dr, &sr, region);
572}
573
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800574static int finish_copybit(struct copybit_device_t *dev)
575{
576 // NOP for MDP copybit
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530577 if(!dev)
578 return -EINVAL;
579
Terence Hampson0124cc72013-04-18 23:45:56 -0700580 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800581}
Ramakant Singh613e3572013-10-17 15:25:14 +0530582static int clear_copybit(struct copybit_device_t *dev,
583 struct copybit_image_t const *buf,
584 struct copybit_rect_t *rect)
585{
586 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
587 uint32_t color = 0; // black color
588
589 if (!ctx) {
590 ALOGE ("%s: Invalid copybit context", __FUNCTION__);
591 return -EINVAL;
592 }
593
594 struct blitReq list1;
595 memset((char *)&list1 , 0 ,sizeof (struct blitReq) );
596 list1.count = 1;
Ramakant Singh613e3572013-10-17 15:25:14 +0530597 int my_tmp_get_fence = -1;
598
Terence Hampsonffbcf432013-12-30 17:18:33 -0500599 list1.sync.acq_fen_fd = ctx->acqFence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530600 list1.sync.rel_fen_fd = &my_tmp_get_fence;
Terence Hampsonffbcf432013-12-30 17:18:33 -0500601 list1.sync.acq_fen_fd_cnt = ctx->list.sync.acq_fen_fd_cnt;
Ramakant Singh613e3572013-10-17 15:25:14 +0530602 mdp_blit_req* req = &list1.req[0];
603
604 if(!req) {
605 ALOGE ("%s : Invalid request", __FUNCTION__);
606 return -EINVAL;
607 }
608
609 set_image(&req->dst, buf);
610 set_image(&req->src, buf);
611
612 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > req->dst.width ||
613 rect->t < 0 || (uint32_t)(rect->b - rect->t) > req->dst.height) {
614 ALOGE ("%s : Invalid rect : src_rect l %d t %d r %d b %d",\
615 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
616 return -EINVAL;
617 }
618
619 req->dst_rect.x = rect->l;
620 req->dst_rect.y = rect->t;
621 req->dst_rect.w = rect->r - rect->l;
622 req->dst_rect.h = rect->b - rect->t;
623
624 req->src_rect = req->dst_rect;
625
626 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
627 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
628 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
629 req->const_color.alpha = MDP_ALPHA_NOP;
630
631 req->transp_mask = MDP_TRANSP_NOP;
632 req->flags = MDP_SOLID_FILL | MDP_MEMORY_ID_TYPE_FB | MDP_BLEND_FG_PREMULT;
633 int status = msm_copybit(ctx, &list1);
634
Terence Hampsonffbcf432013-12-30 17:18:33 -0500635 ctx->list.sync.acq_fen_fd_cnt = 0;
Ramakant Singh613e3572013-10-17 15:25:14 +0530636 if (my_tmp_get_fence != -1)
637 close(my_tmp_get_fence);
638
639 return status;
640}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800641
Sushil Chauhan943797c2013-10-21 17:35:55 -0700642/** Fill the rect on dst with RGBA color **/
643static int fill_color(struct copybit_device_t *dev,
644 struct copybit_image_t const *dst,
645 struct copybit_rect_t const *rect,
646 uint32_t color)
647{
648 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
649 if (!ctx) {
650 ALOGE("%s: Invalid copybit context", __FUNCTION__);
651 return -EINVAL;
652 }
653
654 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
655 ALOGE("%s: Invalid DST w=%d h=%d", __FUNCTION__, dst->w, dst->h);
656 return -EINVAL;
657 }
658
659 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > dst->w ||
660 rect->t < 0 || (uint32_t)(rect->b - rect->t) > dst->h) {
661 ALOGE("%s: Invalid destination rect: l=%d t=%d r=%d b=%d",
662 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
663 return -EINVAL;
664 }
665
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800666 int status = 0;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700667 struct blitReq* list = &ctx->list;
668 mdp_blit_req* req = &list->req[list->count++];
669 set_infos(ctx, req, MDP_SOLID_FILL);
670 set_image(&req->src, dst);
671 set_image(&req->dst, dst);
672
673 req->dst_rect.x = rect->l;
674 req->dst_rect.y = rect->t;
675 req->dst_rect.w = rect->r - rect->l;
676 req->dst_rect.h = rect->b - rect->t;
677 req->src_rect = req->dst_rect;
678
679 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
680 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
681 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
682 req->const_color.alpha = (uint32_t)((color >> 24) & 0xff);
683
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800684 if (list->count == sizeof(list->req)/sizeof(list->req[0])) {
685 status = msm_copybit(ctx, list);
686 list->sync.acq_fen_fd_cnt = 0;
687 list->count = 0;
688 }
Sushil Chauhan943797c2013-10-21 17:35:55 -0700689 return status;
690}
691
Naseer Ahmed29a26812012-06-14 00:56:20 -0700692/*****************************************************************************/
693
694/** Close the copybit device */
695static int close_copybit(struct hw_device_t *dev)
696{
697 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
698 if (ctx) {
699 close(ctx->mFD);
700 free(ctx);
701 }
702 return 0;
703}
704
Terence Hampson0124cc72013-04-18 23:45:56 -0700705static int flush_get_fence(struct copybit_device_t *dev, int* fd)
706{
Terence Hampsonadf47302013-05-23 12:21:02 -0400707 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
708 struct blitReq *list = &ctx->list;
709 int ret = -EINVAL;
710
711 if (list->count) {
712 ret = msm_copybit(ctx, list);
713 if (ret < 0)
714 ALOGE("%s: Blit call failed", __FUNCTION__);
715 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400716 }
717 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400718 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400719 ctx->relFence = -1;
720 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700721}
722
Naseer Ahmed29a26812012-06-14 00:56:20 -0700723/** Open a new instance of a copybit device using name */
724static int open_copybit(const struct hw_module_t* module, const char* name,
725 struct hw_device_t** device)
726{
727 int status = -EINVAL;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530728
Dileep Kumar Reddi8ec85af2014-06-26 09:43:49 +0530729 if (strcmp(name, COPYBIT_HARDWARE_COPYBIT0)) {
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530730 return COPYBIT_FAILURE;
731 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700732 copybit_context_t *ctx;
733 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
734 memset(ctx, 0, sizeof(*ctx));
735
736 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
737 ctx->device.common.version = 1;
738 ctx->device.common.module = const_cast<hw_module_t*>(module);
739 ctx->device.common.close = close_copybit;
740 ctx->device.set_parameter = set_parameter_copybit;
741 ctx->device.get = get;
742 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400743 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700744 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800745 ctx->device.finish = finish_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700746 ctx->device.fill_color = fill_color;
Terence Hampson0124cc72013-04-18 23:45:56 -0700747 ctx->device.flush_get_fence = flush_get_fence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530748 ctx->device.clear = clear_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700749 ctx->mAlpha = MDP_ALPHA_NOP;
750 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400751 ctx->sync.flags = 0;
Sushil Chauhan9be65422013-12-02 13:27:53 -0800752 ctx->relFence = -1;
753 for (int i=0; i < MDP_MAX_FENCE_FD; i++) {
754 ctx->acqFence[i] = -1;
755 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400756 ctx->sync.acq_fen_fd = ctx->acqFence;
757 ctx->sync.rel_fen_fd = &ctx->relFence;
758 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400759 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400760 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
761 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700762 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
763 if (ctx->mFD < 0) {
764 status = errno;
765 ALOGE("Error opening frame buffer errno=%d (%s)",
766 status, strerror(status));
767 status = -status;
768 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700769 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700770 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700771 }
772 return status;
773}