1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <string.h>
17 #include <pthread.h>
18 #include <limits.h>
19 #include <cutils/ashmem.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <dlfcn.h>
23 #include <sys/mman.h>
24 #include "gralloc_cb.h"
25 #include "HostConnection.h"
26 #include "ProcessPipe.h"
27 #include "glUtils.h"
28 #include <cutils/log.h>
29 #include <cutils/properties.h>
30
31 /* Set to 1 or 2 to enable debug traces */
32 #define DEBUG 0
33
34 #if DEBUG >= 1
35 # define D(...) ALOGD(__VA_ARGS__)
36 #else
37 # define D(...) ((void)0)
38 #endif
39
40 #if DEBUG >= 2
41 # define DD(...) ALOGD(__VA_ARGS__)
42 #else
43 # define DD(...) ((void)0)
44 #endif
45
46 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
47
48 //
49 // our private gralloc module structure
50 //
51 struct private_module_t {
52 gralloc_module_t base;
53 };
54
55 /* If not NULL, this is a pointer to the fallback module.
56 * This really is gralloc.default, which we'll use if we detect
57 * that the emulator we're running in does not support GPU emulation.
58 */
59 static gralloc_module_t* sFallback;
60 static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT;
61
62 static void fallback_init(void); // forward
63
64
65 typedef struct _alloc_list_node {
66 buffer_handle_t handle;
67 _alloc_list_node *next;
68 _alloc_list_node *prev;
69 } AllocListNode;
70
71 //
72 // Our gralloc device structure (alloc interface)
73 //
74 struct gralloc_device_t {
75 alloc_device_t device;
76
77 AllocListNode *allocListHead; // double linked list of allocated buffers
78 pthread_mutex_t lock;
79 };
80
81 //
82 // Our framebuffer device structure
83 //
84 struct fb_device_t {
85 framebuffer_device_t device;
86 };
87
map_buffer(cb_handle_t * cb,void ** vaddr)88 static int map_buffer(cb_handle_t *cb, void **vaddr)
89 {
90 if (cb->fd < 0 || cb->ashmemSize <= 0) {
91 return -EINVAL;
92 }
93
94 void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
95 MAP_SHARED, cb->fd, 0);
96 if (addr == MAP_FAILED) {
97 return -errno;
98 }
99
100 cb->ashmemBase = intptr_t(addr);
101 cb->ashmemBasePid = getpid();
102
103 *vaddr = addr;
104 return 0;
105 }
106
107 #define DEFINE_HOST_CONNECTION \
108 HostConnection *hostCon = HostConnection::get(); \
109 renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
110
111 #define EXIT_GRALLOCONLY_HOST_CONNECTION \
112 HostConnection *hostCon = HostConnection::get(); \
113 if (hostCon && hostCon->isGrallocOnly()) { \
114 ALOGD("%s: exiting HostConnection (is buffer-handling thread)", \
115 __FUNCTION__); \
116 HostConnection::exit(); \
117 }
118
119 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
120 HostConnection *hostCon = HostConnection::get(); \
121 if (!hostCon) { \
122 ALOGE("gralloc: Failed to get host connection\n"); \
123 return -EIO; \
124 } \
125 renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \
126 if (!rcEnc) { \
127 ALOGE("gralloc: Failed to get renderControl encoder context\n"); \
128 return -EIO; \
129 }
130
131 #if PLATFORM_SDK_VERSION < 18
132 // On older APIs, just define it as a value no one is going to use.
133 #define HAL_PIXEL_FORMAT_YCbCr_420_888 0xFFFFFFFF
134 #endif
135
136 //
137 // gralloc device functions (alloc interface)
138 //
gralloc_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride)139 static int gralloc_alloc(alloc_device_t* dev,
140 int w, int h, int format, int usage,
141 buffer_handle_t* pHandle, int* pStride)
142 {
143 D("gralloc_alloc w=%d h=%d usage=0x%x format=0x%x\n", w, h, usage, format);
144
145 gralloc_device_t *grdev = (gralloc_device_t *)dev;
146 if (!grdev || !pHandle || !pStride) {
147 ALOGE("gralloc_alloc: Bad inputs (grdev: %p, pHandle: %p, pStride: %p",
148 grdev, pHandle, pStride);
149 return -EINVAL;
150 }
151
152 //
153 // Note: in screen capture mode, both sw_write and hw_write will be on
154 // and this is a valid usage
155 //
156 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
157 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
158 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
159 #if PLATFORM_SDK_VERSION >= 17
160 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
161 bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
162 #else // PLATFORM_SDK_VERSION
163 bool hw_cam_write = false;
164 bool hw_cam_read = false;
165 #endif // PLATFORM_SDK_VERSION
166 #if PLATFORM_SDK_VERSION >= 15
167 bool hw_vid_enc_read = usage & GRALLOC_USAGE_HW_VIDEO_ENCODER;
168 #else // PLATFORM_SDK_VERSION
169 bool hw_vid_enc_read = false;
170 #endif // PLATFORM_SDK_VERSION
171
172 // Keep around original requested format for later validation
173 int frameworkFormat = format;
174 // Pick the right concrete pixel format given the endpoints as encoded in
175 // the usage bits. Every end-point pair needs explicit listing here.
176 #if PLATFORM_SDK_VERSION >= 17
177 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
178 // Camera as producer
179 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
180 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
181 // Camera-to-display is RGBA
182 format = HAL_PIXEL_FORMAT_RGBA_8888;
183 } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
184 // Camera-to-encoder is NV21
185 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
186 } else if ((usage & GRALLOC_USAGE_HW_CAMERA_MASK) ==
187 GRALLOC_USAGE_HW_CAMERA_ZSL) {
188 // Camera-to-ZSL-queue is RGB_888
189 format = HAL_PIXEL_FORMAT_RGB_888;
190 }
191 }
192
193 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
194 ALOGE("gralloc_alloc: Requested auto format selection, "
195 "but no known format for this usage: %d x %d, usage %x",
196 w, h, usage);
197 return -EINVAL;
198 }
199 }
200 else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
201 ALOGW("gralloc_alloc: Requested YCbCr_420_888, taking experimental path. "
202 "usage: %d x %d, usage %x",
203 w, h, usage);
204 }
205 #endif // PLATFORM_SDK_VERSION >= 17
206 bool yuv_format = false;
207
208 int ashmem_size = 0;
209 int stride = w;
210
211 GLenum glFormat = 0;
212 GLenum glType = 0;
213
214 int bpp = 0;
215 int align = 1;
216 switch (format) {
217 case HAL_PIXEL_FORMAT_RGBA_8888:
218 case HAL_PIXEL_FORMAT_RGBX_8888:
219 case HAL_PIXEL_FORMAT_BGRA_8888:
220 bpp = 4;
221 glFormat = GL_RGBA;
222 glType = GL_UNSIGNED_BYTE;
223 break;
224 case HAL_PIXEL_FORMAT_RGB_888:
225 bpp = 3;
226 glFormat = GL_RGB;
227 glType = GL_UNSIGNED_BYTE;
228 break;
229 case HAL_PIXEL_FORMAT_RGB_565:
230 bpp = 2;
231 glFormat = GL_RGB;
232 glType = GL_UNSIGNED_SHORT_5_6_5;
233 break;
234 #if PLATFORM_SDK_VERSION >= 21
235 case HAL_PIXEL_FORMAT_RAW16:
236 case HAL_PIXEL_FORMAT_Y16:
237 #elif PLATFORM_SDK_VERSION >= 16
238 case HAL_PIXEL_FORMAT_RAW_SENSOR:
239 #endif
240 bpp = 2;
241 align = 16*bpp;
242 if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) {
243 // Raw sensor data or Y16 only goes between camera and CPU
244 return -EINVAL;
245 }
246 // Not expecting to actually create any GL surfaces for this
247 glFormat = GL_LUMINANCE;
248 glType = GL_UNSIGNED_SHORT;
249 break;
250 #if PLATFORM_SDK_VERSION >= 17
251 case HAL_PIXEL_FORMAT_BLOB:
252 bpp = 1;
253 if (! (sw_read && hw_cam_write) ) {
254 // Blob data cannot be used by HW other than camera emulator
255 return -EINVAL;
256 }
257 // Not expecting to actually create any GL surfaces for this
258 glFormat = GL_LUMINANCE;
259 glType = GL_UNSIGNED_BYTE;
260 break;
261 #endif // PLATFORM_SDK_VERSION >= 17
262 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
263 align = 1;
264 bpp = 1; // per-channel bpp
265 yuv_format = true;
266 // Not expecting to actually create any GL surfaces for this
267 break;
268 case HAL_PIXEL_FORMAT_YV12:
269 align = 16;
270 bpp = 1; // per-channel bpp
271 yuv_format = true;
272 // We are going to use RGB888 on the host
273 glFormat = GL_RGB;
274 glType = GL_UNSIGNED_BYTE;
275 break;
276 case HAL_PIXEL_FORMAT_YCbCr_420_888:
277 ALOGD("%s: 420_888 format experimental path. "
278 "Initialize rgb565 gl format\n", __FUNCTION__);
279 align = 1;
280 bpp = 1; // per-channel bpp
281 yuv_format = true;
282 // We are going to use RGB888 on the host
283 glFormat = GL_RGB;
284 glType = GL_UNSIGNED_BYTE;
285 break;
286 default:
287 ALOGE("gralloc_alloc: Unknown format %d", format);
288 return -EINVAL;
289 }
290
291 if (usage & GRALLOC_USAGE_HW_FB) {
292 // keep space for postCounter
293 ashmem_size += sizeof(uint32_t);
294 }
295
296 if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
297 // keep space for image on guest memory if SW access is needed
298 // or if the camera is doing writing
299 if (yuv_format) {
300 size_t yStride = (w*bpp + (align - 1)) & ~(align-1);
301 size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1);
302 size_t uvHeight = h / 2;
303 ashmem_size += yStride * h + 2 * (uvHeight * uvStride);
304 stride = yStride / bpp;
305 } else {
306 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
307 ashmem_size += (bpr * h);
308 stride = bpr / bpp;
309 }
310 }
311
312 D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format,
313 ashmem_size, stride, gettid());
314
315 //
316 // Allocate space in ashmem if needed
317 //
318 int fd = -1;
319 if (ashmem_size > 0) {
320 // round to page size;
321 ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
322
323 fd = ashmem_create_region("gralloc-buffer", ashmem_size);
324 if (fd < 0) {
325 ALOGE("gralloc_alloc failed to create ashmem region: %s\n",
326 strerror(errno));
327 return -errno;
328 }
329 }
330
331 cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
332 w, h, frameworkFormat, format,
333 glFormat, glType);
334
335 if (ashmem_size > 0) {
336 //
337 // map ashmem region if exist
338 //
339 void *vaddr;
340 int err = map_buffer(cb, &vaddr);
341 if (err) {
342 close(fd);
343 delete cb;
344 return err;
345 }
346
347 cb->setFd(fd);
348 }
349
350 //
351 // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
352 // Only do this for some h/w usages, not all.
353 // Also do this if we need to read from the surface, in this case the
354 // rendering will still happen on the host but we also need to be able to
355 // read back from the color buffer, which requires that there is a buffer
356 //
357 if (!yuv_format ||
358 frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
359 frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
360 #if PLATFORM_SDK_VERSION >= 15
361 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
362 GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
363 GRALLOC_USAGE_HW_VIDEO_ENCODER |
364 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
365 #else // PLATFORM_SDK_VERSION
366 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
367 GRALLOC_USAGE_HW_2D |
368 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
369 #endif // PLATFORM_SDK_VERSION
370 ALOGD("%s: format %d and usage 0x%x imply creation of host color buffer",
371 __FUNCTION__, frameworkFormat, usage);
372 DEFINE_HOST_CONNECTION;
373 if (hostCon && rcEnc) {
374 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
375 D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
376 }
377
378 if (!cb->hostHandle) {
379 // Could not create colorbuffer on host !!!
380 close(fd);
381 delete cb;
382 ALOGD("%s: failed to create host cb! -EIO", __FUNCTION__);
383 return -EIO;
384 }
385 }
386 }
387
388 //
389 // alloc succeeded - insert the allocated handle to the allocated list
390 //
391 AllocListNode *node = new AllocListNode();
392 pthread_mutex_lock(&grdev->lock);
393 node->handle = cb;
394 node->next = grdev->allocListHead;
395 node->prev = NULL;
396 if (grdev->allocListHead) {
397 grdev->allocListHead->prev = node;
398 }
399 grdev->allocListHead = node;
400 pthread_mutex_unlock(&grdev->lock);
401
402 *pHandle = cb;
403 switch (frameworkFormat) {
404 case HAL_PIXEL_FORMAT_YCbCr_420_888:
405 *pStride = 0;
406 break;
407 default:
408 *pStride = stride;
409 break;
410 }
411 return 0;
412 }
413
414 static int gralloc_free(alloc_device_t* dev,
415 buffer_handle_t handle)
416 {
417 const cb_handle_t *cb = (const cb_handle_t *)handle;
418 if (!cb_handle_t::validate((cb_handle_t*)cb)) {
419 ERR("gralloc_free: invalid handle");
420 return -EINVAL;
421 }
422
423 if (cb->hostHandle != 0) {
424 DEFINE_AND_VALIDATE_HOST_CONNECTION;
425 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
426 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
427 }
428
429 //
430 // detach and unmap ashmem area if present
431 //
432 if (cb->fd > 0) {
433 if (cb->ashmemSize > 0 && cb->ashmemBase) {
434 munmap((void *)cb->ashmemBase, cb->ashmemSize);
435 }
436 close(cb->fd);
437 }
438
439 // remove it from the allocated list
440 gralloc_device_t *grdev = (gralloc_device_t *)dev;
441 pthread_mutex_lock(&grdev->lock);
442 AllocListNode *n = grdev->allocListHead;
443 while( n && n->handle != cb ) {
444 n = n->next;
445 }
446 if (n) {
447 // buffer found on list - remove it from list
448 if (n->next) {
449 n->next->prev = n->prev;
450 }
451 if (n->prev) {
452 n->prev->next = n->next;
453 }
454 else {
455 grdev->allocListHead = n->next;
456 }
457
458 delete n;
459 }
460 pthread_mutex_unlock(&grdev->lock);
461
462 delete cb;
463
464 return 0;
465 }
466
467 static int gralloc_device_close(struct hw_device_t *dev)
468 {
469 gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
470 if (d) {
471
472 // free still allocated buffers
473 while( d->allocListHead != NULL ) {
474 gralloc_free(&d->device, d->allocListHead->handle);
475 }
476
477 // free device
478 free(d);
479 }
480 return 0;
481 }
482
483 static int fb_compositionComplete(struct framebuffer_device_t* dev)
484 {
485 (void)dev;
486
487 return 0;
488 }
489
490 //
491 // Framebuffer device functions
492 //
493 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
494 {
495 fb_device_t *fbdev = (fb_device_t *)dev;
496 cb_handle_t *cb = (cb_handle_t *)buffer;
497
498 if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) {
499 return -EINVAL;
500 }
501
502 // Make sure we have host connection
503 DEFINE_AND_VALIDATE_HOST_CONNECTION;
504
505 // increment the post count of the buffer
506 intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
507 if (!postCountPtr) {
508 // This should not happen
509 return -EINVAL;
510 }
511 (*postCountPtr)++;
512
513 // send post request to host
514 rcEnc->rcFBPost(rcEnc, cb->hostHandle);
515 hostCon->flush();
516
517 return 0;
518 }
519
520 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
521 int l, int t, int w, int h)
522 {
523 fb_device_t *fbdev = (fb_device_t *)dev;
524
525 (void)l;
526 (void)t;
527 (void)w;
528 (void)h;
529
530 if (!fbdev) {
531 return -EINVAL;
532 }
533
534 // Make sure we have host connection
535 DEFINE_AND_VALIDATE_HOST_CONNECTION;
536
537 // send request to host
538 // TODO: XXX - should be implemented
539 //rcEnc->rc_XXX
540
541 return 0;
542 }
543
544 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
545 int interval)
546 {
547 fb_device_t *fbdev = (fb_device_t *)dev;
548
549 if (!fbdev) {
550 return -EINVAL;
551 }
552
553 // Make sure we have host connection
554 DEFINE_AND_VALIDATE_HOST_CONNECTION;
555
556 // send request to host
557 rcEnc->rcFBSetSwapInterval(rcEnc, interval);
558 hostCon->flush();
559
560 return 0;
561 }
562
563 static int fb_close(struct hw_device_t *dev)
564 {
565 fb_device_t *fbdev = (fb_device_t *)dev;
566
567 delete fbdev;
568
569 return 0;
570 }
571
572
573 //
574 // gralloc module functions - refcount + locking interface
575 //
576 static int gralloc_register_buffer(gralloc_module_t const* module,
577 buffer_handle_t handle)
578 {
579 pthread_once(&sFallbackOnce, fallback_init);
580 if (sFallback != NULL) {
581 return sFallback->registerBuffer(sFallback, handle);
582 }
583
584 D("gralloc_register_buffer(%p) called", handle);
585
586 private_module_t *gr = (private_module_t *)module;
587 cb_handle_t *cb = (cb_handle_t *)handle;
588 if (!gr || !cb_handle_t::validate(cb)) {
589 ERR("gralloc_register_buffer(%p): invalid buffer", cb);
590 return -EINVAL;
591 }
592
593 if (cb->hostHandle != 0) {
594 DEFINE_AND_VALIDATE_HOST_CONNECTION;
595 D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
596 rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
597 }
598
599 //
600 // if the color buffer has ashmem region and it is not mapped in this
601 // process map it now.
602 //
603 if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
604 void *vaddr;
605 int err = map_buffer(cb, &vaddr);
606 if (err) {
607 ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
608 return -err;
609 }
610 cb->mappedPid = getpid();
611 }
612
613 return 0;
614 }
615
616 static int gralloc_unregister_buffer(gralloc_module_t const* module,
617 buffer_handle_t handle)
618 {
619 if (sFallback != NULL) {
620 return sFallback->unregisterBuffer(sFallback, handle);
621 }
622
623 private_module_t *gr = (private_module_t *)module;
624 cb_handle_t *cb = (cb_handle_t *)handle;
625 if (!gr || !cb_handle_t::validate(cb)) {
626 ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
627 return -EINVAL;
628 }
629
630 if (cb->hostHandle != 0) {
631 DEFINE_AND_VALIDATE_HOST_CONNECTION;
632 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
633 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
634 }
635
636 //
637 // unmap ashmem region if it was previously mapped in this process
638 // (through register_buffer)
639 //
640 if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
641 void *vaddr;
642 int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
643 if (err) {
644 ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
645 return -EINVAL;
646 }
647 cb->ashmemBase = 0;
648 cb->mappedPid = 0;
649 }
650
651 D("gralloc_unregister_buffer(%p) done\n", cb);
652
653 EXIT_GRALLOCONLY_HOST_CONNECTION;
654 return 0;
655 }
656
657 static signed clamp_rgb(signed value) {
658 if (value > 255) {
659 value = 255;
660 } else if (value < 0) {
661 value = 0;
662 }
663 return value;
664 }
665
666 static void rgb565_to_yv12(char* dest, char* src, int width, int height,
667 int left, int top, int right, int bottom) {
668 int align = 16;
669 int yStride = (width + (align -1)) & ~(align-1);
670 int cStride = (yStride / 2 + (align - 1)) & ~(align-1);
671 int yOffset = 0;
672 int cSize = cStride * height/2;
673
674 uint16_t *rgb_ptr0 = (uint16_t *)src;
675 uint8_t *yv12_y0 = (uint8_t *)dest;
676 uint8_t *yv12_v0 = yv12_y0 + yStride * height;
677 uint8_t *yv12_u0 = yv12_v0 + cSize;
678
679 for (int j = top; j <= bottom; ++j) {
680 uint8_t *yv12_y = yv12_y0 + j * yStride;
681 uint8_t *yv12_v = yv12_v0 + (j/2) * cStride;
682 uint8_t *yv12_u = yv12_v + cSize;
683 uint16_t *rgb_ptr = rgb_ptr0 + j * width;
684 bool jeven = (j & 1) == 0;
685 for (int i = left; i <= right; ++i) {
686 uint8_t r = ((rgb_ptr[i]) >> 11) & 0x01f;
687 uint8_t g = ((rgb_ptr[i]) >> 5) & 0x03f;
688 uint8_t b = (rgb_ptr[i]) & 0x01f;
689 // convert to 8bits
690 // http://stackoverflow.com/questions/2442576/how-does-one-convert-16-bit-rgb565-to-24-bit-rgb888
691 uint8_t R = (r * 527 + 23) >> 6;
692 uint8_t G = (g * 259 + 33) >> 6;
693 uint8_t B = (b * 527 + 23) >> 6;
694 // convert to YV12
695 // frameworks/base/core/jni/android_hardware_camera2_legacy_LegacyCameraDevice.cpp
696 yv12_y[i] = clamp_rgb((77 * R + 150 * G + 29 * B) >> 8);
697 bool ieven = (i & 1) == 0;
698 if (jeven && ieven) {
699 yv12_u[i] = clamp_rgb((( -43 * R - 85 * G + 128 * B) >> 8) + 128);
700 yv12_v[i] = clamp_rgb((( 128 * R - 107 * G - 21 * B) >> 8) + 128);
701 }
702 }
703 }
704 }
705
706 static void rgb888_to_yv12(char* dest, char* src, int width, int height,
707 int left, int top, int right, int bottom) {
708 DD("%s convert %d by %d", __func__, width, height);
709 int align = 16;
710 int yStride = (width + (align -1)) & ~(align-1);
711 int cStride = (yStride / 2 + (align - 1)) & ~(align-1);
712 int yOffset = 0;
713 int cSize = cStride * height/2;
714 int rgb_stride = 3;
715
716 uint8_t *rgb_ptr0 = (uint8_t *)src;
717 uint8_t *yv12_y0 = (uint8_t *)dest;
718 uint8_t *yv12_v0 = yv12_y0 + yStride * height;
719 uint8_t *yv12_u0 = yv12_v0 + cSize;
720
721 for (int j = top; j <= bottom; ++j) {
722 uint8_t *yv12_y = yv12_y0 + j * yStride;
723 uint8_t *yv12_v = yv12_v0 + (j/2) * cStride;
724 uint8_t *yv12_u = yv12_v + cSize;
725 uint8_t *rgb_ptr = rgb_ptr0 + j * width*rgb_stride;
726 bool jeven = (j & 1) == 0;
727 for (int i = left; i <= right; ++i) {
728 uint8_t R = rgb_ptr[i*rgb_stride];
729 uint8_t G = rgb_ptr[i*rgb_stride+1];
730 uint8_t B = rgb_ptr[i*rgb_stride+2];
731 // convert to YV12
732 // frameworks/base/core/jni/android_hardware_camera2_legacy_LegacyCameraDevice.cpp
733 yv12_y[i] = clamp_rgb((77 * R + 150 * G + 29 * B) >> 8);
734 bool ieven = (i & 1) == 0;
735 if (jeven && ieven) {
736 yv12_u[i] = clamp_rgb((( -43 * R - 85 * G + 128 * B) >> 8) + 128);
737 yv12_v[i] = clamp_rgb((( 128 * R - 107 * G - 21 * B) >> 8) + 128);
738 }
739 }
740 }
741 }
742
743 static void rgb888_to_yuv420p(char* dest, char* src, int width, int height,
744 int left, int top, int right, int bottom) {
745 DD("%s convert %d by %d", __func__, width, height);
746 int yStride = width;
747 int cStride = yStride / 2;
748 int yOffset = 0;
749 int cSize = cStride * height/2;
750 int rgb_stride = 3;
751
752 uint8_t *rgb_ptr0 = (uint8_t *)src;
753 uint8_t *yv12_y0 = (uint8_t *)dest;
754 uint8_t *yv12_u0 = yv12_y0 + yStride * height;
755 uint8_t *yv12_v0 = yv12_u0 + cSize;
756
757 for (int j = top; j <= bottom; ++j) {
758 uint8_t *yv12_y = yv12_y0 + j * yStride;
759 uint8_t *yv12_u = yv12_u0 + (j/2) * cStride;
760 uint8_t *yv12_v = yv12_u + cStride;
761 uint8_t *rgb_ptr = rgb_ptr0 + j * width*rgb_stride;
762 bool jeven = (j & 1) == 0;
763 for (int i = left; i <= right; ++i) {
764 uint8_t R = rgb_ptr[i*rgb_stride];
765 uint8_t G = rgb_ptr[i*rgb_stride+1];
766 uint8_t B = rgb_ptr[i*rgb_stride+2];
767 // convert to YV12
768 // frameworks/base/core/jni/android_hardware_camera2_legacy_LegacyCameraDevice.cpp
769 yv12_y[i] = clamp_rgb((77 * R + 150 * G + 29 * B) >> 8);
770 bool ieven = (i & 1) == 0;
771 if (jeven && ieven) {
772 yv12_u[i] = clamp_rgb((( -43 * R - 85 * G + 128 * B) >> 8) + 128);
773 yv12_v[i] = clamp_rgb((( 128 * R - 107 * G - 21 * B) >> 8) + 128);
774 }
775 }
776 }
777 }
778
779 static int gralloc_lock(gralloc_module_t const* module,
780 buffer_handle_t handle, int usage,
781 int l, int t, int w, int h,
782 void** vaddr)
783 {
784 if (sFallback != NULL) {
785 return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
786 }
787
788 private_module_t *gr = (private_module_t *)module;
789 cb_handle_t *cb = (cb_handle_t *)handle;
790 if (!gr || !cb_handle_t::validate(cb)) {
791 ALOGE("gralloc_lock bad handle\n");
792 return -EINVAL;
793 }
794
795 // Validate usage,
796 // 1. cannot be locked for hw access
797 // 2. lock for either sw read or write.
798 // 3. locked sw access must match usage during alloc time.
799 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
800 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
801 bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE);
802 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
803 #if PLATFORM_SDK_VERSION >= 17
804 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
805 bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
806 #else // PLATFORM_SDK_VERSION
807 bool hw_cam_write = false;
808 bool hw_cam_read = false;
809 #endif // PLATFORM_SDK_VERSION
810
811 #if PLATFORM_SDK_VERSION >= 15
812 bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
813 #else // PLATFORM_SDK_VERSION
814 bool hw_vid_enc_read = false;
815 #endif // PLATFORM_SDK_VERSION
816
817 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
818
819 #if PLATFORM_SDK_VERSION >= 15
820 // bug: 30088791
821 // a buffer was created for GRALLOC_USAGE_HW_VIDEO_ENCODER usage but
822 // later a software encoder is reading this buffer: this is actually
823 // legit usage.
824 sw_read_allowed = sw_read_allowed || (cb->usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
825 #endif // PLATFORM_SDK_VERSION >= 15
826
827 bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
828
829 if ( (hw_read || hw_write) ||
830 (!sw_read && !sw_write &&
831 !hw_cam_write && !hw_cam_read &&
832 !hw_vid_enc_read) ||
833 (sw_read && !sw_read_allowed) ||
834 (sw_write && !sw_write_allowed) ) {
835 ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage,
836 cb->usage);
837 //This is not exactly an error and loose it up.
838 //bug: 30784436
839 //return -EINVAL;
840 }
841
842 intptr_t postCount = 0;
843 void *cpu_addr = NULL;
844
845 //
846 // make sure ashmem area is mapped if needed
847 //
848 if (cb->canBePosted() || sw_read || sw_write ||
849 hw_cam_write || hw_cam_read ||
850 hw_vid_enc_read) {
851 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
852 return -EACCES;
853 }
854
855 if (cb->canBePosted()) {
856 postCount = *((intptr_t *)cb->ashmemBase);
857 cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t));
858 }
859 else {
860 cpu_addr = (void *)(cb->ashmemBase);
861 }
862 }
863
864 if (cb->hostHandle) {
865 // Make sure we have host connection
866 DEFINE_AND_VALIDATE_HOST_CONNECTION;
867
868 //
869 // flush color buffer write cache on host and get its sync status.
870 //
871 int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
872 postCount,
873 sw_read);
874 if (hostSyncStatus < 0) {
875 // host failed the color buffer sync - probably since it was already
876 // locked for write access. fail the lock.
877 ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n",
878 postCount, sw_read);
879 return -EBUSY;
880 }
881
882 if (sw_read) {
883 void* rgb_addr = cpu_addr;
884 char* tmpBuf = 0;
885 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
886 cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
887 // We are using RGB88
888 tmpBuf = new char[cb->width * cb->height * 3];
889 rgb_addr = tmpBuf;
890 }
891 D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height);
892 rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
893 0, 0, cb->width, cb->height, cb->glFormat, cb->glType, rgb_addr);
894 if (tmpBuf) {
895 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
896 rgb888_to_yv12((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
897 } else if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
898 rgb888_to_yuv420p((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
899 }
900 delete [] tmpBuf;
901 }
902 }
903 }
904
905 //
906 // is virtual address required ?
907 //
908 if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) {
909 *vaddr = cpu_addr;
910 }
911
912 if (sw_write || hw_cam_write) {
913 //
914 // Keep locked region if locked for s/w write access.
915 //
916 cb->lockedLeft = l;
917 cb->lockedTop = t;
918 cb->lockedWidth = w;
919 cb->lockedHeight = h;
920 }
921
922 DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p",
923 vaddr, vaddr ? *vaddr : 0, usage, cpu_addr);
924
925 return 0;
926 }
927
928 // YV12 is aka YUV420Planar, or YUV420p; the only difference is that YV12 has
929 // certain stride requirements for Y and UV respectively.
930 static void yv12_to_rgb565(char* dest, char* src, int width, int height,
931 int left, int top, int right, int bottom) {
932 DD("%s convert %d by %d", __func__, width, height);
933 int align = 16;
934 int yStride = (width + (align -1)) & ~(align-1);
935 int cStride = (yStride / 2 + (align - 1)) & ~(align-1);
936 int yOffset = 0;
937 int cSize = cStride * height/2;
938
939 uint16_t *rgb_ptr0 = (uint16_t *)dest;
940 uint8_t *yv12_y0 = (uint8_t *)src;
941 uint8_t *yv12_v0 = yv12_y0 + yStride * height;
942 uint8_t *yv12_u0 = yv12_v0 + cSize;
943
944 for (int j = top; j <= bottom; ++j) {
945 uint8_t *yv12_y = yv12_y0 + j * yStride;
946 uint8_t *yv12_v = yv12_v0 + (j/2) * cStride;
947 uint8_t *yv12_u = yv12_v + cSize;
948 uint16_t *rgb_ptr = rgb_ptr0 + (j-top) * (right-left+1);
949 for (int i = left; i <= right; ++i) {
950 // convert to rgb
951 // frameworks/av/media/libstagefright/colorconversion/ColorConverter.cpp
952 signed y1 = (signed)yv12_y[i] - 16;
953 signed u = (signed)yv12_u[i / 2] - 128;
954 signed v = (signed)yv12_v[i / 2] - 128;
955
956 signed u_b = u * 517;
957 signed u_g = -u * 100;
958 signed v_g = -v * 208;
959 signed v_r = v * 409;
960
961 signed tmp1 = y1 * 298;
962 signed b1 = clamp_rgb((tmp1 + u_b) / 256);
963 signed g1 = clamp_rgb((tmp1 + v_g + u_g) / 256);
964 signed r1 = clamp_rgb((tmp1 + v_r) / 256);
965
966 uint16_t rgb1 = ((r1 >> 3) << 11) | ((g1 >> 2) << 5) | (b1 >> 3);
967
968 rgb_ptr[i-left] = rgb1;
969 }
970 }
971 }
972
973 // YV12 is aka YUV420Planar, or YUV420p; the only difference is that YV12 has
974 // certain stride requirements for Y and UV respectively.
975 static void yv12_to_rgb888(char* dest, char* src, int width, int height,
976 int left, int top, int right, int bottom) {
977 DD("%s convert %d by %d", __func__, width, height);
978 int align = 16;
979 int yStride = (width + (align -1)) & ~(align-1);
980 int cStride = (yStride / 2 + (align - 1)) & ~(align-1);
981 int yOffset = 0;
982 int cSize = cStride * height/2;
983 int rgb_stride = 3;
984
985 uint8_t *rgb_ptr0 = (uint8_t *)dest;
986 uint8_t *yv12_y0 = (uint8_t *)src;
987 uint8_t *yv12_v0 = yv12_y0 + yStride * height;
988 uint8_t *yv12_u0 = yv12_v0 + cSize;
989
990 for (int j = top; j <= bottom; ++j) {
991 uint8_t *yv12_y = yv12_y0 + j * yStride;
992 uint8_t *yv12_v = yv12_v0 + (j/2) * cStride;
993 uint8_t *yv12_u = yv12_v + cSize;
994 uint8_t *rgb_ptr = rgb_ptr0 + (j-top) * (right-left+1) * rgb_stride;
995 for (int i = left; i <= right; ++i) {
996 // convert to rgb
997 // frameworks/av/media/libstagefright/colorconversion/ColorConverter.cpp
998 signed y1 = (signed)yv12_y[i] - 16;
999 signed u = (signed)yv12_u[i / 2] - 128;
1000 signed v = (signed)yv12_v[i / 2] - 128;
1001
1002 signed u_b = u * 517;
1003 signed u_g = -u * 100;
1004 signed v_g = -v * 208;
1005 signed v_r = v * 409;
1006
1007 signed tmp1 = y1 * 298;
1008 signed b1 = clamp_rgb((tmp1 + u_b) / 256);
1009 signed g1 = clamp_rgb((tmp1 + v_g + u_g) / 256);
1010 signed r1 = clamp_rgb((tmp1 + v_r) / 256);
1011
1012 rgb_ptr[(i-left)*rgb_stride] = r1;
1013 rgb_ptr[(i-left)*rgb_stride+1] = g1;
1014 rgb_ptr[(i-left)*rgb_stride+2] = b1;
1015 }
1016 }
1017 }
1018
1019 // YV12 is aka YUV420Planar, or YUV420p; the only difference is that YV12 has
1020 // certain stride requirements for Y and UV respectively.
1021 static void yuv420p_to_rgb888(char* dest, char* src, int width, int height,
1022 int left, int top, int right, int bottom) {
1023 DD("%s convert %d by %d", __func__, width, height);
1024 int yStride = width;
1025 int cStride = yStride / 2;
1026 int yOffset = 0;
1027 int cSize = cStride * height/2;
1028 int rgb_stride = 3;
1029
1030 uint8_t *rgb_ptr0 = (uint8_t *)dest;
1031 uint8_t *yv12_y0 = (uint8_t *)src;
1032 uint8_t *yv12_u0 = yv12_y0 + yStride * height;
1033 uint8_t *yv12_v0 = yv12_u0 + cSize;
1034
1035 for (int j = top; j <= bottom; ++j) {
1036 uint8_t *yv12_y = yv12_y0 + j * yStride;
1037 uint8_t *yv12_u = yv12_u0 + (j/2) * cStride;
1038 uint8_t *yv12_v = yv12_u + cSize;
1039 uint8_t *rgb_ptr = rgb_ptr0 + (j-top) * (right-left+1) * rgb_stride;
1040 for (int i = left; i <= right; ++i) {
1041 // convert to rgb
1042 // frameworks/av/media/libstagefright/colorconversion/ColorConverter.cpp
1043 signed y1 = (signed)yv12_y[i] - 16;
1044 signed u = (signed)yv12_u[i / 2] - 128;
1045 signed v = (signed)yv12_v[i / 2] - 128;
1046
1047 signed u_b = u * 517;
1048 signed u_g = -u * 100;
1049 signed v_g = -v * 208;
1050 signed v_r = v * 409;
1051
1052 signed tmp1 = y1 * 298;
1053 signed b1 = clamp_rgb((tmp1 + u_b) / 256);
1054 signed g1 = clamp_rgb((tmp1 + v_g + u_g) / 256);
1055 signed r1 = clamp_rgb((tmp1 + v_r) / 256);
1056
1057 rgb_ptr[(i-left)*rgb_stride] = r1;
1058 rgb_ptr[(i-left)*rgb_stride+1] = g1;
1059 rgb_ptr[(i-left)*rgb_stride+2] = b1;
1060 }
1061 }
1062 }
1063
1064 static int gralloc_unlock(gralloc_module_t const* module,
1065 buffer_handle_t handle)
1066 {
1067 if (sFallback != NULL) {
1068 return sFallback->unlock(sFallback, handle);
1069 }
1070
1071 private_module_t *gr = (private_module_t *)module;
1072 cb_handle_t *cb = (cb_handle_t *)handle;
1073 if (!gr || !cb_handle_t::validate(cb)) {
1074 ALOGD("%s: invalid gr or cb handle. -EINVAL", __FUNCTION__);
1075 return -EINVAL;
1076 }
1077
1078 //
1079 // if buffer was locked for s/w write, we need to update the host with
1080 // the updated data
1081 //
1082 if (cb->hostHandle) {
1083
1084 // Make sure we have host connection
1085 DEFINE_AND_VALIDATE_HOST_CONNECTION;
1086
1087 void *cpu_addr;
1088 if (cb->canBePosted()) {
1089 cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
1090 }
1091 else {
1092 cpu_addr = (void *)(cb->ashmemBase);
1093 }
1094
1095 char* rgb_addr = (char *)cpu_addr;
1096 if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
1097 int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3;
1098 char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp];
1099 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
1100 yv12_to_rgb888(tmpBuf, (char*)cpu_addr, cb->width, cb->height, cb->lockedLeft,
1101 cb->lockedTop, cb->lockedLeft+cb->lockedWidth-1, cb->lockedTop+cb->lockedHeight-1);
1102 } else if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1103 yuv420p_to_rgb888(tmpBuf, (char*)cpu_addr, cb->width, cb->height, cb->lockedLeft,
1104 cb->lockedTop, cb->lockedLeft+cb->lockedWidth-1, cb->lockedTop+cb->lockedHeight-1);
1105 } else {
1106 int dst_line_len = cb->lockedWidth * bpp;
1107 int src_line_len = cb->width * bpp;
1108 char *src = (char *)rgb_addr + cb->lockedTop*src_line_len + cb->lockedLeft*bpp;
1109 char *dst = tmpBuf;
1110 for (int y=0; y<cb->lockedHeight; y++) {
1111 memcpy(dst, src, dst_line_len);
1112 src += src_line_len;
1113 dst += dst_line_len;
1114 }
1115 }
1116
1117 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
1118 cb->lockedLeft, cb->lockedTop,
1119 cb->lockedWidth, cb->lockedHeight,
1120 cb->glFormat, cb->glType,
1121 tmpBuf);
1122
1123 delete [] tmpBuf;
1124 }
1125 else {
1126 char* rgbBuf = 0;
1127 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
1128 // for this format, we need to convert to RGB888 format
1129 // before updating host
1130 rgbBuf = new char[cb->width * cb->height * 3];
1131 yv12_to_rgb888(rgbBuf, (char*)cpu_addr, cb->width, cb->height, 0, 0, cb->width-1, cb->height-1);
1132 rgb_addr = rgbBuf;
1133 } else if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1134 // for this format, we need to convert to RGB888 format
1135 // before updating host
1136 rgbBuf = new char[cb->width * cb->height * 3];
1137 yuv420p_to_rgb888(rgbBuf, (char*)cpu_addr, cb->width, cb->height, 0, 0, cb->width-1, cb->height-1);
1138 rgb_addr = rgbBuf;
1139 }
1140
1141 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0,
1142 cb->width, cb->height,
1143 cb->glFormat, cb->glType,
1144 rgb_addr);
1145 if (rgbBuf) {
1146 delete [] rgbBuf;
1147 }
1148 }
1149
1150 DD("gralloc_unlock success. cpu_addr: %p", cpu_addr);
1151 }
1152
1153 cb->lockedWidth = cb->lockedHeight = 0;
1154 return 0;
1155 }
1156
1157 #if PLATFORM_SDK_VERSION >= 18
1158 static int gralloc_lock_ycbcr(gralloc_module_t const* module,
1159 buffer_handle_t handle, int usage,
1160 int l, int t, int w, int h,
1161 android_ycbcr *ycbcr)
1162 {
1163 // Not supporting fallback module for YCbCr
1164 if (sFallback != NULL) {
1165 ALOGD("%s: has fallback, return -EINVAL", __FUNCTION__);
1166 return -EINVAL;
1167 }
1168
1169 if (!ycbcr) {
1170 ALOGE("%s: got NULL ycbcr struct! -EINVAL", __FUNCTION__);
1171 return -EINVAL;
1172 }
1173
1174 private_module_t *gr = (private_module_t *)module;
1175 cb_handle_t *cb = (cb_handle_t *)handle;
1176 if (!gr || !cb_handle_t::validate(cb)) {
1177 ALOGE("%s: bad colorbuffer handle. -EINVAL", __FUNCTION__);
1178 return -EINVAL;
1179 }
1180
1181 if (cb->frameworkFormat != HAL_PIXEL_FORMAT_YV12 &&
1182 cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888) {
1183 ALOGE("gralloc_lock_ycbcr can only be used with "
1184 "HAL_PIXEL_FORMAT_YCbCr_420_888 or HAL_PIXEL_FORMAT_YV12, got %x instead. "
1185 "-EINVAL",
1186 cb->frameworkFormat);
1187 return -EINVAL;
1188 }
1189
1190 // Make sure memory is mapped, get address
1191 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
1192 ALOGD("%s: ashmembase not mapped. -EACCESS", __FUNCTION__);
1193 return -EACCES;
1194 }
1195
1196 uint8_t *cpu_addr = NULL;
1197
1198 if (cb->canBePosted()) {
1199 cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int));
1200 }
1201 else {
1202 cpu_addr = (uint8_t *)(cb->ashmemBase);
1203 }
1204
1205 // Calculate offsets to underlying YUV data
1206 size_t yStride;
1207 size_t cStride;
1208 size_t cSize;
1209 size_t yOffset;
1210 size_t uOffset;
1211 size_t vOffset;
1212 size_t cStep;
1213 size_t align;
1214 switch (cb->format) {
1215 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1216 yStride = cb->width;
1217 cStride = cb->width;
1218 yOffset = 0;
1219 vOffset = yStride * cb->height;
1220 uOffset = vOffset + 1;
1221 cStep = 2;
1222 break;
1223 case HAL_PIXEL_FORMAT_YV12:
1224 // https://developer.android.com/reference/android/graphics/ImageFormat.html#YV12
1225 align = 16;
1226 yStride = (cb->width + (align -1)) & ~(align-1);
1227 cStride = (yStride / 2 + (align - 1)) & ~(align-1);
1228 yOffset = 0;
1229 cSize = cStride * cb->height/2;
1230 vOffset = yStride * cb->height;
1231 uOffset = vOffset + cSize;
1232 cStep = 1;
1233 break;
1234 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1235 align = 1;
1236 yStride = cb->width;
1237 cStride = yStride / 2;
1238 yOffset = 0;
1239 cSize = cStride * cb->height/2;
1240 uOffset = yStride * cb->height;
1241 vOffset = uOffset + cSize;
1242 cStep = 1;
1243 break;
1244 default:
1245 ALOGE("gralloc_lock_ycbcr unexpected internal format %x",
1246 cb->format);
1247 return -EINVAL;
1248 }
1249
1250 ycbcr->y = cpu_addr + yOffset;
1251 ycbcr->cb = cpu_addr + uOffset;
1252 ycbcr->cr = cpu_addr + vOffset;
1253 ycbcr->ystride = yStride;
1254 ycbcr->cstride = cStride;
1255 ycbcr->chroma_step = cStep;
1256
1257 // Zero out reserved fields
1258 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
1259
1260 //
1261 // Keep locked region if locked for s/w write access.
1262 //
1263 cb->lockedLeft = l;
1264 cb->lockedTop = t;
1265 cb->lockedWidth = w;
1266 cb->lockedHeight = h;
1267
1268 DD("gralloc_lock_ycbcr success. usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, "
1269 ".ystride: %d , .cstride: %d, .chroma_step: %d", usage,
1270 ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride,
1271 ycbcr->chroma_step);
1272
1273 return 0;
1274 }
1275 #endif // PLATFORM_SDK_VERSION >= 18
1276
1277 static int gralloc_device_open(const hw_module_t* module,
1278 const char* name,
1279 hw_device_t** device)
1280 {
1281 int status = -EINVAL;
1282
1283 D("gralloc_device_open %s\n", name);
1284
1285 pthread_once( &sFallbackOnce, fallback_init );
1286 if (sFallback != NULL) {
1287 return sFallback->common.methods->open(&sFallback->common, name, device);
1288 }
1289
1290 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
1291
1292 // Create host connection and keep it in the TLS.
1293 // return error if connection with host can not be established
1294 HostConnection *hostCon = HostConnection::get();
1295 if (!hostCon) {
1296 ALOGE("gralloc: failed to get host connection while opening %s\n", name);
1297 return -EIO;
1298 }
1299
1300 //
1301 // Allocate memory for the gralloc device (alloc interface)
1302 //
1303 gralloc_device_t *dev;
1304 dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t));
1305 if (NULL == dev) {
1306 return -ENOMEM;
1307 }
1308
1309 // Initialize our device structure
1310 //
1311 dev->device.common.tag = HARDWARE_DEVICE_TAG;
1312 dev->device.common.version = 0;
1313 dev->device.common.module = const_cast<hw_module_t*>(module);
1314 dev->device.common.close = gralloc_device_close;
1315
1316 dev->device.alloc = gralloc_alloc;
1317 dev->device.free = gralloc_free;
1318 dev->allocListHead = NULL;
1319 pthread_mutex_init(&dev->lock, NULL);
1320
1321 *device = &dev->device.common;
1322 status = 0;
1323 }
1324 else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
1325
1326 // return error if connection with host can not be established
1327 DEFINE_AND_VALIDATE_HOST_CONNECTION;
1328
1329 //
1330 // Query the host for Framebuffer attributes
1331 //
1332 D("gralloc: query Frabuffer attribs\n");
1333 EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH);
1334 D("gralloc: width=%d\n", width);
1335 EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT);
1336 D("gralloc: height=%d\n", height);
1337 EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI);
1338 D("gralloc: xdpi=%d\n", xdpi);
1339 EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI);
1340 D("gralloc: ydpi=%d\n", ydpi);
1341 EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS);
1342 D("gralloc: fps=%d\n", fps);
1343 EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL);
1344 D("gralloc: min_swap=%d\n", min_si);
1345 EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL);
1346 D("gralloc: max_swap=%d\n", max_si);
1347
1348 //
1349 // Allocate memory for the framebuffer device
1350 //
1351 fb_device_t *dev;
1352 dev = (fb_device_t*)malloc(sizeof(fb_device_t));
1353 if (NULL == dev) {
1354 return -ENOMEM;
1355 }
1356 memset(dev, 0, sizeof(fb_device_t));
1357
1358 // Initialize our device structure
1359 //
1360 dev->device.common.tag = HARDWARE_DEVICE_TAG;
1361 dev->device.common.version = 0;
1362 dev->device.common.module = const_cast<hw_module_t*>(module);
1363 dev->device.common.close = fb_close;
1364 dev->device.setSwapInterval = fb_setSwapInterval;
1365 dev->device.post = fb_post;
1366 dev->device.setUpdateRect = 0; //fb_setUpdateRect;
1367 dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy
1368
1369 const_cast<uint32_t&>(dev->device.flags) = 0;
1370 const_cast<uint32_t&>(dev->device.width) = width;
1371 const_cast<uint32_t&>(dev->device.height) = height;
1372 const_cast<int&>(dev->device.stride) = width;
1373 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888;
1374 const_cast<float&>(dev->device.xdpi) = xdpi;
1375 const_cast<float&>(dev->device.ydpi) = ydpi;
1376 const_cast<float&>(dev->device.fps) = fps;
1377 const_cast<int&>(dev->device.minSwapInterval) = min_si;
1378 const_cast<int&>(dev->device.maxSwapInterval) = max_si;
1379 *device = &dev->device.common;
1380
1381 status = 0;
1382 }
1383
1384 return status;
1385 }
1386
1387 //
1388 // define the HMI symbol - our module interface
1389 //
1390 static struct hw_module_methods_t gralloc_module_methods = {
1391 open: gralloc_device_open
1392 };
1393
1394 struct private_module_t HAL_MODULE_INFO_SYM = {
1395 base: {
1396 common: {
1397 tag: HARDWARE_MODULE_TAG,
1398 #if PLATFORM_SDK_VERSION >= 18
1399 module_api_version: GRALLOC_MODULE_API_VERSION_0_2,
1400 hal_api_version: 0,
1401 #elif PLATFORM_SDK_VERSION >= 16
1402 module_api_version: 1,
1403 hal_api_version: 0,
1404 #else // PLATFORM_SDK_VERSION
1405 version_major: 1,
1406 version_minor: 0,
1407 #endif // PLATFORM_SDK_VERSION
1408 id: GRALLOC_HARDWARE_MODULE_ID,
1409 name: "Graphics Memory Allocator Module",
1410 author: "The Android Open Source Project",
1411 methods: &gralloc_module_methods,
1412 dso: NULL,
1413 reserved: {0, }
1414 },
1415 registerBuffer: gralloc_register_buffer,
1416 unregisterBuffer: gralloc_unregister_buffer,
1417 lock: gralloc_lock,
1418 unlock: gralloc_unlock,
1419 perform: NULL,
1420 #if PLATFORM_SDK_VERSION >= 18
1421 lock_ycbcr: gralloc_lock_ycbcr,
1422 #endif // PLATFORM_SDK_VERSION >= 18
1423 }
1424 };
1425
1426 /* This function is called once to detect whether the emulator supports
1427 * GPU emulation (this is done by looking at the qemu.gles kernel
1428 * parameter, which must be == 1 if this is the case).
1429 *
1430 * If not, then load gralloc.default instead as a fallback.
1431 */
1432 static void
1433 fallback_init(void)
1434 {
1435 char prop[PROPERTY_VALUE_MAX];
1436 void* module;
1437
1438 // qemu.gles=0 -> no GLES 2.x support (only 1.x through software).
1439 // qemu.gles=1 -> host-side GPU emulation through EmuGL
1440 // qemu.gles=2 -> guest-side GPU emulation.
1441 property_get("ro.kernel.qemu.gles", prop, "0");
1442 if (atoi(prop) == 1) {
1443 return;
1444 }
1445 ALOGD("Emulator without host-side GPU emulation detected.");
1446 #if __LP64__
1447 module = dlopen("/system/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1448 #else
1449 module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1450 #endif
1451 if (module != NULL) {
1452 sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
1453 if (sFallback == NULL) {
1454 dlclose(module);
1455 }
1456 }
1457 if (sFallback == NULL) {
1458 ALOGE("Could not find software fallback module!?");
1459 }
1460 }
1461