blob: 6237aa09c286e61252a291d9b1aab79d3d9c58b0 [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
Chia-I Wu3bd03ff2017-11-17 12:04:57 -080034#include <math/mat4.h>
35
Dan Stozadd264162015-03-12 13:58:47 -070036#include <gui/BufferItem.h>
Mathias Agopianca088332013-03-28 17:44:13 -070037#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080038#include <gui/ISurfaceComposer.h>
39#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080040
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070042#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080043
44#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070045#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080046#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080047
Jiyong Park00b15b82017-08-10 20:30:56 +090048extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Craig Donneraec86972016-04-28 18:09:40 -070049#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
50#define EGL_PROTECTED_CONTENT_EXT 0x32C0
Jamie Gennisdbe92452013-09-23 17:22:10 -070051
Andy McFadden97eba892012-12-11 15:21:45 -080052namespace android {
53
Andy McFadden2adaf042012-12-18 09:49:45 -080054// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080055#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
56#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
57//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
58#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
59#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070060
Mathias Agopianad678e12013-07-23 17:28:53 -070061static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080062 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070063 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070064} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080065 "_______________"
66 "_______________"
67 "_____XX_XX_____"
68 "__X_X_____X_X__"
69 "__X_XXXXXXX_X__"
70 "__XXXXXXXXXXX__"
71 "___XX_XXX_XX___"
72 "____XXXXXXX____"
73 "_____X___X_____"
74 "____X_____X____"
75 "_______________"
76 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070077};
Mathias Agopianad678e12013-07-23 17:28:53 -070078
Chia-I Wu3bd03ff2017-11-17 12:04:57 -080079static const mat4 mtxIdentity;
Jamie Gennisf238e282011-01-09 16:33:17 -080080
Mathias Agopian9870c9b2013-08-08 17:46:48 -070081Mutex GLConsumer::sStaticInitLock;
82sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -070083
Craig Donneraec86972016-04-28 18:09:40 -070084static bool hasEglProtectedContentImpl() {
85 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
86 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
87 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
88 size_t extsLen = strlen(exts);
89 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
90 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1);
91 bool atEnd = (cropExtLen+1) < extsLen &&
92 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1));
93 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
Craig Donner4df76b22016-06-13 22:14:15 +000094 return equal || atStart || atEnd || inMiddle;
Craig Donneraec86972016-04-28 18:09:40 -070095}
96
97static bool hasEglProtectedContent() {
98 // Only compute whether the extension is present once the first time this
99 // function is called.
100 static bool hasIt = hasEglProtectedContentImpl();
101 return hasIt;
102}
103
Mathias Agopian3f844832013-08-07 21:24:32 -0700104GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
105 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700106 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700107 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800108 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200109 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800110 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800111 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700112 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700113 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200114 mDefaultWidth(1),
115 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700116 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700117 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800118 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800119 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700120 mEglDisplay(EGL_NO_DISPLAY),
121 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700122 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
123 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800124{
Dan Stozad723bd72014-11-18 10:24:03 -0800125 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800126
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800127 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Jamie Gennisfa28c352011-09-16 17:30:26 -0700128 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700129
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700130 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131}
132
Dan Stozaab574912014-06-25 14:21:45 -0700133GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
134 bool useFenceSync, bool isControlledByApp) :
135 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700136 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700137 mCurrentTransform(0),
138 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
139 mCurrentFence(Fence::NO_FENCE),
140 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700141 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Dan Stozaab574912014-06-25 14:21:45 -0700142 mCurrentFrameNumber(0),
143 mDefaultWidth(1),
144 mDefaultHeight(1),
145 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800146 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700147 mUseFenceSync(useFenceSync),
148 mTexTarget(texTarget),
149 mEglDisplay(EGL_NO_DISPLAY),
150 mEglContext(EGL_NO_CONTEXT),
151 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
152 mAttached(false)
153{
Dan Stozad723bd72014-11-18 10:24:03 -0800154 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700155
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800156 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Dan Stozaab574912014-06-25 14:21:45 -0700157 sizeof(mCurrentTransformMatrix));
158
159 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
160}
161
Andy McFadden2adaf042012-12-18 09:49:45 -0800162status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700163{
Daniel Lamb2675792012-02-23 14:35:13 -0800164 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700165 if (mAbandoned) {
166 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
167 return NO_INIT;
168 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700169 mDefaultWidth = w;
170 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700171 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700172}
173
Andy McFadden2adaf042012-12-18 09:49:45 -0800174status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800175 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800176 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800177 Mutex::Autolock lock(mMutex);
178
179 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800180 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800181 return NO_INIT;
182 }
183
184 // Make sure the EGL state is the same as in previous calls.
185 status_t err = checkAndUpdateEglStateLocked();
186 if (err != NO_ERROR) {
187 return err;
188 }
189
Dan Stozadd264162015-03-12 13:58:47 -0700190 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800191
192 // Acquire the next buffer.
193 // In asynchronous mode the list is guaranteed to be one buffer
194 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700195 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800196 if (err != NO_ERROR) {
197 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
198 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800199 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800200 glBindTexture(mTexTarget, mTexName);
201 err = NO_ERROR;
202 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800203 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800204 strerror(-err), err);
205 }
206 return err;
207 }
208
209 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700210 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800211 if (err != NO_ERROR) {
212 // We always bind the texture.
213 glBindTexture(mTexTarget, mTexName);
214 return err;
215 }
216
Andy McFadden97eba892012-12-11 15:21:45 -0800217 // Bind the new buffer to the GL texture, and wait until it's ready.
218 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700219}
220
Mathias Agopianad678e12013-07-23 17:28:53 -0700221
222status_t GLConsumer::releaseTexImage() {
223 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800224 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700225 Mutex::Autolock lock(mMutex);
226
227 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800228 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700229 return NO_INIT;
230 }
231
232 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700233 status_t err = NO_ERROR;
234
235 if (mAttached) {
236 err = checkAndUpdateEglStateLocked(true);
237 if (err != NO_ERROR) {
238 return err;
239 }
240 } else {
241 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700242 }
243
244 // Update the GLConsumer state.
245 int buf = mCurrentTexture;
246 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
247
Dan Stozad723bd72014-11-18 10:24:03 -0800248 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700249
Mathias Agopian45155962013-08-08 18:16:21 -0700250 if (mAttached) {
251 // Do whatever sync ops we need to do before releasing the slot.
252 err = syncForReleaseLocked(mEglDisplay);
253 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800254 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700255 return err;
256 }
257 } else {
258 // if we're detached, we just use the fence that was created in detachFromContext()
259 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700260 }
261
Mathias Agopian45155962013-08-08 18:16:21 -0700262 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700263 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800264 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700265 strerror(-err), err);
266 return err;
267 }
268
Yi Kong48a619f2018-06-05 16:34:59 -0700269 if (mReleasedTexImage == nullptr) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700270 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
271 }
272
Mathias Agopianad678e12013-07-23 17:28:53 -0700273 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700274 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700275 mCurrentCrop.makeInvalid();
276 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700277 mCurrentTimestamp = 0;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700278 mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopianad678e12013-07-23 17:28:53 -0700279 mCurrentFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700280 mCurrentFenceTime = FenceTime::NO_FENCE;
Mathias Agopianad678e12013-07-23 17:28:53 -0700281
Mathias Agopian45155962013-08-08 18:16:21 -0700282 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700283 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800284 status_t result = bindTextureImageLocked();
285 if (result != NO_ERROR) {
286 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700287 }
Mathias Agopian45155962013-08-08 18:16:21 -0700288 } else {
289 // detached, don't touch the texture (and we may not even have an
290 // EGLDisplay here.
291 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700292 }
293
294 return NO_ERROR;
295}
296
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700297sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
298 Mutex::Autolock _l(sStaticInitLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700299 if (CC_UNLIKELY(sReleasedTexImageBuffer == nullptr)) {
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700300 // The first time, create the debug texture in case the application
301 // continues to use it.
302 sp<GraphicBuffer> buffer = new GraphicBuffer(
303 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
Dan Stozad4079af2016-08-22 17:26:41 -0700304 GraphicBuffer::USAGE_SW_WRITE_RARELY,
305 "[GLConsumer debug texture]");
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700306 uint32_t* bits;
307 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800308 uint32_t stride = buffer->getStride();
309 uint32_t height = buffer->getHeight();
310 memset(bits, 0, stride * height * 4);
311 for (uint32_t y = 0; y < kDebugData.height; y++) {
312 for (uint32_t x = 0; x < kDebugData.width; x++) {
313 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
314 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700315 }
Dan Stozad723bd72014-11-18 10:24:03 -0800316 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700317 }
318 buffer->unlock();
319 sReleasedTexImageBuffer = buffer;
320 }
321 return sReleasedTexImageBuffer;
322}
323
Dan Stozadd264162015-03-12 13:58:47 -0700324status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700325 nsecs_t presentWhen, uint64_t maxFrameNumber) {
326 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
327 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700328 if (err != NO_ERROR) {
329 return err;
330 }
331
Eric Penner5c3d2432014-07-11 19:08:04 -0700332 // If item->mGraphicBuffer is not null, this buffer has not been acquired
333 // before, so any prior EglImage created is using a stale buffer. This
334 // replaces any old EglImage with a new one (using the new buffer).
Yi Kong48a619f2018-06-05 16:34:59 -0700335 if (item->mGraphicBuffer != nullptr) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700336 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700337 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700338 }
339
Jamie Gennis9fea3422012-08-07 18:03:04 -0700340 return NO_ERROR;
341}
342
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700343status_t GLConsumer::releaseBufferLocked(int buf,
344 sp<GraphicBuffer> graphicBuffer,
345 EGLDisplay display, EGLSyncKHR eglFence) {
346 // release the buffer if it hasn't already been discarded by the
347 // BufferQueue. This can happen, for example, when the producer of this
348 // buffer has reallocated the original buffer slot after this buffer
349 // was acquired.
350 status_t err = ConsumerBase::releaseBufferLocked(
351 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700352 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700353 return err;
354}
355
Dan Stoza3ce46042015-11-17 17:00:45 -0800356status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
357 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800358{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700359 status_t err = NO_ERROR;
360
Pablo Ceballos47650f42015-08-04 16:38:17 -0700361 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700362
Jamie Gennis74bed552012-03-28 19:05:54 -0700363 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800364 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700365 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700366 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700367 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700368 return INVALID_OPERATION;
369 }
370
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800371 // Confirm state.
372 err = checkAndUpdateEglStateLocked();
373 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700374 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700375 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800376 return err;
377 }
378
Eric Penner5c3d2432014-07-11 19:08:04 -0700379 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
380 // if nessessary, for the gralloc buffer currently in the slot in
381 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800382 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700383 // means the buffer was previously acquired).
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700384 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700385 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800386 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700387 mEglDisplay, slot);
388 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700389 mEglDisplay, EGL_NO_SYNC_KHR);
390 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800391 }
392
393 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800394 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800395 err = syncForReleaseLocked(mEglDisplay);
396 if (err != NO_ERROR) {
397 // Release the buffer we just acquired. It's not safe to
398 // release the old buffer, so instead we just drop the new frame.
399 // As we are still under lock since acquireBuffer, it is safe to
400 // release by slot.
401 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
402 mEglDisplay, EGL_NO_SYNC_KHR);
403 return err;
404 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800405 }
406
Dan Stozad723bd72014-11-18 10:24:03 -0800407 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700408 mCurrentTexture, mCurrentTextureImage != NULL ?
Yi Kong48a619f2018-06-05 16:34:59 -0700409 mCurrentTextureImage->graphicBufferHandle() : nullptr,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700410 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800411
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700412 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700413 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700414 // the same.
415 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
416
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800417 // release old buffer
418 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800419 if (pendingRelease == nullptr) {
420 status_t status = releaseBufferLocked(
421 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
422 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
423 if (status < NO_ERROR) {
424 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
425 strerror(-status), status);
426 err = status;
427 // keep going, with error raised [?]
428 }
429 } else {
430 pendingRelease->currentTexture = mCurrentTexture;
431 pendingRelease->graphicBuffer =
432 mCurrentTextureImage->graphicBuffer();
433 pendingRelease->display = mEglDisplay;
434 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
435 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800436 }
437 }
438
Andy McFadden2adaf042012-12-18 09:49:45 -0800439 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700440 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700441 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800442 mCurrentCrop = item.mCrop;
443 mCurrentTransform = item.mTransform;
444 mCurrentScalingMode = item.mScalingMode;
445 mCurrentTimestamp = item.mTimestamp;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700446 mCurrentDataSpace = item.mDataSpace;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800447 mCurrentFence = item.mFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700448 mCurrentFenceTime = item.mFenceTime;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700449 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800450
451 computeCurrentTransformMatrixLocked();
452
453 return err;
454}
455
Andy McFadden2adaf042012-12-18 09:49:45 -0800456status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800457 if (mEglDisplay == EGL_NO_DISPLAY) {
458 ALOGE("bindTextureImage: invalid display");
459 return INVALID_OPERATION;
460 }
461
Dan Stozad723bd72014-11-18 10:24:03 -0800462 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800463 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800464 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800465 }
466
467 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700468 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
Yi Kong48a619f2018-06-05 16:34:59 -0700469 mCurrentTextureImage == nullptr) {
Dan Stozad723bd72014-11-18 10:24:03 -0800470 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700471 return NO_INIT;
472 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800473
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700474 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700475 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800476 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700477 mEglDisplay, mCurrentTexture);
478 return UNKNOWN_ERROR;
479 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700480 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
481
Eric Penner2d14a0e2014-08-25 20:16:37 -0700482 // In the rare case that the display is terminated and then initialized
483 // again, we can't detect that the display changed (it didn't), but the
484 // image is invalid. In this case, repeat the exact same steps while
485 // forcing the creation of a new image.
486 if ((error = glGetError()) != GL_NO_ERROR) {
487 glBindTexture(mTexTarget, mTexName);
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700488 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, true);
Dan Stozad723bd72014-11-18 10:24:03 -0800489 if (result != NO_ERROR) {
490 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700491 mEglDisplay, mCurrentTexture);
492 return UNKNOWN_ERROR;
493 }
494 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
495 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800496 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700497 return UNKNOWN_ERROR;
498 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800499 }
Andy McFadden97eba892012-12-11 15:21:45 -0800500
501 // Wait for the new buffer to be ready.
502 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800503}
504
Mathias Agopian45155962013-08-08 18:16:21 -0700505status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700506 EGLDisplay dpy = eglGetCurrentDisplay();
507 EGLContext ctx = eglGetCurrentContext();
508
Mathias Agopian45155962013-08-08 18:16:21 -0700509 if (!contextCheck) {
510 // if this is the first time we're called, mEglDisplay/mEglContext have
511 // never been set, so don't error out (below).
512 if (mEglDisplay == EGL_NO_DISPLAY) {
513 mEglDisplay = dpy;
514 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700515 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700516 mEglContext = ctx;
517 }
518 }
519
520 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800521 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700522 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700523 }
524
Mathias Agopian45155962013-08-08 18:16:21 -0700525 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800526 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700527 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700528 }
529
530 mEglDisplay = dpy;
531 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800532 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800533}
534
Jesse Hall13f01cb2013-03-20 11:37:21 -0700535void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
536 if (fence->isValid() &&
537 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700538 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700539 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700540 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800541 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700542 strerror(-err), err);
543 }
Jesse Hallef194142012-06-14 14:45:17 -0700544 }
545}
546
Andy McFadden2adaf042012-12-18 09:49:45 -0800547status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700548 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800549 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700550 Mutex::Autolock lock(mMutex);
551
552 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800553 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700554 return NO_INIT;
555 }
556
557 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800558 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700559 "context");
560 return INVALID_OPERATION;
561 }
562
563 EGLDisplay dpy = eglGetCurrentDisplay();
564 EGLContext ctx = eglGetCurrentContext();
565
566 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800567 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700568 return INVALID_OPERATION;
569 }
570
571 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800572 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700573 return INVALID_OPERATION;
574 }
575
576 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
577 status_t err = syncForReleaseLocked(dpy);
578 if (err != OK) {
579 return err;
580 }
581
582 glDeleteTextures(1, &mTexName);
583 }
584
585 mEglDisplay = EGL_NO_DISPLAY;
586 mEglContext = EGL_NO_CONTEXT;
587 mAttached = false;
588
589 return OK;
590}
591
Mathias Agopian3f844832013-08-07 21:24:32 -0700592status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700593 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800594 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700595 Mutex::Autolock lock(mMutex);
596
597 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800598 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700599 return NO_INIT;
600 }
601
602 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800603 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700604 "context");
605 return INVALID_OPERATION;
606 }
607
608 EGLDisplay dpy = eglGetCurrentDisplay();
609 EGLContext ctx = eglGetCurrentContext();
610
611 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800612 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700613 return INVALID_OPERATION;
614 }
615
616 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800617 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700618 return INVALID_OPERATION;
619 }
620
621 // We need to bind the texture regardless of whether there's a current
622 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700623 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700624
Jamie Gennis74bed552012-03-28 19:05:54 -0700625 mEglDisplay = dpy;
626 mEglContext = ctx;
627 mTexName = tex;
628 mAttached = true;
629
Yi Kong48a619f2018-06-05 16:34:59 -0700630 if (mCurrentTextureImage != nullptr) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700631 // This may wait for a buffer a second time. This is likely required if
632 // this is a different context, since otherwise the wait could be skipped
633 // by bouncing through another context. For the same context the extra
634 // wait is redundant.
635 status_t err = bindTextureImageLocked();
636 if (err != NO_ERROR) {
637 return err;
638 }
639 }
640
Jamie Gennis74bed552012-03-28 19:05:54 -0700641 return OK;
642}
643
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800644
Andy McFadden2adaf042012-12-18 09:49:45 -0800645status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800646 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700647
Jamie Gennis01dbf552012-09-06 14:54:19 -0700648 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700649 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700650 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
Yi Kong48a619f2018-06-05 16:34:59 -0700651 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700652 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800653 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700654 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700655 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700656 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700657 glFlush();
658 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700659 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700660 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800661 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700662 "fd: %#x", eglGetError());
663 return UNKNOWN_ERROR;
664 }
665 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700666 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700667 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700668 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800669 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700670 "%s (%d)", strerror(-err), err);
671 return err;
672 }
Mathias Agopianca088332013-03-28 17:44:13 -0700673 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700674 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
675 if (fence != EGL_NO_SYNC_KHR) {
676 // There is already a fence for the current slot. We need to
677 // wait on that before replacing it with another fence to
678 // ensure that all outstanding buffer accesses have completed
679 // before the producer accesses it.
680 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
681 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800682 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700683 "fence: %#x", eglGetError());
684 return UNKNOWN_ERROR;
685 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800686 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700687 "fence");
688 return TIMED_OUT;
689 }
690 eglDestroySyncKHR(dpy, fence);
691 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700692
Jamie Gennis01dbf552012-09-06 14:54:19 -0700693 // Create a fence for the outstanding accesses in the current
694 // OpenGL ES context.
Yi Kong48a619f2018-06-05 16:34:59 -0700695 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700696 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800697 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700698 eglGetError());
699 return UNKNOWN_ERROR;
700 }
701 glFlush();
702 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700703 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700704 }
705
706 return OK;
707}
708
Mathias Agopian3f844832013-08-07 21:24:32 -0700709uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700710 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700711}
712
Andy McFadden2adaf042012-12-18 09:49:45 -0800713void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800714 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700715 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
716}
717
Andy McFadden2adaf042012-12-18 09:49:45 -0800718void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700719 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700720 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800721 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700722 return;
723 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700724 bool needsRecompute = mFilteringEnabled != enabled;
725 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700726
Yi Kong48a619f2018-06-05 16:34:59 -0700727 if (needsRecompute && mCurrentTextureImage==nullptr) {
Dan Stozad723bd72014-11-18 10:24:03 -0800728 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700729 }
730
Yi Kong48a619f2018-06-05 16:34:59 -0700731 if (needsRecompute && mCurrentTextureImage != nullptr) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700732 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700733 }
734}
735
Andy McFadden2adaf042012-12-18 09:49:45 -0800736void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800737 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700738 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
739 nullptr : mCurrentTextureImage->graphicBuffer();
740 if (buf == nullptr) {
741 GLC_LOGD("computeCurrentTransformMatrixLocked: "
742 "mCurrentTextureImage is NULL");
743 }
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700744 computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop,
John Reck1a61da52016-04-28 13:18:15 -0700745 mCurrentTransform, mFilteringEnabled);
746}
747
748void GLConsumer::computeTransformMatrix(float outTransform[16],
749 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
750 bool filtering) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800751 // Transform matrices
752 static const mat4 mtxFlipH(
753 -1, 0, 0, 0,
754 0, 1, 0, 0,
755 0, 0, 1, 0,
756 1, 0, 0, 1
757 );
758 static const mat4 mtxFlipV(
759 1, 0, 0, 0,
760 0, -1, 0, 0,
761 0, 0, 1, 0,
762 0, 1, 0, 1
763 );
764 static const mat4 mtxRot90(
765 0, 1, 0, 0,
766 -1, 0, 0, 0,
767 0, 0, 1, 0,
768 1, 0, 0, 1
769 );
Jamie Gennisf238e282011-01-09 16:33:17 -0800770
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800771 mat4 xform;
John Reck1a61da52016-04-28 13:18:15 -0700772 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800773 xform *= mtxFlipH;
Jamie Gennisa214c642011-01-14 13:53:31 -0800774 }
John Reck1a61da52016-04-28 13:18:15 -0700775 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800776 xform *= mtxFlipV;
Jamie Gennisa214c642011-01-14 13:53:31 -0800777 }
John Reck1a61da52016-04-28 13:18:15 -0700778 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800779 xform *= mtxRot90;
Jamie Gennisf238e282011-01-09 16:33:17 -0800780 }
781
John Reck1a61da52016-04-28 13:18:15 -0700782 if (!cropRect.isEmpty()) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700783 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
784 float bufferWidth = buf->getWidth();
785 float bufferHeight = buf->getHeight();
John Reck1a61da52016-04-28 13:18:15 -0700786 float shrinkAmount = 0.0f;
787 if (filtering) {
788 // In order to prevent bilinear sampling beyond the edge of the
789 // crop rectangle we may need to shrink it by 2 texels in each
790 // dimension. Normally this would just need to take 1/2 a texel
791 // off each end, but because the chroma channels of YUV420 images
792 // are subsampled we may need to shrink the crop region by a whole
793 // texel on each side.
794 switch (buf->getPixelFormat()) {
795 case PIXEL_FORMAT_RGBA_8888:
796 case PIXEL_FORMAT_RGBX_8888:
Romain Guyff415142016-12-13 16:51:25 -0800797 case PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -0800798 case PIXEL_FORMAT_RGBA_1010102:
John Reck1a61da52016-04-28 13:18:15 -0700799 case PIXEL_FORMAT_RGB_888:
800 case PIXEL_FORMAT_RGB_565:
801 case PIXEL_FORMAT_BGRA_8888:
802 // We know there's no subsampling of any channels, so we
803 // only need to shrink by a half a pixel.
804 shrinkAmount = 0.5;
805 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700806
John Reck1a61da52016-04-28 13:18:15 -0700807 default:
808 // If we don't recognize the format, we must assume the
809 // worst case (that we care about), which is YUV420.
810 shrinkAmount = 1.0;
811 break;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700812 }
John Reck1a61da52016-04-28 13:18:15 -0700813 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700814
John Reck1a61da52016-04-28 13:18:15 -0700815 // Only shrink the dimensions that are not the size of the buffer.
816 if (cropRect.width() < bufferWidth) {
817 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
818 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
819 bufferWidth;
820 }
821 if (cropRect.height() < bufferHeight) {
822 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
823 bufferHeight;
824 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
825 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700826 }
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800827
828 mat4 crop(
Jamie Gennisdbe92452013-09-23 17:22:10 -0700829 sx, 0, 0, 0,
830 0, sy, 0, 0,
831 0, 0, 1, 0,
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800832 tx, ty, 0, 1
833 );
834 xform = crop * xform;
Jamie Gennisa214c642011-01-14 13:53:31 -0800835 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800836
837 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800838 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800839 // want to expose this to applications, however, so we must add an
840 // additional vertical flip to the transform after all the other transforms.
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800841 xform = mtxFlipV * xform;
842
843 memcpy(outTransform, xform.asArray(), sizeof(xform));
Jamie Gennisf238e282011-01-09 16:33:17 -0800844}
845
Chia-I Wu93b122f2017-12-01 08:51:39 -0800846Rect GLConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
847 Rect outCrop = crop;
848
849 uint32_t newWidth = static_cast<uint32_t>(crop.width());
850 uint32_t newHeight = static_cast<uint32_t>(crop.height());
851
852 if (newWidth * bufferHeight > newHeight * bufferWidth) {
853 newWidth = newHeight * bufferWidth / bufferHeight;
854 ALOGV("too wide: newWidth = %d", newWidth);
855 } else if (newWidth * bufferHeight < newHeight * bufferWidth) {
856 newHeight = newWidth * bufferHeight / bufferWidth;
857 ALOGV("too tall: newHeight = %d", newHeight);
858 }
859
860 uint32_t currentWidth = static_cast<uint32_t>(crop.width());
861 uint32_t currentHeight = static_cast<uint32_t>(crop.height());
862
863 // The crop is too wide
864 if (newWidth < currentWidth) {
865 uint32_t dw = currentWidth - newWidth;
866 auto halfdw = dw / 2;
867 outCrop.left += halfdw;
868 // Not halfdw because it would subtract 1 too few when dw is odd
869 outCrop.right -= (dw - halfdw);
870 // The crop is too tall
871 } else if (newHeight < currentHeight) {
872 uint32_t dh = currentHeight - newHeight;
873 auto halfdh = dh / 2;
874 outCrop.top += halfdh;
875 // Not halfdh because it would subtract 1 too few when dh is odd
876 outCrop.bottom -= (dh - halfdh);
877 }
878
879 ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
880 outCrop.left, outCrop.top,
881 outCrop.right,outCrop.bottom);
882
883 return outCrop;
884}
885
Andy McFadden2adaf042012-12-18 09:49:45 -0800886nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800887 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800888 Mutex::Autolock lock(mMutex);
889 return mCurrentTimestamp;
890}
891
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700892android_dataspace GLConsumer::getCurrentDataSpace() {
893 GLC_LOGV("getCurrentDataSpace");
894 Mutex::Autolock lock(mMutex);
895 return mCurrentDataSpace;
896}
897
Dan Stozad723bd72014-11-18 10:24:03 -0800898uint64_t GLConsumer::getFrameNumber() {
899 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700900 Mutex::Autolock lock(mMutex);
901 return mCurrentFrameNumber;
902}
903
Chia-I Wu06d63de2017-01-04 14:58:51 +0800904sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700905 Mutex::Autolock lock(mMutex);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800906
907 if (outSlot != nullptr) {
908 *outSlot = mCurrentTexture;
909 }
910
911 return (mCurrentTextureImage == nullptr) ?
Yi Kong48a619f2018-06-05 16:34:59 -0700912 nullptr : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700913}
914
Andy McFadden2adaf042012-12-18 09:49:45 -0800915Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700916 Mutex::Autolock lock(mMutex);
Chia-I Wu93b122f2017-12-01 08:51:39 -0800917 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
918 ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
919 : mCurrentCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700920}
921
Andy McFadden2adaf042012-12-18 09:49:45 -0800922uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700923 Mutex::Autolock lock(mMutex);
924 return mCurrentTransform;
925}
926
Andy McFadden2adaf042012-12-18 09:49:45 -0800927uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700928 Mutex::Autolock lock(mMutex);
929 return mCurrentScalingMode;
930}
931
Andy McFadden2adaf042012-12-18 09:49:45 -0800932sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700933 Mutex::Autolock lock(mMutex);
934 return mCurrentFence;
935}
936
Brian Anderson3d4039d2016-09-23 16:31:30 -0700937std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const {
938 Mutex::Autolock lock(mMutex);
939 return mCurrentFenceTime;
940}
941
Andy McFadden2adaf042012-12-18 09:49:45 -0800942status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700943
944 EGLDisplay dpy = eglGetCurrentDisplay();
945 EGLContext ctx = eglGetCurrentContext();
946
947 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800948 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700949 return INVALID_OPERATION;
950 }
951
952 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800953 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700954 return INVALID_OPERATION;
955 }
956
Jamie Gennis1df8c342012-12-20 14:05:45 -0800957 if (mCurrentFence->isValid()) {
Jeffrey Kardatzkea45e1c82018-03-13 12:07:28 -0700958 if (SyncFeatures::getInstance().useWaitSync() &&
959 SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700960 // Create an EGLSyncKHR from the current fence.
961 int fenceFd = mCurrentFence->dup();
962 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800963 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700964 return -errno;
965 }
966 EGLint attribs[] = {
967 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
968 EGL_NONE
969 };
970 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
971 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
972 if (sync == EGL_NO_SYNC_KHR) {
973 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800974 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700975 eglGetError());
976 return UNKNOWN_ERROR;
977 }
978
979 // XXX: The spec draft is inconsistent as to whether this should
980 // return an EGLint or void. Ignore the return value for now, as
981 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700982 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700983 EGLint eglErr = eglGetError();
984 eglDestroySyncKHR(dpy, sync);
985 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800986 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700987 eglErr);
988 return UNKNOWN_ERROR;
989 }
990 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700991 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800992 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700993 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800994 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700995 return err;
996 }
997 }
998 }
999
1000 return NO_ERROR;
1001}
1002
Andy McFadden2adaf042012-12-18 09:49:45 -08001003void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001004 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001005 if (slotIndex == mCurrentTexture) {
1006 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1007 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001008 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001009 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001010}
Daniel Lameae59d22012-01-22 15:26:27 -08001011
Andy McFadden2adaf042012-12-18 09:49:45 -08001012void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001013 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001014 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001015 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001016}
1017
Chia-I Wue2786ea2017-08-07 10:36:08 -07001018status_t GLConsumer::setConsumerUsageBits(uint64_t usage) {
Chia-I Wua81bc492017-11-27 10:16:00 -08001019 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001020}
1021
Mathias Agopian74d211a2013-04-22 16:55:35 +02001022void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001023{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001024 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001025 "%smTexName=%d mCurrentTexture=%d\n"
1026 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1027 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1028 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1029 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001030
Mathias Agopian74d211a2013-04-22 16:55:35 +02001031 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001032}
1033
Eric Penner5c3d2432014-07-11 19:08:04 -07001034GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1035 mGraphicBuffer(graphicBuffer),
1036 mEglImage(EGL_NO_IMAGE_KHR),
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001037 mEglDisplay(EGL_NO_DISPLAY) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001038}
1039
1040GLConsumer::EglImage::~EglImage() {
1041 if (mEglImage != EGL_NO_IMAGE_KHR) {
1042 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1043 ALOGE("~EglImage: eglDestroyImageKHR failed");
1044 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001045 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001046 }
1047}
1048
1049status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001050 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001051 // If there's an image and it's no longer valid, destroy it.
1052 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1053 bool displayInvalid = mEglDisplay != eglDisplay;
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001054 if (haveImage && (displayInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001055 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1056 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1057 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001058 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001059 mEglImage = EGL_NO_IMAGE_KHR;
1060 mEglDisplay = EGL_NO_DISPLAY;
1061 }
1062
1063 // If there's no image, create one.
1064 if (mEglImage == EGL_NO_IMAGE_KHR) {
1065 mEglDisplay = eglDisplay;
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001066 mEglImage = createImage(mEglDisplay, mGraphicBuffer);
Eric Penner5c3d2432014-07-11 19:08:04 -07001067 }
1068
1069 // Fail if we can't create a valid image.
1070 if (mEglImage == EGL_NO_IMAGE_KHR) {
1071 mEglDisplay = EGL_NO_DISPLAY;
Eric Penner5c3d2432014-07-11 19:08:04 -07001072 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001073 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -07001074 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1075 buffer->getUsage(), buffer->getPixelFormat());
1076 return UNKNOWN_ERROR;
1077 }
1078
1079 return OK;
1080}
1081
1082void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001083 glEGLImageTargetTexture2DOES(texTarget,
1084 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001085}
1086
1087EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001088 const sp<GraphicBuffer>& graphicBuffer) {
Dan Stozad723bd72014-11-18 10:24:03 -08001089 EGLClientBuffer cbuf =
1090 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -07001091 const bool createProtectedImage =
1092 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
1093 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -07001094 EGLint attrs[] = {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001095 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
Craig Donneraec86972016-04-28 18:09:40 -07001096 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1097 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001098 EGL_NONE,
1099 };
Yi Kong48a619f2018-06-05 16:34:59 -07001100 eglInitialize(dpy, nullptr, nullptr);
Eric Penner5c3d2432014-07-11 19:08:04 -07001101 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1102 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1103 if (image == EGL_NO_IMAGE_KHR) {
1104 EGLint error = eglGetError();
1105 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001106 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001107 }
1108 return image;
1109}
1110
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001111}; // namespace android