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