Android-x86
Fork
Faire un don

  • R/O
  • HTTP
  • SSH
  • HTTPS

device-generic-goldfish-opengl: Commit

device/generic/goldfish-opengl


Commit MetaInfo

Révisione877c8a2a0b1e2a092db0d89d7f4620aef561b7f (tree)
l'heure2017-06-24 05:53:57
Auteurandroid-build-team Robot <android-build-team-robot@goog...>
Commiterandroid-build-team Robot

Message de Log

Merge cherrypicks of [2454758, 2454859, 2454837, 2454965, 2454984, 2455001, 2454760, 2454860, 2454838, 2454899, 2454970, 2455005, 2455027, 2454973, 2455006, 2455061, 2455007, 2454914, 2454987, 2454974] into oc-release

Change-Id: I3cd8a20b7940e3541c22af7cbacb71ce6eb3d0d0

Change Summary

Modification

--- a/Android.mk
+++ b/Android.mk
@@ -24,6 +24,14 @@ EMUGL_COMMON_INCLUDES := $(EMUGL_PATH)/host/include/libOpenglRender $(EMUGL_PATH
2424 #
2525 EMUGL_COMMON_CFLAGS := -DWITH_GLES2 -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)
2626
27+ifeq (O, $(PLATFORM_VERSION_CODENAME))
28+EMUGL_COMMON_CFLAGS += -DGOLDFISH_HIDL_GRALLOC
29+endif
30+
31+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 25 && echo isApi26OrHigher),isApi26OrHigher)
32+EMUGL_COMMON_CFLAGS += -DGOLDFISH_HIDL_GRALLOC
33+endif
34+
2735 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 18 && echo PreJellyBeanMr2),PreJellyBeanMr2)
2836 ifeq ($(ARCH_ARM_HAVE_TLS_REGISTER),true)
2937 EMUGL_COMMON_CFLAGS += -DHAVE_ARM_TLS_REGISTER
--- a/shared/OpenglCodecCommon/TcpStream.cpp
+++ b/shared/OpenglCodecCommon/TcpStream.cpp
@@ -28,6 +28,38 @@
2828 #include <ws2tcpip.h>
2929 #endif
3030
31+static int _socket_loopback_server(int port, int type)
32+{
33+ struct sockaddr_in addr;
34+
35+ memset(&addr, 0, sizeof(addr));
36+ addr.sin_family = AF_INET;
37+ addr.sin_port = htons(port);
38+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
39+
40+
41+ int s = socket(AF_INET, type, 0);
42+ if (s < 0)
43+ return -1;
44+
45+ int n = 1;
46+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof(n));
47+
48+ if (bind(s, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
49+ close(s);
50+ return -1;
51+ }
52+
53+ if (type == SOCK_STREAM) {
54+ if (listen(s, 4) < 0) {
55+ close(s);
56+ return -1;
57+ }
58+ }
59+
60+ return s;
61+}
62+
3163 TcpStream::TcpStream(size_t bufSize) :
3264 SocketStream(bufSize)
3365 {
@@ -49,7 +81,7 @@ TcpStream::TcpStream(int sock, size_t bufSize) :
4981
5082 int TcpStream::listen(unsigned short port)
5183 {
52- m_sock = socket_loopback_server(port, SOCK_STREAM);
84+ m_sock = _socket_loopback_server(port, SOCK_STREAM);
5385 if (!valid()) return int(ERR_INVALID_SOCKET);
5486
5587 return 0;
--- a/system/OpenglSystemCommon/HostConnection.cpp
+++ b/system/OpenglSystemCommon/HostConnection.cpp
@@ -50,12 +50,15 @@ HostConnection::~HostConnection()
5050 }
5151
5252 HostConnection *HostConnection::get() {
53+ return getWithThreadInfo(getEGLThreadInfo());
54+}
55+
56+HostConnection *HostConnection::getWithThreadInfo(EGLThreadInfo* tinfo) {
5357
5458 /* TODO: Make this configurable with a system property */
5559 const int useQemuPipe = USE_QEMU_PIPE;
5660
5761 // Get thread info
58- EGLThreadInfo *tinfo = getEGLThreadInfo();
5962 if (!tinfo) {
6063 return NULL;
6164 }
--- a/system/OpenglSystemCommon/HostConnection.h
+++ b/system/OpenglSystemCommon/HostConnection.h
@@ -107,10 +107,13 @@ private:
107107 GLESMaxVersion m_glesMaxVersion;
108108 };
109109
110+struct EGLThreadInfo;
111+
110112 class HostConnection
111113 {
112114 public:
113115 static HostConnection *get();
116+ static HostConnection *getWithThreadInfo(EGLThreadInfo* tInfo);
114117 static void exit();
115118 ~HostConnection();
116119
--- a/system/OpenglSystemCommon/ThreadInfo.cpp
+++ b/system/OpenglSystemCommon/ThreadInfo.cpp
@@ -18,19 +18,30 @@
1818
1919 thread_store_t s_tls = THREAD_STORE_INITIALIZER;
2020
21+static bool sDefaultTlsDestructorCallback(void* ptr) { return true; }
22+static bool (*sTlsDestructorCallback)(void*) = sDefaultTlsDestructorCallback;
23+
2124 static void tlsDestruct(void *ptr)
2225 {
26+ sTlsDestructorCallback(ptr);
2327 if (ptr) {
2428 EGLThreadInfo *ti = (EGLThreadInfo *)ptr;
2529 delete ti->hostConn;
2630 delete ti;
31+#ifdef __ANDROID__
2732 ((void **)__get_tls())[TLS_SLOT_OPENGL] = NULL;
33+#endif
2834 }
2935 }
3036
31-EGLThreadInfo *slow_getEGLThreadInfo()
37+void setTlsDestructor(tlsDtorCallback func) {
38+ sTlsDestructorCallback = func;
39+}
40+
41+EGLThreadInfo *goldfish_get_egl_tls()
3242 {
33- EGLThreadInfo *ti = (EGLThreadInfo *)thread_store_get(&s_tls);
43+ EGLThreadInfo* ti = (EGLThreadInfo*)thread_store_get(&s_tls);
44+
3445 if (ti) return ti;
3546
3647 ti = new EGLThreadInfo();
--- a/system/OpenglSystemCommon/ThreadInfo.h
+++ b/system/OpenglSystemCommon/ThreadInfo.h
@@ -18,15 +18,14 @@
1818
1919 #include "HostConnection.h"
2020 #include <pthread.h>
21-#ifdef __ANDROID__
22-#include <bionic_tls.h>
23-#endif
2421
22+#include <bionic_tls.h>
2523 struct EGLContext_t;
24+struct HostConnection;
2625
2726 struct EGLThreadInfo
2827 {
29- EGLThreadInfo() : currentContext(NULL), hostConn(NULL), eglError(EGL_SUCCESS) {}
28+ EGLThreadInfo() : currentContext(NULL), hostConn(NULL), eglError(EGL_SUCCESS) { }
3029
3130 EGLContext_t *currentContext;
3231 HostConnection *hostConn;
@@ -34,26 +33,23 @@ struct EGLThreadInfo
3433 };
3534
3635
37-EGLThreadInfo *slow_getEGLThreadInfo();
36+typedef bool (*tlsDtorCallback)(void*);
37+void setTlsDestructor(tlsDtorCallback);
38+
39+extern "C" __attribute__((visibility("default"))) EGLThreadInfo *goldfish_get_egl_tls();
3840
41+inline EGLThreadInfo* getEGLThreadInfo() {
3942 #ifdef __ANDROID__
40- // We have a dedicated TLS slot in bionic
41- inline EGLThreadInfo* getEGLThreadInfo() {
42- EGLThreadInfo *tInfo =
43- (EGLThreadInfo *)(((uintptr_t *)__get_tls())[TLS_SLOT_OPENGL]);
44- if (!tInfo) {
45- tInfo = slow_getEGLThreadInfo();
46- ((uintptr_t *)__get_tls())[TLS_SLOT_OPENGL] = (uintptr_t)tInfo;
47- }
48- return tInfo;
43+ EGLThreadInfo *tInfo =
44+ (EGLThreadInfo *)(((uintptr_t *)__get_tls())[TLS_SLOT_OPENGL]);
45+ if (!tInfo) {
46+ tInfo = goldfish_get_egl_tls();
47+ ((uintptr_t *)__get_tls())[TLS_SLOT_OPENGL] = (uintptr_t)tInfo;
4948 }
49+ return tInfo;
5050 #else
51- inline EGLThreadInfo* getEGLThreadInfo() {
52- return slow_getEGLThreadInfo();
53- }
51+ return goldfish_get_egl_tls();
5452 #endif
55-
56-
57-
53+}
5854
5955 #endif // of _THREAD_INFO_H
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -144,6 +144,18 @@ const char * eglStrError(EGLint err)
144144 return ret; \
145145 }
146146
147+#define DEFINE_AND_VALIDATE_HOST_CONNECTION_FOR_TLS(ret, tls) \
148+ HostConnection *hostCon = HostConnection::getWithThreadInfo(tls); \
149+ if (!hostCon) { \
150+ ALOGE("egl: Failed to get host connection\n"); \
151+ return ret; \
152+ } \
153+ ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder(); \
154+ if (!rcEnc) { \
155+ ALOGE("egl: Failed to get renderControl encoder context\n"); \
156+ return ret; \
157+ }
158+
147159 #define VALIDATE_CONTEXT_RETURN(context,ret) \
148160 if (!(context)) { \
149161 RETURN_ERROR(ret,EGL_BAD_CONTEXT); \
@@ -1112,41 +1124,40 @@ EGLBoolean eglWaitClient()
11121124 return eglWaitGL();
11131125 }
11141126
1115-EGLBoolean eglReleaseThread()
1116-{
1117- EGLThreadInfo *tInfo = getEGLThreadInfo();
1118- if (tInfo) {
1119- tInfo->eglError = EGL_SUCCESS;
1120- EGLContext_t* context = tInfo->currentContext;
1121- if (context) {
1122- // The following code is doing pretty much the same thing as
1123- // eglMakeCurrent(&s_display, EGL_NO_CONTEXT, EGL_NO_SURFACE, EGL_NO_SURFACE)
1124- // with the only issue that we do not require a valid display here.
1125- DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1126- rcEnc->rcMakeCurrent(rcEnc, 0, 0, 0);
1127- if (context->majorVersion > 1) {
1128- hostCon->gl2Encoder()->setClientState(NULL);
1129- hostCon->gl2Encoder()->setSharedGroup(GLSharedGroupPtr());
1130- }
1131- else {
1132- hostCon->glEncoder()->setClientState(NULL);
1133- hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr());
1134- }
1135- context->flags &= ~EGLContext_t::IS_CURRENT;
1127+// We may need to trigger this directly from the TLS destructor.
1128+static EGLBoolean s_eglReleaseThreadImpl(EGLThreadInfo* tInfo) {
1129+ if (!tInfo) return EGL_TRUE;
11361130
1137- if (context->deletePending) {
1138- if (context->rcContext) {
1139- rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1140- context->rcContext = 0;
1141- }
1142- delete context;
1143- }
1144- tInfo->currentContext = 0;
1131+ tInfo->eglError = EGL_SUCCESS;
1132+ EGLContext_t* context = tInfo->currentContext;
1133+
1134+ if (!context) return EGL_TRUE;
1135+
1136+ // The following code is doing pretty much the same thing as
1137+ // eglMakeCurrent(&s_display, EGL_NO_CONTEXT, EGL_NO_SURFACE, EGL_NO_SURFACE)
1138+ // with the only issue that we do not require a valid display here.
1139+ DEFINE_AND_VALIDATE_HOST_CONNECTION_FOR_TLS(EGL_FALSE, tInfo);
1140+ // We are going to call makeCurrent on the null context and surface
1141+ // anyway once we are on the host, so skip rcMakeCurrent here.
1142+ // rcEnc->rcMakeCurrent(rcEnc, 0, 0, 0);
1143+ context->flags &= ~EGLContext_t::IS_CURRENT;
1144+ if (context->deletePending) {
1145+ if (context->rcContext) {
1146+ rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1147+ context->rcContext = 0;
11451148 }
1149+ delete context;
11461150 }
1151+ tInfo->currentContext = 0;
1152+
11471153 return EGL_TRUE;
11481154 }
11491155
1156+EGLBoolean eglReleaseThread()
1157+{
1158+ return s_eglReleaseThreadImpl(getEGLThreadInfo());
1159+}
1160+
11501161 EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint *attrib_list)
11511162 {
11521163 //TODO
@@ -1464,6 +1475,10 @@ EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLC
14641475 VALIDATE_SURFACE_RETURN(draw, EGL_FALSE);
14651476 VALIDATE_SURFACE_RETURN(read, EGL_FALSE);
14661477
1478+ // Only place to initialize the TLS destructor; any
1479+ // thread can suddenly jump in any eglMakeCurrent
1480+ setTlsDestructor((tlsDtorCallback)s_eglReleaseThreadImpl);
1481+
14671482 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
14681483 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
14691484 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
@@ -1496,7 +1511,8 @@ EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLC
14961511 }
14971512
14981513 if (context && (context->flags & EGLContext_t::IS_CURRENT) && (context != tInfo->currentContext)) {
1499- //context is current to another thread
1514+ // context is current to another thread
1515+ ALOGE("%s: error: EGL_BAD_ACCESS: context %p current to another thread!\n", __FUNCTION__, context);
15001516 setErrorReturn(EGL_BAD_ACCESS, EGL_FALSE);
15011517 }
15021518
@@ -1509,7 +1525,7 @@ EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLC
15091525 //Now make the local bind
15101526 if (context) {
15111527
1512- ALOGD("%s: %p: ver %d %d", __FUNCTION__, context, context->majorVersion, context->minorVersion);
1528+ ALOGD("%s: %p: ver %d %d (tinfo %p)", __FUNCTION__, context, context->majorVersion, context->minorVersion, tInfo);
15131529 // This is a nontrivial context.
15141530 // The thread cannot be gralloc-only anymore.
15151531 hostCon->setGrallocOnly(false);
--- a/system/egl/eglDisplay.cpp
+++ b/system/egl/eglDisplay.cpp
@@ -430,6 +430,9 @@ EGLBoolean eglDisplay::getAttribValue(EGLConfig config, EGLint attribIdx, EGLint
430430 return EGL_TRUE;
431431 }
432432
433+#define EGL_COLOR_COMPONENT_TYPE_EXT 0x3339
434+#define EGL_COLOR_COMPONENT_TYPE_FIXED_EXT 0x333A
435+
433436 EGLBoolean eglDisplay::getConfigAttrib(EGLConfig config, EGLint attrib, EGLint * value)
434437 {
435438 if (attrib == EGL_FRAMEBUFFER_TARGET_ANDROID) {
@@ -445,6 +448,10 @@ EGLBoolean eglDisplay::getConfigAttrib(EGLConfig config, EGLint attrib, EGLint *
445448 *value = EGL_DEPTH_ENCODING_NONE_NV;
446449 return EGL_TRUE;
447450 }
451+ if (attrib == EGL_COLOR_COMPONENT_TYPE_EXT) {
452+ *value = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
453+ return EGL_TRUE;
454+ }
448455 //Though it seems that valueFor() is thread-safe, we don't take chanses
449456 pthread_mutex_lock(&m_lock);
450457 EGLBoolean ret = getAttribValue(config, m_attribs.valueFor(attrib), value);
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -30,6 +30,10 @@
3030 #include <cutils/log.h>
3131 #include <cutils/properties.h>
3232
33+#include <set>
34+#include <string>
35+#include <sstream>
36+
3337 /* Set to 1 or 2 to enable debug traces */
3438 #define DEBUG 0
3539
@@ -47,6 +51,23 @@
4751
4852 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
4953
54+#ifdef GOLDFISH_HIDL_GRALLOC
55+static bool isHidlGralloc = true;
56+#else
57+static bool isHidlGralloc = false;
58+#endif
59+
60+int32_t* getOpenCountPtr(cb_handle_t* cb) {
61+ return ((int32_t*)cb->ashmemBase) + 1;
62+}
63+
64+uint32_t getAshmemColorOffset(cb_handle_t* cb) {
65+ uint32_t res = 0;
66+ if (cb->canBePosted()) res = sizeof(intptr_t);
67+ if (isHidlGralloc) res = sizeof(intptr_t) * 2;
68+ return res;
69+}
70+
5071 //
5172 // our private gralloc module structure
5273 //
@@ -63,13 +84,26 @@ static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT;
6384
6485 static void fallback_init(void); // forward
6586
66-
6787 typedef struct _alloc_list_node {
6888 buffer_handle_t handle;
6989 _alloc_list_node *next;
7090 _alloc_list_node *prev;
7191 } AllocListNode;
7292
93+struct MemRegionInfo {
94+ void* ashmemBase;
95+ mutable uint32_t refCount;
96+};
97+
98+struct MemRegionInfoCmp {
99+ bool operator()(const MemRegionInfo& a, const MemRegionInfo& b) const {
100+ return a.ashmemBase < b.ashmemBase;
101+ }
102+};
103+
104+typedef std::set<MemRegionInfo, MemRegionInfoCmp> MemRegionSet;
105+typedef MemRegionSet::iterator mem_region_handle_t;
106+
73107 //
74108 // Our gralloc device structure (alloc interface)
75109 //
@@ -77,9 +111,161 @@ struct gralloc_device_t {
77111 alloc_device_t device;
78112
79113 AllocListNode *allocListHead; // double linked list of allocated buffers
114+ MemRegionSet ashmemRegions; // to track allocations of each ashmem region
80115 pthread_mutex_t lock;
81116 };
82117
118+struct gralloc_memregions_t {
119+ MemRegionSet ashmemRegions;
120+};
121+
122+#define INITIAL_DMA_REGION_SIZE 4096
123+struct gralloc_dmaregion_t {
124+ goldfish_dma_context goldfish_dma;
125+ uint32_t sz;
126+ uint32_t refcount;
127+ pthread_mutex_t lock;
128+};
129+
130+// global device instance
131+static gralloc_memregions_t* s_grdev = NULL;
132+static gralloc_dmaregion_t* s_grdma = NULL;
133+
134+void init_gralloc_memregions() {
135+ if (s_grdev) return;
136+ s_grdev = new gralloc_memregions_t;
137+}
138+
139+void init_gralloc_dmaregion() {
140+ D("%s: call\n", __FUNCTION__);
141+ if (s_grdma) return;
142+
143+ s_grdma = new gralloc_dmaregion_t;
144+ s_grdma->sz = INITIAL_DMA_REGION_SIZE;
145+ s_grdma->refcount = 0;
146+
147+ pthread_mutex_init(&s_grdma->lock, NULL);
148+ pthread_mutex_lock(&s_grdma->lock);
149+ goldfish_dma_create_region(s_grdma->sz, &s_grdma->goldfish_dma);
150+ pthread_mutex_unlock(&s_grdma->lock);
151+}
152+
153+void get_gralloc_dmaregion() {
154+ if (!s_grdma) return;
155+ pthread_mutex_lock(&s_grdma->lock);
156+ s_grdma->refcount++;
157+ D("%s: call. refcount: %u\n", __FUNCTION__, s_grdma->refcount);
158+ pthread_mutex_unlock(&s_grdma->lock);
159+}
160+
161+static void resize_gralloc_dmaregion_locked(uint32_t new_sz) {
162+ if (!s_grdma) return;
163+ if (s_grdma->goldfish_dma.mapped) {
164+ goldfish_dma_unmap(&s_grdma->goldfish_dma);
165+ }
166+ close(s_grdma->goldfish_dma.fd);
167+ goldfish_dma_create_region(new_sz, &s_grdma->goldfish_dma);
168+ s_grdma->sz = new_sz;
169+}
170+
171+bool put_gralloc_dmaregion() {
172+ if (!s_grdma) return false;
173+ pthread_mutex_lock(&s_grdma->lock);
174+ D("%s: call. refcount before: %u\n", __FUNCTION__, s_grdma->refcount);
175+ s_grdma->refcount--;
176+ bool shouldDelete = !s_grdma->refcount;
177+ if (shouldDelete) {
178+ D("%s: should delete!\n", __FUNCTION__);
179+ resize_gralloc_dmaregion_locked(INITIAL_DMA_REGION_SIZE);
180+ D("%s: done\n", __FUNCTION__);
181+ }
182+ pthread_mutex_unlock(&s_grdma->lock);
183+ D("%s: exit\n", __FUNCTION__);
184+ return shouldDelete;
185+}
186+
187+void gralloc_dmaregion_register_ashmem(uint32_t sz) {
188+ if (!s_grdma) return;
189+ pthread_mutex_lock(&s_grdma->lock);
190+ D("%s: for sz %u, refcount %u", __FUNCTION__, sz, s_grdma->refcount);
191+ uint32_t new_sz = std::max(s_grdma->sz, sz);
192+ if (new_sz != s_grdma->sz) {
193+ D("%s: change sz from %u to %u", __FUNCTION__, s_grdma->sz, sz);
194+ resize_gralloc_dmaregion_locked(new_sz);
195+ }
196+ if (!s_grdma->goldfish_dma.mapped) {
197+ goldfish_dma_map(&s_grdma->goldfish_dma);
198+ }
199+ pthread_mutex_unlock(&s_grdma->lock);
200+}
201+
202+void get_mem_region(void* ashmemBase) {
203+ init_gralloc_memregions();
204+ D("%s: call for %p", __FUNCTION__, ashmemBase);
205+ MemRegionInfo lookup;
206+ lookup.ashmemBase = ashmemBase;
207+ mem_region_handle_t handle = s_grdev->ashmemRegions.find(lookup);
208+ if (handle == s_grdev->ashmemRegions.end()) {
209+ MemRegionInfo newRegion;
210+ newRegion.ashmemBase = ashmemBase;
211+ newRegion.refCount = 1;
212+ s_grdev->ashmemRegions.insert(newRegion);
213+ } else {
214+ handle->refCount++;
215+ }
216+}
217+
218+bool put_mem_region(void* ashmemBase) {
219+ init_gralloc_memregions();
220+ D("%s: call for %p", __FUNCTION__, ashmemBase);
221+ MemRegionInfo lookup;
222+ lookup.ashmemBase = ashmemBase;
223+ mem_region_handle_t handle = s_grdev->ashmemRegions.find(lookup);
224+ if (handle == s_grdev->ashmemRegions.end()) {
225+ ALOGE("%s: error: tried to put nonexistent mem region!", __FUNCTION__);
226+ return true;
227+ } else {
228+ handle->refCount--;
229+ bool shouldRemove = !handle->refCount;
230+ if (shouldRemove) {
231+ s_grdev->ashmemRegions.erase(lookup);
232+ }
233+ return shouldRemove;
234+ }
235+}
236+
237+void dump_regions() {
238+ init_gralloc_memregions();
239+ mem_region_handle_t curr = s_grdev->ashmemRegions.begin();
240+ std::stringstream res;
241+ for (; curr != s_grdev->ashmemRegions.end(); curr++) {
242+ res << "\tashmem base " << curr->ashmemBase << " refcount " << curr->refCount << "\n";
243+ }
244+ ALOGD("ashmem region dump [\n%s]", res.str().c_str());
245+}
246+
247+#if DEBUG
248+
249+#define GET_ASHMEM_REGION(cb) \
250+ dump_regions(); \
251+ get_mem_region((void*)cb->ashmemBase); \
252+ dump_regions(); \
253+
254+#define PUT_ASHMEM_REGION(cb) \
255+ dump_regions(); \
256+ bool SHOULD_UNMAP = put_mem_region((void*)cb->ashmemBase); \
257+ dump_regions(); \
258+
259+#else
260+
261+#define GET_ASHMEM_REGION(cb) \
262+ get_mem_region((void*)cb->ashmemBase); \
263+
264+#define PUT_ASHMEM_REGION(cb) \
265+ bool SHOULD_UNMAP = put_mem_region((void*)cb->ashmemBase); \
266+
267+#endif
268+
83269 //
84270 // Our framebuffer device structure
85271 //
@@ -93,6 +279,9 @@ static int map_buffer(cb_handle_t *cb, void **vaddr)
93279 return -EINVAL;
94280 }
95281
282+ int map_flags = MAP_SHARED;
283+ if (isHidlGralloc) map_flags |= MAP_ANONYMOUS;
284+
96285 void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
97286 MAP_SHARED, cb->fd, 0);
98287 if (addr == MAP_FAILED) {
@@ -102,6 +291,8 @@ static int map_buffer(cb_handle_t *cb, void **vaddr)
102291
103292 cb->ashmemBase = intptr_t(addr);
104293 cb->ashmemBasePid = getpid();
294+ D("%s: %p mapped ashmem base %p size %d\n", __FUNCTION__,
295+ cb, cb->ashmemBase, cb->ashmemSize);
105296
106297 *vaddr = addr;
107298 return 0;
@@ -148,7 +339,7 @@ static void updateHostColorBuffer(cb_handle_t* cb,
148339
149340 char* convertedBuf = NULL;
150341 if ((doLocked && is_rgb_format) ||
151- (cb->goldfish_dma.fd < 0 &&
342+ (!s_grdma &&
152343 (doLocked || !is_rgb_format))) {
153344 convertedBuf = new char[rgbSz];
154345 to_send = convertedBuf;
@@ -162,7 +353,7 @@ static void updateHostColorBuffer(cb_handle_t* cb,
162353 width, height, top, left, bpp);
163354 }
164355
165- if (cb->goldfish_dma.fd > 0) {
356+ if (s_grdma) {
166357 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
167358 get_yv12_offsets(width, height, NULL, NULL,
168359 &send_buffer_size);
@@ -172,12 +363,14 @@ static void updateHostColorBuffer(cb_handle_t* cb,
172363 &send_buffer_size);
173364 }
174365
175- rcEnc->bindDmaContext(&cb->goldfish_dma);
366+ rcEnc->bindDmaContext(&s_grdma->goldfish_dma);
176367 D("%s: call. dma update with sz=%u", __FUNCTION__, send_buffer_size);
368+ pthread_mutex_lock(&s_grdma->lock);
177369 rcEnc->rcUpdateColorBufferDMA(rcEnc, cb->hostHandle,
178370 left, top, width, height,
179371 cb->glFormat, cb->glType,
180372 to_send, send_buffer_size);
373+ pthread_mutex_unlock(&s_grdma->lock);
181374 } else {
182375 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
183376 yv12_to_rgb888(to_send, pixels,
@@ -340,8 +533,6 @@ static int gralloc_alloc(alloc_device_t* dev,
340533 selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YV12;
341534 break;
342535 case HAL_PIXEL_FORMAT_YCbCr_420_888:
343- ALOGD("%s: 420_888 format experimental path. "
344- "Initialize rgb565 gl format\n", __FUNCTION__);
345536 align = 1;
346537 bpp = 1; // per-channel bpp
347538 yuv_format = true;
@@ -355,9 +546,39 @@ static int gralloc_alloc(alloc_device_t* dev,
355546 return -EINVAL;
356547 }
357548
358- if (usage & GRALLOC_USAGE_HW_FB) {
359- // keep space for postCounter
360- ashmem_size += sizeof(uint32_t);
549+ //
550+ // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
551+ // Only do this for some h/w usages, not all.
552+ // Also do this if we need to read from the surface, in this case the
553+ // rendering will still happen on the host but we also need to be able to
554+ // read back from the color buffer, which requires that there is a buffer
555+ //
556+ bool needHostCb = (!yuv_format ||
557+ frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
558+ frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) &&
559+#if PLATFORM_SDK_VERSION >= 15
560+ (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
561+ GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
562+ GRALLOC_USAGE_HW_VIDEO_ENCODER |
563+ GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK))
564+#else // PLATFORM_SDK_VERSION
565+ (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
566+ GRALLOC_USAGE_HW_2D |
567+ GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK))
568+#endif // PLATFORM_SDK_VERSION
569+ ;
570+
571+ if (isHidlGralloc) {
572+ if (needHostCb || (usage & GRALLOC_USAGE_HW_FB)) {
573+ // keep space for postCounter
574+ // AND openCounter for all host cb
575+ ashmem_size += sizeof(uint32_t) * 2;
576+ }
577+ } else {
578+ if (usage & GRALLOC_USAGE_HW_FB) {
579+ // keep space for postCounter
580+ ashmem_size += sizeof(uint32_t) * 1;
581+ }
361582 }
362583
363584 if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
@@ -418,14 +639,8 @@ static int gralloc_alloc(alloc_device_t* dev,
418639
419640 if (rcEnc->getDmaVersion() > 0) {
420641 D("%s: creating goldfish dma region of size %lu (cb fd %d)\n", __FUNCTION__, ashmem_size, cb->fd);
421- err = goldfish_dma_create_region(ashmem_size, &cb->goldfish_dma);
422- if (err) {
423- ALOGE("%s: Failed to create goldfish DMA region", __FUNCTION__);
424- } else {
425- goldfish_dma_map(&cb->goldfish_dma);
426- cb->setDmaFd(cb->goldfish_dma.fd);
427- D("%s: done, cbfd %d dmafd1 %d dmafd2 %d", __FUNCTION__, cb->fd, cb->goldfish_dma.fd, cb->dmafd);
428- }
642+ init_gralloc_dmaregion();
643+ get_gralloc_dmaregion();
429644 } else {
430645 cb->goldfish_dma.fd = -1;
431646 }
@@ -433,43 +648,25 @@ static int gralloc_alloc(alloc_device_t* dev,
433648 cb->goldfish_dma.fd = -1;
434649 }
435650
436- //
437- // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
438- // Only do this for some h/w usages, not all.
439- // Also do this if we need to read from the surface, in this case the
440- // rendering will still happen on the host but we also need to be able to
441- // read back from the color buffer, which requires that there is a buffer
442- //
443- if (!yuv_format ||
444- frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
445- frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
446-#if PLATFORM_SDK_VERSION >= 15
447- if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
448- GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER |
449- GRALLOC_USAGE_HW_VIDEO_ENCODER |
450- GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
451-#else // PLATFORM_SDK_VERSION
452- if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
453- GRALLOC_USAGE_HW_2D |
454- GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_MASK) ) {
455-#endif // PLATFORM_SDK_VERSION
456- if (hostCon && rcEnc) {
457- if (cb->goldfish_dma.fd > 0) {
458- cb->hostHandle = rcEnc->rcCreateColorBufferDMA(rcEnc, w, h, glFormat, cb->emuFrameworkFormat);
459- } else {
460- cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
461- }
462- D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
651+ if (needHostCb) {
652+ if (hostCon && rcEnc) {
653+ if (s_grdma) {
654+ cb->hostHandle = rcEnc->rcCreateColorBufferDMA(rcEnc, w, h, glFormat, cb->emuFrameworkFormat);
655+ } else {
656+ cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
463657 }
658+ D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
659+ }
464660
465- if (!cb->hostHandle) {
466- // Could not create colorbuffer on host !!!
467- close(fd);
468- delete cb;
469- ALOGD("%s: failed to create host cb! -EIO", __FUNCTION__);
470- return -EIO;
471- }
661+ if (!cb->hostHandle) {
662+ // Could not create colorbuffer on host !!!
663+ close(fd);
664+ delete cb;
665+ ALOGD("%s: failed to create host cb! -EIO", __FUNCTION__);
666+ return -EIO;
472667 }
668+
669+ if (isHidlGralloc) { *getOpenCountPtr(cb) = 0; }
473670 }
474671
475672 //
@@ -487,6 +684,8 @@ static int gralloc_alloc(alloc_device_t* dev,
487684 pthread_mutex_unlock(&grdev->lock);
488685
489686 *pHandle = cb;
687+ D("%s: alloc succeded, new ashmem base and size: %p %d handle: %p",
688+ __FUNCTION__, cb->ashmemBase, cb->ashmemSize, cb);
490689 switch (frameworkFormat) {
491690 case HAL_PIXEL_FORMAT_YCbCr_420_888:
492691 *pStride = 0;
@@ -501,17 +700,29 @@ static int gralloc_alloc(alloc_device_t* dev,
501700 static int gralloc_free(alloc_device_t* dev,
502701 buffer_handle_t handle)
503702 {
504- D("%s: start", __FUNCTION__);
505703 cb_handle_t *cb = (cb_handle_t *)handle;
506704 if (!cb_handle_t::validate((cb_handle_t*)cb)) {
507705 ERR("gralloc_free: invalid handle");
508706 return -EINVAL;
509707 }
510708
511- if (cb->hostHandle != 0) {
512- DEFINE_AND_VALIDATE_HOST_CONNECTION;
513- D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
514- rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
709+ D("%s: for buf %p ptr %p size %d\n",
710+ __FUNCTION__, handle, cb->ashmemBase, cb->ashmemSize);
711+
712+ if (cb->hostHandle) {
713+ int32_t openCount = 1;
714+ int32_t* openCountPtr = &openCount;
715+
716+ if (isHidlGralloc) { openCountPtr = getOpenCountPtr(cb); }
717+
718+ if (*openCountPtr > 0) {
719+ DEFINE_AND_VALIDATE_HOST_CONNECTION;
720+ D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
721+ rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
722+ } else {
723+ D("A rcCloseColorBuffer is owed!!! sdk ver: %d", PLATFORM_SDK_VERSION);
724+ *openCountPtr = -1;
725+ }
515726 }
516727
517728 //
@@ -519,19 +730,12 @@ static int gralloc_free(alloc_device_t* dev,
519730 //
520731 if (cb->fd > 0) {
521732 if (cb->ashmemSize > 0 && cb->ashmemBase) {
733+ D("%s: unmapped %p", __FUNCTION__, cb->ashmemBase);
522734 munmap((void *)cb->ashmemBase, cb->ashmemSize);
735+ put_gralloc_dmaregion();
523736 }
524737 close(cb->fd);
525738 }
526- if (cb->dmafd > 0) {
527- D("%s: unmap and free dma fd %d\n", __FUNCTION__, cb->dmafd);
528- cb->goldfish_dma.fd = cb->dmafd;
529- if (cb->ashmemSize > 0 && cb->ashmemBase) {
530- goldfish_dma_unmap(&cb->goldfish_dma);
531- }
532- goldfish_dma_free(&cb->goldfish_dma);
533- D("%s: closed dma fd %d\n", __FUNCTION__, cb->dmafd);
534- }
535739
536740 D("%s: done", __FUNCTION__);
537741 // remove it from the allocated list
@@ -675,6 +879,7 @@ static int fb_close(struct hw_device_t *dev)
675879 static int gralloc_register_buffer(gralloc_module_t const* module,
676880 buffer_handle_t handle)
677881 {
882+
678883 D("%s: start", __FUNCTION__);
679884 pthread_once(&sFallbackOnce, fallback_init);
680885 if (sFallback != NULL) {
@@ -685,6 +890,7 @@ static int gralloc_register_buffer(gralloc_module_t const* module,
685890
686891 private_module_t *gr = (private_module_t *)module;
687892 cb_handle_t *cb = (cb_handle_t *)handle;
893+
688894 if (!gr || !cb_handle_t::validate(cb)) {
689895 ERR("gralloc_register_buffer(%p): invalid buffer", cb);
690896 return -EINVAL;
@@ -708,18 +914,23 @@ static int gralloc_register_buffer(gralloc_module_t const* module,
708914 return -err;
709915 }
710916 cb->mappedPid = getpid();
711- D("%s: checking to map goldfish dma", __FUNCTION__);
712- if (cb->dmafd > 0) {
713- D("%s: attempting to goldfish dma mmap. cbfd %d dmafd1 %d dmafd2 %d", __FUNCTION__, cb->fd, cb->goldfish_dma.fd, cb->dmafd);
714- D("cxt=%p curr pid %d mapped pid %d",
715- &cb->goldfish_dma,
716- (int)getpid(),
717- cb->mappedPid);
718- if (cb->goldfish_dma.fd != cb->dmafd) {
719- cb->goldfish_dma.fd = cb->dmafd;
720- }
721- goldfish_dma_map(&cb->goldfish_dma);
917+
918+ if (isHidlGralloc) {
919+ int32_t* openCountPtr = getOpenCountPtr(cb);
920+ if (!*openCountPtr) *openCountPtr = 1;
722921 }
922+
923+ DEFINE_AND_VALIDATE_HOST_CONNECTION;
924+ if (rcEnc->getDmaVersion() > 0) {
925+ init_gralloc_dmaregion();
926+ gralloc_dmaregion_register_ashmem(cb->ashmemSize);
927+ }
928+
929+ }
930+
931+ if (cb->ashmemSize > 0) {
932+ GET_ASHMEM_REGION(cb);
933+ get_gralloc_dmaregion();
723934 }
724935
725936 return 0;
@@ -728,22 +939,36 @@ static int gralloc_register_buffer(gralloc_module_t const* module,
728939 static int gralloc_unregister_buffer(gralloc_module_t const* module,
729940 buffer_handle_t handle)
730941 {
731- D("%s: call", __FUNCTION__);
732942 if (sFallback != NULL) {
733943 return sFallback->unregisterBuffer(sFallback, handle);
734944 }
735945
736946 private_module_t *gr = (private_module_t *)module;
737947 cb_handle_t *cb = (cb_handle_t *)handle;
948+
738949 if (!gr || !cb_handle_t::validate(cb)) {
739950 ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
740951 return -EINVAL;
741952 }
742953
743- if (cb->hostHandle != 0) {
744- DEFINE_AND_VALIDATE_HOST_CONNECTION;
954+
955+ if (cb->hostHandle) {
745956 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
957+ DEFINE_AND_VALIDATE_HOST_CONNECTION;
746958 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
959+
960+ if (isHidlGralloc) {
961+ // Queue up another rcCloseColorBuffer if applicable.
962+ // invariant: have ashmem.
963+ if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
964+ int32_t* openCountPtr = getOpenCountPtr(cb);
965+ if (*openCountPtr == -1) {
966+ D("%s: revenge of the rcCloseColorBuffer!", __func__);
967+ rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
968+ *openCountPtr = -2;
969+ }
970+ }
971+ }
747972 }
748973
749974 //
@@ -751,7 +976,14 @@ static int gralloc_unregister_buffer(gralloc_module_t const* module,
751976 // (through register_buffer)
752977 //
753978 if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
979+
980+ PUT_ASHMEM_REGION(cb);
981+ put_gralloc_dmaregion();
982+
983+ if (!SHOULD_UNMAP) goto done;
984+
754985 DEFINE_AND_VALIDATE_HOST_CONNECTION;
986+
755987 void *vaddr;
756988 int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
757989 if (err) {
@@ -761,14 +993,9 @@ static int gralloc_unregister_buffer(gralloc_module_t const* module,
761993 cb->ashmemBase = 0;
762994 cb->mappedPid = 0;
763995 D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
764- if (cb->dmafd > 0) {
765- cb->goldfish_dma.fd = cb->dmafd;
766- D("%s: Unmap dma fd %d (%d)", __FUNCTION__, cb->dmafd, cb->goldfish_dma.fd);
767- goldfish_dma_unmap(&cb->goldfish_dma);
768- }
769996 }
770997
771-
998+done:
772999 D("gralloc_unregister_buffer(%p) done\n", cb);
7731000 return 0;
7741001 }
@@ -786,6 +1013,7 @@ static int gralloc_lock(gralloc_module_t const* module,
7861013
7871014 private_module_t *gr = (private_module_t *)module;
7881015 cb_handle_t *cb = (cb_handle_t *)handle;
1016+
7891017 if (!gr || !cb_handle_t::validate(cb)) {
7901018 ALOGE("gralloc_lock bad handle\n");
7911019 return -EINVAL;
@@ -851,13 +1079,7 @@ static int gralloc_lock(gralloc_module_t const* module,
8511079 return -EACCES;
8521080 }
8531081
854- if (cb->canBePosted()) {
855- postCount = *((intptr_t *)cb->ashmemBase);
856- cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t));
857- }
858- else {
859- cpu_addr = (void *)(cb->ashmemBase);
860- }
1082+ cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
8611083 }
8621084
8631085 if (cb->hostHandle) {
@@ -883,11 +1105,12 @@ static int gralloc_lock(gralloc_module_t const* module,
8831105 char* tmpBuf = 0;
8841106 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
8851107 cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
886- // We are using RGB88
1108+ // We are using RGB888
8871109 tmpBuf = new char[cb->width * cb->height * 3];
8881110 rgb_addr = tmpBuf;
8891111 }
890- D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height);
1112+ D("gralloc_lock read back color buffer %d %d ashmem base %p sz %d\n",
1113+ cb->width, cb->height, cb->ashmemBase, cb->ashmemSize);
8911114 rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
8921115 0, 0, cb->width, cb->height, cb->glFormat, cb->glType, rgb_addr);
8931116 if (tmpBuf) {
@@ -933,6 +1156,7 @@ static int gralloc_unlock(gralloc_module_t const* module,
9331156
9341157 private_module_t *gr = (private_module_t *)module;
9351158 cb_handle_t *cb = (cb_handle_t *)handle;
1159+
9361160 if (!gr || !cb_handle_t::validate(cb)) {
9371161 ALOGD("%s: invalid gr or cb handle. -EINVAL", __FUNCTION__);
9381162 return -EINVAL;
@@ -947,13 +1171,7 @@ static int gralloc_unlock(gralloc_module_t const* module,
9471171 // Make sure we have host connection
9481172 DEFINE_AND_VALIDATE_HOST_CONNECTION;
9491173
950- void *cpu_addr;
951- if (cb->canBePosted()) {
952- cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
953- }
954- else {
955- cpu_addr = (void *)(cb->ashmemBase);
956- }
1174+ void *cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
9571175
9581176 char* rgb_addr = (char *)cpu_addr;
9591177 if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
@@ -1010,13 +1228,7 @@ static int gralloc_lock_ycbcr(gralloc_module_t const* module,
10101228 }
10111229
10121230 uint8_t *cpu_addr = NULL;
1013-
1014- if (cb->canBePosted()) {
1015- cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int));
1016- }
1017- else {
1018- cpu_addr = (uint8_t *)(cb->ashmemBase);
1019- }
1231+ cpu_addr = (uint8_t *)(cb->ashmemBase) + getAshmemColorOffset(cb);
10201232
10211233 // Calculate offsets to underlying YUV data
10221234 size_t yStride;
@@ -1121,6 +1333,7 @@ static int gralloc_device_open(const hw_module_t* module,
11211333 if (NULL == dev) {
11221334 return -ENOMEM;
11231335 }
1336+ memset(dev, 0, sizeof(gralloc_device_t));
11241337
11251338 // Initialize our device structure
11261339 //
Afficher sur ancien navigateur de dépôt.