blob: cebcc4e4e76cc117662e37c693482b930d2fba16 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 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
Andy McFadden2adaf042012-12-18 09:49:45 -080017#define LOG_TAG "GLConsumer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070028#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029
Jamie Gennis9fea3422012-08-07 18:03:04 -070030#include <hardware/hardware.h>
31
Dan Stozadd264162015-03-12 13:58:47 -070032#include <gui/BufferItem.h>
Mathias Agopianca088332013-03-28 17:44:13 -070033#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/IGraphicBufferAlloc.h>
35#include <gui/ISurfaceComposer.h>
36#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080037
Mathias Agopian90ac7992012-02-25 18:48:35 -080038#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070039#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080040
41#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070042#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044
Jamie Gennisdbe92452013-09-23 17:22:10 -070045EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
46#define CROP_EXT_STR "EGL_ANDROID_image_crop"
Craig Donneraec86972016-04-28 18:09:40 -070047#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
48#define EGL_PROTECTED_CONTENT_EXT 0x32C0
Jamie Gennisdbe92452013-09-23 17:22:10 -070049
Andy McFadden97eba892012-12-11 15:21:45 -080050namespace android {
51
Andy McFadden2adaf042012-12-18 09:49:45 -080052// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080053#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
55//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
56#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
57#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070058
Mathias Agopianad678e12013-07-23 17:28:53 -070059static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080060 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070061 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070062} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080063 "_______________"
64 "_______________"
65 "_____XX_XX_____"
66 "__X_X_____X_X__"
67 "__X_XXXXXXX_X__"
68 "__XXXXXXXXXXX__"
69 "___XX_XXX_XX___"
70 "____XXXXXXX____"
71 "_____X___X_____"
72 "____X_____X____"
73 "_______________"
74 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070075};
Mathias Agopianad678e12013-07-23 17:28:53 -070076
Jamie Gennisf238e282011-01-09 16:33:17 -080077// Transform matrices
78static float mtxIdentity[16] = {
79 1, 0, 0, 0,
80 0, 1, 0, 0,
81 0, 0, 1, 0,
82 0, 0, 0, 1,
83};
84static float mtxFlipH[16] = {
85 -1, 0, 0, 0,
86 0, 1, 0, 0,
87 0, 0, 1, 0,
88 1, 0, 0, 1,
89};
90static float mtxFlipV[16] = {
91 1, 0, 0, 0,
92 0, -1, 0, 0,
93 0, 0, 1, 0,
94 0, 1, 0, 1,
95};
96static float mtxRot90[16] = {
97 0, 1, 0, 0,
98 -1, 0, 0, 0,
99 0, 0, 1, 0,
100 1, 0, 0, 1,
101};
Jamie Gennisf238e282011-01-09 16:33:17 -0800102
103static void mtxMul(float out[16], const float a[16], const float b[16]);
104
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700105Mutex GLConsumer::sStaticInitLock;
106sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -0700107
Jamie Gennisdbe92452013-09-23 17:22:10 -0700108static bool hasEglAndroidImageCropImpl() {
109 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
110 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
111 size_t cropExtLen = strlen(CROP_EXT_STR);
112 size_t extsLen = strlen(exts);
113 bool equal = !strcmp(CROP_EXT_STR, exts);
114 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
115 bool atEnd = (cropExtLen+1) < extsLen &&
116 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
117 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
118 return equal || atStart || atEnd || inMiddle;
119}
120
121static bool hasEglAndroidImageCrop() {
122 // Only compute whether the extension is present once the first time this
123 // function is called.
124 static bool hasIt = hasEglAndroidImageCropImpl();
125 return hasIt;
126}
127
Craig Donneraec86972016-04-28 18:09:40 -0700128static bool hasEglProtectedContentImpl() {
129 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
130 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
131 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
132 size_t extsLen = strlen(exts);
133 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
134 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1);
135 bool atEnd = (cropExtLen+1) < extsLen &&
136 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1));
137 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
138 return equal || atStart || atEnd || inMiddle;
139}
140
141static bool hasEglProtectedContent() {
142 // Only compute whether the extension is present once the first time this
143 // function is called.
144 static bool hasIt = hasEglProtectedContentImpl();
145 return hasIt;
146}
147
Jamie Gennisdbe92452013-09-23 17:22:10 -0700148static bool isEglImageCroppable(const Rect& crop) {
149 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
150}
151
Mathias Agopian3f844832013-08-07 21:24:32 -0700152GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
153 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700154 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700155 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800156 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200157 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800158 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800159 mCurrentTimestamp(0),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700160 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200161 mDefaultWidth(1),
162 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700163 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700164 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800165 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800166 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700167 mEglDisplay(EGL_NO_DISPLAY),
168 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700169 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
170 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800171{
Dan Stozad723bd72014-11-18 10:24:03 -0800172 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800173
Jamie Gennisfa28c352011-09-16 17:30:26 -0700174 memcpy(mCurrentTransformMatrix, mtxIdentity,
175 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700176
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700177 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178}
179
Dan Stozaab574912014-06-25 14:21:45 -0700180GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
181 bool useFenceSync, bool isControlledByApp) :
182 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700183 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700184 mCurrentTransform(0),
185 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
186 mCurrentFence(Fence::NO_FENCE),
187 mCurrentTimestamp(0),
188 mCurrentFrameNumber(0),
189 mDefaultWidth(1),
190 mDefaultHeight(1),
191 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800192 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700193 mUseFenceSync(useFenceSync),
194 mTexTarget(texTarget),
195 mEglDisplay(EGL_NO_DISPLAY),
196 mEglContext(EGL_NO_CONTEXT),
197 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
198 mAttached(false)
199{
Dan Stozad723bd72014-11-18 10:24:03 -0800200 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700201
202 memcpy(mCurrentTransformMatrix, mtxIdentity,
203 sizeof(mCurrentTransformMatrix));
204
205 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
206}
207
Andy McFadden2adaf042012-12-18 09:49:45 -0800208status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700209{
Daniel Lamb2675792012-02-23 14:35:13 -0800210 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700211 if (mAbandoned) {
212 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
213 return NO_INIT;
214 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700215 mDefaultWidth = w;
216 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700217 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700218}
219
Andy McFadden2adaf042012-12-18 09:49:45 -0800220status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800221 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800222 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800223 Mutex::Autolock lock(mMutex);
224
225 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800226 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800227 return NO_INIT;
228 }
229
230 // Make sure the EGL state is the same as in previous calls.
231 status_t err = checkAndUpdateEglStateLocked();
232 if (err != NO_ERROR) {
233 return err;
234 }
235
Dan Stozadd264162015-03-12 13:58:47 -0700236 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800237
238 // Acquire the next buffer.
239 // In asynchronous mode the list is guaranteed to be one buffer
240 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700241 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800242 if (err != NO_ERROR) {
243 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
244 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800245 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800246 glBindTexture(mTexTarget, mTexName);
247 err = NO_ERROR;
248 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800249 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800250 strerror(-err), err);
251 }
252 return err;
253 }
254
255 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700256 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800257 if (err != NO_ERROR) {
258 // We always bind the texture.
259 glBindTexture(mTexTarget, mTexName);
260 return err;
261 }
262
Andy McFadden97eba892012-12-11 15:21:45 -0800263 // Bind the new buffer to the GL texture, and wait until it's ready.
264 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700265}
266
Mathias Agopianad678e12013-07-23 17:28:53 -0700267
268status_t GLConsumer::releaseTexImage() {
269 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800270 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700271 Mutex::Autolock lock(mMutex);
272
273 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800274 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700275 return NO_INIT;
276 }
277
278 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700279 status_t err = NO_ERROR;
280
281 if (mAttached) {
282 err = checkAndUpdateEglStateLocked(true);
283 if (err != NO_ERROR) {
284 return err;
285 }
286 } else {
287 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700288 }
289
290 // Update the GLConsumer state.
291 int buf = mCurrentTexture;
292 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
293
Dan Stozad723bd72014-11-18 10:24:03 -0800294 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700295
Mathias Agopian45155962013-08-08 18:16:21 -0700296 if (mAttached) {
297 // Do whatever sync ops we need to do before releasing the slot.
298 err = syncForReleaseLocked(mEglDisplay);
299 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800300 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700301 return err;
302 }
303 } else {
304 // if we're detached, we just use the fence that was created in detachFromContext()
305 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700306 }
307
Mathias Agopian45155962013-08-08 18:16:21 -0700308 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700309 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800310 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700311 strerror(-err), err);
312 return err;
313 }
314
Eric Penner5c3d2432014-07-11 19:08:04 -0700315 if (mReleasedTexImage == NULL) {
316 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
317 }
318
Mathias Agopianad678e12013-07-23 17:28:53 -0700319 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700320 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700321 mCurrentCrop.makeInvalid();
322 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700323 mCurrentTimestamp = 0;
324 mCurrentFence = Fence::NO_FENCE;
325
Mathias Agopian45155962013-08-08 18:16:21 -0700326 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700327 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800328 status_t result = bindTextureImageLocked();
329 if (result != NO_ERROR) {
330 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700331 }
Mathias Agopian45155962013-08-08 18:16:21 -0700332 } else {
333 // detached, don't touch the texture (and we may not even have an
334 // EGLDisplay here.
335 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700336 }
337
338 return NO_ERROR;
339}
340
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700341sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
342 Mutex::Autolock _l(sStaticInitLock);
343 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
344 // The first time, create the debug texture in case the application
345 // continues to use it.
346 sp<GraphicBuffer> buffer = new GraphicBuffer(
347 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
348 GraphicBuffer::USAGE_SW_WRITE_RARELY);
349 uint32_t* bits;
350 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800351 uint32_t stride = buffer->getStride();
352 uint32_t height = buffer->getHeight();
353 memset(bits, 0, stride * height * 4);
354 for (uint32_t y = 0; y < kDebugData.height; y++) {
355 for (uint32_t x = 0; x < kDebugData.width; x++) {
356 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
357 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700358 }
Dan Stozad723bd72014-11-18 10:24:03 -0800359 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700360 }
361 buffer->unlock();
362 sReleasedTexImageBuffer = buffer;
363 }
364 return sReleasedTexImageBuffer;
365}
366
Dan Stozadd264162015-03-12 13:58:47 -0700367status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700368 nsecs_t presentWhen, uint64_t maxFrameNumber) {
369 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
370 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700371 if (err != NO_ERROR) {
372 return err;
373 }
374
Eric Penner5c3d2432014-07-11 19:08:04 -0700375 // If item->mGraphicBuffer is not null, this buffer has not been acquired
376 // before, so any prior EglImage created is using a stale buffer. This
377 // replaces any old EglImage with a new one (using the new buffer).
378 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700379 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700380 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700381 }
382
Jamie Gennis9fea3422012-08-07 18:03:04 -0700383 return NO_ERROR;
384}
385
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700386status_t GLConsumer::releaseBufferLocked(int buf,
387 sp<GraphicBuffer> graphicBuffer,
388 EGLDisplay display, EGLSyncKHR eglFence) {
389 // release the buffer if it hasn't already been discarded by the
390 // BufferQueue. This can happen, for example, when the producer of this
391 // buffer has reallocated the original buffer slot after this buffer
392 // was acquired.
393 status_t err = ConsumerBase::releaseBufferLocked(
394 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700395 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700396 return err;
397}
398
Dan Stoza3ce46042015-11-17 17:00:45 -0800399status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
400 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800401{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700402 status_t err = NO_ERROR;
403
Pablo Ceballos47650f42015-08-04 16:38:17 -0700404 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700405
Jamie Gennis74bed552012-03-28 19:05:54 -0700406 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800407 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700408 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700409 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700410 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700411 return INVALID_OPERATION;
412 }
413
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800414 // Confirm state.
415 err = checkAndUpdateEglStateLocked();
416 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700417 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700418 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800419 return err;
420 }
421
Pablo Ceballos6366a102016-03-21 16:54:08 -0700422 // For investigating b/27674961
423 if (mEglSlots[slot].mEglImage == nullptr) {
424 ALOGE("If you see this message in a log please post the log to "
425 "b/27674961");
426 ALOGE("slot = %d, mCurrentTexture = %d, mCurrentTextureImage = %p",
427 slot, mCurrentTexture, mCurrentTextureImage.get());
428 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
429 ALOGE("mEglSlots[%d].mEglImage = %p", i,
430 mEglSlots[i].mEglImage.get());
431 }
432 String8 dump;
433 dumpLocked(dump, "");
434 ALOGE("%s", dump.string());
435 }
436
Eric Penner5c3d2432014-07-11 19:08:04 -0700437 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
438 // if nessessary, for the gralloc buffer currently in the slot in
439 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800440 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700441 // means the buffer was previously acquired).
Pablo Ceballos47650f42015-08-04 16:38:17 -0700442 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700443 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800444 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700445 mEglDisplay, slot);
446 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700447 mEglDisplay, EGL_NO_SYNC_KHR);
448 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800449 }
450
451 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800452 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800453 err = syncForReleaseLocked(mEglDisplay);
454 if (err != NO_ERROR) {
455 // Release the buffer we just acquired. It's not safe to
456 // release the old buffer, so instead we just drop the new frame.
457 // As we are still under lock since acquireBuffer, it is safe to
458 // release by slot.
459 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
460 mEglDisplay, EGL_NO_SYNC_KHR);
461 return err;
462 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800463 }
464
Dan Stozad723bd72014-11-18 10:24:03 -0800465 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700466 mCurrentTexture, mCurrentTextureImage != NULL ?
467 mCurrentTextureImage->graphicBufferHandle() : 0,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700468 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800469
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700470 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700471 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700472 // the same.
473 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
474
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800475 // release old buffer
476 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800477 if (pendingRelease == nullptr) {
478 status_t status = releaseBufferLocked(
479 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
480 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
481 if (status < NO_ERROR) {
482 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
483 strerror(-status), status);
484 err = status;
485 // keep going, with error raised [?]
486 }
487 } else {
488 pendingRelease->currentTexture = mCurrentTexture;
489 pendingRelease->graphicBuffer =
490 mCurrentTextureImage->graphicBuffer();
491 pendingRelease->display = mEglDisplay;
492 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
493 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800494 }
495 }
496
Andy McFadden2adaf042012-12-18 09:49:45 -0800497 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700498 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700499 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800500 mCurrentCrop = item.mCrop;
501 mCurrentTransform = item.mTransform;
502 mCurrentScalingMode = item.mScalingMode;
503 mCurrentTimestamp = item.mTimestamp;
504 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700505 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800506
507 computeCurrentTransformMatrixLocked();
508
509 return err;
510}
511
Andy McFadden2adaf042012-12-18 09:49:45 -0800512status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800513 if (mEglDisplay == EGL_NO_DISPLAY) {
514 ALOGE("bindTextureImage: invalid display");
515 return INVALID_OPERATION;
516 }
517
Dan Stozad723bd72014-11-18 10:24:03 -0800518 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800519 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800520 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800521 }
522
523 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700524 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
525 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800526 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700527 return NO_INIT;
528 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800529
Eric Penner5c3d2432014-07-11 19:08:04 -0700530 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700531 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700532 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800533 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700534 mEglDisplay, mCurrentTexture);
535 return UNKNOWN_ERROR;
536 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700537 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
538
Eric Penner2d14a0e2014-08-25 20:16:37 -0700539 // In the rare case that the display is terminated and then initialized
540 // again, we can't detect that the display changed (it didn't), but the
541 // image is invalid. In this case, repeat the exact same steps while
542 // forcing the creation of a new image.
543 if ((error = glGetError()) != GL_NO_ERROR) {
544 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800545 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
546 mCurrentCrop,
547 true);
548 if (result != NO_ERROR) {
549 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700550 mEglDisplay, mCurrentTexture);
551 return UNKNOWN_ERROR;
552 }
553 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
554 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800555 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700556 return UNKNOWN_ERROR;
557 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800558 }
Andy McFadden97eba892012-12-11 15:21:45 -0800559
560 // Wait for the new buffer to be ready.
561 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800562}
563
Mathias Agopian45155962013-08-08 18:16:21 -0700564status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700565 EGLDisplay dpy = eglGetCurrentDisplay();
566 EGLContext ctx = eglGetCurrentContext();
567
Mathias Agopian45155962013-08-08 18:16:21 -0700568 if (!contextCheck) {
569 // if this is the first time we're called, mEglDisplay/mEglContext have
570 // never been set, so don't error out (below).
571 if (mEglDisplay == EGL_NO_DISPLAY) {
572 mEglDisplay = dpy;
573 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700574 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700575 mEglContext = ctx;
576 }
577 }
578
579 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800580 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700581 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700582 }
583
Mathias Agopian45155962013-08-08 18:16:21 -0700584 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800585 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700586 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700587 }
588
589 mEglDisplay = dpy;
590 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800591 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800592}
593
Jesse Hall13f01cb2013-03-20 11:37:21 -0700594void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
595 if (fence->isValid() &&
596 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700597 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700598 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700599 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800600 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700601 strerror(-err), err);
602 }
Jesse Hallef194142012-06-14 14:45:17 -0700603 }
604}
605
Andy McFadden2adaf042012-12-18 09:49:45 -0800606status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700607 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800608 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700609 Mutex::Autolock lock(mMutex);
610
611 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800612 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700613 return NO_INIT;
614 }
615
616 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800617 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700618 "context");
619 return INVALID_OPERATION;
620 }
621
622 EGLDisplay dpy = eglGetCurrentDisplay();
623 EGLContext ctx = eglGetCurrentContext();
624
625 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800626 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700627 return INVALID_OPERATION;
628 }
629
630 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800631 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700632 return INVALID_OPERATION;
633 }
634
635 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
636 status_t err = syncForReleaseLocked(dpy);
637 if (err != OK) {
638 return err;
639 }
640
641 glDeleteTextures(1, &mTexName);
642 }
643
644 mEglDisplay = EGL_NO_DISPLAY;
645 mEglContext = EGL_NO_CONTEXT;
646 mAttached = false;
647
648 return OK;
649}
650
Mathias Agopian3f844832013-08-07 21:24:32 -0700651status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700652 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800653 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700654 Mutex::Autolock lock(mMutex);
655
656 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800657 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700658 return NO_INIT;
659 }
660
661 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800662 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700663 "context");
664 return INVALID_OPERATION;
665 }
666
667 EGLDisplay dpy = eglGetCurrentDisplay();
668 EGLContext ctx = eglGetCurrentContext();
669
670 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800671 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700672 return INVALID_OPERATION;
673 }
674
675 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800676 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700677 return INVALID_OPERATION;
678 }
679
680 // We need to bind the texture regardless of whether there's a current
681 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700682 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700683
Jamie Gennis74bed552012-03-28 19:05:54 -0700684 mEglDisplay = dpy;
685 mEglContext = ctx;
686 mTexName = tex;
687 mAttached = true;
688
Eric Penner5c3d2432014-07-11 19:08:04 -0700689 if (mCurrentTextureImage != NULL) {
690 // This may wait for a buffer a second time. This is likely required if
691 // this is a different context, since otherwise the wait could be skipped
692 // by bouncing through another context. For the same context the extra
693 // wait is redundant.
694 status_t err = bindTextureImageLocked();
695 if (err != NO_ERROR) {
696 return err;
697 }
698 }
699
Jamie Gennis74bed552012-03-28 19:05:54 -0700700 return OK;
701}
702
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800703
Andy McFadden2adaf042012-12-18 09:49:45 -0800704status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800705 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700706
Jamie Gennis01dbf552012-09-06 14:54:19 -0700707 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700708 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700709 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
710 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
711 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800712 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700713 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700714 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700715 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700716 glFlush();
717 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700718 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700719 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800720 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700721 "fd: %#x", eglGetError());
722 return UNKNOWN_ERROR;
723 }
724 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700725 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700726 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700727 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800728 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700729 "%s (%d)", strerror(-err), err);
730 return err;
731 }
Mathias Agopianca088332013-03-28 17:44:13 -0700732 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700733 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
734 if (fence != EGL_NO_SYNC_KHR) {
735 // There is already a fence for the current slot. We need to
736 // wait on that before replacing it with another fence to
737 // ensure that all outstanding buffer accesses have completed
738 // before the producer accesses it.
739 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
740 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800741 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700742 "fence: %#x", eglGetError());
743 return UNKNOWN_ERROR;
744 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800745 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700746 "fence");
747 return TIMED_OUT;
748 }
749 eglDestroySyncKHR(dpy, fence);
750 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700751
Jamie Gennis01dbf552012-09-06 14:54:19 -0700752 // Create a fence for the outstanding accesses in the current
753 // OpenGL ES context.
754 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
755 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800756 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700757 eglGetError());
758 return UNKNOWN_ERROR;
759 }
760 glFlush();
761 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700762 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700763 }
764
765 return OK;
766}
767
Dan Stozad723bd72014-11-18 10:24:03 -0800768bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700769{
770 switch (format) {
771 // supported YUV formats
772 case HAL_PIXEL_FORMAT_YV12:
773 // Legacy/deprecated YUV formats
774 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
775 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
776 case HAL_PIXEL_FORMAT_YCbCr_422_I:
777 return true;
778 }
779
780 // Any OEM format needs to be considered
781 if (format>=0x100 && format<=0x1FF)
782 return true;
783
784 return false;
785}
786
Mathias Agopian3f844832013-08-07 21:24:32 -0700787uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700788 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700789}
790
Andy McFadden2adaf042012-12-18 09:49:45 -0800791void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800792 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700793 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
794}
795
Andy McFadden2adaf042012-12-18 09:49:45 -0800796void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700797 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700798 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800799 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700800 return;
801 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700802 bool needsRecompute = mFilteringEnabled != enabled;
803 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700804
Eric Penner5c3d2432014-07-11 19:08:04 -0700805 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800806 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700807 }
808
Eric Penner5c3d2432014-07-11 19:08:04 -0700809 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700810 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700811 }
812}
813
Andy McFadden2adaf042012-12-18 09:49:45 -0800814void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800815 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700816 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
817 nullptr : mCurrentTextureImage->graphicBuffer();
818 if (buf == nullptr) {
819 GLC_LOGD("computeCurrentTransformMatrixLocked: "
820 "mCurrentTextureImage is NULL");
821 }
822 computeTransformMatrix(mCurrentTransformMatrix, buf,
823 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop,
824 mCurrentTransform, mFilteringEnabled);
825}
826
827void GLConsumer::computeTransformMatrix(float outTransform[16],
828 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
829 bool filtering) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800830
Jamie Gennisa214c642011-01-14 13:53:31 -0800831 float xform[16];
832 for (int i = 0; i < 16; i++) {
833 xform[i] = mtxIdentity[i];
834 }
John Reck1a61da52016-04-28 13:18:15 -0700835 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800836 float result[16];
837 mtxMul(result, xform, mtxFlipH);
838 for (int i = 0; i < 16; i++) {
839 xform[i] = result[i];
840 }
841 }
John Reck1a61da52016-04-28 13:18:15 -0700842 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800843 float result[16];
844 mtxMul(result, xform, mtxFlipV);
845 for (int i = 0; i < 16; i++) {
846 xform[i] = result[i];
847 }
848 }
John Reck1a61da52016-04-28 13:18:15 -0700849 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800850 float result[16];
851 mtxMul(result, xform, mtxRot90);
852 for (int i = 0; i < 16; i++) {
853 xform[i] = result[i];
854 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800855 }
856
Jamie Gennisdbe92452013-09-23 17:22:10 -0700857 float mtxBeforeFlipV[16];
John Reck1a61da52016-04-28 13:18:15 -0700858 if (!cropRect.isEmpty()) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700859 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
860 float bufferWidth = buf->getWidth();
861 float bufferHeight = buf->getHeight();
John Reck1a61da52016-04-28 13:18:15 -0700862 float shrinkAmount = 0.0f;
863 if (filtering) {
864 // In order to prevent bilinear sampling beyond the edge of the
865 // crop rectangle we may need to shrink it by 2 texels in each
866 // dimension. Normally this would just need to take 1/2 a texel
867 // off each end, but because the chroma channels of YUV420 images
868 // are subsampled we may need to shrink the crop region by a whole
869 // texel on each side.
870 switch (buf->getPixelFormat()) {
871 case PIXEL_FORMAT_RGBA_8888:
872 case PIXEL_FORMAT_RGBX_8888:
873 case PIXEL_FORMAT_RGB_888:
874 case PIXEL_FORMAT_RGB_565:
875 case PIXEL_FORMAT_BGRA_8888:
876 // We know there's no subsampling of any channels, so we
877 // only need to shrink by a half a pixel.
878 shrinkAmount = 0.5;
879 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700880
John Reck1a61da52016-04-28 13:18:15 -0700881 default:
882 // If we don't recognize the format, we must assume the
883 // worst case (that we care about), which is YUV420.
884 shrinkAmount = 1.0;
885 break;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700886 }
John Reck1a61da52016-04-28 13:18:15 -0700887 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700888
John Reck1a61da52016-04-28 13:18:15 -0700889 // Only shrink the dimensions that are not the size of the buffer.
890 if (cropRect.width() < bufferWidth) {
891 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
892 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
893 bufferWidth;
894 }
895 if (cropRect.height() < bufferHeight) {
896 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
897 bufferHeight;
898 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
899 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700900 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700901 float crop[16] = {
902 sx, 0, 0, 0,
903 0, sy, 0, 0,
904 0, 0, 1, 0,
905 tx, ty, 0, 1,
906 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700907
Jamie Gennisdbe92452013-09-23 17:22:10 -0700908 mtxMul(mtxBeforeFlipV, crop, xform);
909 } else {
910 for (int i = 0; i < 16; i++) {
911 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700912 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800913 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800914
915 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800916 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800917 // want to expose this to applications, however, so we must add an
918 // additional vertical flip to the transform after all the other transforms.
John Reck1a61da52016-04-28 13:18:15 -0700919 mtxMul(outTransform, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800920}
921
Andy McFadden2adaf042012-12-18 09:49:45 -0800922nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800923 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800924 Mutex::Autolock lock(mMutex);
925 return mCurrentTimestamp;
926}
927
Dan Stozad723bd72014-11-18 10:24:03 -0800928uint64_t GLConsumer::getFrameNumber() {
929 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700930 Mutex::Autolock lock(mMutex);
931 return mCurrentFrameNumber;
932}
933
Andy McFadden2adaf042012-12-18 09:49:45 -0800934sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700935 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700936 return (mCurrentTextureImage == NULL) ?
937 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700938}
939
Andy McFadden2adaf042012-12-18 09:49:45 -0800940Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700941 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700942
943 Rect outCrop = mCurrentCrop;
944 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800945 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
946 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700947
948 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
949 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800950 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700951 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
952 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800953 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700954 }
955
Dan Stozad723bd72014-11-18 10:24:03 -0800956 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
957 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
958
Daniel Lam016c8cb2012-04-03 15:54:58 -0700959 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800960 if (newWidth < currentWidth) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700961 uint32_t dw = currentWidth - newWidth;
962 auto halfdw = dw / 2;
963 outCrop.left += halfdw;
964 // Not halfdw because it would subtract 1 too few when dw is odd
965 outCrop.right -= (dw - halfdw);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700966 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800967 } else if (newHeight < currentHeight) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700968 uint32_t dh = currentHeight - newHeight;
969 auto halfdh = dh / 2;
970 outCrop.top += halfdh;
971 // Not halfdh because it would subtract 1 too few when dh is odd
972 outCrop.bottom -= (dh - halfdh);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700973 }
974
Dan Stozad723bd72014-11-18 10:24:03 -0800975 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700976 outCrop.left, outCrop.top,
977 outCrop.right,outCrop.bottom);
978 }
979
Daniel Lam016c8cb2012-04-03 15:54:58 -0700980 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700981}
982
Andy McFadden2adaf042012-12-18 09:49:45 -0800983uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700984 Mutex::Autolock lock(mMutex);
985 return mCurrentTransform;
986}
987
Andy McFadden2adaf042012-12-18 09:49:45 -0800988uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700989 Mutex::Autolock lock(mMutex);
990 return mCurrentScalingMode;
991}
992
Andy McFadden2adaf042012-12-18 09:49:45 -0800993sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700994 Mutex::Autolock lock(mMutex);
995 return mCurrentFence;
996}
997
Andy McFadden2adaf042012-12-18 09:49:45 -0800998status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700999 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -07001000 return doGLFenceWaitLocked();
1001}
1002
Andy McFadden2adaf042012-12-18 09:49:45 -08001003status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001004
1005 EGLDisplay dpy = eglGetCurrentDisplay();
1006 EGLContext ctx = eglGetCurrentContext();
1007
1008 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -08001009 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001010 return INVALID_OPERATION;
1011 }
1012
1013 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -08001014 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001015 return INVALID_OPERATION;
1016 }
1017
Jamie Gennis1df8c342012-12-20 14:05:45 -08001018 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -07001019 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001020 // Create an EGLSyncKHR from the current fence.
1021 int fenceFd = mCurrentFence->dup();
1022 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -08001023 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001024 return -errno;
1025 }
1026 EGLint attribs[] = {
1027 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
1028 EGL_NONE
1029 };
1030 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
1031 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
1032 if (sync == EGL_NO_SYNC_KHR) {
1033 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -08001034 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001035 eglGetError());
1036 return UNKNOWN_ERROR;
1037 }
1038
1039 // XXX: The spec draft is inconsistent as to whether this should
1040 // return an EGLint or void. Ignore the return value for now, as
1041 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -07001042 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001043 EGLint eglErr = eglGetError();
1044 eglDestroySyncKHR(dpy, sync);
1045 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -08001046 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001047 eglErr);
1048 return UNKNOWN_ERROR;
1049 }
1050 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001051 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001052 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001053 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -08001054 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001055 return err;
1056 }
1057 }
1058 }
1059
1060 return NO_ERROR;
1061}
1062
Andy McFadden2adaf042012-12-18 09:49:45 -08001063void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001064 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001065 if (slotIndex == mCurrentTexture) {
1066 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1067 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001068 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001069 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001070}
Daniel Lameae59d22012-01-22 15:26:27 -08001071
Andy McFadden2adaf042012-12-18 09:49:45 -08001072void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001073 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001074 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001075 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001076}
1077
Andy McFadden2adaf042012-12-18 09:49:45 -08001078void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001079 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001080 if (mAbandoned) {
1081 GLC_LOGE("setName: GLConsumer is abandoned!");
1082 return;
1083 }
Jamie Gennisfa28c352011-09-16 17:30:26 -07001084 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001085 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001086}
1087
Dan Stozad723bd72014-11-18 10:24:03 -08001088status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001089 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001090 if (mAbandoned) {
1091 GLC_LOGE("setDefaultBufferFormat: GLConsumer is abandoned!");
1092 return NO_INIT;
1093 }
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001094 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001095}
1096
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001097status_t GLConsumer::setDefaultBufferDataSpace(
1098 android_dataspace defaultDataSpace) {
1099 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001100 if (mAbandoned) {
1101 GLC_LOGE("setDefaultBufferDataSpace: GLConsumer is abandoned!");
1102 return NO_INIT;
1103 }
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001104 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1105}
1106
Andy McFadden2adaf042012-12-18 09:49:45 -08001107status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001108 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001109 if (mAbandoned) {
1110 GLC_LOGE("setConsumerUsageBits: GLConsumer is abandoned!");
1111 return NO_INIT;
1112 }
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001113 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001114 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001115}
1116
Andy McFadden2adaf042012-12-18 09:49:45 -08001117status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001118 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001119 if (mAbandoned) {
1120 GLC_LOGE("setTransformHint: GLConsumer is abandoned!");
1121 return NO_INIT;
1122 }
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001123 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001124}
1125
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001126status_t GLConsumer::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1127 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001128 if (mAbandoned) {
1129 GLC_LOGE("setMaxAcquiredBufferCount: GLConsumer is abandoned!");
1130 return NO_INIT;
1131 }
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001132 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
1133}
1134
Mathias Agopian74d211a2013-04-22 16:55:35 +02001135void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001136{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001137 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001138 "%smTexName=%d mCurrentTexture=%d\n"
1139 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1140 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1141 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1142 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001143
Mathias Agopian74d211a2013-04-22 16:55:35 +02001144 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001145}
1146
Jamie Gennisf238e282011-01-09 16:33:17 -08001147static void mtxMul(float out[16], const float a[16], const float b[16]) {
1148 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1149 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1150 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1151 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1152
1153 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1154 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1155 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1156 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1157
1158 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1159 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1160 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1161 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1162
1163 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1164 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1165 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1166 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1167}
1168
Eric Penner5c3d2432014-07-11 19:08:04 -07001169GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1170 mGraphicBuffer(graphicBuffer),
1171 mEglImage(EGL_NO_IMAGE_KHR),
Pablo Ceballos60d69222015-08-07 14:47:20 -07001172 mEglDisplay(EGL_NO_DISPLAY),
1173 mCropRect(Rect::EMPTY_RECT) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001174}
1175
1176GLConsumer::EglImage::~EglImage() {
1177 if (mEglImage != EGL_NO_IMAGE_KHR) {
1178 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1179 ALOGE("~EglImage: eglDestroyImageKHR failed");
1180 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001181 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001182 }
1183}
1184
1185status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001186 const Rect& cropRect,
1187 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001188 // If there's an image and it's no longer valid, destroy it.
1189 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1190 bool displayInvalid = mEglDisplay != eglDisplay;
1191 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001192 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001193 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1194 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1195 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001196 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001197 mEglImage = EGL_NO_IMAGE_KHR;
1198 mEglDisplay = EGL_NO_DISPLAY;
1199 }
1200
1201 // If there's no image, create one.
1202 if (mEglImage == EGL_NO_IMAGE_KHR) {
1203 mEglDisplay = eglDisplay;
1204 mCropRect = cropRect;
1205 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1206 }
1207
1208 // Fail if we can't create a valid image.
1209 if (mEglImage == EGL_NO_IMAGE_KHR) {
1210 mEglDisplay = EGL_NO_DISPLAY;
1211 mCropRect.makeInvalid();
1212 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1213 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1214 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1215 buffer->getUsage(), buffer->getPixelFormat());
1216 return UNKNOWN_ERROR;
1217 }
1218
1219 return OK;
1220}
1221
1222void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001223 glEGLImageTargetTexture2DOES(texTarget,
1224 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001225}
1226
1227EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1228 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001229 EGLClientBuffer cbuf =
1230 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -07001231 const bool createProtectedImage =
1232 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
1233 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -07001234 EGLint attrs[] = {
1235 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1236 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1237 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1238 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1239 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
Craig Donneraec86972016-04-28 18:09:40 -07001240 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1241 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001242 EGL_NONE,
1243 };
1244 if (!crop.isValid()) {
1245 // No crop rect to set, so terminate the attrib array before the crop.
1246 attrs[2] = EGL_NONE;
1247 } else if (!isEglImageCroppable(crop)) {
1248 // The crop rect is not at the origin, so we can't set the crop on the
1249 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1250 // extension. In the future we can add a layered extension that
1251 // removes this restriction if there is hardware that can support it.
1252 attrs[2] = EGL_NONE;
1253 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001254 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001255 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1256 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1257 if (image == EGL_NO_IMAGE_KHR) {
1258 EGLint error = eglGetError();
1259 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001260 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001261 }
1262 return image;
1263}
1264
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001265}; // namespace android