blob: 521d55d5bb469e96c6b635a00a5bbf46bdbb3ffd [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;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700132 case HAL_PIXEL_FORMAT_YCrCb_422_I: return MDP_YCRYCB_H2V1;
133 case HAL_PIXEL_FORMAT_YCbCr_422_I: return MDP_YCBYCR_H2V1;
Raj kamale77f4ec2013-07-03 23:57:50 +0530134 case HAL_PIXEL_FORMAT_YCrCb_422_SP: return MDP_Y_CRCB_H2V1;
135 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return MDP_Y_CRCB_H2V2;
136 case HAL_PIXEL_FORMAT_YCbCr_422_SP: return MDP_Y_CBCR_H2V1;
137 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: return MDP_Y_CBCR_H2V2_ADRENO;
Radhika Ranjan Soni21175f92013-07-12 17:32:15 +0530139 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: return MDP_Y_CBCR_H2V2_VENUS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800140 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: return MDP_Y_CBCR_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700141 }
142 return -1;
143}
144
145/** convert from copybit image to mdp image structure */
146static void set_image(struct mdp_img *img, const struct copybit_image_t *rhs)
147{
148 private_handle_t* hnd = (private_handle_t*)rhs->handle;
149 if(hnd == NULL){
150 ALOGE("copybit: Invalid handle");
151 return;
152 }
153 img->width = rhs->w;
154 img->height = rhs->h;
155 img->format = get_format(rhs->format);
156 img->offset = hnd->offset;
157 img->memory_id = hnd->fd;
158}
159/** setup rectangles */
160static void set_rects(struct copybit_context_t *dev,
161 struct mdp_blit_req *e,
162 const struct copybit_rect_t *dst,
163 const struct copybit_rect_t *src,
164 const struct copybit_rect_t *scissor,
165 uint32_t horiz_padding,
166 uint32_t vert_padding) {
167 struct copybit_rect_t clip;
168 intersect(&clip, scissor, dst);
169
170 e->dst_rect.x = clip.l;
171 e->dst_rect.y = clip.t;
172 e->dst_rect.w = clip.r - clip.l;
173 e->dst_rect.h = clip.b - clip.t;
174
175 uint32_t W, H, delta_x, delta_y;
176 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
177 delta_x = (clip.t - dst->t);
178 delta_y = (dst->r - clip.r);
179 e->src_rect.w = (clip.b - clip.t);
180 e->src_rect.h = (clip.r - clip.l);
181 W = dst->b - dst->t;
182 H = dst->r - dst->l;
183 } else {
184 delta_x = (clip.l - dst->l);
185 delta_y = (clip.t - dst->t);
186 e->src_rect.w = (clip.r - clip.l);
187 e->src_rect.h = (clip.b - clip.t);
188 W = dst->r - dst->l;
189 H = dst->b - dst->t;
190 }
191
192 MULDIV(&delta_x, &e->src_rect.w, src->r - src->l, W);
193 MULDIV(&delta_y, &e->src_rect.h, src->b - src->t, H);
194
195 e->src_rect.x = delta_x + src->l;
196 e->src_rect.y = delta_y + src->t;
197
198 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_V) {
199 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
200 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
201 }else{
202 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
203 }
204 }
205
206 if (dev->mFlags & COPYBIT_TRANSFORM_FLIP_H) {
207 if (dev->mFlags & COPYBIT_TRANSFORM_ROT_90) {
208 e->src_rect.y = (src->t + src->b) - (e->src_rect.y + e->src_rect.h);
209 }else{
210 e->src_rect.x = (src->l + src->r) - (e->src_rect.x + e->src_rect.w);
211 }
212 }
213}
214
215/** setup mdp request */
216static void set_infos(struct copybit_context_t *dev,
217 struct mdp_blit_req *req, int flags)
218{
219 req->alpha = dev->mAlpha;
220 req->transp_mask = MDP_TRANSP_NOP;
221 req->flags = dev->mFlags | flags;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700222 // check if we are blitting to f/b
223 if (COPYBIT_ENABLE == dev->mBlitToFB) {
224 req->flags |= MDP_MEMORY_ID_TYPE_FB;
225 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700226#if defined(COPYBIT_QSD8K)
227 req->flags |= MDP_BLEND_FG_PREMULT;
228#endif
229}
230
231/** copy the bits */
232static int msm_copybit(struct copybit_context_t *dev, void const *list)
233{
Terence Hampsonadf47302013-05-23 12:21:02 -0400234 int err = ioctl(dev->mFD, MSMFB_ASYNC_BLIT,
235 (struct mdp_async_blit_req_list const*)list);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700236 ALOGE_IF(err<0, "copyBits failed (%s)", strerror(errno));
237 if (err == 0) {
238 return 0;
239 } else {
240#if DEBUG_MDP_ERRORS
Terence Hampsonadf47302013-05-23 12:21:02 -0400241 struct mdp_async_blit_req_list const* l =
242 (struct mdp_async_blit_req_list const*)list;
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700243 for (unsigned int i=0 ; i<l->count ; i++) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700244 ALOGE("%d: src={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
245 " dst={w=%d, h=%d, f=%d, rect={%d,%d,%d,%d}}\n"
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700246 " flags=%08x"
Naseer Ahmed29a26812012-06-14 00:56:20 -0700247 ,
248 i,
249 l->req[i].src.width,
250 l->req[i].src.height,
251 l->req[i].src.format,
252 l->req[i].src_rect.x,
253 l->req[i].src_rect.y,
254 l->req[i].src_rect.w,
255 l->req[i].src_rect.h,
256 l->req[i].dst.width,
257 l->req[i].dst.height,
258 l->req[i].dst.format,
259 l->req[i].dst_rect.x,
260 l->req[i].dst_rect.y,
261 l->req[i].dst_rect.w,
262 l->req[i].dst_rect.h,
263 l->req[i].flags
264 );
265 }
266#endif
267 return -errno;
268 }
269}
270
271/*****************************************************************************/
272
273/** Set a parameter to value */
274static int set_parameter_copybit(
275 struct copybit_device_t *dev,
276 int name,
277 int value)
278{
279 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
280 int status = 0;
281 if (ctx) {
282 switch(name) {
283 case COPYBIT_ROTATION_DEG:
284 switch (value) {
285 case 0:
286 ctx->mFlags &= ~0x7;
287 break;
288 case 90:
289 ctx->mFlags &= ~0x7;
290 ctx->mFlags |= MDP_ROT_90;
291 break;
292 case 180:
293 ctx->mFlags &= ~0x7;
294 ctx->mFlags |= MDP_ROT_180;
295 break;
296 case 270:
297 ctx->mFlags &= ~0x7;
298 ctx->mFlags |= MDP_ROT_270;
299 break;
300 default:
301 ALOGE("Invalid value for COPYBIT_ROTATION_DEG");
302 status = -EINVAL;
303 break;
304 }
305 break;
306 case COPYBIT_PLANE_ALPHA:
307 if (value < 0) value = MDP_ALPHA_NOP;
308 if (value >= 256) value = 255;
309 ctx->mAlpha = value;
310 break;
311 case COPYBIT_DITHER:
312 if (value == COPYBIT_ENABLE) {
313 ctx->mFlags |= MDP_DITHER;
314 } else if (value == COPYBIT_DISABLE) {
315 ctx->mFlags &= ~MDP_DITHER;
316 }
317 break;
318 case COPYBIT_BLUR:
319 if (value == COPYBIT_ENABLE) {
320 ctx->mFlags |= MDP_BLUR;
321 } else if (value == COPYBIT_DISABLE) {
322 ctx->mFlags &= ~MDP_BLUR;
323 }
324 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800325 case COPYBIT_BLEND_MODE:
326 if(value == COPYBIT_BLENDING_PREMULT) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700327 ctx->mFlags |= MDP_BLEND_FG_PREMULT;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800328 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700329 ctx->mFlags &= ~MDP_BLEND_FG_PREMULT;
330 }
331 break;
332 case COPYBIT_TRANSFORM:
333 ctx->mFlags &= ~0x7;
334 ctx->mFlags |= value & 0x7;
335 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700336 case COPYBIT_BLIT_TO_FRAMEBUFFER:
337 if (COPYBIT_ENABLE == value) {
338 ctx->mBlitToFB = value;
339 } else if (COPYBIT_DISABLE == value) {
340 ctx->mBlitToFB = value;
341 } else {
342 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
343 __FUNCTION__, value);
344 }
345 break;
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530346 case COPYBIT_FG_LAYER:
347 if(value == COPYBIT_ENABLE) {
348 ctx->mFlags |= MDP_IS_FG;
349 } else if (value == COPYBIT_DISABLE) {
350 ctx->mFlags &= ~MDP_IS_FG;
351 }
352 break ;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700353 default:
354 status = -EINVAL;
355 break;
356 }
357 } else {
358 status = -EINVAL;
359 }
360 return status;
361}
362
363/** Get a static info value */
364static int get(struct copybit_device_t *dev, int name)
365{
366 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
367 int value;
368 if (ctx) {
369 switch(name) {
370 case COPYBIT_MINIFICATION_LIMIT:
371 value = MAX_SCALE_FACTOR;
372 break;
373 case COPYBIT_MAGNIFICATION_LIMIT:
374 value = MAX_SCALE_FACTOR;
375 break;
376 case COPYBIT_SCALING_FRAC_BITS:
377 value = 32;
378 break;
379 case COPYBIT_ROTATION_STEP_DEG:
380 value = 90;
381 break;
382 default:
383 value = -EINVAL;
384 }
385 } else {
386 value = -EINVAL;
387 }
388 return value;
389}
390
Terence Hampsonadf47302013-05-23 12:21:02 -0400391static int set_sync_copybit(struct copybit_device_t *dev,
392 int acquireFenceFd)
393{
394 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
395 if (acquireFenceFd != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400396 if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
397 ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400398 } else {
399 int ret = -EINVAL;
400 struct blitReq *list = &ctx->list;
401
402 // Since fence is full kick off what is already in the list
403 ret = msm_copybit(ctx, list);
404 if (ret < 0) {
405 ALOGE("%s: Blit call failed", __FUNCTION__);
406 return -EINVAL;
407 }
408 list->count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400409 list->sync.acq_fen_fd_cnt = 0;
410 ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
Terence Hampsonadf47302013-05-23 12:21:02 -0400411 ctx->relFence = -1;
412 }
413 }
414 return 0;
415}
416
Naseer Ahmed29a26812012-06-14 00:56:20 -0700417/** do a stretch blit type operation */
418static int stretch_copybit(
419 struct copybit_device_t *dev,
420 struct copybit_image_t const *dst,
421 struct copybit_image_t const *src,
422 struct copybit_rect_t const *dst_rect,
423 struct copybit_rect_t const *src_rect,
424 struct copybit_region_t const *region)
425{
426 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Terence Hampsonadf47302013-05-23 12:21:02 -0400427 struct blitReq *list;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700428 int status = 0;
429 private_handle_t *yv12_handle = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700430
Terence Hampsonadf47302013-05-23 12:21:02 -0400431 if (ctx) {
432 list = &ctx->list;
433
Naseer Ahmed29a26812012-06-14 00:56:20 -0700434 if (ctx->mAlpha < 255) {
435 switch (src->format) {
436 // we don't support plane alpha with RGBA formats
437 case HAL_PIXEL_FORMAT_RGBA_8888:
438 case HAL_PIXEL_FORMAT_BGRA_8888:
439 case HAL_PIXEL_FORMAT_RGBA_5551:
440 case HAL_PIXEL_FORMAT_RGBA_4444:
441 ALOGE ("%s : Unsupported Pixel format %d", __FUNCTION__,
442 src->format);
443 return -EINVAL;
444 }
445 }
446
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800447 if (src_rect->l < 0 || (uint32_t)src_rect->r > src->w ||
448 src_rect->t < 0 || (uint32_t)src_rect->b > src->h) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700449 // this is always invalid
450 ALOGE ("%s : Invalid source rectangle : src_rect l %d t %d r %d b %d",\
451 __FUNCTION__, src_rect->l, src_rect->t, src_rect->r, src_rect->b);
452
453 return -EINVAL;
454 }
455
456 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
457 ALOGE ("%s : Invalid source dimensions w %d h %d", __FUNCTION__, src->w, src->h);
458 return -EINVAL;
459 }
460
461 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
462 ALOGE ("%s : Invalid DST dimensions w %d h %d", __FUNCTION__, dst->w, dst->h);
463 return -EINVAL;
464 }
465
466 if(src->format == HAL_PIXEL_FORMAT_YV12) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800467 int usage =
Terence Hampson0124cc72013-04-18 23:45:56 -0700468 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP | GRALLOC_USAGE_PRIVATE_UNCACHED;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700469 if (0 == alloc_buffer(&yv12_handle,src->w,src->h,
470 src->format, usage)){
471 if(0 == convertYV12toYCrCb420SP(src,yv12_handle)){
472 (const_cast<copybit_image_t *>(src))->format =
473 HAL_PIXEL_FORMAT_YCrCb_420_SP;
474 (const_cast<copybit_image_t *>(src))->handle =
475 yv12_handle;
476 (const_cast<copybit_image_t *>(src))->base =
477 (void *)yv12_handle->base;
478 }
479 else{
480 ALOGE("Error copybit conversion from yv12 failed");
481 if(yv12_handle)
482 free_buffer(yv12_handle);
483 return -EINVAL;
484 }
485 }
486 else{
487 ALOGE("Error:unable to allocate memeory for yv12 software conversion");
488 return -EINVAL;
489 }
490 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400491 const uint32_t maxCount = sizeof(list->req)/sizeof(list->req[0]);
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700492 const struct copybit_rect_t bounds = { 0, 0, (int)dst->w, (int)dst->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700493 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700494 status = 0;
495 while ((status == 0) && region->next(region, &clip)) {
496 intersect(&clip, &bounds, &clip);
Terence Hampsonadf47302013-05-23 12:21:02 -0400497 mdp_blit_req* req = &list->req[list->count];
Naseer Ahmed29a26812012-06-14 00:56:20 -0700498 int flags = 0;
499
500 private_handle_t* src_hnd = (private_handle_t*)src->handle;
501 if(src_hnd != NULL && src_hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
502 flags |= MDP_BLIT_NON_CACHED;
503 }
504
505 set_infos(ctx, req, flags);
506 set_image(&req->dst, dst);
507 set_image(&req->src, src);
508 set_rects(ctx, req, dst_rect, src_rect, &clip, src->horiz_padding, src->vert_padding);
509
510 if (req->src_rect.w<=0 || req->src_rect.h<=0)
511 continue;
512
513 if (req->dst_rect.w<=0 || req->dst_rect.h<=0)
514 continue;
515
Terence Hampsonadf47302013-05-23 12:21:02 -0400516 if (++list->count == maxCount) {
517 status = msm_copybit(ctx, list);
518 if (ctx->relFence != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400519 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400520 }
521 list->count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700522 }
523 }
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400524 if(yv12_handle) {
525 //Before freeing the buffer we need buffer passed through blit call
526 if (list->count != 0) {
527 status = msm_copybit(ctx, list);
528 if (ctx->relFence != -1) {
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400529 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonc67e87f2013-07-04 15:45:47 -0400530 }
531 list->count = 0;
532 }
533 free_buffer(yv12_handle);
534 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700535 } else {
536 ALOGE ("%s : Invalid COPYBIT context", __FUNCTION__);
537 status = -EINVAL;
538 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700539 return status;
540}
541
542/** Perform a blit type operation */
543static int blit_copybit(
544 struct copybit_device_t *dev,
545 struct copybit_image_t const *dst,
546 struct copybit_image_t const *src,
547 struct copybit_region_t const *region)
548{
Dhivya Subramanian7c4baa42013-06-21 14:44:15 -0700549 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
550 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Naseer Ahmed29a26812012-06-14 00:56:20 -0700551 return stretch_copybit(dev, dst, src, &dr, &sr, region);
552}
553
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800554static int finish_copybit(struct copybit_device_t *dev)
555{
556 // NOP for MDP copybit
Terence Hampson0124cc72013-04-18 23:45:56 -0700557 return 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800558}
559
Naseer Ahmed29a26812012-06-14 00:56:20 -0700560/*****************************************************************************/
561
562/** Close the copybit device */
563static int close_copybit(struct hw_device_t *dev)
564{
565 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
566 if (ctx) {
567 close(ctx->mFD);
568 free(ctx);
569 }
570 return 0;
571}
572
Terence Hampson0124cc72013-04-18 23:45:56 -0700573static int flush_get_fence(struct copybit_device_t *dev, int* fd)
574{
Terence Hampsonadf47302013-05-23 12:21:02 -0400575 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
576 struct blitReq *list = &ctx->list;
577 int ret = -EINVAL;
578
579 if (list->count) {
580 ret = msm_copybit(ctx, list);
581 if (ret < 0)
582 ALOGE("%s: Blit call failed", __FUNCTION__);
583 list->count = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400584 }
585 *fd = ctx->relFence;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400586 list->sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400587 ctx->relFence = -1;
588 return ret;
Terence Hampson0124cc72013-04-18 23:45:56 -0700589}
590
Naseer Ahmed29a26812012-06-14 00:56:20 -0700591/** Open a new instance of a copybit device using name */
592static int open_copybit(const struct hw_module_t* module, const char* name,
593 struct hw_device_t** device)
594{
595 int status = -EINVAL;
596 copybit_context_t *ctx;
597 ctx = (copybit_context_t *)malloc(sizeof(copybit_context_t));
598 memset(ctx, 0, sizeof(*ctx));
599
600 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
601 ctx->device.common.version = 1;
602 ctx->device.common.module = const_cast<hw_module_t*>(module);
603 ctx->device.common.close = close_copybit;
604 ctx->device.set_parameter = set_parameter_copybit;
605 ctx->device.get = get;
606 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -0400607 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700608 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800609 ctx->device.finish = finish_copybit;
Terence Hampson0124cc72013-04-18 23:45:56 -0700610 ctx->device.flush_get_fence = flush_get_fence;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700611 ctx->mAlpha = MDP_ALPHA_NOP;
612 ctx->mFlags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400613 ctx->sync.flags = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400614 ctx->sync.acq_fen_fd = ctx->acqFence;
615 ctx->sync.rel_fen_fd = &ctx->relFence;
616 ctx->list.count = 0;
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400617 ctx->list.sync.acq_fen_fd_cnt = 0;
Terence Hampsonadf47302013-05-23 12:21:02 -0400618 ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
619 ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700620 ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
621 if (ctx->mFD < 0) {
622 status = errno;
623 ALOGE("Error opening frame buffer errno=%d (%s)",
624 status, strerror(status));
625 status = -status;
626 } else {
Terence Hampson0124cc72013-04-18 23:45:56 -0700627 status = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700628 *device = &ctx->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700629 }
630 return status;
631}