blob: b1994da7d4cceccdab62006eafc6fdca93c87f2f [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
Mathias Agopiancb496ac2017-05-22 14:21:00 -070024#include <inttypes.h>
25
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080026#include <EGL/egl.h>
27#include <EGL/eglext.h>
28#include <GLES2/gl2.h>
29#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070030#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080031
Jamie Gennis9fea3422012-08-07 18:03:04 -070032#include <hardware/hardware.h>
33
Dan Stozadd264162015-03-12 13:58:47 -070034#include <gui/BufferItem.h>
Mathias Agopianca088332013-03-28 17:44:13 -070035#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <gui/ISurfaceComposer.h>
37#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080038
Mathias Agopian90ac7992012-02-25 18:48:35 -080039#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070040#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080041
42#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070043#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080045
Jamie Gennisdbe92452013-09-23 17:22:10 -070046EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
47#define CROP_EXT_STR "EGL_ANDROID_image_crop"
Craig Donneraec86972016-04-28 18:09:40 -070048#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
49#define EGL_PROTECTED_CONTENT_EXT 0x32C0
Jamie Gennisdbe92452013-09-23 17:22:10 -070050
Andy McFadden97eba892012-12-11 15:21:45 -080051namespace android {
52
Andy McFadden2adaf042012-12-18 09:49:45 -080053// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080054#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
56//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
57#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
58#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070059
Mathias Agopianad678e12013-07-23 17:28:53 -070060static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080061 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070062 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070063} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080064 "_______________"
65 "_______________"
66 "_____XX_XX_____"
67 "__X_X_____X_X__"
68 "__X_XXXXXXX_X__"
69 "__XXXXXXXXXXX__"
70 "___XX_XXX_XX___"
71 "____XXXXXXX____"
72 "_____X___X_____"
73 "____X_____X____"
74 "_______________"
75 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070076};
Mathias Agopianad678e12013-07-23 17:28:53 -070077
Jamie Gennisf238e282011-01-09 16:33:17 -080078// Transform matrices
79static float mtxIdentity[16] = {
80 1, 0, 0, 0,
81 0, 1, 0, 0,
82 0, 0, 1, 0,
83 0, 0, 0, 1,
84};
85static float mtxFlipH[16] = {
86 -1, 0, 0, 0,
87 0, 1, 0, 0,
88 0, 0, 1, 0,
89 1, 0, 0, 1,
90};
91static float mtxFlipV[16] = {
92 1, 0, 0, 0,
93 0, -1, 0, 0,
94 0, 0, 1, 0,
95 0, 1, 0, 1,
96};
97static float mtxRot90[16] = {
98 0, 1, 0, 0,
99 -1, 0, 0, 0,
100 0, 0, 1, 0,
101 1, 0, 0, 1,
102};
Jamie Gennisf238e282011-01-09 16:33:17 -0800103
104static void mtxMul(float out[16], const float a[16], const float b[16]);
105
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700106Mutex GLConsumer::sStaticInitLock;
107sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -0700108
Jamie Gennisdbe92452013-09-23 17:22:10 -0700109static bool hasEglAndroidImageCropImpl() {
110 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
111 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
112 size_t cropExtLen = strlen(CROP_EXT_STR);
113 size_t extsLen = strlen(exts);
114 bool equal = !strcmp(CROP_EXT_STR, exts);
115 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
116 bool atEnd = (cropExtLen+1) < extsLen &&
117 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
118 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
119 return equal || atStart || atEnd || inMiddle;
120}
121
122static bool hasEglAndroidImageCrop() {
123 // Only compute whether the extension is present once the first time this
124 // function is called.
125 static bool hasIt = hasEglAndroidImageCropImpl();
126 return hasIt;
127}
128
Craig Donneraec86972016-04-28 18:09:40 -0700129static bool hasEglProtectedContentImpl() {
130 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
131 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
132 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
133 size_t extsLen = strlen(exts);
134 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
135 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1);
136 bool atEnd = (cropExtLen+1) < extsLen &&
137 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1));
138 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
Craig Donner4df76b22016-06-13 22:14:15 +0000139 return equal || atStart || atEnd || inMiddle;
Craig Donneraec86972016-04-28 18:09:40 -0700140}
141
142static bool hasEglProtectedContent() {
143 // Only compute whether the extension is present once the first time this
144 // function is called.
145 static bool hasIt = hasEglProtectedContentImpl();
146 return hasIt;
147}
148
Jamie Gennisdbe92452013-09-23 17:22:10 -0700149static bool isEglImageCroppable(const Rect& crop) {
150 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
151}
152
Mathias Agopian3f844832013-08-07 21:24:32 -0700153GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
154 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700155 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700156 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800157 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200158 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800159 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800160 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700161 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700162 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200163 mDefaultWidth(1),
164 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700165 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700166 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800167 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800168 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700169 mEglDisplay(EGL_NO_DISPLAY),
170 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700171 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
172 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800173{
Dan Stozad723bd72014-11-18 10:24:03 -0800174 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800175
Jamie Gennisfa28c352011-09-16 17:30:26 -0700176 memcpy(mCurrentTransformMatrix, mtxIdentity,
177 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700178
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700179 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800180}
181
Dan Stozaab574912014-06-25 14:21:45 -0700182GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
183 bool useFenceSync, bool isControlledByApp) :
184 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700185 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700186 mCurrentTransform(0),
187 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
188 mCurrentFence(Fence::NO_FENCE),
189 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700190 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Dan Stozaab574912014-06-25 14:21:45 -0700191 mCurrentFrameNumber(0),
192 mDefaultWidth(1),
193 mDefaultHeight(1),
194 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800195 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700196 mUseFenceSync(useFenceSync),
197 mTexTarget(texTarget),
198 mEglDisplay(EGL_NO_DISPLAY),
199 mEglContext(EGL_NO_CONTEXT),
200 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
201 mAttached(false)
202{
Dan Stozad723bd72014-11-18 10:24:03 -0800203 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700204
205 memcpy(mCurrentTransformMatrix, mtxIdentity,
206 sizeof(mCurrentTransformMatrix));
207
208 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
209}
210
Andy McFadden2adaf042012-12-18 09:49:45 -0800211status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700212{
Daniel Lamb2675792012-02-23 14:35:13 -0800213 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700214 if (mAbandoned) {
215 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
216 return NO_INIT;
217 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700218 mDefaultWidth = w;
219 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700220 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700221}
222
Andy McFadden2adaf042012-12-18 09:49:45 -0800223status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800224 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800225 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800226 Mutex::Autolock lock(mMutex);
227
228 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800229 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800230 return NO_INIT;
231 }
232
233 // Make sure the EGL state is the same as in previous calls.
234 status_t err = checkAndUpdateEglStateLocked();
235 if (err != NO_ERROR) {
236 return err;
237 }
238
Dan Stozadd264162015-03-12 13:58:47 -0700239 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800240
241 // Acquire the next buffer.
242 // In asynchronous mode the list is guaranteed to be one buffer
243 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700244 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800245 if (err != NO_ERROR) {
246 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
247 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800248 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800249 glBindTexture(mTexTarget, mTexName);
250 err = NO_ERROR;
251 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800252 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800253 strerror(-err), err);
254 }
255 return err;
256 }
257
258 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700259 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800260 if (err != NO_ERROR) {
261 // We always bind the texture.
262 glBindTexture(mTexTarget, mTexName);
263 return err;
264 }
265
Andy McFadden97eba892012-12-11 15:21:45 -0800266 // Bind the new buffer to the GL texture, and wait until it's ready.
267 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700268}
269
Mathias Agopianad678e12013-07-23 17:28:53 -0700270
271status_t GLConsumer::releaseTexImage() {
272 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800273 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700274 Mutex::Autolock lock(mMutex);
275
276 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800277 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700278 return NO_INIT;
279 }
280
281 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700282 status_t err = NO_ERROR;
283
284 if (mAttached) {
285 err = checkAndUpdateEglStateLocked(true);
286 if (err != NO_ERROR) {
287 return err;
288 }
289 } else {
290 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700291 }
292
293 // Update the GLConsumer state.
294 int buf = mCurrentTexture;
295 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
296
Dan Stozad723bd72014-11-18 10:24:03 -0800297 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700298
Mathias Agopian45155962013-08-08 18:16:21 -0700299 if (mAttached) {
300 // Do whatever sync ops we need to do before releasing the slot.
301 err = syncForReleaseLocked(mEglDisplay);
302 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800303 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700304 return err;
305 }
306 } else {
307 // if we're detached, we just use the fence that was created in detachFromContext()
308 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700309 }
310
Mathias Agopian45155962013-08-08 18:16:21 -0700311 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700312 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800313 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700314 strerror(-err), err);
315 return err;
316 }
317
Eric Penner5c3d2432014-07-11 19:08:04 -0700318 if (mReleasedTexImage == NULL) {
319 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
320 }
321
Mathias Agopianad678e12013-07-23 17:28:53 -0700322 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700323 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700324 mCurrentCrop.makeInvalid();
325 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700326 mCurrentTimestamp = 0;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700327 mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopianad678e12013-07-23 17:28:53 -0700328 mCurrentFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700329 mCurrentFenceTime = FenceTime::NO_FENCE;
Mathias Agopianad678e12013-07-23 17:28:53 -0700330
Mathias Agopian45155962013-08-08 18:16:21 -0700331 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700332 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800333 status_t result = bindTextureImageLocked();
334 if (result != NO_ERROR) {
335 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700336 }
Mathias Agopian45155962013-08-08 18:16:21 -0700337 } else {
338 // detached, don't touch the texture (and we may not even have an
339 // EGLDisplay here.
340 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700341 }
342
343 return NO_ERROR;
344}
345
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700346sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
347 Mutex::Autolock _l(sStaticInitLock);
348 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
349 // The first time, create the debug texture in case the application
350 // continues to use it.
351 sp<GraphicBuffer> buffer = new GraphicBuffer(
352 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
Dan Stozad4079af2016-08-22 17:26:41 -0700353 GraphicBuffer::USAGE_SW_WRITE_RARELY,
354 "[GLConsumer debug texture]");
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700355 uint32_t* bits;
356 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800357 uint32_t stride = buffer->getStride();
358 uint32_t height = buffer->getHeight();
359 memset(bits, 0, stride * height * 4);
360 for (uint32_t y = 0; y < kDebugData.height; y++) {
361 for (uint32_t x = 0; x < kDebugData.width; x++) {
362 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
363 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700364 }
Dan Stozad723bd72014-11-18 10:24:03 -0800365 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700366 }
367 buffer->unlock();
368 sReleasedTexImageBuffer = buffer;
369 }
370 return sReleasedTexImageBuffer;
371}
372
Dan Stozadd264162015-03-12 13:58:47 -0700373status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700374 nsecs_t presentWhen, uint64_t maxFrameNumber) {
375 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
376 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700377 if (err != NO_ERROR) {
378 return err;
379 }
380
Eric Penner5c3d2432014-07-11 19:08:04 -0700381 // If item->mGraphicBuffer is not null, this buffer has not been acquired
382 // before, so any prior EglImage created is using a stale buffer. This
383 // replaces any old EglImage with a new one (using the new buffer).
384 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700385 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700386 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700387 }
388
Jamie Gennis9fea3422012-08-07 18:03:04 -0700389 return NO_ERROR;
390}
391
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700392status_t GLConsumer::releaseBufferLocked(int buf,
393 sp<GraphicBuffer> graphicBuffer,
394 EGLDisplay display, EGLSyncKHR eglFence) {
395 // release the buffer if it hasn't already been discarded by the
396 // BufferQueue. This can happen, for example, when the producer of this
397 // buffer has reallocated the original buffer slot after this buffer
398 // was acquired.
399 status_t err = ConsumerBase::releaseBufferLocked(
400 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700401 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700402 return err;
403}
404
Dan Stoza3ce46042015-11-17 17:00:45 -0800405status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
406 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800407{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700408 status_t err = NO_ERROR;
409
Pablo Ceballos47650f42015-08-04 16:38:17 -0700410 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700411
Jamie Gennis74bed552012-03-28 19:05:54 -0700412 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800413 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700414 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700415 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700416 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700417 return INVALID_OPERATION;
418 }
419
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800420 // Confirm state.
421 err = checkAndUpdateEglStateLocked();
422 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700423 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700424 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800425 return err;
426 }
427
Eric Penner5c3d2432014-07-11 19:08:04 -0700428 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
429 // if nessessary, for the gralloc buffer currently in the slot in
430 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800431 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700432 // means the buffer was previously acquired).
Pablo Ceballos47650f42015-08-04 16:38:17 -0700433 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700434 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800435 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700436 mEglDisplay, slot);
437 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700438 mEglDisplay, EGL_NO_SYNC_KHR);
439 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800440 }
441
442 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800443 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800444 err = syncForReleaseLocked(mEglDisplay);
445 if (err != NO_ERROR) {
446 // Release the buffer we just acquired. It's not safe to
447 // release the old buffer, so instead we just drop the new frame.
448 // As we are still under lock since acquireBuffer, it is safe to
449 // release by slot.
450 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
451 mEglDisplay, EGL_NO_SYNC_KHR);
452 return err;
453 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800454 }
455
Dan Stozad723bd72014-11-18 10:24:03 -0800456 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700457 mCurrentTexture, mCurrentTextureImage != NULL ?
458 mCurrentTextureImage->graphicBufferHandle() : 0,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700459 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800460
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700461 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700462 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700463 // the same.
464 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
465
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800466 // release old buffer
467 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800468 if (pendingRelease == nullptr) {
469 status_t status = releaseBufferLocked(
470 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
471 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
472 if (status < NO_ERROR) {
473 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
474 strerror(-status), status);
475 err = status;
476 // keep going, with error raised [?]
477 }
478 } else {
479 pendingRelease->currentTexture = mCurrentTexture;
480 pendingRelease->graphicBuffer =
481 mCurrentTextureImage->graphicBuffer();
482 pendingRelease->display = mEglDisplay;
483 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
484 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800485 }
486 }
487
Andy McFadden2adaf042012-12-18 09:49:45 -0800488 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700489 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700490 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800491 mCurrentCrop = item.mCrop;
492 mCurrentTransform = item.mTransform;
493 mCurrentScalingMode = item.mScalingMode;
494 mCurrentTimestamp = item.mTimestamp;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700495 mCurrentDataSpace = item.mDataSpace;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800496 mCurrentFence = item.mFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700497 mCurrentFenceTime = item.mFenceTime;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700498 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800499
500 computeCurrentTransformMatrixLocked();
501
502 return err;
503}
504
Andy McFadden2adaf042012-12-18 09:49:45 -0800505status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800506 if (mEglDisplay == EGL_NO_DISPLAY) {
507 ALOGE("bindTextureImage: invalid display");
508 return INVALID_OPERATION;
509 }
510
Dan Stozad723bd72014-11-18 10:24:03 -0800511 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800512 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800513 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800514 }
515
516 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700517 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
518 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800519 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700520 return NO_INIT;
521 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800522
Eric Penner5c3d2432014-07-11 19:08:04 -0700523 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700524 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700525 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800526 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700527 mEglDisplay, mCurrentTexture);
528 return UNKNOWN_ERROR;
529 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700530 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
531
Eric Penner2d14a0e2014-08-25 20:16:37 -0700532 // In the rare case that the display is terminated and then initialized
533 // again, we can't detect that the display changed (it didn't), but the
534 // image is invalid. In this case, repeat the exact same steps while
535 // forcing the creation of a new image.
536 if ((error = glGetError()) != GL_NO_ERROR) {
537 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800538 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
539 mCurrentCrop,
540 true);
541 if (result != NO_ERROR) {
542 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700543 mEglDisplay, mCurrentTexture);
544 return UNKNOWN_ERROR;
545 }
546 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
547 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800548 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700549 return UNKNOWN_ERROR;
550 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800551 }
Andy McFadden97eba892012-12-11 15:21:45 -0800552
553 // Wait for the new buffer to be ready.
554 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800555}
556
Mathias Agopian45155962013-08-08 18:16:21 -0700557status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700558 EGLDisplay dpy = eglGetCurrentDisplay();
559 EGLContext ctx = eglGetCurrentContext();
560
Mathias Agopian45155962013-08-08 18:16:21 -0700561 if (!contextCheck) {
562 // if this is the first time we're called, mEglDisplay/mEglContext have
563 // never been set, so don't error out (below).
564 if (mEglDisplay == EGL_NO_DISPLAY) {
565 mEglDisplay = dpy;
566 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700567 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700568 mEglContext = ctx;
569 }
570 }
571
572 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800573 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700574 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700575 }
576
Mathias Agopian45155962013-08-08 18:16:21 -0700577 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800578 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700579 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700580 }
581
582 mEglDisplay = dpy;
583 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800584 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800585}
586
Jesse Hall13f01cb2013-03-20 11:37:21 -0700587void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
588 if (fence->isValid() &&
589 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700590 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700591 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700592 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800593 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700594 strerror(-err), err);
595 }
Jesse Hallef194142012-06-14 14:45:17 -0700596 }
597}
598
Andy McFadden2adaf042012-12-18 09:49:45 -0800599status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700600 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800601 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700602 Mutex::Autolock lock(mMutex);
603
604 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800605 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700606 return NO_INIT;
607 }
608
609 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800610 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700611 "context");
612 return INVALID_OPERATION;
613 }
614
615 EGLDisplay dpy = eglGetCurrentDisplay();
616 EGLContext ctx = eglGetCurrentContext();
617
618 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800619 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700620 return INVALID_OPERATION;
621 }
622
623 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800624 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700625 return INVALID_OPERATION;
626 }
627
628 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
629 status_t err = syncForReleaseLocked(dpy);
630 if (err != OK) {
631 return err;
632 }
633
634 glDeleteTextures(1, &mTexName);
635 }
636
637 mEglDisplay = EGL_NO_DISPLAY;
638 mEglContext = EGL_NO_CONTEXT;
639 mAttached = false;
640
641 return OK;
642}
643
Mathias Agopian3f844832013-08-07 21:24:32 -0700644status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700645 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800646 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700647 Mutex::Autolock lock(mMutex);
648
649 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800650 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700651 return NO_INIT;
652 }
653
654 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800655 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700656 "context");
657 return INVALID_OPERATION;
658 }
659
660 EGLDisplay dpy = eglGetCurrentDisplay();
661 EGLContext ctx = eglGetCurrentContext();
662
663 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800664 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700665 return INVALID_OPERATION;
666 }
667
668 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800669 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700670 return INVALID_OPERATION;
671 }
672
673 // We need to bind the texture regardless of whether there's a current
674 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700675 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700676
Jamie Gennis74bed552012-03-28 19:05:54 -0700677 mEglDisplay = dpy;
678 mEglContext = ctx;
679 mTexName = tex;
680 mAttached = true;
681
Eric Penner5c3d2432014-07-11 19:08:04 -0700682 if (mCurrentTextureImage != NULL) {
683 // This may wait for a buffer a second time. This is likely required if
684 // this is a different context, since otherwise the wait could be skipped
685 // by bouncing through another context. For the same context the extra
686 // wait is redundant.
687 status_t err = bindTextureImageLocked();
688 if (err != NO_ERROR) {
689 return err;
690 }
691 }
692
Jamie Gennis74bed552012-03-28 19:05:54 -0700693 return OK;
694}
695
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800696
Andy McFadden2adaf042012-12-18 09:49:45 -0800697status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800698 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700699
Jamie Gennis01dbf552012-09-06 14:54:19 -0700700 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700701 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700702 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
703 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
704 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800705 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700706 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700707 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700708 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700709 glFlush();
710 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700711 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700712 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800713 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700714 "fd: %#x", eglGetError());
715 return UNKNOWN_ERROR;
716 }
717 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700718 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700719 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700720 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800721 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700722 "%s (%d)", strerror(-err), err);
723 return err;
724 }
Mathias Agopianca088332013-03-28 17:44:13 -0700725 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700726 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
727 if (fence != EGL_NO_SYNC_KHR) {
728 // There is already a fence for the current slot. We need to
729 // wait on that before replacing it with another fence to
730 // ensure that all outstanding buffer accesses have completed
731 // before the producer accesses it.
732 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
733 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800734 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700735 "fence: %#x", eglGetError());
736 return UNKNOWN_ERROR;
737 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800738 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700739 "fence");
740 return TIMED_OUT;
741 }
742 eglDestroySyncKHR(dpy, fence);
743 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700744
Jamie Gennis01dbf552012-09-06 14:54:19 -0700745 // Create a fence for the outstanding accesses in the current
746 // OpenGL ES context.
747 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
748 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800749 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700750 eglGetError());
751 return UNKNOWN_ERROR;
752 }
753 glFlush();
754 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700755 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700756 }
757
758 return OK;
759}
760
Dan Stozad723bd72014-11-18 10:24:03 -0800761bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700762{
763 switch (format) {
764 // supported YUV formats
765 case HAL_PIXEL_FORMAT_YV12:
766 // Legacy/deprecated YUV formats
767 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
768 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
769 case HAL_PIXEL_FORMAT_YCbCr_422_I:
770 return true;
771 }
772
773 // Any OEM format needs to be considered
774 if (format>=0x100 && format<=0x1FF)
775 return true;
776
777 return false;
778}
779
Mathias Agopian3f844832013-08-07 21:24:32 -0700780uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700781 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700782}
783
Andy McFadden2adaf042012-12-18 09:49:45 -0800784void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800785 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700786 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
787}
788
Andy McFadden2adaf042012-12-18 09:49:45 -0800789void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700790 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700791 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800792 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700793 return;
794 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700795 bool needsRecompute = mFilteringEnabled != enabled;
796 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700797
Eric Penner5c3d2432014-07-11 19:08:04 -0700798 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800799 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700800 }
801
Eric Penner5c3d2432014-07-11 19:08:04 -0700802 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700803 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700804 }
805}
806
Andy McFadden2adaf042012-12-18 09:49:45 -0800807void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800808 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700809 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
810 nullptr : mCurrentTextureImage->graphicBuffer();
811 if (buf == nullptr) {
812 GLC_LOGD("computeCurrentTransformMatrixLocked: "
813 "mCurrentTextureImage is NULL");
814 }
815 computeTransformMatrix(mCurrentTransformMatrix, buf,
816 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop,
817 mCurrentTransform, mFilteringEnabled);
818}
819
820void GLConsumer::computeTransformMatrix(float outTransform[16],
821 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
822 bool filtering) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800823
Jamie Gennisa214c642011-01-14 13:53:31 -0800824 float xform[16];
825 for (int i = 0; i < 16; i++) {
826 xform[i] = mtxIdentity[i];
827 }
John Reck1a61da52016-04-28 13:18:15 -0700828 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800829 float result[16];
830 mtxMul(result, xform, mtxFlipH);
831 for (int i = 0; i < 16; i++) {
832 xform[i] = result[i];
833 }
834 }
John Reck1a61da52016-04-28 13:18:15 -0700835 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800836 float result[16];
837 mtxMul(result, xform, mtxFlipV);
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_ROT_90) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800843 float result[16];
844 mtxMul(result, xform, mtxRot90);
845 for (int i = 0; i < 16; i++) {
846 xform[i] = result[i];
847 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800848 }
849
Jamie Gennisdbe92452013-09-23 17:22:10 -0700850 float mtxBeforeFlipV[16];
John Reck1a61da52016-04-28 13:18:15 -0700851 if (!cropRect.isEmpty()) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700852 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
853 float bufferWidth = buf->getWidth();
854 float bufferHeight = buf->getHeight();
John Reck1a61da52016-04-28 13:18:15 -0700855 float shrinkAmount = 0.0f;
856 if (filtering) {
857 // In order to prevent bilinear sampling beyond the edge of the
858 // crop rectangle we may need to shrink it by 2 texels in each
859 // dimension. Normally this would just need to take 1/2 a texel
860 // off each end, but because the chroma channels of YUV420 images
861 // are subsampled we may need to shrink the crop region by a whole
862 // texel on each side.
863 switch (buf->getPixelFormat()) {
864 case PIXEL_FORMAT_RGBA_8888:
865 case PIXEL_FORMAT_RGBX_8888:
Romain Guyff415142016-12-13 16:51:25 -0800866 case PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -0800867 case PIXEL_FORMAT_RGBA_1010102:
John Reck1a61da52016-04-28 13:18:15 -0700868 case PIXEL_FORMAT_RGB_888:
869 case PIXEL_FORMAT_RGB_565:
870 case PIXEL_FORMAT_BGRA_8888:
871 // We know there's no subsampling of any channels, so we
872 // only need to shrink by a half a pixel.
873 shrinkAmount = 0.5;
874 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700875
John Reck1a61da52016-04-28 13:18:15 -0700876 default:
877 // If we don't recognize the format, we must assume the
878 // worst case (that we care about), which is YUV420.
879 shrinkAmount = 1.0;
880 break;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700881 }
John Reck1a61da52016-04-28 13:18:15 -0700882 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700883
John Reck1a61da52016-04-28 13:18:15 -0700884 // Only shrink the dimensions that are not the size of the buffer.
885 if (cropRect.width() < bufferWidth) {
886 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
887 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
888 bufferWidth;
889 }
890 if (cropRect.height() < bufferHeight) {
891 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
892 bufferHeight;
893 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
894 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700895 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700896 float crop[16] = {
897 sx, 0, 0, 0,
898 0, sy, 0, 0,
899 0, 0, 1, 0,
900 tx, ty, 0, 1,
901 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700902
Jamie Gennisdbe92452013-09-23 17:22:10 -0700903 mtxMul(mtxBeforeFlipV, crop, xform);
904 } else {
905 for (int i = 0; i < 16; i++) {
906 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700907 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800908 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800909
910 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800911 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800912 // want to expose this to applications, however, so we must add an
913 // additional vertical flip to the transform after all the other transforms.
John Reck1a61da52016-04-28 13:18:15 -0700914 mtxMul(outTransform, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800915}
916
Andy McFadden2adaf042012-12-18 09:49:45 -0800917nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800918 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800919 Mutex::Autolock lock(mMutex);
920 return mCurrentTimestamp;
921}
922
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700923android_dataspace GLConsumer::getCurrentDataSpace() {
924 GLC_LOGV("getCurrentDataSpace");
925 Mutex::Autolock lock(mMutex);
926 return mCurrentDataSpace;
927}
928
Dan Stozad723bd72014-11-18 10:24:03 -0800929uint64_t GLConsumer::getFrameNumber() {
930 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700931 Mutex::Autolock lock(mMutex);
932 return mCurrentFrameNumber;
933}
934
Chia-I Wu06d63de2017-01-04 14:58:51 +0800935sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700936 Mutex::Autolock lock(mMutex);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800937
938 if (outSlot != nullptr) {
939 *outSlot = mCurrentTexture;
940 }
941
942 return (mCurrentTextureImage == nullptr) ?
Eric Penner5c3d2432014-07-11 19:08:04 -0700943 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700944}
945
Andy McFadden2adaf042012-12-18 09:49:45 -0800946Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700947 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700948
949 Rect outCrop = mCurrentCrop;
950 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800951 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
952 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700953
954 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
955 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800956 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700957 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
958 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800959 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700960 }
961
Dan Stozad723bd72014-11-18 10:24:03 -0800962 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
963 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
964
Daniel Lam016c8cb2012-04-03 15:54:58 -0700965 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800966 if (newWidth < currentWidth) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700967 uint32_t dw = currentWidth - newWidth;
968 auto halfdw = dw / 2;
969 outCrop.left += halfdw;
970 // Not halfdw because it would subtract 1 too few when dw is odd
971 outCrop.right -= (dw - halfdw);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700972 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800973 } else if (newHeight < currentHeight) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700974 uint32_t dh = currentHeight - newHeight;
975 auto halfdh = dh / 2;
976 outCrop.top += halfdh;
977 // Not halfdh because it would subtract 1 too few when dh is odd
978 outCrop.bottom -= (dh - halfdh);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700979 }
980
Dan Stozad723bd72014-11-18 10:24:03 -0800981 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700982 outCrop.left, outCrop.top,
983 outCrop.right,outCrop.bottom);
984 }
985
Daniel Lam016c8cb2012-04-03 15:54:58 -0700986 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700987}
988
Andy McFadden2adaf042012-12-18 09:49:45 -0800989uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700990 Mutex::Autolock lock(mMutex);
991 return mCurrentTransform;
992}
993
Andy McFadden2adaf042012-12-18 09:49:45 -0800994uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700995 Mutex::Autolock lock(mMutex);
996 return mCurrentScalingMode;
997}
998
Andy McFadden2adaf042012-12-18 09:49:45 -0800999sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -07001000 Mutex::Autolock lock(mMutex);
1001 return mCurrentFence;
1002}
1003
Brian Anderson3d4039d2016-09-23 16:31:30 -07001004std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const {
1005 Mutex::Autolock lock(mMutex);
1006 return mCurrentFenceTime;
1007}
1008
Andy McFadden2adaf042012-12-18 09:49:45 -08001009status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001010 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -07001011 return doGLFenceWaitLocked();
1012}
1013
Andy McFadden2adaf042012-12-18 09:49:45 -08001014status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001015
1016 EGLDisplay dpy = eglGetCurrentDisplay();
1017 EGLContext ctx = eglGetCurrentContext();
1018
1019 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -08001020 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001021 return INVALID_OPERATION;
1022 }
1023
1024 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -08001025 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001026 return INVALID_OPERATION;
1027 }
1028
Jamie Gennis1df8c342012-12-20 14:05:45 -08001029 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -07001030 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001031 // Create an EGLSyncKHR from the current fence.
1032 int fenceFd = mCurrentFence->dup();
1033 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -08001034 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001035 return -errno;
1036 }
1037 EGLint attribs[] = {
1038 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
1039 EGL_NONE
1040 };
1041 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
1042 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
1043 if (sync == EGL_NO_SYNC_KHR) {
1044 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -08001045 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001046 eglGetError());
1047 return UNKNOWN_ERROR;
1048 }
1049
1050 // XXX: The spec draft is inconsistent as to whether this should
1051 // return an EGLint or void. Ignore the return value for now, as
1052 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -07001053 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001054 EGLint eglErr = eglGetError();
1055 eglDestroySyncKHR(dpy, sync);
1056 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -08001057 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001058 eglErr);
1059 return UNKNOWN_ERROR;
1060 }
1061 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001062 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001063 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001064 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -08001065 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001066 return err;
1067 }
1068 }
1069 }
1070
1071 return NO_ERROR;
1072}
1073
Andy McFadden2adaf042012-12-18 09:49:45 -08001074void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001075 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001076 if (slotIndex == mCurrentTexture) {
1077 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1078 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001079 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001080 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001081}
Daniel Lameae59d22012-01-22 15:26:27 -08001082
Andy McFadden2adaf042012-12-18 09:49:45 -08001083void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001084 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001085 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001086 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001087}
1088
Andy McFadden2adaf042012-12-18 09:49:45 -08001089void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001090 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001091 if (mAbandoned) {
1092 GLC_LOGE("setName: GLConsumer is abandoned!");
1093 return;
1094 }
Jamie Gennisfa28c352011-09-16 17:30:26 -07001095 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001096 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001097}
1098
Dan Stozad723bd72014-11-18 10:24:03 -08001099status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001100 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001101 if (mAbandoned) {
1102 GLC_LOGE("setDefaultBufferFormat: GLConsumer is abandoned!");
1103 return NO_INIT;
1104 }
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001105 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001106}
1107
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001108status_t GLConsumer::setDefaultBufferDataSpace(
1109 android_dataspace defaultDataSpace) {
1110 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001111 if (mAbandoned) {
1112 GLC_LOGE("setDefaultBufferDataSpace: GLConsumer is abandoned!");
1113 return NO_INIT;
1114 }
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001115 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1116}
1117
Chia-I Wue2786ea2017-08-07 10:36:08 -07001118status_t GLConsumer::setConsumerUsageBits(uint64_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001119 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001120 if (mAbandoned) {
1121 GLC_LOGE("setConsumerUsageBits: GLConsumer is abandoned!");
1122 return NO_INIT;
1123 }
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001124 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001125 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001126}
1127
Andy McFadden2adaf042012-12-18 09:49:45 -08001128status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001129 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001130 if (mAbandoned) {
1131 GLC_LOGE("setTransformHint: GLConsumer is abandoned!");
1132 return NO_INIT;
1133 }
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001134 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001135}
1136
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001137status_t GLConsumer::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1138 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001139 if (mAbandoned) {
1140 GLC_LOGE("setMaxAcquiredBufferCount: GLConsumer is abandoned!");
1141 return NO_INIT;
1142 }
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001143 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
1144}
1145
Mathias Agopian74d211a2013-04-22 16:55:35 +02001146void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001147{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001148 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001149 "%smTexName=%d mCurrentTexture=%d\n"
1150 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1151 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1152 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1153 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001154
Mathias Agopian74d211a2013-04-22 16:55:35 +02001155 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001156}
1157
Jamie Gennisf238e282011-01-09 16:33:17 -08001158static void mtxMul(float out[16], const float a[16], const float b[16]) {
1159 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1160 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1161 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1162 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1163
1164 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1165 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1166 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1167 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1168
1169 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1170 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1171 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1172 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1173
1174 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1175 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1176 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1177 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1178}
1179
Eric Penner5c3d2432014-07-11 19:08:04 -07001180GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1181 mGraphicBuffer(graphicBuffer),
1182 mEglImage(EGL_NO_IMAGE_KHR),
Pablo Ceballos60d69222015-08-07 14:47:20 -07001183 mEglDisplay(EGL_NO_DISPLAY),
1184 mCropRect(Rect::EMPTY_RECT) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001185}
1186
1187GLConsumer::EglImage::~EglImage() {
1188 if (mEglImage != EGL_NO_IMAGE_KHR) {
1189 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1190 ALOGE("~EglImage: eglDestroyImageKHR failed");
1191 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001192 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001193 }
1194}
1195
1196status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001197 const Rect& cropRect,
1198 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001199 // If there's an image and it's no longer valid, destroy it.
1200 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1201 bool displayInvalid = mEglDisplay != eglDisplay;
1202 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001203 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001204 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1205 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1206 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001207 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001208 mEglImage = EGL_NO_IMAGE_KHR;
1209 mEglDisplay = EGL_NO_DISPLAY;
1210 }
1211
1212 // If there's no image, create one.
1213 if (mEglImage == EGL_NO_IMAGE_KHR) {
1214 mEglDisplay = eglDisplay;
1215 mCropRect = cropRect;
1216 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1217 }
1218
1219 // Fail if we can't create a valid image.
1220 if (mEglImage == EGL_NO_IMAGE_KHR) {
1221 mEglDisplay = EGL_NO_DISPLAY;
1222 mCropRect.makeInvalid();
1223 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001224 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -07001225 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1226 buffer->getUsage(), buffer->getPixelFormat());
1227 return UNKNOWN_ERROR;
1228 }
1229
1230 return OK;
1231}
1232
1233void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001234 glEGLImageTargetTexture2DOES(texTarget,
1235 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001236}
1237
1238EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1239 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001240 EGLClientBuffer cbuf =
1241 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -07001242 const bool createProtectedImage =
1243 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
1244 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -07001245 EGLint attrs[] = {
1246 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1247 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1248 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1249 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1250 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
Craig Donneraec86972016-04-28 18:09:40 -07001251 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1252 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001253 EGL_NONE,
1254 };
1255 if (!crop.isValid()) {
Craig Donnera94d9402016-10-19 17:18:17 -07001256 // No crop rect to set, so leave the crop out of the attrib array. Make
1257 // sure to propagate the protected content attrs if they are set.
1258 attrs[2] = attrs[10];
1259 attrs[3] = attrs[11];
1260 attrs[4] = EGL_NONE;
Eric Penner5c3d2432014-07-11 19:08:04 -07001261 } else if (!isEglImageCroppable(crop)) {
1262 // The crop rect is not at the origin, so we can't set the crop on the
1263 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1264 // extension. In the future we can add a layered extension that
1265 // removes this restriction if there is hardware that can support it.
Craig Donnera94d9402016-10-19 17:18:17 -07001266 attrs[2] = attrs[10];
1267 attrs[3] = attrs[11];
1268 attrs[4] = EGL_NONE;
Eric Penner5c3d2432014-07-11 19:08:04 -07001269 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001270 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001271 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1272 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1273 if (image == EGL_NO_IMAGE_KHR) {
1274 EGLint error = eglGetError();
1275 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001276 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001277 }
1278 return image;
1279}
1280
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001281}; // namespace android