blob: 416bd5d8c23cfda2fc86702fcb0966171239a9df [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 ** Copyright 2007, 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
17#define LOG_TAG "EGL"
18
19#include <ctype.h>
20#include <string.h>
21#include <errno.h>
22
23#include <sys/ioctl.h>
24
25#if HAVE_ANDROID_OS
26#include <linux/android_pmem.h>
27#endif
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopian07952722009-05-19 19:08:10 -070032#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include <utils/threads.h>
Mathias Agopian07952722009-05-19 19:08:10 -070034#include <binder/IServiceManager.h>
35#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38#include <ui/EGLDisplaySurface.h>
39#include <ui/ISurfaceComposer.h>
40
41#include "hooks.h"
42#include "egl_impl.h"
43
44// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
48/*
49 * we provide our own allocators for the GPU regions, these
50 * allocators go through surfaceflinger
51 */
52
53static Mutex gRegionsLock;
54static request_gpu_t gRegions;
55static sp<ISurfaceComposer> gSurfaceManager;
Mathias Agopian778fb152009-05-08 15:59:34 -070056GL_API ISurfaceComposer* GLES_localSurfaceManager = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58extern egl_connection_t gEGLImpl[2];
59
60const sp<ISurfaceComposer>& getSurfaceFlinger()
61{
62 Mutex::Autolock _l(gRegionsLock);
63
64 /*
65 * There is a little bit of voodoo magic here. We want to access
66 * surfaceflinger for allocating GPU regions, however, when we are
67 * running as part of surfaceflinger, we want to bypass the
68 * service manager because surfaceflinger might not be registered yet.
69 * SurfaceFlinger will populate "GLES_localSurfaceManager" with its
70 * own address, so we can just use that.
71 */
72 if (gSurfaceManager == 0) {
73 if (GLES_localSurfaceManager) {
74 // we're running in SurfaceFlinger's context
75 gSurfaceManager = GLES_localSurfaceManager;
76 } else {
77 // we're a remote process or not part of surfaceflinger,
78 // go through the service manager
79 sp<IServiceManager> sm = defaultServiceManager();
80 if (sm != NULL) {
81 sp<IBinder> binder = sm->getService(String16("SurfaceFlinger"));
82 gSurfaceManager = interface_cast<ISurfaceComposer>(binder);
83 }
84 }
85 }
86 return gSurfaceManager;
87}
88
89class GPURevokeRequester : public BnGPUCallback
90{
91public:
92 virtual void gpuLost() {
93 LOGD("CONTEXT_LOST: Releasing GPU upon request from SurfaceFlinger.");
94 gEGLImpl[IMPL_HARDWARE].hooks = &gHooks[IMPL_CONTEXT_LOST];
95 }
96};
97
98static sp<GPURevokeRequester> gRevokerCallback;
99
100
101request_gpu_t* gpu_acquire(void* user)
102{
103 sp<ISurfaceComposer> server( getSurfaceFlinger() );
104
105 Mutex::Autolock _l(gRegionsLock);
106 if (server == NULL) {
107 return 0;
108 }
109
110 ISurfaceComposer::gpu_info_t info;
111
112 if (gRevokerCallback == 0)
113 gRevokerCallback = new GPURevokeRequester();
114
115 status_t err = server->requestGPU(gRevokerCallback, &info);
116 if (err != NO_ERROR) {
117 LOGD("requestGPU returned %d", err);
118 return 0;
119 }
120
Mathias Agopian16da7952009-05-20 17:58:36 -0700121 if (info.regs == 0) {
122 LOGD("requestGPU() failed");
123 return 0;
124 }
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 bool failed = false;
127 request_gpu_t* gpu = &gRegions;
128 memset(gpu, 0, sizeof(*gpu));
129
130 if (info.regs != 0) {
131 sp<IMemoryHeap> heap(info.regs->getMemory());
132 if (heap != 0) {
133 int fd = heap->heapID();
134 gpu->regs.fd = fd;
135 gpu->regs.base = info.regs->pointer();
136 gpu->regs.size = info.regs->size();
137 gpu->regs.user = info.regs.get();
138#if HAVE_ANDROID_OS
139 struct pmem_region region;
140 if (ioctl(fd, PMEM_GET_PHYS, &region) >= 0)
141 gpu->regs.phys = (void*)region.offset;
142#endif
143 info.regs->incStrong(gpu);
144 } else {
145 LOGE("GPU register handle %p is invalid!", info.regs.get());
146 failed = true;
147 }
148 }
149
150 for (size_t i=0 ; i<info.count && !failed ; i++) {
151 sp<IMemory>& region(info.regions[i].region);
152 if (region != 0) {
153 sp<IMemoryHeap> heap(region->getMemory());
154 if (heap != 0) {
155 const int fd = heap->heapID();
156 gpu->gpu[i].fd = fd;
157 gpu->gpu[i].base = region->pointer();
158 gpu->gpu[i].size = region->size();
159 gpu->gpu[i].user = region.get();
160 gpu->gpu[i].offset = info.regions[i].reserved;
161#if HAVE_ANDROID_OS
162 struct pmem_region reg;
163 if (ioctl(fd, PMEM_GET_PHYS, &reg) >= 0)
164 gpu->gpu[i].phys = (void*)reg.offset;
165#endif
166 region->incStrong(gpu);
167 } else {
168 LOGE("GPU region handle [%d, %p] is invalid!", i, region.get());
169 failed = true;
170 }
171 }
172 }
173
174 if (failed) {
175 // something went wrong, clean up everything!
176 if (gpu->regs.user) {
177 static_cast<IMemory*>(gpu->regs.user)->decStrong(gpu);
178 for (size_t i=0 ; i<info.count ; i++) {
179 if (gpu->gpu[i].user) {
180 static_cast<IMemory*>(gpu->gpu[i].user)->decStrong(gpu);
181 }
182 }
183 }
184 }
185
186 gpu->count = info.count;
187 return gpu;
188}
189
190int gpu_release(void*, request_gpu_t* gpu)
191{
192 sp<IMemory> regs;
193
194 { // scope for lock
195 Mutex::Autolock _l(gRegionsLock);
196 regs = static_cast<IMemory*>(gpu->regs.user);
197 gpu->regs.user = 0;
198 if (regs != 0) regs->decStrong(gpu);
199
200 for (int i=0 ; i<gpu->count ; i++) {
201 sp<IMemory> r(static_cast<IMemory*>(gpu->gpu[i].user));
202 gpu->gpu[i].user = 0;
203 if (r != 0) r->decStrong(gpu);
204 }
205 }
206
207 // there is a special transaction to relinquish the GPU
208 // (it will happen automatically anyway if we don't do this)
209 Parcel data, reply;
210 // NOTE: this transaction does not require an interface token
211 regs->asBinder()->transact(1000, data, &reply);
212 return 1;
213}
214
215// ----------------------------------------------------------------------------
216}; // namespace android
217// ----------------------------------------------------------------------------