blob: b804c217408822c59defd86aa7f033fc8b856e3c [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 */
Naseer Ahmed29a26812012-06-14 00:56:20 -070020#include <cutils/log.h>
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -080021#include <sys/resource.h>
22#include <sys/prctl.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070023
24#include <stdint.h>
25#include <string.h>
26#include <unistd.h>
27#include <errno.h>
28#include <fcntl.h>
29
30#include <sys/ioctl.h>
31#include <sys/types.h>
32#include <sys/mman.h>
33
34#include <linux/msm_kgsl.h>
35
36#include <EGL/eglplatform.h>
37#include <cutils/native_handle.h>
38#include <cutils/ashmem.h>
39#include <linux/ashmem.h>
40#include <gralloc_priv.h>
41
42#include <copybit.h>
43#include <alloc_controller.h>
44#include <memalloc.h>
45
46#include "c2d2.h"
47#include "software_converter.h"
48
49#include <dlfcn.h>
50
51using gralloc::IMemAlloc;
52using gralloc::IonController;
53using gralloc::alloc_data;
Naseer Ahmed29a26812012-06-14 00:56:20 -070054
55C2D_STATUS (*LINK_c2dCreateSurface)( uint32 *surface_id,
56 uint32 surface_bits,
57 C2D_SURFACE_TYPE surface_type,
58 void *surface_definition );
59
60C2D_STATUS (*LINK_c2dUpdateSurface)( uint32 surface_id,
61 uint32 surface_bits,
62 C2D_SURFACE_TYPE surface_type,
63 void *surface_definition );
64
65C2D_STATUS (*LINK_c2dReadSurface)( uint32 surface_id,
66 C2D_SURFACE_TYPE surface_type,
67 void *surface_definition,
68 int32 x, int32 y );
69
70C2D_STATUS (*LINK_c2dDraw)( uint32 target_id,
71 uint32 target_config, C2D_RECT *target_scissor,
72 uint32 target_mask_id, uint32 target_color_key,
73 C2D_OBJECT *objects_list, uint32 num_objects );
74
75C2D_STATUS (*LINK_c2dFinish)( uint32 target_id);
76
77C2D_STATUS (*LINK_c2dFlush)( uint32 target_id, c2d_ts_handle *timestamp);
78
79C2D_STATUS (*LINK_c2dWaitTimestamp)( c2d_ts_handle timestamp );
80
81C2D_STATUS (*LINK_c2dDestroySurface)( uint32 surface_id );
82
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -080083C2D_STATUS (*LINK_c2dMapAddr) ( int mem_fd, void * hostptr, uint32 len,
84 uint32 offset, uint32 flags, void ** gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -070085
86C2D_STATUS (*LINK_c2dUnMapAddr) ( void * gpuaddr);
87
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -080088C2D_STATUS (*LINK_c2dGetDriverCapabilities) ( C2D_DRIVER_INFO * driver_info);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -080089
90/* create a fence fd for the timestamp */
91C2D_STATUS (*LINK_c2dCreateFenceFD) ( uint32 target_id, c2d_ts_handle timestamp,
92 int32 *fd);
Naseer Ahmed183939d2013-03-14 20:44:17 -040093
94C2D_STATUS (*LINK_c2dFillSurface) ( uint32 surface_id, uint32 fill_color,
95 C2D_RECT * fill_rect);
96
Naseer Ahmed29a26812012-06-14 00:56:20 -070097/******************************************************************************/
98
99#if defined(COPYBIT_Z180)
100#define MAX_SCALE_FACTOR (4096)
101#define MAX_DIMENSION (4096)
102#else
103#error "Unsupported HW version"
104#endif
105
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800106// The following defines can be changed as required i.e. as we encounter
107// complex use cases.
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700108#define MAX_RGB_SURFACES 12 // Max. RGB layers currently supported per draw
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800109#define MAX_YUV_2_PLANE_SURFACES 4// Max. 2-plane YUV layers currently supported per draw
110#define MAX_YUV_3_PLANE_SURFACES 1// Max. 3-plane YUV layers currently supported per draw
111// +1 for the destination surface. We cannot have multiple destination surfaces.
112#define MAX_SURFACES (MAX_RGB_SURFACES + MAX_YUV_2_PLANE_SURFACES + MAX_YUV_3_PLANE_SURFACES + 1)
113#define NUM_SURFACE_TYPES 3 // RGB_SURFACE + YUV_SURFACE_2_PLANES + YUV_SURFACE_3_PLANES
114#define MAX_BLIT_OBJECT_COUNT 50 // Max. blit objects that can be passed per draw
Naseer Ahmed29a26812012-06-14 00:56:20 -0700115
116enum {
117 RGB_SURFACE,
118 YUV_SURFACE_2_PLANES,
119 YUV_SURFACE_3_PLANES
120};
121
122enum eConversionType {
123 CONVERT_TO_ANDROID_FORMAT,
124 CONVERT_TO_C2D_FORMAT
125};
126
127enum eC2DFlags {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800128 FLAGS_PREMULTIPLIED_ALPHA = 1<<0,
129 FLAGS_YUV_DESTINATION = 1<<1,
130 FLAGS_TEMP_SRC_DST = 1<<2
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131};
132
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700133static gralloc::IAllocController* sAlloc = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134/******************************************************************************/
135
136/** State information for each device instance */
137struct copybit_context_t {
138 struct copybit_device_t device;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800139 // Templates for the various source surfaces. These templates are created
140 // to avoid the expensive create/destroy C2D Surfaces
141 C2D_OBJECT_STR blit_rgb_object[MAX_RGB_SURFACES];
142 C2D_OBJECT_STR blit_yuv_2_plane_object[MAX_YUV_2_PLANE_SURFACES];
143 C2D_OBJECT_STR blit_yuv_3_plane_object[MAX_YUV_3_PLANE_SURFACES];
144 C2D_OBJECT_STR blit_list[MAX_BLIT_OBJECT_COUNT]; // Z-ordered list of blit objects
145 C2D_DRIVER_INFO c2d_driver_info;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700146 void *libc2d2;
147 alloc_data temp_src_buffer;
148 alloc_data temp_dst_buffer;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800149 unsigned int dst[NUM_SURFACE_TYPES]; // dst surfaces
150 unsigned int mapped_gpu_addr[MAX_SURFACES]; // GPU addresses mapped inside copybit
151 int blit_rgb_count; // Total RGB surfaces being blit
152 int blit_yuv_2_plane_count; // Total 2 plane YUV surfaces being
153 int blit_yuv_3_plane_count; // Total 3 plane YUV surfaces being blit
154 int blit_count; // Total blit objects.
155 unsigned int trg_transform; /* target transform */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 int fb_width;
157 int fb_height;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800158 int src_global_alpha;
159 int config_mask;
160 int dst_surface_type;
161 bool is_premultiplied_alpha;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800162 void* time_stamp;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700163 bool dst_surface_mapped; // Set when dst surface is mapped to GPU addr
164 void* dst_surface_base; // Stores the dst surface addr
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800165
166 // used for signaling the wait thread
167 bool wait_timestamp;
168 pthread_t wait_thread_id;
169 bool stop_thread;
170 pthread_mutex_t wait_cleanup_lock;
171 pthread_cond_t wait_cleanup_cond;
172
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173};
174
175struct bufferInfo {
176 int width;
177 int height;
178 int format;
179};
180
181struct yuvPlaneInfo {
182 int yStride; //luma stride
183 int plane1_stride;
184 int plane2_stride;
185 int plane1_offset;
186 int plane2_offset;
187};
188
189/**
190 * Common hardware methods
191 */
192
193static int open_copybit(const struct hw_module_t* module, const char* name,
194 struct hw_device_t** device);
195
196static struct hw_module_methods_t copybit_module_methods = {
197open: open_copybit
198};
199
200/*
201 * The COPYBIT Module
202 */
203struct copybit_module_t HAL_MODULE_INFO_SYM = {
204common: {
205tag: HARDWARE_MODULE_TAG,
206 version_major: 1,
207 version_minor: 0,
208 id: COPYBIT_HARDWARE_MODULE_ID,
209 name: "QCT COPYBIT C2D 2.0 Module",
210 author: "Qualcomm",
211 methods: &copybit_module_methods
212 }
213};
214
215
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800216/* thread function which waits on the timeStamp and cleans up the surfaces */
217static void* c2d_wait_loop(void* ptr) {
218 copybit_context_t* ctx = (copybit_context_t*)(ptr);
219 char thread_name[64] = "copybitWaitThr";
220 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
221 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
222
223 while(ctx->stop_thread == false) {
224 pthread_mutex_lock(&ctx->wait_cleanup_lock);
225 while(ctx->wait_timestamp == false && !ctx->stop_thread) {
226 pthread_cond_wait(&(ctx->wait_cleanup_cond),
227 &(ctx->wait_cleanup_lock));
228 }
229 if(ctx->wait_timestamp) {
230 if(LINK_c2dWaitTimestamp(ctx->time_stamp)) {
231 ALOGE("%s: LINK_c2dWaitTimeStamp ERROR!!", __FUNCTION__);
232 }
233 ctx->wait_timestamp = false;
234 // Unmap any mapped addresses.
235 for (int i = 0; i < MAX_SURFACES; i++) {
236 if (ctx->mapped_gpu_addr[i]) {
237 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
238 ctx->mapped_gpu_addr[i] = 0;
239 }
240 }
241 // Reset the counts after the draw.
242 ctx->blit_rgb_count = 0;
243 ctx->blit_yuv_2_plane_count = 0;
244 ctx->blit_yuv_3_plane_count = 0;
245 ctx->blit_count = 0;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700246 ctx->dst_surface_mapped = false;
247 ctx->dst_surface_base = 0;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800248 }
249 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
250 if(ctx->stop_thread)
251 break;
252 }
253 pthread_exit(NULL);
254 return NULL;
255}
256
257
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258/* convert COPYBIT_FORMAT to C2D format */
259static int get_format(int format) {
260 switch (format) {
261 case HAL_PIXEL_FORMAT_RGB_565: return C2D_COLOR_FORMAT_565_RGB;
262 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
263 C2D_FORMAT_SWAP_RB |
264 C2D_FORMAT_DISABLE_ALPHA;
265 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB |
266 C2D_FORMAT_SWAP_RB;
267 case HAL_PIXEL_FORMAT_BGRA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
268 case HAL_PIXEL_FORMAT_RGBA_5551: return C2D_COLOR_FORMAT_5551_RGBA;
269 case HAL_PIXEL_FORMAT_RGBA_4444: return C2D_COLOR_FORMAT_4444_RGBA;
270 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV12;
271 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV12;
272 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV21;
273 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: return C2D_COLOR_FORMAT_420_NV12 |
274 C2D_FORMAT_MACROTILED;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800275 default: ALOGE("%s: invalid format (0x%x",
276 __FUNCTION__, format);
277 return -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700278 }
279 return -EINVAL;
280}
281
282/* Get the C2D formats needed for conversion to YUV */
283static int get_c2d_format_for_yuv_destination(int halFormat) {
284 switch (halFormat) {
285 // We do not swap the RB when the target is YUV
286 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
287 C2D_FORMAT_DISABLE_ALPHA;
288 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800289 // The U and V need to be interchanged when the target is YUV
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV21;
291 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV21;
292 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV12;
293 default: return get_format(halFormat);
294 }
295 return -EINVAL;
296}
297
298/* ------------------------------------------------------------------- *//*!
299 * \internal
300 * \brief Get the bpp for a particular color format
301 * \param color format
302 * \return bits per pixel
303 *//* ------------------------------------------------------------------- */
304int c2diGetBpp(int32 colorformat)
305{
306
307 int c2dBpp = 0;
308
309 switch(colorformat&0xFF)
310 {
311 case C2D_COLOR_FORMAT_4444_RGBA:
312 case C2D_COLOR_FORMAT_4444_ARGB:
313 case C2D_COLOR_FORMAT_1555_ARGB:
314 case C2D_COLOR_FORMAT_565_RGB:
315 case C2D_COLOR_FORMAT_5551_RGBA:
316 c2dBpp = 16;
317 break;
318 case C2D_COLOR_FORMAT_8888_RGBA:
319 case C2D_COLOR_FORMAT_8888_ARGB:
320 c2dBpp = 32;
321 break;
322 case C2D_COLOR_FORMAT_8_L:
323 case C2D_COLOR_FORMAT_8_A:
324 c2dBpp = 8;
325 break;
326 case C2D_COLOR_FORMAT_4_A:
327 c2dBpp = 4;
328 break;
329 case C2D_COLOR_FORMAT_1:
330 c2dBpp = 1;
331 break;
332 default:
333 ALOGE("%s ERROR", __func__);
334 break;
335 }
336 return c2dBpp;
337}
338
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700339static uint32 c2d_get_gpuaddr(copybit_context_t* ctx,
340 struct private_handle_t *handle, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700341{
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700342 uint32 memtype, *gpuaddr = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700343 C2D_STATUS rc;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700344 int freeindex = 0;
345 bool mapaddr = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346
347 if(!handle)
348 return 0;
349
350 if (handle->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
351 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP))
352 memtype = KGSL_USER_MEM_TYPE_PMEM;
353 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
354 memtype = KGSL_USER_MEM_TYPE_ASHMEM;
355 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
356 memtype = KGSL_USER_MEM_TYPE_ION;
357 else {
358 ALOGE("Invalid handle flags: 0x%x", handle->flags);
359 return 0;
360 }
361
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700362 // Check for a freeindex in the mapped_gpu_addr list
363 for (freeindex = 0; freeindex < MAX_SURFACES; freeindex++) {
364 if (ctx->mapped_gpu_addr[freeindex] == 0) {
365 // free index is available
366 // map GPU addr and use this as mapped_idx
367 mapaddr = true;
368 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800369 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700370 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700371
372 if(mapaddr) {
373 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size,
374 handle->offset, memtype, (void**)&gpuaddr);
375
376 if (rc == C2D_STATUS_OK) {
377 // We have mapped the GPU address inside copybit. We need to unmap
378 // this address after the blit. Store this address
379 ctx->mapped_gpu_addr[freeindex] = (uint32) gpuaddr;
380 mapped_idx = freeindex;
381 }
382 }
383 return (uint32) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700384}
385
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800386static void unmap_gpuaddr(copybit_context_t* ctx, int mapped_idx)
387{
388 if (!ctx || (mapped_idx == -1))
389 return;
390
391 if (ctx->mapped_gpu_addr[mapped_idx]) {
392 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[mapped_idx]);
393 ctx->mapped_gpu_addr[mapped_idx] = 0;
394 }
395}
396
Naseer Ahmed29a26812012-06-14 00:56:20 -0700397static int is_supported_rgb_format(int format)
398{
399 switch(format) {
400 case HAL_PIXEL_FORMAT_RGBA_8888:
401 case HAL_PIXEL_FORMAT_RGBX_8888:
402 case HAL_PIXEL_FORMAT_RGB_565:
403 case HAL_PIXEL_FORMAT_BGRA_8888:
404 case HAL_PIXEL_FORMAT_RGBA_5551:
405 case HAL_PIXEL_FORMAT_RGBA_4444: {
406 return COPYBIT_SUCCESS;
407 }
408 default:
409 return COPYBIT_FAILURE;
410 }
411}
412
413static int get_num_planes(int format)
414{
415 switch(format) {
416 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
417 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
418 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
419 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
420 return 2;
421 }
422 case HAL_PIXEL_FORMAT_YV12: {
423 return 3;
424 }
425 default:
426 return COPYBIT_FAILURE;
427 }
428}
429
430static int is_supported_yuv_format(int format)
431{
432 switch(format) {
433 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
434 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
435 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
436 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
437 return COPYBIT_SUCCESS;
438 }
439 default:
440 return COPYBIT_FAILURE;
441 }
442}
443
444static int is_valid_destination_format(int format)
445{
446 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
447 // C2D does not support NV12Tile as a destination format.
448 return COPYBIT_FAILURE;
449 }
450 return COPYBIT_SUCCESS;
451}
452
453static int calculate_yuv_offset_and_stride(const bufferInfo& info,
454 yuvPlaneInfo& yuvInfo)
455{
456 int width = info.width;
457 int height = info.height;
458 int format = info.format;
459
460 int aligned_height = 0;
461 int aligned_width = 0, size = 0;
462
463 switch (format) {
464 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
465 /* NV12 Tile buffers have their luma height aligned to 32bytes and width
466 * aligned to 128 bytes. The chroma offset starts at an 8K boundary
467 */
468 aligned_height = ALIGN(height, 32);
469 aligned_width = ALIGN(width, 128);
470 size = aligned_width * aligned_height;
471 yuvInfo.plane1_offset = ALIGN(size,8192);
472 yuvInfo.yStride = aligned_width;
473 yuvInfo.plane1_stride = aligned_width;
474 break;
475 }
476 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
477 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
478 case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
479 aligned_width = ALIGN(width, 32);
480 yuvInfo.yStride = aligned_width;
481 yuvInfo.plane1_stride = aligned_width;
482 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
483 // The encoder requires a 2K aligned chroma offset
484 yuvInfo.plane1_offset = ALIGN(aligned_width * height, 2048);
485 } else
486 yuvInfo.plane1_offset = aligned_width * height;
487
488 break;
489 }
490 default: {
491 return COPYBIT_FAILURE;
492 }
493 }
494 return COPYBIT_SUCCESS;
495}
496
497/** create C2D surface from copybit image */
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800498static int set_image(copybit_context_t* ctx, uint32 surfaceId,
499 const struct copybit_image_t *rhs,
500 const eC2DFlags flags, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700501{
502 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
503 C2D_SURFACE_TYPE surfaceType;
504 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800505 uint32 gpuaddr = 0;
506 int c2d_format;
507 mapped_idx = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700508
509 if (flags & FLAGS_YUV_DESTINATION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800510 c2d_format = get_c2d_format_for_yuv_destination(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700511 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800512 c2d_format = get_format(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700513 }
514
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800515 if(c2d_format == -EINVAL) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700516 ALOGE("%s: invalid format", __FUNCTION__);
517 return -EINVAL;
518 }
519
520 if(handle == NULL) {
521 ALOGE("%s: invalid handle", __func__);
522 return -EINVAL;
523 }
524
525 if (handle->gpuaddr == 0) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800526 gpuaddr = c2d_get_gpuaddr(ctx, handle, mapped_idx);
527 if(!gpuaddr) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700528 ALOGE("%s: c2d_get_gpuaddr failed", __FUNCTION__);
529 return COPYBIT_FAILURE;
530 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800531 } else {
532 gpuaddr = handle->gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700533 }
534
535 /* create C2D surface */
536 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
537 /* RGB */
538 C2D_RGB_SURFACE_DEF surfaceDef;
539
540 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS);
541
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800542 surfaceDef.phys = (void*) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700543 surfaceDef.buffer = (void*) (handle->base);
544
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800545 surfaceDef.format = c2d_format |
Naseer Ahmed29a26812012-06-14 00:56:20 -0700546 ((flags & FLAGS_PREMULTIPLIED_ALPHA) ? C2D_FORMAT_PREMULTIPLIED : 0);
547 surfaceDef.width = rhs->w;
548 surfaceDef.height = rhs->h;
549 int aligned_width = ALIGN(surfaceDef.width,32);
550 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
551
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800552 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
553 &surfaceDef)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700554 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800555 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700556 status = COPYBIT_FAILURE;
557 }
558 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
559 C2D_YUV_SURFACE_DEF surfaceDef;
560 memset(&surfaceDef, 0, sizeof(surfaceDef));
561 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800562 surfaceDef.format = c2d_format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700563
564 bufferInfo info;
565 info.width = rhs->w;
566 info.height = rhs->h;
567 info.format = rhs->format;
568
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700569 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700570 status = calculate_yuv_offset_and_stride(info, yuvInfo);
571 if(status != COPYBIT_SUCCESS) {
572 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800573 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700574 }
575
576 surfaceDef.width = rhs->w;
577 surfaceDef.height = rhs->h;
578 surfaceDef.plane0 = (void*) (handle->base);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800579 surfaceDef.phys0 = (void*) (gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700580 surfaceDef.stride0 = yuvInfo.yStride;
581
582 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800583 surfaceDef.phys1 = (void*) (gpuaddr + yuvInfo.plane1_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 surfaceDef.stride1 = yuvInfo.plane1_stride;
585 if (3 == get_num_planes(rhs->format)) {
586 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800587 surfaceDef.phys2 = (void*) (gpuaddr + yuvInfo.plane2_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700588 surfaceDef.stride2 = yuvInfo.plane2_stride;
589 }
590
591 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
592 &surfaceDef)) {
593 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800594 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700595 status = COPYBIT_FAILURE;
596 }
597 } else {
598 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800599 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700600 status = COPYBIT_FAILURE;
601 }
602
603 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604}
605
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800606/** copy the bits */
607static int msm_copybit(struct copybit_context_t *ctx, unsigned int target)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700608{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800609 if (ctx->blit_count == 0) {
610 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700611 }
612
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800613 for (int i = 0; i < ctx->blit_count; i++)
614 {
615 ctx->blit_list[i].next = &(ctx->blit_list[i+1]);
616 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800617 ctx->blit_list[ctx->blit_count-1].next = NULL;
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800618 uint32_t target_transform = ctx->trg_transform;
619 if (ctx->c2d_driver_info.capabilities_mask &
620 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
621 // For A3xx - set 0x0 as the transform is set in the config_mask
622 target_transform = 0x0;
623 }
624 if(LINK_c2dDraw(target, target_transform, 0x0, 0, 0, ctx->blit_list,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800625 ctx->blit_count)) {
626 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
627 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700628 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700629 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700630}
631
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800632
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800633
634static int flush_get_fence_copybit (struct copybit_device_t *dev, int* fd)
635{
636 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
637 int status = COPYBIT_FAILURE;
638 if (!ctx)
639 return COPYBIT_FAILURE;
640 pthread_mutex_lock(&ctx->wait_cleanup_lock);
641 status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
642
643 if(LINK_c2dFlush(ctx->dst[ctx->dst_surface_type], &ctx->time_stamp)) {
644 ALOGE("%s: LINK_c2dFlush ERROR", __FUNCTION__);
645 // unlock the mutex and return failure
646 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
647 return COPYBIT_FAILURE;
648 }
649 if(LINK_c2dCreateFenceFD(ctx->dst[ctx->dst_surface_type], ctx->time_stamp,
650 fd)) {
651 ALOGE("%s: LINK_c2dCreateFenceFD ERROR", __FUNCTION__);
652 status = COPYBIT_FAILURE;
653 }
654 if(status == COPYBIT_SUCCESS) {
655 //signal the wait_thread
656 ctx->wait_timestamp = true;
657 pthread_cond_signal(&ctx->wait_cleanup_cond);
658 }
659 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
660 return status;
661}
662
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800663static int finish_copybit(struct copybit_device_t *dev)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700664{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800665 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
666 if (!ctx)
667 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700668
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800669 int status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700670
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800671 if(LINK_c2dFinish(ctx->dst[ctx->dst_surface_type])) {
672 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
673 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700674 }
675
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800676 // Unmap any mapped addresses.
677 for (int i = 0; i < MAX_SURFACES; i++) {
678 if (ctx->mapped_gpu_addr[i]) {
679 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
680 ctx->mapped_gpu_addr[i] = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700681 }
682 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700683
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800684 // Reset the counts after the draw.
685 ctx->blit_rgb_count = 0;
686 ctx->blit_yuv_2_plane_count = 0;
687 ctx->blit_yuv_3_plane_count = 0;
688 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700689 return status;
690}
691
Naseer Ahmed183939d2013-03-14 20:44:17 -0400692static int clear_copybit(struct copybit_device_t *dev,
693 struct copybit_image_t const *buf,
694 struct copybit_rect_t *rect)
695{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700696 int ret = COPYBIT_SUCCESS;
Naseer Ahmed183939d2013-03-14 20:44:17 -0400697 int flags = FLAGS_PREMULTIPLIED_ALPHA;
698 int mapped_dst_idx = -1;
699 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
700 C2D_RECT c2drect = {rect->l, rect->t, rect->r - rect->l, rect->b - rect->t};
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700701 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700702 if(!ctx->dst_surface_mapped) {
703 ret = set_image(ctx, ctx->dst[RGB_SURFACE], buf,
704 (eC2DFlags)flags, mapped_dst_idx);
705 if(ret) {
706 ALOGE("%s: set_image error", __FUNCTION__);
707 unmap_gpuaddr(ctx, mapped_dst_idx);
708 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
709 return COPYBIT_FAILURE;
710 }
711 //clear_copybit is the first call made by HWC for each composition
712 //with the dest surface, hence set dst_surface_mapped.
713 ctx->dst_surface_mapped = true;
714 ctx->dst_surface_base = buf->base;
715 ret = LINK_c2dFillSurface(ctx->dst[RGB_SURFACE], 0x0, &c2drect);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400716 }
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700717 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400718 return ret;
719}
720
721
Naseer Ahmed29a26812012-06-14 00:56:20 -0700722/** setup rectangles */
723static void set_rects(struct copybit_context_t *ctx,
724 C2D_OBJECT *c2dObject,
725 const struct copybit_rect_t *dst,
726 const struct copybit_rect_t *src,
727 const struct copybit_rect_t *scissor)
728{
729 // Set the target rect.
730 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
731 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
732 /* target rotation is 270 */
733 c2dObject->target_rect.x = (dst->t)<<16;
734 c2dObject->target_rect.y = ctx->fb_width?(ALIGN(ctx->fb_width,32)- dst->r):dst->r;
735 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
736 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
737 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
738 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
739 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
740 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
741 c2dObject->target_rect.y = (dst->l)<<16;
742 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
743 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
744 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
745 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
746 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
747 c2dObject->target_rect.x = ctx->fb_width?(ALIGN(ctx->fb_width,32) - dst->r):dst->r;
748 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
749 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
750 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
751 } else {
752 c2dObject->target_rect.x = (dst->l)<<16;
753 c2dObject->target_rect.y = (dst->t)<<16;
754 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
755 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
756 }
757 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
758
759 // Set the source rect
760 c2dObject->source_rect.x = (src->l)<<16;
761 c2dObject->source_rect.y = (src->t)<<16;
762 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
763 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
764 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
765
766 // Set the scissor rect
767 c2dObject->scissor_rect.x = scissor->l;
768 c2dObject->scissor_rect.y = scissor->t;
769 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
770 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
771 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
772}
773
Naseer Ahmed29a26812012-06-14 00:56:20 -0700774/*****************************************************************************/
775
776/** Set a parameter to value */
777static int set_parameter_copybit(
778 struct copybit_device_t *dev,
779 int name,
780 int value)
781{
782 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800783 int status = COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700784 if (!ctx) {
785 ALOGE("%s: null context", __FUNCTION__);
786 return -EINVAL;
787 }
788
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800789 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700790 switch(name) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700791 case COPYBIT_PLANE_ALPHA:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800792 {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700793 if (value < 0) value = 0;
794 if (value >= 256) value = 255;
795
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800796 ctx->src_global_alpha = value;
797 if (value < 255)
798 ctx->config_mask |= C2D_GLOBAL_ALPHA_BIT;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700799 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800800 ctx->config_mask &= ~C2D_GLOBAL_ALPHA_BIT;
801 }
802 break;
803 case COPYBIT_BLEND_MODE:
804 {
805 if (value == COPYBIT_BLENDING_NONE) {
806 ctx->config_mask |= C2D_ALPHA_BLEND_NONE;
807 ctx->is_premultiplied_alpha = true;
808 } else if (value == COPYBIT_BLENDING_PREMULT) {
809 ctx->is_premultiplied_alpha = true;
810 } else {
811 ctx->config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700812 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800813 }
814 break;
815 case COPYBIT_TRANSFORM:
816 {
817 unsigned int transform = 0;
818 uint32 config_mask = 0;
819 config_mask |= C2D_OVERRIDE_GLOBAL_TARGET_ROTATE_CONFIG;
820 if((value & 0x7) == COPYBIT_TRANSFORM_ROT_180) {
821 transform = C2D_TARGET_ROTATE_180;
822 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_180;
823 } else if((value & 0x7) == COPYBIT_TRANSFORM_ROT_270) {
824 transform = C2D_TARGET_ROTATE_90;
825 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_90;
826 } else if(value == COPYBIT_TRANSFORM_ROT_90) {
827 transform = C2D_TARGET_ROTATE_270;
828 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_270;
829 } else {
830 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_0;
831 if(value & COPYBIT_TRANSFORM_FLIP_H) {
832 config_mask |= C2D_MIRROR_H_BIT;
833 } else if(value & COPYBIT_TRANSFORM_FLIP_V) {
834 config_mask |= C2D_MIRROR_V_BIT;
835 }
836 }
837
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800838 if (ctx->c2d_driver_info.capabilities_mask &
839 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
840 ctx->config_mask |= config_mask;
841 } else {
842 // The transform for this surface does not match the current
843 // target transform. Draw all previous surfaces. This will be
844 // changed once we have a new mechanism to send different
845 // target rotations to c2d.
846 finish_copybit(dev);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800847 }
848 ctx->trg_transform = transform;
849 }
850 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700851 case COPYBIT_FRAMEBUFFER_WIDTH:
852 ctx->fb_width = value;
853 break;
854 case COPYBIT_FRAMEBUFFER_HEIGHT:
855 ctx->fb_height = value;
856 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800857 case COPYBIT_ROTATION_DEG:
858 case COPYBIT_DITHER:
859 case COPYBIT_BLUR:
Naseer Ahmed45a99602012-07-31 19:15:24 -0700860 case COPYBIT_BLIT_TO_FRAMEBUFFER:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800861 // Do nothing
Naseer Ahmed45a99602012-07-31 19:15:24 -0700862 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700863 default:
864 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800865 status = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700866 break;
867 }
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800868 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
869 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700870}
871
872/** Get a static info value */
873static int get(struct copybit_device_t *dev, int name)
874{
875 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
876 int value;
877
878 if (!ctx) {
879 ALOGE("%s: null context error", __FUNCTION__);
880 return -EINVAL;
881 }
882
883 switch(name) {
884 case COPYBIT_MINIFICATION_LIMIT:
885 value = MAX_SCALE_FACTOR;
886 break;
887 case COPYBIT_MAGNIFICATION_LIMIT:
888 value = MAX_SCALE_FACTOR;
889 break;
890 case COPYBIT_SCALING_FRAC_BITS:
891 value = 32;
892 break;
893 case COPYBIT_ROTATION_STEP_DEG:
894 value = 1;
895 break;
896 default:
897 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
898 value = -EINVAL;
899 }
900 return value;
901}
902
903static int is_alpha(int cformat)
904{
905 int alpha = 0;
906 switch (cformat & 0xFF) {
907 case C2D_COLOR_FORMAT_8888_ARGB:
908 case C2D_COLOR_FORMAT_8888_RGBA:
909 case C2D_COLOR_FORMAT_5551_RGBA:
910 case C2D_COLOR_FORMAT_4444_ARGB:
911 alpha = 1;
912 break;
913 default:
914 alpha = 0;
915 break;
916 }
917
918 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
919 alpha = 0;
920
921 return alpha;
922}
923
924/* Function to check if we need a temporary buffer for the blit.
925 * This would happen if the requested destination stride and the
926 * C2D stride do not match. We ignore RGB buffers, since their
927 * stride is always aligned to 32.
928 */
929static bool need_temp_buffer(struct copybit_image_t const *img)
930{
931 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
932 return false;
933
934 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
935
936 // The width parameter in the handle contains the aligned_w. We check if we
937 // need to convert based on this param. YUV formats have bpp=1, so checking
938 // if the requested stride is aligned should suffice.
939 if (0 == (handle->width)%32) {
940 return false;
941 }
942
943 return true;
944}
945
946/* Function to extract the information from the copybit image and set the corresponding
947 * values in the bufferInfo struct.
948 */
949static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
950{
951 info.width = img->w;
952 info.height = img->h;
953 info.format = img->format;
954}
955
956/* Function to get the required size for a particular format, inorder for C2D to perform
957 * the blit operation.
958 */
959static size_t get_size(const bufferInfo& info)
960{
961 size_t size = 0;
962 int w = info.width;
963 int h = info.height;
964 int aligned_w = ALIGN(w, 32);
965 switch(info.format) {
966 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
967 {
968 // Chroma for this format is aligned to 2K.
969 size = ALIGN((aligned_w*h), 2048) +
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800970 ALIGN(aligned_w/2, 32) * (h/2) *2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700971 size = ALIGN(size, 4096);
972 } break;
973 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
974 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
975 {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800976 size = aligned_w * h +
977 ALIGN(aligned_w/2, 32) * (h/2) * 2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700978 size = ALIGN(size, 4096);
979 } break;
980 default: break;
981 }
982 return size;
983}
984
985/* Function to allocate memory for the temporary buffer. This memory is
986 * allocated from Ashmem. It is the caller's responsibility to free this
987 * memory.
988 */
989static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
990{
991 ALOGD("%s E", __FUNCTION__);
992 // Alloc memory from system heap
993 data.base = 0;
994 data.fd = -1;
995 data.offset = 0;
996 data.size = get_size(info);
997 data.align = getpagesize();
998 data.uncached = true;
999 int allocFlags = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
1000
1001 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001002 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -07001003 }
1004
1005 if (sAlloc == 0) {
1006 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
1007 return COPYBIT_FAILURE;
1008 }
1009
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001010 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001011 if (0 != err) {
1012 ALOGE("%s: allocate failed", __FUNCTION__);
1013 return COPYBIT_FAILURE;
1014 }
1015
1016 ALOGD("%s X", __FUNCTION__);
1017 return err;
1018}
1019
1020/* Function to free the temporary allocated memory.*/
1021static void free_temp_buffer(alloc_data &data)
1022{
1023 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001024 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001025 memalloc->free_buffer(data.base, data.size, 0, data.fd);
1026 }
1027}
1028
1029/* Function to perform the software color conversion. Convert the
1030 * C2D compatible format to the Android compatible format
1031 */
1032static int copy_image(private_handle_t *src_handle,
1033 struct copybit_image_t const *rhs,
1034 eConversionType conversionType)
1035{
1036 if (src_handle->fd == -1) {
1037 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
1038 return COPYBIT_FAILURE;
1039 }
1040
1041 // Copy the info.
1042 int ret = COPYBIT_SUCCESS;
1043 switch(rhs->format) {
1044 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1045 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1046 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1047 {
1048 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
1049 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
1050 } else {
1051 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
1052 }
1053
1054 } break;
1055 default: {
1056 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
1057 ret = COPYBIT_FAILURE;
1058 } break;
1059 }
1060 return ret;
1061}
1062
1063static void delete_handle(private_handle_t *handle)
1064{
1065 if (handle) {
1066 delete handle;
1067 handle = 0;
1068 }
1069}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001070
1071static bool need_to_execute_draw(struct copybit_context_t* ctx,
1072 eC2DFlags flags)
1073{
1074 if (flags & FLAGS_TEMP_SRC_DST) {
1075 return true;
1076 }
1077 if (flags & FLAGS_YUV_DESTINATION) {
1078 return true;
1079 }
1080 return false;
1081}
1082
Naseer Ahmed29a26812012-06-14 00:56:20 -07001083/** do a stretch blit type operation */
1084static int stretch_copybit_internal(
1085 struct copybit_device_t *dev,
1086 struct copybit_image_t const *dst,
1087 struct copybit_image_t const *src,
1088 struct copybit_rect_t const *dst_rect,
1089 struct copybit_rect_t const *src_rect,
1090 struct copybit_region_t const *region,
1091 bool enableBlend)
1092{
1093 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1094 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001095 int flags = 0;
1096 int src_surface_type;
1097 int mapped_src_idx = -1, mapped_dst_idx = -1;
1098 C2D_OBJECT_STR src_surface;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001099
1100 if (!ctx) {
1101 ALOGE("%s: null context error", __FUNCTION__);
1102 return -EINVAL;
1103 }
1104
1105 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1106 ALOGE("%s: src dimension error", __FUNCTION__);
1107 return -EINVAL;
1108 }
1109
1110 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001111 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w,
1112 dst->h);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001113 return -EINVAL;
1114 }
1115
Naseer Ahmed29a26812012-06-14 00:56:20 -07001116 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001117 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__,
1118 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001119 return COPYBIT_FAILURE;
1120 }
1121
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001122 int dst_surface_type;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001123 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001124 dst_surface_type = RGB_SURFACE;
1125 flags |= FLAGS_PREMULTIPLIED_ALPHA;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001126 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001127 int num_planes = get_num_planes(dst->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001128 flags |= FLAGS_YUV_DESTINATION;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001129 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001130 dst_surface_type = YUV_SURFACE_2_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001131 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001132 dst_surface_type = YUV_SURFACE_3_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001133 } else {
1134 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1135 __FUNCTION__, dst->format);
1136 return COPYBIT_FAILURE;
1137 }
1138 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001139 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__,
1140 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001141 return COPYBIT_FAILURE;
1142 }
1143
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001144 if (ctx->blit_rgb_count == MAX_RGB_SURFACES ||
1145 ctx->blit_yuv_2_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1146 ctx->blit_yuv_3_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1147 ctx->blit_count == MAX_BLIT_OBJECT_COUNT ||
1148 ctx->dst_surface_type != dst_surface_type) {
1149 // we have reached the max. limits of our internal structures or
1150 // changed the target.
1151 // Draw the remaining surfaces. We need to do the finish here since
1152 // we need to free up the surface templates.
1153 finish_copybit(dev);
1154 }
1155
1156 ctx->dst_surface_type = dst_surface_type;
1157
1158 // Update the destination
Naseer Ahmed29a26812012-06-14 00:56:20 -07001159 copybit_image_t dst_image;
1160 dst_image.w = dst->w;
1161 dst_image.h = dst->h;
1162 dst_image.format = dst->format;
1163 dst_image.handle = dst->handle;
1164 // Check if we need a temp. copy for the destination. We'd need this the destination
1165 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1166 // aligned to 32.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001167 bool need_temp_dst = need_temp_buffer(dst);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001168 bufferInfo dst_info;
1169 populate_buffer_info(dst, dst_info);
1170 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1171 dst_info.width, dst_info.height);
1172 if (dst_hnd == NULL) {
1173 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1174 return COPYBIT_FAILURE;
1175 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001176 if (need_temp_dst) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001177 if (get_size(dst_info) != ctx->temp_dst_buffer.size) {
1178 free_temp_buffer(ctx->temp_dst_buffer);
1179 // Create a temp buffer and set that as the destination.
1180 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1181 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1182 delete_handle(dst_hnd);
1183 return COPYBIT_FAILURE;
1184 }
1185 }
1186 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1187 dst_hnd->size = ctx->temp_dst_buffer.size;
1188 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
1189 dst_hnd->base = (int)(ctx->temp_dst_buffer.base);
1190 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1191 dst_hnd->gpuaddr = 0;
1192 dst_image.handle = dst_hnd;
1193 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -07001194 if(!ctx->dst_surface_mapped) {
1195 //map the destination surface to GPU address
1196 status = set_image(ctx, ctx->dst[ctx->dst_surface_type], &dst_image,
1197 (eC2DFlags)flags, mapped_dst_idx);
1198 if(status) {
1199 ALOGE("%s: dst: set_image error", __FUNCTION__);
1200 delete_handle(dst_hnd);
1201 unmap_gpuaddr(ctx, mapped_dst_idx);
1202 return COPYBIT_FAILURE;
1203 }
1204 ctx->dst_surface_mapped = true;
1205 ctx->dst_surface_base = dst->base;
1206 } else if(ctx->dst_surface_mapped && ctx->dst_surface_base != dst->base) {
1207 // Destination surface for the operation should be same for multiple
1208 // requests, this check is catch if there is any case when the
1209 // destination changes
1210 ALOGE("%s: a different destination surface!!", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001211 }
1212
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001213 // Update the source
1214 flags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001215 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001216 src_surface_type = RGB_SURFACE;
1217 src_surface = ctx->blit_rgb_object[ctx->blit_rgb_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001218 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1219 int num_planes = get_num_planes(src->format);
1220 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001221 src_surface_type = YUV_SURFACE_2_PLANES;
1222 src_surface = ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001223 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001224 src_surface_type = YUV_SURFACE_3_PLANES;
1225 src_surface = ctx->blit_yuv_3_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001226 } else {
1227 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1228 __FUNCTION__, src->format);
1229 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001230 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001231 return -EINVAL;
1232 }
1233 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001234 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__,
1235 src->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001236 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001237 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001238 return -EINVAL;
1239 }
1240
1241 copybit_image_t src_image;
1242 src_image.w = src->w;
1243 src_image.h = src->h;
1244 src_image.format = src->format;
1245 src_image.handle = src->handle;
1246
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001247 bool need_temp_src = need_temp_buffer(src);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001248 bufferInfo src_info;
1249 populate_buffer_info(src, src_info);
1250 private_handle_t* src_hnd = new private_handle_t(-1, 0, 0, 0, src_info.format,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001251 src_info.width, src_info.height);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001252 if (NULL == src_hnd) {
1253 ALOGE("%s: src_hnd is null", __FUNCTION__);
1254 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001255 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001256 return COPYBIT_FAILURE;
1257 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001258 if (need_temp_src) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001259 if (get_size(src_info) != ctx->temp_src_buffer.size) {
1260 free_temp_buffer(ctx->temp_src_buffer);
1261 // Create a temp buffer and set that as the destination.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001262 if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
1263 ctx->temp_src_buffer)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001264 ALOGE("%s: get_temp_buffer(src) failed", __FUNCTION__);
1265 delete_handle(dst_hnd);
1266 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001267 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001268 return COPYBIT_FAILURE;
1269 }
1270 }
1271 src_hnd->fd = ctx->temp_src_buffer.fd;
1272 src_hnd->size = ctx->temp_src_buffer.size;
1273 src_hnd->flags = ctx->temp_src_buffer.allocType;
1274 src_hnd->base = (int)(ctx->temp_src_buffer.base);
1275 src_hnd->offset = ctx->temp_src_buffer.offset;
1276 src_hnd->gpuaddr = 0;
1277 src_image.handle = src_hnd;
1278
1279 // Copy the source.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001280 status = copy_image((private_handle_t *)src->handle, &src_image,
1281 CONVERT_TO_C2D_FORMAT);
1282 if (status == COPYBIT_FAILURE) {
1283 ALOGE("%s:copy_image failed in temp source",__FUNCTION__);
1284 delete_handle(dst_hnd);
1285 delete_handle(src_hnd);
1286 unmap_gpuaddr(ctx, mapped_dst_idx);
1287 return status;
1288 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001289
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001290 // Clean the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001291 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001292 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001293 src_hnd->offset, src_hnd->fd,
1294 gralloc::CACHE_CLEAN)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001295 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1296 delete_handle(dst_hnd);
1297 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001298 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001299 return COPYBIT_FAILURE;
1300 }
1301 }
1302
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001303 flags |= (ctx->is_premultiplied_alpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1304 flags |= (ctx->dst_surface_type != RGB_SURFACE) ? FLAGS_YUV_DESTINATION : 0;
1305 status = set_image(ctx, src_surface.surface_id, &src_image,
1306 (eC2DFlags)flags, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001307 if(status) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001308 ALOGE("%s: set_image (src) error", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001309 delete_handle(dst_hnd);
1310 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001311 unmap_gpuaddr(ctx, mapped_dst_idx);
1312 unmap_gpuaddr(ctx, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001313 return COPYBIT_FAILURE;
1314 }
1315
Arun Kumar K.Re29916b2013-03-20 11:42:53 -07001316 src_surface.config_mask = C2D_NO_ANTIALIASING_BIT | ctx->config_mask;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001317 src_surface.global_alpha = ctx->src_global_alpha;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001318 if (enableBlend) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001319 if(src_surface.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1320 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1321 if(!(src_surface.global_alpha)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001322 // src alpha is zero
Naseer Ahmed29a26812012-06-14 00:56:20 -07001323 delete_handle(dst_hnd);
1324 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001325 unmap_gpuaddr(ctx, mapped_dst_idx);
1326 unmap_gpuaddr(ctx, mapped_src_idx);
1327 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001328 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001329 }
1330 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001331 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001332 }
1333
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001334 if (src_surface_type == RGB_SURFACE) {
1335 ctx->blit_rgb_object[ctx->blit_rgb_count] = src_surface;
1336 ctx->blit_rgb_count++;
1337 } else if (src_surface_type == YUV_SURFACE_2_PLANES) {
1338 ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count] = src_surface;
1339 ctx->blit_yuv_2_plane_count++;
1340 } else {
1341 ctx->blit_yuv_3_plane_object[ctx->blit_yuv_3_plane_count] = src_surface;
1342 ctx->blit_yuv_3_plane_count++;
1343 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001344
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001345 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001346 while ((status == 0) && region->next(region, &clip)) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001347 set_rects(ctx, &(src_surface), dst_rect, src_rect, &clip);
1348 if (ctx->blit_count == MAX_BLIT_OBJECT_COUNT) {
1349 ALOGW("Reached end of blit count");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001350 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001351 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001352 ctx->blit_list[ctx->blit_count] = src_surface;
1353 ctx->blit_count++;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001354 }
1355
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001356 // Check if we need to perform an early draw-finish.
1357 flags |= (need_temp_dst || need_temp_src) ? FLAGS_TEMP_SRC_DST : 0;
1358 if (need_to_execute_draw(ctx, (eC2DFlags)flags))
1359 {
1360 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001361 }
1362
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001363 if (need_temp_dst) {
1364 // copy the temp. destination without the alignment to the actual
1365 // destination.
1366 status = copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1367 if (status == COPYBIT_FAILURE) {
1368 ALOGE("%s:copy_image failed in temp Dest",__FUNCTION__);
1369 delete_handle(dst_hnd);
1370 delete_handle(src_hnd);
1371 unmap_gpuaddr(ctx, mapped_dst_idx);
1372 unmap_gpuaddr(ctx, mapped_src_idx);
1373 return status;
1374 }
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001375 // Clean the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001376 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001377 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001378 dst_hnd->offset, dst_hnd->fd,
1379 gralloc::CACHE_CLEAN);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001380 }
1381 delete_handle(dst_hnd);
1382 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001383
1384 ctx->is_premultiplied_alpha = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001385 ctx->fb_width = 0;
1386 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001387 ctx->config_mask = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001388 return status;
1389}
1390
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001391
Naseer Ahmed29a26812012-06-14 00:56:20 -07001392static int stretch_copybit(
1393 struct copybit_device_t *dev,
1394 struct copybit_image_t const *dst,
1395 struct copybit_image_t const *src,
1396 struct copybit_rect_t const *dst_rect,
1397 struct copybit_rect_t const *src_rect,
1398 struct copybit_region_t const *region)
1399{
1400 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001401 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001402 bool needsBlending = (ctx->src_global_alpha != 0);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001403 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1404 status = stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001405 region, needsBlending);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001406 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1407 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001408}
1409
1410/** Perform a blit type operation */
1411static int blit_copybit(
1412 struct copybit_device_t *dev,
1413 struct copybit_image_t const *dst,
1414 struct copybit_image_t const *src,
1415 struct copybit_region_t const *region)
1416{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001417 int status = COPYBIT_SUCCESS;
1418 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Naseer Ahmedbb887bd2013-04-16 14:12:27 -04001419 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
1420 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001421 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1422 status = stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1423 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1424 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001425}
1426
1427/*****************************************************************************/
1428
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001429static void clean_up(copybit_context_t* ctx)
1430{
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001431 void* ret;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001432 if (!ctx)
1433 return;
1434
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001435 // stop the wait_cleanup_thread
1436 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1437 ctx->stop_thread = true;
1438 // Signal waiting thread
1439 pthread_cond_signal(&ctx->wait_cleanup_cond);
1440 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1441 // waits for the cleanup thread to exit
1442 pthread_join(ctx->wait_thread_id, &ret);
1443 pthread_mutex_destroy(&ctx->wait_cleanup_lock);
1444 pthread_cond_destroy (&ctx->wait_cleanup_cond);
1445
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001446 for (int i = 0; i < NUM_SURFACE_TYPES; i++) {
1447 if (ctx->dst[i])
1448 LINK_c2dDestroySurface(ctx->dst[i]);
1449 }
1450
1451 for (int i = 0; i < MAX_RGB_SURFACES; i++) {
1452 if (ctx->blit_rgb_object[i].surface_id)
1453 LINK_c2dDestroySurface(ctx->blit_rgb_object[i].surface_id);
1454 }
1455
1456 for (int i = 0; i < MAX_YUV_2_PLANE_SURFACES; i++) {
1457 if (ctx->blit_yuv_2_plane_object[i].surface_id)
1458 LINK_c2dDestroySurface(ctx->blit_yuv_2_plane_object[i].surface_id);
1459 }
1460
1461 for (int i = 0; i < MAX_YUV_3_PLANE_SURFACES; i++) {
1462 if (ctx->blit_yuv_3_plane_object[i].surface_id)
1463 LINK_c2dDestroySurface(ctx->blit_yuv_3_plane_object[i].surface_id);
1464 }
1465
1466 if (ctx->libc2d2) {
1467 ::dlclose(ctx->libc2d2);
1468 ALOGV("dlclose(libc2d2)");
1469 }
1470
1471 free(ctx);
1472}
1473
Naseer Ahmed29a26812012-06-14 00:56:20 -07001474/** Close the copybit device */
1475static int close_copybit(struct hw_device_t *dev)
1476{
1477 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1478 if (ctx) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001479 free_temp_buffer(ctx->temp_src_buffer);
1480 free_temp_buffer(ctx->temp_dst_buffer);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001481 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001482 clean_up(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001483 return 0;
1484}
1485
1486/** Open a new instance of a copybit device using name */
1487static int open_copybit(const struct hw_module_t* module, const char* name,
1488 struct hw_device_t** device)
1489{
1490 int status = COPYBIT_SUCCESS;
1491 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1492 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1493 struct copybit_context_t *ctx;
1494 char fbName[64];
1495
1496 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1497 if(!ctx) {
1498 ALOGE("%s: malloc failed", __FUNCTION__);
1499 return COPYBIT_FAILURE;
1500 }
1501
1502 /* initialize drawstate */
1503 memset(ctx, 0, sizeof(*ctx));
Naseer Ahmed29a26812012-06-14 00:56:20 -07001504 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1505 if (!ctx->libc2d2) {
1506 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001507 clean_up(ctx);
1508 status = COPYBIT_FAILURE;
1509 *device = NULL;
1510 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001511 }
1512 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1513 "c2dCreateSurface");
1514 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1515 "c2dUpdateSurface");
1516 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1517 "c2dReadSurface");
1518 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1519 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1520 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1521 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1522 "c2dWaitTimestamp");
1523 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1524 "c2dDestroySurface");
1525 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1526 "c2dMapAddr");
1527 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1528 "c2dUnMapAddr");
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001529 *(void **)&LINK_c2dGetDriverCapabilities = ::dlsym(ctx->libc2d2,
1530 "c2dGetDriverCapabilities");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001531 *(void **)&LINK_c2dCreateFenceFD = ::dlsym(ctx->libc2d2,
1532 "c2dCreateFenceFD");
Naseer Ahmed183939d2013-03-14 20:44:17 -04001533 *(void **)&LINK_c2dFillSurface = ::dlsym(ctx->libc2d2,
1534 "c2dFillSurface");
Naseer Ahmed29a26812012-06-14 00:56:20 -07001535
1536 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001537 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp ||
1538 !LINK_c2dFinish || !LINK_c2dDestroySurface ||
Naseer Ahmed183939d2013-03-14 20:44:17 -04001539 !LINK_c2dGetDriverCapabilities || !LINK_c2dCreateFenceFD ||
1540 !LINK_c2dFillSurface) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001541 ALOGE("%s: dlsym ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001542 clean_up(ctx);
1543 status = COPYBIT_FAILURE;
1544 *device = NULL;
1545 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001546 }
1547
1548 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1549 ctx->device.common.version = 1;
1550 ctx->device.common.module = (hw_module_t*)(module);
1551 ctx->device.common.close = close_copybit;
1552 ctx->device.set_parameter = set_parameter_copybit;
1553 ctx->device.get = get;
1554 ctx->device.blit = blit_copybit;
1555 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001556 ctx->device.finish = finish_copybit;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001557 ctx->device.flush_get_fence = flush_get_fence_copybit;
Naseer Ahmed183939d2013-03-14 20:44:17 -04001558 ctx->device.clear = clear_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001559
1560 /* Create RGB Surface */
1561 surfDefinition.buffer = (void*)0xdddddddd;
1562 surfDefinition.phys = (void*)0xdddddddd;
1563 surfDefinition.stride = 1 * 4;
1564 surfDefinition.width = 1;
1565 surfDefinition.height = 1;
1566 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1567 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1568 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001569 C2D_SURFACE_WITH_PHYS |
1570 C2D_SURFACE_WITH_PHYS_DUMMY ),
1571 &surfDefinition)) {
1572 ALOGE("%s: create ctx->dst_surface[RGB_SURFACE] failed", __FUNCTION__);
1573 ctx->dst[RGB_SURFACE] = 0;
1574 clean_up(ctx);
1575 status = COPYBIT_FAILURE;
1576 *device = NULL;
1577 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001578 }
1579
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001580 unsigned int surface_id = 0;
1581 for (int i = 0; i < MAX_RGB_SURFACES; i++)
1582 {
1583 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001584 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001585 C2D_SURFACE_WITH_PHYS |
1586 C2D_SURFACE_WITH_PHYS_DUMMY ),
1587 &surfDefinition)) {
1588 ALOGE("%s: create RGB source surface %d failed", __FUNCTION__, i);
1589 ctx->blit_rgb_object[i].surface_id = 0;
1590 status = COPYBIT_FAILURE;
1591 break;
1592 } else {
1593 ctx->blit_rgb_object[i].surface_id = surface_id;
1594 ALOGW("%s i = %d surface_id=%d", __FUNCTION__, i,
1595 ctx->blit_rgb_object[i].surface_id);
1596 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001597 }
1598
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001599 if (status == COPYBIT_FAILURE) {
1600 clean_up(ctx);
1601 status = COPYBIT_FAILURE;
1602 *device = NULL;
1603 return status;
1604 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001605
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001606 // Create 2 plane YUV surfaces
1607 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001608 yuvSurfaceDef.width = 4;
1609 yuvSurfaceDef.height = 4;
1610 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1611 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1612 yuvSurfaceDef.stride0 = 4;
1613
1614 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1615 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1616 yuvSurfaceDef.stride1 = 4;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001617 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1618 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001619 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1620 C2D_SURFACE_WITH_PHYS |
1621 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001622 &yuvSurfaceDef)) {
1623 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001624 ctx->dst[YUV_SURFACE_2_PLANES] = 0;
1625 clean_up(ctx);
1626 status = COPYBIT_FAILURE;
1627 *device = NULL;
1628 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001629 }
1630
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001631 for (int i=0; i < MAX_YUV_2_PLANE_SURFACES; i++)
1632 {
1633 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
1634 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1635 C2D_SURFACE_WITH_PHYS |
1636 C2D_SURFACE_WITH_PHYS_DUMMY ),
1637 &yuvSurfaceDef)) {
1638 ALOGE("%s: create YUV source %d failed", __FUNCTION__, i);
1639 ctx->blit_yuv_2_plane_object[i].surface_id = 0;
1640 status = COPYBIT_FAILURE;
1641 break;
1642 } else {
1643 ctx->blit_yuv_2_plane_object[i].surface_id = surface_id;
1644 ALOGW("%s: 2 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1645 ctx->blit_yuv_2_plane_object[i].surface_id);
1646 }
1647 }
1648
1649 if (status == COPYBIT_FAILURE) {
1650 clean_up(ctx);
1651 status = COPYBIT_FAILURE;
1652 *device = NULL;
1653 return status;
1654 }
1655
1656 // Create YUV 3 plane surfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -07001657 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1658 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1659 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1660 yuvSurfaceDef.stride2 = 4;
1661
Naseer Ahmed29a26812012-06-14 00:56:20 -07001662 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1663 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001664 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1665 C2D_SURFACE_WITH_PHYS |
1666 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001667 &yuvSurfaceDef)) {
1668 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001669 ctx->dst[YUV_SURFACE_3_PLANES] = 0;
1670 clean_up(ctx);
1671 status = COPYBIT_FAILURE;
1672 *device = NULL;
1673 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001674 }
1675
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001676 for (int i=0; i < MAX_YUV_3_PLANE_SURFACES; i++)
1677 {
1678 if (LINK_c2dCreateSurface(&(surface_id),
1679 C2D_TARGET | C2D_SOURCE,
1680 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1681 C2D_SURFACE_WITH_PHYS |
1682 C2D_SURFACE_WITH_PHYS_DUMMY),
1683 &yuvSurfaceDef)) {
1684 ALOGE("%s: create 3 plane YUV surface %d failed", __FUNCTION__, i);
1685 ctx->blit_yuv_3_plane_object[i].surface_id = 0;
1686 status = COPYBIT_FAILURE;
1687 break;
1688 } else {
1689 ctx->blit_yuv_3_plane_object[i].surface_id = surface_id;
1690 ALOGW("%s: 3 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1691 ctx->blit_yuv_3_plane_object[i].surface_id);
1692 }
1693 }
1694
1695 if (status == COPYBIT_FAILURE) {
1696 clean_up(ctx);
1697 status = COPYBIT_FAILURE;
1698 *device = NULL;
1699 return status;
1700 }
1701
1702 if (LINK_c2dGetDriverCapabilities(&(ctx->c2d_driver_info))) {
1703 ALOGE("%s: LINK_c2dGetDriverCapabilities failed", __FUNCTION__);
1704 clean_up(ctx);
1705 status = COPYBIT_FAILURE;
1706 *device = NULL;
1707 return status;
1708 }
1709 // Initialize context variables.
1710 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1711
Naseer Ahmed29a26812012-06-14 00:56:20 -07001712 ctx->temp_src_buffer.fd = -1;
1713 ctx->temp_src_buffer.base = 0;
1714 ctx->temp_src_buffer.size = 0;
1715
1716 ctx->temp_dst_buffer.fd = -1;
1717 ctx->temp_dst_buffer.base = 0;
1718 ctx->temp_dst_buffer.size = 0;
1719
1720 ctx->fb_width = 0;
1721 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001722
1723 ctx->blit_rgb_count = 0;
1724 ctx->blit_yuv_2_plane_count = 0;
1725 ctx->blit_yuv_3_plane_count = 0;
1726 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001727
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001728 ctx->wait_timestamp = false;
1729 ctx->stop_thread = false;
1730 pthread_mutex_init(&(ctx->wait_cleanup_lock), NULL);
1731 pthread_cond_init(&(ctx->wait_cleanup_cond), NULL);
1732 /* Start the wait thread */
1733 pthread_attr_t attr;
1734 pthread_attr_init(&attr);
1735 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1736
1737 pthread_create(&ctx->wait_thread_id, &attr, &c2d_wait_loop,
1738 (void *)ctx);
1739 pthread_attr_destroy(&attr);
1740
Naseer Ahmed29a26812012-06-14 00:56:20 -07001741 *device = &ctx->device.common;
1742 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001743}