1/*
2* Copyright (C) 2011 The Android Open Source Project
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16#include <string.h>
17#include <pthread.h>
18#include <limits.h>
19#include <cutils/ashmem.h>
20#include <unistd.h>
21#include <errno.h>
22#include <dlfcn.h>
23#include <sys/mman.h>
24#include "gralloc_cb.h"
25#include "HostConnection.h"
26#include "glUtils.h"
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
30/* Set to 1 or 2 to enable debug traces */
31#define DEBUG  0
32
33#if DEBUG >= 1
34#  define D(...)   ALOGD(__VA_ARGS__)
35#else
36#  define D(...)   ((void)0)
37#endif
38
39#if DEBUG >= 2
40#  define DD(...)  ALOGD(__VA_ARGS__)
41#else
42#  define DD(...)  ((void)0)
43#endif
44
45#define DBG_FUNC DBG("%s\n", __FUNCTION__)
46//
47// our private gralloc module structure
48//
49struct private_module_t {
50    gralloc_module_t base;
51};
52
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.
56 */
57static gralloc_module_t*  sFallback;
58static pthread_once_t     sFallbackOnce = PTHREAD_ONCE_INIT;
59
60static void fallback_init(void);  // forward
61
62
63typedef struct _alloc_list_node {
64    buffer_handle_t handle;
65    _alloc_list_node *next;
66    _alloc_list_node *prev;
67} AllocListNode;
68
69//
70// Our gralloc device structure (alloc interface)
71//
72struct gralloc_device_t {
73    alloc_device_t  device;
74
75    AllocListNode *allocListHead;    // double linked list of allocated buffers
76    pthread_mutex_t lock;
77};
78
79//
80// Our framebuffer device structure
81//
82struct fb_device_t {
83    framebuffer_device_t  device;
84};
85
86static int map_buffer(cb_handle_t *cb, void **vaddr)
87{
88    if (cb->fd < 0 || cb->ashmemSize <= 0) {
89        return -EINVAL;
90    }
91
92    void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
93                      MAP_SHARED, cb->fd, 0);
94    if (addr == MAP_FAILED) {
95        return -errno;
96    }
97
98    cb->ashmemBase = intptr_t(addr);
99    cb->ashmemBasePid = getpid();
100
101    *vaddr = addr;
102    return 0;
103}
104
105#define DEFINE_HOST_CONNECTION \
106    HostConnection *hostCon = HostConnection::get(); \
107    renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
108
109#define DEFINE_AND_VALIDATE_HOST_CONNECTION \
110    HostConnection *hostCon = HostConnection::get(); \
111    if (!hostCon) { \
112        ALOGE("gralloc: Failed to get host connection\n"); \
113        return -EIO; \
114    } \
115    renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \
116    if (!rcEnc) { \
117        ALOGE("gralloc: Failed to get renderControl encoder context\n"); \
118        return -EIO; \
119    }
120
121
122//
123// gralloc device functions (alloc interface)
124//
125static int gralloc_alloc(alloc_device_t* dev,
126                         int w, int h, int format, int usage,
127                         buffer_handle_t* pHandle, int* pStride)
128{
129    D("gralloc_alloc w=%d h=%d usage=0x%x\n", w, h, usage);
130
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);
135        return -EINVAL;
136    }
137
138    //
139    // Note: in screen capture mode, both sw_write and hw_write will be on
140    // and this is a valid usage
141    //
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;
148
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;
166            }
167        }
168
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",
172                    w, h, usage);
173            return -EINVAL;
174        }
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;
179        }
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",
183                    w, h, usage);
184        }
185    }
186    bool yuv_format = false;
187
188    int ashmem_size = 0;
189    int stride = w;
190
191    GLenum glFormat = 0;
192    GLenum glType = 0;
193
194    int bpp = 0;
195    int align = 1;
196    switch (format) {
197        case HAL_PIXEL_FORMAT_RGBA_8888:
198        case HAL_PIXEL_FORMAT_RGBX_8888:
199        case HAL_PIXEL_FORMAT_BGRA_8888:
200            bpp = 4;
201            glFormat = GL_RGBA;
202            glType = GL_UNSIGNED_BYTE;
203            break;
204        case HAL_PIXEL_FORMAT_RGB_888:
205            bpp = 3;
206            glFormat = GL_RGB;
207            glType = GL_UNSIGNED_BYTE;
208            break;
209        case HAL_PIXEL_FORMAT_RGB_565:
210            bpp = 2;
211            glFormat = GL_RGB;
212            glType = GL_UNSIGNED_SHORT_5_6_5;
213            break;
214        case HAL_PIXEL_FORMAT_RAW_SENSOR:
215            bpp = 2;
216            align = 16*bpp;
217            if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) {
218                // Raw sensor data only goes between camera and CPU
219                return -EINVAL;
220            }
221            // Not expecting to actually create any GL surfaces for this
222            glFormat = GL_LUMINANCE;
223            glType = GL_UNSIGNED_SHORT;
224            break;
225        case HAL_PIXEL_FORMAT_BLOB:
226            bpp = 1;
227            if (! (sw_read && hw_cam_write) ) {
228                // Blob data cannot be used by HW other than camera emulator
229                return -EINVAL;
230            }
231            // Not expecting to actually create any GL surfaces for this
232            glFormat = GL_LUMINANCE;
233            glType = GL_UNSIGNED_BYTE;
234            break;
235        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
236            align = 1;
237            bpp = 1; // per-channel bpp
238            yuv_format = true;
239            // Not expecting to actually create any GL surfaces for this
240            break;
241        case HAL_PIXEL_FORMAT_YV12:
242            align = 16;
243            bpp = 1; // per-channel bpp
244            yuv_format = true;
245            // Not expecting to actually create any GL surfaces for this
246            break;
247        default:
248            ALOGE("gralloc_alloc: Unknown format %d", format);
249            return -EINVAL;
250    }
251
252    if (usage & GRALLOC_USAGE_HW_FB) {
253        // keep space for postCounter
254        ashmem_size += sizeof(uint32_t);
255    }
256
257    if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
258        // keep space for image on guest memory if SW access is needed
259        // or if the camera is doing writing
260        if (yuv_format) {
261            size_t yStride = (w*bpp + (align - 1)) & ~(align-1);
262            size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1);
263            size_t uvHeight = h / 2;
264            ashmem_size += yStride * h + 2 * (uvHeight * uvStride);
265            stride = yStride / bpp;
266        } else {
267            size_t bpr = (w*bpp + (align-1)) & ~(align-1);
268            ashmem_size += (bpr * h);
269            stride = bpr / bpp;
270        }
271    }
272
273    D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format,
274            ashmem_size, stride, gettid());
275
276    //
277    // Allocate space in ashmem if needed
278    //
279    int fd = -1;
280    if (ashmem_size > 0) {
281        // round to page size;
282        ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
283
284        fd = ashmem_create_region("gralloc-buffer", ashmem_size);
285        if (fd < 0) {
286            ALOGE("gralloc_alloc failed to create ashmem region: %s\n",
287                    strerror(errno));
288            return -errno;
289        }
290    }
291
292    cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
293                                      w, h, frameworkFormat, format,
294                                      glFormat, glType);
295
296    if (ashmem_size > 0) {
297        //
298        // map ashmem region if exist
299        //
300        void *vaddr;
301        int err = map_buffer(cb, &vaddr);
302        if (err) {
303            close(fd);
304            delete cb;
305            return err;
306        }
307
308        cb->setFd(fd);
309    }
310
311    //
312    // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
313    // Only do this for some h/w usages, not all.
314    //
315    if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
316                    GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
317                    GRALLOC_USAGE_HW_FB) ) {
318        DEFINE_HOST_CONNECTION;
319        if (hostCon && rcEnc) {
320            cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
321            D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
322        }
323
324        if (!cb->hostHandle) {
325           // Could not create colorbuffer on host !!!
326           close(fd);
327           delete cb;
328           return -EIO;
329        }
330    }
331
332    //
333    // alloc succeeded - insert the allocated handle to the allocated list
334    //
335    AllocListNode *node = new AllocListNode();
336    pthread_mutex_lock(&grdev->lock);
337    node->handle = cb;
338    node->next =  grdev->allocListHead;
339    node->prev =  NULL;
340    if (grdev->allocListHead) {
341        grdev->allocListHead->prev = node;
342    }
343    grdev->allocListHead = node;
344    pthread_mutex_unlock(&grdev->lock);
345
346    *pHandle = cb;
347    if (frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
348        *pStride = 0;
349    } else {
350        *pStride = stride;
351    }
352    return 0;
353}
354
355static int gralloc_free(alloc_device_t* dev,
356                        buffer_handle_t handle)
357{
358    const cb_handle_t *cb = (const cb_handle_t *)handle;
359    if (!cb_handle_t::validate((cb_handle_t*)cb)) {
360        ERR("gralloc_free: invalid handle");
361        return -EINVAL;
362    }
363
364    if (cb->hostHandle != 0) {
365        DEFINE_AND_VALIDATE_HOST_CONNECTION;
366        D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
367        rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
368    }
369
370    //
371    // detach and unmap ashmem area if present
372    //
373    if (cb->fd > 0) {
374        if (cb->ashmemSize > 0 && cb->ashmemBase) {
375            munmap((void *)cb->ashmemBase, cb->ashmemSize);
376        }
377        close(cb->fd);
378    }
379
380    // remove it from the allocated list
381    gralloc_device_t *grdev = (gralloc_device_t *)dev;
382    pthread_mutex_lock(&grdev->lock);
383    AllocListNode *n = grdev->allocListHead;
384    while( n && n->handle != cb ) {
385        n = n->next;
386    }
387    if (n) {
388       // buffer found on list - remove it from list
389       if (n->next) {
390           n->next->prev = n->prev;
391       }
392       if (n->prev) {
393           n->prev->next = n->next;
394       }
395       else {
396           grdev->allocListHead = n->next;
397       }
398
399       delete n;
400    }
401    pthread_mutex_unlock(&grdev->lock);
402
403    delete cb;
404
405    return 0;
406}
407
408static int gralloc_device_close(struct hw_device_t *dev)
409{
410    gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
411    if (d) {
412
413        // free still allocated buffers
414        while( d->allocListHead != NULL ) {
415            gralloc_free(&d->device, d->allocListHead->handle);
416        }
417
418        // free device
419        free(d);
420    }
421    return 0;
422}
423
424static int fb_compositionComplete(struct framebuffer_device_t* dev)
425{
426    (void)dev;
427
428    return 0;
429}
430
431//
432// Framebuffer device functions
433//
434static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
435{
436    fb_device_t *fbdev = (fb_device_t *)dev;
437    cb_handle_t *cb = (cb_handle_t *)buffer;
438
439    if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) {
440        return -EINVAL;
441    }
442
443    // Make sure we have host connection
444    DEFINE_AND_VALIDATE_HOST_CONNECTION;
445
446    // increment the post count of the buffer
447    intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
448    if (!postCountPtr) {
449        // This should not happen
450        return -EINVAL;
451    }
452    (*postCountPtr)++;
453
454    // send post request to host
455    rcEnc->rcFBPost(rcEnc, cb->hostHandle);
456    hostCon->flush();
457
458    return 0;
459}
460
461static int fb_setUpdateRect(struct framebuffer_device_t* dev,
462        int l, int t, int w, int h)
463{
464    fb_device_t *fbdev = (fb_device_t *)dev;
465
466    (void)l;
467    (void)t;
468    (void)w;
469    (void)h;
470
471    if (!fbdev) {
472        return -EINVAL;
473    }
474
475    // Make sure we have host connection
476    DEFINE_AND_VALIDATE_HOST_CONNECTION;
477
478    // send request to host
479    // TODO: XXX - should be implemented
480    //rcEnc->rc_XXX
481
482    return 0;
483}
484
485static int fb_setSwapInterval(struct framebuffer_device_t* dev,
486            int interval)
487{
488    fb_device_t *fbdev = (fb_device_t *)dev;
489
490    if (!fbdev) {
491        return -EINVAL;
492    }
493
494    // Make sure we have host connection
495    DEFINE_AND_VALIDATE_HOST_CONNECTION;
496
497    // send request to host
498    rcEnc->rcFBSetSwapInterval(rcEnc, interval);
499    hostCon->flush();
500
501    return 0;
502}
503
504static int fb_close(struct hw_device_t *dev)
505{
506    fb_device_t *fbdev = (fb_device_t *)dev;
507
508    delete fbdev;
509
510    return 0;
511}
512
513
514//
515// gralloc module functions - refcount + locking interface
516//
517static int gralloc_register_buffer(gralloc_module_t const* module,
518                                   buffer_handle_t handle)
519{
520    pthread_once(&sFallbackOnce, fallback_init);
521    if (sFallback != NULL) {
522        return sFallback->registerBuffer(sFallback, handle);
523    }
524
525    D("gralloc_register_buffer(%p) called", handle);
526
527    private_module_t *gr = (private_module_t *)module;
528    cb_handle_t *cb = (cb_handle_t *)handle;
529    if (!gr || !cb_handle_t::validate(cb)) {
530        ERR("gralloc_register_buffer(%p): invalid buffer", cb);
531        return -EINVAL;
532    }
533
534    if (cb->hostHandle != 0) {
535        DEFINE_AND_VALIDATE_HOST_CONNECTION;
536        D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
537        rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
538    }
539
540    //
541    // if the color buffer has ashmem region and it is not mapped in this
542    // process map it now.
543    //
544    if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
545        void *vaddr;
546        int err = map_buffer(cb, &vaddr);
547        if (err) {
548            ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
549            return -err;
550        }
551        cb->mappedPid = getpid();
552    }
553
554    return 0;
555}
556
557static int gralloc_unregister_buffer(gralloc_module_t const* module,
558                                     buffer_handle_t handle)
559{
560    if (sFallback != NULL) {
561        return sFallback->unregisterBuffer(sFallback, handle);
562    }
563
564    private_module_t *gr = (private_module_t *)module;
565    cb_handle_t *cb = (cb_handle_t *)handle;
566    if (!gr || !cb_handle_t::validate(cb)) {
567        ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
568        return -EINVAL;
569    }
570
571    if (cb->hostHandle != 0) {
572        DEFINE_AND_VALIDATE_HOST_CONNECTION;
573        D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
574        rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
575    }
576
577    //
578    // unmap ashmem region if it was previously mapped in this process
579    // (through register_buffer)
580    //
581    if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
582        void *vaddr;
583        int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
584        if (err) {
585            ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
586            return -EINVAL;
587        }
588        cb->ashmemBase = 0;
589        cb->mappedPid = 0;
590    }
591
592    D("gralloc_unregister_buffer(%p) done\n", cb);
593
594    return 0;
595}
596
597static int gralloc_lock(gralloc_module_t const* module,
598                        buffer_handle_t handle, int usage,
599                        int l, int t, int w, int h,
600                        void** vaddr)
601{
602    if (sFallback != NULL) {
603        return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
604    }
605
606    private_module_t *gr = (private_module_t *)module;
607    cb_handle_t *cb = (cb_handle_t *)handle;
608    if (!gr || !cb_handle_t::validate(cb)) {
609        ALOGE("gralloc_lock bad handle\n");
610        return -EINVAL;
611    }
612
613    // validate format
614    if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
615        ALOGE("gralloc_lock can't be used with YCbCr_420_888 format");
616        return -EINVAL;
617    }
618
619    // Validate usage,
620    //   1. cannot be locked for hw access
621    //   2. lock for either sw read or write.
622    //   3. locked sw access must match usage during alloc time.
623    bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
624    bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
625    bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE);
626    bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
627    bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
628    bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
629    bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
630    bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
631    bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
632
633    if ( (hw_read || hw_write) ||
634         (!sw_read && !sw_write &&
635                 !hw_cam_write && !hw_cam_read &&
636                 !hw_vid_enc_read) ||
637         (sw_read && !sw_read_allowed) ||
638         (sw_write && !sw_write_allowed) ) {
639        ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage,
640                cb->usage);
641        return -EINVAL;
642    }
643
644    intptr_t postCount = 0;
645    void *cpu_addr = NULL;
646
647    //
648    // make sure ashmem area is mapped if needed
649    //
650    if (cb->canBePosted() || sw_read || sw_write ||
651            hw_cam_write || hw_cam_read ||
652            hw_vid_enc_read) {
653        if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
654            return -EACCES;
655        }
656
657        if (cb->canBePosted()) {
658            postCount = *((intptr_t *)cb->ashmemBase);
659            cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t));
660        }
661        else {
662            cpu_addr = (void *)(cb->ashmemBase);
663        }
664    }
665
666    if (cb->hostHandle) {
667        // Make sure we have host connection
668        DEFINE_AND_VALIDATE_HOST_CONNECTION;
669
670        //
671        // flush color buffer write cache on host and get its sync status.
672        //
673        int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
674                                                            postCount,
675                                                            sw_read);
676        if (hostSyncStatus < 0) {
677            // host failed the color buffer sync - probably since it was already
678            // locked for write access. fail the lock.
679            ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n",
680                 postCount, sw_read);
681            return -EBUSY;
682        }
683
684        const bool sw_read = (cb->usage & GRALLOC_USAGE_SW_READ_MASK);
685        const bool hw_write = (cb->usage & GRALLOC_USAGE_HW_RENDER);
686        const bool screen_capture_mode = (sw_read && hw_write);
687        if (screen_capture_mode) {
688            D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height);
689            DEFINE_AND_VALIDATE_HOST_CONNECTION;
690            rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
691                    0, 0, cb->width, cb->height, GL_RGBA, GL_UNSIGNED_BYTE, cpu_addr);
692        }
693    }
694
695    //
696    // is virtual address required ?
697    //
698    if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) {
699        *vaddr = cpu_addr;
700    }
701
702    if (sw_write || hw_cam_write) {
703        //
704        // Keep locked region if locked for s/w write access.
705        //
706        cb->lockedLeft = l;
707        cb->lockedTop = t;
708        cb->lockedWidth = w;
709        cb->lockedHeight = h;
710    }
711
712    DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p",
713            vaddr, vaddr ? *vaddr : 0, usage, cpu_addr);
714
715    return 0;
716}
717
718static int gralloc_unlock(gralloc_module_t const* module,
719                          buffer_handle_t handle)
720{
721    if (sFallback != NULL) {
722        return sFallback->unlock(sFallback, handle);
723    }
724
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)) {
728        return -EINVAL;
729    }
730
731    //
732    // if buffer was locked for s/w write, we need to update the host with
733    // the updated data
734    //
735    if (cb->lockedWidth > 0 && cb->lockedHeight > 0 && cb->hostHandle) {
736
737        // Make sure we have host connection
738        DEFINE_AND_VALIDATE_HOST_CONNECTION;
739
740        void *cpu_addr;
741        if (cb->canBePosted()) {
742            cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
743        }
744        else {
745            cpu_addr = (void *)(cb->ashmemBase);
746        }
747
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];
751
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;
755            char *dst = tmpBuf;
756            for (int y=0; y<cb->lockedHeight; y++) {
757                memcpy(dst, src, dst_line_len);
758                src += src_line_len;
759                dst += dst_line_len;
760            }
761
762            rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
763                                       cb->lockedLeft, cb->lockedTop,
764                                       cb->lockedWidth, cb->lockedHeight,
765                                       cb->glFormat, cb->glType,
766                                       tmpBuf);
767
768            delete [] tmpBuf;
769        }
770        else {
771            rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0,
772                                       cb->width, cb->height,
773                                       cb->glFormat, cb->glType,
774                                       cpu_addr);
775        }
776    }
777
778    cb->lockedWidth = cb->lockedHeight = 0;
779    return 0;
780}
781
782static 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)
786{
787    // Not supporting fallback module for YCbCr
788    if (sFallback != NULL) {
789        return -EINVAL;
790    }
791
792    if (!ycbcr) {
793        ALOGE("gralloc_lock_ycbcr got NULL ycbcr struct");
794        return -EINVAL;
795    }
796
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");
801        return -EINVAL;
802    }
803
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);
808        return -EINVAL;
809    }
810
811    // Validate usage
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));
816
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",
820                usage, cb->usage);
821        return -EINVAL;
822    }
823
824    // Make sure memory is mapped, get address
825    if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
826        return -EACCES;
827    }
828
829    uint8_t *cpu_addr = NULL;
830
831    if (cb->canBePosted()) {
832        cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int));
833    }
834    else {
835        cpu_addr = (uint8_t *)(cb->ashmemBase);
836    }
837
838    // Calculate offsets to underlying YUV data
839    size_t yStride;
840    size_t cStride;
841    size_t yOffset;
842    size_t uOffset;
843    size_t vOffset;
844    size_t cStep;
845    switch (cb->format) {
846        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
847            yStride = cb->width;
848            cStride = cb->width;
849            yOffset = 0;
850            vOffset = yStride * cb->height;
851            uOffset = vOffset + 1;
852            cStep = 2;
853            break;
854        default:
855            ALOGE("gralloc_lock_ycbcr unexpected internal format %x",
856                    cb->format);
857            return -EINVAL;
858    }
859
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;
866
867    // Zero out reserved fields
868    memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
869
870    //
871    // Keep locked region if locked for s/w write access.
872    //
873    cb->lockedLeft = l;
874    cb->lockedTop = t;
875    cb->lockedWidth = w;
876    cb->lockedHeight = h;
877
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,
881            ycbcr->chroma_step);
882
883    return 0;
884}
885
886static int gralloc_device_open(const hw_module_t* module,
887                               const char* name,
888                               hw_device_t** device)
889{
890    int status = -EINVAL;
891
892    D("gralloc_device_open %s\n", name);
893
894    pthread_once( &sFallbackOnce, fallback_init );
895    if (sFallback != NULL) {
896        return sFallback->common.methods->open(&sFallback->common, name, device);
897    }
898
899    if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
900
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();
904        if (!hostCon) {
905            ALOGE("gralloc: failed to get host connection while opening %s\n", name);
906            return -EIO;
907        }
908
909        //
910        // Allocate memory for the gralloc device (alloc interface)
911        //
912        gralloc_device_t *dev;
913        dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t));
914        if (NULL == dev) {
915            return -ENOMEM;
916        }
917
918        // Initialize our device structure
919        //
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;
924
925        dev->device.alloc   = gralloc_alloc;
926        dev->device.free    = gralloc_free;
927        dev->allocListHead  = NULL;
928        pthread_mutex_init(&dev->lock, NULL);
929
930        *device = &dev->device.common;
931        status = 0;
932    }
933    else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
934
935        // return error if connection with host can not be established
936        DEFINE_AND_VALIDATE_HOST_CONNECTION;
937
938        //
939        // Query the host for Framebuffer attributes
940        //
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);
956
957        //
958        // Allocate memory for the framebuffer device
959        //
960        fb_device_t *dev;
961        dev = (fb_device_t*)malloc(sizeof(fb_device_t));
962        if (NULL == dev) {
963            return -ENOMEM;
964        }
965        memset(dev, 0, sizeof(fb_device_t));
966
967        // Initialize our device structure
968        //
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
977
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;
989
990        status = 0;
991    }
992
993    return status;
994}
995
996//
997// define the HMI symbol - our module interface
998//
999static struct hw_module_methods_t gralloc_module_methods = {
1000        open: gralloc_device_open
1001};
1002
1003struct private_module_t HAL_MODULE_INFO_SYM = {
1004    base: {
1005        common: {
1006            tag: HARDWARE_MODULE_TAG,
1007            module_api_version: GRALLOC_MODULE_API_VERSION_0_2,
1008            hal_api_version: 0,
1009            id: GRALLOC_HARDWARE_MODULE_ID,
1010            name: "Graphics Memory Allocator Module",
1011            author: "The Android Open Source Project",
1012            methods: &gralloc_module_methods,
1013            dso: NULL,
1014            reserved: {0, }
1015        },
1016        registerBuffer: gralloc_register_buffer,
1017        unregisterBuffer: gralloc_unregister_buffer,
1018        lock: gralloc_lock,
1019        unlock: gralloc_unlock,
1020        perform: NULL,
1021        lock_ycbcr: gralloc_lock_ycbcr,
1022    }
1023};
1024
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 > 0 if this is the case).
1028 *
1029 * If not, then load gralloc.default instead as a fallback.
1030 */
1031static void
1032fallback_init(void)
1033{
1034    char  prop[PROPERTY_VALUE_MAX];
1035    void* module;
1036
1037    property_get("ro.kernel.qemu.gles", prop, "0");
1038    if (atoi(prop) > 0) {
1039        return;
1040    }
1041    ALOGD("Emulator without GPU emulation detected.");
1042#if __LP64__
1043    module = dlopen("/system/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1044#else
1045    module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
1046#endif
1047    if (module != NULL) {
1048        sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
1049        if (sFallback == NULL) {
1050            dlclose(module);
1051        }
1052    }
1053    if (sFallback == NULL) {
1054        ALOGE("Could not find software fallback module!?");
1055    }
1056}
1057