blob: 2a4b1c5f0edafd52bfdbb5a7073c0d7a6f8706bf [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;
129 case HAL_PIXEL_FORMAT_RGB_888: return MDP_RGB_888;
130 case HAL_PIXEL_FORMAT_RGBA_8888: return MDP_RGBA_8888;
131 case HAL_PIXEL_FORMAT_BGRA_8888: return MDP_BGRA_8888;
Raj kamale77f4ec2013-07-03 23:57:50 +0530132 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CRCB_H2V1;
133 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CRCB_H2V2;
134 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CBCR_H2V1;
135 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700136 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Radhika Ranjan Soni21175f92013-07-12 17:32:15 +0530137 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800138 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700139 }
140 return -1;
141}
142
143/** convert from copybit image to mdp image structure */
144static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
145{
146 private_handle_t* hnd = (private_handle_t*)rhs->handle;
147 if(hnd == NULL){
148 ALOGE("copybit: Invalid handle");
149 return;
150 }
151 img->width = rhs->w;
152 img->height = rhs->h;
153 img->format = get_format(rhs->format);
154 img->offset = hnd->offset;
155 img->memory_id = hnd->fd;
156}
157/** setup rectangles */
158static void set_rects(struct copybit_context_t *dev,
159 struct mdp_blit_req *e,
160 const struct copybit_rect_t *dst,
161 const struct copybit_rect_t *src,
162 const struct copybit_rect_t *scissor,
163 uint32_t horiz_padding,
164 uint32_t vert_padding) {
165 struct copybit_rect_t clip;
166 intersect(&clip, scissor, dst);
167
168 e->dst_rect.x = clip.l;
169 e->dst_rect.y = clip.t;
170 e->dst_rect.w = clip.r - clip.l;
171 e->dst_rect.h = clip.b - clip.t;
172
173 uint32_t W, H, delta_x, delta_y;
174 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
175 delta_x = (clip.t - dst->t);
176 delta_y = (dst->r - clip.r);
177 e->src_rect.w = (clip.b - clip.t);
178 e->src_rect.h = (clip.r - clip.l);
179 W = dst->b - dst->t;
180 H = dst->r - dst->l;
181 } else {
182 delta_x = (clip.l - dst->l);
183 delta_y = (clip.t - dst->t);
184 e->src_rect.w = (clip.r - clip.l);
185 e->src_rect.h = (clip.b - clip.t);
186 W = dst->r - dst->l;
187 H = dst->b - dst->t;
188 }
189
190 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
191 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
192
193 e->src_rect.x = delta_x + src->l;
194 e->src_rect.y = delta_y + src->t;
195
196 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
197 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
198 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
199 }else{
200 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
201 }
202 }
203
204 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
205 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
206 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
207 }else{
208 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
209 }
210 }
211}
212
213/** setup mdp request */
214static void set_infos(struct copybit_context_t *dev,
215 struct mdp_blit_req *req, int flags)
216{
217 req->alpha = dev->mAlpha;
218 req->transp_mask = MDP_TRANSP_NOP;
219 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700220 // check if we are blitting to f/b
221 if (COPYBIT_ENABLE == dev->mBlitToFB) {
222 req->flags |= MDP_MEMORY_ID_TYPE_FB;
223 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700224#if defined(COPYBIT_QSD8K)
225 req->flags |= MDP_BLEND_FG_PREMULT;
226#endif
227}
228
229/** copy the bits */
230static int msm_copybit(struct copybit_context_t *dev, void const *list)
231{
Terence Hampsonadf47302013-05-23 12:21:02 -0400232 int err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
233 (struct mdp_async_blit_req_list const*)list);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700234 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
235 if (err == 0) {
236 return 0;
237 } else {
238#if DEBUG_MDP_ERRORS
Terence Hampsonadf47302013-05-23 12:21:02 -0400239 struct mdp_async_blit_req_list const* l =
240 (struct mdp_async_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700241 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700242 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
243 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700244 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700245 ,
246 i,
247 l->req[i].src.width,
248 l->req[i].src.height,
249 l->req[i].src.format,
250 l->req[i].src_rect.x,
251 l->req[i].src_rect.y,
252 l->req[i].src_rect.w,
253 l->req[i].src_rect.h,
254 l->req[i].dst.width,
255 l->req[i].dst.height,
256 l->req[i].dst.format,
257 l->req[i].dst_rect.x,
258 l->req[i].dst_rect.y,
259 l->req[i].dst_rect.w,
260 l->req[i].dst_rect.h,
261 l->req[i].flags
262 );
263 }
264#endif
265 return -errno;
266 }
267}
268
269/*****************************************************************************/
270
271/** Set a parameter to value */
272static int set_parameter_copybit(
273 struct copybit_device_t *dev,
274 int name,
275 int value)
276{
277 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
278 int status = 0;
279 if (ctx) {
280 switch(name) {
281 case COPYBIT_ROTATION_DEG:
282 switch (value) {
283 case 0:
284 ctx->mFlags &= ~0x7;
285 break;
286 case 90:
287 ctx->mFlags &= ~0x7;
288 ctx->mFlags |= MDP_ROT_90;
289 break;
290 case 180:
291 ctx->mFlags &= ~0x7;
292 ctx->mFlags |= MDP_ROT_180;
293 break;
294 case 270:
295 ctx->mFlags &= ~0x7;
296 ctx->mFlags |= MDP_ROT_270;
297 break;
298 default:
299 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
300 status = -EINVAL;
301 break;
302 }
303 break;
304 case COPYBIT_PLANE_ALPHA:
305 if (value < 0) value = MDP_ALPHA_NOP;
306 if (value >= 256) value = 255;
307 ctx->mAlpha = value;
308 break;
309 case COPYBIT_DITHER:
310 if (value == COPYBIT_ENABLE) {
311 ctx->mFlags |= MDP_DITHER;
312 } else if (value == COPYBIT_DISABLE) {
313 ctx->mFlags &= ~MDP_DITHER;
314 }
315 break;
316 case COPYBIT_BLUR:
317 if (value == COPYBIT_ENABLE) {
318 ctx->mFlags |= MDP_BLUR;
319 } else if (value == COPYBIT_DISABLE) {
320 ctx->mFlags &= ~MDP_BLUR;
321 }
322 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800323 case COPYBIT_BLEND_MODE:
324 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800326 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700327 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
328 }
329 break;
330 case COPYBIT_TRANSFORM:
331 ctx->mFlags &= ~0x7;
332 ctx->mFlags |= value & 0x7;
333 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700334 case COPYBIT_BLIT_TO_FRAMEBUFFER:
335 if (COPYBIT_ENABLE == value) {
336 ctx->mBlitToFB = value;
337 } else if (COPYBIT_DISABLE == value) {
338 ctx->mBlitToFB = value;
339 } else {
340 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
341 __FUNCTION__, value);
342 }
343 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344 default:
345 status = -EINVAL;
346 break;
347 }
348 } else {
349 status = -EINVAL;
350 }
351 return status;
352}
353
354/** Get a static info value */
355static int get(struct copybit_device_t *dev, int name)
356{
357 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
358 int value;
359 if (ctx) {
360 switch(name) {
361 case COPYBIT_MINIFICATION_LIMIT:
362 value = MAX_SCALE_FACTOR;
363 break;
364 case COPYBIT_MAGNIFICATION_LIMIT:
365 value = MAX_SCALE_FACTOR;
366 break;
367 case COPYBIT_SCALING_FRAC_BITS:
368 value = 32;
369 break;
370 case COPYBIT_ROTATION_STEP_DEG:
371 value = 90;
372 break;
373 default:
374 value = -EINVAL;
375 }
376 } else {
377 value = -EINVAL;
378 }
379 return value;
380}
381
Terence Hampsonadf47302013-05-23 12:21:02 -0400382static int set_sync_copybit(struct copybit_device_t *dev,
383 int acquireFenceFd)
384{
385 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
386 if (acquireFenceFd != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400387 if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
388 ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400389 } else {
390 int ret = -EINVAL;
391 struct blitReq *list = &ctx->list;
392
393 // Since fence is full kick off what is already in the list
394 ret = msm_copybit(ctx, list);
395 if (ret < 0) {
396 ALOGE("%s: Blit call failed", __FUNCTION__);
397 return -EINVAL;
398 }
399 list->count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400400 list->sync.acq_fen_fd_cnt = 0;
401 ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400402 ctx->relFence = -1;
403 }
404 }
405 return 0;
406}
407
Naseer Ahmed29a26812012-06-14 00:56:20 -0700408/** do a stretch blit type operation */
409static int stretch_copybit(
410 struct copybit_device_t *dev,
411 struct copybit_image_t const *dst,
412 struct copybit_image_t const *src,
413 struct copybit_rect_t const *dst_rect,
414 struct copybit_rect_t const *src_rect,
415 struct copybit_region_t const *region)
416{
417 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Terence Hampsonadf47302013-05-23 12:21:02 -0400418 struct blitReq *list;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700419 int status = 0;
420 private_handle_t *yv12_handle = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700421
Terence Hampsonadf47302013-05-23 12:21:02 -0400422 if (ctx) {
423 list = &ctx->list;
424
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425 if (ctx->mAlpha < 255) {
426 switch (src->format) {
427 // we don't support plane alpha with RGBA formats
428 case HAL_PIXEL_FORMAT_RGBA_8888:
429 case HAL_PIXEL_FORMAT_BGRA_8888:
430 case HAL_PIXEL_FORMAT_RGBA_5551:
431 case HAL_PIXEL_FORMAT_RGBA_4444:
432 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
433 src->format);
434 return -EINVAL;
435 }
436 }
437
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800438 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
439 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700440 // this is always invalid
441 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
442 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
443
444 return -EINVAL;
445 }
446
447 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
448 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
449 return -EINVAL;
450 }
451
452 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
453 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
454 return -EINVAL;
455 }
456
457 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800458 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700459 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700460 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
461 src->format, usage)){
462 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
463 (const_cast<copybit_image_t *>(src))->format =
464 HAL_PIXEL_FORMAT_YCrCb_420_SP;
465 (const_cast<copybit_image_t *>(src))->handle =
466 yv12_handle;
467 (const_cast<copybit_image_t *>(src))->base =
468 (void *)yv12_handle->base;
469 }
470 else{
471 ALOGE("Error copybit conversion from yv12 failed");
472 if(yv12_handle)
473 free_buffer(yv12_handle);
474 return -EINVAL;
475 }
476 }
477 else{
478 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
479 return -EINVAL;
480 }
481 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400482 const uint32_t maxCount = sizeof(list->req)/sizeof(list->req[0]);
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700483 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700484 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700485 status = 0;
486 while ((status == 0) && region->next(region, &clip)) {
487 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400488 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700489 int flags = 0;
490
491 private_handle_t* src_hnd = (private_handle_t*)src->handle;
492 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
493 flags |= MDP_BLIT_NON_CACHED;
494 }
495
496 set_infos(ctx, req, flags);
497 set_image(&req->dst, dst);
498 set_image(&req->src, src);
499 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
500
501 if (req->src_rect.w<=0 || req->src_rect.h<=0)
502 continue;
503
504 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
505 continue;
506
Terence Hampsonadf47302013-05-23 12:21:02 -0400507 if (++list->count == maxCount) {
508 status = msm_copybit(ctx, list);
509 if (ctx->relFence != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400510 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400511 }
512 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700513 }
514 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400515 if(yv12_handle) {
516 //Before freeing the buffer we need buffer passed through blit call
517 if (list->count != 0) {
518 status = msm_copybit(ctx, list);
519 if (ctx->relFence != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400520 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400521 }
522 list->count = 0;
523 }
524 free_buffer(yv12_handle);
525 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700526 } else {
527 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
528 status = -EINVAL;
529 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700530 return status;
531}
532
533/** Perform a blit type operation */
534static int blit_copybit(
535 struct copybit_device_t *dev,
536 struct copybit_image_t const *dst,
537 struct copybit_image_t const *src,
538 struct copybit_region_t const *region)
539{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700540 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
541 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700542 return stretch_copybit(dev, dst, src, &dr, &sr, region);
543}
544
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800545static int finish_copybit(struct copybit_device_t *dev)
546{
547 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700548 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800549}
550
Naseer Ahmed29a26812012-06-14 00:56:20 -0700551/*****************************************************************************/
552
553/** Close the copybit device */
554static int close_copybit(struct hw_device_t *dev)
555{
556 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
557 if (ctx) {
558 close(ctx->mFD);
559 free(ctx);
560 }
561 return 0;
562}
563
Terence Hampson0124cc72013-04-18 23:45:56 -0700564static int flush_get_fence(struct copybit_device_t *dev, int* fd)
565{
Terence Hampsonadf47302013-05-23 12:21:02 -0400566 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
567 struct blitReq *list = &ctx->list;
568 int ret = -EINVAL;
569
570 if (list->count) {
571 ret = msm_copybit(ctx, list);
572 if (ret < 0)
573 ALOGE("%s: Blit call failed", __FUNCTION__);
574 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400575 }
576 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400577 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400578 ctx->relFence = -1;
579 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700580}
581
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582/** Open a new instance of a copybit device using name */
583static int open_copybit(const struct hw_module_t* module, const char* name,
584 struct hw_device_t** device)
585{
586 int status = -EINVAL;
587 copybit_context_t *ctx;
588 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
589 memset(ctx, 0, sizeof(*ctx));
590
591 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
592 ctx->device.common.version = 1;
593 ctx->device.common.module = const_cast<hw_module_t*>(module);
594 ctx->device.common.close = close_copybit;
595 ctx->device.set_parameter = set_parameter_copybit;
596 ctx->device.get = get;
597 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400598 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700599 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800600 ctx->device.finish = finish_copybit;
Terence Hampson0124cc72013-04-18 23:45:56 -0700601 ctx->device.flush_get_fence = flush_get_fence;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700602 ctx->mAlpha = MDP_ALPHA_NOP;
603 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400604 ctx->sync.flags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400605 ctx->sync.acq_fen_fd = ctx->acqFence;
606 ctx->sync.rel_fen_fd = &ctx->relFence;
607 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400608 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400609 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
610 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700611 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
612 if (ctx->mFD < 0) {
613 status = errno;
614 ALOGE("Error opening frame buffer errno=%d (%s)",
615 status, strerror(status));
616 status = -status;
617 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700618 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700619 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700620 }
621 return status;
622}