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 "HostConnection.h"
27 #include <cutils/log.h>
28 #include <cutils/properties.h>
30 /* Set to 1 or 2 to enable debug traces */
34 # define D(...) ALOGD(__VA_ARGS__)
36 # define D(...) ((void)0)
40 # define DD(...) ALOGD(__VA_ARGS__)
42 # define DD(...) ((void)0)
45 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
47 // our private gralloc module structure
49 struct private_module_t {
50 gralloc_module_t base;
53 /* If not NULL, this is a pointer to the fallback module.
54 * This really is gralloc.default, which we'll use if we detect
55 * that the emulator we're running in does not support GPU emulation.
57 static gralloc_module_t* sFallback;
58 static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT;
60 static void fallback_init(void); // forward
63 typedef struct _alloc_list_node {
64 buffer_handle_t handle;
65 _alloc_list_node *next;
66 _alloc_list_node *prev;
70 // Our gralloc device structure (alloc interface)
72 struct gralloc_device_t {
73 alloc_device_t device;
75 AllocListNode *allocListHead; // double linked list of allocated buffers
80 // Our framebuffer device structure
83 framebuffer_device_t device;
86 static int map_buffer(cb_handle_t *cb, void **vaddr)
88 if (cb->fd < 0 || cb->ashmemSize <= 0) {
92 void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
93 MAP_SHARED, cb->fd, 0);
94 if (addr == MAP_FAILED) {
98 cb->ashmemBase = intptr_t(addr);
99 cb->ashmemBasePid = getpid();
105 #define DEFINE_HOST_CONNECTION \
106 HostConnection *hostCon = HostConnection::get(); \
107 renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
109 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
110 HostConnection *hostCon = HostConnection::get(); \
112 ALOGE("gralloc: Failed to get host connection\n"); \
115 renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \
117 ALOGE("gralloc: Failed to get renderControl encoder context\n"); \
123 // gralloc device functions (alloc interface)
125 static int gralloc_alloc(alloc_device_t* dev,
126 int w, int h, int format, int usage,
127 buffer_handle_t* pHandle, int* pStride)
129 D("gralloc_alloc w=%d h=%d usage=0x%x\n", w, h, usage);
131 gralloc_device_t *grdev = (gralloc_device_t *)dev;
132 if (!grdev || !pHandle || !pStride) {
133 ALOGE("gralloc_alloc: Bad inputs (grdev: %p, pHandle: %p, pStride: %p",
134 grdev, pHandle, pStride);
139 // Note: in screen capture mode, both sw_write and hw_write will be on
140 // and this is a valid usage
142 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
143 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
144 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
145 bool hw_cam_write = usage & GRALLOC_USAGE_HW_CAMERA_WRITE;
146 bool hw_cam_read = usage & GRALLOC_USAGE_HW_CAMERA_READ;
147 bool hw_vid_enc_read = usage & GRALLOC_USAGE_HW_VIDEO_ENCODER;
149 // Keep around original requested format for later validation
150 int frameworkFormat = format;
151 // Pick the right concrete pixel format given the endpoints as encoded in
152 // the usage bits. Every end-point pair needs explicit listing here.
153 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
154 // Camera as producer
155 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
156 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
157 // Camera-to-display is RGBA
158 format = HAL_PIXEL_FORMAT_RGBA_8888;
159 } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
160 // Camera-to-encoder is NV21
161 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
162 } else if ((usage & GRALLOC_USAGE_HW_CAMERA_MASK) ==
163 GRALLOC_USAGE_HW_CAMERA_ZSL) {
164 // Camera-to-ZSL-queue is RGB_888
165 format = HAL_PIXEL_FORMAT_RGB_888;
169 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
170 ALOGE("gralloc_alloc: Requested auto format selection, "
171 "but no known format for this usage: %d x %d, usage %x",
175 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
176 // Flexible framework-accessible YUV format; map to NV21 for now
177 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
178 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
180 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
181 ALOGE("gralloc_alloc: Requested YCbCr_420_888, but no known "
182 "specific format for this usage: %d x %d, usage %x",
186 bool yuv_format = false;
197 case HAL_PIXEL_FORMAT_RGBA_8888:
198 case HAL_PIXEL_FORMAT_RGBX_8888:
199 case HAL_PIXEL_FORMAT_BGRA_8888:
202 glType = GL_UNSIGNED_BYTE;
204 case HAL_PIXEL_FORMAT_RGB_888:
207 glType = GL_UNSIGNED_BYTE;
209 case HAL_PIXEL_FORMAT_RGB_565:
212 glType = GL_UNSIGNED_SHORT_5_6_5;
214 case HAL_PIXEL_FORMAT_RAW16:
215 case HAL_PIXEL_FORMAT_Y16:
218 if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) {
219 // Raw sensor data or Y16 only goes between camera and CPU
222 // Not expecting to actually create any GL surfaces for this
223 glFormat = GL_LUMINANCE;
224 glType = GL_UNSIGNED_SHORT;
226 case HAL_PIXEL_FORMAT_BLOB:
228 if (! (sw_read && hw_cam_write) ) {
229 // Blob data cannot be used by HW other than camera emulator
232 // Not expecting to actually create any GL surfaces for this
233 glFormat = GL_LUMINANCE;
234 glType = GL_UNSIGNED_BYTE;
236 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
238 bpp = 1; // per-channel bpp
240 // Not expecting to actually create any GL surfaces for this
242 case HAL_PIXEL_FORMAT_YV12:
244 bpp = 1; // per-channel bpp
246 // Not expecting to actually create any GL surfaces for this
249 ALOGE("gralloc_alloc: Unknown format %d", format);
253 if (usage & GRALLOC_USAGE_HW_FB) {
254 // keep space for postCounter
255 ashmem_size += sizeof(uint32_t);
258 if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
259 // keep space for image on guest memory if SW access is needed
260 // or if the camera is doing writing
262 size_t yStride = (w*bpp + (align - 1)) & ~(align-1);
263 size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1);
264 size_t uvHeight = h / 2;
265 ashmem_size += yStride * h + 2 * (uvHeight * uvStride);
266 stride = yStride / bpp;
268 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
269 ashmem_size += (bpr * h);
274 D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format,
275 ashmem_size, stride, gettid());
278 // Allocate space in ashmem if needed
281 if (ashmem_size > 0) {
282 // round to page size;
283 ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
285 fd = ashmem_create_region("gralloc-buffer", ashmem_size);
287 ALOGE("gralloc_alloc failed to create ashmem region: %s\n",
293 cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
294 w, h, frameworkFormat, format,
297 if (ashmem_size > 0) {
299 // map ashmem region if exist
302 int err = map_buffer(cb, &vaddr);
313 // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
314 // Only do this for some h/w usages, not all.
315 // Also do this if we need to read from the surface, in this case the
316 // rendering will still happen on the host but we also need to be able to
317 // read back from the color buffer, which requires that there is a buffer
319 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
320 GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
321 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
322 DEFINE_HOST_CONNECTION;
323 if (hostCon && rcEnc) {
324 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
325 D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
328 if (!cb->hostHandle) {
329 // Could not create colorbuffer on host !!!
337 // alloc succeeded - insert the allocated handle to the allocated list
339 AllocListNode *node = new AllocListNode();
340 pthread_mutex_lock(&grdev->lock);
342 node->next = grdev->allocListHead;
344 if (grdev->allocListHead) {
345 grdev->allocListHead->prev = node;
347 grdev->allocListHead = node;
348 pthread_mutex_unlock(&grdev->lock);
351 if (frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
359 static int gralloc_free(alloc_device_t* dev,
360 buffer_handle_t handle)
362 const cb_handle_t *cb = (const cb_handle_t *)handle;
363 if (!cb_handle_t::validate((cb_handle_t*)cb)) {
364 ERR("gralloc_free: invalid handle");
368 if (cb->hostHandle != 0) {
369 DEFINE_AND_VALIDATE_HOST_CONNECTION;
370 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
371 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
375 // detach and unmap ashmem area if present
378 if (cb->ashmemSize > 0 && cb->ashmemBase) {
379 munmap((void *)cb->ashmemBase, cb->ashmemSize);
384 // remove it from the allocated list
385 gralloc_device_t *grdev = (gralloc_device_t *)dev;
386 pthread_mutex_lock(&grdev->lock);
387 AllocListNode *n = grdev->allocListHead;
388 while( n && n->handle != cb ) {
392 // buffer found on list - remove it from list
394 n->next->prev = n->prev;
397 n->prev->next = n->next;
400 grdev->allocListHead = n->next;
405 pthread_mutex_unlock(&grdev->lock);
412 static int gralloc_device_close(struct hw_device_t *dev)
414 gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
417 // free still allocated buffers
418 while( d->allocListHead != NULL ) {
419 gralloc_free(&d->device, d->allocListHead->handle);
428 static int fb_compositionComplete(struct framebuffer_device_t* dev)
436 // Framebuffer device functions
438 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
440 fb_device_t *fbdev = (fb_device_t *)dev;
441 cb_handle_t *cb = (cb_handle_t *)buffer;
443 if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) {
447 // Make sure we have host connection
448 DEFINE_AND_VALIDATE_HOST_CONNECTION;
450 // increment the post count of the buffer
451 intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
453 // This should not happen
458 // send post request to host
459 rcEnc->rcFBPost(rcEnc, cb->hostHandle);
465 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
466 int l, int t, int w, int h)
468 fb_device_t *fbdev = (fb_device_t *)dev;
479 // Make sure we have host connection
480 DEFINE_AND_VALIDATE_HOST_CONNECTION;
482 // send request to host
483 // TODO: XXX - should be implemented
489 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
492 fb_device_t *fbdev = (fb_device_t *)dev;
498 // Make sure we have host connection
499 DEFINE_AND_VALIDATE_HOST_CONNECTION;
501 // send request to host
502 rcEnc->rcFBSetSwapInterval(rcEnc, interval);
508 static int fb_close(struct hw_device_t *dev)
510 fb_device_t *fbdev = (fb_device_t *)dev;
519 // gralloc module functions - refcount + locking interface
521 static int gralloc_register_buffer(gralloc_module_t const* module,
522 buffer_handle_t handle)
524 pthread_once(&sFallbackOnce, fallback_init);
525 if (sFallback != NULL) {
526 return sFallback->registerBuffer(sFallback, handle);
529 D("gralloc_register_buffer(%p) called", handle);
531 private_module_t *gr = (private_module_t *)module;
532 cb_handle_t *cb = (cb_handle_t *)handle;
533 if (!gr || !cb_handle_t::validate(cb)) {
534 ERR("gralloc_register_buffer(%p): invalid buffer", cb);
538 if (cb->hostHandle != 0) {
539 DEFINE_AND_VALIDATE_HOST_CONNECTION;
540 D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
541 rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
545 // if the color buffer has ashmem region and it is not mapped in this
546 // process map it now.
548 if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
550 int err = map_buffer(cb, &vaddr);
552 ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
555 cb->mappedPid = getpid();
561 static int gralloc_unregister_buffer(gralloc_module_t const* module,
562 buffer_handle_t handle)
564 if (sFallback != NULL) {
565 return sFallback->unregisterBuffer(sFallback, handle);
568 private_module_t *gr = (private_module_t *)module;
569 cb_handle_t *cb = (cb_handle_t *)handle;
570 if (!gr || !cb_handle_t::validate(cb)) {
571 ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
575 if (cb->hostHandle != 0) {
576 DEFINE_AND_VALIDATE_HOST_CONNECTION;
577 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
578 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
582 // unmap ashmem region if it was previously mapped in this process
583 // (through register_buffer)
585 if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
587 int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
589 ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
596 D("gralloc_unregister_buffer(%p) done\n", cb);
601 static int gralloc_lock(gralloc_module_t const* module,
602 buffer_handle_t handle, int usage,
603 int l, int t, int w, int h,
606 if (sFallback != NULL) {
607 return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
610 private_module_t *gr = (private_module_t *)module;
611 cb_handle_t *cb = (cb_handle_t *)handle;
612 if (!gr || !cb_handle_t::validate(cb)) {
613 ALOGE("gralloc_lock bad handle\n");
618 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
619 ALOGE("gralloc_lock can't be used with YCbCr_420_888 format");
624 // 1. cannot be locked for hw access
625 // 2. lock for either sw read or write.
626 // 3. locked sw access must match usage during alloc time.
627 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
628 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
629 bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE);
630 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
631 bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
632 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
633 bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
634 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
635 bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
637 if ( (hw_read || hw_write) ||
638 (!sw_read && !sw_write &&
639 !hw_cam_write && !hw_cam_read &&
641 (sw_read && !sw_read_allowed) ||
642 (sw_write && !sw_write_allowed) ) {
643 ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage,
648 intptr_t postCount = 0;
649 void *cpu_addr = NULL;
652 // make sure ashmem area is mapped if needed
654 if (cb->canBePosted() || sw_read || sw_write ||
655 hw_cam_write || hw_cam_read ||
657 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
661 if (cb->canBePosted()) {
662 postCount = *((intptr_t *)cb->ashmemBase);
663 cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t));
666 cpu_addr = (void *)(cb->ashmemBase);
670 if (cb->hostHandle) {
671 // Make sure we have host connection
672 DEFINE_AND_VALIDATE_HOST_CONNECTION;
675 // flush color buffer write cache on host and get its sync status.
677 int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
680 if (hostSyncStatus < 0) {
681 // host failed the color buffer sync - probably since it was already
682 // locked for write access. fail the lock.
683 ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n",
689 D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height);
690 rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
691 0, 0, cb->width, cb->height, cb->glFormat, cb->glType, cpu_addr);
696 // is virtual address required ?
698 if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) {
702 if (sw_write || hw_cam_write) {
704 // Keep locked region if locked for s/w write access.
709 cb->lockedHeight = h;
712 DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p",
713 vaddr, vaddr ? *vaddr : 0, usage, cpu_addr);
718 static int gralloc_unlock(gralloc_module_t const* module,
719 buffer_handle_t handle)
721 if (sFallback != NULL) {
722 return sFallback->unlock(sFallback, handle);
725 private_module_t *gr = (private_module_t *)module;
726 cb_handle_t *cb = (cb_handle_t *)handle;
727 if (!gr || !cb_handle_t::validate(cb)) {
732 // if buffer was locked for s/w write, we need to update the host with
735 if (cb->hostHandle) {
737 // Make sure we have host connection
738 DEFINE_AND_VALIDATE_HOST_CONNECTION;
741 if (cb->canBePosted()) {
742 cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
745 cpu_addr = (void *)(cb->ashmemBase);
748 if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
749 int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3;
750 char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp];
752 int dst_line_len = cb->lockedWidth * bpp;
753 int src_line_len = cb->width * bpp;
754 char *src = (char *)cpu_addr + cb->lockedTop*src_line_len + cb->lockedLeft*bpp;
756 for (int y=0; y<cb->lockedHeight; y++) {
757 memcpy(dst, src, dst_line_len);
762 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
763 cb->lockedLeft, cb->lockedTop,
764 cb->lockedWidth, cb->lockedHeight,
765 cb->glFormat, cb->glType,
771 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0,
772 cb->width, cb->height,
773 cb->glFormat, cb->glType,
778 cb->lockedWidth = cb->lockedHeight = 0;
782 static int gralloc_lock_ycbcr(gralloc_module_t const* module,
783 buffer_handle_t handle, int usage,
784 int l, int t, int w, int h,
785 android_ycbcr *ycbcr)
787 // Not supporting fallback module for YCbCr
788 if (sFallback != NULL) {
793 ALOGE("gralloc_lock_ycbcr got NULL ycbcr struct");
797 private_module_t *gr = (private_module_t *)module;
798 cb_handle_t *cb = (cb_handle_t *)handle;
799 if (!gr || !cb_handle_t::validate(cb)) {
800 ALOGE("gralloc_lock_ycbcr bad handle\n");
804 if (cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888) {
805 ALOGE("gralloc_lock_ycbcr can only be used with "
806 "HAL_PIXEL_FORMAT_YCbCr_420_888, got %x instead",
807 cb->frameworkFormat);
812 // For now, only allow camera write, software read.
813 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
814 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
815 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
817 if ( (!hw_cam_write && !sw_read) ||
818 (sw_read && !sw_read_allowed) ) {
819 ALOGE("gralloc_lock_ycbcr usage mismatch usage:0x%x cb->usage:0x%x\n",
824 // Make sure memory is mapped, get address
825 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
829 uint8_t *cpu_addr = NULL;
831 if (cb->canBePosted()) {
832 cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int));
835 cpu_addr = (uint8_t *)(cb->ashmemBase);
838 // Calculate offsets to underlying YUV data
845 switch (cb->format) {
846 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
850 vOffset = yStride * cb->height;
851 uOffset = vOffset + 1;
855 ALOGE("gralloc_lock_ycbcr unexpected internal format %x",
860 ycbcr->y = cpu_addr + yOffset;
861 ycbcr->cb = cpu_addr + uOffset;
862 ycbcr->cr = cpu_addr + vOffset;
863 ycbcr->ystride = yStride;
864 ycbcr->cstride = cStride;
865 ycbcr->chroma_step = cStep;
867 // Zero out reserved fields
868 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
871 // Keep locked region if locked for s/w write access.
876 cb->lockedHeight = h;
878 DD("gralloc_lock_ycbcr success. usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, "
879 ".ystride: %d , .cstride: %d, .chroma_step: %d", usage,
880 ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride,
886 static int gralloc_device_open(const hw_module_t* module,
888 hw_device_t** device)
890 int status = -EINVAL;
892 D("gralloc_device_open %s\n", name);
894 pthread_once( &sFallbackOnce, fallback_init );
895 if (sFallback != NULL) {
896 return sFallback->common.methods->open(&sFallback->common, name, device);
899 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
901 // Create host connection and keep it in the TLS.
902 // return error if connection with host can not be established
903 HostConnection *hostCon = HostConnection::get();
905 ALOGE("gralloc: failed to get host connection while opening %s\n", name);
910 // Allocate memory for the gralloc device (alloc interface)
912 gralloc_device_t *dev;
913 dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t));
918 // Initialize our device structure
920 dev->device.common.tag = HARDWARE_DEVICE_TAG;
921 dev->device.common.version = 0;
922 dev->device.common.module = const_cast<hw_module_t*>(module);
923 dev->device.common.close = gralloc_device_close;
925 dev->device.alloc = gralloc_alloc;
926 dev->device.free = gralloc_free;
927 dev->allocListHead = NULL;
928 pthread_mutex_init(&dev->lock, NULL);
930 *device = &dev->device.common;
933 else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
935 // return error if connection with host can not be established
936 DEFINE_AND_VALIDATE_HOST_CONNECTION;
939 // Query the host for Framebuffer attributes
941 D("gralloc: query Frabuffer attribs\n");
942 EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH);
943 D("gralloc: width=%d\n", width);
944 EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT);
945 D("gralloc: height=%d\n", height);
946 EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI);
947 D("gralloc: xdpi=%d\n", xdpi);
948 EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI);
949 D("gralloc: ydpi=%d\n", ydpi);
950 EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS);
951 D("gralloc: fps=%d\n", fps);
952 EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL);
953 D("gralloc: min_swap=%d\n", min_si);
954 EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL);
955 D("gralloc: max_swap=%d\n", max_si);
958 // Allocate memory for the framebuffer device
961 dev = (fb_device_t*)malloc(sizeof(fb_device_t));
965 memset(dev, 0, sizeof(fb_device_t));
967 // Initialize our device structure
969 dev->device.common.tag = HARDWARE_DEVICE_TAG;
970 dev->device.common.version = 0;
971 dev->device.common.module = const_cast<hw_module_t*>(module);
972 dev->device.common.close = fb_close;
973 dev->device.setSwapInterval = fb_setSwapInterval;
974 dev->device.post = fb_post;
975 dev->device.setUpdateRect = 0; //fb_setUpdateRect;
976 dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy
978 const_cast<uint32_t&>(dev->device.flags) = 0;
979 const_cast<uint32_t&>(dev->device.width) = width;
980 const_cast<uint32_t&>(dev->device.height) = height;
981 const_cast<int&>(dev->device.stride) = width;
982 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888;
983 const_cast<float&>(dev->device.xdpi) = xdpi;
984 const_cast<float&>(dev->device.ydpi) = ydpi;
985 const_cast<float&>(dev->device.fps) = fps;
986 const_cast<int&>(dev->device.minSwapInterval) = min_si;
987 const_cast<int&>(dev->device.maxSwapInterval) = max_si;
988 *device = &dev->device.common;
997 // define the HMI symbol - our module interface
999 static struct hw_module_methods_t gralloc_module_methods = {
1000 open: gralloc_device_open
1003 struct private_module_t HAL_MODULE_INFO_SYM = {
1006 tag: HARDWARE_MODULE_TAG,
1007 module_api_version: GRALLOC_MODULE_API_VERSION_0_2,
1009 id: GRALLOC_HARDWARE_MODULE_ID,
1010 name: "Graphics Memory Allocator Module",
1011 author: "The Android Open Source Project",
1012 methods: &gralloc_module_methods,
1016 registerBuffer: gralloc_register_buffer,
1017 unregisterBuffer: gralloc_unregister_buffer,
1019 unlock: gralloc_unlock,
1021 lock_ycbcr: gralloc_lock_ycbcr,
1025 /* This function is called once to detect whether the emulator supports
1026 * GPU emulation (this is done by looking at the qemu.gles kernel
1027 * parameter, which must be == 1 if this is the case).
1029 * If not, then load gralloc.default instead as a fallback.
1034 char prop[PROPERTY_VALUE_MAX];
1037 // qemu.gles=0 -> no GLES 2.x support (only 1.x through software).
1038 // qemu.gles=1 -> host-side GPU emulation through EmuGL
1039 // qemu.gles=2 -> guest-side GPU emulation.
1040 property_get("ro.kernel.qemu.gles", prop, "0");
1041 if (atoi(prop) == 1) {
1044 ALOGD("Emulator without host-side GPU emulation detected.");
1046 module = dlopen("/system/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1048 module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1050 if (module != NULL) {
1051 sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
1052 if (sFallback == NULL) {
1056 if (sFallback == NULL) {
1057 ALOGE("Could not find software fallback module!?");