blob: 773e9b854afd95332f778e1ed44120542cd37bea [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2010 - 2011, Code Aurora Forum. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Naseer Ahmed29a26812012-06-14 00:56:20 -070018#include <cutils/log.h>
19
20#include <linux/msm_mdp.h>
21#include <linux/fb.h>
22
23#include <stdint.h>
24#include <string.h>
25#include <unistd.h>
26#include <errno.h>
27#include <fcntl.h>
28
29#include <sys/ioctl.h>
30#include <sys/types.h>
31#include <sys/mman.h>
32
33#include <copybit.h>
34
35#include "gralloc_priv.h"
36#include "software_converter.h"
37
38#define DEBUG_MDP_ERRORS 1
39
40/******************************************************************************/
41
42#if defined(COPYBIT_MSM7K)
43#define MAX_SCALE_FACTOR (4)
44#define MAX_DIMENSION (4096)
45#elif defined(COPYBIT_QSD8K)
46#define MAX_SCALE_FACTOR (8)
47#define MAX_DIMENSION (2048)
48#else
49#error "Unsupported MDP version"
50#endif
51
52/******************************************************************************/
53
54/** State information for each device instance */
55struct copybit_context_t {
56 struct copybit_device_t device;
57 int mFD;
58 uint8_t mAlpha;
59 int mFlags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070060 bool mBlitToFB;
Naseer Ahmed29a26812012-06-14 00:56:20 -070061};
62
63/**
64 * Common hardware methods
65 */
66
67static int open_copybit(const struct hw_module_t* module, const char* name,
68 struct hw_device_t** device);
69
70static struct hw_module_methods_t copybit_module_methods = {
71open: open_copybit
72};
73
74/*
75 * The COPYBIT Module
76 */
77struct copybit_module_t HAL_MODULE_INFO_SYM = {
78common: {
79tag: HARDWARE_MODULE_TAG,
80 version_major: 1,
81 version_minor: 0,
82 id: COPYBIT_HARDWARE_MODULE_ID,
83 name: "QCT MSM7K COPYBIT Module",
84 author: "Google, Inc.",
85 methods: &copybit_module_methods
86 }
87};
88
89/******************************************************************************/
90
91/** min of int a, b */
92static inline int min(int a, int b) {
93 return (a<b) ? a : b;
94}
95
96/** max of int a, b */
97static inline int max(int a, int b) {
98 return (a>b) ? a : b;
99}
100
101/** scale each parameter by mul/div. Assume div isn't 0 */
102static inline void MULDIV(uint32_t *a, uint32_t *b, int mul, int div) {
103 if (mul != div) {
104 *a = (mul * *a) / div;
105 *b = (mul * *b) / div;
106 }
107}
108
109/** Determine the intersection of lhs & rhs store in out */
110static void intersect(struct copybit_rect_t *out,
111 const struct copybit_rect_t *lhs,
112 const struct copybit_rect_t *rhs) {
113 out->l = max(lhs->l, rhs->l);
114 out->t = max(lhs->t, rhs->t);
115 out->r = min(lhs->r, rhs->r);
116 out->b = min(lhs->b, rhs->b);
117}
118
119/** convert COPYBIT_FORMAT to MDP format */
120static int get_format(int format) {
121 switch (format) {
122 case HAL_PIXEL_FORMAT_RGB_565: return MDP_RGB_565;
123 case HAL_PIXEL_FORMAT_RGBX_8888: return MDP_RGBX_8888;
124 case HAL_PIXEL_FORMAT_RGB_888: return MDP_RGB_888;
125 case HAL_PIXEL_FORMAT_RGBA_8888: return MDP_RGBA_8888;
126 case HAL_PIXEL_FORMAT_BGRA_8888: return MDP_BGRA_8888;
127 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CBCR_H2V1;
128 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CBCR_H2V2;
129 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CRCB_H2V1;
130 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CRCB_H2V2;
131 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
132 }
133 return -1;
134}
135
136/** convert from copybit image to mdp image structure */
137static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
138{
139 private_handle_t* hnd = (private_handle_t*)rhs->handle;
140 if(hnd == NULL){
141 ALOGE("copybit: Invalid handle");
142 return;
143 }
144 img->width = rhs->w;
145 img->height = rhs->h;
146 img->format = get_format(rhs->format);
147 img->offset = hnd->offset;
148 img->memory_id = hnd->fd;
149}
150/** setup rectangles */
151static void set_rects(struct copybit_context_t *dev,
152 struct mdp_blit_req *e,
153 const struct copybit_rect_t *dst,
154 const struct copybit_rect_t *src,
155 const struct copybit_rect_t *scissor,
156 uint32_t horiz_padding,
157 uint32_t vert_padding) {
158 struct copybit_rect_t clip;
159 intersect(&clip, scissor, dst);
160
161 e->dst_rect.x = clip.l;
162 e->dst_rect.y = clip.t;
163 e->dst_rect.w = clip.r - clip.l;
164 e->dst_rect.h = clip.b - clip.t;
165
166 uint32_t W, H, delta_x, delta_y;
167 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
168 delta_x = (clip.t - dst->t);
169 delta_y = (dst->r - clip.r);
170 e->src_rect.w = (clip.b - clip.t);
171 e->src_rect.h = (clip.r - clip.l);
172 W = dst->b - dst->t;
173 H = dst->r - dst->l;
174 } else {
175 delta_x = (clip.l - dst->l);
176 delta_y = (clip.t - dst->t);
177 e->src_rect.w = (clip.r - clip.l);
178 e->src_rect.h = (clip.b - clip.t);
179 W = dst->r - dst->l;
180 H = dst->b - dst->t;
181 }
182
183 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
184 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
185
186 e->src_rect.x = delta_x + src->l;
187 e->src_rect.y = delta_y + src->t;
188
189 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
190 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
191 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
192 }else{
193 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
194 }
195 }
196
197 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
198 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
199 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
200 }else{
201 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
202 }
203 }
204}
205
206/** setup mdp request */
207static void set_infos(struct copybit_context_t *dev,
208 struct mdp_blit_req *req, int flags)
209{
210 req->alpha = dev->mAlpha;
211 req->transp_mask = MDP_TRANSP_NOP;
212 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700213 // check if we are blitting to f/b
214 if (COPYBIT_ENABLE == dev->mBlitToFB) {
215 req->flags |= MDP_MEMORY_ID_TYPE_FB;
216 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700217#if defined(COPYBIT_QSD8K)
218 req->flags |= MDP_BLEND_FG_PREMULT;
219#endif
220}
221
222/** copy the bits */
223static int msm_copybit(struct copybit_context_t *dev, void const *list)
224{
225 int err = ioctl(dev->mFD, MSMFB_BLIT,
226 (struct mdp_blit_req_list const*)list);
227 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
228 if (err == 0) {
229 return 0;
230 } else {
231#if DEBUG_MDP_ERRORS
232 struct mdp_blit_req_list const* l =
233 (struct mdp_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700234 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700235 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
236 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700237 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700238 ,
239 i,
240 l->req[i].src.width,
241 l->req[i].src.height,
242 l->req[i].src.format,
243 l->req[i].src_rect.x,
244 l->req[i].src_rect.y,
245 l->req[i].src_rect.w,
246 l->req[i].src_rect.h,
247 l->req[i].dst.width,
248 l->req[i].dst.height,
249 l->req[i].dst.format,
250 l->req[i].dst_rect.x,
251 l->req[i].dst_rect.y,
252 l->req[i].dst_rect.w,
253 l->req[i].dst_rect.h,
254 l->req[i].flags
255 );
256 }
257#endif
258 return -errno;
259 }
260}
261
262/*****************************************************************************/
263
264/** Set a parameter to value */
265static int set_parameter_copybit(
266 struct copybit_device_t *dev,
267 int name,
268 int value)
269{
270 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
271 int status = 0;
272 if (ctx) {
273 switch(name) {
274 case COPYBIT_ROTATION_DEG:
275 switch (value) {
276 case 0:
277 ctx->mFlags &= ~0x7;
278 break;
279 case 90:
280 ctx->mFlags &= ~0x7;
281 ctx->mFlags |= MDP_ROT_90;
282 break;
283 case 180:
284 ctx->mFlags &= ~0x7;
285 ctx->mFlags |= MDP_ROT_180;
286 break;
287 case 270:
288 ctx->mFlags &= ~0x7;
289 ctx->mFlags |= MDP_ROT_270;
290 break;
291 default:
292 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
293 status = -EINVAL;
294 break;
295 }
296 break;
297 case COPYBIT_PLANE_ALPHA:
298 if (value < 0) value = MDP_ALPHA_NOP;
299 if (value >= 256) value = 255;
300 ctx->mAlpha = value;
301 break;
302 case COPYBIT_DITHER:
303 if (value == COPYBIT_ENABLE) {
304 ctx->mFlags |= MDP_DITHER;
305 } else if (value == COPYBIT_DISABLE) {
306 ctx->mFlags &= ~MDP_DITHER;
307 }
308 break;
309 case COPYBIT_BLUR:
310 if (value == COPYBIT_ENABLE) {
311 ctx->mFlags |= MDP_BLUR;
312 } else if (value == COPYBIT_DISABLE) {
313 ctx->mFlags &= ~MDP_BLUR;
314 }
315 break;
316 case COPYBIT_PREMULTIPLIED_ALPHA:
317 if(value == COPYBIT_ENABLE) {
318 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
319 } else if (value == COPYBIT_DISABLE) {
320 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
321 }
322 break;
323 case COPYBIT_TRANSFORM:
324 ctx->mFlags &= ~0x7;
325 ctx->mFlags |= value & 0x7;
326 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700327 case COPYBIT_BLIT_TO_FRAMEBUFFER:
328 if (COPYBIT_ENABLE == value) {
329 ctx->mBlitToFB = value;
330 } else if (COPYBIT_DISABLE == value) {
331 ctx->mBlitToFB = value;
332 } else {
333 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
334 __FUNCTION__, value);
335 }
336 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700337 default:
338 status = -EINVAL;
339 break;
340 }
341 } else {
342 status = -EINVAL;
343 }
344 return status;
345}
346
347/** Get a static info value */
348static int get(struct copybit_device_t *dev, int name)
349{
350 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
351 int value;
352 if (ctx) {
353 switch(name) {
354 case COPYBIT_MINIFICATION_LIMIT:
355 value = MAX_SCALE_FACTOR;
356 break;
357 case COPYBIT_MAGNIFICATION_LIMIT:
358 value = MAX_SCALE_FACTOR;
359 break;
360 case COPYBIT_SCALING_FRAC_BITS:
361 value = 32;
362 break;
363 case COPYBIT_ROTATION_STEP_DEG:
364 value = 90;
365 break;
366 default:
367 value = -EINVAL;
368 }
369 } else {
370 value = -EINVAL;
371 }
372 return value;
373}
374
375/** do a stretch blit type operation */
376static int stretch_copybit(
377 struct copybit_device_t *dev,
378 struct copybit_image_t const *dst,
379 struct copybit_image_t const *src,
380 struct copybit_rect_t const *dst_rect,
381 struct copybit_rect_t const *src_rect,
382 struct copybit_region_t const *region)
383{
384 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
385 int status = 0;
386 private_handle_t *yv12_handle = NULL;
387 if (ctx) {
388 struct {
389 uint32_t count;
390 struct mdp_blit_req req[12];
391 } list;
392
393 if (ctx->mAlpha < 255) {
394 switch (src->format) {
395 // we don't support plane alpha with RGBA formats
396 case HAL_PIXEL_FORMAT_RGBA_8888:
397 case HAL_PIXEL_FORMAT_BGRA_8888:
398 case HAL_PIXEL_FORMAT_RGBA_5551:
399 case HAL_PIXEL_FORMAT_RGBA_4444:
400 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
401 src->format);
402 return -EINVAL;
403 }
404 }
405
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700406 if (src_rect->l < 0 || src_rect->r > (int)src->w ||
407 src_rect->t < 0 || src_rect->b > (int)src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700408 // this is always invalid
409 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
410 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
411
412 return -EINVAL;
413 }
414
415 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
416 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
417 return -EINVAL;
418 }
419
420 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
421 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
422 return -EINVAL;
423 }
424
425 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700426 int usage = GRALLOC_USAGE_PRIVATE_MM_HEAP;
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
504/*****************************************************************************/
505
506/** Close the copybit device */
507static int close_copybit(struct hw_device_t *dev)
508{
509 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
510 if (ctx) {
511 close(ctx->mFD);
512 free(ctx);
513 }
514 return 0;
515}
516
517/** Open a new instance of a copybit device using name */
518static int open_copybit(const struct hw_module_t* module, const char* name,
519 struct hw_device_t** device)
520{
521 int status = -EINVAL;
522 copybit_context_t *ctx;
523 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
524 memset(ctx, 0, sizeof(*ctx));
525
526 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
527 ctx->device.common.version = 1;
528 ctx->device.common.module = const_cast<hw_module_t*>(module);
529 ctx->device.common.close = close_copybit;
530 ctx->device.set_parameter = set_parameter_copybit;
531 ctx->device.get = get;
532 ctx->device.blit = blit_copybit;
533 ctx->device.stretch = stretch_copybit;
534 ctx->mAlpha = MDP_ALPHA_NOP;
535 ctx->mFlags = 0;
536 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
537 if (ctx->mFD < 0) {
538 status = errno;
539 ALOGE("Error opening frame buffer errno=%d (%s)",
540 status, strerror(status));
541 status = -status;
542 } else {
543 struct fb_fix_screeninfo finfo;
544 if (ioctl(ctx->mFD, FBIOGET_FSCREENINFO, &finfo) == 0) {
545 if (strncmp(finfo.id, "msmfb", 5) == 0) {
546 /* Success */
547 status = 0;
548 } else {
549 ALOGE("Error not msm frame buffer");
550 status = -EINVAL;
551 }
552 } else {
553 ALOGE("Error executing ioctl for screen info");
554 status = -errno;
555 }
556 }
557
558 if (status == 0) {
559 *device = &ctx->device.common;
560 } else {
561 close_copybit(&ctx->device.common);
562 }
563 return status;
564}