blob: 23daa27454134ff43712d3d139f1c5b10dfc7e18 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05303 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08004 *
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>
Naseer Ahmed29a26812012-06-14 00:56:20 -070038#include <gralloc_priv.h>
39
40#include <copybit.h>
41#include <alloc_controller.h>
42#include <memalloc.h>
43
44#include "c2d2.h"
45#include "software_converter.h"
46
47#include <dlfcn.h>
48
49using gralloc::IMemAlloc;
50using gralloc::IonController;
51using gralloc::alloc_data;
Naseer Ahmed29a26812012-06-14 00:56:20 -070052
53C2D_STATUS (*LINK_c2dCreateSurface)( uint32 *surface_id,
54 uint32 surface_bits,
55 C2D_SURFACE_TYPE surface_type,
56 void *surface_definition );
57
58C2D_STATUS (*LINK_c2dUpdateSurface)( uint32 surface_id,
59 uint32 surface_bits,
60 C2D_SURFACE_TYPE surface_type,
61 void *surface_definition );
62
63C2D_STATUS (*LINK_c2dReadSurface)( uint32 surface_id,
64 C2D_SURFACE_TYPE surface_type,
65 void *surface_definition,
66 int32 x, int32 y );
67
68C2D_STATUS (*LINK_c2dDraw)( uint32 target_id,
69 uint32 target_config, C2D_RECT *target_scissor,
70 uint32 target_mask_id, uint32 target_color_key,
71 C2D_OBJECT *objects_list, uint32 num_objects );
72
73C2D_STATUS (*LINK_c2dFinish)( uint32 target_id);
74
75C2D_STATUS (*LINK_c2dFlush)( uint32 target_id, c2d_ts_handle *timestamp);
76
77C2D_STATUS (*LINK_c2dWaitTimestamp)( c2d_ts_handle timestamp );
78
79C2D_STATUS (*LINK_c2dDestroySurface)( uint32 surface_id );
80
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +053081C2D_STATUS (*LINK_c2dMapAddr) ( int mem_fd, void * hostptr, size_t len,
82 size_t offset, uint32 flags, void ** gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -070083
84C2D_STATUS (*LINK_c2dUnMapAddr) ( void * gpuaddr);
85
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -080086C2D_STATUS (*LINK_c2dGetDriverCapabilities) ( C2D_DRIVER_INFO * driver_info);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -080087
88/* create a fence fd for the timestamp */
89C2D_STATUS (*LINK_c2dCreateFenceFD) ( uint32 target_id, c2d_ts_handle timestamp,
90 int32 *fd);
Naseer Ahmed183939d2013-03-14 20:44:17 -040091
92C2D_STATUS (*LINK_c2dFillSurface) ( uint32 surface_id, uint32 fill_color,
93 C2D_RECT * fill_rect);
94
Naseer Ahmed29a26812012-06-14 00:56:20 -070095/******************************************************************************/
96
97#if defined(COPYBIT_Z180)
98#define MAX_SCALE_FACTOR (4096)
99#define MAX_DIMENSION (4096)
100#else
101#error "Unsupported HW version"
102#endif
103
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800104// The following defines can be changed as required i.e. as we encounter
105// complex use cases.
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700106#define MAX_RGB_SURFACES 32 // Max. RGB layers currently supported per draw
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800107#define MAX_YUV_2_PLANE_SURFACES 4// Max. 2-plane YUV layers currently supported per draw
108#define MAX_YUV_3_PLANE_SURFACES 1// Max. 3-plane YUV layers currently supported per draw
109// +1 for the destination surface. We cannot have multiple destination surfaces.
110#define MAX_SURFACES (MAX_RGB_SURFACES + MAX_YUV_2_PLANE_SURFACES + MAX_YUV_3_PLANE_SURFACES + 1)
111#define NUM_SURFACE_TYPES 3 // RGB_SURFACE + YUV_SURFACE_2_PLANES + YUV_SURFACE_3_PLANES
112#define MAX_BLIT_OBJECT_COUNT 50 // Max. blit objects that can be passed per draw
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113
114enum {
115 RGB_SURFACE,
116 YUV_SURFACE_2_PLANES,
117 YUV_SURFACE_3_PLANES
118};
119
120enum eConversionType {
121 CONVERT_TO_ANDROID_FORMAT,
122 CONVERT_TO_C2D_FORMAT
123};
124
125enum eC2DFlags {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800126 FLAGS_PREMULTIPLIED_ALPHA = 1<<0,
127 FLAGS_YUV_DESTINATION = 1<<1,
128 FLAGS_TEMP_SRC_DST = 1<<2
Naseer Ahmed29a26812012-06-14 00:56:20 -0700129};
130
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700131static gralloc::IAllocController* sAlloc = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700132/******************************************************************************/
133
134/** State information for each device instance */
135struct copybit_context_t {
136 struct copybit_device_t device;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800137 // Templates for the various source surfaces. These templates are created
138 // to avoid the expensive create/destroy C2D Surfaces
139 C2D_OBJECT_STR blit_rgb_object[MAX_RGB_SURFACES];
140 C2D_OBJECT_STR blit_yuv_2_plane_object[MAX_YUV_2_PLANE_SURFACES];
141 C2D_OBJECT_STR blit_yuv_3_plane_object[MAX_YUV_3_PLANE_SURFACES];
142 C2D_OBJECT_STR blit_list[MAX_BLIT_OBJECT_COUNT]; // Z-ordered list of blit objects
143 C2D_DRIVER_INFO c2d_driver_info;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700144 void *libc2d2;
145 alloc_data temp_src_buffer;
146 alloc_data temp_dst_buffer;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800147 unsigned int dst[NUM_SURFACE_TYPES]; // dst surfaces
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530148 uintptr_t mapped_gpu_addr[MAX_SURFACES]; // GPU addresses mapped inside copybit
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800149 int blit_rgb_count; // Total RGB surfaces being blit
150 int blit_yuv_2_plane_count; // Total 2 plane YUV surfaces being
151 int blit_yuv_3_plane_count; // Total 3 plane YUV surfaces being blit
152 int blit_count; // Total blit objects.
153 unsigned int trg_transform; /* target transform */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700154 int fb_width;
155 int fb_height;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800156 int src_global_alpha;
157 int config_mask;
158 int dst_surface_type;
159 bool is_premultiplied_alpha;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800160 void* time_stamp;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700161 bool dst_surface_mapped; // Set when dst surface is mapped to GPU addr
162 void* dst_surface_base; // Stores the dst surface addr
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800163
164 // used for signaling the wait thread
165 bool wait_timestamp;
166 pthread_t wait_thread_id;
167 bool stop_thread;
168 pthread_mutex_t wait_cleanup_lock;
169 pthread_cond_t wait_cleanup_cond;
170
Naseer Ahmed29a26812012-06-14 00:56:20 -0700171};
172
173struct bufferInfo {
174 int width;
175 int height;
176 int format;
177};
178
179struct yuvPlaneInfo {
180 int yStride; //luma stride
181 int plane1_stride;
182 int plane2_stride;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530183 size_t plane1_offset;
184 size_t plane2_offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700185};
186
187/**
188 * Common hardware methods
189 */
190
191static int open_copybit(const struct hw_module_t* module, const char* name,
192 struct hw_device_t** device);
193
194static struct hw_module_methods_t copybit_module_methods = {
195open: open_copybit
196};
197
198/*
199 * The COPYBIT Module
200 */
201struct copybit_module_t HAL_MODULE_INFO_SYM = {
202common: {
203tag: HARDWARE_MODULE_TAG,
204 version_major: 1,
205 version_minor: 0,
206 id: COPYBIT_HARDWARE_MODULE_ID,
207 name: "QCT COPYBIT C2D 2.0 Module",
208 author: "Qualcomm",
209 methods: &copybit_module_methods
210 }
211};
212
213
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800214/* thread function which waits on the timeStamp and cleans up the surfaces */
215static void* c2d_wait_loop(void* ptr) {
216 copybit_context_t* ctx = (copybit_context_t*)(ptr);
217 char thread_name[64] = "copybitWaitThr";
218 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
219 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
220
221 while(ctx->stop_thread == false) {
222 pthread_mutex_lock(&ctx->wait_cleanup_lock);
223 while(ctx->wait_timestamp == false && !ctx->stop_thread) {
224 pthread_cond_wait(&(ctx->wait_cleanup_cond),
225 &(ctx->wait_cleanup_lock));
226 }
227 if(ctx->wait_timestamp) {
228 if(LINK_c2dWaitTimestamp(ctx->time_stamp)) {
229 ALOGE("%s: LINK_c2dWaitTimeStamp ERROR!!", __FUNCTION__);
230 }
231 ctx->wait_timestamp = false;
232 // Unmap any mapped addresses.
233 for (int i = 0; i < MAX_SURFACES; i++) {
234 if (ctx->mapped_gpu_addr[i]) {
235 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
236 ctx->mapped_gpu_addr[i] = 0;
237 }
238 }
239 // Reset the counts after the draw.
240 ctx->blit_rgb_count = 0;
241 ctx->blit_yuv_2_plane_count = 0;
242 ctx->blit_yuv_3_plane_count = 0;
243 ctx->blit_count = 0;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700244 ctx->dst_surface_mapped = false;
245 ctx->dst_surface_base = 0;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800246 }
247 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
248 if(ctx->stop_thread)
249 break;
250 }
251 pthread_exit(NULL);
252 return NULL;
253}
254
255
Naseer Ahmed29a26812012-06-14 00:56:20 -0700256/* convert COPYBIT_FORMAT to C2D format */
257static int get_format(int format) {
258 switch (format) {
259 case HAL_PIXEL_FORMAT_RGB_565: return C2D_COLOR_FORMAT_565_RGB;
Omprakash Dhyade3cc819e2014-05-30 16:52:46 -0700260 case HAL_PIXEL_FORMAT_RGB_888: return C2D_COLOR_FORMAT_888_RGB |
261 C2D_FORMAT_SWAP_RB;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700262 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;
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700268 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;
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 Chauhan43755de2014-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
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530342static size_t c2d_get_gpuaddr(copybit_context_t* ctx,
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700343 struct private_handle_t *handle, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344{
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530345 uint32 memtype;
346 size_t *gpuaddr = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700347 C2D_STATUS rc;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700348 int freeindex = 0;
349 bool mapaddr = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700350
351 if(!handle)
352 return 0;
353
354 if (handle->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
355 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP))
356 memtype = KGSL_USER_MEM_TYPE_PMEM;
357 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
358 memtype = KGSL_USER_MEM_TYPE_ASHMEM;
359 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
360 memtype = KGSL_USER_MEM_TYPE_ION;
361 else {
362 ALOGE("Invalid handle flags: 0x%x", handle->flags);
363 return 0;
364 }
365
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700366 // Check for a freeindex in the mapped_gpu_addr list
367 for (freeindex = 0; freeindex < MAX_SURFACES; freeindex++) {
368 if (ctx->mapped_gpu_addr[freeindex] == 0) {
369 // free index is available
370 // map GPU addr and use this as mapped_idx
371 mapaddr = true;
372 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800373 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700374 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700375
376 if(mapaddr) {
377 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size,
378 handle->offset, memtype, (void**)&gpuaddr);
379
380 if (rc == C2D_STATUS_OK) {
381 // We have mapped the GPU address inside copybit. We need to unmap
382 // this address after the blit. Store this address
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530383 ctx->mapped_gpu_addr[freeindex] = (size_t)gpuaddr;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700384 mapped_idx = freeindex;
385 }
386 }
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530387 return (size_t)gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700388}
389
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800390static void unmap_gpuaddr(copybit_context_t* ctx, int mapped_idx)
391{
392 if (!ctx || (mapped_idx == -1))
393 return;
394
395 if (ctx->mapped_gpu_addr[mapped_idx]) {
396 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[mapped_idx]);
397 ctx->mapped_gpu_addr[mapped_idx] = 0;
398 }
399}
400
Naseer Ahmed29a26812012-06-14 00:56:20 -0700401static int is_supported_rgb_format(int format)
402{
403 switch(format) {
404 case HAL_PIXEL_FORMAT_RGBA_8888:
405 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan43755de2014-05-15 11:20:32 -0700406 case HAL_PIXEL_FORMAT_RGB_888:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700407 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700408 case HAL_PIXEL_FORMAT_BGRA_8888:
409 case HAL_PIXEL_FORMAT_RGBA_5551:
410 case HAL_PIXEL_FORMAT_RGBA_4444: {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700411 return COPYBIT_SUCCESS;
412 }
413 default:
414 return COPYBIT_FAILURE;
415 }
416}
417
418static int get_num_planes(int format)
419{
420 switch(format) {
421 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
422 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
423 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
424 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
425 return 2;
426 }
427 case HAL_PIXEL_FORMAT_YV12: {
428 return 3;
429 }
430 default:
431 return COPYBIT_FAILURE;
432 }
433}
434
435static int is_supported_yuv_format(int format)
436{
437 switch(format) {
438 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
439 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
440 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
441 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
442 return COPYBIT_SUCCESS;
443 }
444 default:
445 return COPYBIT_FAILURE;
446 }
447}
448
449static int is_valid_destination_format(int format)
450{
451 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
452 // C2D does not support NV12Tile as a destination format.
453 return COPYBIT_FAILURE;
454 }
455 return COPYBIT_SUCCESS;
456}
457
458static int calculate_yuv_offset_and_stride(const bufferInfo& info,
459 yuvPlaneInfo& yuvInfo)
460{
461 int width = info.width;
462 int height = info.height;
463 int format = info.format;
464
465 int aligned_height = 0;
466 int aligned_width = 0, size = 0;
467
468 switch (format) {
469 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
470 /* NV12 Tile buffers have their luma height aligned to 32bytes and width
471 * aligned to 128 bytes. The chroma offset starts at an 8K boundary
472 */
473 aligned_height = ALIGN(height, 32);
474 aligned_width = ALIGN(width, 128);
475 size = aligned_width * aligned_height;
476 yuvInfo.plane1_offset = ALIGN(size,8192);
477 yuvInfo.yStride = aligned_width;
478 yuvInfo.plane1_stride = aligned_width;
479 break;
480 }
481 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
482 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
483 case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
484 aligned_width = ALIGN(width, 32);
485 yuvInfo.yStride = aligned_width;
486 yuvInfo.plane1_stride = aligned_width;
487 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
488 // The encoder requires a 2K aligned chroma offset
489 yuvInfo.plane1_offset = ALIGN(aligned_width * height, 2048);
490 } else
491 yuvInfo.plane1_offset = aligned_width * height;
492
493 break;
494 }
495 default: {
496 return COPYBIT_FAILURE;
497 }
498 }
499 return COPYBIT_SUCCESS;
500}
501
502/** create C2D surface from copybit image */
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800503static int set_image(copybit_context_t* ctx, uint32 surfaceId,
504 const struct copybit_image_t *rhs,
505 const eC2DFlags flags, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700506{
507 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
508 C2D_SURFACE_TYPE surfaceType;
509 int status = COPYBIT_SUCCESS;
Naseer Ahmed9eb5e092014-09-25 13:24:44 -0400510 uint64_t gpuaddr = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800511 int c2d_format;
512 mapped_idx = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700513
514 if (flags & FLAGS_YUV_DESTINATION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800515 c2d_format = get_c2d_format_for_yuv_destination(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700516 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800517 c2d_format = get_format(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 }
519
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800520 if(c2d_format == -EINVAL) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700521 ALOGE("%s: invalid format", __FUNCTION__);
522 return -EINVAL;
523 }
524
525 if(handle == NULL) {
526 ALOGE("%s: invalid handle", __func__);
527 return -EINVAL;
528 }
529
530 if (handle->gpuaddr == 0) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800531 gpuaddr = c2d_get_gpuaddr(ctx, handle, mapped_idx);
532 if(!gpuaddr) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700533 ALOGE("%s: c2d_get_gpuaddr failed", __FUNCTION__);
534 return COPYBIT_FAILURE;
535 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800536 } else {
537 gpuaddr = handle->gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538 }
539
540 /* create C2D surface */
541 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
542 /* RGB */
543 C2D_RGB_SURFACE_DEF surfaceDef;
544
545 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS);
546
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800547 surfaceDef.phys = (void*) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700548 surfaceDef.buffer = (void*) (handle->base);
549
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800550 surfaceDef.format = c2d_format |
Naseer Ahmed29a26812012-06-14 00:56:20 -0700551 ((flags & FLAGS_PREMULTIPLIED_ALPHA) ? C2D_FORMAT_PREMULTIPLIED : 0);
552 surfaceDef.width = rhs->w;
553 surfaceDef.height = rhs->h;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530554 int aligned_width = ALIGN((int)surfaceDef.width,32);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700555 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
556
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800557 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
558 &surfaceDef)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700559 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800560 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700561 status = COPYBIT_FAILURE;
562 }
563 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
564 C2D_YUV_SURFACE_DEF surfaceDef;
565 memset(&surfaceDef, 0, sizeof(surfaceDef));
566 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800567 surfaceDef.format = c2d_format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700568
569 bufferInfo info;
570 info.width = rhs->w;
571 info.height = rhs->h;
572 info.format = rhs->format;
573
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700574 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700575 status = calculate_yuv_offset_and_stride(info, yuvInfo);
576 if(status != COPYBIT_SUCCESS) {
577 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800578 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700579 }
580
581 surfaceDef.width = rhs->w;
582 surfaceDef.height = rhs->h;
583 surfaceDef.plane0 = (void*) (handle->base);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800584 surfaceDef.phys0 = (void*) (gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700585 surfaceDef.stride0 = yuvInfo.yStride;
586
587 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800588 surfaceDef.phys1 = (void*) (gpuaddr + yuvInfo.plane1_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700589 surfaceDef.stride1 = yuvInfo.plane1_stride;
590 if (3 == get_num_planes(rhs->format)) {
591 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800592 surfaceDef.phys2 = (void*) (gpuaddr + yuvInfo.plane2_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700593 surfaceDef.stride2 = yuvInfo.plane2_stride;
594 }
595
596 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
597 &surfaceDef)) {
598 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
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 } else {
603 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800604 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700605 status = COPYBIT_FAILURE;
606 }
607
608 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700609}
610
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800611/** copy the bits */
612static int msm_copybit(struct copybit_context_t *ctx, unsigned int target)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700613{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800614 if (ctx->blit_count == 0) {
615 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700616 }
617
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800618 for (int i = 0; i < ctx->blit_count; i++)
619 {
620 ctx->blit_list[i].next = &(ctx->blit_list[i+1]);
621 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800622 ctx->blit_list[ctx->blit_count-1].next = NULL;
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800623 uint32_t target_transform = ctx->trg_transform;
624 if (ctx->c2d_driver_info.capabilities_mask &
625 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
626 // For A3xx - set 0x0 as the transform is set in the config_mask
627 target_transform = 0x0;
628 }
629 if(LINK_c2dDraw(target, target_transform, 0x0, 0, 0, ctx->blit_list,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800630 ctx->blit_count)) {
631 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
632 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700633 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700634 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700635}
636
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800637
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800638
639static int flush_get_fence_copybit (struct copybit_device_t *dev, int* fd)
640{
641 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
642 int status = COPYBIT_FAILURE;
643 if (!ctx)
644 return COPYBIT_FAILURE;
645 pthread_mutex_lock(&ctx->wait_cleanup_lock);
646 status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
647
648 if(LINK_c2dFlush(ctx->dst[ctx->dst_surface_type], &ctx->time_stamp)) {
649 ALOGE("%s: LINK_c2dFlush ERROR", __FUNCTION__);
650 // unlock the mutex and return failure
651 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
652 return COPYBIT_FAILURE;
653 }
654 if(LINK_c2dCreateFenceFD(ctx->dst[ctx->dst_surface_type], ctx->time_stamp,
655 fd)) {
656 ALOGE("%s: LINK_c2dCreateFenceFD ERROR", __FUNCTION__);
657 status = COPYBIT_FAILURE;
658 }
659 if(status == COPYBIT_SUCCESS) {
660 //signal the wait_thread
661 ctx->wait_timestamp = true;
662 pthread_cond_signal(&ctx->wait_cleanup_cond);
663 }
664 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
665 return status;
666}
667
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800668static int finish_copybit(struct copybit_device_t *dev)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700669{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800670 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
671 if (!ctx)
672 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700673
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800674 int status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700675
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800676 if(LINK_c2dFinish(ctx->dst[ctx->dst_surface_type])) {
677 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
678 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700679 }
680
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800681 // Unmap any mapped addresses.
682 for (int i = 0; i < MAX_SURFACES; i++) {
683 if (ctx->mapped_gpu_addr[i]) {
684 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
685 ctx->mapped_gpu_addr[i] = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700686 }
687 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700688
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800689 // Reset the counts after the draw.
690 ctx->blit_rgb_count = 0;
691 ctx->blit_yuv_2_plane_count = 0;
692 ctx->blit_yuv_3_plane_count = 0;
693 ctx->blit_count = 0;
Pawan Kumar74d9ea92013-05-03 14:25:49 +0530694 ctx->dst_surface_mapped = false;
695 ctx->dst_surface_base = 0;
696
Naseer Ahmed29a26812012-06-14 00:56:20 -0700697 return status;
698}
699
Naseer Ahmed183939d2013-03-14 20:44:17 -0400700static int clear_copybit(struct copybit_device_t *dev,
701 struct copybit_image_t const *buf,
702 struct copybit_rect_t *rect)
703{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700704 int ret = COPYBIT_SUCCESS;
Naseer Ahmed183939d2013-03-14 20:44:17 -0400705 int flags = FLAGS_PREMULTIPLIED_ALPHA;
706 int mapped_dst_idx = -1;
707 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
708 C2D_RECT c2drect = {rect->l, rect->t, rect->r - rect->l, rect->b - rect->t};
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700709 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700710 if(!ctx->dst_surface_mapped) {
711 ret = set_image(ctx, ctx->dst[RGB_SURFACE], buf,
712 (eC2DFlags)flags, mapped_dst_idx);
713 if(ret) {
714 ALOGE("%s: set_image error", __FUNCTION__);
715 unmap_gpuaddr(ctx, mapped_dst_idx);
716 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
717 return COPYBIT_FAILURE;
718 }
719 //clear_copybit is the first call made by HWC for each composition
720 //with the dest surface, hence set dst_surface_mapped.
721 ctx->dst_surface_mapped = true;
722 ctx->dst_surface_base = buf->base;
723 ret = LINK_c2dFillSurface(ctx->dst[RGB_SURFACE], 0x0, &c2drect);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400724 }
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700725 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400726 return ret;
727}
728
729
Naseer Ahmed29a26812012-06-14 00:56:20 -0700730/** setup rectangles */
731static void set_rects(struct copybit_context_t *ctx,
732 C2D_OBJECT *c2dObject,
733 const struct copybit_rect_t *dst,
734 const struct copybit_rect_t *src,
735 const struct copybit_rect_t *scissor)
736{
737 // Set the target rect.
738 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
739 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
740 /* target rotation is 270 */
741 c2dObject->target_rect.x = (dst->t)<<16;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530742 c2dObject->target_rect.y = ctx->fb_width?
743 (ALIGN(ctx->fb_width,32)- dst->r):dst->r;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700744 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
745 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
746 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
747 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
748 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
749 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
750 c2dObject->target_rect.y = (dst->l)<<16;
751 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
752 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
753 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
754 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
755 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530756 c2dObject->target_rect.x = ctx->fb_width?
757 (ALIGN(ctx->fb_width,32) - dst->r):dst->r;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700758 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
759 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
760 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
761 } else {
762 c2dObject->target_rect.x = (dst->l)<<16;
763 c2dObject->target_rect.y = (dst->t)<<16;
764 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
765 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
766 }
767 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
768
769 // Set the source rect
770 c2dObject->source_rect.x = (src->l)<<16;
771 c2dObject->source_rect.y = (src->t)<<16;
772 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
773 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
774 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
775
776 // Set the scissor rect
777 c2dObject->scissor_rect.x = scissor->l;
778 c2dObject->scissor_rect.y = scissor->t;
779 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
780 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
781 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
782}
783
Naseer Ahmed29a26812012-06-14 00:56:20 -0700784/*****************************************************************************/
785
786/** Set a parameter to value */
787static int set_parameter_copybit(
788 struct copybit_device_t *dev,
789 int name,
790 int value)
791{
792 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800793 int status = COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700794 if (!ctx) {
795 ALOGE("%s: null context", __FUNCTION__);
796 return -EINVAL;
797 }
798
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800799 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700800 switch(name) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700801 case COPYBIT_PLANE_ALPHA:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800802 {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700803 if (value < 0) value = 0;
804 if (value >= 256) value = 255;
805
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800806 ctx->src_global_alpha = value;
807 if (value < 255)
808 ctx->config_mask |= C2D_GLOBAL_ALPHA_BIT;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700809 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800810 ctx->config_mask &= ~C2D_GLOBAL_ALPHA_BIT;
811 }
812 break;
813 case COPYBIT_BLEND_MODE:
814 {
815 if (value == COPYBIT_BLENDING_NONE) {
816 ctx->config_mask |= C2D_ALPHA_BLEND_NONE;
817 ctx->is_premultiplied_alpha = true;
818 } else if (value == COPYBIT_BLENDING_PREMULT) {
819 ctx->is_premultiplied_alpha = true;
820 } else {
821 ctx->config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700822 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800823 }
824 break;
825 case COPYBIT_TRANSFORM:
826 {
827 unsigned int transform = 0;
828 uint32 config_mask = 0;
829 config_mask |= C2D_OVERRIDE_GLOBAL_TARGET_ROTATE_CONFIG;
830 if((value & 0x7) == COPYBIT_TRANSFORM_ROT_180) {
831 transform = C2D_TARGET_ROTATE_180;
832 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_180;
833 } else if((value & 0x7) == COPYBIT_TRANSFORM_ROT_270) {
834 transform = C2D_TARGET_ROTATE_90;
835 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_90;
836 } else if(value == COPYBIT_TRANSFORM_ROT_90) {
837 transform = C2D_TARGET_ROTATE_270;
838 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_270;
839 } else {
840 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_0;
841 if(value & COPYBIT_TRANSFORM_FLIP_H) {
842 config_mask |= C2D_MIRROR_H_BIT;
843 } else if(value & COPYBIT_TRANSFORM_FLIP_V) {
844 config_mask |= C2D_MIRROR_V_BIT;
845 }
846 }
847
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800848 if (ctx->c2d_driver_info.capabilities_mask &
849 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
850 ctx->config_mask |= config_mask;
851 } else {
852 // The transform for this surface does not match the current
853 // target transform. Draw all previous surfaces. This will be
854 // changed once we have a new mechanism to send different
855 // target rotations to c2d.
856 finish_copybit(dev);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800857 }
858 ctx->trg_transform = transform;
859 }
860 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700861 case COPYBIT_FRAMEBUFFER_WIDTH:
862 ctx->fb_width = value;
863 break;
864 case COPYBIT_FRAMEBUFFER_HEIGHT:
865 ctx->fb_height = value;
866 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800867 case COPYBIT_ROTATION_DEG:
868 case COPYBIT_DITHER:
869 case COPYBIT_BLUR:
Naseer Ahmed45a99602012-07-31 19:15:24 -0700870 case COPYBIT_BLIT_TO_FRAMEBUFFER:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800871 // Do nothing
Naseer Ahmed45a99602012-07-31 19:15:24 -0700872 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700873 default:
874 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800875 status = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700876 break;
877 }
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800878 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
879 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700880}
881
882/** Get a static info value */
883static int get(struct copybit_device_t *dev, int name)
884{
885 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
886 int value;
887
888 if (!ctx) {
889 ALOGE("%s: null context error", __FUNCTION__);
890 return -EINVAL;
891 }
892
893 switch(name) {
894 case COPYBIT_MINIFICATION_LIMIT:
895 value = MAX_SCALE_FACTOR;
896 break;
897 case COPYBIT_MAGNIFICATION_LIMIT:
898 value = MAX_SCALE_FACTOR;
899 break;
900 case COPYBIT_SCALING_FRAC_BITS:
901 value = 32;
902 break;
903 case COPYBIT_ROTATION_STEP_DEG:
904 value = 1;
905 break;
906 default:
907 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
908 value = -EINVAL;
909 }
910 return value;
911}
912
913static int is_alpha(int cformat)
914{
915 int alpha = 0;
916 switch (cformat & 0xFF) {
917 case C2D_COLOR_FORMAT_8888_ARGB:
918 case C2D_COLOR_FORMAT_8888_RGBA:
919 case C2D_COLOR_FORMAT_5551_RGBA:
920 case C2D_COLOR_FORMAT_4444_ARGB:
921 alpha = 1;
922 break;
923 default:
924 alpha = 0;
925 break;
926 }
927
928 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
929 alpha = 0;
930
931 return alpha;
932}
933
934/* Function to check if we need a temporary buffer for the blit.
935 * This would happen if the requested destination stride and the
936 * C2D stride do not match. We ignore RGB buffers, since their
937 * stride is always aligned to 32.
938 */
939static bool need_temp_buffer(struct copybit_image_t const *img)
940{
941 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
942 return false;
943
944 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
945
946 // The width parameter in the handle contains the aligned_w. We check if we
947 // need to convert based on this param. YUV formats have bpp=1, so checking
948 // if the requested stride is aligned should suffice.
949 if (0 == (handle->width)%32) {
950 return false;
951 }
952
953 return true;
954}
955
956/* Function to extract the information from the copybit image and set the corresponding
957 * values in the bufferInfo struct.
958 */
959static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
960{
961 info.width = img->w;
962 info.height = img->h;
963 info.format = img->format;
964}
965
966/* Function to get the required size for a particular format, inorder for C2D to perform
967 * the blit operation.
968 */
Shalaj Jaina70b4352014-06-15 13:47:47 -0700969static int get_size(const bufferInfo& info)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700970{
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530971 int size = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700972 int w = info.width;
973 int h = info.height;
974 int aligned_w = ALIGN(w, 32);
975 switch(info.format) {
976 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
977 {
978 // Chroma for this format is aligned to 2K.
979 size = ALIGN((aligned_w*h), 2048) +
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800980 ALIGN(aligned_w/2, 32) * (h/2) *2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700981 size = ALIGN(size, 4096);
982 } break;
983 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
984 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
985 {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800986 size = aligned_w * h +
987 ALIGN(aligned_w/2, 32) * (h/2) * 2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700988 size = ALIGN(size, 4096);
989 } break;
990 default: break;
991 }
992 return size;
993}
994
995/* Function to allocate memory for the temporary buffer. This memory is
996 * allocated from Ashmem. It is the caller's responsibility to free this
997 * memory.
998 */
999static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
1000{
1001 ALOGD("%s E", __FUNCTION__);
1002 // Alloc memory from system heap
1003 data.base = 0;
1004 data.fd = -1;
1005 data.offset = 0;
1006 data.size = get_size(info);
1007 data.align = getpagesize();
1008 data.uncached = true;
1009 int allocFlags = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
1010
1011 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001012 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -07001013 }
1014
1015 if (sAlloc == 0) {
1016 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
1017 return COPYBIT_FAILURE;
1018 }
1019
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001020 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001021 if (0 != err) {
1022 ALOGE("%s: allocate failed", __FUNCTION__);
1023 return COPYBIT_FAILURE;
1024 }
1025
1026 ALOGD("%s X", __FUNCTION__);
1027 return err;
1028}
1029
1030/* Function to free the temporary allocated memory.*/
1031static void free_temp_buffer(alloc_data &data)
1032{
1033 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001034 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001035 memalloc->free_buffer(data.base, data.size, 0, data.fd);
1036 }
1037}
1038
1039/* Function to perform the software color conversion. Convert the
1040 * C2D compatible format to the Android compatible format
1041 */
1042static int copy_image(private_handle_t *src_handle,
1043 struct copybit_image_t const *rhs,
1044 eConversionType conversionType)
1045{
1046 if (src_handle->fd == -1) {
1047 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
1048 return COPYBIT_FAILURE;
1049 }
1050
1051 // Copy the info.
1052 int ret = COPYBIT_SUCCESS;
1053 switch(rhs->format) {
1054 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1055 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1056 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1057 {
1058 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
1059 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
1060 } else {
1061 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
1062 }
1063
1064 } break;
1065 default: {
1066 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
1067 ret = COPYBIT_FAILURE;
1068 } break;
1069 }
1070 return ret;
1071}
1072
1073static void delete_handle(private_handle_t *handle)
1074{
1075 if (handle) {
1076 delete handle;
1077 handle = 0;
1078 }
1079}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001080
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301081static bool need_to_execute_draw(eC2DFlags flags)
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001082{
1083 if (flags & FLAGS_TEMP_SRC_DST) {
1084 return true;
1085 }
1086 if (flags & FLAGS_YUV_DESTINATION) {
1087 return true;
1088 }
1089 return false;
1090}
1091
Naseer Ahmed29a26812012-06-14 00:56:20 -07001092/** do a stretch blit type operation */
1093static int stretch_copybit_internal(
1094 struct copybit_device_t *dev,
1095 struct copybit_image_t const *dst,
1096 struct copybit_image_t const *src,
1097 struct copybit_rect_t const *dst_rect,
1098 struct copybit_rect_t const *src_rect,
1099 struct copybit_region_t const *region,
1100 bool enableBlend)
1101{
1102 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1103 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001104 int flags = 0;
1105 int src_surface_type;
1106 int mapped_src_idx = -1, mapped_dst_idx = -1;
1107 C2D_OBJECT_STR src_surface;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001108
1109 if (!ctx) {
1110 ALOGE("%s: null context error", __FUNCTION__);
1111 return -EINVAL;
1112 }
1113
1114 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1115 ALOGE("%s: src dimension error", __FUNCTION__);
1116 return -EINVAL;
1117 }
1118
1119 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001120 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w,
1121 dst->h);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001122 return -EINVAL;
1123 }
1124
Naseer Ahmed29a26812012-06-14 00:56:20 -07001125 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001126 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__,
1127 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001128 return COPYBIT_FAILURE;
1129 }
1130
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001131 int dst_surface_type;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001132 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001133 dst_surface_type = RGB_SURFACE;
1134 flags |= FLAGS_PREMULTIPLIED_ALPHA;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001135 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001136 int num_planes = get_num_planes(dst->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001137 flags |= FLAGS_YUV_DESTINATION;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001138 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001139 dst_surface_type = YUV_SURFACE_2_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001140 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001141 dst_surface_type = YUV_SURFACE_3_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001142 } else {
1143 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1144 __FUNCTION__, dst->format);
1145 return COPYBIT_FAILURE;
1146 }
1147 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001148 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__,
1149 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001150 return COPYBIT_FAILURE;
1151 }
1152
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001153 if (ctx->blit_rgb_count == MAX_RGB_SURFACES ||
1154 ctx->blit_yuv_2_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1155 ctx->blit_yuv_3_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1156 ctx->blit_count == MAX_BLIT_OBJECT_COUNT ||
1157 ctx->dst_surface_type != dst_surface_type) {
1158 // we have reached the max. limits of our internal structures or
1159 // changed the target.
1160 // Draw the remaining surfaces. We need to do the finish here since
1161 // we need to free up the surface templates.
1162 finish_copybit(dev);
1163 }
1164
1165 ctx->dst_surface_type = dst_surface_type;
1166
1167 // Update the destination
Naseer Ahmed29a26812012-06-14 00:56:20 -07001168 copybit_image_t dst_image;
1169 dst_image.w = dst->w;
1170 dst_image.h = dst->h;
1171 dst_image.format = dst->format;
1172 dst_image.handle = dst->handle;
1173 // Check if we need a temp. copy for the destination. We'd need this the destination
1174 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1175 // aligned to 32.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001176 bool need_temp_dst = need_temp_buffer(dst);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001177 bufferInfo dst_info;
1178 populate_buffer_info(dst, dst_info);
1179 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1180 dst_info.width, dst_info.height);
1181 if (dst_hnd == NULL) {
1182 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1183 return COPYBIT_FAILURE;
1184 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001185 if (need_temp_dst) {
Naseer Ahmed9eb5e092014-09-25 13:24:44 -04001186 if (get_size(dst_info) != (int) ctx->temp_dst_buffer.size) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001187 free_temp_buffer(ctx->temp_dst_buffer);
1188 // Create a temp buffer and set that as the destination.
1189 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1190 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1191 delete_handle(dst_hnd);
1192 return COPYBIT_FAILURE;
1193 }
1194 }
1195 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1196 dst_hnd->size = ctx->temp_dst_buffer.size;
1197 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301198 dst_hnd->base = (uintptr_t)(ctx->temp_dst_buffer.base);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001199 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1200 dst_hnd->gpuaddr = 0;
1201 dst_image.handle = dst_hnd;
1202 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -07001203 if(!ctx->dst_surface_mapped) {
1204 //map the destination surface to GPU address
1205 status = set_image(ctx, ctx->dst[ctx->dst_surface_type], &dst_image,
1206 (eC2DFlags)flags, mapped_dst_idx);
1207 if(status) {
1208 ALOGE("%s: dst: set_image error", __FUNCTION__);
1209 delete_handle(dst_hnd);
1210 unmap_gpuaddr(ctx, mapped_dst_idx);
1211 return COPYBIT_FAILURE;
1212 }
1213 ctx->dst_surface_mapped = true;
1214 ctx->dst_surface_base = dst->base;
1215 } else if(ctx->dst_surface_mapped && ctx->dst_surface_base != dst->base) {
1216 // Destination surface for the operation should be same for multiple
1217 // requests, this check is catch if there is any case when the
1218 // destination changes
1219 ALOGE("%s: a different destination surface!!", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001220 }
1221
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001222 // Update the source
1223 flags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001224 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001225 src_surface_type = RGB_SURFACE;
1226 src_surface = ctx->blit_rgb_object[ctx->blit_rgb_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001227 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1228 int num_planes = get_num_planes(src->format);
1229 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001230 src_surface_type = YUV_SURFACE_2_PLANES;
1231 src_surface = ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001232 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001233 src_surface_type = YUV_SURFACE_3_PLANES;
1234 src_surface = ctx->blit_yuv_3_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001235 } else {
1236 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1237 __FUNCTION__, src->format);
1238 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001239 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001240 return -EINVAL;
1241 }
1242 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001243 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__,
1244 src->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001245 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001246 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001247 return -EINVAL;
1248 }
1249
1250 copybit_image_t src_image;
1251 src_image.w = src->w;
1252 src_image.h = src->h;
1253 src_image.format = src->format;
1254 src_image.handle = src->handle;
1255
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001256 bool need_temp_src = need_temp_buffer(src);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001257 bufferInfo src_info;
1258 populate_buffer_info(src, src_info);
1259 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 -08001260 src_info.width, src_info.height);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001261 if (NULL == src_hnd) {
1262 ALOGE("%s: src_hnd is null", __FUNCTION__);
1263 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001264 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001265 return COPYBIT_FAILURE;
1266 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001267 if (need_temp_src) {
Naseer Ahmed9eb5e092014-09-25 13:24:44 -04001268 if (get_size(src_info) != (int) ctx->temp_src_buffer.size) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001269 free_temp_buffer(ctx->temp_src_buffer);
1270 // Create a temp buffer and set that as the destination.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001271 if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
1272 ctx->temp_src_buffer)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001273 ALOGE("%s: get_temp_buffer(src) failed", __FUNCTION__);
1274 delete_handle(dst_hnd);
1275 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001276 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001277 return COPYBIT_FAILURE;
1278 }
1279 }
1280 src_hnd->fd = ctx->temp_src_buffer.fd;
1281 src_hnd->size = ctx->temp_src_buffer.size;
1282 src_hnd->flags = ctx->temp_src_buffer.allocType;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301283 src_hnd->base = (uintptr_t)(ctx->temp_src_buffer.base);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001284 src_hnd->offset = ctx->temp_src_buffer.offset;
1285 src_hnd->gpuaddr = 0;
1286 src_image.handle = src_hnd;
1287
1288 // Copy the source.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001289 status = copy_image((private_handle_t *)src->handle, &src_image,
1290 CONVERT_TO_C2D_FORMAT);
1291 if (status == COPYBIT_FAILURE) {
1292 ALOGE("%s:copy_image failed in temp source",__FUNCTION__);
1293 delete_handle(dst_hnd);
1294 delete_handle(src_hnd);
1295 unmap_gpuaddr(ctx, mapped_dst_idx);
1296 return status;
1297 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001298
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001299 // Clean the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001300 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001301 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001302 src_hnd->offset, src_hnd->fd,
1303 gralloc::CACHE_CLEAN)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001304 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1305 delete_handle(dst_hnd);
1306 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001307 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001308 return COPYBIT_FAILURE;
1309 }
1310 }
1311
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001312 flags |= (ctx->is_premultiplied_alpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1313 flags |= (ctx->dst_surface_type != RGB_SURFACE) ? FLAGS_YUV_DESTINATION : 0;
1314 status = set_image(ctx, src_surface.surface_id, &src_image,
1315 (eC2DFlags)flags, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001316 if(status) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001317 ALOGE("%s: set_image (src) error", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001318 delete_handle(dst_hnd);
1319 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001320 unmap_gpuaddr(ctx, mapped_dst_idx);
1321 unmap_gpuaddr(ctx, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001322 return COPYBIT_FAILURE;
1323 }
1324
Arun Kumar K.Re29916b2013-03-20 11:42:53 -07001325 src_surface.config_mask = C2D_NO_ANTIALIASING_BIT | ctx->config_mask;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001326 src_surface.global_alpha = ctx->src_global_alpha;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001327 if (enableBlend) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001328 if(src_surface.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1329 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1330 if(!(src_surface.global_alpha)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001331 // src alpha is zero
Naseer Ahmed29a26812012-06-14 00:56:20 -07001332 delete_handle(dst_hnd);
1333 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001334 unmap_gpuaddr(ctx, mapped_dst_idx);
1335 unmap_gpuaddr(ctx, mapped_src_idx);
1336 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001337 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001338 }
1339 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001340 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001341 }
1342
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001343 if (src_surface_type == RGB_SURFACE) {
1344 ctx->blit_rgb_object[ctx->blit_rgb_count] = src_surface;
1345 ctx->blit_rgb_count++;
1346 } else if (src_surface_type == YUV_SURFACE_2_PLANES) {
1347 ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count] = src_surface;
1348 ctx->blit_yuv_2_plane_count++;
1349 } else {
1350 ctx->blit_yuv_3_plane_object[ctx->blit_yuv_3_plane_count] = src_surface;
1351 ctx->blit_yuv_3_plane_count++;
1352 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001353
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001354 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001355 while ((status == 0) && region->next(region, &clip)) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001356 set_rects(ctx, &(src_surface), dst_rect, src_rect, &clip);
1357 if (ctx->blit_count == MAX_BLIT_OBJECT_COUNT) {
1358 ALOGW("Reached end of blit count");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001359 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001360 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001361 ctx->blit_list[ctx->blit_count] = src_surface;
1362 ctx->blit_count++;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001363 }
1364
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001365 // Check if we need to perform an early draw-finish.
1366 flags |= (need_temp_dst || need_temp_src) ? FLAGS_TEMP_SRC_DST : 0;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301367 if (need_to_execute_draw((eC2DFlags)flags))
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001368 {
1369 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001370 }
1371
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001372 if (need_temp_dst) {
1373 // copy the temp. destination without the alignment to the actual
1374 // destination.
1375 status = copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1376 if (status == COPYBIT_FAILURE) {
1377 ALOGE("%s:copy_image failed in temp Dest",__FUNCTION__);
1378 delete_handle(dst_hnd);
1379 delete_handle(src_hnd);
1380 unmap_gpuaddr(ctx, mapped_dst_idx);
1381 unmap_gpuaddr(ctx, mapped_src_idx);
1382 return status;
1383 }
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001384 // Clean the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001385 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001386 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001387 dst_hnd->offset, dst_hnd->fd,
1388 gralloc::CACHE_CLEAN);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001389 }
1390 delete_handle(dst_hnd);
1391 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001392
1393 ctx->is_premultiplied_alpha = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001394 ctx->fb_width = 0;
1395 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001396 ctx->config_mask = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001397 return status;
1398}
1399
Terence Hampsonadf47302013-05-23 12:21:02 -04001400static int set_sync_copybit(struct copybit_device_t *dev,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301401 int /*acquireFenceFd*/)
Terence Hampsonadf47302013-05-23 12:21:02 -04001402{
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301403 if(!dev)
1404 return -EINVAL;
1405
Terence Hampsonadf47302013-05-23 12:21:02 -04001406 return 0;
1407}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001408
Naseer Ahmed29a26812012-06-14 00:56:20 -07001409static int stretch_copybit(
1410 struct copybit_device_t *dev,
1411 struct copybit_image_t const *dst,
1412 struct copybit_image_t const *src,
1413 struct copybit_rect_t const *dst_rect,
1414 struct copybit_rect_t const *src_rect,
1415 struct copybit_region_t const *region)
1416{
1417 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001418 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001419 bool needsBlending = (ctx->src_global_alpha != 0);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001420 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1421 status = stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001422 region, needsBlending);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001423 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1424 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001425}
1426
1427/** Perform a blit type operation */
1428static int blit_copybit(
1429 struct copybit_device_t *dev,
1430 struct copybit_image_t const *dst,
1431 struct copybit_image_t const *src,
1432 struct copybit_region_t const *region)
1433{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001434 int status = COPYBIT_SUCCESS;
1435 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Naseer Ahmedbb887bd2013-04-16 14:12:27 -04001436 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
1437 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001438 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1439 status = stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1440 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1441 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001442}
1443
Sushil Chauhan943797c2013-10-21 17:35:55 -07001444/** Fill the rect on dst with RGBA color **/
1445static int fill_color(struct copybit_device_t *dev,
1446 struct copybit_image_t const *dst,
1447 struct copybit_rect_t const *rect,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301448 uint32_t /*color*/)
Sushil Chauhan943797c2013-10-21 17:35:55 -07001449{
1450 // TODO: Implement once c2d driver supports color fill
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301451 if(!dev || !dst || !rect)
1452 return -EINVAL;
1453
Sushil Chauhan943797c2013-10-21 17:35:55 -07001454 return -EINVAL;
1455}
1456
Naseer Ahmed29a26812012-06-14 00:56:20 -07001457/*****************************************************************************/
1458
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001459static void clean_up(copybit_context_t* ctx)
1460{
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001461 void* ret;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001462 if (!ctx)
1463 return;
1464
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001465 // stop the wait_cleanup_thread
1466 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1467 ctx->stop_thread = true;
1468 // Signal waiting thread
1469 pthread_cond_signal(&ctx->wait_cleanup_cond);
1470 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1471 // waits for the cleanup thread to exit
1472 pthread_join(ctx->wait_thread_id, &ret);
1473 pthread_mutex_destroy(&ctx->wait_cleanup_lock);
1474 pthread_cond_destroy (&ctx->wait_cleanup_cond);
1475
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001476 for (int i = 0; i < NUM_SURFACE_TYPES; i++) {
1477 if (ctx->dst[i])
1478 LINK_c2dDestroySurface(ctx->dst[i]);
1479 }
1480
1481 for (int i = 0; i < MAX_RGB_SURFACES; i++) {
1482 if (ctx->blit_rgb_object[i].surface_id)
1483 LINK_c2dDestroySurface(ctx->blit_rgb_object[i].surface_id);
1484 }
1485
1486 for (int i = 0; i < MAX_YUV_2_PLANE_SURFACES; i++) {
1487 if (ctx->blit_yuv_2_plane_object[i].surface_id)
1488 LINK_c2dDestroySurface(ctx->blit_yuv_2_plane_object[i].surface_id);
1489 }
1490
1491 for (int i = 0; i < MAX_YUV_3_PLANE_SURFACES; i++) {
1492 if (ctx->blit_yuv_3_plane_object[i].surface_id)
1493 LINK_c2dDestroySurface(ctx->blit_yuv_3_plane_object[i].surface_id);
1494 }
1495
1496 if (ctx->libc2d2) {
1497 ::dlclose(ctx->libc2d2);
1498 ALOGV("dlclose(libc2d2)");
1499 }
1500
1501 free(ctx);
1502}
1503
Naseer Ahmed29a26812012-06-14 00:56:20 -07001504/** Close the copybit device */
1505static int close_copybit(struct hw_device_t *dev)
1506{
1507 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1508 if (ctx) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001509 free_temp_buffer(ctx->temp_src_buffer);
1510 free_temp_buffer(ctx->temp_dst_buffer);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001511 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001512 clean_up(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001513 return 0;
1514}
1515
1516/** Open a new instance of a copybit device using name */
1517static int open_copybit(const struct hw_module_t* module, const char* name,
1518 struct hw_device_t** device)
1519{
1520 int status = COPYBIT_SUCCESS;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301521 if (strcmp(name, COPYBIT_HARDWARE_COPYBIT0)) {
1522 return COPYBIT_FAILURE;
1523 }
1524
Naseer Ahmed29a26812012-06-14 00:56:20 -07001525 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1526 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1527 struct copybit_context_t *ctx;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001528
1529 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1530 if(!ctx) {
1531 ALOGE("%s: malloc failed", __FUNCTION__);
1532 return COPYBIT_FAILURE;
1533 }
1534
1535 /* initialize drawstate */
1536 memset(ctx, 0, sizeof(*ctx));
Naseer Ahmed29a26812012-06-14 00:56:20 -07001537 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1538 if (!ctx->libc2d2) {
1539 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001540 clean_up(ctx);
1541 status = COPYBIT_FAILURE;
1542 *device = NULL;
1543 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001544 }
1545 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1546 "c2dCreateSurface");
1547 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1548 "c2dUpdateSurface");
1549 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1550 "c2dReadSurface");
1551 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1552 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1553 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1554 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1555 "c2dWaitTimestamp");
1556 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1557 "c2dDestroySurface");
1558 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1559 "c2dMapAddr");
1560 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1561 "c2dUnMapAddr");
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001562 *(void **)&LINK_c2dGetDriverCapabilities = ::dlsym(ctx->libc2d2,
1563 "c2dGetDriverCapabilities");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001564 *(void **)&LINK_c2dCreateFenceFD = ::dlsym(ctx->libc2d2,
1565 "c2dCreateFenceFD");
Naseer Ahmed183939d2013-03-14 20:44:17 -04001566 *(void **)&LINK_c2dFillSurface = ::dlsym(ctx->libc2d2,
1567 "c2dFillSurface");
Naseer Ahmed29a26812012-06-14 00:56:20 -07001568
1569 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001570 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp ||
1571 !LINK_c2dFinish || !LINK_c2dDestroySurface ||
Naseer Ahmed183939d2013-03-14 20:44:17 -04001572 !LINK_c2dGetDriverCapabilities || !LINK_c2dCreateFenceFD ||
1573 !LINK_c2dFillSurface) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001574 ALOGE("%s: dlsym ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001575 clean_up(ctx);
1576 status = COPYBIT_FAILURE;
1577 *device = NULL;
1578 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001579 }
1580
1581 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1582 ctx->device.common.version = 1;
1583 ctx->device.common.module = (hw_module_t*)(module);
1584 ctx->device.common.close = close_copybit;
1585 ctx->device.set_parameter = set_parameter_copybit;
1586 ctx->device.get = get;
1587 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -04001588 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001589 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001590 ctx->device.finish = finish_copybit;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001591 ctx->device.flush_get_fence = flush_get_fence_copybit;
Naseer Ahmed183939d2013-03-14 20:44:17 -04001592 ctx->device.clear = clear_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -07001593 ctx->device.fill_color = fill_color;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001594
1595 /* Create RGB Surface */
1596 surfDefinition.buffer = (void*)0xdddddddd;
1597 surfDefinition.phys = (void*)0xdddddddd;
1598 surfDefinition.stride = 1 * 4;
1599 surfDefinition.width = 1;
1600 surfDefinition.height = 1;
1601 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1602 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1603 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001604 C2D_SURFACE_WITH_PHYS |
1605 C2D_SURFACE_WITH_PHYS_DUMMY ),
1606 &surfDefinition)) {
1607 ALOGE("%s: create ctx->dst_surface[RGB_SURFACE] failed", __FUNCTION__);
1608 ctx->dst[RGB_SURFACE] = 0;
1609 clean_up(ctx);
1610 status = COPYBIT_FAILURE;
1611 *device = NULL;
1612 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001613 }
1614
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001615 unsigned int surface_id = 0;
1616 for (int i = 0; i < MAX_RGB_SURFACES; i++)
1617 {
1618 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001619 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001620 C2D_SURFACE_WITH_PHYS |
1621 C2D_SURFACE_WITH_PHYS_DUMMY ),
1622 &surfDefinition)) {
1623 ALOGE("%s: create RGB source surface %d failed", __FUNCTION__, i);
1624 ctx->blit_rgb_object[i].surface_id = 0;
1625 status = COPYBIT_FAILURE;
1626 break;
1627 } else {
1628 ctx->blit_rgb_object[i].surface_id = surface_id;
1629 ALOGW("%s i = %d surface_id=%d", __FUNCTION__, i,
1630 ctx->blit_rgb_object[i].surface_id);
1631 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001632 }
1633
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001634 if (status == COPYBIT_FAILURE) {
1635 clean_up(ctx);
1636 status = COPYBIT_FAILURE;
1637 *device = NULL;
1638 return status;
1639 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001640
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001641 // Create 2 plane YUV surfaces
1642 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001643 yuvSurfaceDef.width = 4;
1644 yuvSurfaceDef.height = 4;
1645 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1646 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1647 yuvSurfaceDef.stride0 = 4;
1648
1649 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1650 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1651 yuvSurfaceDef.stride1 = 4;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001652 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1653 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001654 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1655 C2D_SURFACE_WITH_PHYS |
1656 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001657 &yuvSurfaceDef)) {
1658 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001659 ctx->dst[YUV_SURFACE_2_PLANES] = 0;
1660 clean_up(ctx);
1661 status = COPYBIT_FAILURE;
1662 *device = NULL;
1663 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001664 }
1665
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001666 for (int i=0; i < MAX_YUV_2_PLANE_SURFACES; i++)
1667 {
1668 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
1669 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1670 C2D_SURFACE_WITH_PHYS |
1671 C2D_SURFACE_WITH_PHYS_DUMMY ),
1672 &yuvSurfaceDef)) {
1673 ALOGE("%s: create YUV source %d failed", __FUNCTION__, i);
1674 ctx->blit_yuv_2_plane_object[i].surface_id = 0;
1675 status = COPYBIT_FAILURE;
1676 break;
1677 } else {
1678 ctx->blit_yuv_2_plane_object[i].surface_id = surface_id;
1679 ALOGW("%s: 2 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1680 ctx->blit_yuv_2_plane_object[i].surface_id);
1681 }
1682 }
1683
1684 if (status == COPYBIT_FAILURE) {
1685 clean_up(ctx);
1686 status = COPYBIT_FAILURE;
1687 *device = NULL;
1688 return status;
1689 }
1690
1691 // Create YUV 3 plane surfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -07001692 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1693 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1694 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1695 yuvSurfaceDef.stride2 = 4;
1696
Naseer Ahmed29a26812012-06-14 00:56:20 -07001697 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1698 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001699 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1700 C2D_SURFACE_WITH_PHYS |
1701 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001702 &yuvSurfaceDef)) {
1703 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001704 ctx->dst[YUV_SURFACE_3_PLANES] = 0;
1705 clean_up(ctx);
1706 status = COPYBIT_FAILURE;
1707 *device = NULL;
1708 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001709 }
1710
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001711 for (int i=0; i < MAX_YUV_3_PLANE_SURFACES; i++)
1712 {
1713 if (LINK_c2dCreateSurface(&(surface_id),
1714 C2D_TARGET | C2D_SOURCE,
1715 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1716 C2D_SURFACE_WITH_PHYS |
1717 C2D_SURFACE_WITH_PHYS_DUMMY),
1718 &yuvSurfaceDef)) {
1719 ALOGE("%s: create 3 plane YUV surface %d failed", __FUNCTION__, i);
1720 ctx->blit_yuv_3_plane_object[i].surface_id = 0;
1721 status = COPYBIT_FAILURE;
1722 break;
1723 } else {
1724 ctx->blit_yuv_3_plane_object[i].surface_id = surface_id;
1725 ALOGW("%s: 3 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1726 ctx->blit_yuv_3_plane_object[i].surface_id);
1727 }
1728 }
1729
1730 if (status == COPYBIT_FAILURE) {
1731 clean_up(ctx);
1732 status = COPYBIT_FAILURE;
1733 *device = NULL;
1734 return status;
1735 }
1736
1737 if (LINK_c2dGetDriverCapabilities(&(ctx->c2d_driver_info))) {
1738 ALOGE("%s: LINK_c2dGetDriverCapabilities failed", __FUNCTION__);
1739 clean_up(ctx);
1740 status = COPYBIT_FAILURE;
1741 *device = NULL;
1742 return status;
1743 }
1744 // Initialize context variables.
1745 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1746
Naseer Ahmed29a26812012-06-14 00:56:20 -07001747 ctx->temp_src_buffer.fd = -1;
1748 ctx->temp_src_buffer.base = 0;
1749 ctx->temp_src_buffer.size = 0;
1750
1751 ctx->temp_dst_buffer.fd = -1;
1752 ctx->temp_dst_buffer.base = 0;
1753 ctx->temp_dst_buffer.size = 0;
1754
1755 ctx->fb_width = 0;
1756 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001757
1758 ctx->blit_rgb_count = 0;
1759 ctx->blit_yuv_2_plane_count = 0;
1760 ctx->blit_yuv_3_plane_count = 0;
1761 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001762
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001763 ctx->wait_timestamp = false;
1764 ctx->stop_thread = false;
1765 pthread_mutex_init(&(ctx->wait_cleanup_lock), NULL);
1766 pthread_cond_init(&(ctx->wait_cleanup_cond), NULL);
1767 /* Start the wait thread */
1768 pthread_attr_t attr;
1769 pthread_attr_init(&attr);
1770 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1771
1772 pthread_create(&ctx->wait_thread_id, &attr, &c2d_wait_loop,
1773 (void *)ctx);
1774 pthread_attr_destroy(&attr);
1775
Naseer Ahmed29a26812012-06-14 00:56:20 -07001776 *device = &ctx->device.common;
1777 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001778}