2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <cutils/ashmem.h>
24 #include "gralloc_cb.h"
25 #include "goldfish_dma.h"
26 #include "FormatConversions.h"
27 #include "HostConnection.h"
28 #include "ProcessPipe.h"
30 #include <cutils/log.h>
31 #include <cutils/properties.h>
33 /* Set to 1 or 2 to enable debug traces */
37 # define D(...) ALOGD(__VA_ARGS__)
39 # define D(...) ((void)0)
43 # define DD(...) ALOGD(__VA_ARGS__)
45 # define DD(...) ((void)0)
48 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
51 // our private gralloc module structure
53 struct private_module_t {
54 gralloc_module_t base;
57 /* If not NULL, this is a pointer to the fallback module.
58 * This really is gralloc.default, which we'll use if we detect
59 * that the emulator we're running in does not support GPU emulation.
61 static gralloc_module_t* sFallback;
62 static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT;
64 static void fallback_init(void); // forward
67 typedef struct _alloc_list_node {
68 buffer_handle_t handle;
69 _alloc_list_node *next;
70 _alloc_list_node *prev;
74 // Our gralloc device structure (alloc interface)
76 struct gralloc_device_t {
77 alloc_device_t device;
79 AllocListNode *allocListHead; // double linked list of allocated buffers
84 // Our framebuffer device structure
87 framebuffer_device_t device;
90 static int map_buffer(cb_handle_t *cb, void **vaddr)
92 if (cb->fd < 0 || cb->ashmemSize <= 0) {
96 void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
97 MAP_SHARED, cb->fd, 0);
98 if (addr == MAP_FAILED) {
99 ALOGE("%s: failed to map ashmem region!", __FUNCTION__);
103 cb->ashmemBase = intptr_t(addr);
104 cb->ashmemBasePid = getpid();
110 #define DEFINE_HOST_CONNECTION \
111 HostConnection *hostCon = HostConnection::get(); \
112 ExtendedRCEncoderContext *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
114 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
115 HostConnection *hostCon = HostConnection::get(); \
117 ALOGE("gralloc: Failed to get host connection\n"); \
120 ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder(); \
122 ALOGE("gralloc: Failed to get renderControl encoder context\n"); \
126 #if PLATFORM_SDK_VERSION < 18
127 // On older APIs, just define it as a value no one is going to use.
128 #define HAL_PIXEL_FORMAT_YCbCr_420_888 0xFFFFFFFF
131 static void updateHostColorBuffer(cb_handle_t* cb,
134 D("%s: call. doLocked=%d", __FUNCTION__, doLocked);
135 DEFINE_HOST_CONNECTION;
136 int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3;
137 int left = doLocked ? cb->lockedLeft : 0;
138 int top = doLocked ? cb->lockedTop : 0;
139 int width = doLocked ? cb->lockedWidth : cb->width;
140 int height = doLocked ? cb->lockedHeight : cb->height;
142 char* to_send = pixels;
143 uint32_t rgbSz = width * height * bpp;
144 uint32_t send_buffer_size = rgbSz;
146 cb->frameworkFormat != HAL_PIXEL_FORMAT_YV12 &&
147 cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888;
149 char* convertedBuf = NULL;
150 if ((doLocked && is_rgb_format) ||
151 (cb->goldfish_dma.fd < 0 &&
152 (doLocked || !is_rgb_format))) {
153 convertedBuf = new char[rgbSz];
154 to_send = convertedBuf;
155 send_buffer_size = rgbSz;
158 if (doLocked && is_rgb_format) {
159 copy_rgb_buffer_from_unlocked(
162 width, height, top, left, bpp);
165 if (cb->goldfish_dma.fd > 0) {
166 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
167 get_yv12_offsets(width, height, NULL, NULL,
170 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
171 get_yuv420p_offsets(width, height, NULL, NULL,
175 rcEnc->bindDmaContext(&cb->goldfish_dma);
176 D("%s: call. dma update with sz=%u", __FUNCTION__, send_buffer_size);
177 rcEnc->rcUpdateColorBufferDMA(rcEnc, cb->hostHandle,
178 left, top, width, height,
179 cb->glFormat, cb->glType,
180 to_send, send_buffer_size);
182 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
183 yv12_to_rgb888(to_send, pixels,
184 width, height, left, top,
185 left + width - 1, top + height - 1);
187 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
188 yuv420p_to_rgb888(to_send, pixels,
189 width, height, left, top,
190 left + width - 1, top + height - 1);
192 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
193 left, top, width, height,
194 cb->glFormat, cb->glType, to_send);
197 if (convertedBuf) delete [] convertedBuf;
201 // gralloc device functions (alloc interface)
203 static int gralloc_alloc(alloc_device_t* dev,
204 int w, int h, int format, int usage,
205 buffer_handle_t* pHandle, int* pStride)
207 D("gralloc_alloc w=%d h=%d usage=0x%x format=0x%x\n", w, h, usage, format);
209 gralloc_device_t *grdev = (gralloc_device_t *)dev;
210 if (!grdev || !pHandle || !pStride) {
211 ALOGE("gralloc_alloc: Bad inputs (grdev: %p, pHandle: %p, pStride: %p",
212 grdev, pHandle, pStride);
217 // Note: in screen capture mode, both sw_write and hw_write will be on
218 // and this is a valid usage
220 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
221 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
222 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
223 #if PLATFORM_SDK_VERSION >= 17
224 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
225 bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
226 #else // PLATFORM_SDK_VERSION
227 bool hw_cam_write = false;
228 bool hw_cam_read = false;
229 #endif // PLATFORM_SDK_VERSION
230 #if PLATFORM_SDK_VERSION >= 15
231 bool hw_vid_enc_read = usage & GRALLOC_USAGE_HW_VIDEO_ENCODER;
232 #else // PLATFORM_SDK_VERSION
233 bool hw_vid_enc_read = false;
234 #endif // PLATFORM_SDK_VERSION
236 // Keep around original requested format for later validation
237 int frameworkFormat = format;
238 // Pick the right concrete pixel format given the endpoints as encoded in
239 // the usage bits. Every end-point pair needs explicit listing here.
240 #if PLATFORM_SDK_VERSION >= 17
241 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
242 // Camera as producer
243 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
244 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
245 // Camera-to-display is RGBA
246 format = HAL_PIXEL_FORMAT_RGBA_8888;
247 } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
248 // Camera-to-encoder is NV21
249 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
250 } else if ((usage & GRALLOC_USAGE_HW_CAMERA_MASK) ==
251 GRALLOC_USAGE_HW_CAMERA_ZSL) {
252 // Camera-to-ZSL-queue is RGB_888
253 format = HAL_PIXEL_FORMAT_RGB_888;
257 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
258 ALOGE("gralloc_alloc: Requested auto format selection, "
259 "but no known format for this usage: %d x %d, usage %x",
264 else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
265 ALOGW("gralloc_alloc: Requested YCbCr_420_888, taking experimental path. "
266 "usage: %d x %d, usage %x",
269 #endif // PLATFORM_SDK_VERSION >= 17
270 bool yuv_format = false;
277 EmulatorFrameworkFormat selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_GL_COMPATIBLE;
282 case HAL_PIXEL_FORMAT_RGBA_8888:
283 case HAL_PIXEL_FORMAT_RGBX_8888:
284 case HAL_PIXEL_FORMAT_BGRA_8888:
287 glType = GL_UNSIGNED_BYTE;
289 case HAL_PIXEL_FORMAT_RGB_888:
292 glType = GL_UNSIGNED_BYTE;
294 case HAL_PIXEL_FORMAT_RGB_565:
297 glType = GL_UNSIGNED_SHORT_5_6_5;
299 #if PLATFORM_SDK_VERSION >= 21
300 case HAL_PIXEL_FORMAT_RAW16:
301 case HAL_PIXEL_FORMAT_Y16:
302 #elif PLATFORM_SDK_VERSION >= 16
303 case HAL_PIXEL_FORMAT_RAW_SENSOR:
307 if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) {
308 // Raw sensor data or Y16 only goes between camera and CPU
311 // Not expecting to actually create any GL surfaces for this
312 glFormat = GL_LUMINANCE;
313 glType = GL_UNSIGNED_SHORT;
315 #if PLATFORM_SDK_VERSION >= 17
316 case HAL_PIXEL_FORMAT_BLOB:
318 if (! (sw_read && hw_cam_write) ) {
319 // Blob data cannot be used by HW other than camera emulator
322 // Not expecting to actually create any GL surfaces for this
323 glFormat = GL_LUMINANCE;
324 glType = GL_UNSIGNED_BYTE;
326 #endif // PLATFORM_SDK_VERSION >= 17
327 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
329 bpp = 1; // per-channel bpp
331 // Not expecting to actually create any GL surfaces for this
333 case HAL_PIXEL_FORMAT_YV12:
335 bpp = 1; // per-channel bpp
337 // We are going to use RGB888 on the host
339 glType = GL_UNSIGNED_BYTE;
340 selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YV12;
342 case HAL_PIXEL_FORMAT_YCbCr_420_888:
343 ALOGD("%s: 420_888 format experimental path. "
344 "Initialize rgb565 gl format\n", __FUNCTION__);
346 bpp = 1; // per-channel bpp
348 // We are going to use RGB888 on the host
350 glType = GL_UNSIGNED_BYTE;
351 selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
354 ALOGE("gralloc_alloc: Unknown format %d", format);
358 if (usage & GRALLOC_USAGE_HW_FB) {
359 // keep space for postCounter
360 ashmem_size += sizeof(uint32_t);
363 if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
364 // keep space for image on guest memory if SW access is needed
365 // or if the camera is doing writing
367 size_t yStride = (w*bpp + (align - 1)) & ~(align-1);
368 size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1);
369 size_t uvHeight = h / 2;
370 ashmem_size += yStride * h + 2 * (uvHeight * uvStride);
371 stride = yStride / bpp;
373 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
374 ashmem_size += (bpr * h);
379 D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format,
380 ashmem_size, stride, gettid());
383 // Allocate space in ashmem if needed
386 if (ashmem_size > 0) {
387 // round to page size;
388 ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
390 ALOGD("%s: Creating ashmem region of size %lu\n", __FUNCTION__, ashmem_size);
391 fd = ashmem_create_region("gralloc-buffer", ashmem_size);
393 ALOGE("gralloc_alloc failed to create ashmem region: %s\n",
399 cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
400 w, h, frameworkFormat, format,
401 glFormat, glType, selectedEmuFrameworkFormat);
403 DEFINE_HOST_CONNECTION;
404 if (ashmem_size > 0) {
407 // map ashmem region if exist
410 int err = map_buffer(cb, &vaddr);
419 if (rcEnc->getDmaVersion() > 0) {
420 D("%s: creating goldfish dma region of size %lu (cb fd %d)\n", __FUNCTION__, ashmem_size, cb->fd);
421 err = goldfish_dma_create_region(ashmem_size, &cb->goldfish_dma);
423 ALOGE("%s: Failed to create goldfish DMA region", __FUNCTION__);
425 goldfish_dma_map(&cb->goldfish_dma);
426 cb->setDmaFd(cb->goldfish_dma.fd);
427 D("%s: done, cbfd %d dmafd1 %d dmafd2 %d", __FUNCTION__, cb->fd, cb->goldfish_dma.fd, cb->dmafd);
430 cb->goldfish_dma.fd = -1;
433 cb->goldfish_dma.fd = -1;
437 // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
438 // Only do this for some h/w usages, not all.
439 // Also do this if we need to read from the surface, in this case the
440 // rendering will still happen on the host but we also need to be able to
441 // read back from the color buffer, which requires that there is a buffer
444 frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
445 frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
446 #if PLATFORM_SDK_VERSION >= 15
447 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
448 GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
449 GRALLOC_USAGE_HW_VIDEO_ENCODER |
450 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
451 #else // PLATFORM_SDK_VERSION
452 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
453 GRALLOC_USAGE_HW_2D |
454 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
455 #endif // PLATFORM_SDK_VERSION
456 if (hostCon && rcEnc) {
457 if (cb->goldfish_dma.fd > 0) {
458 cb->hostHandle = rcEnc->rcCreateColorBufferDMA(rcEnc, w, h, glFormat, cb->emuFrameworkFormat);
460 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
462 D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
465 if (!cb->hostHandle) {
466 // Could not create colorbuffer on host !!!
469 ALOGD("%s: failed to create host cb! -EIO", __FUNCTION__);
476 // alloc succeeded - insert the allocated handle to the allocated list
478 AllocListNode *node = new AllocListNode();
479 pthread_mutex_lock(&grdev->lock);
481 node->next = grdev->allocListHead;
483 if (grdev->allocListHead) {
484 grdev->allocListHead->prev = node;
486 grdev->allocListHead = node;
487 pthread_mutex_unlock(&grdev->lock);
490 switch (frameworkFormat) {
491 case HAL_PIXEL_FORMAT_YCbCr_420_888:
501 static int gralloc_free(alloc_device_t* dev,
502 buffer_handle_t handle)
504 D("%s: start", __FUNCTION__);
505 cb_handle_t *cb = (cb_handle_t *)handle;
506 if (!cb_handle_t::validate((cb_handle_t*)cb)) {
507 ERR("gralloc_free: invalid handle");
511 if (cb->hostHandle != 0) {
512 DEFINE_AND_VALIDATE_HOST_CONNECTION;
513 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
514 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
518 // detach and unmap ashmem area if present
521 if (cb->ashmemSize > 0 && cb->ashmemBase) {
522 munmap((void *)cb->ashmemBase, cb->ashmemSize);
527 D("%s: unmap and free dma fd %d\n", __FUNCTION__, cb->dmafd);
528 cb->goldfish_dma.fd = cb->dmafd;
529 if (cb->ashmemSize > 0 && cb->ashmemBase) {
530 goldfish_dma_unmap(&cb->goldfish_dma);
532 goldfish_dma_free(&cb->goldfish_dma);
533 D("%s: closed dma fd %d\n", __FUNCTION__, cb->dmafd);
536 D("%s: done", __FUNCTION__);
537 // remove it from the allocated list
538 gralloc_device_t *grdev = (gralloc_device_t *)dev;
539 pthread_mutex_lock(&grdev->lock);
540 AllocListNode *n = grdev->allocListHead;
541 while( n && n->handle != cb ) {
545 // buffer found on list - remove it from list
547 n->next->prev = n->prev;
550 n->prev->next = n->next;
553 grdev->allocListHead = n->next;
558 pthread_mutex_unlock(&grdev->lock);
562 D("%s: exit", __FUNCTION__);
566 static int gralloc_device_close(struct hw_device_t *dev)
568 gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
571 // free still allocated buffers
572 while( d->allocListHead != NULL ) {
573 gralloc_free(&d->device, d->allocListHead->handle);
582 static int fb_compositionComplete(struct framebuffer_device_t* dev)
590 // Framebuffer device functions
592 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
594 fb_device_t *fbdev = (fb_device_t *)dev;
595 cb_handle_t *cb = (cb_handle_t *)buffer;
597 if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) {
601 // Make sure we have host connection
602 DEFINE_AND_VALIDATE_HOST_CONNECTION;
604 // increment the post count of the buffer
605 intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
607 // This should not happen
612 // send post request to host
613 rcEnc->rcFBPost(rcEnc, cb->hostHandle);
619 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
620 int l, int t, int w, int h)
622 fb_device_t *fbdev = (fb_device_t *)dev;
633 // Make sure we have host connection
634 DEFINE_AND_VALIDATE_HOST_CONNECTION;
636 // send request to host
637 // TODO: XXX - should be implemented
643 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
646 fb_device_t *fbdev = (fb_device_t *)dev;
652 // Make sure we have host connection
653 DEFINE_AND_VALIDATE_HOST_CONNECTION;
655 // send request to host
656 rcEnc->rcFBSetSwapInterval(rcEnc, interval);
662 static int fb_close(struct hw_device_t *dev)
664 fb_device_t *fbdev = (fb_device_t *)dev;
673 // gralloc module functions - refcount + locking interface
675 static int gralloc_register_buffer(gralloc_module_t const* module,
676 buffer_handle_t handle)
678 D("%s: start", __FUNCTION__);
679 pthread_once(&sFallbackOnce, fallback_init);
680 if (sFallback != NULL) {
681 return sFallback->registerBuffer(sFallback, handle);
684 D("gralloc_register_buffer(%p) called", handle);
686 private_module_t *gr = (private_module_t *)module;
687 cb_handle_t *cb = (cb_handle_t *)handle;
688 if (!gr || !cb_handle_t::validate(cb)) {
689 ERR("gralloc_register_buffer(%p): invalid buffer", cb);
693 if (cb->hostHandle != 0) {
694 DEFINE_AND_VALIDATE_HOST_CONNECTION;
695 D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
696 rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
700 // if the color buffer has ashmem region and it is not mapped in this
701 // process map it now.
703 if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
705 int err = map_buffer(cb, &vaddr);
707 ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
710 cb->mappedPid = getpid();
711 D("%s: checking to map goldfish dma", __FUNCTION__);
713 D("%s: attempting to goldfish dma mmap. cbfd %d dmafd1 %d dmafd2 %d", __FUNCTION__, cb->fd, cb->goldfish_dma.fd, cb->dmafd);
714 D("cxt=%p curr pid %d mapped pid %d",
718 if (cb->goldfish_dma.fd != cb->dmafd) {
719 cb->goldfish_dma.fd = cb->dmafd;
721 goldfish_dma_map(&cb->goldfish_dma);
728 static int gralloc_unregister_buffer(gralloc_module_t const* module,
729 buffer_handle_t handle)
731 D("%s: call", __FUNCTION__);
732 if (sFallback != NULL) {
733 return sFallback->unregisterBuffer(sFallback, handle);
736 private_module_t *gr = (private_module_t *)module;
737 cb_handle_t *cb = (cb_handle_t *)handle;
738 if (!gr || !cb_handle_t::validate(cb)) {
739 ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
743 if (cb->hostHandle != 0) {
744 DEFINE_AND_VALIDATE_HOST_CONNECTION;
745 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
746 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
750 // unmap ashmem region if it was previously mapped in this process
751 // (through register_buffer)
753 if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
754 DEFINE_AND_VALIDATE_HOST_CONNECTION;
756 int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
758 ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
763 D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
765 cb->goldfish_dma.fd = cb->dmafd;
766 D("%s: Unmap dma fd %d (%d)", __FUNCTION__, cb->dmafd, cb->goldfish_dma.fd);
767 goldfish_dma_unmap(&cb->goldfish_dma);
772 D("gralloc_unregister_buffer(%p) done\n", cb);
778 static int gralloc_lock(gralloc_module_t const* module,
779 buffer_handle_t handle, int usage,
780 int l, int t, int w, int h,
783 if (sFallback != NULL) {
784 return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
787 private_module_t *gr = (private_module_t *)module;
788 cb_handle_t *cb = (cb_handle_t *)handle;
789 if (!gr || !cb_handle_t::validate(cb)) {
790 ALOGE("gralloc_lock bad handle\n");
795 // 1. cannot be locked for hw access
796 // 2. lock for either sw read or write.
797 // 3. locked sw access must match usage during alloc time.
798 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
799 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
800 bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE);
801 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
802 #if PLATFORM_SDK_VERSION >= 17
803 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
804 bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
805 #else // PLATFORM_SDK_VERSION
806 bool hw_cam_write = false;
807 bool hw_cam_read = false;
808 #endif // PLATFORM_SDK_VERSION
810 #if PLATFORM_SDK_VERSION >= 15
811 bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
812 #else // PLATFORM_SDK_VERSION
813 bool hw_vid_enc_read = false;
814 #endif // PLATFORM_SDK_VERSION
816 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
818 #if PLATFORM_SDK_VERSION >= 15
820 // a buffer was created for GRALLOC_USAGE_HW_VIDEO_ENCODER usage but
821 // later a software encoder is reading this buffer: this is actually
823 sw_read_allowed = sw_read_allowed || (cb->usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
824 #endif // PLATFORM_SDK_VERSION >= 15
826 bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
828 if ( (hw_read || hw_write) ||
829 (!sw_read && !sw_write &&
830 !hw_cam_write && !hw_cam_read &&
832 (sw_read && !sw_read_allowed) ||
833 (sw_write && !sw_write_allowed) ) {
834 ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage,
836 //This is not exactly an error and loose it up.
841 intptr_t postCount = 0;
842 void *cpu_addr = NULL;
845 // make sure ashmem area is mapped if needed
847 if (cb->canBePosted() || sw_read || sw_write ||
848 hw_cam_write || hw_cam_read ||
850 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
854 if (cb->canBePosted()) {
855 postCount = *((intptr_t *)cb->ashmemBase);
856 cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t));
859 cpu_addr = (void *)(cb->ashmemBase);
863 if (cb->hostHandle) {
864 // Make sure we have host connection
865 DEFINE_AND_VALIDATE_HOST_CONNECTION;
868 // flush color buffer write cache on host and get its sync status.
870 int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
873 if (hostSyncStatus < 0) {
874 // host failed the color buffer sync - probably since it was already
875 // locked for write access. fail the lock.
876 ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n",
882 void* rgb_addr = cpu_addr;
884 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
885 cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
886 // We are using RGB88
887 tmpBuf = new char[cb->width * cb->height * 3];
890 D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height);
891 rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
892 0, 0, cb->width, cb->height, cb->glFormat, cb->glType, rgb_addr);
894 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
895 rgb888_to_yv12((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
896 } else if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
897 rgb888_to_yuv420p((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
905 // is virtual address required ?
907 if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) {
911 if (sw_write || hw_cam_write) {
913 // Keep locked region if locked for s/w write access.
918 cb->lockedHeight = h;
921 DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p",
922 vaddr, vaddr ? *vaddr : 0, usage, cpu_addr);
927 static int gralloc_unlock(gralloc_module_t const* module,
928 buffer_handle_t handle)
930 if (sFallback != NULL) {
931 return sFallback->unlock(sFallback, handle);
934 private_module_t *gr = (private_module_t *)module;
935 cb_handle_t *cb = (cb_handle_t *)handle;
936 if (!gr || !cb_handle_t::validate(cb)) {
937 ALOGD("%s: invalid gr or cb handle. -EINVAL", __FUNCTION__);
942 // if buffer was locked for s/w write, we need to update the host with
945 if (cb->hostHandle) {
947 // Make sure we have host connection
948 DEFINE_AND_VALIDATE_HOST_CONNECTION;
951 if (cb->canBePosted()) {
952 cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
955 cpu_addr = (void *)(cb->ashmemBase);
958 char* rgb_addr = (char *)cpu_addr;
959 if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
960 updateHostColorBuffer(cb, true, rgb_addr);
963 updateHostColorBuffer(cb, false, rgb_addr);
966 DD("gralloc_unlock success. cpu_addr: %p", cpu_addr);
969 cb->lockedWidth = cb->lockedHeight = 0;
973 #if PLATFORM_SDK_VERSION >= 18
974 static int gralloc_lock_ycbcr(gralloc_module_t const* module,
975 buffer_handle_t handle, int usage,
976 int l, int t, int w, int h,
977 android_ycbcr *ycbcr)
979 // Not supporting fallback module for YCbCr
980 if (sFallback != NULL) {
981 ALOGD("%s: has fallback, return -EINVAL", __FUNCTION__);
986 ALOGE("%s: got NULL ycbcr struct! -EINVAL", __FUNCTION__);
990 private_module_t *gr = (private_module_t *)module;
991 cb_handle_t *cb = (cb_handle_t *)handle;
992 if (!gr || !cb_handle_t::validate(cb)) {
993 ALOGE("%s: bad colorbuffer handle. -EINVAL", __FUNCTION__);
997 if (cb->frameworkFormat != HAL_PIXEL_FORMAT_YV12 &&
998 cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888) {
999 ALOGE("gralloc_lock_ycbcr can only be used with "
1000 "HAL_PIXEL_FORMAT_YCbCr_420_888 or HAL_PIXEL_FORMAT_YV12, got %x instead. "
1002 cb->frameworkFormat);
1006 // Make sure memory is mapped, get address
1007 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
1008 ALOGD("%s: ashmembase not mapped. -EACCESS", __FUNCTION__);
1012 uint8_t *cpu_addr = NULL;
1014 if (cb->canBePosted()) {
1015 cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int));
1018 cpu_addr = (uint8_t *)(cb->ashmemBase);
1021 // Calculate offsets to underlying YUV data
1030 switch (cb->format) {
1031 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1032 yStride = cb->width;
1033 cStride = cb->width;
1035 vOffset = yStride * cb->height;
1036 uOffset = vOffset + 1;
1039 case HAL_PIXEL_FORMAT_YV12:
1040 // https://developer.android.com/reference/android/graphics/ImageFormat.html#YV12
1042 yStride = (cb->width + (align -1)) & ~(align-1);
1043 cStride = (yStride / 2 + (align - 1)) & ~(align-1);
1045 cSize = cStride * cb->height/2;
1046 vOffset = yStride * cb->height;
1047 uOffset = vOffset + cSize;
1050 case HAL_PIXEL_FORMAT_YCbCr_420_888:
1052 yStride = cb->width;
1053 cStride = yStride / 2;
1055 cSize = cStride * cb->height/2;
1056 uOffset = yStride * cb->height;
1057 vOffset = uOffset + cSize;
1061 ALOGE("gralloc_lock_ycbcr unexpected internal format %x",
1066 ycbcr->y = cpu_addr + yOffset;
1067 ycbcr->cb = cpu_addr + uOffset;
1068 ycbcr->cr = cpu_addr + vOffset;
1069 ycbcr->ystride = yStride;
1070 ycbcr->cstride = cStride;
1071 ycbcr->chroma_step = cStep;
1073 // Zero out reserved fields
1074 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
1077 // Keep locked region if locked for s/w write access.
1081 cb->lockedWidth = w;
1082 cb->lockedHeight = h;
1084 DD("gralloc_lock_ycbcr success. usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, "
1085 ".ystride: %d , .cstride: %d, .chroma_step: %d", usage,
1086 ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride,
1087 ycbcr->chroma_step);
1091 #endif // PLATFORM_SDK_VERSION >= 18
1093 static int gralloc_device_open(const hw_module_t* module,
1095 hw_device_t** device)
1097 int status = -EINVAL;
1099 D("gralloc_device_open %s\n", name);
1101 pthread_once( &sFallbackOnce, fallback_init );
1102 if (sFallback != NULL) {
1103 return sFallback->common.methods->open(&sFallback->common, name, device);
1106 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
1108 // Create host connection and keep it in the TLS.
1109 // return error if connection with host can not be established
1110 HostConnection *hostCon = HostConnection::get();
1112 ALOGE("gralloc: failed to get host connection while opening %s\n", name);
1117 // Allocate memory for the gralloc device (alloc interface)
1119 gralloc_device_t *dev;
1120 dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t));
1125 // Initialize our device structure
1127 dev->device.common.tag = HARDWARE_DEVICE_TAG;
1128 dev->device.common.version = 0;
1129 dev->device.common.module = const_cast<hw_module_t*>(module);
1130 dev->device.common.close = gralloc_device_close;
1132 dev->device.alloc = gralloc_alloc;
1133 dev->device.free = gralloc_free;
1134 dev->allocListHead = NULL;
1135 pthread_mutex_init(&dev->lock, NULL);
1137 *device = &dev->device.common;
1140 else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
1142 // return error if connection with host can not be established
1143 DEFINE_AND_VALIDATE_HOST_CONNECTION;
1146 // Query the host for Framebuffer attributes
1148 D("gralloc: query Frabuffer attribs\n");
1149 EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH);
1150 D("gralloc: width=%d\n", width);
1151 EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT);
1152 D("gralloc: height=%d\n", height);
1153 EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI);
1154 D("gralloc: xdpi=%d\n", xdpi);
1155 EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI);
1156 D("gralloc: ydpi=%d\n", ydpi);
1157 EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS);
1158 D("gralloc: fps=%d\n", fps);
1159 EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL);
1160 D("gralloc: min_swap=%d\n", min_si);
1161 EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL);
1162 D("gralloc: max_swap=%d\n", max_si);
1165 // Allocate memory for the framebuffer device
1168 dev = (fb_device_t*)malloc(sizeof(fb_device_t));
1172 memset(dev, 0, sizeof(fb_device_t));
1174 // Initialize our device structure
1176 dev->device.common.tag = HARDWARE_DEVICE_TAG;
1177 dev->device.common.version = 0;
1178 dev->device.common.module = const_cast<hw_module_t*>(module);
1179 dev->device.common.close = fb_close;
1180 dev->device.setSwapInterval = fb_setSwapInterval;
1181 dev->device.post = fb_post;
1182 dev->device.setUpdateRect = 0; //fb_setUpdateRect;
1183 dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy
1185 const_cast<uint32_t&>(dev->device.flags) = 0;
1186 const_cast<uint32_t&>(dev->device.width) = width;
1187 const_cast<uint32_t&>(dev->device.height) = height;
1188 const_cast<int&>(dev->device.stride) = width;
1189 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888;
1190 const_cast<float&>(dev->device.xdpi) = xdpi;
1191 const_cast<float&>(dev->device.ydpi) = ydpi;
1192 const_cast<float&>(dev->device.fps) = fps;
1193 const_cast<int&>(dev->device.minSwapInterval) = min_si;
1194 const_cast<int&>(dev->device.maxSwapInterval) = max_si;
1195 *device = &dev->device.common;
1204 // define the HMI symbol - our module interface
1206 static struct hw_module_methods_t gralloc_module_methods = {
1207 open: gralloc_device_open
1210 struct private_module_t HAL_MODULE_INFO_SYM = {
1213 tag: HARDWARE_MODULE_TAG,
1214 #if PLATFORM_SDK_VERSION >= 18
1215 module_api_version: GRALLOC_MODULE_API_VERSION_0_2,
1217 #elif PLATFORM_SDK_VERSION >= 16
1218 module_api_version: 1,
1220 #else // PLATFORM_SDK_VERSION
1223 #endif // PLATFORM_SDK_VERSION
1224 id: GRALLOC_HARDWARE_MODULE_ID,
1225 name: "Graphics Memory Allocator Module",
1226 author: "The Android Open Source Project",
1227 methods: &gralloc_module_methods,
1231 registerBuffer: gralloc_register_buffer,
1232 unregisterBuffer: gralloc_unregister_buffer,
1234 unlock: gralloc_unlock,
1236 #if PLATFORM_SDK_VERSION >= 18
1237 lock_ycbcr: gralloc_lock_ycbcr,
1238 #endif // PLATFORM_SDK_VERSION >= 18
1242 /* This function is called once to detect whether the emulator supports
1243 * GPU emulation (this is done by looking at the qemu.gles kernel
1244 * parameter, which must be == 1 if this is the case).
1246 * If not, then load gralloc.default instead as a fallback.
1251 char prop[PROPERTY_VALUE_MAX];
1254 // qemu.gles=0 -> no GLES 2.x support (only 1.x through software).
1255 // qemu.gles=1 -> host-side GPU emulation through EmuGL
1256 // qemu.gles=2 -> guest-side GPU emulation.
1257 property_get("ro.kernel.qemu.gles", prop, "0");
1258 if (atoi(prop) == 1) {
1261 ALOGD("Emulator without host-side GPU emulation detected.");
1263 module = dlopen("/vendor/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1265 module = dlopen("/vendor/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1267 if (module != NULL) {
1268 sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
1269 if (sFallback == NULL) {
1273 if (sFallback == NULL) {
1274 ALOGE("Could not find software fallback module!?");