blob: 43b90d1ea18b916902d957af24d750199f5731c0 [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;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800128 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700129 }
130 return -1;
131}
132
133/** convert from copybit image to mdp image structure */
134static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
135{
136 private_handle_t* hnd = (private_handle_t*)rhs->handle;
137 if(hnd == NULL){
138 ALOGE("copybit: Invalid handle");
139 return;
140 }
141 img->width = rhs->w;
142 img->height = rhs->h;
143 img->format = get_format(rhs->format);
144 img->offset = hnd->offset;
145 img->memory_id = hnd->fd;
146}
147/** setup rectangles */
148static void set_rects(struct copybit_context_t *dev,
149 struct mdp_blit_req *e,
150 const struct copybit_rect_t *dst,
151 const struct copybit_rect_t *src,
152 const struct copybit_rect_t *scissor,
153 uint32_t horiz_padding,
154 uint32_t vert_padding) {
155 struct copybit_rect_t clip;
156 intersect(&clip, scissor, dst);
157
158 e->dst_rect.x = clip.l;
159 e->dst_rect.y = clip.t;
160 e->dst_rect.w = clip.r - clip.l;
161 e->dst_rect.h = clip.b - clip.t;
162
163 uint32_t W, H, delta_x, delta_y;
164 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
165 delta_x = (clip.t - dst->t);
166 delta_y = (dst->r - clip.r);
167 e->src_rect.w = (clip.b - clip.t);
168 e->src_rect.h = (clip.r - clip.l);
169 W = dst->b - dst->t;
170 H = dst->r - dst->l;
171 } else {
172 delta_x = (clip.l - dst->l);
173 delta_y = (clip.t - dst->t);
174 e->src_rect.w = (clip.r - clip.l);
175 e->src_rect.h = (clip.b - clip.t);
176 W = dst->r - dst->l;
177 H = dst->b - dst->t;
178 }
179
180 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
181 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
182
183 e->src_rect.x = delta_x + src->l;
184 e->src_rect.y = delta_y + src->t;
185
186 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
187 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
188 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
189 }else{
190 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
191 }
192 }
193
194 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
195 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
196 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
197 }else{
198 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
199 }
200 }
201}
202
203/** setup mdp request */
204static void set_infos(struct copybit_context_t *dev,
205 struct mdp_blit_req *req, int flags)
206{
207 req->alpha = dev->mAlpha;
208 req->transp_mask = MDP_TRANSP_NOP;
209 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700210 // check if we are blitting to f/b
211 if (COPYBIT_ENABLE == dev->mBlitToFB) {
212 req->flags |= MDP_MEMORY_ID_TYPE_FB;
213 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700214#if defined(COPYBIT_QSD8K)
215 req->flags |= MDP_BLEND_FG_PREMULT;
216#endif
217}
218
219/** copy the bits */
220static int msm_copybit(struct copybit_context_t *dev, void const *list)
221{
222 int err = ioctl(dev->mFD, MSMFB_BLIT,
223 (struct mdp_blit_req_list const*)list);
224 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
225 if (err == 0) {
226 return 0;
227 } else {
228#if DEBUG_MDP_ERRORS
229 struct mdp_blit_req_list const* l =
230 (struct mdp_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700231 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700232 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
233 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700234 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700235 ,
236 i,
237 l->req[i].src.width,
238 l->req[i].src.height,
239 l->req[i].src.format,
240 l->req[i].src_rect.x,
241 l->req[i].src_rect.y,
242 l->req[i].src_rect.w,
243 l->req[i].src_rect.h,
244 l->req[i].dst.width,
245 l->req[i].dst.height,
246 l->req[i].dst.format,
247 l->req[i].dst_rect.x,
248 l->req[i].dst_rect.y,
249 l->req[i].dst_rect.w,
250 l->req[i].dst_rect.h,
251 l->req[i].flags
252 );
253 }
254#endif
255 return -errno;
256 }
257}
258
259/*****************************************************************************/
260
261/** Set a parameter to value */
262static int set_parameter_copybit(
263 struct copybit_device_t *dev,
264 int name,
265 int value)
266{
267 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
268 int status = 0;
269 if (ctx) {
270 switch(name) {
271 case COPYBIT_ROTATION_DEG:
272 switch (value) {
273 case 0:
274 ctx->mFlags &= ~0x7;
275 break;
276 case 90:
277 ctx->mFlags &= ~0x7;
278 ctx->mFlags |= MDP_ROT_90;
279 break;
280 case 180:
281 ctx->mFlags &= ~0x7;
282 ctx->mFlags |= MDP_ROT_180;
283 break;
284 case 270:
285 ctx->mFlags &= ~0x7;
286 ctx->mFlags |= MDP_ROT_270;
287 break;
288 default:
289 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
290 status = -EINVAL;
291 break;
292 }
293 break;
294 case COPYBIT_PLANE_ALPHA:
295 if (value < 0) value = MDP_ALPHA_NOP;
296 if (value >= 256) value = 255;
297 ctx->mAlpha = value;
298 break;
299 case COPYBIT_DITHER:
300 if (value == COPYBIT_ENABLE) {
301 ctx->mFlags |= MDP_DITHER;
302 } else if (value == COPYBIT_DISABLE) {
303 ctx->mFlags &= ~MDP_DITHER;
304 }
305 break;
306 case COPYBIT_BLUR:
307 if (value == COPYBIT_ENABLE) {
308 ctx->mFlags |= MDP_BLUR;
309 } else if (value == COPYBIT_DISABLE) {
310 ctx->mFlags &= ~MDP_BLUR;
311 }
312 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800313 case COPYBIT_BLEND_MODE:
314 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700315 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800316 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700317 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
318 }
319 break;
320 case COPYBIT_TRANSFORM:
321 ctx->mFlags &= ~0x7;
322 ctx->mFlags |= value & 0x7;
323 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700324 case COPYBIT_BLIT_TO_FRAMEBUFFER:
325 if (COPYBIT_ENABLE == value) {
326 ctx->mBlitToFB = value;
327 } else if (COPYBIT_DISABLE == value) {
328 ctx->mBlitToFB = value;
329 } else {
330 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
331 __FUNCTION__, value);
332 }
333 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700334 default:
335 status = -EINVAL;
336 break;
337 }
338 } else {
339 status = -EINVAL;
340 }
341 return status;
342}
343
344/** Get a static info value */
345static int get(struct copybit_device_t *dev, int name)
346{
347 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
348 int value;
349 if (ctx) {
350 switch(name) {
351 case COPYBIT_MINIFICATION_LIMIT:
352 value = MAX_SCALE_FACTOR;
353 break;
354 case COPYBIT_MAGNIFICATION_LIMIT:
355 value = MAX_SCALE_FACTOR;
356 break;
357 case COPYBIT_SCALING_FRAC_BITS:
358 value = 32;
359 break;
360 case COPYBIT_ROTATION_STEP_DEG:
361 value = 90;
362 break;
363 default:
364 value = -EINVAL;
365 }
366 } else {
367 value = -EINVAL;
368 }
369 return value;
370}
371
372/** do a stretch blit type operation */
373static int stretch_copybit(
374 struct copybit_device_t *dev,
375 struct copybit_image_t const *dst,
376 struct copybit_image_t const *src,
377 struct copybit_rect_t const *dst_rect,
378 struct copybit_rect_t const *src_rect,
379 struct copybit_region_t const *region)
380{
381 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
382 int status = 0;
383 private_handle_t *yv12_handle = NULL;
384 if (ctx) {
385 struct {
386 uint32_t count;
387 struct mdp_blit_req req[12];
388 } list;
389
Terence Hampson0124cc72013-04-18 23:45:56 -0700390 memset(&list, 0, sizeof(list));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700391 if (ctx->mAlpha < 255) {
392 switch (src->format) {
393 // we don't support plane alpha with RGBA formats
394 case HAL_PIXEL_FORMAT_RGBA_8888:
395 case HAL_PIXEL_FORMAT_BGRA_8888:
396 case HAL_PIXEL_FORMAT_RGBA_5551:
397 case HAL_PIXEL_FORMAT_RGBA_4444:
398 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
399 src->format);
400 return -EINVAL;
401 }
402 }
403
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800404 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
405 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700406 // this is always invalid
407 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
408 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
409
410 return -EINVAL;
411 }
412
413 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
414 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
415 return -EINVAL;
416 }
417
418 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
419 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
420 return -EINVAL;
421 }
422
423 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800424 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700425 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700426 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
427 src->format, usage)){
428 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
429 (const_cast<copybit_image_t *>(src))->format =
430 HAL_PIXEL_FORMAT_YCrCb_420_SP;
431 (const_cast<copybit_image_t *>(src))->handle =
432 yv12_handle;
433 (const_cast<copybit_image_t *>(src))->base =
434 (void *)yv12_handle->base;
435 }
436 else{
437 ALOGE("Error copybit conversion from yv12 failed");
438 if(yv12_handle)
439 free_buffer(yv12_handle);
440 return -EINVAL;
441 }
442 }
443 else{
444 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
445 return -EINVAL;
446 }
447 }
448 const uint32_t maxCount = sizeof(list.req)/sizeof(list.req[0]);
449 const struct copybit_rect_t bounds = { 0, 0, dst->w, dst->h };
450 struct copybit_rect_t clip;
451 list.count = 0;
452 status = 0;
453 while ((status == 0) && region->next(region, &clip)) {
454 intersect(&clip, &bounds, &clip);
455 mdp_blit_req* req = &list.req[list.count];
456 int flags = 0;
457
458 private_handle_t* src_hnd = (private_handle_t*)src->handle;
459 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
460 flags |= MDP_BLIT_NON_CACHED;
461 }
462
463 set_infos(ctx, req, flags);
464 set_image(&req->dst, dst);
465 set_image(&req->src, src);
466 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
467
468 if (req->src_rect.w<=0 || req->src_rect.h<=0)
469 continue;
470
471 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
472 continue;
473
474 if (++list.count == maxCount) {
475 status = msm_copybit(ctx, &list);
476 list.count = 0;
477 }
478 }
479 if ((status == 0) && list.count) {
480 status = msm_copybit(ctx, &list);
481 }
482 } else {
483 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
484 status = -EINVAL;
485 }
486 if(yv12_handle)
487 free_buffer(yv12_handle);
488 return status;
489}
490
491/** Perform a blit type operation */
492static int blit_copybit(
493 struct copybit_device_t *dev,
494 struct copybit_image_t const *dst,
495 struct copybit_image_t const *src,
496 struct copybit_region_t const *region)
497{
498 struct copybit_rect_t dr = { 0, 0, dst->w, dst->h };
499 struct copybit_rect_t sr = { 0, 0, src->w, src->h };
500 return stretch_copybit(dev, dst, src, &dr, &sr, region);
501}
502
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800503static int finish_copybit(struct copybit_device_t *dev)
504{
505 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700506 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800507}
508
Naseer Ahmed29a26812012-06-14 00:56:20 -0700509/*****************************************************************************/
510
511/** Close the copybit device */
512static int close_copybit(struct hw_device_t *dev)
513{
514 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
515 if (ctx) {
516 close(ctx->mFD);
517 free(ctx);
518 }
519 return 0;
520}
521
Terence Hampson0124cc72013-04-18 23:45:56 -0700522static int flush_get_fence(struct copybit_device_t *dev, int* fd)
523{
524 return -1;
525}
526
Naseer Ahmed29a26812012-06-14 00:56:20 -0700527/** Open a new instance of a copybit device using name */
528static int open_copybit(const struct hw_module_t* module, const char* name,
529 struct hw_device_t** device)
530{
531 int status = -EINVAL;
532 copybit_context_t *ctx;
533 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
534 memset(ctx, 0, sizeof(*ctx));
535
536 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
537 ctx->device.common.version = 1;
538 ctx->device.common.module = const_cast<hw_module_t*>(module);
539 ctx->device.common.close = close_copybit;
540 ctx->device.set_parameter = set_parameter_copybit;
541 ctx->device.get = get;
542 ctx->device.blit = blit_copybit;
543 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800544 ctx->device.finish = finish_copybit;
Terence Hampson0124cc72013-04-18 23:45:56 -0700545 ctx->device.flush_get_fence = flush_get_fence;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700546 ctx->mAlpha = MDP_ALPHA_NOP;
547 ctx->mFlags = 0;
548 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
549 if (ctx->mFD < 0) {
550 status = errno;
551 ALOGE("Error opening frame buffer errno=%d (%s)",
552 status, strerror(status));
553 status = -status;
554 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700555 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700556 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700557 }
558 return status;
559}