OSDN Git Service

236f633c2426a5dd934419bed9aacf910c3cc6c0
[android-x86/device-generic-goldfish-opengl.git] / system / gralloc / gralloc.cpp
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 "goldfish_dma.h"
26 #include "FormatConversions.h"
27 #include "HostConnection.h"
28 #include "ProcessPipe.h"
29 #include "glUtils.h"
30 #include <cutils/log.h>
31 #include <cutils/properties.h>
32
33 /* Set to 1 or 2 to enable debug traces */
34 #define DEBUG  0
35
36 #if DEBUG >= 1
37 #  define D(...)   ALOGD(__VA_ARGS__)
38 #else
39 #  define D(...)   ((void)0)
40 #endif
41
42 #if DEBUG >= 2
43 #  define DD(...)  ALOGD(__VA_ARGS__)
44 #else
45 #  define DD(...)  ((void)0)
46 #endif
47
48 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
49
50 //
51 // our private gralloc module structure
52 //
53 struct private_module_t {
54     gralloc_module_t base;
55 };
56
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.
60  */
61 static gralloc_module_t*  sFallback;
62 static pthread_once_t     sFallbackOnce = PTHREAD_ONCE_INIT;
63
64 static void fallback_init(void);  // forward
65
66
67 typedef struct _alloc_list_node {
68     buffer_handle_t handle;
69     _alloc_list_node *next;
70     _alloc_list_node *prev;
71 } AllocListNode;
72
73 //
74 // Our gralloc device structure (alloc interface)
75 //
76 struct gralloc_device_t {
77     alloc_device_t  device;
78
79     AllocListNode *allocListHead;    // double linked list of allocated buffers
80     pthread_mutex_t lock;
81 };
82
83 //
84 // Our framebuffer device structure
85 //
86 struct fb_device_t {
87     framebuffer_device_t  device;
88 };
89
90 static int map_buffer(cb_handle_t *cb, void **vaddr)
91 {
92     if (cb->fd < 0 || cb->ashmemSize <= 0) {
93         return -EINVAL;
94     }
95
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__);
100         return -errno;
101     }
102
103     cb->ashmemBase = intptr_t(addr);
104     cb->ashmemBasePid = getpid();
105
106     *vaddr = addr;
107     return 0;
108 }
109
110 #define DEFINE_HOST_CONNECTION \
111     HostConnection *hostCon = HostConnection::get(); \
112     ExtendedRCEncoderContext *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
113
114 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
115     HostConnection *hostCon = HostConnection::get(); \
116     if (!hostCon) { \
117         ALOGE("gralloc: Failed to get host connection\n"); \
118         return -EIO; \
119     } \
120     ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder(); \
121     if (!rcEnc) { \
122         ALOGE("gralloc: Failed to get renderControl encoder context\n"); \
123         return -EIO; \
124     }
125
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
129 #endif
130
131 static void updateHostColorBuffer(cb_handle_t* cb,
132                               bool doLocked,
133                               char* pixels) {
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;
141
142     char* to_send = pixels;
143     uint32_t rgbSz = width * height * bpp;
144     uint32_t send_buffer_size = rgbSz;
145     bool is_rgb_format =
146         cb->frameworkFormat != HAL_PIXEL_FORMAT_YV12 &&
147         cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888;
148
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;
156     }
157
158     if (doLocked && is_rgb_format) {
159         copy_rgb_buffer_from_unlocked(
160                 to_send, pixels,
161                 cb->width,
162                 width, height, top, left, bpp);
163     }
164
165     if (cb->goldfish_dma.fd > 0) {
166         if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
167             get_yv12_offsets(width, height, NULL, NULL,
168                              &send_buffer_size);
169         }
170         if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
171             get_yuv420p_offsets(width, height, NULL, NULL,
172                                 &send_buffer_size);
173         }
174
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);
181     } else {
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);
186         }
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);
191         }
192         rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
193                 left, top, width, height,
194                 cb->glFormat, cb->glType, to_send);
195     }
196
197     if (convertedBuf) delete [] convertedBuf;
198 }
199
200 //
201 // gralloc device functions (alloc interface)
202 //
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)
206 {
207     D("gralloc_alloc w=%d h=%d usage=0x%x format=0x%x\n", w, h, usage, format);
208
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);
213         return -EINVAL;
214     }
215
216     //
217     // Note: in screen capture mode, both sw_write and hw_write will be on
218     // and this is a valid usage
219     //
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
235
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;
254             }
255         }
256
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",
260                     w, h, usage);
261             return -EINVAL;
262         }
263     }
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",
267                 w, h, usage);
268     }
269 #endif // PLATFORM_SDK_VERSION >= 17
270     bool yuv_format = false;
271
272     int ashmem_size = 0;
273     int stride = w;
274
275     GLenum glFormat = 0;
276     GLenum glType = 0;
277     EmulatorFrameworkFormat selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_GL_COMPATIBLE;
278
279     int bpp = 0;
280     int align = 1;
281     switch (format) {
282         case HAL_PIXEL_FORMAT_RGBA_8888:
283         case HAL_PIXEL_FORMAT_RGBX_8888:
284         case HAL_PIXEL_FORMAT_BGRA_8888:
285             bpp = 4;
286             glFormat = GL_RGBA;
287             glType = GL_UNSIGNED_BYTE;
288             break;
289         case HAL_PIXEL_FORMAT_RGB_888:
290             bpp = 3;
291             glFormat = GL_RGB;
292             glType = GL_UNSIGNED_BYTE;
293             break;
294         case HAL_PIXEL_FORMAT_RGB_565:
295             bpp = 2;
296             glFormat = GL_RGB;
297             glType = GL_UNSIGNED_SHORT_5_6_5;
298             break;
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:
304 #endif
305             bpp = 2;
306             align = 16*bpp;
307             if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) {
308                 // Raw sensor data or Y16 only goes between camera and CPU
309                 return -EINVAL;
310             }
311             // Not expecting to actually create any GL surfaces for this
312             glFormat = GL_LUMINANCE;
313             glType = GL_UNSIGNED_SHORT;
314             break;
315 #if PLATFORM_SDK_VERSION >= 17
316         case HAL_PIXEL_FORMAT_BLOB:
317             bpp = 1;
318             if (! (sw_read && hw_cam_write) ) {
319                 // Blob data cannot be used by HW other than camera emulator
320                 return -EINVAL;
321             }
322             // Not expecting to actually create any GL surfaces for this
323             glFormat = GL_LUMINANCE;
324             glType = GL_UNSIGNED_BYTE;
325             break;
326 #endif // PLATFORM_SDK_VERSION >= 17
327         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
328             align = 1;
329             bpp = 1; // per-channel bpp
330             yuv_format = true;
331             // Not expecting to actually create any GL surfaces for this
332             break;
333         case HAL_PIXEL_FORMAT_YV12:
334             align = 16;
335             bpp = 1; // per-channel bpp
336             yuv_format = true;
337             // We are going to use RGB888 on the host
338             glFormat = GL_RGB;
339             glType = GL_UNSIGNED_BYTE;
340             selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YV12;
341             break;
342         case HAL_PIXEL_FORMAT_YCbCr_420_888:
343             ALOGD("%s: 420_888 format experimental path. "
344                   "Initialize rgb565 gl format\n", __FUNCTION__);
345             align = 1;
346             bpp = 1; // per-channel bpp
347             yuv_format = true;
348             // We are going to use RGB888 on the host
349             glFormat = GL_RGB;
350             glType = GL_UNSIGNED_BYTE;
351             selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
352             break;
353         default:
354             ALOGE("gralloc_alloc: Unknown format %d", format);
355             return -EINVAL;
356     }
357
358     if (usage & GRALLOC_USAGE_HW_FB) {
359         // keep space for postCounter
360         ashmem_size += sizeof(uint32_t);
361     }
362
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
366         if (yuv_format) {
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;
372         } else {
373             size_t bpr = (w*bpp + (align-1)) & ~(align-1);
374             ashmem_size += (bpr * h);
375             stride = bpr / bpp;
376         }
377     }
378
379     D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format,
380             ashmem_size, stride, gettid());
381
382     //
383     // Allocate space in ashmem if needed
384     //
385     int fd = -1;
386     if (ashmem_size > 0) {
387         // round to page size;
388         ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
389
390         ALOGD("%s: Creating ashmem region of size %lu\n", __FUNCTION__, ashmem_size);
391         fd = ashmem_create_region("gralloc-buffer", ashmem_size);
392         if (fd < 0) {
393             ALOGE("gralloc_alloc failed to create ashmem region: %s\n",
394                     strerror(errno));
395             return -errno;
396         }
397     }
398
399     cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
400                                       w, h, frameworkFormat, format,
401                                       glFormat, glType, selectedEmuFrameworkFormat);
402
403     DEFINE_HOST_CONNECTION;
404     if (ashmem_size > 0) {
405
406         //
407         // map ashmem region if exist
408         //
409         void *vaddr;
410         int err = map_buffer(cb, &vaddr);
411         if (err) {
412             close(fd);
413             delete cb;
414             return err;
415         }
416
417         cb->setFd(fd);
418
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);
422             if (err) {
423                 ALOGE("%s: Failed to create goldfish DMA region", __FUNCTION__);
424             } else {
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);
428             }
429         } else {
430             cb->goldfish_dma.fd = -1;
431         }
432     } else {
433         cb->goldfish_dma.fd = -1;
434     }
435
436     //
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
442     //
443     if (!yuv_format ||
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);
459                 } else {
460                     cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
461                 }
462                 D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
463             }
464
465             if (!cb->hostHandle) {
466               // Could not create colorbuffer on host !!!
467               close(fd);
468               delete cb;
469               ALOGD("%s: failed to create host cb! -EIO", __FUNCTION__);
470               return -EIO;
471             }
472         }
473     }
474
475     //
476     // alloc succeeded - insert the allocated handle to the allocated list
477     //
478     AllocListNode *node = new AllocListNode();
479     pthread_mutex_lock(&grdev->lock);
480     node->handle = cb;
481     node->next =  grdev->allocListHead;
482     node->prev =  NULL;
483     if (grdev->allocListHead) {
484         grdev->allocListHead->prev = node;
485     }
486     grdev->allocListHead = node;
487     pthread_mutex_unlock(&grdev->lock);
488
489     *pHandle = cb;
490     switch (frameworkFormat) {
491     case HAL_PIXEL_FORMAT_YCbCr_420_888:
492         *pStride = 0;
493         break;
494     default:
495         *pStride = stride;
496         break;
497     }
498     return 0;
499 }
500
501 static int gralloc_free(alloc_device_t* dev,
502                         buffer_handle_t handle)
503 {
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");
508         return -EINVAL;
509     }
510
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);
515     }
516
517     //
518     // detach and unmap ashmem area if present
519     //
520     if (cb->fd > 0) {
521         if (cb->ashmemSize > 0 && cb->ashmemBase) {
522             munmap((void *)cb->ashmemBase, cb->ashmemSize);
523         }
524         close(cb->fd);
525     }
526     if (cb->dmafd > 0) {
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);
531         }
532         goldfish_dma_free(&cb->goldfish_dma);
533         D("%s: closed dma fd %d\n", __FUNCTION__, cb->dmafd);
534     }
535
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 ) {
542         n = n->next;
543     }
544     if (n) {
545        // buffer found on list - remove it from list
546        if (n->next) {
547            n->next->prev = n->prev;
548        }
549        if (n->prev) {
550            n->prev->next = n->next;
551        }
552        else {
553            grdev->allocListHead = n->next;
554        }
555
556        delete n;
557     }
558     pthread_mutex_unlock(&grdev->lock);
559
560     delete cb;
561
562     D("%s: exit", __FUNCTION__);
563     return 0;
564 }
565
566 static int gralloc_device_close(struct hw_device_t *dev)
567 {
568     gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
569     if (d) {
570
571         // free still allocated buffers
572         while( d->allocListHead != NULL ) {
573             gralloc_free(&d->device, d->allocListHead->handle);
574         }
575
576         // free device
577         free(d);
578     }
579     return 0;
580 }
581
582 static int fb_compositionComplete(struct framebuffer_device_t* dev)
583 {
584     (void)dev;
585
586     return 0;
587 }
588
589 //
590 // Framebuffer device functions
591 //
592 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
593 {
594     fb_device_t *fbdev = (fb_device_t *)dev;
595     cb_handle_t *cb = (cb_handle_t *)buffer;
596
597     if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) {
598         return -EINVAL;
599     }
600
601     // Make sure we have host connection
602     DEFINE_AND_VALIDATE_HOST_CONNECTION;
603
604     // increment the post count of the buffer
605     intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
606     if (!postCountPtr) {
607         // This should not happen
608         return -EINVAL;
609     }
610     (*postCountPtr)++;
611
612     // send post request to host
613     rcEnc->rcFBPost(rcEnc, cb->hostHandle);
614     hostCon->flush();
615
616     return 0;
617 }
618
619 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
620         int l, int t, int w, int h)
621 {
622     fb_device_t *fbdev = (fb_device_t *)dev;
623
624     (void)l;
625     (void)t;
626     (void)w;
627     (void)h;
628
629     if (!fbdev) {
630         return -EINVAL;
631     }
632
633     // Make sure we have host connection
634     DEFINE_AND_VALIDATE_HOST_CONNECTION;
635
636     // send request to host
637     // TODO: XXX - should be implemented
638     //rcEnc->rc_XXX
639
640     return 0;
641 }
642
643 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
644             int interval)
645 {
646     fb_device_t *fbdev = (fb_device_t *)dev;
647
648     if (!fbdev) {
649         return -EINVAL;
650     }
651
652     // Make sure we have host connection
653     DEFINE_AND_VALIDATE_HOST_CONNECTION;
654
655     // send request to host
656     rcEnc->rcFBSetSwapInterval(rcEnc, interval);
657     hostCon->flush();
658
659     return 0;
660 }
661
662 static int fb_close(struct hw_device_t *dev)
663 {
664     fb_device_t *fbdev = (fb_device_t *)dev;
665
666     delete fbdev;
667
668     return 0;
669 }
670
671
672 //
673 // gralloc module functions - refcount + locking interface
674 //
675 static int gralloc_register_buffer(gralloc_module_t const* module,
676                                    buffer_handle_t handle)
677 {
678     D("%s: start", __FUNCTION__);
679     pthread_once(&sFallbackOnce, fallback_init);
680     if (sFallback != NULL) {
681         return sFallback->registerBuffer(sFallback, handle);
682     }
683
684     D("gralloc_register_buffer(%p) called", handle);
685
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);
690         return -EINVAL;
691     }
692
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);
697     }
698
699     //
700     // if the color buffer has ashmem region and it is not mapped in this
701     // process map it now.
702     //
703     if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
704         void *vaddr;
705         int err = map_buffer(cb, &vaddr);
706         if (err) {
707             ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
708             return -err;
709         }
710         cb->mappedPid = getpid();
711         D("%s: checking to map goldfish dma", __FUNCTION__);
712         if (cb->dmafd > 0) {
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",
715               &cb->goldfish_dma,
716               (int)getpid(),
717               cb->mappedPid);
718             if (cb->goldfish_dma.fd != cb->dmafd) {
719                 cb->goldfish_dma.fd = cb->dmafd;
720             }
721             goldfish_dma_map(&cb->goldfish_dma);
722         }
723     }
724
725     return 0;
726 }
727
728 static int gralloc_unregister_buffer(gralloc_module_t const* module,
729                                      buffer_handle_t handle)
730 {
731     D("%s: call", __FUNCTION__);
732     if (sFallback != NULL) {
733         return sFallback->unregisterBuffer(sFallback, handle);
734     }
735
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);
740         return -EINVAL;
741     }
742
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);
747     }
748
749     //
750     // unmap ashmem region if it was previously mapped in this process
751     // (through register_buffer)
752     //
753     if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
754         DEFINE_AND_VALIDATE_HOST_CONNECTION;
755         void *vaddr;
756         int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
757         if (err) {
758             ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
759             return -EINVAL;
760         }
761         cb->ashmemBase = 0;
762         cb->mappedPid = 0;
763         D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
764         if (cb->dmafd > 0) {
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);
768         }
769     }
770
771
772     D("gralloc_unregister_buffer(%p) done\n", cb);
773     return 0;
774 }
775
776
777
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,
781                         void** vaddr)
782 {
783     if (sFallback != NULL) {
784         return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
785     }
786
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");
791         return -EINVAL;
792     }
793
794     // Validate usage,
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
809
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
815
816     bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
817
818 #if PLATFORM_SDK_VERSION >= 15
819     // bug: 30088791
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
822     // legit usage.
823     sw_read_allowed = sw_read_allowed || (cb->usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
824 #endif // PLATFORM_SDK_VERSION >= 15
825
826     bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
827
828     if ( (hw_read || hw_write) ||
829          (!sw_read && !sw_write &&
830                  !hw_cam_write && !hw_cam_read &&
831                  !hw_vid_enc_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,
835                 cb->usage);
836         //This is not exactly an error and loose it up.
837         //bug: 30784436
838         //return -EINVAL;
839     }
840
841     intptr_t postCount = 0;
842     void *cpu_addr = NULL;
843
844     //
845     // make sure ashmem area is mapped if needed
846     //
847     if (cb->canBePosted() || sw_read || sw_write ||
848             hw_cam_write || hw_cam_read ||
849             hw_vid_enc_read) {
850         if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
851             return -EACCES;
852         }
853
854         if (cb->canBePosted()) {
855             postCount = *((intptr_t *)cb->ashmemBase);
856             cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t));
857         }
858         else {
859             cpu_addr = (void *)(cb->ashmemBase);
860         }
861     }
862
863     if (cb->hostHandle) {
864         // Make sure we have host connection
865         DEFINE_AND_VALIDATE_HOST_CONNECTION;
866
867         //
868         // flush color buffer write cache on host and get its sync status.
869         //
870         int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
871                                                             postCount,
872                                                             sw_read);
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",
877                  postCount, sw_read);
878             return -EBUSY;
879         }
880
881         if (sw_read) {
882             void* rgb_addr = cpu_addr;
883             char* tmpBuf = 0;
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];
888                 rgb_addr = tmpBuf;
889             }
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);
893             if (tmpBuf) {
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);
898                 }
899                 delete [] tmpBuf;
900             }
901         }
902     }
903
904     //
905     // is virtual address required ?
906     //
907     if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) {
908         *vaddr = cpu_addr;
909     }
910
911     if (sw_write || hw_cam_write) {
912         //
913         // Keep locked region if locked for s/w write access.
914         //
915         cb->lockedLeft = l;
916         cb->lockedTop = t;
917         cb->lockedWidth = w;
918         cb->lockedHeight = h;
919     }
920
921     DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p",
922             vaddr, vaddr ? *vaddr : 0, usage, cpu_addr);
923
924     return 0;
925 }
926
927 static int gralloc_unlock(gralloc_module_t const* module,
928                           buffer_handle_t handle)
929 {
930     if (sFallback != NULL) {
931         return sFallback->unlock(sFallback, handle);
932     }
933
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__);
938         return -EINVAL;
939     }
940
941     //
942     // if buffer was locked for s/w write, we need to update the host with
943     // the updated data
944     //
945     if (cb->hostHandle) {
946
947         // Make sure we have host connection
948         DEFINE_AND_VALIDATE_HOST_CONNECTION;
949
950         void *cpu_addr;
951         if (cb->canBePosted()) {
952             cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
953         }
954         else {
955             cpu_addr = (void *)(cb->ashmemBase);
956         }
957
958         char* rgb_addr = (char *)cpu_addr;
959         if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
960             updateHostColorBuffer(cb, true, rgb_addr);
961         }
962         else {
963             updateHostColorBuffer(cb, false, rgb_addr);
964         }
965
966         DD("gralloc_unlock success. cpu_addr: %p", cpu_addr);
967     }
968
969     cb->lockedWidth = cb->lockedHeight = 0;
970     return 0;
971 }
972
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)
978 {
979     // Not supporting fallback module for YCbCr
980     if (sFallback != NULL) {
981         ALOGD("%s: has fallback, return -EINVAL", __FUNCTION__);
982         return -EINVAL;
983     }
984
985     if (!ycbcr) {
986         ALOGE("%s: got NULL ycbcr struct! -EINVAL", __FUNCTION__);
987         return -EINVAL;
988     }
989
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__);
994         return -EINVAL;
995     }
996
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. "
1001                 "-EINVAL",
1002                 cb->frameworkFormat);
1003         return -EINVAL;
1004     }
1005
1006     // Make sure memory is mapped, get address
1007     if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
1008         ALOGD("%s: ashmembase not mapped. -EACCESS", __FUNCTION__);
1009         return -EACCES;
1010     }
1011
1012     uint8_t *cpu_addr = NULL;
1013
1014     if (cb->canBePosted()) {
1015         cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int));
1016     }
1017     else {
1018         cpu_addr = (uint8_t *)(cb->ashmemBase);
1019     }
1020
1021     // Calculate offsets to underlying YUV data
1022     size_t yStride;
1023     size_t cStride;
1024     size_t cSize;
1025     size_t yOffset;
1026     size_t uOffset;
1027     size_t vOffset;
1028     size_t cStep;
1029     size_t align;
1030     switch (cb->format) {
1031         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1032             yStride = cb->width;
1033             cStride = cb->width;
1034             yOffset = 0;
1035             vOffset = yStride * cb->height;
1036             uOffset = vOffset + 1;
1037             cStep = 2;
1038             break;
1039         case HAL_PIXEL_FORMAT_YV12:
1040             // https://developer.android.com/reference/android/graphics/ImageFormat.html#YV12
1041             align = 16;
1042             yStride = (cb->width + (align -1)) & ~(align-1);
1043             cStride = (yStride / 2 + (align - 1)) & ~(align-1);
1044             yOffset = 0;
1045             cSize = cStride * cb->height/2;
1046             vOffset = yStride * cb->height;
1047             uOffset = vOffset + cSize;
1048             cStep = 1;
1049             break;
1050         case HAL_PIXEL_FORMAT_YCbCr_420_888:
1051             align = 1;
1052             yStride = cb->width;
1053             cStride = yStride / 2;
1054             yOffset = 0;
1055             cSize = cStride * cb->height/2;
1056             uOffset = yStride * cb->height;
1057             vOffset = uOffset + cSize;
1058             cStep = 1;
1059             break;
1060         default:
1061             ALOGE("gralloc_lock_ycbcr unexpected internal format %x",
1062                     cb->format);
1063             return -EINVAL;
1064     }
1065
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;
1072
1073     // Zero out reserved fields
1074     memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
1075
1076     //
1077     // Keep locked region if locked for s/w write access.
1078     //
1079     cb->lockedLeft = l;
1080     cb->lockedTop = t;
1081     cb->lockedWidth = w;
1082     cb->lockedHeight = h;
1083
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);
1088
1089     return 0;
1090 }
1091 #endif // PLATFORM_SDK_VERSION >= 18
1092
1093 static int gralloc_device_open(const hw_module_t* module,
1094                                const char* name,
1095                                hw_device_t** device)
1096 {
1097     int status = -EINVAL;
1098
1099     D("gralloc_device_open %s\n", name);
1100
1101     pthread_once( &sFallbackOnce, fallback_init );
1102     if (sFallback != NULL) {
1103         return sFallback->common.methods->open(&sFallback->common, name, device);
1104     }
1105
1106     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
1107
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();
1111         if (!hostCon) {
1112             ALOGE("gralloc: failed to get host connection while opening %s\n", name);
1113             return -EIO;
1114         }
1115
1116         //
1117         // Allocate memory for the gralloc device (alloc interface)
1118         //
1119         gralloc_device_t *dev;
1120         dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t));
1121         if (NULL == dev) {
1122             return -ENOMEM;
1123         }
1124
1125         // Initialize our device structure
1126         //
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;
1131
1132         dev->device.alloc   = gralloc_alloc;
1133         dev->device.free    = gralloc_free;
1134         dev->allocListHead  = NULL;
1135         pthread_mutex_init(&dev->lock, NULL);
1136
1137         *device = &dev->device.common;
1138         status = 0;
1139     }
1140     else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
1141
1142         // return error if connection with host can not be established
1143         DEFINE_AND_VALIDATE_HOST_CONNECTION;
1144
1145         //
1146         // Query the host for Framebuffer attributes
1147         //
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);
1163
1164         //
1165         // Allocate memory for the framebuffer device
1166         //
1167         fb_device_t *dev;
1168         dev = (fb_device_t*)malloc(sizeof(fb_device_t));
1169         if (NULL == dev) {
1170             return -ENOMEM;
1171         }
1172         memset(dev, 0, sizeof(fb_device_t));
1173
1174         // Initialize our device structure
1175         //
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
1184
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;
1196
1197         status = 0;
1198     }
1199
1200     return status;
1201 }
1202
1203 //
1204 // define the HMI symbol - our module interface
1205 //
1206 static struct hw_module_methods_t gralloc_module_methods = {
1207         open: gralloc_device_open
1208 };
1209
1210 struct private_module_t HAL_MODULE_INFO_SYM = {
1211     base: {
1212         common: {
1213             tag: HARDWARE_MODULE_TAG,
1214 #if PLATFORM_SDK_VERSION >= 18
1215             module_api_version: GRALLOC_MODULE_API_VERSION_0_2,
1216             hal_api_version: 0,
1217 #elif PLATFORM_SDK_VERSION >= 16
1218             module_api_version: 1,
1219             hal_api_version: 0,
1220 #else // PLATFORM_SDK_VERSION
1221             version_major: 1,
1222             version_minor: 0,
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,
1228             dso: NULL,
1229             reserved: {0, }
1230         },
1231         registerBuffer: gralloc_register_buffer,
1232         unregisterBuffer: gralloc_unregister_buffer,
1233         lock: gralloc_lock,
1234         unlock: gralloc_unlock,
1235         perform: NULL,
1236 #if PLATFORM_SDK_VERSION >= 18
1237         lock_ycbcr: gralloc_lock_ycbcr,
1238 #endif // PLATFORM_SDK_VERSION >= 18
1239     }
1240 };
1241
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).
1245  *
1246  * If not, then load gralloc.default instead as a fallback.
1247  */
1248 static void
1249 fallback_init(void)
1250 {
1251     char  prop[PROPERTY_VALUE_MAX];
1252     void* module;
1253
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) {
1259         return;
1260     }
1261     ALOGD("Emulator without host-side GPU emulation detected.");
1262 #if __LP64__
1263     module = dlopen("/vendor/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1264 #else
1265     module = dlopen("/vendor/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1266 #endif
1267     if (module != NULL) {
1268         sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
1269         if (sFallback == NULL) {
1270             dlclose(module);
1271         }
1272     }
1273     if (sFallback == NULL) {
1274         ALOGE("Could not find software fallback module!?");
1275     }
1276 }