blob: 0811c35bc430d84e8b9c467d767724d47b8f6498 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08003 * 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.
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"
40
41#define DEBUG_MDP_ERRORS 1
42
43/******************************************************************************/
44
Naseer Ahmed29a26812012-06-14 00:56:20 -070045#define MAX_SCALE_FACTOR (4)
46#define MAX_DIMENSION (4096)
Naseer Ahmed29a26812012-06-14 00:56:20 -070047
48/******************************************************************************/
Terence Hampsonadf47302013-05-23 12:21:02 -040049struct blitReq{
50 struct mdp_buf_sync sync;
51 uint32_t count;
52 struct mdp_blit_req req[10];
53};
Naseer Ahmed29a26812012-06-14 00:56:20 -070054
55/** State information for each device instance */
56struct copybit_context_t {
57 struct copybit_device_t device;
58 int mFD;
59 uint8_t mAlpha;
60 int mFlags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070061 bool mBlitToFB;
Terence Hampsonadf47302013-05-23 12:21:02 -040062 int acqFence[MDP_MAX_FENCE_FD];
63 int relFence;
64 struct mdp_buf_sync sync;
65 struct blitReq list;
Naseer Ahmed29a26812012-06-14 00:56:20 -070066};
67
68/**
69 * Common hardware methods
70 */
71
72static int open_copybit(const struct hw_module_t* module, const char* name,
73 struct hw_device_t** device);
74
75static struct hw_module_methods_t copybit_module_methods = {
vasy952f1d42015-11-17 15:26:37 +030076 .open = open_copybit
Naseer Ahmed29a26812012-06-14 00:56:20 -070077};
78
79/*
80 * The COPYBIT Module
81 */
82struct copybit_module_t HAL_MODULE_INFO_SYM = {
vasy952f1d42015-11-17 15:26:37 +030083 .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 }
Naseer Ahmed29a26812012-06-14 00:56:20 -070092};
93
94/******************************************************************************/
95
96/** min of int a, b */
97static inline int min(int a, int b) {
98 return (a<b) ? a : b;
99}
100
101/** max of int a, b */
102static 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 */
107static 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 */
115static 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 */
125static 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;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700129 case HAL_PIXEL_FORMAT_BGRX_8888: return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700130 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;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700133 case HAL_PIXEL_FORMAT_YCrCb_422_I: return MDP_YCRYCB_H2V1;
134 case HAL_PIXEL_FORMAT_YCbCr_422_I: return MDP_YCBYCR_H2V1;
Raj kamale77f4ec2013-07-03 23:57:50 +0530135 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;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700139 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Radhika Ranjan Soni21175f92013-07-12 17:32:15 +0530140 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800141 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700142 }
143 return -1;
144}
145
146/** convert from copybit image to mdp image structure */
147static 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 */
161static 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 */
217static 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;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700223 // check if we are blitting to f/b
224 if (COPYBIT_ENABLE == dev->mBlitToFB) {
225 req->flags |= MDP_MEMORY_ID_TYPE_FB;
226 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227#if defined(COPYBIT_QSD8K)
228 req->flags |= MDP_BLEND_FG_PREMULT;
229#endif
230}
231
232/** copy the bits */
233static int msm_copybit(struct copybit_context_t *dev, void const *list)
234{
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400235 int err;
236 if (dev->relFence != -1) {
237 close(dev->relFence);
238 dev->relFence = -1;
239 }
240 err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
Terence Hampsonadf47302013-05-23 12:21:02 -0400241 (struct mdp_async_blit_req_list const*)list);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700242 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
243 if (err == 0) {
244 return 0;
245 } else {
246#if DEBUG_MDP_ERRORS
Terence Hampsonadf47302013-05-23 12:21:02 -0400247 struct mdp_async_blit_req_list const* l =
248 (struct mdp_async_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700249 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700250 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"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700252 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700253 ,
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 */
280static 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;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800331 case COPYBIT_BLEND_MODE:
332 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700333 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800334 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
336 }
337 break;
338 case COPYBIT_TRANSFORM:
339 ctx->mFlags &= ~0x7;
340 ctx->mFlags |= value & 0x7;
341 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700342 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;
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530352 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 ;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700359 default:
360 status = -EINVAL;
361 break;
362 }
363 } else {
364 status = -EINVAL;
365 }
366 return status;
367}
368
369/** Get a static info value */
370static 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
Terence Hampsonadf47302013-05-23 12:21:02 -0400397static 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) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400402 if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
403 ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400404 } 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;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400415 list->sync.acq_fen_fd_cnt = 0;
416 ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400417 }
418 }
419 return 0;
420}
421
Naseer Ahmed29a26812012-06-14 00:56:20 -0700422/** do a stretch blit type operation */
423static 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;
Terence Hampsonadf47302013-05-23 12:21:02 -0400432 struct blitReq *list;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700433 int status = 0;
434 private_handle_t *yv12_handle = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700435
Terence Hampsonadf47302013-05-23 12:21:02 -0400436 if (ctx) {
437 list = &ctx->list;
438
Naseer Ahmed29a26812012-06-14 00:56:20 -0700439 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:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700444 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
445 src->format);
446 return -EINVAL;
447 }
448 }
449
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800450 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
451 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700452 // 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) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800470 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700471 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700472 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 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400494 const uint32_t maxCount = sizeof(list->req)/sizeof(list->req[0]);
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700495 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700496 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700497 status = 0;
498 while ((status == 0) && region->next(region, &clip)) {
499 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400500 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700501 int flags = 0;
502
503 private_handle_t* src_hnd = (private_handle_t*)src->handle;
Saurabh Shah74f3cb92014-12-19 10:05:41 -0800504 if(src_hnd != NULL &&
505 (!(src_hnd->flags & private_handle_t::PRIV_FLAGS_CACHED))) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700506 flags |= MDP_BLIT_NON_CACHED;
507 }
508
509 set_infos(ctx, req, flags);
510 set_image(&req->dst, dst);
511 set_image(&req->src, src);
512 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
513
514 if (req->src_rect.w<=0 || req->src_rect.h<=0)
515 continue;
516
517 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
518 continue;
519
Terence Hampsonadf47302013-05-23 12:21:02 -0400520 if (++list->count == maxCount) {
521 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400522 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400523 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700524 }
525 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400526 if(yv12_handle) {
527 //Before freeing the buffer we need buffer passed through blit call
528 if (list->count != 0) {
529 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400530 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400531 list->count = 0;
532 }
533 free_buffer(yv12_handle);
534 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700535 } else {
536 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
537 status = -EINVAL;
538 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700539 return status;
540}
541
542/** Perform a blit type operation */
543static int blit_copybit(
544 struct copybit_device_t *dev,
545 struct copybit_image_t const *dst,
546 struct copybit_image_t const *src,
547 struct copybit_region_t const *region)
548{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700549 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
550 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700551 return stretch_copybit(dev, dst, src, &dr, &sr, region);
552}
553
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800554static int finish_copybit(struct copybit_device_t *dev)
555{
556 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700557 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800558}
Ramakant Singh613e3572013-10-17 15:25:14 +0530559static int clear_copybit(struct copybit_device_t *dev,
560 struct copybit_image_t const *buf,
561 struct copybit_rect_t *rect)
562{
563 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
564 uint32_t color = 0; // black color
565
566 if (!ctx) {
567 ALOGE ("%s: Invalid copybit context", __FUNCTION__);
568 return -EINVAL;
569 }
570
571 struct blitReq list1;
572 memset((char *)&list1 , 0 ,sizeof (struct blitReq) );
573 list1.count = 1;
574 int rel_fen_fd = -1;
575 int my_tmp_get_fence = -1;
576
Terence Hampsonffbcf432013-12-30 17:18:33 -0500577 list1.sync.acq_fen_fd = ctx->acqFence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530578 list1.sync.rel_fen_fd = &my_tmp_get_fence;
Terence Hampsonffbcf432013-12-30 17:18:33 -0500579 list1.sync.acq_fen_fd_cnt = ctx->list.sync.acq_fen_fd_cnt;
Ramakant Singh613e3572013-10-17 15:25:14 +0530580 mdp_blit_req* req = &list1.req[0];
581
582 if(!req) {
583 ALOGE ("%s : Invalid request", __FUNCTION__);
584 return -EINVAL;
585 }
586
587 set_image(&req->dst, buf);
588 set_image(&req->src, buf);
589
590 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > req->dst.width ||
591 rect->t < 0 || (uint32_t)(rect->b - rect->t) > req->dst.height) {
592 ALOGE ("%s : Invalid rect : src_rect l %d t %d r %d b %d",\
593 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
594 return -EINVAL;
595 }
596
597 req->dst_rect.x = rect->l;
598 req->dst_rect.y = rect->t;
599 req->dst_rect.w = rect->r - rect->l;
600 req->dst_rect.h = rect->b - rect->t;
601
602 req->src_rect = req->dst_rect;
603
604 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
605 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
606 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
607 req->const_color.alpha = MDP_ALPHA_NOP;
608
609 req->transp_mask = MDP_TRANSP_NOP;
610 req->flags = MDP_SOLID_FILL | MDP_MEMORY_ID_TYPE_FB | MDP_BLEND_FG_PREMULT;
611 int status = msm_copybit(ctx, &list1);
612
Terence Hampsonffbcf432013-12-30 17:18:33 -0500613 ctx->list.sync.acq_fen_fd_cnt = 0;
Ramakant Singh613e3572013-10-17 15:25:14 +0530614 if (my_tmp_get_fence != -1)
615 close(my_tmp_get_fence);
616
617 return status;
618}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800619
Sushil Chauhan943797c2013-10-21 17:35:55 -0700620/** Fill the rect on dst with RGBA color **/
621static int fill_color(struct copybit_device_t *dev,
622 struct copybit_image_t const *dst,
623 struct copybit_rect_t const *rect,
624 uint32_t color)
625{
626 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
627 if (!ctx) {
628 ALOGE("%s: Invalid copybit context", __FUNCTION__);
629 return -EINVAL;
630 }
631
632 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
633 ALOGE("%s: Invalid DST w=%d h=%d", __FUNCTION__, dst->w, dst->h);
634 return -EINVAL;
635 }
636
637 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > dst->w ||
638 rect->t < 0 || (uint32_t)(rect->b - rect->t) > dst->h) {
639 ALOGE("%s: Invalid destination rect: l=%d t=%d r=%d b=%d",
640 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
641 return -EINVAL;
642 }
643
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800644 int status = 0;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700645 struct blitReq* list = &ctx->list;
646 mdp_blit_req* req = &list->req[list->count++];
647 set_infos(ctx, req, MDP_SOLID_FILL);
648 set_image(&req->src, dst);
649 set_image(&req->dst, dst);
650
651 req->dst_rect.x = rect->l;
652 req->dst_rect.y = rect->t;
653 req->dst_rect.w = rect->r - rect->l;
654 req->dst_rect.h = rect->b - rect->t;
655 req->src_rect = req->dst_rect;
656
657 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
658 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
659 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
660 req->const_color.alpha = (uint32_t)((color >> 24) & 0xff);
661
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800662 if (list->count == sizeof(list->req)/sizeof(list->req[0])) {
663 status = msm_copybit(ctx, list);
664 list->sync.acq_fen_fd_cnt = 0;
665 list->count = 0;
666 }
Sushil Chauhan943797c2013-10-21 17:35:55 -0700667 return status;
668}
669
Naseer Ahmed29a26812012-06-14 00:56:20 -0700670/*****************************************************************************/
671
672/** Close the copybit device */
673static int close_copybit(struct hw_device_t *dev)
674{
675 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
676 if (ctx) {
677 close(ctx->mFD);
678 free(ctx);
679 }
680 return 0;
681}
682
Terence Hampson0124cc72013-04-18 23:45:56 -0700683static int flush_get_fence(struct copybit_device_t *dev, int* fd)
684{
Terence Hampsonadf47302013-05-23 12:21:02 -0400685 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
686 struct blitReq *list = &ctx->list;
687 int ret = -EINVAL;
688
689 if (list->count) {
690 ret = msm_copybit(ctx, list);
691 if (ret < 0)
692 ALOGE("%s: Blit call failed", __FUNCTION__);
693 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400694 }
695 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400696 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400697 ctx->relFence = -1;
698 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700699}
700
Naseer Ahmed29a26812012-06-14 00:56:20 -0700701/** Open a new instance of a copybit device using name */
702static int open_copybit(const struct hw_module_t* module, const char* name,
703 struct hw_device_t** device)
704{
705 int status = -EINVAL;
706 copybit_context_t *ctx;
707 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
708 memset(ctx, 0, sizeof(*ctx));
709
710 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
711 ctx->device.common.version = 1;
712 ctx->device.common.module = const_cast<hw_module_t*>(module);
713 ctx->device.common.close = close_copybit;
714 ctx->device.set_parameter = set_parameter_copybit;
715 ctx->device.get = get;
716 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400717 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700718 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800719 ctx->device.finish = finish_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700720 ctx->device.fill_color = fill_color;
Terence Hampson0124cc72013-04-18 23:45:56 -0700721 ctx->device.flush_get_fence = flush_get_fence;
Ramakant Singh613e3572013-10-17 15:25:14 +0530722 ctx->device.clear = clear_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700723 ctx->mAlpha = MDP_ALPHA_NOP;
724 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400725 ctx->sync.flags = 0;
Sushil Chauhan9be65422013-12-02 13:27:53 -0800726 ctx->relFence = -1;
727 for (int i=0; i < MDP_MAX_FENCE_FD; i++) {
728 ctx->acqFence[i] = -1;
729 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400730 ctx->sync.acq_fen_fd = ctx->acqFence;
731 ctx->sync.rel_fen_fd = &ctx->relFence;
732 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400733 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400734 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
735 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700736 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
737 if (ctx->mFD < 0) {
738 status = errno;
739 ALOGE("Error opening frame buffer errno=%d (%s)",
740 status, strerror(status));
741 status = -status;
742 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700743 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700744 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700745 }
746 return status;
747}