blob: 885efec9b9881a8c74b2b2ac4a0020b4985c88c8 [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);
Jamie Gennisdbe92452013-09-23 17:22:10 -070049#define CROP_EXT_STR "EGL_ANDROID_image_crop"
Craig Donneraec86972016-04-28 18:09:40 -070050#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
51#define EGL_PROTECTED_CONTENT_EXT 0x32C0
Jamie Gennisdbe92452013-09-23 17:22:10 -070052
Andy McFadden97eba892012-12-11 15:21:45 -080053namespace android {
54
Andy McFadden2adaf042012-12-18 09:49:45 -080055// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080056#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
57#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
58//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
59#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
60#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070061
Mathias Agopianad678e12013-07-23 17:28:53 -070062static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080063 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070064 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070065} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080066 "_______________"
67 "_______________"
68 "_____XX_XX_____"
69 "__X_X_____X_X__"
70 "__X_XXXXXXX_X__"
71 "__XXXXXXXXXXX__"
72 "___XX_XXX_XX___"
73 "____XXXXXXX____"
74 "_____X___X_____"
75 "____X_____X____"
76 "_______________"
77 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070078};
Mathias Agopianad678e12013-07-23 17:28:53 -070079
Chia-I Wu3bd03ff2017-11-17 12:04:57 -080080static const mat4 mtxIdentity;
Jamie Gennisf238e282011-01-09 16:33:17 -080081
Mathias Agopian9870c9b2013-08-08 17:46:48 -070082Mutex GLConsumer::sStaticInitLock;
83sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -070084
Jamie Gennisdbe92452013-09-23 17:22:10 -070085static bool hasEglAndroidImageCropImpl() {
86 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
87 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
88 size_t cropExtLen = strlen(CROP_EXT_STR);
89 size_t extsLen = strlen(exts);
90 bool equal = !strcmp(CROP_EXT_STR, exts);
91 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
92 bool atEnd = (cropExtLen+1) < extsLen &&
93 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
94 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
95 return equal || atStart || atEnd || inMiddle;
96}
97
98static bool hasEglAndroidImageCrop() {
99 // Only compute whether the extension is present once the first time this
100 // function is called.
101 static bool hasIt = hasEglAndroidImageCropImpl();
102 return hasIt;
103}
104
Craig Donneraec86972016-04-28 18:09:40 -0700105static bool hasEglProtectedContentImpl() {
106 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
107 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
108 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
109 size_t extsLen = strlen(exts);
110 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
111 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1);
112 bool atEnd = (cropExtLen+1) < extsLen &&
113 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1));
114 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
Craig Donner4df76b22016-06-13 22:14:15 +0000115 return equal || atStart || atEnd || inMiddle;
Craig Donneraec86972016-04-28 18:09:40 -0700116}
117
118static bool hasEglProtectedContent() {
119 // Only compute whether the extension is present once the first time this
120 // function is called.
121 static bool hasIt = hasEglProtectedContentImpl();
122 return hasIt;
123}
124
Jamie Gennisdbe92452013-09-23 17:22:10 -0700125static bool isEglImageCroppable(const Rect& crop) {
126 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
127}
128
Mathias Agopian3f844832013-08-07 21:24:32 -0700129GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
130 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700131 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700132 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800133 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200134 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800135 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800136 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700137 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700138 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200139 mDefaultWidth(1),
140 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700141 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700142 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800143 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800144 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700145 mEglDisplay(EGL_NO_DISPLAY),
146 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700147 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
148 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800149{
Dan Stozad723bd72014-11-18 10:24:03 -0800150 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800151
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800152 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Jamie Gennisfa28c352011-09-16 17:30:26 -0700153 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700154
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700155 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800156}
157
Dan Stozaab574912014-06-25 14:21:45 -0700158GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
159 bool useFenceSync, bool isControlledByApp) :
160 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700161 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700162 mCurrentTransform(0),
163 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
164 mCurrentFence(Fence::NO_FENCE),
165 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700166 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Dan Stozaab574912014-06-25 14:21:45 -0700167 mCurrentFrameNumber(0),
168 mDefaultWidth(1),
169 mDefaultHeight(1),
170 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800171 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700172 mUseFenceSync(useFenceSync),
173 mTexTarget(texTarget),
174 mEglDisplay(EGL_NO_DISPLAY),
175 mEglContext(EGL_NO_CONTEXT),
176 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
177 mAttached(false)
178{
Dan Stozad723bd72014-11-18 10:24:03 -0800179 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700180
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800181 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Dan Stozaab574912014-06-25 14:21:45 -0700182 sizeof(mCurrentTransformMatrix));
183
184 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
185}
186
Andy McFadden2adaf042012-12-18 09:49:45 -0800187status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700188{
Daniel Lamb2675792012-02-23 14:35:13 -0800189 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700190 if (mAbandoned) {
191 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
192 return NO_INIT;
193 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700194 mDefaultWidth = w;
195 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700196 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700197}
198
Andy McFadden2adaf042012-12-18 09:49:45 -0800199status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800200 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800201 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800202 Mutex::Autolock lock(mMutex);
203
204 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800205 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800206 return NO_INIT;
207 }
208
209 // Make sure the EGL state is the same as in previous calls.
210 status_t err = checkAndUpdateEglStateLocked();
211 if (err != NO_ERROR) {
212 return err;
213 }
214
Dan Stozadd264162015-03-12 13:58:47 -0700215 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800216
217 // Acquire the next buffer.
218 // In asynchronous mode the list is guaranteed to be one buffer
219 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700220 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800221 if (err != NO_ERROR) {
222 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
223 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800224 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800225 glBindTexture(mTexTarget, mTexName);
226 err = NO_ERROR;
227 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800228 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800229 strerror(-err), err);
230 }
231 return err;
232 }
233
234 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700235 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800236 if (err != NO_ERROR) {
237 // We always bind the texture.
238 glBindTexture(mTexTarget, mTexName);
239 return err;
240 }
241
Andy McFadden97eba892012-12-11 15:21:45 -0800242 // Bind the new buffer to the GL texture, and wait until it's ready.
243 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700244}
245
Mathias Agopianad678e12013-07-23 17:28:53 -0700246
247status_t GLConsumer::releaseTexImage() {
248 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800249 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700250 Mutex::Autolock lock(mMutex);
251
252 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800253 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700254 return NO_INIT;
255 }
256
257 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700258 status_t err = NO_ERROR;
259
260 if (mAttached) {
261 err = checkAndUpdateEglStateLocked(true);
262 if (err != NO_ERROR) {
263 return err;
264 }
265 } else {
266 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700267 }
268
269 // Update the GLConsumer state.
270 int buf = mCurrentTexture;
271 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
272
Dan Stozad723bd72014-11-18 10:24:03 -0800273 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700274
Mathias Agopian45155962013-08-08 18:16:21 -0700275 if (mAttached) {
276 // Do whatever sync ops we need to do before releasing the slot.
277 err = syncForReleaseLocked(mEglDisplay);
278 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800279 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700280 return err;
281 }
282 } else {
283 // if we're detached, we just use the fence that was created in detachFromContext()
284 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700285 }
286
Mathias Agopian45155962013-08-08 18:16:21 -0700287 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700288 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800289 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700290 strerror(-err), err);
291 return err;
292 }
293
Eric Penner5c3d2432014-07-11 19:08:04 -0700294 if (mReleasedTexImage == NULL) {
295 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
296 }
297
Mathias Agopianad678e12013-07-23 17:28:53 -0700298 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700299 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700300 mCurrentCrop.makeInvalid();
301 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700302 mCurrentTimestamp = 0;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700303 mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopianad678e12013-07-23 17:28:53 -0700304 mCurrentFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700305 mCurrentFenceTime = FenceTime::NO_FENCE;
Mathias Agopianad678e12013-07-23 17:28:53 -0700306
Mathias Agopian45155962013-08-08 18:16:21 -0700307 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700308 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800309 status_t result = bindTextureImageLocked();
310 if (result != NO_ERROR) {
311 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700312 }
Mathias Agopian45155962013-08-08 18:16:21 -0700313 } else {
314 // detached, don't touch the texture (and we may not even have an
315 // EGLDisplay here.
316 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700317 }
318
319 return NO_ERROR;
320}
321
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700322sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
323 Mutex::Autolock _l(sStaticInitLock);
324 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
325 // The first time, create the debug texture in case the application
326 // continues to use it.
327 sp<GraphicBuffer> buffer = new GraphicBuffer(
328 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
Dan Stozad4079af2016-08-22 17:26:41 -0700329 GraphicBuffer::USAGE_SW_WRITE_RARELY,
330 "[GLConsumer debug texture]");
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700331 uint32_t* bits;
332 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800333 uint32_t stride = buffer->getStride();
334 uint32_t height = buffer->getHeight();
335 memset(bits, 0, stride * height * 4);
336 for (uint32_t y = 0; y < kDebugData.height; y++) {
337 for (uint32_t x = 0; x < kDebugData.width; x++) {
338 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
339 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700340 }
Dan Stozad723bd72014-11-18 10:24:03 -0800341 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700342 }
343 buffer->unlock();
344 sReleasedTexImageBuffer = buffer;
345 }
346 return sReleasedTexImageBuffer;
347}
348
Dan Stozadd264162015-03-12 13:58:47 -0700349status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700350 nsecs_t presentWhen, uint64_t maxFrameNumber) {
351 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
352 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700353 if (err != NO_ERROR) {
354 return err;
355 }
356
Eric Penner5c3d2432014-07-11 19:08:04 -0700357 // If item->mGraphicBuffer is not null, this buffer has not been acquired
358 // before, so any prior EglImage created is using a stale buffer. This
359 // replaces any old EglImage with a new one (using the new buffer).
360 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700361 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700362 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700363 }
364
Jamie Gennis9fea3422012-08-07 18:03:04 -0700365 return NO_ERROR;
366}
367
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700368status_t GLConsumer::releaseBufferLocked(int buf,
369 sp<GraphicBuffer> graphicBuffer,
370 EGLDisplay display, EGLSyncKHR eglFence) {
371 // release the buffer if it hasn't already been discarded by the
372 // BufferQueue. This can happen, for example, when the producer of this
373 // buffer has reallocated the original buffer slot after this buffer
374 // was acquired.
375 status_t err = ConsumerBase::releaseBufferLocked(
376 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700377 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700378 return err;
379}
380
Dan Stoza3ce46042015-11-17 17:00:45 -0800381status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
382 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800383{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700384 status_t err = NO_ERROR;
385
Pablo Ceballos47650f42015-08-04 16:38:17 -0700386 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700387
Jamie Gennis74bed552012-03-28 19:05:54 -0700388 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800389 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700390 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700391 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700392 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700393 return INVALID_OPERATION;
394 }
395
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800396 // Confirm state.
397 err = checkAndUpdateEglStateLocked();
398 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700399 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700400 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800401 return err;
402 }
403
Eric Penner5c3d2432014-07-11 19:08:04 -0700404 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
405 // if nessessary, for the gralloc buffer currently in the slot in
406 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800407 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700408 // means the buffer was previously acquired).
Pablo Ceballos47650f42015-08-04 16:38:17 -0700409 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700410 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800411 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700412 mEglDisplay, slot);
413 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700414 mEglDisplay, EGL_NO_SYNC_KHR);
415 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800416 }
417
418 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800419 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800420 err = syncForReleaseLocked(mEglDisplay);
421 if (err != NO_ERROR) {
422 // Release the buffer we just acquired. It's not safe to
423 // release the old buffer, so instead we just drop the new frame.
424 // As we are still under lock since acquireBuffer, it is safe to
425 // release by slot.
426 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
427 mEglDisplay, EGL_NO_SYNC_KHR);
428 return err;
429 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800430 }
431
Dan Stozad723bd72014-11-18 10:24:03 -0800432 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700433 mCurrentTexture, mCurrentTextureImage != NULL ?
434 mCurrentTextureImage->graphicBufferHandle() : 0,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700435 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800436
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700437 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700438 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700439 // the same.
440 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
441
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800442 // release old buffer
443 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800444 if (pendingRelease == nullptr) {
445 status_t status = releaseBufferLocked(
446 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
447 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
448 if (status < NO_ERROR) {
449 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
450 strerror(-status), status);
451 err = status;
452 // keep going, with error raised [?]
453 }
454 } else {
455 pendingRelease->currentTexture = mCurrentTexture;
456 pendingRelease->graphicBuffer =
457 mCurrentTextureImage->graphicBuffer();
458 pendingRelease->display = mEglDisplay;
459 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
460 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800461 }
462 }
463
Andy McFadden2adaf042012-12-18 09:49:45 -0800464 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700465 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700466 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800467 mCurrentCrop = item.mCrop;
468 mCurrentTransform = item.mTransform;
469 mCurrentScalingMode = item.mScalingMode;
470 mCurrentTimestamp = item.mTimestamp;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700471 mCurrentDataSpace = item.mDataSpace;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800472 mCurrentFence = item.mFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700473 mCurrentFenceTime = item.mFenceTime;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700474 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800475
476 computeCurrentTransformMatrixLocked();
477
478 return err;
479}
480
Andy McFadden2adaf042012-12-18 09:49:45 -0800481status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800482 if (mEglDisplay == EGL_NO_DISPLAY) {
483 ALOGE("bindTextureImage: invalid display");
484 return INVALID_OPERATION;
485 }
486
Dan Stozad723bd72014-11-18 10:24:03 -0800487 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800488 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800489 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800490 }
491
492 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700493 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
494 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800495 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700496 return NO_INIT;
497 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800498
Eric Penner5c3d2432014-07-11 19:08:04 -0700499 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700500 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700501 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800502 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700503 mEglDisplay, mCurrentTexture);
504 return UNKNOWN_ERROR;
505 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700506 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
507
Eric Penner2d14a0e2014-08-25 20:16:37 -0700508 // In the rare case that the display is terminated and then initialized
509 // again, we can't detect that the display changed (it didn't), but the
510 // image is invalid. In this case, repeat the exact same steps while
511 // forcing the creation of a new image.
512 if ((error = glGetError()) != GL_NO_ERROR) {
513 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800514 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
515 mCurrentCrop,
516 true);
517 if (result != NO_ERROR) {
518 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700519 mEglDisplay, mCurrentTexture);
520 return UNKNOWN_ERROR;
521 }
522 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
523 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800524 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700525 return UNKNOWN_ERROR;
526 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800527 }
Andy McFadden97eba892012-12-11 15:21:45 -0800528
529 // Wait for the new buffer to be ready.
530 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800531}
532
Mathias Agopian45155962013-08-08 18:16:21 -0700533status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700534 EGLDisplay dpy = eglGetCurrentDisplay();
535 EGLContext ctx = eglGetCurrentContext();
536
Mathias Agopian45155962013-08-08 18:16:21 -0700537 if (!contextCheck) {
538 // if this is the first time we're called, mEglDisplay/mEglContext have
539 // never been set, so don't error out (below).
540 if (mEglDisplay == EGL_NO_DISPLAY) {
541 mEglDisplay = dpy;
542 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700543 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700544 mEglContext = ctx;
545 }
546 }
547
548 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800549 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700550 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700551 }
552
Mathias Agopian45155962013-08-08 18:16:21 -0700553 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800554 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700555 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700556 }
557
558 mEglDisplay = dpy;
559 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800560 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800561}
562
Jesse Hall13f01cb2013-03-20 11:37:21 -0700563void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
564 if (fence->isValid() &&
565 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700566 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700567 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700568 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800569 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700570 strerror(-err), err);
571 }
Jesse Hallef194142012-06-14 14:45:17 -0700572 }
573}
574
Andy McFadden2adaf042012-12-18 09:49:45 -0800575status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700576 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800577 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 Mutex::Autolock lock(mMutex);
579
580 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800581 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700582 return NO_INIT;
583 }
584
585 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800586 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700587 "context");
588 return INVALID_OPERATION;
589 }
590
591 EGLDisplay dpy = eglGetCurrentDisplay();
592 EGLContext ctx = eglGetCurrentContext();
593
594 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800595 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700596 return INVALID_OPERATION;
597 }
598
599 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800600 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700601 return INVALID_OPERATION;
602 }
603
604 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
605 status_t err = syncForReleaseLocked(dpy);
606 if (err != OK) {
607 return err;
608 }
609
610 glDeleteTextures(1, &mTexName);
611 }
612
613 mEglDisplay = EGL_NO_DISPLAY;
614 mEglContext = EGL_NO_CONTEXT;
615 mAttached = false;
616
617 return OK;
618}
619
Mathias Agopian3f844832013-08-07 21:24:32 -0700620status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700621 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800622 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700623 Mutex::Autolock lock(mMutex);
624
625 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800626 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700627 return NO_INIT;
628 }
629
630 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800631 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700632 "context");
633 return INVALID_OPERATION;
634 }
635
636 EGLDisplay dpy = eglGetCurrentDisplay();
637 EGLContext ctx = eglGetCurrentContext();
638
639 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800640 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700641 return INVALID_OPERATION;
642 }
643
644 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800645 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700646 return INVALID_OPERATION;
647 }
648
649 // We need to bind the texture regardless of whether there's a current
650 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700651 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700652
Jamie Gennis74bed552012-03-28 19:05:54 -0700653 mEglDisplay = dpy;
654 mEglContext = ctx;
655 mTexName = tex;
656 mAttached = true;
657
Eric Penner5c3d2432014-07-11 19:08:04 -0700658 if (mCurrentTextureImage != NULL) {
659 // This may wait for a buffer a second time. This is likely required if
660 // this is a different context, since otherwise the wait could be skipped
661 // by bouncing through another context. For the same context the extra
662 // wait is redundant.
663 status_t err = bindTextureImageLocked();
664 if (err != NO_ERROR) {
665 return err;
666 }
667 }
668
Jamie Gennis74bed552012-03-28 19:05:54 -0700669 return OK;
670}
671
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800672
Andy McFadden2adaf042012-12-18 09:49:45 -0800673status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800674 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700675
Jamie Gennis01dbf552012-09-06 14:54:19 -0700676 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700677 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700678 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
679 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
680 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800681 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700682 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700683 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700684 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700685 glFlush();
686 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700687 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700688 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800689 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700690 "fd: %#x", eglGetError());
691 return UNKNOWN_ERROR;
692 }
693 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700694 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700695 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700696 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800697 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700698 "%s (%d)", strerror(-err), err);
699 return err;
700 }
Mathias Agopianca088332013-03-28 17:44:13 -0700701 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700702 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
703 if (fence != EGL_NO_SYNC_KHR) {
704 // There is already a fence for the current slot. We need to
705 // wait on that before replacing it with another fence to
706 // ensure that all outstanding buffer accesses have completed
707 // before the producer accesses it.
708 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
709 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800710 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700711 "fence: %#x", eglGetError());
712 return UNKNOWN_ERROR;
713 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800714 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700715 "fence");
716 return TIMED_OUT;
717 }
718 eglDestroySyncKHR(dpy, fence);
719 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700720
Jamie Gennis01dbf552012-09-06 14:54:19 -0700721 // Create a fence for the outstanding accesses in the current
722 // OpenGL ES context.
723 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
724 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800725 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700726 eglGetError());
727 return UNKNOWN_ERROR;
728 }
729 glFlush();
730 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700731 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700732 }
733
734 return OK;
735}
736
Mathias Agopian3f844832013-08-07 21:24:32 -0700737uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700738 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700739}
740
Andy McFadden2adaf042012-12-18 09:49:45 -0800741void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800742 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700743 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
744}
745
Andy McFadden2adaf042012-12-18 09:49:45 -0800746void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700747 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700748 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800749 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700750 return;
751 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700752 bool needsRecompute = mFilteringEnabled != enabled;
753 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700754
Eric Penner5c3d2432014-07-11 19:08:04 -0700755 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800756 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700757 }
758
Eric Penner5c3d2432014-07-11 19:08:04 -0700759 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700760 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700761 }
762}
763
Andy McFadden2adaf042012-12-18 09:49:45 -0800764void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800765 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700766 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
767 nullptr : mCurrentTextureImage->graphicBuffer();
768 if (buf == nullptr) {
769 GLC_LOGD("computeCurrentTransformMatrixLocked: "
770 "mCurrentTextureImage is NULL");
771 }
772 computeTransformMatrix(mCurrentTransformMatrix, buf,
773 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop,
774 mCurrentTransform, mFilteringEnabled);
775}
776
777void GLConsumer::computeTransformMatrix(float outTransform[16],
778 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
779 bool filtering) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800780 // Transform matrices
781 static const mat4 mtxFlipH(
782 -1, 0, 0, 0,
783 0, 1, 0, 0,
784 0, 0, 1, 0,
785 1, 0, 0, 1
786 );
787 static const mat4 mtxFlipV(
788 1, 0, 0, 0,
789 0, -1, 0, 0,
790 0, 0, 1, 0,
791 0, 1, 0, 1
792 );
793 static const mat4 mtxRot90(
794 0, 1, 0, 0,
795 -1, 0, 0, 0,
796 0, 0, 1, 0,
797 1, 0, 0, 1
798 );
Jamie Gennisf238e282011-01-09 16:33:17 -0800799
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800800 mat4 xform;
John Reck1a61da52016-04-28 13:18:15 -0700801 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800802 xform *= mtxFlipH;
Jamie Gennisa214c642011-01-14 13:53:31 -0800803 }
John Reck1a61da52016-04-28 13:18:15 -0700804 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800805 xform *= mtxFlipV;
Jamie Gennisa214c642011-01-14 13:53:31 -0800806 }
John Reck1a61da52016-04-28 13:18:15 -0700807 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800808 xform *= mtxRot90;
Jamie Gennisf238e282011-01-09 16:33:17 -0800809 }
810
John Reck1a61da52016-04-28 13:18:15 -0700811 if (!cropRect.isEmpty()) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700812 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
813 float bufferWidth = buf->getWidth();
814 float bufferHeight = buf->getHeight();
John Reck1a61da52016-04-28 13:18:15 -0700815 float shrinkAmount = 0.0f;
816 if (filtering) {
817 // In order to prevent bilinear sampling beyond the edge of the
818 // crop rectangle we may need to shrink it by 2 texels in each
819 // dimension. Normally this would just need to take 1/2 a texel
820 // off each end, but because the chroma channels of YUV420 images
821 // are subsampled we may need to shrink the crop region by a whole
822 // texel on each side.
823 switch (buf->getPixelFormat()) {
824 case PIXEL_FORMAT_RGBA_8888:
825 case PIXEL_FORMAT_RGBX_8888:
Romain Guyff415142016-12-13 16:51:25 -0800826 case PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -0800827 case PIXEL_FORMAT_RGBA_1010102:
John Reck1a61da52016-04-28 13:18:15 -0700828 case PIXEL_FORMAT_RGB_888:
829 case PIXEL_FORMAT_RGB_565:
830 case PIXEL_FORMAT_BGRA_8888:
831 // We know there's no subsampling of any channels, so we
832 // only need to shrink by a half a pixel.
833 shrinkAmount = 0.5;
834 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700835
John Reck1a61da52016-04-28 13:18:15 -0700836 default:
837 // If we don't recognize the format, we must assume the
838 // worst case (that we care about), which is YUV420.
839 shrinkAmount = 1.0;
840 break;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700841 }
John Reck1a61da52016-04-28 13:18:15 -0700842 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700843
John Reck1a61da52016-04-28 13:18:15 -0700844 // Only shrink the dimensions that are not the size of the buffer.
845 if (cropRect.width() < bufferWidth) {
846 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
847 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
848 bufferWidth;
849 }
850 if (cropRect.height() < bufferHeight) {
851 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
852 bufferHeight;
853 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
854 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700855 }
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800856
857 mat4 crop(
Jamie Gennisdbe92452013-09-23 17:22:10 -0700858 sx, 0, 0, 0,
859 0, sy, 0, 0,
860 0, 0, 1, 0,
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800861 tx, ty, 0, 1
862 );
863 xform = crop * xform;
Jamie Gennisa214c642011-01-14 13:53:31 -0800864 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800865
866 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800867 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800868 // want to expose this to applications, however, so we must add an
869 // additional vertical flip to the transform after all the other transforms.
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800870 xform = mtxFlipV * xform;
871
872 memcpy(outTransform, xform.asArray(), sizeof(xform));
Jamie Gennisf238e282011-01-09 16:33:17 -0800873}
874
Chia-I Wu93b122f2017-12-01 08:51:39 -0800875Rect GLConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
876 Rect outCrop = crop;
877
878 uint32_t newWidth = static_cast<uint32_t>(crop.width());
879 uint32_t newHeight = static_cast<uint32_t>(crop.height());
880
881 if (newWidth * bufferHeight > newHeight * bufferWidth) {
882 newWidth = newHeight * bufferWidth / bufferHeight;
883 ALOGV("too wide: newWidth = %d", newWidth);
884 } else if (newWidth * bufferHeight < newHeight * bufferWidth) {
885 newHeight = newWidth * bufferHeight / bufferWidth;
886 ALOGV("too tall: newHeight = %d", newHeight);
887 }
888
889 uint32_t currentWidth = static_cast<uint32_t>(crop.width());
890 uint32_t currentHeight = static_cast<uint32_t>(crop.height());
891
892 // The crop is too wide
893 if (newWidth < currentWidth) {
894 uint32_t dw = currentWidth - newWidth;
895 auto halfdw = dw / 2;
896 outCrop.left += halfdw;
897 // Not halfdw because it would subtract 1 too few when dw is odd
898 outCrop.right -= (dw - halfdw);
899 // The crop is too tall
900 } else if (newHeight < currentHeight) {
901 uint32_t dh = currentHeight - newHeight;
902 auto halfdh = dh / 2;
903 outCrop.top += halfdh;
904 // Not halfdh because it would subtract 1 too few when dh is odd
905 outCrop.bottom -= (dh - halfdh);
906 }
907
908 ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
909 outCrop.left, outCrop.top,
910 outCrop.right,outCrop.bottom);
911
912 return outCrop;
913}
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
Chia-I Wu06d63de2017-01-04 14:58:51 +0800933sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700934 Mutex::Autolock lock(mMutex);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800935
936 if (outSlot != nullptr) {
937 *outSlot = mCurrentTexture;
938 }
939
940 return (mCurrentTextureImage == nullptr) ?
Eric Penner5c3d2432014-07-11 19:08:04 -0700941 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700942}
943
Andy McFadden2adaf042012-12-18 09:49:45 -0800944Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700945 Mutex::Autolock lock(mMutex);
Chia-I Wu93b122f2017-12-01 08:51:39 -0800946 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
947 ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
948 : mCurrentCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700949}
950
Andy McFadden2adaf042012-12-18 09:49:45 -0800951uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700952 Mutex::Autolock lock(mMutex);
953 return mCurrentTransform;
954}
955
Andy McFadden2adaf042012-12-18 09:49:45 -0800956uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700957 Mutex::Autolock lock(mMutex);
958 return mCurrentScalingMode;
959}
960
Andy McFadden2adaf042012-12-18 09:49:45 -0800961sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700962 Mutex::Autolock lock(mMutex);
963 return mCurrentFence;
964}
965
Brian Anderson3d4039d2016-09-23 16:31:30 -0700966std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const {
967 Mutex::Autolock lock(mMutex);
968 return mCurrentFenceTime;
969}
970
Andy McFadden2adaf042012-12-18 09:49:45 -0800971status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700972
973 EGLDisplay dpy = eglGetCurrentDisplay();
974 EGLContext ctx = eglGetCurrentContext();
975
976 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800977 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700978 return INVALID_OPERATION;
979 }
980
981 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800982 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700983 return INVALID_OPERATION;
984 }
985
Jamie Gennis1df8c342012-12-20 14:05:45 -0800986 if (mCurrentFence->isValid()) {
Jeffrey Kardatzkea45e1c82018-03-13 12:07:28 -0700987 if (SyncFeatures::getInstance().useWaitSync() &&
988 SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700989 // Create an EGLSyncKHR from the current fence.
990 int fenceFd = mCurrentFence->dup();
991 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800992 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700993 return -errno;
994 }
995 EGLint attribs[] = {
996 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
997 EGL_NONE
998 };
999 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
1000 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
1001 if (sync == EGL_NO_SYNC_KHR) {
1002 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -08001003 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001004 eglGetError());
1005 return UNKNOWN_ERROR;
1006 }
1007
1008 // XXX: The spec draft is inconsistent as to whether this should
1009 // return an EGLint or void. Ignore the return value for now, as
1010 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -07001011 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001012 EGLint eglErr = eglGetError();
1013 eglDestroySyncKHR(dpy, sync);
1014 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -08001015 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001016 eglErr);
1017 return UNKNOWN_ERROR;
1018 }
1019 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001020 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001021 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001022 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -08001023 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001024 return err;
1025 }
1026 }
1027 }
1028
1029 return NO_ERROR;
1030}
1031
Andy McFadden2adaf042012-12-18 09:49:45 -08001032void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001033 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001034 if (slotIndex == mCurrentTexture) {
1035 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1036 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001037 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001038 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001039}
Daniel Lameae59d22012-01-22 15:26:27 -08001040
Andy McFadden2adaf042012-12-18 09:49:45 -08001041void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001042 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001043 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001044 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001045}
1046
Chia-I Wue2786ea2017-08-07 10:36:08 -07001047status_t GLConsumer::setConsumerUsageBits(uint64_t usage) {
Chia-I Wua81bc492017-11-27 10:16:00 -08001048 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001049}
1050
Mathias Agopian74d211a2013-04-22 16:55:35 +02001051void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001052{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001053 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001054 "%smTexName=%d mCurrentTexture=%d\n"
1055 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1056 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1057 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1058 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001059
Mathias Agopian74d211a2013-04-22 16:55:35 +02001060 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001061}
1062
Eric Penner5c3d2432014-07-11 19:08:04 -07001063GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1064 mGraphicBuffer(graphicBuffer),
1065 mEglImage(EGL_NO_IMAGE_KHR),
Pablo Ceballos60d69222015-08-07 14:47:20 -07001066 mEglDisplay(EGL_NO_DISPLAY),
1067 mCropRect(Rect::EMPTY_RECT) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001068}
1069
1070GLConsumer::EglImage::~EglImage() {
1071 if (mEglImage != EGL_NO_IMAGE_KHR) {
1072 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1073 ALOGE("~EglImage: eglDestroyImageKHR failed");
1074 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001075 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001076 }
1077}
1078
1079status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001080 const Rect& cropRect,
1081 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001082 // If there's an image and it's no longer valid, destroy it.
1083 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1084 bool displayInvalid = mEglDisplay != eglDisplay;
1085 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001086 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001087 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1088 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1089 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001090 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001091 mEglImage = EGL_NO_IMAGE_KHR;
1092 mEglDisplay = EGL_NO_DISPLAY;
1093 }
1094
1095 // If there's no image, create one.
1096 if (mEglImage == EGL_NO_IMAGE_KHR) {
1097 mEglDisplay = eglDisplay;
1098 mCropRect = cropRect;
1099 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1100 }
1101
1102 // Fail if we can't create a valid image.
1103 if (mEglImage == EGL_NO_IMAGE_KHR) {
1104 mEglDisplay = EGL_NO_DISPLAY;
1105 mCropRect.makeInvalid();
1106 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001107 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -07001108 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1109 buffer->getUsage(), buffer->getPixelFormat());
1110 return UNKNOWN_ERROR;
1111 }
1112
1113 return OK;
1114}
1115
1116void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001117 glEGLImageTargetTexture2DOES(texTarget,
1118 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001119}
1120
1121EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1122 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001123 EGLClientBuffer cbuf =
1124 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -07001125 const bool createProtectedImage =
1126 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
1127 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -07001128 EGLint attrs[] = {
1129 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1130 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1131 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1132 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1133 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
Craig Donneraec86972016-04-28 18:09:40 -07001134 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1135 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001136 EGL_NONE,
1137 };
1138 if (!crop.isValid()) {
Craig Donnera94d9402016-10-19 17:18:17 -07001139 // No crop rect to set, so leave the crop out of the attrib array. Make
1140 // sure to propagate the protected content attrs if they are set.
1141 attrs[2] = attrs[10];
1142 attrs[3] = attrs[11];
1143 attrs[4] = EGL_NONE;
Eric Penner5c3d2432014-07-11 19:08:04 -07001144 } else if (!isEglImageCroppable(crop)) {
1145 // The crop rect is not at the origin, so we can't set the crop on the
1146 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1147 // extension. In the future we can add a layered extension that
1148 // removes this restriction if there is hardware that can support it.
Craig Donnera94d9402016-10-19 17:18:17 -07001149 attrs[2] = attrs[10];
1150 attrs[3] = attrs[11];
1151 attrs[4] = EGL_NONE;
Eric Penner5c3d2432014-07-11 19:08:04 -07001152 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001153 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001154 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1155 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1156 if (image == EGL_NO_IMAGE_KHR) {
1157 EGLint error = eglGetError();
1158 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001159 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001160 }
1161 return image;
1162}
1163
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001164}; // namespace android