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 <hardware/gralloc.h>
25 
26 #include <gralloc_cb_bp.h>
27 #include "gralloc_common.h"
28 
29 #include "goldfish_dma.h"
30 #include "goldfish_address_space.h"
31 #include "FormatConversions.h"
32 #include "HostConnection.h"
33 #include "ProcessPipe.h"
34 #include "ThreadInfo.h"
35 #include "glUtils.h"
36 #include <qemu_pipe_bp.h>
37 
38 #if PLATFORM_SDK_VERSION < 26
39 #include <cutils/log.h>
40 #else
41 #include <log/log.h>
42 #endif
43 #include <cutils/properties.h>
44 
45 #include <set>
46 #include <map>
47 #include <vector>
48 #include <string>
49 #include <sstream>
50 
51 /* Set to 1 or 2 to enable debug traces */
52 #define DEBUG  0
53 
54 #ifndef D
55 
56 #if DEBUG >= 1
57 #  define D(...)   ALOGD(__VA_ARGS__)
58 #else
59 #  define D(...)   ((void)0)
60 #endif
61 
62 #endif
63 
64 #if DEBUG >= 2
65 #  define DD(...)  ALOGD(__VA_ARGS__)
66 #else
67 #  define DD(...)  ((void)0)
68 #endif
69 
70 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
71 
72 #define GOLDFISH_OFFSET_UNIT 8
73 
74 #define OMX_COLOR_FormatYUV420Planar 19
75 
76 #ifdef GOLDFISH_HIDL_GRALLOC
77 static const bool isHidlGralloc = true;
78 #else
79 static const bool isHidlGralloc = false;
80 #endif
81 
82 const uint32_t CB_HANDLE_MAGIC_OLD = CB_HANDLE_MAGIC_BASE | 0x1;
83 
84 struct cb_handle_old_t : public cb_handle_t {
cb_handle_old_tcb_handle_old_t85     cb_handle_old_t(int p_fd, int p_ashmemSize, int p_usage,
86                     int p_width, int p_height,
87                     int p_format, int p_glFormat, int p_glType)
88             : cb_handle_t(p_fd,
89                           QEMU_PIPE_INVALID_HANDLE,
90                           CB_HANDLE_MAGIC_OLD,
91                           0,
92                           p_usage,
93                           p_width,
94                           p_height,
95                           p_format,
96                           p_glFormat,
97                           p_glType,
98                           p_ashmemSize,
99                           NULL,
100                           ~uint64_t(0)),
101               ashmemBasePid(0),
102               mappedPid(0) {
103         numInts = CB_HANDLE_NUM_INTS(numFds);
104     }
105 
hasRefcountPipecb_handle_old_t106     bool hasRefcountPipe() const {
107         return qemu_pipe_valid(hostHandleRefCountFd);
108     }
109 
setRefcountPipeFdcb_handle_old_t110     void setRefcountPipeFd(QEMU_PIPE_HANDLE fd) {
111         if (qemu_pipe_valid(fd)) {
112             numFds++;
113         }
114         hostHandleRefCountFd = fd;
115         numInts = CB_HANDLE_NUM_INTS(numFds);
116     }
117 
canBePostedcb_handle_old_t118     bool canBePosted() const {
119         return (0 != (usage & GRALLOC_USAGE_HW_FB));
120     }
121 
isValidcb_handle_old_t122     bool isValid() const {
123         return (version == sizeof(native_handle)) && (magic == CB_HANDLE_MAGIC_OLD);
124     }
125 
fromcb_handle_old_t126     static cb_handle_old_t* from(void* p) {
127         if (!p) { return NULL; }
128         cb_handle_old_t* cb = static_cast<cb_handle_old_t*>(p);
129         return cb->isValid() ? cb : NULL;
130     }
131 
fromcb_handle_old_t132     static const cb_handle_old_t* from(const void* p) {
133         return from(const_cast<void*>(p));
134     }
135 
from_unconstcb_handle_old_t136     static cb_handle_old_t* from_unconst(const void* p) {
137         return from(const_cast<void*>(p));
138     }
139 
140     int32_t ashmemBasePid;      // process id which mapped the ashmem region
141     int32_t mappedPid;          // process id which succeeded gralloc_register call
142 };
143 
getOpenCountPtr(const cb_handle_old_t * cb)144 int32_t* getOpenCountPtr(const cb_handle_old_t* cb) {
145     return ((int32_t*)cb->getBufferPtr()) + 1;
146 }
147 
getAshmemColorOffset(cb_handle_old_t * cb)148 uint32_t getAshmemColorOffset(cb_handle_old_t* cb) {
149     uint32_t res = 0;
150     if (cb->canBePosted()) res = GOLDFISH_OFFSET_UNIT;
151     if (isHidlGralloc) res = GOLDFISH_OFFSET_UNIT * 2;
152     return res;
153 }
154 
155 //
156 // our private gralloc module structure
157 //
158 struct private_module_t {
159     gralloc_module_t base;
160 };
161 
162 /* If not NULL, this is a pointer to the fallback module.
163  * This really is gralloc.default, which we'll use if we detect
164  * that the emulator we're running in does not support GPU emulation.
165  */
166 static gralloc_module_t*  sFallback;
167 static pthread_once_t     sFallbackOnce = PTHREAD_ONCE_INIT;
168 
169 static void fallback_init(void);  // forward
170 
171 //
172 // Our gralloc device structure (alloc interface)
173 //
174 struct gralloc_device_t {
175     alloc_device_t  device;
176     std::set<buffer_handle_t> allocated;
177     pthread_mutex_t lock;
178 };
179 
180 struct gralloc_memregions_t {
181     typedef std::map<void*, uint32_t> MemRegionMap;  // base -> refCount
182     typedef MemRegionMap::const_iterator mem_region_handle_t;
183 
gralloc_memregions_tgralloc_memregions_t184     gralloc_memregions_t() {
185         pthread_mutex_init(&lock, NULL);
186     }
187 
188     MemRegionMap ashmemRegions;
189     pthread_mutex_t lock;
190 };
191 
192 #define INITIAL_DMA_REGION_SIZE 4096
193 struct gralloc_dmaregion_t {
gralloc_dmaregion_tgralloc_dmaregion_t194     gralloc_dmaregion_t(ExtendedRCEncoderContext *rcEnc)
195       : host_memory_allocator(
196             rcEnc->featureInfo_const()->hasSharedSlotsHostMemoryAllocator),
197         sz(INITIAL_DMA_REGION_SIZE),
198         refcount(0),
199         bigbufCount(0) {
200         memset(&goldfish_dma, 0, sizeof(goldfish_dma));
201         pthread_mutex_init(&lock, NULL);
202 
203         if (rcEnc->hasDirectMem()) {
204             host_memory_allocator.hostMalloc(&address_space_block, sz);
205         } else if (rcEnc->getDmaVersion() > 0) {
206             goldfish_dma_create_region(sz, &goldfish_dma);
207         }
208     }
209 
210     goldfish_dma_context goldfish_dma;
211     GoldfishAddressSpaceHostMemoryAllocator host_memory_allocator;
212     GoldfishAddressSpaceBlock address_space_block;
213     uint32_t sz;
214     uint32_t refcount;
215     pthread_mutex_t lock;
216     uint32_t bigbufCount;
217 };
218 
219 // global device instance
220 static gralloc_memregions_t* s_memregions = NULL;
221 static gralloc_dmaregion_t* s_grdma = NULL;
222 
init_gralloc_memregions()223 static gralloc_memregions_t* init_gralloc_memregions() {
224     if (!s_memregions) {
225         s_memregions = new gralloc_memregions_t;
226     }
227     return s_memregions;
228 }
229 
has_DMA_support(const ExtendedRCEncoderContext * rcEnc)230 static bool has_DMA_support(const ExtendedRCEncoderContext *rcEnc) {
231     return rcEnc->getDmaVersion() > 0 || rcEnc->hasDirectMem();
232 }
233 
init_gralloc_dmaregion(ExtendedRCEncoderContext * rcEnc)234 static gralloc_dmaregion_t* init_gralloc_dmaregion(ExtendedRCEncoderContext *rcEnc) {
235     D("%s: call\n", __func__);
236     if (!s_grdma) {
237         s_grdma = new gralloc_dmaregion_t(rcEnc);
238     }
239     return s_grdma;
240 }
241 
get_gralloc_region(ExtendedRCEncoderContext * rcEnc)242 static void get_gralloc_region(ExtendedRCEncoderContext *rcEnc) {
243     gralloc_dmaregion_t* grdma = init_gralloc_dmaregion(rcEnc);
244 
245     pthread_mutex_lock(&grdma->lock);
246     grdma->refcount++;
247     D("%s: call. refcount: %u\n", __func__, grdma->refcount);
248     pthread_mutex_unlock(&grdma->lock);
249 }
250 
resize_gralloc_dmaregion_locked(gralloc_dmaregion_t * grdma,uint32_t new_sz)251 static void resize_gralloc_dmaregion_locked(gralloc_dmaregion_t* grdma, uint32_t new_sz) {
252     if (grdma->goldfish_dma.mapped_addr) {
253         goldfish_dma_unmap(&grdma->goldfish_dma);
254     }
255     close(grdma->goldfish_dma.fd);
256     goldfish_dma_create_region(new_sz, &grdma->goldfish_dma);
257     grdma->sz = new_sz;
258 }
259 
260 // max dma size: 2x 4K rgba8888
261 #define MAX_DMA_SIZE 66355200
262 
put_gralloc_region_direct_mem_locked(gralloc_dmaregion_t * grdma,uint32_t)263 static bool put_gralloc_region_direct_mem_locked(gralloc_dmaregion_t* grdma, uint32_t /* sz, unused */) {
264     const bool shouldDelete = !grdma->refcount;
265     if (shouldDelete) {
266         grdma->host_memory_allocator.hostFree(&grdma->address_space_block);
267     }
268 
269     return shouldDelete;
270 }
271 
put_gralloc_region_dma_locked(gralloc_dmaregion_t * grdma,uint32_t sz)272 static bool put_gralloc_region_dma_locked(gralloc_dmaregion_t* grdma, uint32_t sz) {
273     D("%s: call. refcount before: %u\n", __func__, grdma->refcount);
274     grdma->refcount--;
275     if (sz > MAX_DMA_SIZE && grdma->bigbufCount) {
276         grdma->bigbufCount--;
277     }
278     bool shouldDelete = !grdma->refcount;
279     if (shouldDelete) {
280         D("%s: should delete!\n", __func__);
281         resize_gralloc_dmaregion_locked(grdma, INITIAL_DMA_REGION_SIZE);
282         D("%s: done\n", __func__);
283     }
284     D("%s: exit\n", __func__);
285     return shouldDelete;
286 }
287 
put_gralloc_region(ExtendedRCEncoderContext * rcEnc,uint32_t sz)288 static bool put_gralloc_region(ExtendedRCEncoderContext *rcEnc, uint32_t sz) {
289     bool shouldDelete;
290 
291     gralloc_dmaregion_t* grdma = init_gralloc_dmaregion(rcEnc);
292     pthread_mutex_lock(&grdma->lock);
293     if (rcEnc->hasDirectMem()) {
294         shouldDelete = put_gralloc_region_direct_mem_locked(grdma, sz);
295     } else if (rcEnc->getDmaVersion() > 0) {
296         shouldDelete = put_gralloc_region_dma_locked(grdma, sz);
297     } else {
298         shouldDelete = false;
299     }
300     pthread_mutex_unlock(&grdma->lock);
301 
302     return shouldDelete;
303 }
304 
gralloc_dmaregion_register_ashmem_direct_mem_locked(gralloc_dmaregion_t * grdma,uint32_t new_sz)305 static void gralloc_dmaregion_register_ashmem_direct_mem_locked(gralloc_dmaregion_t* grdma, uint32_t new_sz) {
306     if (new_sz == grdma->sz) return;
307 
308     GoldfishAddressSpaceHostMemoryAllocator* allocator = &grdma->host_memory_allocator;
309     GoldfishAddressSpaceBlock* block = &grdma->address_space_block;
310     allocator->hostFree(block);
311     allocator->hostMalloc(block, new_sz);
312     grdma->sz = new_sz;
313 }
314 
gralloc_dmaregion_register_ashmem_dma_locked(gralloc_dmaregion_t * grdma,uint32_t new_sz)315 static void gralloc_dmaregion_register_ashmem_dma_locked(gralloc_dmaregion_t* grdma, uint32_t new_sz) {
316     if (new_sz != grdma->sz) {
317         if (new_sz > MAX_DMA_SIZE)  {
318             D("%s: requested sz %u too large (limit %u), set to fallback.",
319               __func__, new_sz, MAX_DMA_SIZE);
320             grdma->bigbufCount++;
321         } else {
322             D("%s: change sz from %u to %u", __func__, grdma->sz, new_sz);
323             resize_gralloc_dmaregion_locked(grdma, new_sz);
324         }
325     }
326     if (!grdma->goldfish_dma.mapped_addr) {
327         goldfish_dma_map(&grdma->goldfish_dma);
328     }
329 }
330 
gralloc_dmaregion_register_ashmem(ExtendedRCEncoderContext * rcEnc,uint32_t sz)331 static void gralloc_dmaregion_register_ashmem(ExtendedRCEncoderContext *rcEnc, uint32_t sz) {
332     gralloc_dmaregion_t* grdma = init_gralloc_dmaregion(rcEnc);
333 
334     pthread_mutex_lock(&grdma->lock);
335     D("%s: for sz %u, refcount %u", __func__, sz, grdma->refcount);
336     const uint32_t new_sz = std::max(grdma->sz, sz);
337 
338     if (rcEnc->hasDirectMem()) {
339         gralloc_dmaregion_register_ashmem_direct_mem_locked(grdma, new_sz);
340     } else if (rcEnc->getDmaVersion() > 0) {
341         gralloc_dmaregion_register_ashmem_dma_locked(grdma, new_sz);
342     } else {
343         ALOGE("%s: unexpected DMA type", __func__);
344     }
345 
346     pthread_mutex_unlock(&grdma->lock);
347 }
348 
get_mem_region(void * ashmemBase)349 static void get_mem_region(void* ashmemBase) {
350     D("%s: call for %p", __func__, ashmemBase);
351 
352     gralloc_memregions_t* memregions = init_gralloc_memregions();
353 
354     pthread_mutex_lock(&memregions->lock);
355     ++memregions->ashmemRegions[ashmemBase];
356     pthread_mutex_unlock(&memregions->lock);
357 }
358 
put_mem_region(ExtendedRCEncoderContext *,void * ashmemBase)359 static bool put_mem_region(ExtendedRCEncoderContext *, void* ashmemBase) {
360     D("%s: call for %p", __func__, ashmemBase);
361 
362     gralloc_memregions_t* memregions = init_gralloc_memregions();
363     bool shouldRemove;
364 
365     pthread_mutex_lock(&memregions->lock);
366     gralloc_memregions_t::MemRegionMap::iterator i = memregions->ashmemRegions.find(ashmemBase);
367     if (i == memregions->ashmemRegions.end()) {
368         shouldRemove = true;
369         ALOGE("%s: error: tried to put a nonexistent mem region (%p)!", __func__, ashmemBase);
370     } else {
371         shouldRemove = --i->second == 0;
372         if (shouldRemove) {
373             memregions->ashmemRegions.erase(i);
374         }
375     }
376     pthread_mutex_unlock(&memregions->lock);
377 
378     return shouldRemove;
379 }
380 
381 #if DEBUG
dump_regions(ExtendedRCEncoderContext *)382 static void dump_regions(ExtendedRCEncoderContext *) {
383     gralloc_memregions_t* memregions = init_gralloc_memregions();
384     gralloc_memregions_t::mem_region_handle_t curr = memregions->ashmemRegions.begin();
385     std::stringstream res;
386     for (; curr != memregions->ashmemRegions.end(); ++curr) {
387         res << "\tashmem base " << curr->first << " refcount " << curr->second << "\n";
388     }
389     ALOGD("ashmem region dump [\n%s]", res.str().c_str());
390 }
391 #endif
392 
get_ashmem_region(ExtendedRCEncoderContext * rcEnc,cb_handle_old_t * cb)393 static void get_ashmem_region(ExtendedRCEncoderContext *rcEnc, cb_handle_old_t *cb) {
394 #if DEBUG
395     dump_regions(rcEnc);
396 #endif
397 
398     get_mem_region(cb->getBufferPtr());
399 
400 #if DEBUG
401     dump_regions(rcEnc);
402 #endif
403 
404     get_gralloc_region(rcEnc);
405 }
406 
put_ashmem_region(ExtendedRCEncoderContext * rcEnc,cb_handle_old_t * cb)407 static bool put_ashmem_region(ExtendedRCEncoderContext *rcEnc, cb_handle_old_t *cb) {
408 #if DEBUG
409     dump_regions(rcEnc);
410 #endif
411 
412     const bool should_unmap = put_mem_region(rcEnc, cb->getBufferPtr());
413 
414 #if DEBUG
415     dump_regions(rcEnc);
416 #endif
417 
418     put_gralloc_region(rcEnc, cb->bufferSize);
419 
420     return should_unmap;
421 }
422 
map_buffer(cb_handle_old_t * cb,void ** vaddr)423 static int map_buffer(cb_handle_old_t *cb, void **vaddr)
424 {
425     if (cb->bufferFd < 0) {
426         return -EINVAL;
427     }
428 
429     void *addr = mmap(0, cb->bufferSize, PROT_READ | PROT_WRITE,
430                       MAP_SHARED, cb->bufferFd, 0);
431     if (addr == MAP_FAILED) {
432         ALOGE("%s: failed to map ashmem region!", __FUNCTION__);
433         return -errno;
434     }
435 
436     cb->setBufferPtr(addr);
437     cb->ashmemBasePid = getpid();
438     D("%s: %p mapped ashmem base %p size %d\n", __FUNCTION__,
439       cb, addr, cb->bufferSize);
440 
441     *vaddr = addr;
442     return 0;
443 }
444 
445 static HostConnection* sHostCon = NULL;
446 
createOrGetHostConnection()447 static HostConnection* createOrGetHostConnection() {
448     if (!sHostCon) {
449         sHostCon = HostConnection::createUnique().release();
450     }
451     return sHostCon;
452 }
453 
454 #define DEFINE_HOST_CONNECTION \
455     HostConnection *hostCon = createOrGetHostConnection(); \
456     ExtendedRCEncoderContext *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL); \
457     bool hasVulkan = rcEnc->featureInfo_const()->hasVulkan; (void)hasVulkan; \
458 
459 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
460     HostConnection *hostCon = createOrGetHostConnection(); \
461     if (!hostCon) { \
462         ALOGE("gralloc: Failed to get host connection\n"); \
463         return -EIO; \
464     } \
465     ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder(); \
466     if (!rcEnc) { \
467         ALOGE("gralloc: Failed to get renderControl encoder context\n"); \
468         return -EIO; \
469     } \
470     bool hasVulkan = rcEnc->featureInfo_const()->hasVulkan; (void)hasVulkan;\
471 
472 #if PLATFORM_SDK_VERSION < 18
473 // On older APIs, just define it as a value no one is going to use.
474 #define HAL_PIXEL_FORMAT_YCbCr_420_888 0xFFFFFFFF
475 #endif
476 
updateHostColorBuffer(cb_handle_old_t * cb,bool doLocked,char * pixels)477 static void updateHostColorBuffer(cb_handle_old_t* cb,
478                               bool doLocked,
479                               char* pixels) {
480     D("%s: call. doLocked=%d", __FUNCTION__, doLocked);
481 
482     DEFINE_HOST_CONNECTION;
483     gralloc_dmaregion_t* grdma = init_gralloc_dmaregion(rcEnc);
484 
485     int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3;
486     int left = doLocked ? cb->lockedLeft : 0;
487     int top = doLocked ? cb->lockedTop : 0;
488     int width = doLocked ? cb->lockedWidth : cb->width;
489     int height = doLocked ? cb->lockedHeight : cb->height;
490 
491     char* to_send = pixels;
492     uint32_t rgbSz = width * height * bpp;
493     uint32_t send_buffer_size = rgbSz;
494     bool is_rgb_format =
495         cb->format != HAL_PIXEL_FORMAT_YV12 &&
496         cb->format != HAL_PIXEL_FORMAT_YCbCr_420_888;
497 
498     std::vector<char> convertedBuf;
499 
500     if (doLocked && is_rgb_format) {
501         convertedBuf.resize(rgbSz);
502         to_send = &convertedBuf.front();
503         copy_rgb_buffer_from_unlocked(
504                 to_send, pixels,
505                 cb->width,
506                 width, height, top, left, bpp);
507     }
508 
509     const bool hasDMA = has_DMA_support(rcEnc);
510     if (hasDMA && grdma->bigbufCount) {
511         D("%s: there are big buffers alive, use fallback (count %u)", __FUNCTION__,
512           grdma->bigbufCount);
513     }
514 
515     if (hasDMA && !grdma->bigbufCount) {
516         switch (cb->format) {
517         case HAL_PIXEL_FORMAT_YV12:
518             get_yv12_offsets(width, height, NULL, NULL, &send_buffer_size);
519             break;
520 
521         case HAL_PIXEL_FORMAT_YCbCr_420_888:
522             get_yuv420p_offsets(width, height, NULL, NULL, &send_buffer_size);
523             break;
524         }
525 
526         if (grdma->address_space_block.guestPtr()) {
527             rcEnc->bindDmaDirectly(grdma->address_space_block.guestPtr(),
528                                    grdma->address_space_block.physAddr());
529         } else if (grdma->goldfish_dma.mapped_addr) {
530             rcEnc->bindDmaContext(&grdma->goldfish_dma);
531         } else {
532             ALOGE("%s: Unexpected DMA", __func__);
533         }
534 
535         D("%s: call. dma update with sz=%u", __func__, send_buffer_size);
536         pthread_mutex_lock(&grdma->lock);
537         rcEnc->rcUpdateColorBufferDMA(rcEnc, cb->hostHandle,
538                 left, top, width, height,
539                 cb->glFormat, cb->glType,
540                 to_send, send_buffer_size);
541         pthread_mutex_unlock(&grdma->lock);
542     } else {
543         switch (cb->format) {
544         case HAL_PIXEL_FORMAT_YV12:
545             convertedBuf.resize(rgbSz);
546             to_send = &convertedBuf.front();
547             D("convert yv12 to rgb888 here");
548             yv12_to_rgb888(to_send, pixels,
549                            width, height, left, top,
550                            left + width - 1, top + height - 1);
551             break;
552 
553         case HAL_PIXEL_FORMAT_YCbCr_420_888:
554             convertedBuf.resize(rgbSz);
555             to_send = &convertedBuf.front();
556             yuv420p_to_rgb888(to_send, pixels,
557                               width, height, left, top,
558                               left + width - 1, top + height - 1);
559             break;
560         }
561 
562         rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
563                 left, top, width, height,
564                 cb->glFormat, cb->glType, to_send);
565     }
566 }
567 
568 //
569 // gralloc device functions (alloc interface)
570 //
gralloc_dump(struct alloc_device_t *,char *,int)571 static void gralloc_dump(struct alloc_device_t* /*dev*/, char* /*buff*/, int /*buff_len*/) {}
572 
gralloc_get_buffer_format(const int frameworkFormat,const int usage)573 static int gralloc_get_buffer_format(const int frameworkFormat, const int usage) {
574     // Pick the right concrete pixel format given the endpoints as encoded in
575     // the usage bits.  Every end-point pair needs explicit listing here.
576 #if PLATFORM_SDK_VERSION >= 17
577     if (frameworkFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
578         // Camera as producer
579         if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
580             if (usage & GRALLOC_USAGE_HW_TEXTURE) {
581                 // Camera-to-display is RGBA
582                 return HAL_PIXEL_FORMAT_RGBA_8888;
583             } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
584                 // Camera-to-encoder is NV21
585                 return HAL_PIXEL_FORMAT_YCrCb_420_SP;
586             }
587         }
588 
589         ALOGE("gralloc_alloc: Requested auto format selection, "
590               "but no known format for this usage=%x", usage);
591         return -EINVAL;
592     } else if (frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
593         ALOGW("gralloc_alloc: Requested YCbCr_420_888, taking experimental path. "
594               "usage=%x", usage);
595     } else if (frameworkFormat == OMX_COLOR_FormatYUV420Planar &&
596                (usage & GOLDFISH_GRALLOC_USAGE_GPU_DATA_BUFFER)) {
597         ALOGW("gralloc_alloc: Requested OMX_COLOR_FormatYUV420Planar, given "
598               "YCbCr_420_888, taking experimental path. "
599               "usage=%x", usage);
600         return HAL_PIXEL_FORMAT_YCbCr_420_888;
601     }
602 #endif // PLATFORM_SDK_VERSION >= 17
603 
604     return frameworkFormat;
605 }
606 
gralloc_alloc(alloc_device_t * dev,int w,int h,const int frameworkFormat,int usage,buffer_handle_t * pHandle,int * pStride)607 static int gralloc_alloc(alloc_device_t* dev,
608                          int w, int h, const int frameworkFormat, int usage,
609                          buffer_handle_t* pHandle, int* pStride)
610 {
611     D("gralloc_alloc w=%d h=%d usage=0x%x frameworkFormat=0x%x\n", w, h, usage, frameworkFormat);
612 
613     gralloc_device_t *grdev = (gralloc_device_t *)dev;
614     if (!grdev || !pHandle || !pStride) {
615         ALOGE("gralloc_alloc: Bad inputs (grdev: %p, pHandle: %p, pStride: %p",
616                 grdev, pHandle, pStride);
617         return -EINVAL;
618     }
619 
620     const int format = gralloc_get_buffer_format(frameworkFormat, usage);
621     if (format < 0) {
622         return format;
623     }
624 
625     //
626     // Note: in screen capture mode, both sw_write and hw_write will be on
627     // and this is a valid usage
628     //
629     bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
630     bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); (void)hw_write;
631     bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
632     const bool hw_texture = usage & GRALLOC_USAGE_HW_TEXTURE;
633     const bool hw_render = usage & GRALLOC_USAGE_HW_RENDER;
634     const bool hw_2d = usage & GRALLOC_USAGE_HW_2D;
635     const bool hw_composer = usage & GRALLOC_USAGE_HW_COMPOSER;
636     const bool hw_fb = usage & GRALLOC_USAGE_HW_FB;
637     const bool rgb888_unsupported_usage =
638         hw_texture || hw_render || hw_2d || hw_composer || hw_fb;
639 #if PLATFORM_SDK_VERSION >= 17
640     bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
641     bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
642 #else // PLATFORM_SDK_VERSION
643     bool hw_cam_write = false;
644     bool hw_cam_read = false;
645 #endif // PLATFORM_SDK_VERSION
646 #if PLATFORM_SDK_VERSION >= 15
647     bool hw_vid_enc_read = usage & GRALLOC_USAGE_HW_VIDEO_ENCODER;
648 #else // PLATFORM_SDK_VERSION
649     bool hw_vid_enc_read = false;
650 #endif // PLATFORM_SDK_VERSION
651 
652     bool yuv_format = false;
653     bool raw_format = false;
654     int ashmem_size = 0;
655     int stride = w;
656 
657     GLenum glFormat = 0;
658     GLenum glType = 0;
659     EmulatorFrameworkFormat selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_GL_COMPATIBLE;
660 
661     int bpp = 0;
662     int align = 1;
663     switch (format) {
664         case HAL_PIXEL_FORMAT_RGBA_8888:
665         case HAL_PIXEL_FORMAT_RGBX_8888:
666         case HAL_PIXEL_FORMAT_BGRA_8888:
667             bpp = 4;
668             glFormat = GL_RGBA;
669             glType = GL_UNSIGNED_BYTE;
670             break;
671         case HAL_PIXEL_FORMAT_RGB_888:
672             if (rgb888_unsupported_usage) {
673                 return -EINVAL;  // we dont support RGB_888 for HW usage
674             } else {
675                 bpp = 3;
676                 glFormat = GL_RGB;
677                 glType = GL_UNSIGNED_BYTE;
678                 break;
679             }
680         case HAL_PIXEL_FORMAT_RGB_565:
681             bpp = 2;
682             // Workaround: distinguish vs the RGB8/RGBA8
683             // by changing |glFormat| to GL_RGB565
684             // (previously, it was still GL_RGB)
685             glFormat = GL_RGB565;
686             glType = GL_UNSIGNED_SHORT_5_6_5;
687             break;
688 #if PLATFORM_SDK_VERSION >= 26
689         case HAL_PIXEL_FORMAT_RGBA_FP16:
690             bpp = 8;
691             glFormat = GL_RGBA16F;
692             glType = GL_HALF_FLOAT;
693             break;
694         case HAL_PIXEL_FORMAT_RGBA_1010102:
695             bpp = 4;
696             glFormat = GL_RGB10_A2;
697             glType = GL_UNSIGNED_INT_2_10_10_10_REV;
698             break;
699 #endif // PLATFORM_SDK_VERSION >= 26
700 #if PLATFORM_SDK_VERSION >= 21
701         case HAL_PIXEL_FORMAT_RAW16:
702         case HAL_PIXEL_FORMAT_Y16:
703 #elif PLATFORM_SDK_VERSION >= 16
704         case HAL_PIXEL_FORMAT_RAW_SENSOR:
705 #endif
706             bpp = 2;
707             align = 16*bpp;
708             if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) {
709                 // Raw sensor data or Y16 only goes between camera and CPU
710                 return -EINVAL;
711             }
712             // Not expecting to actually create any GL surfaces for this
713             glFormat = GL_LUMINANCE;
714             glType = GL_UNSIGNED_SHORT;
715             raw_format = true;
716             break;
717 #if PLATFORM_SDK_VERSION >= 17
718         case HAL_PIXEL_FORMAT_BLOB:
719             bpp = 1;
720             if (! (sw_read) ) {
721                 // Blob data cannot be used by HW other than camera emulator
722                 // But there is a CTS test trying to have access to it
723                 // BUG: https://buganizer.corp.google.com/issues/37719518
724                 return -EINVAL;
725             }
726             // Not expecting to actually create any GL surfaces for this
727             glFormat = GL_LUMINANCE;
728             glType = GL_UNSIGNED_BYTE;
729             break;
730 #endif // PLATFORM_SDK_VERSION >= 17
731         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
732             align = 1;
733             bpp = 1; // per-channel bpp
734             yuv_format = true;
735             // Not expecting to actually create any GL surfaces for this
736             break;
737         case HAL_PIXEL_FORMAT_YV12:
738             align = 16;
739             bpp = 1; // per-channel bpp
740             yuv_format = true;
741             // We are going to use RGB8888 on the host for Vulkan
742             glFormat = GL_RGBA;
743             glType = GL_UNSIGNED_BYTE;
744             selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YV12;
745             break;
746         case HAL_PIXEL_FORMAT_YCbCr_420_888:
747             align = 1;
748             bpp = 1; // per-channel bpp
749             yuv_format = true;
750             // We are going to use RGB888 on the host
751             glFormat = GL_RGB;
752             glType = GL_UNSIGNED_BYTE;
753             selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
754             break;
755         default:
756             ALOGE("gralloc_alloc: Unknown format %d", format);
757             return -EINVAL;
758     }
759 
760     //
761     // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
762     // Only do this for some h/w usages, not all.
763     // Also do this if we need to read from the surface, in this case the
764     // rendering will still happen on the host but we also need to be able to
765     // read back from the color buffer, which requires that there is a buffer
766     //
767     DEFINE_AND_VALIDATE_HOST_CONNECTION;
768 #if PLATFORM_SDK_VERSION >= 17
769     bool needHostCb = ((!yuv_format && frameworkFormat != HAL_PIXEL_FORMAT_BLOB) ||
770                       usage & GOLDFISH_GRALLOC_USAGE_GPU_DATA_BUFFER ||
771 #else
772     bool needHostCb = (!yuv_format ||
773 #endif // !(PLATFORM_SDK_VERSION >= 17)
774                        frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
775                        frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) &&
776                        !raw_format &&
777 #if PLATFORM_SDK_VERSION >= 15
778                       (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
779                                 GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
780                                 GRALLOC_USAGE_HW_VIDEO_ENCODER |
781                                 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK))
782 #else // PLATFORM_SDK_VERSION
783                       (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
784                                 GRALLOC_USAGE_HW_2D |
785                                 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK))
786 #endif // PLATFORM_SDK_VERSION
787                       ;
788 
789     if (isHidlGralloc) {
790         if (needHostCb || (usage & GRALLOC_USAGE_HW_FB)) {
791             // keep space for postCounter
792             // AND openCounter for all host cb
793             ashmem_size += GOLDFISH_OFFSET_UNIT * 2;
794         }
795     } else {
796         if (usage & GRALLOC_USAGE_HW_FB) {
797             // keep space for postCounter
798             ashmem_size += GOLDFISH_OFFSET_UNIT * 1;
799         }
800     }
801 
802     // API26 always expect at least one file descriptor is associated with
803     // one color buffer
804     // BUG: 37719038
805     if (PLATFORM_SDK_VERSION >= 26 ||
806         sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
807         // keep space for image on guest memory if SW access is needed
808         // or if the camera is doing writing
809         if (yuv_format) {
810             size_t yStride = (w*bpp + (align - 1)) & ~(align-1);
811             size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1);
812             size_t uvHeight = h / 2;
813             ashmem_size += yStride * h + 2 * (uvHeight * uvStride);
814             stride = yStride / bpp;
815         } else {
816             size_t bpr = (w*bpp + (align-1)) & ~(align-1);
817             ashmem_size += (bpr * h);
818             stride = bpr / bpp;
819         }
820     }
821 
822     D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format,
823       ashmem_size, stride, getCurrentThreadId());
824 
825     //
826     // Allocate space in ashmem if needed
827     //
828     int fd = -1;
829     if (ashmem_size > 0) {
830         // round to page size;
831         ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
832 
833         ALOGD("%s: Creating ashmem region of size %d\n", __FUNCTION__, ashmem_size);
834         fd = ashmem_create_region("gralloc-buffer", ashmem_size);
835         if (fd < 0) {
836             ALOGE("gralloc_alloc failed to create ashmem region: %s\n",
837                     strerror(errno));
838             return -errno;
839         }
840     }
841 
842     cb_handle_old_t *cb = new cb_handle_old_t(fd, ashmem_size, usage,
843                                               w, h, format,
844                                               glFormat, glType);
845 
846     if (ashmem_size > 0) {
847         //
848         // map ashmem region if exist
849         //
850         void *vaddr;
851         int err = map_buffer(cb, &vaddr);
852         if (err) {
853             close(fd);
854             delete cb;
855             return err;
856         }
857     }
858 
859     const bool hasDMA = has_DMA_support(rcEnc);
860 
861     if (needHostCb) {
862         if (hostCon && rcEnc) {
863             GLenum allocFormat = glFormat;
864             // The handling of RGBX_8888 is very subtle. Most of the time
865             // we want it to be treated as RGBA_8888, with the exception
866             // that alpha is always ignored and treated as 1. The solution
867             // is to create 3 channel RGB texture instead and host GL will
868             // handle the Alpha channel.
869             if (HAL_PIXEL_FORMAT_RGBX_8888 == format) {
870                 allocFormat = GL_RGB;
871             }
872 
873             hostCon->lock();
874             if (hasDMA) {
875                 cb->hostHandle = rcEnc->rcCreateColorBufferDMA(rcEnc, w, h, allocFormat, selectedEmuFrameworkFormat);
876             } else {
877                 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, allocFormat);
878             }
879             hostCon->unlock();
880         }
881 
882         if (!cb->hostHandle) {
883             // Could not create colorbuffer on host !!!
884             close(fd);
885             delete cb;
886             ALOGE("%s: failed to create host cb! -EIO", __FUNCTION__);
887             return -EIO;
888         } else {
889             QEMU_PIPE_HANDLE refcountPipeFd = qemu_pipe_open("refcount");
890             if(qemu_pipe_valid(refcountPipeFd)) {
891                 cb->setRefcountPipeFd(refcountPipeFd);
892                 qemu_pipe_write(refcountPipeFd, &cb->hostHandle, 4);
893             }
894             D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
895         }
896 
897         if (isHidlGralloc) { *getOpenCountPtr(cb) = 0; }
898     }
899 
900     //
901     // alloc succeeded - insert the allocated handle to the allocated list
902     //
903     pthread_mutex_lock(&grdev->lock);
904     grdev->allocated.insert(cb);
905     pthread_mutex_unlock(&grdev->lock);
906 
907     *pHandle = cb;
908     D("%s: alloc succeded, new ashmem base and size: %p %d handle: %p",
909       __FUNCTION__, cb->ashmemBase, cb->ashmemSize, cb);
910     switch (frameworkFormat) {
911     case HAL_PIXEL_FORMAT_YCbCr_420_888:
912         *pStride = 0;
913         break;
914     default:
915         *pStride = stride;
916         break;
917     }
918 
919     hostCon->lock();
920     if (hasDMA) {
921         get_gralloc_region(rcEnc);  // map_buffer(cb, ...) refers here
922     }
923     hostCon->unlock();
924 
925     return 0;
926 }
927 
gralloc_free(alloc_device_t * dev,buffer_handle_t handle)928 static int gralloc_free(alloc_device_t* dev,
929                         buffer_handle_t handle)
930 {
931     DEFINE_AND_VALIDATE_HOST_CONNECTION;
932 
933     const cb_handle_old_t *cb = cb_handle_old_t::from(handle);
934     if (!cb) {
935         ERR("gralloc_free: invalid handle %p", handle);
936         return -EINVAL;
937     }
938 
939     D("%s: for buf %p ptr %p size %d\n",
940       __FUNCTION__, handle, cb->getBufferPtr(), cb->bufferSize);
941 
942     if (cb->hostHandle && !cb->hasRefcountPipe()) {
943         int32_t openCount = 1;
944         int32_t* openCountPtr = &openCount;
945 
946         if (isHidlGralloc && cb->getBufferPtr()) {
947             openCountPtr = getOpenCountPtr(cb);
948         }
949 
950         if (*openCountPtr > 0) {
951             D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
952             hostCon->lock();
953             rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
954             hostCon->unlock();
955         } else {
956             D("A rcCloseColorBuffer is owed!!! sdk ver: %d", PLATFORM_SDK_VERSION);
957             *openCountPtr = -1;
958         }
959     }
960 
961     //
962     // detach and unmap ashmem area if present
963     //
964     if (cb->bufferFd > 0) {
965         if (cb->bufferSize > 0 && cb->getBufferPtr()) {
966             D("%s: unmapped %p", __FUNCTION__, cb->getBufferPtr());
967             munmap(cb->getBufferPtr(), cb->bufferSize);
968             put_gralloc_region(rcEnc, cb->bufferSize);
969         }
970         close(cb->bufferFd);
971     }
972 
973     if(qemu_pipe_valid(cb->hostHandleRefCountFd)) {
974         qemu_pipe_close(cb->hostHandleRefCountFd);
975     }
976     D("%s: done", __FUNCTION__);
977     // remove it from the allocated list
978     gralloc_device_t *grdev = (gralloc_device_t *)dev;
979 
980     pthread_mutex_lock(&grdev->lock);
981     grdev->allocated.erase(cb);
982     pthread_mutex_unlock(&grdev->lock);
983 
984     delete cb;
985 
986     D("%s: exit", __FUNCTION__);
987     return 0;
988 }
989 
gralloc_device_close(struct hw_device_t * dev)990 static int gralloc_device_close(struct hw_device_t *dev)
991 {
992     gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
993     if (d) {
994         for (std::set<buffer_handle_t>::const_iterator i = d->allocated.begin();
995              i != d->allocated.end(); ++i) {
996             gralloc_free(&d->device, *i);
997         }
998 
999         delete d;
1000     }
1001     return 0;
1002 }
1003 
1004 //
1005 // gralloc module functions - refcount + locking interface
1006 //
gralloc_register_buffer(gralloc_module_t const * module,buffer_handle_t handle)1007 static int gralloc_register_buffer(gralloc_module_t const* module,
1008                                    buffer_handle_t handle)
1009 {
1010     DEFINE_AND_VALIDATE_HOST_CONNECTION;
1011 
1012     D("%s: start", __FUNCTION__);
1013     pthread_once(&sFallbackOnce, fallback_init);
1014     if (sFallback != NULL) {
1015         return sFallback->registerBuffer(sFallback, handle);
1016     }
1017 
1018     private_module_t *gr = (private_module_t *)module;
1019     if (!gr) {
1020         return -EINVAL;
1021     }
1022 
1023     cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
1024     if (!cb) {
1025         ERR("gralloc_register_buffer(%p): invalid buffer", cb);
1026         return -EINVAL;
1027     }
1028 
1029     D("gralloc_register_buffer(%p) w %d h %d format 0x%x",
1030         handle, cb->width, cb->height, cb->format);
1031 
1032     if (cb->hostHandle != 0 && !cb->hasRefcountPipe()) {
1033         D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
1034         hostCon->lock();
1035         rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
1036         hostCon->unlock();
1037     }
1038 
1039     //
1040     // if the color buffer has ashmem region and it is not mapped in this
1041     // process map it now.
1042     //
1043     if (cb->bufferSize > 0 && cb->mappedPid != getpid()) {
1044         void *vaddr;
1045         int err = map_buffer(cb, &vaddr);
1046         if (err) {
1047             ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
1048             return -err;
1049         }
1050         cb->mappedPid = getpid();
1051 
1052         if (isHidlGralloc) {
1053             int32_t* openCountPtr = getOpenCountPtr(cb);
1054             if (!*openCountPtr) *openCountPtr = 1;
1055         }
1056     }
1057 
1058     if (cb->bufferSize > 0) {
1059         get_ashmem_region(rcEnc, cb);
1060     }
1061 
1062     return 0;
1063 }
1064 
gralloc_unregister_buffer(gralloc_module_t const * module,buffer_handle_t handle)1065 static int gralloc_unregister_buffer(gralloc_module_t const* module,
1066                                      buffer_handle_t handle)
1067 {
1068     DEFINE_AND_VALIDATE_HOST_CONNECTION;
1069 
1070     if (sFallback != NULL) {
1071         return sFallback->unregisterBuffer(sFallback, handle);
1072     }
1073 
1074     private_module_t *gr = (private_module_t *)module;
1075     if (!gr) {
1076         return -EINVAL;
1077     }
1078 
1079     cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
1080     if (!cb) {
1081         ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
1082         return -EINVAL;
1083     }
1084 
1085 
1086     if (cb->hostHandle && !cb->hasRefcountPipe()) {
1087         D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
1088         hostCon->lock();
1089         rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
1090 
1091         if (isHidlGralloc) {
1092             // Queue up another rcCloseColorBuffer if applicable.
1093             // invariant: have ashmem.
1094             if (cb->bufferSize > 0 && cb->mappedPid == getpid()) {
1095                 int32_t* openCountPtr = getOpenCountPtr(cb);
1096                 if (*openCountPtr == -1) {
1097                     D("%s: revenge of the rcCloseColorBuffer!", __func__);
1098                     rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
1099                     *openCountPtr = -2;
1100                 }
1101             }
1102         }
1103         hostCon->unlock();
1104     }
1105 
1106     //
1107     // unmap ashmem region if it was previously mapped in this process
1108     // (through register_buffer)
1109     //
1110     if (cb->bufferSize > 0 && cb->mappedPid == getpid()) {
1111         const bool should_unmap = put_ashmem_region(rcEnc, cb);
1112         if (!should_unmap) goto done;
1113 
1114         int err = munmap(cb->getBufferPtr(), cb->bufferSize);
1115         if (err) {
1116             ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
1117             return -EINVAL;
1118         }
1119         cb->bufferSize = 0;
1120         cb->mappedPid = 0;
1121         D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
1122     }
1123 
1124 done:
1125     D("gralloc_unregister_buffer(%p) done\n", cb);
1126     return 0;
1127 }
1128 
gralloc_lock(gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr)1129 static int gralloc_lock(gralloc_module_t const* module,
1130                         buffer_handle_t handle, int usage,
1131                         int l, int t, int w, int h,
1132                         void** vaddr)
1133 {
1134     if (sFallback != NULL) {
1135         return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
1136     }
1137 
1138     private_module_t *gr = (private_module_t *)module;
1139     if (!gr) {
1140         return -EINVAL;
1141     }
1142 
1143     cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
1144     if (!cb) {
1145         ALOGE("gralloc_lock bad handle\n");
1146         return -EINVAL;
1147     }
1148 
1149     // Validate usage,
1150     //   1. cannot be locked for hw access
1151     //   2. lock for either sw read or write.
1152     //   3. locked sw access must match usage during alloc time.
1153     bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
1154     bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
1155     bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE);
1156     bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
1157 #if PLATFORM_SDK_VERSION >= 17
1158     bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
1159     bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
1160 #else // PLATFORM_SDK_VERSION
1161     bool hw_cam_write = false;
1162     bool hw_cam_read = false;
1163 #endif // PLATFORM_SDK_VERSION
1164 
1165 #if PLATFORM_SDK_VERSION >= 15
1166     bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
1167 #else // PLATFORM_SDK_VERSION
1168     bool hw_vid_enc_read = false;
1169 #endif // PLATFORM_SDK_VERSION
1170 
1171     bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
1172 
1173 #if PLATFORM_SDK_VERSION >= 15
1174     // bug: 30088791
1175     // a buffer was created for GRALLOC_USAGE_HW_VIDEO_ENCODER usage but
1176     // later a software encoder is reading this buffer: this is actually
1177     // legit usage.
1178     sw_read_allowed = sw_read_allowed || (cb->usage & GRALLOC_USAGE_HW_VIDEO_ENCODER);
1179 #endif // PLATFORM_SDK_VERSION >= 15
1180 
1181     bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
1182 
1183     if ( (hw_read || hw_write) ||
1184          (!sw_read && !sw_write &&
1185                  !hw_cam_write && !hw_cam_read &&
1186                  !hw_vid_enc_read) ||
1187          (sw_read && !sw_read_allowed) ||
1188          (sw_write && !sw_write_allowed) ) {
1189         ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage,
1190                 cb->usage);
1191         //This is not exactly an error and loose it up.
1192         //bug: 30784436
1193         //return -EINVAL;
1194     }
1195 
1196     void *cpu_addr = NULL;
1197 
1198     //
1199     // make sure ashmem area is mapped if needed
1200     //
1201     if (cb->canBePosted() || sw_read || sw_write ||
1202             hw_cam_write || hw_cam_read ||
1203             hw_vid_enc_read) {
1204         if (cb->ashmemBasePid != getpid() || !cb->getBufferPtr()) {
1205             return -EACCES;
1206         }
1207 
1208         cpu_addr = (void *)((char*)cb->getBufferPtr() + getAshmemColorOffset(cb));
1209     }
1210 
1211     if (cb->hostHandle) {
1212         // Make sure we have host connection
1213         DEFINE_AND_VALIDATE_HOST_CONNECTION;
1214         hostCon->lock();
1215 
1216         //
1217         // flush color buffer write cache on host and get its sync status.
1218         //
1219         int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
1220                                                             0,
1221                                                             sw_read);
1222         if (hostSyncStatus < 0) {
1223             // host failed the color buffer sync - probably since it was already
1224             // locked for write access. fail the lock.
1225             ALOGE("gralloc_lock cacheFlush failed sw_read=%d\n", sw_read);
1226             return -EBUSY;
1227         }
1228 
1229         // camera delivers bits to the buffer directly and does not require
1230         // an explicit read.
1231         if (sw_read & !(usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
1232             D("gralloc_lock read back color buffer %d %d ashmem base %p sz %d\n",
1233               cb->width, cb->height, cb->ashmemBase, cb->ashmemSize);
1234             void* rgb_addr = cpu_addr;
1235             char* tmpBuf = 0;
1236             if (cb->format == HAL_PIXEL_FORMAT_YV12 ||
1237                 cb->format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1238                 if (rcEnc->hasYUVCache()) {
1239                     uint32_t buffer_size;
1240                     if (cb->format == HAL_PIXEL_FORMAT_YV12) {
1241                        get_yv12_offsets(cb->width, cb->height, NULL, NULL,
1242                                         &buffer_size);
1243                     } else {
1244                        get_yuv420p_offsets(cb->width, cb->height, NULL, NULL,
1245                                            &buffer_size);
1246                     }
1247                     D("read YUV copy from host");
1248                     rcEnc->rcReadColorBufferYUV(rcEnc, cb->hostHandle,
1249                                             0, 0, cb->width, cb->height,
1250                                             rgb_addr, buffer_size);
1251                 } else {
1252                     // We are using RGB888
1253                     tmpBuf = new char[cb->width * cb->height * 3];
1254                     rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
1255                                               0, 0, cb->width, cb->height, cb->glFormat, cb->glType, tmpBuf);
1256                     if (cb->format == HAL_PIXEL_FORMAT_YV12) {
1257                         D("convert rgb888 to yv12 here");
1258                         rgb888_to_yv12((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
1259                     } else if (cb->format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1260                         D("convert rgb888 to yuv420p here");
1261                         rgb888_to_yuv420p((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
1262                     }
1263                     delete [] tmpBuf;
1264                 }
1265             } else {
1266                 rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
1267                         0, 0, cb->width, cb->height, cb->glFormat, cb->glType, rgb_addr);
1268             }
1269         }
1270 
1271         if (has_DMA_support(rcEnc)) {
1272             gralloc_dmaregion_register_ashmem(rcEnc, cb->bufferSize);
1273         }
1274         hostCon->unlock();
1275     }
1276 
1277     //
1278     // is virtual address required ?
1279     //
1280     if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) {
1281         *vaddr = cpu_addr;
1282     }
1283 
1284     if (sw_write || hw_cam_write) {
1285         //
1286         // Keep locked region if locked for s/w write access.
1287         //
1288         cb->lockedLeft = l;
1289         cb->lockedTop = t;
1290         cb->lockedWidth = w;
1291         cb->lockedHeight = h;
1292     }
1293 
1294     DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p, base: %p",
1295             vaddr, vaddr ? *vaddr : 0, usage, cpu_addr, cb->ashmemBase);
1296 
1297     return 0;
1298 }
1299 
gralloc_unlock(gralloc_module_t const * module,buffer_handle_t handle)1300 static int gralloc_unlock(gralloc_module_t const* module,
1301                           buffer_handle_t handle)
1302 {
1303     if (sFallback != NULL) {
1304         return sFallback->unlock(sFallback, handle);
1305     }
1306 
1307     private_module_t *gr = (private_module_t *)module;
1308     if (!gr) {
1309         return -EINVAL;
1310     }
1311 
1312     cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
1313     if (!cb) {
1314         ALOGD("%s: invalid cb handle. -EINVAL", __FUNCTION__);
1315         return -EINVAL;
1316     }
1317 
1318     //
1319     // if buffer was locked for s/w write, we need to update the host with
1320     // the updated data
1321     //
1322     if (cb->hostHandle) {
1323 
1324         // Make sure we have host connection
1325         DEFINE_AND_VALIDATE_HOST_CONNECTION;
1326         hostCon->lock();
1327 
1328         char *cpu_addr = (char*)cb->getBufferPtr() + getAshmemColorOffset(cb);
1329 
1330         if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
1331             updateHostColorBuffer(cb, true, cpu_addr);
1332         }
1333         else {
1334             updateHostColorBuffer(cb, false, cpu_addr);
1335         }
1336 
1337         hostCon->unlock();
1338         DD("gralloc_unlock success. cpu_addr: %p", cpu_addr);
1339     }
1340 
1341     cb->lockedWidth = cb->lockedHeight = 0;
1342     return 0;
1343 }
1344 
1345 #if PLATFORM_SDK_VERSION >= 18
gralloc_lock_ycbcr(gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,android_ycbcr * ycbcr)1346 static int gralloc_lock_ycbcr(gralloc_module_t const* module,
1347                               buffer_handle_t handle, int usage,
1348                               int l, int t, int w, int h,
1349                               android_ycbcr *ycbcr)
1350 {
1351     // Not supporting fallback module for YCbCr
1352     if (sFallback != NULL) {
1353         ALOGD("%s: has fallback, return -EINVAL", __FUNCTION__);
1354         return -EINVAL;
1355     }
1356 
1357     if (!ycbcr) {
1358         ALOGE("%s: got NULL ycbcr struct! -EINVAL", __FUNCTION__);
1359         return -EINVAL;
1360     }
1361 
1362     private_module_t *gr = (private_module_t *)module;
1363     if (!gr) {
1364         return -EINVAL;
1365     }
1366 
1367     cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
1368     if (!cb) {
1369         ALOGE("%s: bad colorbuffer handle. -EINVAL", __FUNCTION__);
1370         return -EINVAL;
1371     }
1372 
1373     if (cb->format != HAL_PIXEL_FORMAT_YV12 &&
1374         cb->format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
1375         ALOGE("gralloc_lock_ycbcr can only be used with "
1376                 "HAL_PIXEL_FORMAT_YCbCr_420_888 or HAL_PIXEL_FORMAT_YV12, got %x instead. "
1377                 "-EINVAL",
1378                 cb->format);
1379         return -EINVAL;
1380     }
1381 
1382     usage |= (cb->usage & GRALLOC_USAGE_HW_CAMERA_MASK);
1383 
1384     void *vaddr;
1385     int ret = gralloc_lock(module, handle, usage, l, t, w, h, &vaddr);
1386     if (ret) {
1387         return ret;
1388     }
1389 
1390     uint8_t* cpu_addr = static_cast<uint8_t*>(vaddr);
1391 
1392     // Calculate offsets to underlying YUV data
1393     size_t yStride;
1394     size_t cStride;
1395     size_t cSize;
1396     size_t yOffset;
1397     size_t uOffset;
1398     size_t vOffset;
1399     size_t cStep;
1400     size_t align;
1401     switch (cb->format) {
1402         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1403             yStride = cb->width;
1404             cStride = cb->width;
1405             yOffset = 0;
1406             vOffset = yStride * cb->height;
1407             uOffset = vOffset + 1;
1408             cStep = 2;
1409             break;
1410         case HAL_PIXEL_FORMAT_YV12:
1411             // https://developer.android.com/reference/android/graphics/ImageFormat.html#YV12
1412             align = 16;
1413             yStride = (cb->width + (align -1)) & ~(align-1);
1414             cStride = (yStride / 2 + (align - 1)) & ~(align-1);
1415             yOffset = 0;
1416             cSize = cStride * cb->height/2;
1417             vOffset = yStride * cb->height;
1418             uOffset = vOffset + cSize;
1419             cStep = 1;
1420             break;
1421         case HAL_PIXEL_FORMAT_YCbCr_420_888:
1422             yStride = cb->width;
1423             cStride = yStride / 2;
1424             yOffset = 0;
1425             uOffset = cb->height * yStride;
1426             vOffset = uOffset + cStride * cb->height / 2;
1427             cStep = 1;
1428             break;
1429         default:
1430             ALOGE("gralloc_lock_ycbcr unexpected internal format %x",
1431                     cb->format);
1432             return -EINVAL;
1433     }
1434 
1435     ycbcr->y = cpu_addr + yOffset;
1436     ycbcr->cb = cpu_addr + uOffset;
1437     ycbcr->cr = cpu_addr + vOffset;
1438     ycbcr->ystride = yStride;
1439     ycbcr->cstride = cStride;
1440     ycbcr->chroma_step = cStep;
1441 
1442     // Zero out reserved fields
1443     memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
1444 
1445     DD("gralloc_lock_ycbcr success. usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, "
1446            ".ystride: %d , .cstride: %d, .chroma_step: %d, base: %p", usage,
1447            ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride,
1448            ycbcr->chroma_step, cb->ashmemBase);
1449 
1450     return 0;
1451 }
1452 #endif // PLATFORM_SDK_VERSION >= 18
1453 
gralloc_device_open(const hw_module_t * module,const char * name,hw_device_t ** device)1454 static int gralloc_device_open(const hw_module_t* module,
1455                                const char* name,
1456                                hw_device_t** device)
1457 {
1458     int status = -EINVAL;
1459 
1460     D("gralloc_device_open %s\n", name);
1461 
1462     pthread_once( &sFallbackOnce, fallback_init );
1463     if (sFallback != NULL) {
1464         return sFallback->common.methods->open(&sFallback->common, name, device);
1465     }
1466 
1467     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
1468 
1469         // Create host connection and keep it in the TLS.
1470         // return error if connection with host can not be established
1471         HostConnection *hostConn = createOrGetHostConnection();
1472         if (!hostConn) {
1473             ALOGE("gralloc: failed to get host connection while opening %s\n", name);
1474             return -EIO;
1475         }
1476 
1477         //
1478         // Allocate memory for the gralloc device (alloc interface)
1479         //
1480         gralloc_device_t *dev = new gralloc_device_t;
1481         if (NULL == dev) {
1482             return -ENOMEM;
1483         }
1484 
1485         // Initialize our device structure
1486         //
1487         dev->device.common.tag = HARDWARE_DEVICE_TAG;
1488         dev->device.common.version = 0;
1489         dev->device.common.module = const_cast<hw_module_t*>(module);
1490         dev->device.common.close = gralloc_device_close;
1491 
1492         dev->device.alloc   = gralloc_alloc;
1493         dev->device.free    = gralloc_free;
1494         dev->device.dump = gralloc_dump;
1495         pthread_mutex_init(&dev->lock, NULL);
1496 
1497         *device = &dev->device.common;
1498         status = 0;
1499     }
1500 
1501     return status;
1502 }
1503 
1504 //
1505 // define the HMI symbol - our module interface
1506 //
1507 static struct hw_module_methods_t gralloc_module_methods = {
1508     .open = gralloc_device_open,
1509 };
1510 
1511 struct private_module_t HAL_MODULE_INFO_SYM = {
1512     base: {
1513         common: {
1514             tag: HARDWARE_MODULE_TAG,
1515 #if PLATFORM_SDK_VERSION >= 18
1516             module_api_version: GRALLOC_MODULE_API_VERSION_0_2,
1517             hal_api_version: 0,
1518 #elif PLATFORM_SDK_VERSION >= 16
1519             module_api_version: 1,
1520             hal_api_version: 0,
1521 #else // PLATFORM_SDK_VERSION
1522             version_major: 1,
1523             version_minor: 0,
1524 #endif // PLATFORM_SDK_VERSION
1525             id: GRALLOC_HARDWARE_MODULE_ID,
1526             name: "Graphics Memory Allocator Module",
1527             author: "The Android Open Source Project",
1528             methods: &gralloc_module_methods,
1529             dso: NULL,
1530             reserved: {0, }
1531         },
1532         registerBuffer: gralloc_register_buffer,
1533         unregisterBuffer: gralloc_unregister_buffer,
1534         lock: gralloc_lock,
1535         unlock: gralloc_unlock,
1536         perform: NULL,
1537 #if PLATFORM_SDK_VERSION >= 18
1538         lock_ycbcr: gralloc_lock_ycbcr,
1539 #endif // PLATFORM_SDK_VERSION >= 18
1540 #if PLATFORM_SDK_VERSION >= 29 // For Q and later
1541         getTransportSize: NULL,
1542         validateBufferSize: NULL,
1543 #endif // PLATFORM_SDK_VERSION >= 29
1544     }
1545 };
1546 
1547 /* This function is called once to detect whether the emulator supports
1548  * GPU emulation (this is done by looking at the qemu.gles kernel
1549  * parameter, which must be == 1 if this is the case).
1550  *
1551  * If not, then load gralloc.default instead as a fallback.
1552  */
1553 
1554 #if __LP64__
1555 static const char kGrallocDefaultSystemPath[] = "/system/lib64/hw/gralloc.goldfish.default.so";
1556 static const char kGrallocDefaultVendorPath[] = "/vendor/lib64/hw/gralloc.goldfish.default.so";
1557 static const char kGrallocDefaultSystemPathPreP[] = "/system/lib64/hw/gralloc.default.so";
1558 static const char kGrallocDefaultVendorPathPreP[] = "/vendor/lib64/hw/gralloc.default.so";
1559 #else
1560 static const char kGrallocDefaultSystemPath[] = "/system/lib/hw/gralloc.goldfish.default.so";
1561 static const char kGrallocDefaultVendorPath[] = "/vendor/lib/hw/gralloc.goldfish.default.so";
1562 static const char kGrallocDefaultSystemPathPreP[] = "/system/lib/hw/gralloc.default.so";
1563 static const char kGrallocDefaultVendorPathPreP[] = "/vendor/lib/hw/gralloc.default.so";
1564 #endif
1565 
1566 static void
fallback_init(void)1567 fallback_init(void)
1568 {
1569     char  prop[PROPERTY_VALUE_MAX];
1570     void* module;
1571 
1572     // cuttlefish case: no fallback (if we use sw rendering,
1573     // we are not using this lib anyway (would use minigbm))
1574     property_get("ro.boot.hardware", prop, "");
1575 
1576     bool isValid = prop[0] != '\0';
1577 
1578     if (isValid && !strcmp(prop, "cutf_cvm")) {
1579         return;
1580     }
1581 
1582     // qemu.gles=0 -> no GLES 2.x support (only 1.x through software).
1583     // qemu.gles=1 -> host-side GPU emulation through EmuGL
1584     // qemu.gles=2 -> guest-side GPU emulation.
1585     property_get("ro.kernel.qemu.gles", prop, "999");
1586 
1587     bool useFallback = false;
1588     switch (atoi(prop)) {
1589         case 0:
1590             useFallback = true;
1591             break;
1592         case 1:
1593             useFallback = false;
1594             break;
1595         case 2:
1596             useFallback = true;
1597             break;
1598         default:
1599             useFallback = false;
1600             break;
1601     }
1602 
1603     if (!useFallback) return;
1604 
1605     ALOGD("Emulator without host-side GPU emulation detected. "
1606           "Loading gralloc.default.so from %s...",
1607           kGrallocDefaultVendorPath);
1608     module = dlopen(kGrallocDefaultVendorPath, RTLD_LAZY | RTLD_LOCAL);
1609     if (!module) {
1610       module = dlopen(kGrallocDefaultVendorPathPreP, RTLD_LAZY | RTLD_LOCAL);
1611     }
1612     if (!module) {
1613         // vendor folder didn't work. try system
1614         ALOGD("gralloc.default.so not found in /vendor. Trying %s...",
1615               kGrallocDefaultSystemPath);
1616         module = dlopen(kGrallocDefaultSystemPath, RTLD_LAZY | RTLD_LOCAL);
1617         if (!module) {
1618           module = dlopen(kGrallocDefaultSystemPathPreP, RTLD_LAZY | RTLD_LOCAL);
1619         }
1620     }
1621 
1622     if (module != NULL) {
1623         sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
1624         if (sFallback == NULL) {
1625             dlclose(module);
1626         }
1627     }
1628     if (sFallback == NULL) {
1629         ALOGE("FATAL: Could not find gralloc.default.so!");
1630     }
1631 }
1632