blob: f6ebdd79ca51c00cc9363bd92cd8aab44bf1327c [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.
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700108#define MAX_RGB_SURFACES 32 // 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 = {
Steve Kondik5164dda2015-11-01 02:51:41 -0800197 .open = open_copybit
Naseer Ahmed29a26812012-06-14 00:56:20 -0700198};
199
200/*
201 * The COPYBIT Module
202 */
203struct copybit_module_t HAL_MODULE_INFO_SYM = {
Steve Kondik5164dda2015-11-01 02:51:41 -0800204 .common = {
205 .tag = 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 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700213};
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;
Omprakash Dhyade8071adb2014-05-30 16:52:46 -0700262 case HAL_PIXEL_FORMAT_RGB_888: return C2D_COLOR_FORMAT_888_RGB |
263 C2D_FORMAT_SWAP_RB;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
265 C2D_FORMAT_SWAP_RB |
266 C2D_FORMAT_DISABLE_ALPHA;
267 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB |
268 C2D_FORMAT_SWAP_RB;
269 case HAL_PIXEL_FORMAT_BGRA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 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;
Sushil Chauhanb7929562014-05-15 11:20:32 -0700322 case C2D_COLOR_FORMAT_888_RGB:
323 c2dBpp = 24;
324 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 case C2D_COLOR_FORMAT_8_L:
326 case C2D_COLOR_FORMAT_8_A:
327 c2dBpp = 8;
328 break;
329 case C2D_COLOR_FORMAT_4_A:
330 c2dBpp = 4;
331 break;
332 case C2D_COLOR_FORMAT_1:
333 c2dBpp = 1;
334 break;
335 default:
336 ALOGE("%s ERROR", __func__);
337 break;
338 }
339 return c2dBpp;
340}
341
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700342static uint32 c2d_get_gpuaddr(copybit_context_t* ctx,
343 struct private_handle_t *handle, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344{
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700345 uint32 memtype, *gpuaddr = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346 C2D_STATUS rc;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700347 int freeindex = 0;
348 bool mapaddr = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700349
350 if(!handle)
351 return 0;
352
353 if (handle->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
354 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP))
355 memtype = KGSL_USER_MEM_TYPE_PMEM;
356 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
357 memtype = KGSL_USER_MEM_TYPE_ASHMEM;
358 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
359 memtype = KGSL_USER_MEM_TYPE_ION;
360 else {
361 ALOGE("Invalid handle flags: 0x%x", handle->flags);
362 return 0;
363 }
364
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700365 // Check for a freeindex in the mapped_gpu_addr list
366 for (freeindex = 0; freeindex < MAX_SURFACES; freeindex++) {
367 if (ctx->mapped_gpu_addr[freeindex] == 0) {
368 // free index is available
369 // map GPU addr and use this as mapped_idx
370 mapaddr = true;
371 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800372 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700374
375 if(mapaddr) {
376 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size,
377 handle->offset, memtype, (void**)&gpuaddr);
378
379 if (rc == C2D_STATUS_OK) {
380 // We have mapped the GPU address inside copybit. We need to unmap
381 // this address after the blit. Store this address
382 ctx->mapped_gpu_addr[freeindex] = (uint32) gpuaddr;
383 mapped_idx = freeindex;
384 }
385 }
386 return (uint32) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700387}
388
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800389static void unmap_gpuaddr(copybit_context_t* ctx, int mapped_idx)
390{
391 if (!ctx || (mapped_idx == -1))
392 return;
393
394 if (ctx->mapped_gpu_addr[mapped_idx]) {
395 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[mapped_idx]);
396 ctx->mapped_gpu_addr[mapped_idx] = 0;
397 }
398}
399
Naseer Ahmed29a26812012-06-14 00:56:20 -0700400static int is_supported_rgb_format(int format)
401{
402 switch(format) {
403 case HAL_PIXEL_FORMAT_RGBA_8888:
404 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhanb7929562014-05-15 11:20:32 -0700405 case HAL_PIXEL_FORMAT_RGB_888:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700406 case HAL_PIXEL_FORMAT_RGB_565:
Mathias Agopiane4001e72013-09-17 10:37:22 -0700407 case HAL_PIXEL_FORMAT_BGRA_8888: {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700408 return COPYBIT_SUCCESS;
409 }
410 default:
411 return COPYBIT_FAILURE;
412 }
413}
414
415static int get_num_planes(int format)
416{
417 switch(format) {
418 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
419 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
420 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
421 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
422 return 2;
423 }
424 case HAL_PIXEL_FORMAT_YV12: {
425 return 3;
426 }
427 default:
428 return COPYBIT_FAILURE;
429 }
430}
431
432static int is_supported_yuv_format(int format)
433{
434 switch(format) {
435 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
436 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
437 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
438 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
439 return COPYBIT_SUCCESS;
440 }
441 default:
442 return COPYBIT_FAILURE;
443 }
444}
445
446static int is_valid_destination_format(int format)
447{
448 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
449 // C2D does not support NV12Tile as a destination format.
450 return COPYBIT_FAILURE;
451 }
452 return COPYBIT_SUCCESS;
453}
454
455static int calculate_yuv_offset_and_stride(const bufferInfo& info,
456 yuvPlaneInfo& yuvInfo)
457{
458 int width = info.width;
459 int height = info.height;
460 int format = info.format;
461
462 int aligned_height = 0;
463 int aligned_width = 0, size = 0;
464
465 switch (format) {
466 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
467 /* NV12 Tile buffers have their luma height aligned to 32bytes and width
468 * aligned to 128 bytes. The chroma offset starts at an 8K boundary
469 */
470 aligned_height = ALIGN(height, 32);
471 aligned_width = ALIGN(width, 128);
472 size = aligned_width * aligned_height;
473 yuvInfo.plane1_offset = ALIGN(size,8192);
474 yuvInfo.yStride = aligned_width;
475 yuvInfo.plane1_stride = aligned_width;
476 break;
477 }
478 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
479 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
480 case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
481 aligned_width = ALIGN(width, 32);
482 yuvInfo.yStride = aligned_width;
483 yuvInfo.plane1_stride = aligned_width;
484 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
485 // The encoder requires a 2K aligned chroma offset
486 yuvInfo.plane1_offset = ALIGN(aligned_width * height, 2048);
487 } else
488 yuvInfo.plane1_offset = aligned_width * height;
489
490 break;
491 }
492 default: {
493 return COPYBIT_FAILURE;
494 }
495 }
496 return COPYBIT_SUCCESS;
497}
498
499/** create C2D surface from copybit image */
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800500static int set_image(copybit_context_t* ctx, uint32 surfaceId,
501 const struct copybit_image_t *rhs,
502 const eC2DFlags flags, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700503{
504 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
505 C2D_SURFACE_TYPE surfaceType;
506 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800507 uint32 gpuaddr = 0;
508 int c2d_format;
509 mapped_idx = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700510
511 if (flags & FLAGS_YUV_DESTINATION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800512 c2d_format = get_c2d_format_for_yuv_destination(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700513 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800514 c2d_format = get_format(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700515 }
516
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800517 if(c2d_format == -EINVAL) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 ALOGE("%s: invalid format", __FUNCTION__);
519 return -EINVAL;
520 }
521
522 if(handle == NULL) {
523 ALOGE("%s: invalid handle", __func__);
524 return -EINVAL;
525 }
526
527 if (handle->gpuaddr == 0) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800528 gpuaddr = c2d_get_gpuaddr(ctx, handle, mapped_idx);
529 if(!gpuaddr) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700530 ALOGE("%s: c2d_get_gpuaddr failed", __FUNCTION__);
531 return COPYBIT_FAILURE;
532 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800533 } else {
534 gpuaddr = handle->gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700535 }
536
537 /* create C2D surface */
538 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
539 /* RGB */
540 C2D_RGB_SURFACE_DEF surfaceDef;
541
542 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS);
543
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800544 surfaceDef.phys = (void*) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700545 surfaceDef.buffer = (void*) (handle->base);
546
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800547 surfaceDef.format = c2d_format |
Naseer Ahmed29a26812012-06-14 00:56:20 -0700548 ((flags & FLAGS_PREMULTIPLIED_ALPHA) ? C2D_FORMAT_PREMULTIPLIED : 0);
549 surfaceDef.width = rhs->w;
550 surfaceDef.height = rhs->h;
551 int aligned_width = ALIGN(surfaceDef.width,32);
552 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
553
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800554 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
555 &surfaceDef)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700556 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800557 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700558 status = COPYBIT_FAILURE;
559 }
560 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
561 C2D_YUV_SURFACE_DEF surfaceDef;
562 memset(&surfaceDef, 0, sizeof(surfaceDef));
563 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800564 surfaceDef.format = c2d_format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700565
566 bufferInfo info;
567 info.width = rhs->w;
568 info.height = rhs->h;
569 info.format = rhs->format;
570
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700571 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700572 status = calculate_yuv_offset_and_stride(info, yuvInfo);
573 if(status != COPYBIT_SUCCESS) {
574 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800575 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700576 }
577
578 surfaceDef.width = rhs->w;
579 surfaceDef.height = rhs->h;
580 surfaceDef.plane0 = (void*) (handle->base);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800581 surfaceDef.phys0 = (void*) (gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 surfaceDef.stride0 = yuvInfo.yStride;
583
584 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800585 surfaceDef.phys1 = (void*) (gpuaddr + yuvInfo.plane1_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700586 surfaceDef.stride1 = yuvInfo.plane1_stride;
587 if (3 == get_num_planes(rhs->format)) {
588 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800589 surfaceDef.phys2 = (void*) (gpuaddr + yuvInfo.plane2_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700590 surfaceDef.stride2 = yuvInfo.plane2_stride;
591 }
592
593 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
594 &surfaceDef)) {
595 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800596 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700597 status = COPYBIT_FAILURE;
598 }
599 } else {
600 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800601 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700602 status = COPYBIT_FAILURE;
603 }
604
605 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700606}
607
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800608/** copy the bits */
609static int msm_copybit(struct copybit_context_t *ctx, unsigned int target)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700610{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800611 if (ctx->blit_count == 0) {
612 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700613 }
614
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800615 for (int i = 0; i < ctx->blit_count; i++)
616 {
617 ctx->blit_list[i].next = &(ctx->blit_list[i+1]);
618 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800619 ctx->blit_list[ctx->blit_count-1].next = NULL;
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800620 uint32_t target_transform = ctx->trg_transform;
621 if (ctx->c2d_driver_info.capabilities_mask &
622 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
623 // For A3xx - set 0x0 as the transform is set in the config_mask
624 target_transform = 0x0;
625 }
626 if(LINK_c2dDraw(target, target_transform, 0x0, 0, 0, ctx->blit_list,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800627 ctx->blit_count)) {
628 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
629 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700630 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700631 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700632}
633
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800634
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800635
636static int flush_get_fence_copybit (struct copybit_device_t *dev, int* fd)
637{
638 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
639 int status = COPYBIT_FAILURE;
640 if (!ctx)
641 return COPYBIT_FAILURE;
642 pthread_mutex_lock(&ctx->wait_cleanup_lock);
643 status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
644
645 if(LINK_c2dFlush(ctx->dst[ctx->dst_surface_type], &ctx->time_stamp)) {
646 ALOGE("%s: LINK_c2dFlush ERROR", __FUNCTION__);
647 // unlock the mutex and return failure
648 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
649 return COPYBIT_FAILURE;
650 }
651 if(LINK_c2dCreateFenceFD(ctx->dst[ctx->dst_surface_type], ctx->time_stamp,
652 fd)) {
653 ALOGE("%s: LINK_c2dCreateFenceFD ERROR", __FUNCTION__);
654 status = COPYBIT_FAILURE;
655 }
656 if(status == COPYBIT_SUCCESS) {
657 //signal the wait_thread
658 ctx->wait_timestamp = true;
659 pthread_cond_signal(&ctx->wait_cleanup_cond);
660 }
661 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
662 return status;
663}
664
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800665static int finish_copybit(struct copybit_device_t *dev)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700666{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800667 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
668 if (!ctx)
669 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700670
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800671 int status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700672
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800673 if(LINK_c2dFinish(ctx->dst[ctx->dst_surface_type])) {
674 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
675 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700676 }
677
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800678 // Unmap any mapped addresses.
679 for (int i = 0; i < MAX_SURFACES; i++) {
680 if (ctx->mapped_gpu_addr[i]) {
681 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
682 ctx->mapped_gpu_addr[i] = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700683 }
684 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700685
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800686 // Reset the counts after the draw.
687 ctx->blit_rgb_count = 0;
688 ctx->blit_yuv_2_plane_count = 0;
689 ctx->blit_yuv_3_plane_count = 0;
690 ctx->blit_count = 0;
Pawan Kumar74d9ea92013-05-03 14:25:49 +0530691 ctx->dst_surface_mapped = false;
692 ctx->dst_surface_base = 0;
693
Naseer Ahmed29a26812012-06-14 00:56:20 -0700694 return status;
695}
696
Naseer Ahmed183939d2013-03-14 20:44:17 -0400697static int clear_copybit(struct copybit_device_t *dev,
698 struct copybit_image_t const *buf,
699 struct copybit_rect_t *rect)
700{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700701 int ret = COPYBIT_SUCCESS;
Naseer Ahmed183939d2013-03-14 20:44:17 -0400702 int flags = FLAGS_PREMULTIPLIED_ALPHA;
703 int mapped_dst_idx = -1;
704 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
705 C2D_RECT c2drect = {rect->l, rect->t, rect->r - rect->l, rect->b - rect->t};
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700706 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700707 if(!ctx->dst_surface_mapped) {
708 ret = set_image(ctx, ctx->dst[RGB_SURFACE], buf,
709 (eC2DFlags)flags, mapped_dst_idx);
710 if(ret) {
711 ALOGE("%s: set_image error", __FUNCTION__);
712 unmap_gpuaddr(ctx, mapped_dst_idx);
713 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
714 return COPYBIT_FAILURE;
715 }
716 //clear_copybit is the first call made by HWC for each composition
717 //with the dest surface, hence set dst_surface_mapped.
718 ctx->dst_surface_mapped = true;
719 ctx->dst_surface_base = buf->base;
720 ret = LINK_c2dFillSurface(ctx->dst[RGB_SURFACE], 0x0, &c2drect);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400721 }
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700722 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400723 return ret;
724}
725
726
Naseer Ahmed29a26812012-06-14 00:56:20 -0700727/** setup rectangles */
728static void set_rects(struct copybit_context_t *ctx,
729 C2D_OBJECT *c2dObject,
730 const struct copybit_rect_t *dst,
731 const struct copybit_rect_t *src,
732 const struct copybit_rect_t *scissor)
733{
734 // Set the target rect.
735 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
736 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
737 /* target rotation is 270 */
738 c2dObject->target_rect.x = (dst->t)<<16;
739 c2dObject->target_rect.y = ctx->fb_width?(ALIGN(ctx->fb_width,32)- dst->r):dst->r;
740 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
741 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
742 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
743 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
744 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
745 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
746 c2dObject->target_rect.y = (dst->l)<<16;
747 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
748 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
749 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
750 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
751 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
752 c2dObject->target_rect.x = ctx->fb_width?(ALIGN(ctx->fb_width,32) - dst->r):dst->r;
753 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
754 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
755 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
756 } else {
757 c2dObject->target_rect.x = (dst->l)<<16;
758 c2dObject->target_rect.y = (dst->t)<<16;
759 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
760 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
761 }
762 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
763
764 // Set the source rect
765 c2dObject->source_rect.x = (src->l)<<16;
766 c2dObject->source_rect.y = (src->t)<<16;
767 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
768 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
769 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
770
771 // Set the scissor rect
772 c2dObject->scissor_rect.x = scissor->l;
773 c2dObject->scissor_rect.y = scissor->t;
774 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
775 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
776 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
777}
778
Naseer Ahmed29a26812012-06-14 00:56:20 -0700779/*****************************************************************************/
780
781/** Set a parameter to value */
782static int set_parameter_copybit(
783 struct copybit_device_t *dev,
784 int name,
785 int value)
786{
787 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800788 int status = COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700789 if (!ctx) {
790 ALOGE("%s: null context", __FUNCTION__);
791 return -EINVAL;
792 }
793
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800794 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700795 switch(name) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700796 case COPYBIT_PLANE_ALPHA:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800797 {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700798 if (value < 0) value = 0;
799 if (value >= 256) value = 255;
800
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800801 ctx->src_global_alpha = value;
802 if (value < 255)
803 ctx->config_mask |= C2D_GLOBAL_ALPHA_BIT;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700804 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800805 ctx->config_mask &= ~C2D_GLOBAL_ALPHA_BIT;
806 }
807 break;
808 case COPYBIT_BLEND_MODE:
809 {
810 if (value == COPYBIT_BLENDING_NONE) {
811 ctx->config_mask |= C2D_ALPHA_BLEND_NONE;
812 ctx->is_premultiplied_alpha = true;
813 } else if (value == COPYBIT_BLENDING_PREMULT) {
814 ctx->is_premultiplied_alpha = true;
815 } else {
816 ctx->config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700817 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800818 }
819 break;
820 case COPYBIT_TRANSFORM:
821 {
822 unsigned int transform = 0;
823 uint32 config_mask = 0;
824 config_mask |= C2D_OVERRIDE_GLOBAL_TARGET_ROTATE_CONFIG;
825 if((value & 0x7) == COPYBIT_TRANSFORM_ROT_180) {
826 transform = C2D_TARGET_ROTATE_180;
827 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_180;
828 } else if((value & 0x7) == COPYBIT_TRANSFORM_ROT_270) {
829 transform = C2D_TARGET_ROTATE_90;
830 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_90;
831 } else if(value == COPYBIT_TRANSFORM_ROT_90) {
832 transform = C2D_TARGET_ROTATE_270;
833 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_270;
834 } else {
835 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_0;
836 if(value & COPYBIT_TRANSFORM_FLIP_H) {
837 config_mask |= C2D_MIRROR_H_BIT;
838 } else if(value & COPYBIT_TRANSFORM_FLIP_V) {
839 config_mask |= C2D_MIRROR_V_BIT;
840 }
841 }
842
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800843 if (ctx->c2d_driver_info.capabilities_mask &
844 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
845 ctx->config_mask |= config_mask;
846 } else {
847 // The transform for this surface does not match the current
848 // target transform. Draw all previous surfaces. This will be
849 // changed once we have a new mechanism to send different
850 // target rotations to c2d.
851 finish_copybit(dev);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800852 }
853 ctx->trg_transform = transform;
854 }
855 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700856 case COPYBIT_FRAMEBUFFER_WIDTH:
857 ctx->fb_width = value;
858 break;
859 case COPYBIT_FRAMEBUFFER_HEIGHT:
860 ctx->fb_height = value;
861 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800862 case COPYBIT_ROTATION_DEG:
863 case COPYBIT_DITHER:
864 case COPYBIT_BLUR:
Naseer Ahmed45a99602012-07-31 19:15:24 -0700865 case COPYBIT_BLIT_TO_FRAMEBUFFER:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800866 // Do nothing
Naseer Ahmed45a99602012-07-31 19:15:24 -0700867 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700868 default:
869 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800870 status = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700871 break;
872 }
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800873 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
874 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700875}
876
877/** Get a static info value */
878static int get(struct copybit_device_t *dev, int name)
879{
880 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
881 int value;
882
883 if (!ctx) {
884 ALOGE("%s: null context error", __FUNCTION__);
885 return -EINVAL;
886 }
887
888 switch(name) {
889 case COPYBIT_MINIFICATION_LIMIT:
890 value = MAX_SCALE_FACTOR;
891 break;
892 case COPYBIT_MAGNIFICATION_LIMIT:
893 value = MAX_SCALE_FACTOR;
894 break;
895 case COPYBIT_SCALING_FRAC_BITS:
896 value = 32;
897 break;
898 case COPYBIT_ROTATION_STEP_DEG:
899 value = 1;
900 break;
901 default:
902 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
903 value = -EINVAL;
904 }
905 return value;
906}
907
908static int is_alpha(int cformat)
909{
910 int alpha = 0;
911 switch (cformat & 0xFF) {
912 case C2D_COLOR_FORMAT_8888_ARGB:
913 case C2D_COLOR_FORMAT_8888_RGBA:
914 case C2D_COLOR_FORMAT_5551_RGBA:
915 case C2D_COLOR_FORMAT_4444_ARGB:
916 alpha = 1;
917 break;
918 default:
919 alpha = 0;
920 break;
921 }
922
923 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
924 alpha = 0;
925
926 return alpha;
927}
928
929/* Function to check if we need a temporary buffer for the blit.
930 * This would happen if the requested destination stride and the
931 * C2D stride do not match. We ignore RGB buffers, since their
932 * stride is always aligned to 32.
933 */
934static bool need_temp_buffer(struct copybit_image_t const *img)
935{
936 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
937 return false;
938
939 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
940
941 // The width parameter in the handle contains the aligned_w. We check if we
942 // need to convert based on this param. YUV formats have bpp=1, so checking
943 // if the requested stride is aligned should suffice.
944 if (0 == (handle->width)%32) {
945 return false;
946 }
947
948 return true;
949}
950
951/* Function to extract the information from the copybit image and set the corresponding
952 * values in the bufferInfo struct.
953 */
954static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
955{
956 info.width = img->w;
957 info.height = img->h;
958 info.format = img->format;
959}
960
961/* Function to get the required size for a particular format, inorder for C2D to perform
962 * the blit operation.
963 */
964static size_t get_size(const bufferInfo& info)
965{
966 size_t size = 0;
967 int w = info.width;
968 int h = info.height;
969 int aligned_w = ALIGN(w, 32);
970 switch(info.format) {
971 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
972 {
973 // Chroma for this format is aligned to 2K.
974 size = ALIGN((aligned_w*h), 2048) +
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800975 ALIGN(aligned_w/2, 32) * (h/2) *2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700976 size = ALIGN(size, 4096);
977 } break;
978 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
979 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
980 {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800981 size = aligned_w * h +
982 ALIGN(aligned_w/2, 32) * (h/2) * 2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700983 size = ALIGN(size, 4096);
984 } break;
985 default: break;
986 }
987 return size;
988}
989
990/* Function to allocate memory for the temporary buffer. This memory is
991 * allocated from Ashmem. It is the caller's responsibility to free this
992 * memory.
993 */
994static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
995{
996 ALOGD("%s E", __FUNCTION__);
997 // Alloc memory from system heap
998 data.base = 0;
999 data.fd = -1;
1000 data.offset = 0;
1001 data.size = get_size(info);
1002 data.align = getpagesize();
1003 data.uncached = true;
1004 int allocFlags = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
1005
1006 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001007 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -07001008 }
1009
1010 if (sAlloc == 0) {
1011 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
1012 return COPYBIT_FAILURE;
1013 }
1014
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001015 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001016 if (0 != err) {
1017 ALOGE("%s: allocate failed", __FUNCTION__);
1018 return COPYBIT_FAILURE;
1019 }
1020
1021 ALOGD("%s X", __FUNCTION__);
1022 return err;
1023}
1024
1025/* Function to free the temporary allocated memory.*/
1026static void free_temp_buffer(alloc_data &data)
1027{
1028 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001029 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001030 memalloc->free_buffer(data.base, data.size, 0, data.fd);
1031 }
1032}
1033
1034/* Function to perform the software color conversion. Convert the
1035 * C2D compatible format to the Android compatible format
1036 */
1037static int copy_image(private_handle_t *src_handle,
1038 struct copybit_image_t const *rhs,
1039 eConversionType conversionType)
1040{
1041 if (src_handle->fd == -1) {
1042 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
1043 return COPYBIT_FAILURE;
1044 }
1045
1046 // Copy the info.
1047 int ret = COPYBIT_SUCCESS;
1048 switch(rhs->format) {
1049 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1050 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1051 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1052 {
1053 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
1054 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
1055 } else {
1056 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
1057 }
1058
1059 } break;
1060 default: {
1061 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
1062 ret = COPYBIT_FAILURE;
1063 } break;
1064 }
1065 return ret;
1066}
1067
1068static void delete_handle(private_handle_t *handle)
1069{
1070 if (handle) {
1071 delete handle;
1072 handle = 0;
1073 }
1074}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001075
1076static bool need_to_execute_draw(struct copybit_context_t* ctx,
1077 eC2DFlags flags)
1078{
1079 if (flags & FLAGS_TEMP_SRC_DST) {
1080 return true;
1081 }
1082 if (flags & FLAGS_YUV_DESTINATION) {
1083 return true;
1084 }
1085 return false;
1086}
1087
Naseer Ahmed29a26812012-06-14 00:56:20 -07001088/** do a stretch blit type operation */
1089static int stretch_copybit_internal(
1090 struct copybit_device_t *dev,
1091 struct copybit_image_t const *dst,
1092 struct copybit_image_t const *src,
1093 struct copybit_rect_t const *dst_rect,
1094 struct copybit_rect_t const *src_rect,
1095 struct copybit_region_t const *region,
1096 bool enableBlend)
1097{
1098 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1099 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001100 int flags = 0;
1101 int src_surface_type;
1102 int mapped_src_idx = -1, mapped_dst_idx = -1;
1103 C2D_OBJECT_STR src_surface;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001104
1105 if (!ctx) {
1106 ALOGE("%s: null context error", __FUNCTION__);
1107 return -EINVAL;
1108 }
1109
1110 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1111 ALOGE("%s: src dimension error", __FUNCTION__);
1112 return -EINVAL;
1113 }
1114
1115 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001116 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w,
1117 dst->h);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001118 return -EINVAL;
1119 }
1120
Naseer Ahmed29a26812012-06-14 00:56:20 -07001121 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001122 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__,
1123 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001124 return COPYBIT_FAILURE;
1125 }
1126
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001127 int dst_surface_type;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001128 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001129 dst_surface_type = RGB_SURFACE;
1130 flags |= FLAGS_PREMULTIPLIED_ALPHA;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001131 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001132 int num_planes = get_num_planes(dst->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001133 flags |= FLAGS_YUV_DESTINATION;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001134 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001135 dst_surface_type = YUV_SURFACE_2_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001136 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001137 dst_surface_type = YUV_SURFACE_3_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001138 } else {
1139 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1140 __FUNCTION__, dst->format);
1141 return COPYBIT_FAILURE;
1142 }
1143 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001144 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__,
1145 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001146 return COPYBIT_FAILURE;
1147 }
1148
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001149 if (ctx->blit_rgb_count == MAX_RGB_SURFACES ||
1150 ctx->blit_yuv_2_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1151 ctx->blit_yuv_3_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1152 ctx->blit_count == MAX_BLIT_OBJECT_COUNT ||
1153 ctx->dst_surface_type != dst_surface_type) {
1154 // we have reached the max. limits of our internal structures or
1155 // changed the target.
1156 // Draw the remaining surfaces. We need to do the finish here since
1157 // we need to free up the surface templates.
1158 finish_copybit(dev);
1159 }
1160
1161 ctx->dst_surface_type = dst_surface_type;
1162
1163 // Update the destination
Naseer Ahmed29a26812012-06-14 00:56:20 -07001164 copybit_image_t dst_image;
1165 dst_image.w = dst->w;
1166 dst_image.h = dst->h;
1167 dst_image.format = dst->format;
1168 dst_image.handle = dst->handle;
1169 // Check if we need a temp. copy for the destination. We'd need this the destination
1170 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1171 // aligned to 32.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001172 bool need_temp_dst = need_temp_buffer(dst);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001173 bufferInfo dst_info;
1174 populate_buffer_info(dst, dst_info);
1175 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1176 dst_info.width, dst_info.height);
1177 if (dst_hnd == NULL) {
1178 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1179 return COPYBIT_FAILURE;
1180 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001181 if (need_temp_dst) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001182 if (get_size(dst_info) != ctx->temp_dst_buffer.size) {
1183 free_temp_buffer(ctx->temp_dst_buffer);
1184 // Create a temp buffer and set that as the destination.
1185 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1186 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1187 delete_handle(dst_hnd);
1188 return COPYBIT_FAILURE;
1189 }
1190 }
1191 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1192 dst_hnd->size = ctx->temp_dst_buffer.size;
1193 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
1194 dst_hnd->base = (int)(ctx->temp_dst_buffer.base);
1195 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1196 dst_hnd->gpuaddr = 0;
1197 dst_image.handle = dst_hnd;
1198 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -07001199 if(!ctx->dst_surface_mapped) {
1200 //map the destination surface to GPU address
1201 status = set_image(ctx, ctx->dst[ctx->dst_surface_type], &dst_image,
1202 (eC2DFlags)flags, mapped_dst_idx);
1203 if(status) {
1204 ALOGE("%s: dst: set_image error", __FUNCTION__);
1205 delete_handle(dst_hnd);
1206 unmap_gpuaddr(ctx, mapped_dst_idx);
1207 return COPYBIT_FAILURE;
1208 }
1209 ctx->dst_surface_mapped = true;
1210 ctx->dst_surface_base = dst->base;
1211 } else if(ctx->dst_surface_mapped && ctx->dst_surface_base != dst->base) {
1212 // Destination surface for the operation should be same for multiple
1213 // requests, this check is catch if there is any case when the
1214 // destination changes
1215 ALOGE("%s: a different destination surface!!", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001216 }
1217
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001218 // Update the source
1219 flags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001220 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001221 src_surface_type = RGB_SURFACE;
1222 src_surface = ctx->blit_rgb_object[ctx->blit_rgb_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001223 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1224 int num_planes = get_num_planes(src->format);
1225 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001226 src_surface_type = YUV_SURFACE_2_PLANES;
1227 src_surface = ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001228 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001229 src_surface_type = YUV_SURFACE_3_PLANES;
1230 src_surface = ctx->blit_yuv_3_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001231 } else {
1232 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1233 __FUNCTION__, src->format);
1234 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001235 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001236 return -EINVAL;
1237 }
1238 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001239 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__,
1240 src->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001241 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001242 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001243 return -EINVAL;
1244 }
1245
1246 copybit_image_t src_image;
1247 src_image.w = src->w;
1248 src_image.h = src->h;
1249 src_image.format = src->format;
1250 src_image.handle = src->handle;
1251
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001252 bool need_temp_src = need_temp_buffer(src);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001253 bufferInfo src_info;
1254 populate_buffer_info(src, src_info);
1255 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 -08001256 src_info.width, src_info.height);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001257 if (NULL == src_hnd) {
1258 ALOGE("%s: src_hnd is null", __FUNCTION__);
1259 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001260 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001261 return COPYBIT_FAILURE;
1262 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001263 if (need_temp_src) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001264 if (get_size(src_info) != ctx->temp_src_buffer.size) {
1265 free_temp_buffer(ctx->temp_src_buffer);
1266 // Create a temp buffer and set that as the destination.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001267 if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
1268 ctx->temp_src_buffer)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001269 ALOGE("%s: get_temp_buffer(src) failed", __FUNCTION__);
1270 delete_handle(dst_hnd);
1271 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001272 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001273 return COPYBIT_FAILURE;
1274 }
1275 }
1276 src_hnd->fd = ctx->temp_src_buffer.fd;
1277 src_hnd->size = ctx->temp_src_buffer.size;
1278 src_hnd->flags = ctx->temp_src_buffer.allocType;
1279 src_hnd->base = (int)(ctx->temp_src_buffer.base);
1280 src_hnd->offset = ctx->temp_src_buffer.offset;
1281 src_hnd->gpuaddr = 0;
1282 src_image.handle = src_hnd;
1283
1284 // Copy the source.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001285 status = copy_image((private_handle_t *)src->handle, &src_image,
1286 CONVERT_TO_C2D_FORMAT);
1287 if (status == COPYBIT_FAILURE) {
1288 ALOGE("%s:copy_image failed in temp source",__FUNCTION__);
1289 delete_handle(dst_hnd);
1290 delete_handle(src_hnd);
1291 unmap_gpuaddr(ctx, mapped_dst_idx);
1292 return status;
1293 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001294
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001295 // Clean the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001296 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001297 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001298 src_hnd->offset, src_hnd->fd,
1299 gralloc::CACHE_CLEAN)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001300 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1301 delete_handle(dst_hnd);
1302 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001303 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001304 return COPYBIT_FAILURE;
1305 }
1306 }
1307
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001308 flags |= (ctx->is_premultiplied_alpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1309 flags |= (ctx->dst_surface_type != RGB_SURFACE) ? FLAGS_YUV_DESTINATION : 0;
1310 status = set_image(ctx, src_surface.surface_id, &src_image,
1311 (eC2DFlags)flags, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001312 if(status) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001313 ALOGE("%s: set_image (src) error", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001314 delete_handle(dst_hnd);
1315 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001316 unmap_gpuaddr(ctx, mapped_dst_idx);
1317 unmap_gpuaddr(ctx, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001318 return COPYBIT_FAILURE;
1319 }
1320
Arun Kumar K.Re29916b2013-03-20 11:42:53 -07001321 src_surface.config_mask = C2D_NO_ANTIALIASING_BIT | ctx->config_mask;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001322 src_surface.global_alpha = ctx->src_global_alpha;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001323 if (enableBlend) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001324 if(src_surface.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1325 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1326 if(!(src_surface.global_alpha)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001327 // src alpha is zero
Naseer Ahmed29a26812012-06-14 00:56:20 -07001328 delete_handle(dst_hnd);
1329 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001330 unmap_gpuaddr(ctx, mapped_dst_idx);
1331 unmap_gpuaddr(ctx, mapped_src_idx);
1332 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001333 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001334 }
1335 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001336 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001337 }
1338
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001339 if (src_surface_type == RGB_SURFACE) {
1340 ctx->blit_rgb_object[ctx->blit_rgb_count] = src_surface;
1341 ctx->blit_rgb_count++;
1342 } else if (src_surface_type == YUV_SURFACE_2_PLANES) {
1343 ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count] = src_surface;
1344 ctx->blit_yuv_2_plane_count++;
1345 } else {
1346 ctx->blit_yuv_3_plane_object[ctx->blit_yuv_3_plane_count] = src_surface;
1347 ctx->blit_yuv_3_plane_count++;
1348 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001349
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001350 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001351 while ((status == 0) && region->next(region, &clip)) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001352 set_rects(ctx, &(src_surface), dst_rect, src_rect, &clip);
1353 if (ctx->blit_count == MAX_BLIT_OBJECT_COUNT) {
1354 ALOGW("Reached end of blit count");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001355 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001356 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001357 ctx->blit_list[ctx->blit_count] = src_surface;
1358 ctx->blit_count++;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001359 }
1360
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001361 // Check if we need to perform an early draw-finish.
1362 flags |= (need_temp_dst || need_temp_src) ? FLAGS_TEMP_SRC_DST : 0;
1363 if (need_to_execute_draw(ctx, (eC2DFlags)flags))
1364 {
1365 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001366 }
1367
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001368 if (need_temp_dst) {
1369 // copy the temp. destination without the alignment to the actual
1370 // destination.
1371 status = copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1372 if (status == COPYBIT_FAILURE) {
1373 ALOGE("%s:copy_image failed in temp Dest",__FUNCTION__);
1374 delete_handle(dst_hnd);
1375 delete_handle(src_hnd);
1376 unmap_gpuaddr(ctx, mapped_dst_idx);
1377 unmap_gpuaddr(ctx, mapped_src_idx);
1378 return status;
1379 }
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001380 // Clean the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001381 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001382 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001383 dst_hnd->offset, dst_hnd->fd,
1384 gralloc::CACHE_CLEAN);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001385 }
1386 delete_handle(dst_hnd);
1387 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001388
1389 ctx->is_premultiplied_alpha = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001390 ctx->fb_width = 0;
1391 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001392 ctx->config_mask = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001393 return status;
1394}
1395
Terence Hampsonadf47302013-05-23 12:21:02 -04001396static int set_sync_copybit(struct copybit_device_t *dev,
1397 int acquireFenceFd)
1398{
1399 return 0;
1400}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001401
Naseer Ahmed29a26812012-06-14 00:56:20 -07001402static int stretch_copybit(
1403 struct copybit_device_t *dev,
1404 struct copybit_image_t const *dst,
1405 struct copybit_image_t const *src,
1406 struct copybit_rect_t const *dst_rect,
1407 struct copybit_rect_t const *src_rect,
1408 struct copybit_region_t const *region)
1409{
1410 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001411 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001412 bool needsBlending = (ctx->src_global_alpha != 0);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001413 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1414 status = stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001415 region, needsBlending);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001416 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1417 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001418}
1419
1420/** Perform a blit type operation */
1421static int blit_copybit(
1422 struct copybit_device_t *dev,
1423 struct copybit_image_t const *dst,
1424 struct copybit_image_t const *src,
1425 struct copybit_region_t const *region)
1426{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001427 int status = COPYBIT_SUCCESS;
1428 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Naseer Ahmedbb887bd2013-04-16 14:12:27 -04001429 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
1430 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001431 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1432 status = stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1433 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1434 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001435}
1436
Sushil Chauhan943797c2013-10-21 17:35:55 -07001437/** Fill the rect on dst with RGBA color **/
1438static int fill_color(struct copybit_device_t *dev,
1439 struct copybit_image_t const *dst,
1440 struct copybit_rect_t const *rect,
1441 uint32_t color)
1442{
1443 // TODO: Implement once c2d driver supports color fill
1444 return -EINVAL;
1445}
1446
Naseer Ahmed29a26812012-06-14 00:56:20 -07001447/*****************************************************************************/
1448
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001449static void clean_up(copybit_context_t* ctx)
1450{
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001451 void* ret;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001452 if (!ctx)
1453 return;
1454
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001455 // stop the wait_cleanup_thread
1456 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1457 ctx->stop_thread = true;
1458 // Signal waiting thread
1459 pthread_cond_signal(&ctx->wait_cleanup_cond);
1460 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1461 // waits for the cleanup thread to exit
1462 pthread_join(ctx->wait_thread_id, &ret);
1463 pthread_mutex_destroy(&ctx->wait_cleanup_lock);
1464 pthread_cond_destroy (&ctx->wait_cleanup_cond);
1465
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001466 for (int i = 0; i < NUM_SURFACE_TYPES; i++) {
1467 if (ctx->dst[i])
1468 LINK_c2dDestroySurface(ctx->dst[i]);
1469 }
1470
1471 for (int i = 0; i < MAX_RGB_SURFACES; i++) {
1472 if (ctx->blit_rgb_object[i].surface_id)
1473 LINK_c2dDestroySurface(ctx->blit_rgb_object[i].surface_id);
1474 }
1475
1476 for (int i = 0; i < MAX_YUV_2_PLANE_SURFACES; i++) {
1477 if (ctx->blit_yuv_2_plane_object[i].surface_id)
1478 LINK_c2dDestroySurface(ctx->blit_yuv_2_plane_object[i].surface_id);
1479 }
1480
1481 for (int i = 0; i < MAX_YUV_3_PLANE_SURFACES; i++) {
1482 if (ctx->blit_yuv_3_plane_object[i].surface_id)
1483 LINK_c2dDestroySurface(ctx->blit_yuv_3_plane_object[i].surface_id);
1484 }
1485
1486 if (ctx->libc2d2) {
1487 ::dlclose(ctx->libc2d2);
1488 ALOGV("dlclose(libc2d2)");
1489 }
1490
1491 free(ctx);
1492}
1493
Naseer Ahmed29a26812012-06-14 00:56:20 -07001494/** Close the copybit device */
1495static int close_copybit(struct hw_device_t *dev)
1496{
1497 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1498 if (ctx) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001499 free_temp_buffer(ctx->temp_src_buffer);
1500 free_temp_buffer(ctx->temp_dst_buffer);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001501 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001502 clean_up(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001503 return 0;
1504}
1505
1506/** Open a new instance of a copybit device using name */
1507static int open_copybit(const struct hw_module_t* module, const char* name,
1508 struct hw_device_t** device)
1509{
1510 int status = COPYBIT_SUCCESS;
1511 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1512 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1513 struct copybit_context_t *ctx;
1514 char fbName[64];
1515
1516 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1517 if(!ctx) {
1518 ALOGE("%s: malloc failed", __FUNCTION__);
1519 return COPYBIT_FAILURE;
1520 }
1521
1522 /* initialize drawstate */
1523 memset(ctx, 0, sizeof(*ctx));
Naseer Ahmed29a26812012-06-14 00:56:20 -07001524 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1525 if (!ctx->libc2d2) {
1526 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001527 clean_up(ctx);
1528 status = COPYBIT_FAILURE;
1529 *device = NULL;
1530 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001531 }
1532 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1533 "c2dCreateSurface");
1534 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1535 "c2dUpdateSurface");
1536 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1537 "c2dReadSurface");
1538 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1539 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1540 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1541 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1542 "c2dWaitTimestamp");
1543 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1544 "c2dDestroySurface");
1545 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1546 "c2dMapAddr");
1547 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1548 "c2dUnMapAddr");
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001549 *(void **)&LINK_c2dGetDriverCapabilities = ::dlsym(ctx->libc2d2,
1550 "c2dGetDriverCapabilities");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001551 *(void **)&LINK_c2dCreateFenceFD = ::dlsym(ctx->libc2d2,
1552 "c2dCreateFenceFD");
Naseer Ahmed183939d2013-03-14 20:44:17 -04001553 *(void **)&LINK_c2dFillSurface = ::dlsym(ctx->libc2d2,
1554 "c2dFillSurface");
Naseer Ahmed29a26812012-06-14 00:56:20 -07001555
1556 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001557 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp ||
1558 !LINK_c2dFinish || !LINK_c2dDestroySurface ||
Naseer Ahmed183939d2013-03-14 20:44:17 -04001559 !LINK_c2dGetDriverCapabilities || !LINK_c2dCreateFenceFD ||
1560 !LINK_c2dFillSurface) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001561 ALOGE("%s: dlsym ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001562 clean_up(ctx);
1563 status = COPYBIT_FAILURE;
1564 *device = NULL;
1565 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001566 }
1567
1568 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1569 ctx->device.common.version = 1;
1570 ctx->device.common.module = (hw_module_t*)(module);
1571 ctx->device.common.close = close_copybit;
1572 ctx->device.set_parameter = set_parameter_copybit;
1573 ctx->device.get = get;
1574 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -04001575 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001576 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001577 ctx->device.finish = finish_copybit;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001578 ctx->device.flush_get_fence = flush_get_fence_copybit;
Naseer Ahmed183939d2013-03-14 20:44:17 -04001579 ctx->device.clear = clear_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -07001580 ctx->device.fill_color = fill_color;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001581
1582 /* Create RGB Surface */
1583 surfDefinition.buffer = (void*)0xdddddddd;
1584 surfDefinition.phys = (void*)0xdddddddd;
1585 surfDefinition.stride = 1 * 4;
1586 surfDefinition.width = 1;
1587 surfDefinition.height = 1;
1588 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1589 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1590 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001591 C2D_SURFACE_WITH_PHYS |
1592 C2D_SURFACE_WITH_PHYS_DUMMY ),
1593 &surfDefinition)) {
1594 ALOGE("%s: create ctx->dst_surface[RGB_SURFACE] failed", __FUNCTION__);
1595 ctx->dst[RGB_SURFACE] = 0;
1596 clean_up(ctx);
1597 status = COPYBIT_FAILURE;
1598 *device = NULL;
1599 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001600 }
1601
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001602 unsigned int surface_id = 0;
1603 for (int i = 0; i < MAX_RGB_SURFACES; i++)
1604 {
1605 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001606 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001607 C2D_SURFACE_WITH_PHYS |
1608 C2D_SURFACE_WITH_PHYS_DUMMY ),
1609 &surfDefinition)) {
1610 ALOGE("%s: create RGB source surface %d failed", __FUNCTION__, i);
1611 ctx->blit_rgb_object[i].surface_id = 0;
1612 status = COPYBIT_FAILURE;
1613 break;
1614 } else {
1615 ctx->blit_rgb_object[i].surface_id = surface_id;
1616 ALOGW("%s i = %d surface_id=%d", __FUNCTION__, i,
1617 ctx->blit_rgb_object[i].surface_id);
1618 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001619 }
1620
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001621 if (status == COPYBIT_FAILURE) {
1622 clean_up(ctx);
1623 status = COPYBIT_FAILURE;
1624 *device = NULL;
1625 return status;
1626 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001627
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001628 // Create 2 plane YUV surfaces
1629 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001630 yuvSurfaceDef.width = 4;
1631 yuvSurfaceDef.height = 4;
1632 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1633 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1634 yuvSurfaceDef.stride0 = 4;
1635
1636 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1637 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1638 yuvSurfaceDef.stride1 = 4;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001639 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1640 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001641 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1642 C2D_SURFACE_WITH_PHYS |
1643 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001644 &yuvSurfaceDef)) {
1645 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001646 ctx->dst[YUV_SURFACE_2_PLANES] = 0;
1647 clean_up(ctx);
1648 status = COPYBIT_FAILURE;
1649 *device = NULL;
1650 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001651 }
1652
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001653 for (int i=0; i < MAX_YUV_2_PLANE_SURFACES; i++)
1654 {
1655 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
1656 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1657 C2D_SURFACE_WITH_PHYS |
1658 C2D_SURFACE_WITH_PHYS_DUMMY ),
1659 &yuvSurfaceDef)) {
1660 ALOGE("%s: create YUV source %d failed", __FUNCTION__, i);
1661 ctx->blit_yuv_2_plane_object[i].surface_id = 0;
1662 status = COPYBIT_FAILURE;
1663 break;
1664 } else {
1665 ctx->blit_yuv_2_plane_object[i].surface_id = surface_id;
1666 ALOGW("%s: 2 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1667 ctx->blit_yuv_2_plane_object[i].surface_id);
1668 }
1669 }
1670
1671 if (status == COPYBIT_FAILURE) {
1672 clean_up(ctx);
1673 status = COPYBIT_FAILURE;
1674 *device = NULL;
1675 return status;
1676 }
1677
1678 // Create YUV 3 plane surfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -07001679 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1680 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1681 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1682 yuvSurfaceDef.stride2 = 4;
1683
Naseer Ahmed29a26812012-06-14 00:56:20 -07001684 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1685 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001686 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1687 C2D_SURFACE_WITH_PHYS |
1688 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001689 &yuvSurfaceDef)) {
1690 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001691 ctx->dst[YUV_SURFACE_3_PLANES] = 0;
1692 clean_up(ctx);
1693 status = COPYBIT_FAILURE;
1694 *device = NULL;
1695 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001696 }
1697
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001698 for (int i=0; i < MAX_YUV_3_PLANE_SURFACES; i++)
1699 {
1700 if (LINK_c2dCreateSurface(&(surface_id),
1701 C2D_TARGET | C2D_SOURCE,
1702 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1703 C2D_SURFACE_WITH_PHYS |
1704 C2D_SURFACE_WITH_PHYS_DUMMY),
1705 &yuvSurfaceDef)) {
1706 ALOGE("%s: create 3 plane YUV surface %d failed", __FUNCTION__, i);
1707 ctx->blit_yuv_3_plane_object[i].surface_id = 0;
1708 status = COPYBIT_FAILURE;
1709 break;
1710 } else {
1711 ctx->blit_yuv_3_plane_object[i].surface_id = surface_id;
1712 ALOGW("%s: 3 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1713 ctx->blit_yuv_3_plane_object[i].surface_id);
1714 }
1715 }
1716
1717 if (status == COPYBIT_FAILURE) {
1718 clean_up(ctx);
1719 status = COPYBIT_FAILURE;
1720 *device = NULL;
1721 return status;
1722 }
1723
1724 if (LINK_c2dGetDriverCapabilities(&(ctx->c2d_driver_info))) {
1725 ALOGE("%s: LINK_c2dGetDriverCapabilities failed", __FUNCTION__);
1726 clean_up(ctx);
1727 status = COPYBIT_FAILURE;
1728 *device = NULL;
1729 return status;
1730 }
1731 // Initialize context variables.
1732 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1733
Naseer Ahmed29a26812012-06-14 00:56:20 -07001734 ctx->temp_src_buffer.fd = -1;
1735 ctx->temp_src_buffer.base = 0;
1736 ctx->temp_src_buffer.size = 0;
1737
1738 ctx->temp_dst_buffer.fd = -1;
1739 ctx->temp_dst_buffer.base = 0;
1740 ctx->temp_dst_buffer.size = 0;
1741
1742 ctx->fb_width = 0;
1743 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001744
1745 ctx->blit_rgb_count = 0;
1746 ctx->blit_yuv_2_plane_count = 0;
1747 ctx->blit_yuv_3_plane_count = 0;
1748 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001749
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001750 ctx->wait_timestamp = false;
1751 ctx->stop_thread = false;
1752 pthread_mutex_init(&(ctx->wait_cleanup_lock), NULL);
1753 pthread_cond_init(&(ctx->wait_cleanup_cond), NULL);
1754 /* Start the wait thread */
1755 pthread_attr_t attr;
1756 pthread_attr_init(&attr);
1757 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1758
1759 pthread_create(&ctx->wait_thread_id, &attr, &c2d_wait_loop,
1760 (void *)ctx);
1761 pthread_attr_destroy(&attr);
1762
Naseer Ahmed29a26812012-06-14 00:56:20 -07001763 *device = &ctx->device.common;
1764 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001765}