blob: b37eee08356922e6f968fa99db48be99122f78c4 [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 = {
76open: open_copybit
77};
78
79/*
80 * The COPYBIT Module
81 */
82struct copybit_module_t HAL_MODULE_INFO_SYM = {
83common: {
84tag: HARDWARE_MODULE_TAG,
85 version_major: 1,
86 version_minor: 0,
87 id: COPYBIT_HARDWARE_MODULE_ID,
88 name: "QCT MSM7K COPYBIT Module",
89 author: "Google, Inc.",
90 methods: &copybit_module_methods
91 }
92};
93
94/******************************************************************************/
95
96/** min of int a, b */
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;
504 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
505 flags |= MDP_BLIT_NON_CACHED;
506 }
507
508 set_infos(ctx, req, flags);
509 set_image(&req->dst, dst);
510 set_image(&req->src, src);
511 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
512
513 if (req->src_rect.w<=0 || req->src_rect.h<=0)
514 continue;
515
516 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
517 continue;
518
Terence Hampsonadf47302013-05-23 12:21:02 -0400519 if (++list->count == maxCount) {
520 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400521 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400522 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700523 }
524 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400525 if(yv12_handle) {
526 //Before freeing the buffer we need buffer passed through blit call
527 if (list->count != 0) {
528 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400529 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400530 list->count = 0;
531 }
532 free_buffer(yv12_handle);
533 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700534 } else {
535 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
536 status = -EINVAL;
537 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538 return status;
539}
540
541/** Perform a blit type operation */
542static int blit_copybit(
543 struct copybit_device_t *dev,
544 struct copybit_image_t const *dst,
545 struct copybit_image_t const *src,
546 struct copybit_region_t const *region)
547{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700548 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
549 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700550 return stretch_copybit(dev, dst, src, &dr, &sr, region);
551}
552
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800553static int finish_copybit(struct copybit_device_t *dev)
554{
555 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700556 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800557}
558
Sushil Chauhan943797c2013-10-21 17:35:55 -0700559/** Fill the rect on dst with RGBA color **/
560static int fill_color(struct copybit_device_t *dev,
561 struct copybit_image_t const *dst,
562 struct copybit_rect_t const *rect,
563 uint32_t color)
564{
565 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
566 if (!ctx) {
567 ALOGE("%s: Invalid copybit context", __FUNCTION__);
568 return -EINVAL;
569 }
570
571 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
572 ALOGE("%s: Invalid DST w=%d h=%d", __FUNCTION__, dst->w, dst->h);
573 return -EINVAL;
574 }
575
576 if (rect->l < 0 || (uint32_t)(rect->r - rect->l) > dst->w ||
577 rect->t < 0 || (uint32_t)(rect->b - rect->t) > dst->h) {
578 ALOGE("%s: Invalid destination rect: l=%d t=%d r=%d b=%d",
579 __FUNCTION__, rect->l, rect->t, rect->r, rect->b);
580 return -EINVAL;
581 }
582
583 struct blitReq* list = &ctx->list;
584 mdp_blit_req* req = &list->req[list->count++];
585 set_infos(ctx, req, MDP_SOLID_FILL);
586 set_image(&req->src, dst);
587 set_image(&req->dst, dst);
588
589 req->dst_rect.x = rect->l;
590 req->dst_rect.y = rect->t;
591 req->dst_rect.w = rect->r - rect->l;
592 req->dst_rect.h = rect->b - rect->t;
593 req->src_rect = req->dst_rect;
594
595 req->const_color.r = (uint32_t)((color >> 0) & 0xff);
596 req->const_color.g = (uint32_t)((color >> 8) & 0xff);
597 req->const_color.b = (uint32_t)((color >> 16) & 0xff);
598 req->const_color.alpha = (uint32_t)((color >> 24) & 0xff);
599
600 int status = msm_copybit(ctx, list);
601 return status;
602}
603
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604/*****************************************************************************/
605
606/** Close the copybit device */
607static int close_copybit(struct hw_device_t *dev)
608{
609 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
610 if (ctx) {
611 close(ctx->mFD);
612 free(ctx);
613 }
614 return 0;
615}
616
Terence Hampson0124cc72013-04-18 23:45:56 -0700617static int flush_get_fence(struct copybit_device_t *dev, int* fd)
618{
Terence Hampsonadf47302013-05-23 12:21:02 -0400619 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
620 struct blitReq *list = &ctx->list;
621 int ret = -EINVAL;
622
623 if (list->count) {
624 ret = msm_copybit(ctx, list);
625 if (ret < 0)
626 ALOGE("%s: Blit call failed", __FUNCTION__);
627 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400628 }
629 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400630 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400631 ctx->relFence = -1;
632 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700633}
634
Naseer Ahmed29a26812012-06-14 00:56:20 -0700635/** Open a new instance of a copybit device using name */
636static int open_copybit(const struct hw_module_t* module, const char* name,
637 struct hw_device_t** device)
638{
639 int status = -EINVAL;
640 copybit_context_t *ctx;
641 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
642 memset(ctx, 0, sizeof(*ctx));
643
644 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
645 ctx->device.common.version = 1;
646 ctx->device.common.module = const_cast<hw_module_t*>(module);
647 ctx->device.common.close = close_copybit;
648 ctx->device.set_parameter = set_parameter_copybit;
649 ctx->device.get = get;
650 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400651 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700652 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800653 ctx->device.finish = finish_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -0700654 ctx->device.fill_color = fill_color;
Terence Hampson0124cc72013-04-18 23:45:56 -0700655 ctx->device.flush_get_fence = flush_get_fence;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700656 ctx->mAlpha = MDP_ALPHA_NOP;
657 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400658 ctx->sync.flags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400659 ctx->sync.acq_fen_fd = ctx->acqFence;
660 ctx->sync.rel_fen_fd = &ctx->relFence;
661 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400662 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400663 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
664 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700665 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
666 if (ctx->mFD < 0) {
667 status = errno;
668 ALOGE("Error opening frame buffer errno=%d (%s)",
669 status, strerror(status));
670 status = -status;
671 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700672 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700673 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700674 }
675 return status;
676}