blob: 9936eb6667bc1ebe5e61fa856660b79882b33c28 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Arun Kumar K.R6364fa62015-01-08 16:08:51 -08003 * Copyright (c) 2010-2015, 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
39#include <copybit.h>
40#include <alloc_controller.h>
41#include <memalloc.h>
42
43#include "c2d2.h"
44#include "software_converter.h"
45
46#include <dlfcn.h>
47
48using gralloc::IMemAlloc;
49using gralloc::IonController;
50using gralloc::alloc_data;
Naseer Ahmed29a26812012-06-14 00:56:20 -070051
52C2D_STATUS (*LINK_c2dCreateSurface)( uint32 *surface_id,
53 uint32 surface_bits,
54 C2D_SURFACE_TYPE surface_type,
55 void *surface_definition );
56
57C2D_STATUS (*LINK_c2dUpdateSurface)( uint32 surface_id,
58 uint32 surface_bits,
59 C2D_SURFACE_TYPE surface_type,
60 void *surface_definition );
61
62C2D_STATUS (*LINK_c2dReadSurface)( uint32 surface_id,
63 C2D_SURFACE_TYPE surface_type,
64 void *surface_definition,
65 int32 x, int32 y );
66
67C2D_STATUS (*LINK_c2dDraw)( uint32 target_id,
68 uint32 target_config, C2D_RECT *target_scissor,
69 uint32 target_mask_id, uint32 target_color_key,
70 C2D_OBJECT *objects_list, uint32 num_objects );
71
72C2D_STATUS (*LINK_c2dFinish)( uint32 target_id);
73
74C2D_STATUS (*LINK_c2dFlush)( uint32 target_id, c2d_ts_handle *timestamp);
75
76C2D_STATUS (*LINK_c2dWaitTimestamp)( c2d_ts_handle timestamp );
77
78C2D_STATUS (*LINK_c2dDestroySurface)( uint32 surface_id );
79
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +053080C2D_STATUS (*LINK_c2dMapAddr) ( int mem_fd, void * hostptr, size_t len,
81 size_t offset, uint32 flags, void ** gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -070082
83C2D_STATUS (*LINK_c2dUnMapAddr) ( void * gpuaddr);
84
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -080085C2D_STATUS (*LINK_c2dGetDriverCapabilities) ( C2D_DRIVER_INFO * driver_info);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -080086
87/* create a fence fd for the timestamp */
88C2D_STATUS (*LINK_c2dCreateFenceFD) ( uint32 target_id, c2d_ts_handle timestamp,
89 int32 *fd);
Naseer Ahmed183939d2013-03-14 20:44:17 -040090
91C2D_STATUS (*LINK_c2dFillSurface) ( uint32 surface_id, uint32 fill_color,
92 C2D_RECT * fill_rect);
93
Naseer Ahmed29a26812012-06-14 00:56:20 -070094/******************************************************************************/
95
96#if defined(COPYBIT_Z180)
97#define MAX_SCALE_FACTOR (4096)
98#define MAX_DIMENSION (4096)
99#else
100#error "Unsupported HW version"
101#endif
102
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800103// The following defines can be changed as required i.e. as we encounter
104// complex use cases.
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700105#define MAX_RGB_SURFACES 32 // Max. RGB layers currently supported per draw
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800106#define MAX_YUV_2_PLANE_SURFACES 4// Max. 2-plane YUV layers currently supported per draw
107#define MAX_YUV_3_PLANE_SURFACES 1// Max. 3-plane YUV layers currently supported per draw
108// +1 for the destination surface. We cannot have multiple destination surfaces.
109#define MAX_SURFACES (MAX_RGB_SURFACES + MAX_YUV_2_PLANE_SURFACES + MAX_YUV_3_PLANE_SURFACES + 1)
110#define NUM_SURFACE_TYPES 3 // RGB_SURFACE + YUV_SURFACE_2_PLANES + YUV_SURFACE_3_PLANES
111#define MAX_BLIT_OBJECT_COUNT 50 // Max. blit objects that can be passed per draw
Naseer Ahmed29a26812012-06-14 00:56:20 -0700112
113enum {
114 RGB_SURFACE,
115 YUV_SURFACE_2_PLANES,
116 YUV_SURFACE_3_PLANES
117};
118
119enum eConversionType {
120 CONVERT_TO_ANDROID_FORMAT,
121 CONVERT_TO_C2D_FORMAT
122};
123
124enum eC2DFlags {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800125 FLAGS_PREMULTIPLIED_ALPHA = 1<<0,
126 FLAGS_YUV_DESTINATION = 1<<1,
radhakrishnad7131e62015-10-13 12:32:30 +0530127 FLAGS_TEMP_SRC_DST = 1<<2,
128 FLAGS_UBWC_FORMAT_MODE = 1<<3
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
radhakrishnad7131e62015-10-13 12:32:30 +0530163 bool is_src_ubwc_format;
164 bool is_dst_ubwc_format;
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;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530185 size_t plane1_offset;
186 size_t plane2_offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700187};
188
189/**
190 * Common hardware methods
191 */
192
193static int open_copybit(const struct hw_module_t* module, const char* name,
194 struct hw_device_t** device);
195
196static struct hw_module_methods_t copybit_module_methods = {
197open: open_copybit
198};
199
200/*
201 * The COPYBIT Module
202 */
203struct copybit_module_t HAL_MODULE_INFO_SYM = {
204common: {
205tag: HARDWARE_MODULE_TAG,
206 version_major: 1,
207 version_minor: 0,
208 id: COPYBIT_HARDWARE_MODULE_ID,
209 name: "QCT COPYBIT C2D 2.0 Module",
210 author: "Qualcomm",
211 methods: &copybit_module_methods
212 }
213};
214
215
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800216/* thread function which waits on the timeStamp and cleans up the surfaces */
217static void* c2d_wait_loop(void* ptr) {
218 copybit_context_t* ctx = (copybit_context_t*)(ptr);
219 char thread_name[64] = "copybitWaitThr";
220 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
221 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
222
223 while(ctx->stop_thread == false) {
224 pthread_mutex_lock(&ctx->wait_cleanup_lock);
225 while(ctx->wait_timestamp == false && !ctx->stop_thread) {
226 pthread_cond_wait(&(ctx->wait_cleanup_cond),
227 &(ctx->wait_cleanup_lock));
228 }
229 if(ctx->wait_timestamp) {
230 if(LINK_c2dWaitTimestamp(ctx->time_stamp)) {
231 ALOGE("%s: LINK_c2dWaitTimeStamp ERROR!!", __FUNCTION__);
232 }
233 ctx->wait_timestamp = false;
234 // Unmap any mapped addresses.
235 for (int i = 0; i < MAX_SURFACES; i++) {
236 if (ctx->mapped_gpu_addr[i]) {
237 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
238 ctx->mapped_gpu_addr[i] = 0;
239 }
240 }
241 // Reset the counts after the draw.
242 ctx->blit_rgb_count = 0;
243 ctx->blit_yuv_2_plane_count = 0;
244 ctx->blit_yuv_3_plane_count = 0;
245 ctx->blit_count = 0;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700246 ctx->dst_surface_mapped = false;
247 ctx->dst_surface_base = 0;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800248 }
249 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
250 if(ctx->stop_thread)
251 break;
252 }
253 pthread_exit(NULL);
254 return NULL;
255}
256
257
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258/* convert COPYBIT_FORMAT to C2D format */
259static int get_format(int format) {
260 switch (format) {
261 case HAL_PIXEL_FORMAT_RGB_565: return C2D_COLOR_FORMAT_565_RGB;
Omprakash Dhyade3cc819e2014-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;
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700270 case HAL_PIXEL_FORMAT_RGBA_5551: return C2D_COLOR_FORMAT_5551_RGBA;
271 case HAL_PIXEL_FORMAT_RGBA_4444: return C2D_COLOR_FORMAT_4444_RGBA;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700272 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV12;
273 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV12;
274 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV21;
275 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: return C2D_COLOR_FORMAT_420_NV12 |
276 C2D_FORMAT_MACROTILED;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800277 default: ALOGE("%s: invalid format (0x%x",
278 __FUNCTION__, format);
279 return -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700280 }
281 return -EINVAL;
282}
283
284/* Get the C2D formats needed for conversion to YUV */
285static int get_c2d_format_for_yuv_destination(int halFormat) {
286 switch (halFormat) {
287 // We do not swap the RB when the target is YUV
288 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
289 C2D_FORMAT_DISABLE_ALPHA;
290 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800291 // The U and V need to be interchanged when the target is YUV
Naseer Ahmed29a26812012-06-14 00:56:20 -0700292 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV21;
293 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV21;
294 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV12;
295 default: return get_format(halFormat);
296 }
297 return -EINVAL;
298}
299
300/* ------------------------------------------------------------------- *//*!
301 * \internal
302 * \brief Get the bpp for a particular color format
303 * \param color format
304 * \return bits per pixel
305 *//* ------------------------------------------------------------------- */
306int c2diGetBpp(int32 colorformat)
307{
308
309 int c2dBpp = 0;
310
311 switch(colorformat&0xFF)
312 {
313 case C2D_COLOR_FORMAT_4444_RGBA:
314 case C2D_COLOR_FORMAT_4444_ARGB:
315 case C2D_COLOR_FORMAT_1555_ARGB:
316 case C2D_COLOR_FORMAT_565_RGB:
317 case C2D_COLOR_FORMAT_5551_RGBA:
318 c2dBpp = 16;
319 break;
320 case C2D_COLOR_FORMAT_8888_RGBA:
321 case C2D_COLOR_FORMAT_8888_ARGB:
322 c2dBpp = 32;
323 break;
Sushil Chauhan43755de2014-05-15 11:20:32 -0700324 case C2D_COLOR_FORMAT_888_RGB:
325 c2dBpp = 24;
326 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700327 case C2D_COLOR_FORMAT_8_L:
328 case C2D_COLOR_FORMAT_8_A:
329 c2dBpp = 8;
330 break;
331 case C2D_COLOR_FORMAT_4_A:
332 c2dBpp = 4;
333 break;
334 case C2D_COLOR_FORMAT_1:
335 c2dBpp = 1;
336 break;
337 default:
338 ALOGE("%s ERROR", __func__);
339 break;
340 }
341 return c2dBpp;
342}
343
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530344static size_t c2d_get_gpuaddr(copybit_context_t* ctx,
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700345 struct private_handle_t *handle, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346{
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530347 uint32 memtype;
348 size_t *gpuaddr = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700349 C2D_STATUS rc;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700350 int freeindex = 0;
351 bool mapaddr = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700352
353 if(!handle)
354 return 0;
355
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500356 if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700357 memtype = KGSL_USER_MEM_TYPE_ION;
358 else {
359 ALOGE("Invalid handle flags: 0x%x", handle->flags);
360 return 0;
361 }
362
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700363 // Check for a freeindex in the mapped_gpu_addr list
364 for (freeindex = 0; freeindex < MAX_SURFACES; freeindex++) {
365 if (ctx->mapped_gpu_addr[freeindex] == 0) {
366 // free index is available
367 // map GPU addr and use this as mapped_idx
368 mapaddr = true;
369 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800370 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700371 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700372
373 if(mapaddr) {
374 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size,
375 handle->offset, memtype, (void**)&gpuaddr);
376
377 if (rc == C2D_STATUS_OK) {
378 // We have mapped the GPU address inside copybit. We need to unmap
379 // this address after the blit. Store this address
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530380 ctx->mapped_gpu_addr[freeindex] = (size_t)gpuaddr;
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700381 mapped_idx = freeindex;
382 }
383 }
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530384 return (size_t)gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385}
386
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800387static void unmap_gpuaddr(copybit_context_t* ctx, int mapped_idx)
388{
389 if (!ctx || (mapped_idx == -1))
390 return;
391
392 if (ctx->mapped_gpu_addr[mapped_idx]) {
393 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[mapped_idx]);
394 ctx->mapped_gpu_addr[mapped_idx] = 0;
395 }
396}
397
Naseer Ahmed29a26812012-06-14 00:56:20 -0700398static int is_supported_rgb_format(int format)
399{
400 switch(format) {
401 case HAL_PIXEL_FORMAT_RGBA_8888:
402 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan43755de2014-05-15 11:20:32 -0700403 case HAL_PIXEL_FORMAT_RGB_888:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700404 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700405 case HAL_PIXEL_FORMAT_BGRA_8888:
406 case HAL_PIXEL_FORMAT_RGBA_5551:
407 case HAL_PIXEL_FORMAT_RGBA_4444: {
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;
Naseer Ahmed9eb5e092014-09-25 13:24:44 -0400507 uint64_t gpuaddr = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800508 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);
radhakrishnad7131e62015-10-13 12:32:30 +0530549
550 surfaceDef.format = surfaceDef.format |
551 ((flags & FLAGS_UBWC_FORMAT_MODE) ? C2D_FORMAT_UBWC_COMPRESSED : 0);
552
Naseer Ahmed29a26812012-06-14 00:56:20 -0700553 surfaceDef.width = rhs->w;
554 surfaceDef.height = rhs->h;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530555 int aligned_width = ALIGN((int)surfaceDef.width,32);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700556 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
557
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800558 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
559 &surfaceDef)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700560 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800561 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700562 status = COPYBIT_FAILURE;
563 }
564 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
565 C2D_YUV_SURFACE_DEF surfaceDef;
566 memset(&surfaceDef, 0, sizeof(surfaceDef));
567 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800568 surfaceDef.format = c2d_format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700569
570 bufferInfo info;
571 info.width = rhs->w;
572 info.height = rhs->h;
573 info.format = rhs->format;
574
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700575 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700576 status = calculate_yuv_offset_and_stride(info, yuvInfo);
577 if(status != COPYBIT_SUCCESS) {
578 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800579 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700580 }
581
582 surfaceDef.width = rhs->w;
583 surfaceDef.height = rhs->h;
584 surfaceDef.plane0 = (void*) (handle->base);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800585 surfaceDef.phys0 = (void*) (gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700586 surfaceDef.stride0 = yuvInfo.yStride;
587
588 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800589 surfaceDef.phys1 = (void*) (gpuaddr + yuvInfo.plane1_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700590 surfaceDef.stride1 = yuvInfo.plane1_stride;
591 if (3 == get_num_planes(rhs->format)) {
592 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800593 surfaceDef.phys2 = (void*) (gpuaddr + yuvInfo.plane2_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700594 surfaceDef.stride2 = yuvInfo.plane2_stride;
595 }
596
597 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
598 &surfaceDef)) {
599 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800600 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700601 status = COPYBIT_FAILURE;
602 }
603 } else {
604 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800605 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700606 status = COPYBIT_FAILURE;
607 }
608
609 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700610}
611
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800612/** copy the bits */
613static int msm_copybit(struct copybit_context_t *ctx, unsigned int target)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700614{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800615 if (ctx->blit_count == 0) {
616 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700617 }
618
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800619 for (int i = 0; i < ctx->blit_count; i++)
620 {
621 ctx->blit_list[i].next = &(ctx->blit_list[i+1]);
622 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800623 ctx->blit_list[ctx->blit_count-1].next = NULL;
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800624 uint32_t target_transform = ctx->trg_transform;
625 if (ctx->c2d_driver_info.capabilities_mask &
626 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
627 // For A3xx - set 0x0 as the transform is set in the config_mask
628 target_transform = 0x0;
629 }
630 if(LINK_c2dDraw(target, target_transform, 0x0, 0, 0, ctx->blit_list,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800631 ctx->blit_count)) {
632 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
633 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700634 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700635 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700636}
637
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800638
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800639
640static int flush_get_fence_copybit (struct copybit_device_t *dev, int* fd)
641{
642 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
643 int status = COPYBIT_FAILURE;
644 if (!ctx)
645 return COPYBIT_FAILURE;
646 pthread_mutex_lock(&ctx->wait_cleanup_lock);
647 status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
648
649 if(LINK_c2dFlush(ctx->dst[ctx->dst_surface_type], &ctx->time_stamp)) {
650 ALOGE("%s: LINK_c2dFlush ERROR", __FUNCTION__);
651 // unlock the mutex and return failure
652 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
653 return COPYBIT_FAILURE;
654 }
655 if(LINK_c2dCreateFenceFD(ctx->dst[ctx->dst_surface_type], ctx->time_stamp,
656 fd)) {
657 ALOGE("%s: LINK_c2dCreateFenceFD ERROR", __FUNCTION__);
658 status = COPYBIT_FAILURE;
659 }
660 if(status == COPYBIT_SUCCESS) {
661 //signal the wait_thread
662 ctx->wait_timestamp = true;
663 pthread_cond_signal(&ctx->wait_cleanup_cond);
664 }
665 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
666 return status;
667}
668
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800669static int finish_copybit(struct copybit_device_t *dev)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700670{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800671 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
672 if (!ctx)
673 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700674
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800675 int status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700676
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800677 if(LINK_c2dFinish(ctx->dst[ctx->dst_surface_type])) {
678 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
679 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700680 }
681
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800682 // Unmap any mapped addresses.
683 for (int i = 0; i < MAX_SURFACES; i++) {
684 if (ctx->mapped_gpu_addr[i]) {
685 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
686 ctx->mapped_gpu_addr[i] = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700687 }
688 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700689
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800690 // Reset the counts after the draw.
691 ctx->blit_rgb_count = 0;
692 ctx->blit_yuv_2_plane_count = 0;
693 ctx->blit_yuv_3_plane_count = 0;
694 ctx->blit_count = 0;
Pawan Kumar74d9ea92013-05-03 14:25:49 +0530695 ctx->dst_surface_mapped = false;
696 ctx->dst_surface_base = 0;
697
Naseer Ahmed29a26812012-06-14 00:56:20 -0700698 return status;
699}
700
Naseer Ahmed183939d2013-03-14 20:44:17 -0400701static int clear_copybit(struct copybit_device_t *dev,
702 struct copybit_image_t const *buf,
703 struct copybit_rect_t *rect)
704{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700705 int ret = COPYBIT_SUCCESS;
Naseer Ahmed183939d2013-03-14 20:44:17 -0400706 int flags = FLAGS_PREMULTIPLIED_ALPHA;
707 int mapped_dst_idx = -1;
708 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
radhakrishnad7131e62015-10-13 12:32:30 +0530709 if (ctx->is_dst_ubwc_format)
710 flags |= FLAGS_UBWC_FORMAT_MODE;
Naseer Ahmed183939d2013-03-14 20:44:17 -0400711 C2D_RECT c2drect = {rect->l, rect->t, rect->r - rect->l, rect->b - rect->t};
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700712 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Arun Kumar K.R14031eb2013-04-15 14:26:43 -0700713 if(!ctx->dst_surface_mapped) {
714 ret = set_image(ctx, ctx->dst[RGB_SURFACE], buf,
715 (eC2DFlags)flags, mapped_dst_idx);
716 if(ret) {
717 ALOGE("%s: set_image error", __FUNCTION__);
718 unmap_gpuaddr(ctx, mapped_dst_idx);
719 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
720 return COPYBIT_FAILURE;
721 }
722 //clear_copybit is the first call made by HWC for each composition
723 //with the dest surface, hence set dst_surface_mapped.
724 ctx->dst_surface_mapped = true;
725 ctx->dst_surface_base = buf->base;
726 ret = LINK_c2dFillSurface(ctx->dst[RGB_SURFACE], 0x0, &c2drect);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400727 }
Arun Kumar K.R71cfd812013-04-03 11:25:41 -0700728 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400729 return ret;
730}
731
732
Naseer Ahmed29a26812012-06-14 00:56:20 -0700733/** setup rectangles */
734static void set_rects(struct copybit_context_t *ctx,
735 C2D_OBJECT *c2dObject,
736 const struct copybit_rect_t *dst,
737 const struct copybit_rect_t *src,
738 const struct copybit_rect_t *scissor)
739{
740 // Set the target rect.
741 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
742 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
743 /* target rotation is 270 */
744 c2dObject->target_rect.x = (dst->t)<<16;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530745 c2dObject->target_rect.y = ctx->fb_width?
746 (ALIGN(ctx->fb_width,32)- dst->r):dst->r;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700747 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
748 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
749 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
750 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
751 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
752 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
753 c2dObject->target_rect.y = (dst->l)<<16;
754 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
755 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
756 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
757 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
758 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530759 c2dObject->target_rect.x = ctx->fb_width?
760 (ALIGN(ctx->fb_width,32) - dst->r):dst->r;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700761 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
762 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
763 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
764 } else {
765 c2dObject->target_rect.x = (dst->l)<<16;
766 c2dObject->target_rect.y = (dst->t)<<16;
767 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
768 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
769 }
770 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
771
772 // Set the source rect
773 c2dObject->source_rect.x = (src->l)<<16;
774 c2dObject->source_rect.y = (src->t)<<16;
775 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
776 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
777 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
778
779 // Set the scissor rect
780 c2dObject->scissor_rect.x = scissor->l;
781 c2dObject->scissor_rect.y = scissor->t;
782 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
783 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
784 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
785}
786
Naseer Ahmed29a26812012-06-14 00:56:20 -0700787/*****************************************************************************/
788
789/** Set a parameter to value */
790static int set_parameter_copybit(
791 struct copybit_device_t *dev,
792 int name,
793 int value)
794{
795 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800796 int status = COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700797 if (!ctx) {
798 ALOGE("%s: null context", __FUNCTION__);
799 return -EINVAL;
800 }
801
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800802 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700803 switch(name) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700804 case COPYBIT_PLANE_ALPHA:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800805 {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700806 if (value < 0) value = 0;
807 if (value >= 256) value = 255;
808
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800809 ctx->src_global_alpha = value;
810 if (value < 255)
811 ctx->config_mask |= C2D_GLOBAL_ALPHA_BIT;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700812 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800813 ctx->config_mask &= ~C2D_GLOBAL_ALPHA_BIT;
814 }
815 break;
816 case COPYBIT_BLEND_MODE:
817 {
818 if (value == COPYBIT_BLENDING_NONE) {
819 ctx->config_mask |= C2D_ALPHA_BLEND_NONE;
820 ctx->is_premultiplied_alpha = true;
821 } else if (value == COPYBIT_BLENDING_PREMULT) {
822 ctx->is_premultiplied_alpha = true;
823 } else {
824 ctx->config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700825 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800826 }
827 break;
828 case COPYBIT_TRANSFORM:
829 {
830 unsigned int transform = 0;
831 uint32 config_mask = 0;
832 config_mask |= C2D_OVERRIDE_GLOBAL_TARGET_ROTATE_CONFIG;
833 if((value & 0x7) == COPYBIT_TRANSFORM_ROT_180) {
834 transform = C2D_TARGET_ROTATE_180;
835 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_180;
836 } else if((value & 0x7) == COPYBIT_TRANSFORM_ROT_270) {
837 transform = C2D_TARGET_ROTATE_90;
838 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_90;
839 } else if(value == COPYBIT_TRANSFORM_ROT_90) {
840 transform = C2D_TARGET_ROTATE_270;
841 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_270;
842 } else {
843 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_0;
844 if(value & COPYBIT_TRANSFORM_FLIP_H) {
845 config_mask |= C2D_MIRROR_H_BIT;
846 } else if(value & COPYBIT_TRANSFORM_FLIP_V) {
847 config_mask |= C2D_MIRROR_V_BIT;
848 }
849 }
850
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800851 if (ctx->c2d_driver_info.capabilities_mask &
852 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
853 ctx->config_mask |= config_mask;
854 } else {
855 // The transform for this surface does not match the current
856 // target transform. Draw all previous surfaces. This will be
857 // changed once we have a new mechanism to send different
858 // target rotations to c2d.
859 finish_copybit(dev);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800860 }
861 ctx->trg_transform = transform;
862 }
863 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700864 case COPYBIT_FRAMEBUFFER_WIDTH:
865 ctx->fb_width = value;
866 break;
867 case COPYBIT_FRAMEBUFFER_HEIGHT:
868 ctx->fb_height = value;
869 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800870 case COPYBIT_ROTATION_DEG:
871 case COPYBIT_DITHER:
872 case COPYBIT_BLUR:
Naseer Ahmed45a99602012-07-31 19:15:24 -0700873 case COPYBIT_BLIT_TO_FRAMEBUFFER:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800874 // Do nothing
Naseer Ahmed45a99602012-07-31 19:15:24 -0700875 break;
radhakrishnad7131e62015-10-13 12:32:30 +0530876 case COPYBIT_SRC_FORMAT_MODE:
877 ctx->is_src_ubwc_format = (value == COPYBIT_UBWC_COMPRESSED);
878 break;
879 case COPYBIT_DST_FORMAT_MODE:
880 ctx->is_dst_ubwc_format = (value == COPYBIT_UBWC_COMPRESSED);
881 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700882 default:
883 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800884 status = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700885 break;
886 }
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800887 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
888 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700889}
890
891/** Get a static info value */
892static int get(struct copybit_device_t *dev, int name)
893{
894 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
895 int value;
896
897 if (!ctx) {
898 ALOGE("%s: null context error", __FUNCTION__);
899 return -EINVAL;
900 }
901
902 switch(name) {
903 case COPYBIT_MINIFICATION_LIMIT:
904 value = MAX_SCALE_FACTOR;
905 break;
906 case COPYBIT_MAGNIFICATION_LIMIT:
907 value = MAX_SCALE_FACTOR;
908 break;
909 case COPYBIT_SCALING_FRAC_BITS:
910 value = 32;
911 break;
912 case COPYBIT_ROTATION_STEP_DEG:
913 value = 1;
914 break;
radhakrishnad7131e62015-10-13 12:32:30 +0530915 case COPYBIT_UBWC_SUPPORT:
916 value = 0;
917 if (ctx->c2d_driver_info.capabilities_mask & C2D_DRIVER_SUPPORTS_UBWC_COMPRESSED_OP) {
918 value = 1;
919 }
920 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700921 default:
922 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
923 value = -EINVAL;
924 }
925 return value;
926}
927
928static int is_alpha(int cformat)
929{
930 int alpha = 0;
931 switch (cformat & 0xFF) {
932 case C2D_COLOR_FORMAT_8888_ARGB:
933 case C2D_COLOR_FORMAT_8888_RGBA:
934 case C2D_COLOR_FORMAT_5551_RGBA:
935 case C2D_COLOR_FORMAT_4444_ARGB:
936 alpha = 1;
937 break;
938 default:
939 alpha = 0;
940 break;
941 }
942
943 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
944 alpha = 0;
945
946 return alpha;
947}
948
949/* Function to check if we need a temporary buffer for the blit.
950 * This would happen if the requested destination stride and the
951 * C2D stride do not match. We ignore RGB buffers, since their
952 * stride is always aligned to 32.
953 */
954static bool need_temp_buffer(struct copybit_image_t const *img)
955{
956 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
957 return false;
958
959 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
960
961 // The width parameter in the handle contains the aligned_w. We check if we
962 // need to convert based on this param. YUV formats have bpp=1, so checking
963 // if the requested stride is aligned should suffice.
964 if (0 == (handle->width)%32) {
965 return false;
966 }
967
968 return true;
969}
970
971/* Function to extract the information from the copybit image and set the corresponding
972 * values in the bufferInfo struct.
973 */
974static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
975{
976 info.width = img->w;
977 info.height = img->h;
978 info.format = img->format;
979}
980
981/* Function to get the required size for a particular format, inorder for C2D to perform
982 * the blit operation.
983 */
Shalaj Jaina70b4352014-06-15 13:47:47 -0700984static int get_size(const bufferInfo& info)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700985{
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +0530986 int size = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700987 int w = info.width;
988 int h = info.height;
989 int aligned_w = ALIGN(w, 32);
990 switch(info.format) {
991 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
992 {
993 // Chroma for this format is aligned to 2K.
994 size = ALIGN((aligned_w*h), 2048) +
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800995 ALIGN(aligned_w/2, 32) * (h/2) *2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700996 size = ALIGN(size, 4096);
997 } break;
998 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
999 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1000 {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001001 size = aligned_w * h +
1002 ALIGN(aligned_w/2, 32) * (h/2) * 2;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001003 size = ALIGN(size, 4096);
1004 } break;
1005 default: break;
1006 }
1007 return size;
1008}
1009
1010/* Function to allocate memory for the temporary buffer. This memory is
1011 * allocated from Ashmem. It is the caller's responsibility to free this
1012 * memory.
1013 */
1014static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
1015{
1016 ALOGD("%s E", __FUNCTION__);
1017 // Alloc memory from system heap
1018 data.base = 0;
1019 data.fd = -1;
1020 data.offset = 0;
1021 data.size = get_size(info);
1022 data.align = getpagesize();
1023 data.uncached = true;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -05001024 int allocFlags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001025
1026 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001027 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -07001028 }
1029
1030 if (sAlloc == 0) {
1031 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
1032 return COPYBIT_FAILURE;
1033 }
1034
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001035 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001036 if (0 != err) {
1037 ALOGE("%s: allocate failed", __FUNCTION__);
1038 return COPYBIT_FAILURE;
1039 }
1040
1041 ALOGD("%s X", __FUNCTION__);
1042 return err;
1043}
1044
1045/* Function to free the temporary allocated memory.*/
1046static void free_temp_buffer(alloc_data &data)
1047{
1048 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001049 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001050 memalloc->free_buffer(data.base, data.size, 0, data.fd);
1051 }
1052}
1053
1054/* Function to perform the software color conversion. Convert the
1055 * C2D compatible format to the Android compatible format
1056 */
1057static int copy_image(private_handle_t *src_handle,
1058 struct copybit_image_t const *rhs,
1059 eConversionType conversionType)
1060{
1061 if (src_handle->fd == -1) {
1062 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
1063 return COPYBIT_FAILURE;
1064 }
1065
1066 // Copy the info.
1067 int ret = COPYBIT_SUCCESS;
1068 switch(rhs->format) {
1069 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1070 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1071 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1072 {
1073 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
1074 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
1075 } else {
1076 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
1077 }
1078
1079 } break;
1080 default: {
1081 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
1082 ret = COPYBIT_FAILURE;
1083 } break;
1084 }
1085 return ret;
1086}
1087
1088static void delete_handle(private_handle_t *handle)
1089{
1090 if (handle) {
1091 delete handle;
1092 handle = 0;
1093 }
1094}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001095
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301096static bool need_to_execute_draw(eC2DFlags flags)
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001097{
1098 if (flags & FLAGS_TEMP_SRC_DST) {
1099 return true;
1100 }
1101 if (flags & FLAGS_YUV_DESTINATION) {
1102 return true;
1103 }
1104 return false;
1105}
1106
Naseer Ahmed29a26812012-06-14 00:56:20 -07001107/** do a stretch blit type operation */
1108static int stretch_copybit_internal(
1109 struct copybit_device_t *dev,
1110 struct copybit_image_t const *dst,
1111 struct copybit_image_t const *src,
1112 struct copybit_rect_t const *dst_rect,
1113 struct copybit_rect_t const *src_rect,
1114 struct copybit_region_t const *region,
1115 bool enableBlend)
1116{
1117 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1118 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001119 int flags = 0;
1120 int src_surface_type;
1121 int mapped_src_idx = -1, mapped_dst_idx = -1;
1122 C2D_OBJECT_STR src_surface;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001123
1124 if (!ctx) {
1125 ALOGE("%s: null context error", __FUNCTION__);
1126 return -EINVAL;
1127 }
1128
1129 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1130 ALOGE("%s: src dimension error", __FUNCTION__);
1131 return -EINVAL;
1132 }
1133
1134 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001135 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w,
1136 dst->h);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001137 return -EINVAL;
1138 }
1139
Naseer Ahmed29a26812012-06-14 00:56:20 -07001140 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001141 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__,
1142 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001143 return COPYBIT_FAILURE;
1144 }
1145
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001146 int dst_surface_type;
radhakrishnad7131e62015-10-13 12:32:30 +05301147 if (ctx->is_dst_ubwc_format)
1148 flags |= FLAGS_UBWC_FORMAT_MODE;
1149
Naseer Ahmed29a26812012-06-14 00:56:20 -07001150 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001151 dst_surface_type = RGB_SURFACE;
1152 flags |= FLAGS_PREMULTIPLIED_ALPHA;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001153 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001154 int num_planes = get_num_planes(dst->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001155 flags |= FLAGS_YUV_DESTINATION;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001156 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001157 dst_surface_type = YUV_SURFACE_2_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001158 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001159 dst_surface_type = YUV_SURFACE_3_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001160 } else {
1161 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1162 __FUNCTION__, dst->format);
1163 return COPYBIT_FAILURE;
1164 }
1165 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001166 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__,
1167 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001168 return COPYBIT_FAILURE;
1169 }
1170
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001171 if (ctx->blit_rgb_count == MAX_RGB_SURFACES ||
1172 ctx->blit_yuv_2_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1173 ctx->blit_yuv_3_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1174 ctx->blit_count == MAX_BLIT_OBJECT_COUNT ||
1175 ctx->dst_surface_type != dst_surface_type) {
1176 // we have reached the max. limits of our internal structures or
1177 // changed the target.
1178 // Draw the remaining surfaces. We need to do the finish here since
1179 // we need to free up the surface templates.
1180 finish_copybit(dev);
1181 }
1182
1183 ctx->dst_surface_type = dst_surface_type;
1184
1185 // Update the destination
Naseer Ahmed29a26812012-06-14 00:56:20 -07001186 copybit_image_t dst_image;
1187 dst_image.w = dst->w;
1188 dst_image.h = dst->h;
1189 dst_image.format = dst->format;
1190 dst_image.handle = dst->handle;
1191 // Check if we need a temp. copy for the destination. We'd need this the destination
1192 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1193 // aligned to 32.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001194 bool need_temp_dst = need_temp_buffer(dst);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001195 bufferInfo dst_info;
1196 populate_buffer_info(dst, dst_info);
1197 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1198 dst_info.width, dst_info.height);
1199 if (dst_hnd == NULL) {
1200 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1201 return COPYBIT_FAILURE;
1202 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001203 if (need_temp_dst) {
Naseer Ahmed9eb5e092014-09-25 13:24:44 -04001204 if (get_size(dst_info) != (int) ctx->temp_dst_buffer.size) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001205 free_temp_buffer(ctx->temp_dst_buffer);
1206 // Create a temp buffer and set that as the destination.
1207 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1208 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1209 delete_handle(dst_hnd);
1210 return COPYBIT_FAILURE;
1211 }
1212 }
1213 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1214 dst_hnd->size = ctx->temp_dst_buffer.size;
1215 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301216 dst_hnd->base = (uintptr_t)(ctx->temp_dst_buffer.base);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001217 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1218 dst_hnd->gpuaddr = 0;
1219 dst_image.handle = dst_hnd;
1220 }
Arun Kumar K.R14031eb2013-04-15 14:26:43 -07001221 if(!ctx->dst_surface_mapped) {
1222 //map the destination surface to GPU address
1223 status = set_image(ctx, ctx->dst[ctx->dst_surface_type], &dst_image,
1224 (eC2DFlags)flags, mapped_dst_idx);
1225 if(status) {
1226 ALOGE("%s: dst: set_image error", __FUNCTION__);
1227 delete_handle(dst_hnd);
1228 unmap_gpuaddr(ctx, mapped_dst_idx);
1229 return COPYBIT_FAILURE;
1230 }
1231 ctx->dst_surface_mapped = true;
1232 ctx->dst_surface_base = dst->base;
1233 } else if(ctx->dst_surface_mapped && ctx->dst_surface_base != dst->base) {
1234 // Destination surface for the operation should be same for multiple
1235 // requests, this check is catch if there is any case when the
1236 // destination changes
1237 ALOGE("%s: a different destination surface!!", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001238 }
1239
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001240 // Update the source
1241 flags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001242 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001243 src_surface_type = RGB_SURFACE;
1244 src_surface = ctx->blit_rgb_object[ctx->blit_rgb_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001245 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1246 int num_planes = get_num_planes(src->format);
1247 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001248 src_surface_type = YUV_SURFACE_2_PLANES;
1249 src_surface = ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001250 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001251 src_surface_type = YUV_SURFACE_3_PLANES;
1252 src_surface = ctx->blit_yuv_3_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001253 } else {
1254 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1255 __FUNCTION__, src->format);
1256 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001257 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001258 return -EINVAL;
1259 }
1260 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001261 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__,
1262 src->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001263 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 -EINVAL;
1266 }
1267
1268 copybit_image_t src_image;
1269 src_image.w = src->w;
1270 src_image.h = src->h;
1271 src_image.format = src->format;
1272 src_image.handle = src->handle;
1273
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001274 bool need_temp_src = need_temp_buffer(src);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001275 bufferInfo src_info;
1276 populate_buffer_info(src, src_info);
1277 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 -08001278 src_info.width, src_info.height);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001279 if (NULL == src_hnd) {
1280 ALOGE("%s: src_hnd is null", __FUNCTION__);
1281 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001282 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001283 return COPYBIT_FAILURE;
1284 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001285 if (need_temp_src) {
Naseer Ahmed9eb5e092014-09-25 13:24:44 -04001286 if (get_size(src_info) != (int) ctx->temp_src_buffer.size) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001287 free_temp_buffer(ctx->temp_src_buffer);
1288 // Create a temp buffer and set that as the destination.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001289 if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
1290 ctx->temp_src_buffer)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001291 ALOGE("%s: get_temp_buffer(src) failed", __FUNCTION__);
1292 delete_handle(dst_hnd);
1293 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001294 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001295 return COPYBIT_FAILURE;
1296 }
1297 }
1298 src_hnd->fd = ctx->temp_src_buffer.fd;
1299 src_hnd->size = ctx->temp_src_buffer.size;
1300 src_hnd->flags = ctx->temp_src_buffer.allocType;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301301 src_hnd->base = (uintptr_t)(ctx->temp_src_buffer.base);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001302 src_hnd->offset = ctx->temp_src_buffer.offset;
1303 src_hnd->gpuaddr = 0;
1304 src_image.handle = src_hnd;
1305
1306 // Copy the source.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001307 status = copy_image((private_handle_t *)src->handle, &src_image,
1308 CONVERT_TO_C2D_FORMAT);
1309 if (status == COPYBIT_FAILURE) {
1310 ALOGE("%s:copy_image failed in temp source",__FUNCTION__);
1311 delete_handle(dst_hnd);
1312 delete_handle(src_hnd);
1313 unmap_gpuaddr(ctx, mapped_dst_idx);
1314 return status;
1315 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001316
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001317 // Clean the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001318 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001319 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001320 src_hnd->offset, src_hnd->fd,
1321 gralloc::CACHE_CLEAN)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001322 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1323 delete_handle(dst_hnd);
1324 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001325 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001326 return COPYBIT_FAILURE;
1327 }
1328 }
1329
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001330 flags |= (ctx->is_premultiplied_alpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1331 flags |= (ctx->dst_surface_type != RGB_SURFACE) ? FLAGS_YUV_DESTINATION : 0;
radhakrishnad7131e62015-10-13 12:32:30 +05301332 flags |= (ctx->is_src_ubwc_format) ? FLAGS_UBWC_FORMAT_MODE : 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001333 status = set_image(ctx, src_surface.surface_id, &src_image,
1334 (eC2DFlags)flags, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001335 if(status) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001336 ALOGE("%s: set_image (src) error", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001337 delete_handle(dst_hnd);
1338 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001339 unmap_gpuaddr(ctx, mapped_dst_idx);
1340 unmap_gpuaddr(ctx, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001341 return COPYBIT_FAILURE;
1342 }
1343
Arun Kumar K.Re29916b2013-03-20 11:42:53 -07001344 src_surface.config_mask = C2D_NO_ANTIALIASING_BIT | ctx->config_mask;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001345 src_surface.global_alpha = ctx->src_global_alpha;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001346 if (enableBlend) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001347 if(src_surface.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1348 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1349 if(!(src_surface.global_alpha)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001350 // src alpha is zero
Naseer Ahmed29a26812012-06-14 00:56:20 -07001351 delete_handle(dst_hnd);
1352 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001353 unmap_gpuaddr(ctx, mapped_dst_idx);
1354 unmap_gpuaddr(ctx, mapped_src_idx);
1355 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001356 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001357 }
1358 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001359 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001360 }
1361
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001362 if (src_surface_type == RGB_SURFACE) {
1363 ctx->blit_rgb_object[ctx->blit_rgb_count] = src_surface;
1364 ctx->blit_rgb_count++;
1365 } else if (src_surface_type == YUV_SURFACE_2_PLANES) {
1366 ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count] = src_surface;
1367 ctx->blit_yuv_2_plane_count++;
1368 } else {
1369 ctx->blit_yuv_3_plane_object[ctx->blit_yuv_3_plane_count] = src_surface;
1370 ctx->blit_yuv_3_plane_count++;
1371 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001372
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001373 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001374 while ((status == 0) && region->next(region, &clip)) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001375 set_rects(ctx, &(src_surface), dst_rect, src_rect, &clip);
1376 if (ctx->blit_count == MAX_BLIT_OBJECT_COUNT) {
1377 ALOGW("Reached end of blit count");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001378 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001379 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001380 ctx->blit_list[ctx->blit_count] = src_surface;
1381 ctx->blit_count++;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001382 }
1383
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001384 // Check if we need to perform an early draw-finish.
1385 flags |= (need_temp_dst || need_temp_src) ? FLAGS_TEMP_SRC_DST : 0;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301386 if (need_to_execute_draw((eC2DFlags)flags))
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001387 {
1388 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001389 }
1390
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001391 if (need_temp_dst) {
1392 // copy the temp. destination without the alignment to the actual
1393 // destination.
1394 status = copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1395 if (status == COPYBIT_FAILURE) {
1396 ALOGE("%s:copy_image failed in temp Dest",__FUNCTION__);
1397 delete_handle(dst_hnd);
1398 delete_handle(src_hnd);
1399 unmap_gpuaddr(ctx, mapped_dst_idx);
1400 unmap_gpuaddr(ctx, mapped_src_idx);
1401 return status;
1402 }
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001403 // Clean the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001404 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001405 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
Naseer Ahmedaeab91f2013-03-20 21:01:19 -04001406 dst_hnd->offset, dst_hnd->fd,
1407 gralloc::CACHE_CLEAN);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001408 }
1409 delete_handle(dst_hnd);
1410 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001411
1412 ctx->is_premultiplied_alpha = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001413 ctx->fb_width = 0;
1414 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001415 ctx->config_mask = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001416 return status;
1417}
1418
Terence Hampsonadf47302013-05-23 12:21:02 -04001419static int set_sync_copybit(struct copybit_device_t *dev,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301420 int /*acquireFenceFd*/)
Terence Hampsonadf47302013-05-23 12:21:02 -04001421{
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301422 if(!dev)
1423 return -EINVAL;
1424
Terence Hampsonadf47302013-05-23 12:21:02 -04001425 return 0;
1426}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001427
Naseer Ahmed29a26812012-06-14 00:56:20 -07001428static int stretch_copybit(
1429 struct copybit_device_t *dev,
1430 struct copybit_image_t const *dst,
1431 struct copybit_image_t const *src,
1432 struct copybit_rect_t const *dst_rect,
1433 struct copybit_rect_t const *src_rect,
1434 struct copybit_region_t const *region)
1435{
1436 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001437 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001438 bool needsBlending = (ctx->src_global_alpha != 0);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001439 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1440 status = stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001441 region, needsBlending);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001442 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1443 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001444}
1445
1446/** Perform a blit type operation */
1447static int blit_copybit(
1448 struct copybit_device_t *dev,
1449 struct copybit_image_t const *dst,
1450 struct copybit_image_t const *src,
1451 struct copybit_region_t const *region)
1452{
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001453 int status = COPYBIT_SUCCESS;
1454 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Naseer Ahmedbb887bd2013-04-16 14:12:27 -04001455 struct copybit_rect_t dr = { 0, 0, (int)dst->w, (int)dst->h };
1456 struct copybit_rect_t sr = { 0, 0, (int)src->w, (int)src->h };
Arun Kumar K.R71cfd812013-04-03 11:25:41 -07001457 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1458 status = stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1459 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1460 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001461}
1462
Sushil Chauhan943797c2013-10-21 17:35:55 -07001463/** Fill the rect on dst with RGBA color **/
1464static int fill_color(struct copybit_device_t *dev,
1465 struct copybit_image_t const *dst,
1466 struct copybit_rect_t const *rect,
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301467 uint32_t /*color*/)
Sushil Chauhan943797c2013-10-21 17:35:55 -07001468{
1469 // TODO: Implement once c2d driver supports color fill
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301470 if(!dev || !dst || !rect)
1471 return -EINVAL;
1472
Sushil Chauhan943797c2013-10-21 17:35:55 -07001473 return -EINVAL;
1474}
1475
Naseer Ahmed29a26812012-06-14 00:56:20 -07001476/*****************************************************************************/
1477
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001478static void clean_up(copybit_context_t* ctx)
1479{
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001480 void* ret;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001481 if (!ctx)
1482 return;
1483
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001484 // stop the wait_cleanup_thread
1485 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1486 ctx->stop_thread = true;
1487 // Signal waiting thread
1488 pthread_cond_signal(&ctx->wait_cleanup_cond);
1489 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1490 // waits for the cleanup thread to exit
1491 pthread_join(ctx->wait_thread_id, &ret);
1492 pthread_mutex_destroy(&ctx->wait_cleanup_lock);
1493 pthread_cond_destroy (&ctx->wait_cleanup_cond);
1494
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001495 for (int i = 0; i < NUM_SURFACE_TYPES; i++) {
1496 if (ctx->dst[i])
1497 LINK_c2dDestroySurface(ctx->dst[i]);
1498 }
1499
1500 for (int i = 0; i < MAX_RGB_SURFACES; i++) {
1501 if (ctx->blit_rgb_object[i].surface_id)
1502 LINK_c2dDestroySurface(ctx->blit_rgb_object[i].surface_id);
1503 }
1504
1505 for (int i = 0; i < MAX_YUV_2_PLANE_SURFACES; i++) {
1506 if (ctx->blit_yuv_2_plane_object[i].surface_id)
1507 LINK_c2dDestroySurface(ctx->blit_yuv_2_plane_object[i].surface_id);
1508 }
1509
1510 for (int i = 0; i < MAX_YUV_3_PLANE_SURFACES; i++) {
1511 if (ctx->blit_yuv_3_plane_object[i].surface_id)
1512 LINK_c2dDestroySurface(ctx->blit_yuv_3_plane_object[i].surface_id);
1513 }
1514
1515 if (ctx->libc2d2) {
1516 ::dlclose(ctx->libc2d2);
1517 ALOGV("dlclose(libc2d2)");
1518 }
1519
1520 free(ctx);
1521}
1522
Naseer Ahmed29a26812012-06-14 00:56:20 -07001523/** Close the copybit device */
1524static int close_copybit(struct hw_device_t *dev)
1525{
1526 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1527 if (ctx) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001528 free_temp_buffer(ctx->temp_src_buffer);
1529 free_temp_buffer(ctx->temp_dst_buffer);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001530 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001531 clean_up(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001532 return 0;
1533}
1534
1535/** Open a new instance of a copybit device using name */
1536static int open_copybit(const struct hw_module_t* module, const char* name,
1537 struct hw_device_t** device)
1538{
1539 int status = COPYBIT_SUCCESS;
Praveena Pachipulusuaef031c2014-02-12 11:46:39 +05301540 if (strcmp(name, COPYBIT_HARDWARE_COPYBIT0)) {
1541 return COPYBIT_FAILURE;
1542 }
1543
Naseer Ahmed29a26812012-06-14 00:56:20 -07001544 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1545 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1546 struct copybit_context_t *ctx;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001547
1548 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1549 if(!ctx) {
1550 ALOGE("%s: malloc failed", __FUNCTION__);
1551 return COPYBIT_FAILURE;
1552 }
1553
1554 /* initialize drawstate */
1555 memset(ctx, 0, sizeof(*ctx));
Naseer Ahmed29a26812012-06-14 00:56:20 -07001556 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1557 if (!ctx->libc2d2) {
1558 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001559 clean_up(ctx);
1560 status = COPYBIT_FAILURE;
1561 *device = NULL;
1562 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001563 }
1564 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1565 "c2dCreateSurface");
1566 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1567 "c2dUpdateSurface");
1568 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1569 "c2dReadSurface");
1570 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1571 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1572 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1573 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1574 "c2dWaitTimestamp");
1575 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1576 "c2dDestroySurface");
1577 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1578 "c2dMapAddr");
1579 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1580 "c2dUnMapAddr");
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001581 *(void **)&LINK_c2dGetDriverCapabilities = ::dlsym(ctx->libc2d2,
1582 "c2dGetDriverCapabilities");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001583 *(void **)&LINK_c2dCreateFenceFD = ::dlsym(ctx->libc2d2,
1584 "c2dCreateFenceFD");
Naseer Ahmed183939d2013-03-14 20:44:17 -04001585 *(void **)&LINK_c2dFillSurface = ::dlsym(ctx->libc2d2,
1586 "c2dFillSurface");
Naseer Ahmed29a26812012-06-14 00:56:20 -07001587
1588 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001589 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp ||
1590 !LINK_c2dFinish || !LINK_c2dDestroySurface ||
Naseer Ahmed183939d2013-03-14 20:44:17 -04001591 !LINK_c2dGetDriverCapabilities || !LINK_c2dCreateFenceFD ||
1592 !LINK_c2dFillSurface) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001593 ALOGE("%s: dlsym ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001594 clean_up(ctx);
1595 status = COPYBIT_FAILURE;
1596 *device = NULL;
1597 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001598 }
1599
1600 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1601 ctx->device.common.version = 1;
1602 ctx->device.common.module = (hw_module_t*)(module);
1603 ctx->device.common.close = close_copybit;
1604 ctx->device.set_parameter = set_parameter_copybit;
1605 ctx->device.get = get;
1606 ctx->device.blit = blit_copybit;
Terence Hampsonadf47302013-05-23 12:21:02 -04001607 ctx->device.set_sync = set_sync_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001608 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001609 ctx->device.finish = finish_copybit;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001610 ctx->device.flush_get_fence = flush_get_fence_copybit;
Naseer Ahmed183939d2013-03-14 20:44:17 -04001611 ctx->device.clear = clear_copybit;
Sushil Chauhan943797c2013-10-21 17:35:55 -07001612 ctx->device.fill_color = fill_color;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001613
1614 /* Create RGB Surface */
1615 surfDefinition.buffer = (void*)0xdddddddd;
1616 surfDefinition.phys = (void*)0xdddddddd;
1617 surfDefinition.stride = 1 * 4;
1618 surfDefinition.width = 1;
1619 surfDefinition.height = 1;
1620 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1621 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1622 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001623 C2D_SURFACE_WITH_PHYS |
1624 C2D_SURFACE_WITH_PHYS_DUMMY ),
1625 &surfDefinition)) {
1626 ALOGE("%s: create ctx->dst_surface[RGB_SURFACE] failed", __FUNCTION__);
1627 ctx->dst[RGB_SURFACE] = 0;
1628 clean_up(ctx);
1629 status = COPYBIT_FAILURE;
1630 *device = NULL;
1631 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001632 }
1633
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001634 unsigned int surface_id = 0;
1635 for (int i = 0; i < MAX_RGB_SURFACES; i++)
1636 {
1637 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001638 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001639 C2D_SURFACE_WITH_PHYS |
1640 C2D_SURFACE_WITH_PHYS_DUMMY ),
1641 &surfDefinition)) {
1642 ALOGE("%s: create RGB source surface %d failed", __FUNCTION__, i);
1643 ctx->blit_rgb_object[i].surface_id = 0;
1644 status = COPYBIT_FAILURE;
1645 break;
1646 } else {
1647 ctx->blit_rgb_object[i].surface_id = surface_id;
1648 ALOGW("%s i = %d surface_id=%d", __FUNCTION__, i,
1649 ctx->blit_rgb_object[i].surface_id);
1650 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001651 }
1652
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001653 if (status == COPYBIT_FAILURE) {
1654 clean_up(ctx);
1655 status = COPYBIT_FAILURE;
1656 *device = NULL;
1657 return status;
1658 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001659
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001660 // Create 2 plane YUV surfaces
1661 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001662 yuvSurfaceDef.width = 4;
1663 yuvSurfaceDef.height = 4;
1664 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1665 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1666 yuvSurfaceDef.stride0 = 4;
1667
1668 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1669 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1670 yuvSurfaceDef.stride1 = 4;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001671 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1672 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001673 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1674 C2D_SURFACE_WITH_PHYS |
1675 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001676 &yuvSurfaceDef)) {
1677 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001678 ctx->dst[YUV_SURFACE_2_PLANES] = 0;
1679 clean_up(ctx);
1680 status = COPYBIT_FAILURE;
1681 *device = NULL;
1682 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001683 }
1684
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001685 for (int i=0; i < MAX_YUV_2_PLANE_SURFACES; i++)
1686 {
1687 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
1688 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1689 C2D_SURFACE_WITH_PHYS |
1690 C2D_SURFACE_WITH_PHYS_DUMMY ),
1691 &yuvSurfaceDef)) {
1692 ALOGE("%s: create YUV source %d failed", __FUNCTION__, i);
1693 ctx->blit_yuv_2_plane_object[i].surface_id = 0;
1694 status = COPYBIT_FAILURE;
1695 break;
1696 } else {
1697 ctx->blit_yuv_2_plane_object[i].surface_id = surface_id;
1698 ALOGW("%s: 2 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1699 ctx->blit_yuv_2_plane_object[i].surface_id);
1700 }
1701 }
1702
1703 if (status == COPYBIT_FAILURE) {
1704 clean_up(ctx);
1705 status = COPYBIT_FAILURE;
1706 *device = NULL;
1707 return status;
1708 }
1709
1710 // Create YUV 3 plane surfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -07001711 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1712 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1713 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1714 yuvSurfaceDef.stride2 = 4;
1715
Naseer Ahmed29a26812012-06-14 00:56:20 -07001716 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1717 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001718 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1719 C2D_SURFACE_WITH_PHYS |
1720 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001721 &yuvSurfaceDef)) {
1722 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001723 ctx->dst[YUV_SURFACE_3_PLANES] = 0;
1724 clean_up(ctx);
1725 status = COPYBIT_FAILURE;
1726 *device = NULL;
1727 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001728 }
1729
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001730 for (int i=0; i < MAX_YUV_3_PLANE_SURFACES; i++)
1731 {
1732 if (LINK_c2dCreateSurface(&(surface_id),
1733 C2D_TARGET | C2D_SOURCE,
1734 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1735 C2D_SURFACE_WITH_PHYS |
1736 C2D_SURFACE_WITH_PHYS_DUMMY),
1737 &yuvSurfaceDef)) {
1738 ALOGE("%s: create 3 plane YUV surface %d failed", __FUNCTION__, i);
1739 ctx->blit_yuv_3_plane_object[i].surface_id = 0;
1740 status = COPYBIT_FAILURE;
1741 break;
1742 } else {
1743 ctx->blit_yuv_3_plane_object[i].surface_id = surface_id;
1744 ALOGW("%s: 3 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1745 ctx->blit_yuv_3_plane_object[i].surface_id);
1746 }
1747 }
1748
1749 if (status == COPYBIT_FAILURE) {
1750 clean_up(ctx);
1751 status = COPYBIT_FAILURE;
1752 *device = NULL;
1753 return status;
1754 }
1755
1756 if (LINK_c2dGetDriverCapabilities(&(ctx->c2d_driver_info))) {
1757 ALOGE("%s: LINK_c2dGetDriverCapabilities failed", __FUNCTION__);
1758 clean_up(ctx);
1759 status = COPYBIT_FAILURE;
1760 *device = NULL;
1761 return status;
1762 }
1763 // Initialize context variables.
1764 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1765
Naseer Ahmed29a26812012-06-14 00:56:20 -07001766 ctx->temp_src_buffer.fd = -1;
1767 ctx->temp_src_buffer.base = 0;
1768 ctx->temp_src_buffer.size = 0;
1769
1770 ctx->temp_dst_buffer.fd = -1;
1771 ctx->temp_dst_buffer.base = 0;
1772 ctx->temp_dst_buffer.size = 0;
1773
1774 ctx->fb_width = 0;
1775 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001776
1777 ctx->blit_rgb_count = 0;
1778 ctx->blit_yuv_2_plane_count = 0;
1779 ctx->blit_yuv_3_plane_count = 0;
1780 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001781
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001782 ctx->wait_timestamp = false;
1783 ctx->stop_thread = false;
1784 pthread_mutex_init(&(ctx->wait_cleanup_lock), NULL);
1785 pthread_cond_init(&(ctx->wait_cleanup_cond), NULL);
1786 /* Start the wait thread */
1787 pthread_attr_t attr;
1788 pthread_attr_init(&attr);
1789 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1790
1791 pthread_create(&ctx->wait_thread_id, &attr, &c2d_wait_loop,
1792 (void *)ctx);
1793 pthread_attr_destroy(&attr);
1794
Naseer Ahmed29a26812012-06-14 00:56:20 -07001795 *device = &ctx->device.common;
1796 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001797}