blob: 3b7039c8584e2794fed59efe2173b3572c231a8c [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:
444 case HAL_PIXEL_FORMAT_RGBA_5551:
445 case HAL_PIXEL_FORMAT_RGBA_4444:
446 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
447 src->format);
448 return -EINVAL;
449 }
450 }
451
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800452 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
453 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700454 // this is always invalid
455 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
456 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
457
458 return -EINVAL;
459 }
460
461 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
462 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
463 return -EINVAL;
464 }
465
466 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
467 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
468 return -EINVAL;
469 }
470
471 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800472 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700473 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700474 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
475 src->format, usage)){
476 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
477 (const_cast<copybit_image_t *>(src))->format =
478 HAL_PIXEL_FORMAT_YCrCb_420_SP;
479 (const_cast<copybit_image_t *>(src))->handle =
480 yv12_handle;
481 (const_cast<copybit_image_t *>(src))->base =
482 (void *)yv12_handle->base;
483 }
484 else{
485 ALOGE("Error copybit conversion from yv12 failed");
486 if(yv12_handle)
487 free_buffer(yv12_handle);
488 return -EINVAL;
489 }
490 }
491 else{
492 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
493 return -EINVAL;
494 }
495 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400496 const uint32_t maxCount = sizeof(list->req)/sizeof(list->req[0]);
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700497 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700498 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700499 status = 0;
500 while ((status == 0) && region->next(region, &clip)) {
501 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400502 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700503 int flags = 0;
504
505 private_handle_t* src_hnd = (private_handle_t*)src->handle;
506 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
507 flags |= MDP_BLIT_NON_CACHED;
508 }
509
510 set_infos(ctx, req, flags);
511 set_image(&req->dst, dst);
512 set_image(&req->src, src);
513 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
514
515 if (req->src_rect.w<=0 || req->src_rect.h<=0)
516 continue;
517
518 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
519 continue;
520
Terence Hampsonadf47302013-05-23 12:21:02 -0400521 if (++list->count == maxCount) {
522 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400523 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400524 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700525 }
526 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400527 if(yv12_handle) {
528 //Before freeing the buffer we need buffer passed through blit call
529 if (list->count != 0) {
530 status = msm_copybit(ctx, list);
Terence Hampsonb6e4b1e2013-09-13 17:17:11 -0400531 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400532 list->count = 0;
533 }
534 free_buffer(yv12_handle);
535 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700536 } else {
537 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
538 status = -EINVAL;
539 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700540 return status;
541}
542
543/** Perform a blit type operation */
544static int blit_copybit(
545 struct copybit_device_t *dev,
546 struct copybit_image_t const *dst,
547 struct copybit_image_t const *src,
548 struct copybit_region_t const *region)
549{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700550 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
551 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700552 return stretch_copybit(dev, dst, src, &dr, &sr, region);
553}
554
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800555static int finish_copybit(struct copybit_device_t *dev)
556{
557 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700558 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800559}
560
Naseer Ahmed29a26812012-06-14 00:56:20 -0700561/*****************************************************************************/
562
563/** Close the copybit device */
564static int close_copybit(struct hw_device_t *dev)
565{
566 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
567 if (ctx) {
568 close(ctx->mFD);
569 free(ctx);
570 }
571 return 0;
572}
573
Terence Hampson0124cc72013-04-18 23:45:56 -0700574static int flush_get_fence(struct copybit_device_t *dev, int* fd)
575{
Terence Hampsonadf47302013-05-23 12:21:02 -0400576 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
577 struct blitReq *list = &ctx->list;
578 int ret = -EINVAL;
579
580 if (list->count) {
581 ret = msm_copybit(ctx, list);
582 if (ret < 0)
583 ALOGE("%s: Blit call failed", __FUNCTION__);
584 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400585 }
586 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400587 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400588 ctx->relFence = -1;
589 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700590}
591
Naseer Ahmed29a26812012-06-14 00:56:20 -0700592/** Open a new instance of a copybit device using name */
593static int open_copybit(const struct hw_module_t* module, const char* name,
594 struct hw_device_t** device)
595{
596 int status = -EINVAL;
597 copybit_context_t *ctx;
598 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
599 memset(ctx, 0, sizeof(*ctx));
600
601 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
602 ctx->device.common.version = 1;
603 ctx->device.common.module = const_cast<hw_module_t*>(module);
604 ctx->device.common.close = close_copybit;
605 ctx->device.set_parameter = set_parameter_copybit;
606 ctx->device.get = get;
607 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400608 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700609 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800610 ctx->device.finish = finish_copybit;
Terence Hampson0124cc72013-04-18 23:45:56 -0700611 ctx->device.flush_get_fence = flush_get_fence;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700612 ctx->mAlpha = MDP_ALPHA_NOP;
613 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400614 ctx->sync.flags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400615 ctx->sync.acq_fen_fd = ctx->acqFence;
616 ctx->sync.rel_fen_fd = &ctx->relFence;
617 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400618 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400619 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
620 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700621 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
622 if (ctx->mFD < 0) {
623 status = errno;
624 ALOGE("Error opening frame buffer errno=%d (%s)",
625 status, strerror(status));
626 status = -status;
627 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700628 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700629 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700630 }
631 return status;
632}