blob: 22e6972466b8de542c8a066fdb73e8fcfa079f21 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Tharaga Balachandran4e07cfc2020-01-20 13:48:40 -05003 * Copyright (c) 2010 - 2014, 2020 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 Ahmedfcad05e2018-03-06 20:41:14 -050021#include <log/log.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070022
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;
Ramakant Singh140965d2015-01-08 23:45:04 +053067 uint8_t dynamic_fps;
Naseer Ahmed29a26812012-06-14 00:56:20 -070068};
69
70/**
71 * Common hardware methods
72 */
73
74static int open_copybit(const struct hw_module_t* module, const char* name,
75 struct hw_device_t** device);
76
77static struct hw_module_methods_t copybit_module_methods = {
78open: open_copybit
79};
80
81/*
82 * The COPYBIT Module
83 */
84struct copybit_module_t HAL_MODULE_INFO_SYM = {
85common: {
86tag: HARDWARE_MODULE_TAG,
87 version_major: 1,
88 version_minor: 0,
89 id: COPYBIT_HARDWARE_MODULE_ID,
90 name: "QCT MSM7K COPYBIT Module",
91 author: "Google, Inc.",
92 methods: &copybit_module_methods
93 }
94};
95
96/******************************************************************************/
97
98/** min of int a, b */
99static inline int min(int a, int b) {
100 return (a<b) ? a : b;
101}
102
103/** max of int a, b */
104static inline int max(int a, int b) {
105 return (a>b) ? a : b;
106}
107
108/** scale each parameter by mul/div. Assume div isn't 0 */
109static inline void MULDIV(uint32_t *a, uint32_t *b, int mul, int div) {
110 if (mul != div) {
111 *a = (mul * *a) / div;
112 *b = (mul * *b) / div;
113 }
114}
115
116/** Determine the intersection of lhs & rhs store in out */
117static void intersect(struct copybit_rect_t *out,
118 const struct copybit_rect_t *lhs,
119 const struct copybit_rect_t *rhs) {
120 out->l = max(lhs->l, rhs->l);
121 out->t = max(lhs->t, rhs->t);
122 out->r = min(lhs->r, rhs->r);
123 out->b = min(lhs->b, rhs->b);
124}
125
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530126static bool validateCopybitRect(struct copybit_rect_t *rect) {
127 return ((rect->b > rect->t) && (rect->r > rect->l)) ;
128}
129
Naseer Ahmed29a26812012-06-14 00:56:20 -0700130/** convert COPYBIT_FORMAT to MDP format */
131static int get_format(int format) {
132 switch (format) {
133 case HAL_PIXEL_FORMAT_RGB_565: return MDP_RGB_565;
Ramkumar Radhakrishnanb8eb16d2014-10-09 13:54:07 -0700134 case HAL_PIXEL_FORMAT_RGBA_5551: return MDP_RGBA_5551;
135 case HAL_PIXEL_FORMAT_RGBA_4444: return MDP_RGBA_4444;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700136 case HAL_PIXEL_FORMAT_RGBX_8888: return MDP_RGBX_8888;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700137 case HAL_PIXEL_FORMAT_BGRX_8888: return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 case HAL_PIXEL_FORMAT_RGB_888: return MDP_RGB_888;
139 case HAL_PIXEL_FORMAT_RGBA_8888: return MDP_RGBA_8888;
140 case HAL_PIXEL_FORMAT_BGRA_8888: return MDP_BGRA_8888;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700141 case HAL_PIXEL_FORMAT_YCrCb_422_I: return MDP_YCRYCB_H2V1;
142 case HAL_PIXEL_FORMAT_YCbCr_422_I: return MDP_YCBYCR_H2V1;
Raj kamale77f4ec2013-07-03 23:57:50 +0530143 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CRCB_H2V1;
144 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CRCB_H2V2;
145 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CBCR_H2V1;
146 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700147 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Radhika Ranjan Soni21175f92013-07-12 17:32:15 +0530148 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530149 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: return MDP_Y_CRCB_H2V2_VENUS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800150 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Rahul Sharma61173ea2017-01-05 09:15:40 +0530151 case HAL_PIXEL_FORMAT_CbYCrY_422_I: return MDP_CBYCRY_H2V1;
152 case HAL_PIXEL_FORMAT_BGR_888: return MDP_BGR_888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700153 }
154 return -1;
155}
156
157/** convert from copybit image to mdp image structure */
158static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
159{
160 private_handle_t* hnd = (private_handle_t*)rhs->handle;
161 if(hnd == NULL){
162 ALOGE("copybit: Invalid handle");
163 return;
164 }
165 img->width = rhs->w;
166 img->height = rhs->h;
167 img->format = get_format(rhs->format);
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530168 img->offset = (uint32_t)hnd->offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700169 img->memory_id = hnd->fd;
170}
171/** setup rectangles */
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530172static bool set_rects(struct copybit_context_t *dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173 struct mdp_blit_req *e,
174 const struct copybit_rect_t *dst,
175 const struct copybit_rect_t *src,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530176 const struct copybit_rect_t *scissor) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700177 struct copybit_rect_t clip;
178 intersect(&clip, scissor, dst);
179
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530180 if (!validateCopybitRect(&clip))
181 return false;
182
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183 e->dst_rect.x = clip.l;
184 e->dst_rect.y = clip.t;
185 e->dst_rect.w = clip.r - clip.l;
186 e->dst_rect.h = clip.b - clip.t;
187
188 uint32_t W, H, delta_x, delta_y;
189 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
190 delta_x = (clip.t - dst->t);
191 delta_y = (dst->r - clip.r);
192 e->src_rect.w = (clip.b - clip.t);
193 e->src_rect.h = (clip.r - clip.l);
194 W = dst->b - dst->t;
195 H = dst->r - dst->l;
196 } else {
197 delta_x = (clip.l - dst->l);
198 delta_y = (clip.t - dst->t);
199 e->src_rect.w = (clip.r - clip.l);
200 e->src_rect.h = (clip.b - clip.t);
201 W = dst->r - dst->l;
202 H = dst->b - dst->t;
203 }
204
205 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
206 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
207
208 e->src_rect.x = delta_x + src->l;
209 e->src_rect.y = delta_y + src->t;
210
211 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
212 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
213 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
214 }else{
215 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
216 }
217 }
218
219 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
220 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
221 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
222 }else{
223 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
224 }
225 }
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530226 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227}
228
229/** setup mdp request */
230static void set_infos(struct copybit_context_t *dev,
231 struct mdp_blit_req *req, int flags)
232{
233 req->alpha = dev->mAlpha;
Ramakant Singh140965d2015-01-08 23:45:04 +0530234 req->fps = dev->dynamic_fps;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700235 req->transp_mask = MDP_TRANSP_NOP;
236 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700237 // check if we are blitting to f/b
238 if (COPYBIT_ENABLE == dev->mBlitToFB) {
239 req->flags |= MDP_MEMORY_ID_TYPE_FB;
240 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700241#if defined(COPYBIT_QSD8K)
242 req->flags |= MDP_BLEND_FG_PREMULT;
243#endif
244}
245
246/** copy the bits */
247static int msm_copybit(struct copybit_context_t *dev, void const *list)
248{
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400249 int err;
250 if (dev->relFence != -1) {
251 close(dev->relFence);
252 dev->relFence = -1;
253 }
254 err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
Terence Hampsonadf47302013-05-23 12:21:02 -0400255 (struct mdp_async_blit_req_list const*)list);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700256 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
257 if (err == 0) {
258 return 0;
259 } else {
260#if DEBUG_MDP_ERRORS
Terence Hampsonadf47302013-05-23 12:21:02 -0400261 struct mdp_async_blit_req_list const* l =
262 (struct mdp_async_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700263 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
265 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Ramakant Singh140965d2015-01-08 23:45:04 +0530266 " flags=%08x, fps=%d"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700267 ,
268 i,
269 l->req[i].src.width,
270 l->req[i].src.height,
271 l->req[i].src.format,
272 l->req[i].src_rect.x,
273 l->req[i].src_rect.y,
274 l->req[i].src_rect.w,
275 l->req[i].src_rect.h,
276 l->req[i].dst.width,
277 l->req[i].dst.height,
278 l->req[i].dst.format,
279 l->req[i].dst_rect.x,
280 l->req[i].dst_rect.y,
281 l->req[i].dst_rect.w,
282 l->req[i].dst_rect.h,
Ramakant Singh140965d2015-01-08 23:45:04 +0530283 l->req[i].flags,
284 l->req[i].fps
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285 );
286 }
287#endif
288 return -errno;
289 }
290}
291
292/*****************************************************************************/
293
294/** Set a parameter to value */
295static int set_parameter_copybit(
296 struct copybit_device_t *dev,
297 int name,
298 int value)
299{
300 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
301 int status = 0;
302 if (ctx) {
303 switch(name) {
304 case COPYBIT_ROTATION_DEG:
305 switch (value) {
306 case 0:
307 ctx->mFlags &= ~0x7;
308 break;
309 case 90:
310 ctx->mFlags &= ~0x7;
311 ctx->mFlags |= MDP_ROT_90;
312 break;
313 case 180:
314 ctx->mFlags &= ~0x7;
315 ctx->mFlags |= MDP_ROT_180;
316 break;
317 case 270:
318 ctx->mFlags &= ~0x7;
319 ctx->mFlags |= MDP_ROT_270;
320 break;
321 default:
322 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
323 status = -EINVAL;
324 break;
325 }
326 break;
327 case COPYBIT_PLANE_ALPHA:
328 if (value < 0) value = MDP_ALPHA_NOP;
329 if (value >= 256) value = 255;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530330 ctx->mAlpha = (uint8_t)value;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700331 break;
Ramakant Singh140965d2015-01-08 23:45:04 +0530332 case COPYBIT_DYNAMIC_FPS:
333 ctx->dynamic_fps = (uint8_t)value;
334 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 case COPYBIT_DITHER:
336 if (value == COPYBIT_ENABLE) {
337 ctx->mFlags |= MDP_DITHER;
338 } else if (value == COPYBIT_DISABLE) {
339 ctx->mFlags &= ~MDP_DITHER;
340 }
341 break;
342 case COPYBIT_BLUR:
343 if (value == COPYBIT_ENABLE) {
344 ctx->mFlags |= MDP_BLUR;
345 } else if (value == COPYBIT_DISABLE) {
346 ctx->mFlags &= ~MDP_BLUR;
347 }
348 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800349 case COPYBIT_BLEND_MODE:
350 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700351 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800352 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700353 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
354 }
355 break;
356 case COPYBIT_TRANSFORM:
357 ctx->mFlags &= ~0x7;
358 ctx->mFlags |= value & 0x7;
359 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700360 case COPYBIT_BLIT_TO_FRAMEBUFFER:
361 if (COPYBIT_ENABLE == value) {
362 ctx->mBlitToFB = value;
363 } else if (COPYBIT_DISABLE == value) {
364 ctx->mBlitToFB = value;
365 } else {
366 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
367 __FUNCTION__, value);
368 }
369 break;
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530370 case COPYBIT_FG_LAYER:
371 if(value == COPYBIT_ENABLE) {
372 ctx->mFlags |= MDP_IS_FG;
373 } else if (value == COPYBIT_DISABLE) {
374 ctx->mFlags &= ~MDP_IS_FG;
375 }
376 break ;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700377 default:
378 status = -EINVAL;
379 break;
380 }
381 } else {
382 status = -EINVAL;
383 }
384 return status;
385}
386
387/** Get a static info value */
388static int get(struct copybit_device_t *dev, int name)
389{
390 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
391 int value;
392 if (ctx) {
393 switch(name) {
394 case COPYBIT_MINIFICATION_LIMIT:
395 value = MAX_SCALE_FACTOR;
396 break;
397 case COPYBIT_MAGNIFICATION_LIMIT:
398 value = MAX_SCALE_FACTOR;
399 break;
400 case COPYBIT_SCALING_FRAC_BITS:
401 value = 32;
402 break;
403 case COPYBIT_ROTATION_STEP_DEG:
404 value = 90;
405 break;
406 default:
407 value = -EINVAL;
408 }
409 } else {
410 value = -EINVAL;
411 }
412 return value;
413}
414
Terence Hampsonadf47302013-05-23 12:21:02 -0400415static int set_sync_copybit(struct copybit_device_t *dev,
416 int acquireFenceFd)
417{
418 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
419 if (acquireFenceFd != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400420 if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
421 ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400422 } else {
423 int ret = -EINVAL;
424 struct blitReq *list = &ctx->list;
425
426 // Since fence is full kick off what is already in the list
427 ret = msm_copybit(ctx, list);
428 if (ret < 0) {
429 ALOGE("%s: Blit call failed", __FUNCTION__);
430 return -EINVAL;
431 }
432 list->count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400433 list->sync.acq_fen_fd_cnt = 0;
434 ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400435 }
436 }
437 return 0;
438}
439
Naseer Ahmed29a26812012-06-14 00:56:20 -0700440/** do a stretch blit type operation */
441static int stretch_copybit(
442 struct copybit_device_t *dev,
443 struct copybit_image_t const *dst,
444 struct copybit_image_t const *src,
445 struct copybit_rect_t const *dst_rect,
446 struct copybit_rect_t const *src_rect,
447 struct copybit_region_t const *region)
448{
449 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Terence Hampsonadf47302013-05-23 12:21:02 -0400450 struct blitReq *list;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700451 int status = 0;
452 private_handle_t *yv12_handle = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700453
Terence Hampsonadf47302013-05-23 12:21:02 -0400454 if (ctx) {
455 list = &ctx->list;
456
Naseer Ahmed29a26812012-06-14 00:56:20 -0700457 if (ctx->mAlpha < 255) {
458 switch (src->format) {
459 // we don't support plane alpha with RGBA formats
460 case HAL_PIXEL_FORMAT_RGBA_8888:
461 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700462 case HAL_PIXEL_FORMAT_RGBA_5551:
463 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700464 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
465 src->format);
466 return -EINVAL;
467 }
468 }
469
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800470 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
471 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700472 // this is always invalid
473 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
474 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
475
476 return -EINVAL;
477 }
478
479 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
480 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
481 return -EINVAL;
482 }
483
484 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
485 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
486 return -EINVAL;
487 }
488
489 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800490 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700491 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700492 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
493 src->format, usage)){
494 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
495 (const_cast<copybit_image_t *>(src))->format =
496 HAL_PIXEL_FORMAT_YCrCb_420_SP;
497 (const_cast<copybit_image_t *>(src))->handle =
498 yv12_handle;
499 (const_cast<copybit_image_t *>(src))->base =
500 (void *)yv12_handle->base;
501 }
502 else{
503 ALOGE("Error copybit conversion from yv12 failed");
504 if(yv12_handle)
505 free_buffer(yv12_handle);
506 return -EINVAL;
507 }
508 }
509 else{
510 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
511 return -EINVAL;
512 }
513 }
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530514 const uint32_t maxCount =
515 (uint32_t)(sizeof(list->req)/sizeof(list->req[0]));
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700516 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700517 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 status = 0;
519 while ((status == 0) && region->next(region, &clip)) {
520 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400521 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700522 int flags = 0;
523
524 private_handle_t* src_hnd = (private_handle_t*)src->handle;
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800525 if(src_hnd != NULL &&
526 (!(src_hnd->flags & private_handle_t::PRIV_FLAGS_CACHED))) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700527 flags |= MDP_BLIT_NON_CACHED;
528 }
529
Sushil Chauhan0265c472014-10-01 16:39:02 -0700530 // Set Color Space for MDP to configure CSC matrix
531 req->color_space = ITU_R_601;
Sushil Chauhan0265c472014-10-01 16:39:02 -0700532
Naseer Ahmed29a26812012-06-14 00:56:20 -0700533 set_infos(ctx, req, flags);
534 set_image(&req->dst, dst);
535 set_image(&req->src, src);
Ramakant Singh17fabdb2014-12-15 12:28:01 +0530536 if (set_rects(ctx, req, dst_rect, src_rect, &clip) == false)
537 continue;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538
539 if (req->src_rect.w<=0 || req->src_rect.h<=0)
540 continue;
541
542 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
543 continue;
544
Terence Hampsonadf47302013-05-23 12:21:02 -0400545 if (++list->count == maxCount) {
546 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400547 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400548 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700549 }
550 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400551 if(yv12_handle) {
552 //Before freeing the buffer we need buffer passed through blit call
553 if (list->count != 0) {
554 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400555 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400556 list->count = 0;
557 }
558 free_buffer(yv12_handle);
559 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700560 } else {
561 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
562 status = -EINVAL;
563 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700564 return status;
565}
566
567/** Perform a blit type operation */
568static int blit_copybit(
569 struct copybit_device_t *dev,
570 struct copybit_image_t const *dst,
571 struct copybit_image_t const *src,
572 struct copybit_region_t const *region)
573{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700574 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
575 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700576 return stretch_copybit(dev, dst, src, &dr, &sr, region);
577}
578
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800579static int finish_copybit(struct copybit_device_t *dev)
580{
581 // NOP for MDP copybit
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530582 if(!dev)
583 return -EINVAL;
584
Terence Hampson0124cc72013-04-18 23:45:56 -0700585 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800586}
Ramakant Singh613e3572013-10-17 15:25:14 +0530587static int clear_copybit(struct copybit_device_t *dev,
588 struct copybit_image_t const *buf,
589 struct copybit_rect_t *rect)
590{
591 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
592 uint32_t color = 0; // black color
593
594 if (!ctx) {
595 ALOGE ("%s: Invalid copybit context", __FUNCTION__);
596 return -EINVAL;
597 }
598
599 struct blitReq list1;
600 memset((char *)&list1 , 0 ,sizeof (struct blitReq) );
601 list1.count = 1;
Ramakant Singh613e3572013-10-17 15:25:14 +0530602 int my_tmp_get_fence = -1;
603
Terence Hampsonffbcf432013-12-30 17:18:33 -0500604 list1.sync.acq_fen_fd = ctx->acqFence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530605 list1.sync.rel_fen_fd = &my_tmp_get_fence;
Terence Hampsonffbcf432013-12-30 17:18:33 -0500606 list1.sync.acq_fen_fd_cnt = ctx->list.sync.acq_fen_fd_cnt;
Ramakant Singh613e3572013-10-17 15:25:14 +0530607 mdp_blit_req* req = &list1.req[0];
608
609 if(!req) {
610 ALOGE ("%s : Invalid request", __FUNCTION__);
611 return -EINVAL;
612 }
613
614 set_image(&req->dst, buf);
615 set_image(&req->src, buf);
616
617 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > req->dst.width ||
618 rect->t < 0 || (uint32_t)(rect->b - rect->t) > req->dst.height) {
619 ALOGE ("%s : Invalid rect : src_rect l %d t %d r %d b %d",\
620 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
621 return -EINVAL;
622 }
623
624 req->dst_rect.x = rect->l;
625 req->dst_rect.y = rect->t;
626 req->dst_rect.w = rect->r - rect->l;
627 req->dst_rect.h = rect->b - rect->t;
628
629 req->src_rect = req->dst_rect;
630
631 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
632 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
633 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
634 req->const_color.alpha = MDP_ALPHA_NOP;
635
636 req->transp_mask = MDP_TRANSP_NOP;
637 req->flags = MDP_SOLID_FILL | MDP_MEMORY_ID_TYPE_FB | MDP_BLEND_FG_PREMULT;
638 int status = msm_copybit(ctx, &list1);
639
Terence Hampsonffbcf432013-12-30 17:18:33 -0500640 ctx->list.sync.acq_fen_fd_cnt = 0;
Ramakant Singh613e3572013-10-17 15:25:14 +0530641 if (my_tmp_get_fence != -1)
642 close(my_tmp_get_fence);
643
644 return status;
645}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800646
Sushil Chauhan943797c2013-10-21 17:35:55 -0700647/** Fill the rect on dst with RGBA color **/
648static int fill_color(struct copybit_device_t *dev,
649 struct copybit_image_t const *dst,
650 struct copybit_rect_t const *rect,
651 uint32_t color)
652{
653 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
654 if (!ctx) {
655 ALOGE("%s: Invalid copybit context", __FUNCTION__);
656 return -EINVAL;
657 }
658
659 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
660 ALOGE("%s: Invalid DST w=%d h=%d", __FUNCTION__, dst->w, dst->h);
661 return -EINVAL;
662 }
663
664 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > dst->w ||
665 rect->t < 0 || (uint32_t)(rect->b - rect->t) > dst->h) {
666 ALOGE("%s: Invalid destination rect: l=%d t=%d r=%d b=%d",
667 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
668 return -EINVAL;
669 }
670
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800671 int status = 0;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700672 struct blitReq* list = &ctx->list;
673 mdp_blit_req* req = &list->req[list->count++];
674 set_infos(ctx, req, MDP_SOLID_FILL);
675 set_image(&req->src, dst);
676 set_image(&req->dst, dst);
677
678 req->dst_rect.x = rect->l;
679 req->dst_rect.y = rect->t;
680 req->dst_rect.w = rect->r - rect->l;
681 req->dst_rect.h = rect->b - rect->t;
682 req->src_rect = req->dst_rect;
683
684 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
685 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
686 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
687 req->const_color.alpha = (uint32_t)((color >> 24) & 0xff);
688
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800689 if (list->count == sizeof(list->req)/sizeof(list->req[0])) {
690 status = msm_copybit(ctx, list);
691 list->sync.acq_fen_fd_cnt = 0;
692 list->count = 0;
693 }
Sushil Chauhan943797c2013-10-21 17:35:55 -0700694 return status;
695}
696
Naseer Ahmed29a26812012-06-14 00:56:20 -0700697/*****************************************************************************/
698
699/** Close the copybit device */
700static int close_copybit(struct hw_device_t *dev)
701{
702 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
703 if (ctx) {
704 close(ctx->mFD);
705 free(ctx);
706 }
707 return 0;
708}
709
Terence Hampson0124cc72013-04-18 23:45:56 -0700710static int flush_get_fence(struct copybit_device_t *dev, int* fd)
711{
Terence Hampsonadf47302013-05-23 12:21:02 -0400712 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
713 struct blitReq *list = &ctx->list;
714 int ret = -EINVAL;
715
716 if (list->count) {
717 ret = msm_copybit(ctx, list);
718 if (ret < 0)
719 ALOGE("%s: Blit call failed", __FUNCTION__);
720 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400721 }
722 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400723 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400724 ctx->relFence = -1;
725 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700726}
727
Naseer Ahmed29a26812012-06-14 00:56:20 -0700728/** Open a new instance of a copybit device using name */
729static int open_copybit(const struct hw_module_t* module, const char* name,
730 struct hw_device_t** device)
731{
732 int status = -EINVAL;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530733
Dileep Kumar Reddi8ec85af2014-06-26 09:43:49 +0530734 if (strcmp(name, COPYBIT_HARDWARE_COPYBIT0)) {
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530735 return COPYBIT_FAILURE;
736 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700737 copybit_context_t *ctx;
738 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
Ramakant Singhcab868b2015-02-25 16:19:43 +0530739
740 if (ctx == NULL ) {
741 return COPYBIT_FAILURE;
742 }
743
Naseer Ahmed29a26812012-06-14 00:56:20 -0700744 memset(ctx, 0, sizeof(*ctx));
745
746 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
747 ctx->device.common.version = 1;
748 ctx->device.common.module = const_cast<hw_module_t*>(module);
749 ctx->device.common.close = close_copybit;
750 ctx->device.set_parameter = set_parameter_copybit;
751 ctx->device.get = get;
752 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400753 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700754 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800755 ctx->device.finish = finish_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700756 ctx->device.fill_color = fill_color;
Terence Hampson0124cc72013-04-18 23:45:56 -0700757 ctx->device.flush_get_fence = flush_get_fence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530758 ctx->device.clear = clear_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700759 ctx->mAlpha = MDP_ALPHA_NOP;
Ramakant Singh140965d2015-01-08 23:45:04 +0530760 //dynamic_fps is zero means default
761 //panel refresh rate for driver.
762 ctx->dynamic_fps = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700763 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400764 ctx->sync.flags = 0;
Sushil Chauhan9be65422013-12-02 13:27:53 -0800765 ctx->relFence = -1;
766 for (int i=0; i < MDP_MAX_FENCE_FD; i++) {
767 ctx->acqFence[i] = -1;
768 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400769 ctx->sync.acq_fen_fd = ctx->acqFence;
770 ctx->sync.rel_fen_fd = &ctx->relFence;
771 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400772 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400773 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
774 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700775 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
776 if (ctx->mFD < 0) {
777 status = errno;
778 ALOGE("Error opening frame buffer errno=%d (%s)",
779 status, strerror(status));
780 status = -status;
781 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700782 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700783 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700784 }
785 return status;
786}