blob: b11a3e5a02e6a764734a4728aaced7a2dd066ecc [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 " ");
Craig Donner4df76b22016-06-13 22:14:15 +0000138 return equal || atStart || atEnd || inMiddle;
Craig Donneraec86972016-04-28 18:09:40 -0700139}
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),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700160 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700161 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200162 mDefaultWidth(1),
163 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700164 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700165 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800166 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800167 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700168 mEglDisplay(EGL_NO_DISPLAY),
169 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700170 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
171 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800172{
Dan Stozad723bd72014-11-18 10:24:03 -0800173 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800174
Jamie Gennisfa28c352011-09-16 17:30:26 -0700175 memcpy(mCurrentTransformMatrix, mtxIdentity,
176 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700177
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700178 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800179}
180
Dan Stozaab574912014-06-25 14:21:45 -0700181GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
182 bool useFenceSync, bool isControlledByApp) :
183 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700184 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700185 mCurrentTransform(0),
186 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
187 mCurrentFence(Fence::NO_FENCE),
188 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700189 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Dan Stozaab574912014-06-25 14:21:45 -0700190 mCurrentFrameNumber(0),
191 mDefaultWidth(1),
192 mDefaultHeight(1),
193 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800194 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700195 mUseFenceSync(useFenceSync),
196 mTexTarget(texTarget),
197 mEglDisplay(EGL_NO_DISPLAY),
198 mEglContext(EGL_NO_CONTEXT),
199 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
200 mAttached(false)
201{
Dan Stozad723bd72014-11-18 10:24:03 -0800202 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700203
204 memcpy(mCurrentTransformMatrix, mtxIdentity,
205 sizeof(mCurrentTransformMatrix));
206
207 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
208}
209
Andy McFadden2adaf042012-12-18 09:49:45 -0800210status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700211{
Daniel Lamb2675792012-02-23 14:35:13 -0800212 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700213 if (mAbandoned) {
214 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
215 return NO_INIT;
216 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700217 mDefaultWidth = w;
218 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700219 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700220}
221
Andy McFadden2adaf042012-12-18 09:49:45 -0800222status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800223 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800224 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800225 Mutex::Autolock lock(mMutex);
226
227 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800228 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800229 return NO_INIT;
230 }
231
232 // Make sure the EGL state is the same as in previous calls.
233 status_t err = checkAndUpdateEglStateLocked();
234 if (err != NO_ERROR) {
235 return err;
236 }
237
Dan Stozadd264162015-03-12 13:58:47 -0700238 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800239
240 // Acquire the next buffer.
241 // In asynchronous mode the list is guaranteed to be one buffer
242 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700243 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800244 if (err != NO_ERROR) {
245 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
246 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800247 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800248 glBindTexture(mTexTarget, mTexName);
249 err = NO_ERROR;
250 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800251 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800252 strerror(-err), err);
253 }
254 return err;
255 }
256
257 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700258 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800259 if (err != NO_ERROR) {
260 // We always bind the texture.
261 glBindTexture(mTexTarget, mTexName);
262 return err;
263 }
264
Andy McFadden97eba892012-12-11 15:21:45 -0800265 // Bind the new buffer to the GL texture, and wait until it's ready.
266 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700267}
268
Mathias Agopianad678e12013-07-23 17:28:53 -0700269
270status_t GLConsumer::releaseTexImage() {
271 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800272 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700273 Mutex::Autolock lock(mMutex);
274
275 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800276 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700277 return NO_INIT;
278 }
279
280 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700281 status_t err = NO_ERROR;
282
283 if (mAttached) {
284 err = checkAndUpdateEglStateLocked(true);
285 if (err != NO_ERROR) {
286 return err;
287 }
288 } else {
289 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700290 }
291
292 // Update the GLConsumer state.
293 int buf = mCurrentTexture;
294 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
295
Dan Stozad723bd72014-11-18 10:24:03 -0800296 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700297
Mathias Agopian45155962013-08-08 18:16:21 -0700298 if (mAttached) {
299 // Do whatever sync ops we need to do before releasing the slot.
300 err = syncForReleaseLocked(mEglDisplay);
301 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800302 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700303 return err;
304 }
305 } else {
306 // if we're detached, we just use the fence that was created in detachFromContext()
307 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700308 }
309
Mathias Agopian45155962013-08-08 18:16:21 -0700310 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700311 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800312 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700313 strerror(-err), err);
314 return err;
315 }
316
Eric Penner5c3d2432014-07-11 19:08:04 -0700317 if (mReleasedTexImage == NULL) {
318 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
319 }
320
Mathias Agopianad678e12013-07-23 17:28:53 -0700321 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700322 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700323 mCurrentCrop.makeInvalid();
324 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700325 mCurrentTimestamp = 0;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700326 mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopianad678e12013-07-23 17:28:53 -0700327 mCurrentFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700328 mCurrentFenceTime = FenceTime::NO_FENCE;
Mathias Agopianad678e12013-07-23 17:28:53 -0700329
Mathias Agopian45155962013-08-08 18:16:21 -0700330 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700331 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800332 status_t result = bindTextureImageLocked();
333 if (result != NO_ERROR) {
334 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700335 }
Mathias Agopian45155962013-08-08 18:16:21 -0700336 } else {
337 // detached, don't touch the texture (and we may not even have an
338 // EGLDisplay here.
339 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700340 }
341
342 return NO_ERROR;
343}
344
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700345sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
346 Mutex::Autolock _l(sStaticInitLock);
347 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
348 // The first time, create the debug texture in case the application
349 // continues to use it.
350 sp<GraphicBuffer> buffer = new GraphicBuffer(
351 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
Dan Stozad4079af2016-08-22 17:26:41 -0700352 GraphicBuffer::USAGE_SW_WRITE_RARELY,
353 "[GLConsumer debug texture]");
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700354 uint32_t* bits;
355 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800356 uint32_t stride = buffer->getStride();
357 uint32_t height = buffer->getHeight();
358 memset(bits, 0, stride * height * 4);
359 for (uint32_t y = 0; y < kDebugData.height; y++) {
360 for (uint32_t x = 0; x < kDebugData.width; x++) {
361 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
362 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700363 }
Dan Stozad723bd72014-11-18 10:24:03 -0800364 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700365 }
366 buffer->unlock();
367 sReleasedTexImageBuffer = buffer;
368 }
369 return sReleasedTexImageBuffer;
370}
371
Dan Stozadd264162015-03-12 13:58:47 -0700372status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700373 nsecs_t presentWhen, uint64_t maxFrameNumber) {
374 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
375 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700376 if (err != NO_ERROR) {
377 return err;
378 }
379
Eric Penner5c3d2432014-07-11 19:08:04 -0700380 // If item->mGraphicBuffer is not null, this buffer has not been acquired
381 // before, so any prior EglImage created is using a stale buffer. This
382 // replaces any old EglImage with a new one (using the new buffer).
383 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700384 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700385 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700386 }
387
Jamie Gennis9fea3422012-08-07 18:03:04 -0700388 return NO_ERROR;
389}
390
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700391status_t GLConsumer::releaseBufferLocked(int buf,
392 sp<GraphicBuffer> graphicBuffer,
393 EGLDisplay display, EGLSyncKHR eglFence) {
394 // release the buffer if it hasn't already been discarded by the
395 // BufferQueue. This can happen, for example, when the producer of this
396 // buffer has reallocated the original buffer slot after this buffer
397 // was acquired.
398 status_t err = ConsumerBase::releaseBufferLocked(
399 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700400 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700401 return err;
402}
403
Dan Stoza3ce46042015-11-17 17:00:45 -0800404status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
405 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800406{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700407 status_t err = NO_ERROR;
408
Pablo Ceballos47650f42015-08-04 16:38:17 -0700409 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700410
Jamie Gennis74bed552012-03-28 19:05:54 -0700411 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800412 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700413 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700414 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700415 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700416 return INVALID_OPERATION;
417 }
418
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800419 // Confirm state.
420 err = checkAndUpdateEglStateLocked();
421 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700422 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700423 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800424 return err;
425 }
426
Eric Penner5c3d2432014-07-11 19:08:04 -0700427 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
428 // if nessessary, for the gralloc buffer currently in the slot in
429 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800430 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700431 // means the buffer was previously acquired).
Pablo Ceballos47650f42015-08-04 16:38:17 -0700432 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700433 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800434 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700435 mEglDisplay, slot);
436 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700437 mEglDisplay, EGL_NO_SYNC_KHR);
438 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800439 }
440
441 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800442 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800443 err = syncForReleaseLocked(mEglDisplay);
444 if (err != NO_ERROR) {
445 // Release the buffer we just acquired. It's not safe to
446 // release the old buffer, so instead we just drop the new frame.
447 // As we are still under lock since acquireBuffer, it is safe to
448 // release by slot.
449 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
450 mEglDisplay, EGL_NO_SYNC_KHR);
451 return err;
452 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800453 }
454
Dan Stozad723bd72014-11-18 10:24:03 -0800455 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700456 mCurrentTexture, mCurrentTextureImage != NULL ?
457 mCurrentTextureImage->graphicBufferHandle() : 0,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700458 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800459
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700460 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700461 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700462 // the same.
463 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
464
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800465 // release old buffer
466 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800467 if (pendingRelease == nullptr) {
468 status_t status = releaseBufferLocked(
469 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
470 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
471 if (status < NO_ERROR) {
472 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
473 strerror(-status), status);
474 err = status;
475 // keep going, with error raised [?]
476 }
477 } else {
478 pendingRelease->currentTexture = mCurrentTexture;
479 pendingRelease->graphicBuffer =
480 mCurrentTextureImage->graphicBuffer();
481 pendingRelease->display = mEglDisplay;
482 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
483 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800484 }
485 }
486
Andy McFadden2adaf042012-12-18 09:49:45 -0800487 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700488 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700489 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800490 mCurrentCrop = item.mCrop;
491 mCurrentTransform = item.mTransform;
492 mCurrentScalingMode = item.mScalingMode;
493 mCurrentTimestamp = item.mTimestamp;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700494 mCurrentDataSpace = item.mDataSpace;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800495 mCurrentFence = item.mFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700496 mCurrentFenceTime = item.mFenceTime;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700497 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800498
499 computeCurrentTransformMatrixLocked();
500
501 return err;
502}
503
Andy McFadden2adaf042012-12-18 09:49:45 -0800504status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800505 if (mEglDisplay == EGL_NO_DISPLAY) {
506 ALOGE("bindTextureImage: invalid display");
507 return INVALID_OPERATION;
508 }
509
Dan Stozad723bd72014-11-18 10:24:03 -0800510 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800511 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800512 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800513 }
514
515 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700516 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
517 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800518 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700519 return NO_INIT;
520 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800521
Eric Penner5c3d2432014-07-11 19:08:04 -0700522 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700523 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700524 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800525 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700526 mEglDisplay, mCurrentTexture);
527 return UNKNOWN_ERROR;
528 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700529 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
530
Eric Penner2d14a0e2014-08-25 20:16:37 -0700531 // In the rare case that the display is terminated and then initialized
532 // again, we can't detect that the display changed (it didn't), but the
533 // image is invalid. In this case, repeat the exact same steps while
534 // forcing the creation of a new image.
535 if ((error = glGetError()) != GL_NO_ERROR) {
536 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800537 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
538 mCurrentCrop,
539 true);
540 if (result != NO_ERROR) {
541 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700542 mEglDisplay, mCurrentTexture);
543 return UNKNOWN_ERROR;
544 }
545 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
546 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800547 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700548 return UNKNOWN_ERROR;
549 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800550 }
Andy McFadden97eba892012-12-11 15:21:45 -0800551
552 // Wait for the new buffer to be ready.
553 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800554}
555
Mathias Agopian45155962013-08-08 18:16:21 -0700556status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700557 EGLDisplay dpy = eglGetCurrentDisplay();
558 EGLContext ctx = eglGetCurrentContext();
559
Mathias Agopian45155962013-08-08 18:16:21 -0700560 if (!contextCheck) {
561 // if this is the first time we're called, mEglDisplay/mEglContext have
562 // never been set, so don't error out (below).
563 if (mEglDisplay == EGL_NO_DISPLAY) {
564 mEglDisplay = dpy;
565 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700566 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700567 mEglContext = ctx;
568 }
569 }
570
571 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800572 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700573 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700574 }
575
Mathias Agopian45155962013-08-08 18:16:21 -0700576 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800577 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700579 }
580
581 mEglDisplay = dpy;
582 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800583 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800584}
585
Jesse Hall13f01cb2013-03-20 11:37:21 -0700586void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
587 if (fence->isValid() &&
588 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700589 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700590 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700591 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800592 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700593 strerror(-err), err);
594 }
Jesse Hallef194142012-06-14 14:45:17 -0700595 }
596}
597
Andy McFadden2adaf042012-12-18 09:49:45 -0800598status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700599 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800600 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700601 Mutex::Autolock lock(mMutex);
602
603 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800604 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700605 return NO_INIT;
606 }
607
608 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800609 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700610 "context");
611 return INVALID_OPERATION;
612 }
613
614 EGLDisplay dpy = eglGetCurrentDisplay();
615 EGLContext ctx = eglGetCurrentContext();
616
617 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800618 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700619 return INVALID_OPERATION;
620 }
621
622 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800623 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700624 return INVALID_OPERATION;
625 }
626
627 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
628 status_t err = syncForReleaseLocked(dpy);
629 if (err != OK) {
630 return err;
631 }
632
633 glDeleteTextures(1, &mTexName);
634 }
635
636 mEglDisplay = EGL_NO_DISPLAY;
637 mEglContext = EGL_NO_CONTEXT;
638 mAttached = false;
639
640 return OK;
641}
642
Mathias Agopian3f844832013-08-07 21:24:32 -0700643status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700644 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800645 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700646 Mutex::Autolock lock(mMutex);
647
648 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800649 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700650 return NO_INIT;
651 }
652
653 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800654 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700655 "context");
656 return INVALID_OPERATION;
657 }
658
659 EGLDisplay dpy = eglGetCurrentDisplay();
660 EGLContext ctx = eglGetCurrentContext();
661
662 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800663 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700664 return INVALID_OPERATION;
665 }
666
667 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800668 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700669 return INVALID_OPERATION;
670 }
671
672 // We need to bind the texture regardless of whether there's a current
673 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700674 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700675
Jamie Gennis74bed552012-03-28 19:05:54 -0700676 mEglDisplay = dpy;
677 mEglContext = ctx;
678 mTexName = tex;
679 mAttached = true;
680
Eric Penner5c3d2432014-07-11 19:08:04 -0700681 if (mCurrentTextureImage != NULL) {
682 // This may wait for a buffer a second time. This is likely required if
683 // this is a different context, since otherwise the wait could be skipped
684 // by bouncing through another context. For the same context the extra
685 // wait is redundant.
686 status_t err = bindTextureImageLocked();
687 if (err != NO_ERROR) {
688 return err;
689 }
690 }
691
Jamie Gennis74bed552012-03-28 19:05:54 -0700692 return OK;
693}
694
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800695
Andy McFadden2adaf042012-12-18 09:49:45 -0800696status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800697 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700698
Jamie Gennis01dbf552012-09-06 14:54:19 -0700699 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700700 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700701 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
702 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
703 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800704 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700705 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700706 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700707 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700708 glFlush();
709 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700710 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700711 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800712 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700713 "fd: %#x", eglGetError());
714 return UNKNOWN_ERROR;
715 }
716 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700717 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700718 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700719 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800720 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700721 "%s (%d)", strerror(-err), err);
722 return err;
723 }
Mathias Agopianca088332013-03-28 17:44:13 -0700724 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700725 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
726 if (fence != EGL_NO_SYNC_KHR) {
727 // There is already a fence for the current slot. We need to
728 // wait on that before replacing it with another fence to
729 // ensure that all outstanding buffer accesses have completed
730 // before the producer accesses it.
731 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
732 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800733 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700734 "fence: %#x", eglGetError());
735 return UNKNOWN_ERROR;
736 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800737 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700738 "fence");
739 return TIMED_OUT;
740 }
741 eglDestroySyncKHR(dpy, fence);
742 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700743
Jamie Gennis01dbf552012-09-06 14:54:19 -0700744 // Create a fence for the outstanding accesses in the current
745 // OpenGL ES context.
746 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
747 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800748 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700749 eglGetError());
750 return UNKNOWN_ERROR;
751 }
752 glFlush();
753 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700754 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700755 }
756
757 return OK;
758}
759
Dan Stozad723bd72014-11-18 10:24:03 -0800760bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700761{
762 switch (format) {
763 // supported YUV formats
764 case HAL_PIXEL_FORMAT_YV12:
765 // Legacy/deprecated YUV formats
766 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
767 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
768 case HAL_PIXEL_FORMAT_YCbCr_422_I:
769 return true;
770 }
771
772 // Any OEM format needs to be considered
773 if (format>=0x100 && format<=0x1FF)
774 return true;
775
776 return false;
777}
778
Mathias Agopian3f844832013-08-07 21:24:32 -0700779uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700780 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700781}
782
Andy McFadden2adaf042012-12-18 09:49:45 -0800783void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800784 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700785 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
786}
787
Andy McFadden2adaf042012-12-18 09:49:45 -0800788void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700789 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700790 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800791 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700792 return;
793 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700794 bool needsRecompute = mFilteringEnabled != enabled;
795 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700796
Eric Penner5c3d2432014-07-11 19:08:04 -0700797 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800798 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700799 }
800
Eric Penner5c3d2432014-07-11 19:08:04 -0700801 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700802 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700803 }
804}
805
Andy McFadden2adaf042012-12-18 09:49:45 -0800806void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800807 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700808 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
809 nullptr : mCurrentTextureImage->graphicBuffer();
810 if (buf == nullptr) {
811 GLC_LOGD("computeCurrentTransformMatrixLocked: "
812 "mCurrentTextureImage is NULL");
813 }
814 computeTransformMatrix(mCurrentTransformMatrix, buf,
815 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop,
816 mCurrentTransform, mFilteringEnabled);
817}
818
819void GLConsumer::computeTransformMatrix(float outTransform[16],
820 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
821 bool filtering) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800822
Jamie Gennisa214c642011-01-14 13:53:31 -0800823 float xform[16];
824 for (int i = 0; i < 16; i++) {
825 xform[i] = mtxIdentity[i];
826 }
John Reck1a61da52016-04-28 13:18:15 -0700827 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800828 float result[16];
829 mtxMul(result, xform, mtxFlipH);
830 for (int i = 0; i < 16; i++) {
831 xform[i] = result[i];
832 }
833 }
John Reck1a61da52016-04-28 13:18:15 -0700834 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800835 float result[16];
836 mtxMul(result, xform, mtxFlipV);
837 for (int i = 0; i < 16; i++) {
838 xform[i] = result[i];
839 }
840 }
John Reck1a61da52016-04-28 13:18:15 -0700841 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800842 float result[16];
843 mtxMul(result, xform, mtxRot90);
844 for (int i = 0; i < 16; i++) {
845 xform[i] = result[i];
846 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800847 }
848
Jamie Gennisdbe92452013-09-23 17:22:10 -0700849 float mtxBeforeFlipV[16];
John Reck1a61da52016-04-28 13:18:15 -0700850 if (!cropRect.isEmpty()) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700851 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
852 float bufferWidth = buf->getWidth();
853 float bufferHeight = buf->getHeight();
John Reck1a61da52016-04-28 13:18:15 -0700854 float shrinkAmount = 0.0f;
855 if (filtering) {
856 // In order to prevent bilinear sampling beyond the edge of the
857 // crop rectangle we may need to shrink it by 2 texels in each
858 // dimension. Normally this would just need to take 1/2 a texel
859 // off each end, but because the chroma channels of YUV420 images
860 // are subsampled we may need to shrink the crop region by a whole
861 // texel on each side.
862 switch (buf->getPixelFormat()) {
863 case PIXEL_FORMAT_RGBA_8888:
864 case PIXEL_FORMAT_RGBX_8888:
Romain Guyff415142016-12-13 16:51:25 -0800865 case PIXEL_FORMAT_RGBA_FP16:
John Reck1a61da52016-04-28 13:18:15 -0700866 case PIXEL_FORMAT_RGB_888:
867 case PIXEL_FORMAT_RGB_565:
868 case PIXEL_FORMAT_BGRA_8888:
869 // We know there's no subsampling of any channels, so we
870 // only need to shrink by a half a pixel.
871 shrinkAmount = 0.5;
872 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700873
John Reck1a61da52016-04-28 13:18:15 -0700874 default:
875 // If we don't recognize the format, we must assume the
876 // worst case (that we care about), which is YUV420.
877 shrinkAmount = 1.0;
878 break;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700879 }
John Reck1a61da52016-04-28 13:18:15 -0700880 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700881
John Reck1a61da52016-04-28 13:18:15 -0700882 // Only shrink the dimensions that are not the size of the buffer.
883 if (cropRect.width() < bufferWidth) {
884 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
885 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
886 bufferWidth;
887 }
888 if (cropRect.height() < bufferHeight) {
889 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
890 bufferHeight;
891 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
892 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700893 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700894 float crop[16] = {
895 sx, 0, 0, 0,
896 0, sy, 0, 0,
897 0, 0, 1, 0,
898 tx, ty, 0, 1,
899 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700900
Jamie Gennisdbe92452013-09-23 17:22:10 -0700901 mtxMul(mtxBeforeFlipV, crop, xform);
902 } else {
903 for (int i = 0; i < 16; i++) {
904 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700905 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800906 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800907
908 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800909 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800910 // want to expose this to applications, however, so we must add an
911 // additional vertical flip to the transform after all the other transforms.
John Reck1a61da52016-04-28 13:18:15 -0700912 mtxMul(outTransform, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800913}
914
Andy McFadden2adaf042012-12-18 09:49:45 -0800915nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800916 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800917 Mutex::Autolock lock(mMutex);
918 return mCurrentTimestamp;
919}
920
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700921android_dataspace GLConsumer::getCurrentDataSpace() {
922 GLC_LOGV("getCurrentDataSpace");
923 Mutex::Autolock lock(mMutex);
924 return mCurrentDataSpace;
925}
926
Dan Stozad723bd72014-11-18 10:24:03 -0800927uint64_t GLConsumer::getFrameNumber() {
928 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700929 Mutex::Autolock lock(mMutex);
930 return mCurrentFrameNumber;
931}
932
Andy McFadden2adaf042012-12-18 09:49:45 -0800933sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700934 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700935 return (mCurrentTextureImage == NULL) ?
936 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700937}
938
Andy McFadden2adaf042012-12-18 09:49:45 -0800939Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700940 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700941
942 Rect outCrop = mCurrentCrop;
943 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800944 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
945 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700946
947 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
948 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800949 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700950 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
951 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800952 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700953 }
954
Dan Stozad723bd72014-11-18 10:24:03 -0800955 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
956 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
957
Daniel Lam016c8cb2012-04-03 15:54:58 -0700958 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800959 if (newWidth < currentWidth) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700960 uint32_t dw = currentWidth - newWidth;
961 auto halfdw = dw / 2;
962 outCrop.left += halfdw;
963 // Not halfdw because it would subtract 1 too few when dw is odd
964 outCrop.right -= (dw - halfdw);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700965 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800966 } else if (newHeight < currentHeight) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700967 uint32_t dh = currentHeight - newHeight;
968 auto halfdh = dh / 2;
969 outCrop.top += halfdh;
970 // Not halfdh because it would subtract 1 too few when dh is odd
971 outCrop.bottom -= (dh - halfdh);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700972 }
973
Dan Stozad723bd72014-11-18 10:24:03 -0800974 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700975 outCrop.left, outCrop.top,
976 outCrop.right,outCrop.bottom);
977 }
978
Daniel Lam016c8cb2012-04-03 15:54:58 -0700979 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700980}
981
Andy McFadden2adaf042012-12-18 09:49:45 -0800982uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700983 Mutex::Autolock lock(mMutex);
984 return mCurrentTransform;
985}
986
Andy McFadden2adaf042012-12-18 09:49:45 -0800987uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700988 Mutex::Autolock lock(mMutex);
989 return mCurrentScalingMode;
990}
991
Andy McFadden2adaf042012-12-18 09:49:45 -0800992sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700993 Mutex::Autolock lock(mMutex);
994 return mCurrentFence;
995}
996
Brian Anderson3d4039d2016-09-23 16:31:30 -0700997std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const {
998 Mutex::Autolock lock(mMutex);
999 return mCurrentFenceTime;
1000}
1001
Andy McFadden2adaf042012-12-18 09:49:45 -08001002status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001003 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -07001004 return doGLFenceWaitLocked();
1005}
1006
Andy McFadden2adaf042012-12-18 09:49:45 -08001007status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001008
1009 EGLDisplay dpy = eglGetCurrentDisplay();
1010 EGLContext ctx = eglGetCurrentContext();
1011
1012 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -08001013 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001014 return INVALID_OPERATION;
1015 }
1016
1017 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -08001018 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001019 return INVALID_OPERATION;
1020 }
1021
Jamie Gennis1df8c342012-12-20 14:05:45 -08001022 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -07001023 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -07001024 // Create an EGLSyncKHR from the current fence.
1025 int fenceFd = mCurrentFence->dup();
1026 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -08001027 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001028 return -errno;
1029 }
1030 EGLint attribs[] = {
1031 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
1032 EGL_NONE
1033 };
1034 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
1035 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
1036 if (sync == EGL_NO_SYNC_KHR) {
1037 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -08001038 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001039 eglGetError());
1040 return UNKNOWN_ERROR;
1041 }
1042
1043 // XXX: The spec draft is inconsistent as to whether this should
1044 // return an EGLint or void. Ignore the return value for now, as
1045 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -07001046 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001047 EGLint eglErr = eglGetError();
1048 eglDestroySyncKHR(dpy, sync);
1049 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -08001050 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001051 eglErr);
1052 return UNKNOWN_ERROR;
1053 }
1054 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001055 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001056 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001057 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -08001058 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001059 return err;
1060 }
1061 }
1062 }
1063
1064 return NO_ERROR;
1065}
1066
Andy McFadden2adaf042012-12-18 09:49:45 -08001067void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001068 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001069 if (slotIndex == mCurrentTexture) {
1070 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1071 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001072 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001073 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001074}
Daniel Lameae59d22012-01-22 15:26:27 -08001075
Andy McFadden2adaf042012-12-18 09:49:45 -08001076void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001077 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001078 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001079 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001080}
1081
Andy McFadden2adaf042012-12-18 09:49:45 -08001082void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001083 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001084 if (mAbandoned) {
1085 GLC_LOGE("setName: GLConsumer is abandoned!");
1086 return;
1087 }
Jamie Gennisfa28c352011-09-16 17:30:26 -07001088 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001089 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001090}
1091
Dan Stozad723bd72014-11-18 10:24:03 -08001092status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001093 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001094 if (mAbandoned) {
1095 GLC_LOGE("setDefaultBufferFormat: GLConsumer is abandoned!");
1096 return NO_INIT;
1097 }
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001098 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001099}
1100
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001101status_t GLConsumer::setDefaultBufferDataSpace(
1102 android_dataspace defaultDataSpace) {
1103 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001104 if (mAbandoned) {
1105 GLC_LOGE("setDefaultBufferDataSpace: GLConsumer is abandoned!");
1106 return NO_INIT;
1107 }
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001108 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1109}
1110
Andy McFadden2adaf042012-12-18 09:49:45 -08001111status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001112 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001113 if (mAbandoned) {
1114 GLC_LOGE("setConsumerUsageBits: GLConsumer is abandoned!");
1115 return NO_INIT;
1116 }
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001117 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001118 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001119}
1120
Andy McFadden2adaf042012-12-18 09:49:45 -08001121status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001122 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001123 if (mAbandoned) {
1124 GLC_LOGE("setTransformHint: GLConsumer is abandoned!");
1125 return NO_INIT;
1126 }
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001127 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001128}
1129
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001130status_t GLConsumer::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1131 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -07001132 if (mAbandoned) {
1133 GLC_LOGE("setMaxAcquiredBufferCount: GLConsumer is abandoned!");
1134 return NO_INIT;
1135 }
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001136 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
1137}
1138
Mathias Agopian74d211a2013-04-22 16:55:35 +02001139void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001140{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001141 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001142 "%smTexName=%d mCurrentTexture=%d\n"
1143 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1144 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1145 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1146 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001147
Mathias Agopian74d211a2013-04-22 16:55:35 +02001148 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001149}
1150
Jamie Gennisf238e282011-01-09 16:33:17 -08001151static void mtxMul(float out[16], const float a[16], const float b[16]) {
1152 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1153 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1154 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1155 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1156
1157 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1158 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1159 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1160 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1161
1162 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1163 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1164 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1165 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1166
1167 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1168 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1169 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1170 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1171}
1172
Eric Penner5c3d2432014-07-11 19:08:04 -07001173GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1174 mGraphicBuffer(graphicBuffer),
1175 mEglImage(EGL_NO_IMAGE_KHR),
Pablo Ceballos60d69222015-08-07 14:47:20 -07001176 mEglDisplay(EGL_NO_DISPLAY),
1177 mCropRect(Rect::EMPTY_RECT) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001178}
1179
1180GLConsumer::EglImage::~EglImage() {
1181 if (mEglImage != EGL_NO_IMAGE_KHR) {
1182 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1183 ALOGE("~EglImage: eglDestroyImageKHR failed");
1184 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001185 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001186 }
1187}
1188
1189status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001190 const Rect& cropRect,
1191 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001192 // If there's an image and it's no longer valid, destroy it.
1193 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1194 bool displayInvalid = mEglDisplay != eglDisplay;
1195 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001196 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001197 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1198 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1199 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001200 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001201 mEglImage = EGL_NO_IMAGE_KHR;
1202 mEglDisplay = EGL_NO_DISPLAY;
1203 }
1204
1205 // If there's no image, create one.
1206 if (mEglImage == EGL_NO_IMAGE_KHR) {
1207 mEglDisplay = eglDisplay;
1208 mCropRect = cropRect;
1209 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1210 }
1211
1212 // Fail if we can't create a valid image.
1213 if (mEglImage == EGL_NO_IMAGE_KHR) {
1214 mEglDisplay = EGL_NO_DISPLAY;
1215 mCropRect.makeInvalid();
1216 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1217 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1218 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1219 buffer->getUsage(), buffer->getPixelFormat());
1220 return UNKNOWN_ERROR;
1221 }
1222
1223 return OK;
1224}
1225
1226void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001227 glEGLImageTargetTexture2DOES(texTarget,
1228 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001229}
1230
1231EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1232 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001233 EGLClientBuffer cbuf =
1234 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -07001235 const bool createProtectedImage =
1236 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
1237 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -07001238 EGLint attrs[] = {
1239 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1240 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1241 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1242 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1243 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
Craig Donneraec86972016-04-28 18:09:40 -07001244 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1245 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001246 EGL_NONE,
1247 };
1248 if (!crop.isValid()) {
Craig Donnera94d9402016-10-19 17:18:17 -07001249 // No crop rect to set, so leave the crop out of the attrib array. Make
1250 // sure to propagate the protected content attrs if they are set.
1251 attrs[2] = attrs[10];
1252 attrs[3] = attrs[11];
1253 attrs[4] = EGL_NONE;
Eric Penner5c3d2432014-07-11 19:08:04 -07001254 } else if (!isEglImageCroppable(crop)) {
1255 // The crop rect is not at the origin, so we can't set the crop on the
1256 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1257 // extension. In the future we can add a layered extension that
1258 // removes this restriction if there is hardware that can support it.
Craig Donnera94d9402016-10-19 17:18:17 -07001259 attrs[2] = attrs[10];
1260 attrs[3] = attrs[11];
1261 attrs[4] = EGL_NONE;
Eric Penner5c3d2432014-07-11 19:08:04 -07001262 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001263 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001264 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1265 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1266 if (image == EGL_NO_IMAGE_KHR) {
1267 EGLint error = eglGetError();
1268 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001269 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001270 }
1271 return image;
1272}
1273
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001274}; // namespace android