blob: e641f2e600fa2908eb1855dabb68a7868188f35d [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/******************************************************************************/
49
50/** State information for each device instance */
51struct copybit_context_t {
52 struct copybit_device_t device;
53 int mFD;
54 uint8_t mAlpha;
55 int mFlags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070056 bool mBlitToFB;
Naseer Ahmed29a26812012-06-14 00:56:20 -070057};
58
59/**
60 * Common hardware methods
61 */
62
63static int open_copybit(const struct hw_module_t* module, const char* name,
64 struct hw_device_t** device);
65
66static struct hw_module_methods_t copybit_module_methods = {
67open: open_copybit
68};
69
70/*
71 * The COPYBIT Module
72 */
73struct copybit_module_t HAL_MODULE_INFO_SYM = {
74common: {
75tag: HARDWARE_MODULE_TAG,
76 version_major: 1,
77 version_minor: 0,
78 id: COPYBIT_HARDWARE_MODULE_ID,
79 name: "QCT MSM7K COPYBIT Module",
80 author: "Google, Inc.",
81 methods: &copybit_module_methods
82 }
83};
84
85/******************************************************************************/
86
87/** min of int a, b */
88static inline int min(int a, int b) {
89 return (a<b) ? a : b;
90}
91
92/** max of int a, b */
93static inline int max(int a, int b) {
94 return (a>b) ? a : b;
95}
96
97/** scale each parameter by mul/div. Assume div isn't 0 */
98static inline void MULDIV(uint32_t *a, uint32_t *b, int mul, int div) {
99 if (mul != div) {
100 *a = (mul * *a) / div;
101 *b = (mul * *b) / div;
102 }
103}
104
105/** Determine the intersection of lhs & rhs store in out */
106static void intersect(struct copybit_rect_t *out,
107 const struct copybit_rect_t *lhs,
108 const struct copybit_rect_t *rhs) {
109 out->l = max(lhs->l, rhs->l);
110 out->t = max(lhs->t, rhs->t);
111 out->r = min(lhs->r, rhs->r);
112 out->b = min(lhs->b, rhs->b);
113}
114
115/** convert COPYBIT_FORMAT to MDP format */
116static int get_format(int format) {
117 switch (format) {
118 case HAL_PIXEL_FORMAT_RGB_565: return MDP_RGB_565;
119 case HAL_PIXEL_FORMAT_RGBX_8888: return MDP_RGBX_8888;
120 case HAL_PIXEL_FORMAT_RGB_888: return MDP_RGB_888;
121 case HAL_PIXEL_FORMAT_RGBA_8888: return MDP_RGBA_8888;
122 case HAL_PIXEL_FORMAT_BGRA_8888: return MDP_BGRA_8888;
123 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CBCR_H2V1;
124 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CBCR_H2V2;
125 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CRCB_H2V1;
126 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CRCB_H2V2;
127 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Terence Hampsond0fd5792013-05-29 11:56:52 -0400128 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_ADRENO;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800129 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700130 }
131 return -1;
132}
133
134/** convert from copybit image to mdp image structure */
135static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
136{
137 private_handle_t* hnd = (private_handle_t*)rhs->handle;
138 if(hnd == NULL){
139 ALOGE("copybit: Invalid handle");
140 return;
141 }
142 img->width = rhs->w;
143 img->height = rhs->h;
144 img->format = get_format(rhs->format);
145 img->offset = hnd->offset;
146 img->memory_id = hnd->fd;
147}
148/** setup rectangles */
149static void set_rects(struct copybit_context_t *dev,
150 struct mdp_blit_req *e,
151 const struct copybit_rect_t *dst,
152 const struct copybit_rect_t *src,
153 const struct copybit_rect_t *scissor,
154 uint32_t horiz_padding,
155 uint32_t vert_padding) {
156 struct copybit_rect_t clip;
157 intersect(&clip, scissor, dst);
158
159 e->dst_rect.x = clip.l;
160 e->dst_rect.y = clip.t;
161 e->dst_rect.w = clip.r - clip.l;
162 e->dst_rect.h = clip.b - clip.t;
163
164 uint32_t W, H, delta_x, delta_y;
165 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
166 delta_x = (clip.t - dst->t);
167 delta_y = (dst->r - clip.r);
168 e->src_rect.w = (clip.b - clip.t);
169 e->src_rect.h = (clip.r - clip.l);
170 W = dst->b - dst->t;
171 H = dst->r - dst->l;
172 } else {
173 delta_x = (clip.l - dst->l);
174 delta_y = (clip.t - dst->t);
175 e->src_rect.w = (clip.r - clip.l);
176 e->src_rect.h = (clip.b - clip.t);
177 W = dst->r - dst->l;
178 H = dst->b - dst->t;
179 }
180
181 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
182 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
183
184 e->src_rect.x = delta_x + src->l;
185 e->src_rect.y = delta_y + src->t;
186
187 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
188 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
189 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
190 }else{
191 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
192 }
193 }
194
195 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
196 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
197 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
198 }else{
199 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
200 }
201 }
202}
203
204/** setup mdp request */
205static void set_infos(struct copybit_context_t *dev,
206 struct mdp_blit_req *req, int flags)
207{
208 req->alpha = dev->mAlpha;
209 req->transp_mask = MDP_TRANSP_NOP;
210 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700211 // check if we are blitting to f/b
212 if (COPYBIT_ENABLE == dev->mBlitToFB) {
213 req->flags |= MDP_MEMORY_ID_TYPE_FB;
214 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700215#if defined(COPYBIT_QSD8K)
216 req->flags |= MDP_BLEND_FG_PREMULT;
217#endif
218}
219
220/** copy the bits */
221static int msm_copybit(struct copybit_context_t *dev, void const *list)
222{
223 int err = ioctl(dev->mFD, MSMFB_BLIT,
224 (struct mdp_blit_req_list const*)list);
225 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
226 if (err == 0) {
227 return 0;
228 } else {
229#if DEBUG_MDP_ERRORS
230 struct mdp_blit_req_list const* l =
231 (struct mdp_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700232 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700233 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
234 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700235 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700236 ,
237 i,
238 l->req[i].src.width,
239 l->req[i].src.height,
240 l->req[i].src.format,
241 l->req[i].src_rect.x,
242 l->req[i].src_rect.y,
243 l->req[i].src_rect.w,
244 l->req[i].src_rect.h,
245 l->req[i].dst.width,
246 l->req[i].dst.height,
247 l->req[i].dst.format,
248 l->req[i].dst_rect.x,
249 l->req[i].dst_rect.y,
250 l->req[i].dst_rect.w,
251 l->req[i].dst_rect.h,
252 l->req[i].flags
253 );
254 }
255#endif
256 return -errno;
257 }
258}
259
260/*****************************************************************************/
261
262/** Set a parameter to value */
263static int set_parameter_copybit(
264 struct copybit_device_t *dev,
265 int name,
266 int value)
267{
268 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
269 int status = 0;
270 if (ctx) {
271 switch(name) {
272 case COPYBIT_ROTATION_DEG:
273 switch (value) {
274 case 0:
275 ctx->mFlags &= ~0x7;
276 break;
277 case 90:
278 ctx->mFlags &= ~0x7;
279 ctx->mFlags |= MDP_ROT_90;
280 break;
281 case 180:
282 ctx->mFlags &= ~0x7;
283 ctx->mFlags |= MDP_ROT_180;
284 break;
285 case 270:
286 ctx->mFlags &= ~0x7;
287 ctx->mFlags |= MDP_ROT_270;
288 break;
289 default:
290 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
291 status = -EINVAL;
292 break;
293 }
294 break;
295 case COPYBIT_PLANE_ALPHA:
296 if (value < 0) value = MDP_ALPHA_NOP;
297 if (value >= 256) value = 255;
298 ctx->mAlpha = value;
299 break;
300 case COPYBIT_DITHER:
301 if (value == COPYBIT_ENABLE) {
302 ctx->mFlags |= MDP_DITHER;
303 } else if (value == COPYBIT_DISABLE) {
304 ctx->mFlags &= ~MDP_DITHER;
305 }
306 break;
307 case COPYBIT_BLUR:
308 if (value == COPYBIT_ENABLE) {
309 ctx->mFlags |= MDP_BLUR;
310 } else if (value == COPYBIT_DISABLE) {
311 ctx->mFlags &= ~MDP_BLUR;
312 }
313 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800314 case COPYBIT_BLEND_MODE:
315 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800317 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700318 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
319 }
320 break;
321 case COPYBIT_TRANSFORM:
322 ctx->mFlags &= ~0x7;
323 ctx->mFlags |= value & 0x7;
324 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700325 case COPYBIT_BLIT_TO_FRAMEBUFFER:
326 if (COPYBIT_ENABLE == value) {
327 ctx->mBlitToFB = value;
328 } else if (COPYBIT_DISABLE == value) {
329 ctx->mBlitToFB = value;
330 } else {
331 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
332 __FUNCTION__, value);
333 }
334 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 default:
336 status = -EINVAL;
337 break;
338 }
339 } else {
340 status = -EINVAL;
341 }
342 return status;
343}
344
345/** Get a static info value */
346static int get(struct copybit_device_t *dev, int name)
347{
348 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
349 int value;
350 if (ctx) {
351 switch(name) {
352 case COPYBIT_MINIFICATION_LIMIT:
353 value = MAX_SCALE_FACTOR;
354 break;
355 case COPYBIT_MAGNIFICATION_LIMIT:
356 value = MAX_SCALE_FACTOR;
357 break;
358 case COPYBIT_SCALING_FRAC_BITS:
359 value = 32;
360 break;
361 case COPYBIT_ROTATION_STEP_DEG:
362 value = 90;
363 break;
364 default:
365 value = -EINVAL;
366 }
367 } else {
368 value = -EINVAL;
369 }
370 return value;
371}
372
373/** do a stretch blit type operation */
374static int stretch_copybit(
375 struct copybit_device_t *dev,
376 struct copybit_image_t const *dst,
377 struct copybit_image_t const *src,
378 struct copybit_rect_t const *dst_rect,
379 struct copybit_rect_t const *src_rect,
380 struct copybit_region_t const *region)
381{
382 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
383 int status = 0;
384 private_handle_t *yv12_handle = NULL;
385 if (ctx) {
386 struct {
387 uint32_t count;
388 struct mdp_blit_req req[12];
389 } list;
390
Terence Hampson0124cc72013-04-18 23:45:56 -0700391 memset(&list, 0, sizeof(list));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700392 if (ctx->mAlpha < 255) {
393 switch (src->format) {
394 // we don't support plane alpha with RGBA formats
395 case HAL_PIXEL_FORMAT_RGBA_8888:
396 case HAL_PIXEL_FORMAT_BGRA_8888:
397 case HAL_PIXEL_FORMAT_RGBA_5551:
398 case HAL_PIXEL_FORMAT_RGBA_4444:
399 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
400 src->format);
401 return -EINVAL;
402 }
403 }
404
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800405 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
406 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700407 // this is always invalid
408 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
409 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
410
411 return -EINVAL;
412 }
413
414 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
415 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
416 return -EINVAL;
417 }
418
419 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
420 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
421 return -EINVAL;
422 }
423
424 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800425 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700426 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700427 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
428 src->format, usage)){
429 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
430 (const_cast<copybit_image_t *>(src))->format =
431 HAL_PIXEL_FORMAT_YCrCb_420_SP;
432 (const_cast<copybit_image_t *>(src))->handle =
433 yv12_handle;
434 (const_cast<copybit_image_t *>(src))->base =
435 (void *)yv12_handle->base;
436 }
437 else{
438 ALOGE("Error copybit conversion from yv12 failed");
439 if(yv12_handle)
440 free_buffer(yv12_handle);
441 return -EINVAL;
442 }
443 }
444 else{
445 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
446 return -EINVAL;
447 }
448 }
449 const uint32_t maxCount = sizeof(list.req)/sizeof(list.req[0]);
450 const struct copybit_rect_t bounds = { 0, 0, dst->w, dst->h };
451 struct copybit_rect_t clip;
452 list.count = 0;
453 status = 0;
454 while ((status == 0) && region->next(region, &clip)) {
455 intersect(&clip, &bounds, &clip);
456 mdp_blit_req* req = &list.req[list.count];
457 int flags = 0;
458
459 private_handle_t* src_hnd = (private_handle_t*)src->handle;
460 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
461 flags |= MDP_BLIT_NON_CACHED;
462 }
463
464 set_infos(ctx, req, flags);
465 set_image(&req->dst, dst);
466 set_image(&req->src, src);
467 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
468
469 if (req->src_rect.w<=0 || req->src_rect.h<=0)
470 continue;
471
472 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
473 continue;
474
475 if (++list.count == maxCount) {
476 status = msm_copybit(ctx, &list);
477 list.count = 0;
478 }
479 }
480 if ((status == 0) && list.count) {
481 status = msm_copybit(ctx, &list);
482 }
483 } else {
484 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
485 status = -EINVAL;
486 }
487 if(yv12_handle)
488 free_buffer(yv12_handle);
489 return status;
490}
491
492/** Perform a blit type operation */
493static int blit_copybit(
494 struct copybit_device_t *dev,
495 struct copybit_image_t const *dst,
496 struct copybit_image_t const *src,
497 struct copybit_region_t const *region)
498{
499 struct copybit_rect_t dr = { 0, 0, dst->w, dst->h };
500 struct copybit_rect_t sr = { 0, 0, src->w, src->h };
501 return stretch_copybit(dev, dst, src, &dr, &sr, region);
502}
503
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800504static int finish_copybit(struct copybit_device_t *dev)
505{
506 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700507 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800508}
509
Naseer Ahmed29a26812012-06-14 00:56:20 -0700510/*****************************************************************************/
511
512/** Close the copybit device */
513static int close_copybit(struct hw_device_t *dev)
514{
515 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
516 if (ctx) {
517 close(ctx->mFD);
518 free(ctx);
519 }
520 return 0;
521}
522
Terence Hampson0124cc72013-04-18 23:45:56 -0700523static int flush_get_fence(struct copybit_device_t *dev, int* fd)
524{
525 return -1;
526}
527
Naseer Ahmed29a26812012-06-14 00:56:20 -0700528/** Open a new instance of a copybit device using name */
529static int open_copybit(const struct hw_module_t* module, const char* name,
530 struct hw_device_t** device)
531{
532 int status = -EINVAL;
533 copybit_context_t *ctx;
534 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
535 memset(ctx, 0, sizeof(*ctx));
536
537 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
538 ctx->device.common.version = 1;
539 ctx->device.common.module = const_cast<hw_module_t*>(module);
540 ctx->device.common.close = close_copybit;
541 ctx->device.set_parameter = set_parameter_copybit;
542 ctx->device.get = get;
543 ctx->device.blit = blit_copybit;
544 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800545 ctx->device.finish = finish_copybit;
Terence Hampson0124cc72013-04-18 23:45:56 -0700546 ctx->device.flush_get_fence = flush_get_fence;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700547 ctx->mAlpha = MDP_ALPHA_NOP;
548 ctx->mFlags = 0;
549 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
550 if (ctx->mFD < 0) {
551 status = errno;
552 ALOGE("Error opening frame buffer errno=%d (%s)",
553 status, strerror(status));
554 status = -status;
555 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700556 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700557 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700558 }
559 return status;
560}