blob: d256ae5389db31f4bc74b549153a2cb03ddd31f7 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andy McFadden2adaf042012-12-18 09:49:45 -080017#define LOG_TAG "GLConsumer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070028#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029
Jamie Gennis9fea3422012-08-07 18:03:04 -070030#include <hardware/hardware.h>
31
Dan Stozadd264162015-03-12 13:58:47 -070032#include <gui/BufferItem.h>
Mathias Agopianca088332013-03-28 17:44:13 -070033#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/IGraphicBufferAlloc.h>
35#include <gui/ISurfaceComposer.h>
36#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080037
Mathias Agopian90ac7992012-02-25 18:48:35 -080038#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070039#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080040
41#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070042#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044
Jamie Gennisdbe92452013-09-23 17:22:10 -070045EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
46#define CROP_EXT_STR "EGL_ANDROID_image_crop"
47
Andy McFadden97eba892012-12-11 15:21:45 -080048namespace android {
49
Andy McFadden2adaf042012-12-18 09:49:45 -080050// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080051#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
53//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070056
Mathias Agopianad678e12013-07-23 17:28:53 -070057static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080058 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070059 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070060} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080061 "_______________"
62 "_______________"
63 "_____XX_XX_____"
64 "__X_X_____X_X__"
65 "__X_XXXXXXX_X__"
66 "__XXXXXXXXXXX__"
67 "___XX_XXX_XX___"
68 "____XXXXXXX____"
69 "_____X___X_____"
70 "____X_____X____"
71 "_______________"
72 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070073};
Mathias Agopianad678e12013-07-23 17:28:53 -070074
Jamie Gennisf238e282011-01-09 16:33:17 -080075// Transform matrices
76static float mtxIdentity[16] = {
77 1, 0, 0, 0,
78 0, 1, 0, 0,
79 0, 0, 1, 0,
80 0, 0, 0, 1,
81};
82static float mtxFlipH[16] = {
83 -1, 0, 0, 0,
84 0, 1, 0, 0,
85 0, 0, 1, 0,
86 1, 0, 0, 1,
87};
88static float mtxFlipV[16] = {
89 1, 0, 0, 0,
90 0, -1, 0, 0,
91 0, 0, 1, 0,
92 0, 1, 0, 1,
93};
94static float mtxRot90[16] = {
95 0, 1, 0, 0,
96 -1, 0, 0, 0,
97 0, 0, 1, 0,
98 1, 0, 0, 1,
99};
Jamie Gennisf238e282011-01-09 16:33:17 -0800100
101static void mtxMul(float out[16], const float a[16], const float b[16]);
102
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700103Mutex GLConsumer::sStaticInitLock;
104sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -0700105
Jamie Gennisdbe92452013-09-23 17:22:10 -0700106static bool hasEglAndroidImageCropImpl() {
107 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
108 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
109 size_t cropExtLen = strlen(CROP_EXT_STR);
110 size_t extsLen = strlen(exts);
111 bool equal = !strcmp(CROP_EXT_STR, exts);
112 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
113 bool atEnd = (cropExtLen+1) < extsLen &&
114 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
115 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
116 return equal || atStart || atEnd || inMiddle;
117}
118
119static bool hasEglAndroidImageCrop() {
120 // Only compute whether the extension is present once the first time this
121 // function is called.
122 static bool hasIt = hasEglAndroidImageCropImpl();
123 return hasIt;
124}
125
126static bool isEglImageCroppable(const Rect& crop) {
127 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
128}
129
Mathias Agopian3f844832013-08-07 21:24:32 -0700130GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
131 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700132 ConsumerBase(bq, isControlledByApp),
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),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700137 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200138 mDefaultWidth(1),
139 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700140 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700141 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800142 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800143 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700144 mEglDisplay(EGL_NO_DISPLAY),
145 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700146 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
147 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800148{
Dan Stozad723bd72014-11-18 10:24:03 -0800149 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800150
Jamie Gennisfa28c352011-09-16 17:30:26 -0700151 memcpy(mCurrentTransformMatrix, mtxIdentity,
152 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700153
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700154 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800155}
156
Dan Stozaab574912014-06-25 14:21:45 -0700157GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
158 bool useFenceSync, bool isControlledByApp) :
159 ConsumerBase(bq, isControlledByApp),
160 mCurrentTransform(0),
161 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
162 mCurrentFence(Fence::NO_FENCE),
163 mCurrentTimestamp(0),
164 mCurrentFrameNumber(0),
165 mDefaultWidth(1),
166 mDefaultHeight(1),
167 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800168 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700169 mUseFenceSync(useFenceSync),
170 mTexTarget(texTarget),
171 mEglDisplay(EGL_NO_DISPLAY),
172 mEglContext(EGL_NO_CONTEXT),
173 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
174 mAttached(false)
175{
Dan Stozad723bd72014-11-18 10:24:03 -0800176 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700177
178 memcpy(mCurrentTransformMatrix, mtxIdentity,
179 sizeof(mCurrentTransformMatrix));
180
181 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
182}
183
Andy McFadden2adaf042012-12-18 09:49:45 -0800184status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700185 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700186 return mConsumer->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700187}
188
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189
Andy McFadden2adaf042012-12-18 09:49:45 -0800190status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700191{
Daniel Lamb2675792012-02-23 14:35:13 -0800192 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700193 mDefaultWidth = w;
194 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700195 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700196}
197
Andy McFadden2adaf042012-12-18 09:49:45 -0800198status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800199 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800200 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800201 Mutex::Autolock lock(mMutex);
202
203 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800204 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800205 return NO_INIT;
206 }
207
208 // Make sure the EGL state is the same as in previous calls.
209 status_t err = checkAndUpdateEglStateLocked();
210 if (err != NO_ERROR) {
211 return err;
212 }
213
Dan Stozadd264162015-03-12 13:58:47 -0700214 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800215
216 // Acquire the next buffer.
217 // In asynchronous mode the list is guaranteed to be one buffer
218 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700219 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800220 if (err != NO_ERROR) {
221 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
222 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800223 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800224 glBindTexture(mTexTarget, mTexName);
225 err = NO_ERROR;
226 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800227 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800228 strerror(-err), err);
229 }
230 return err;
231 }
232
233 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700234 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800235 if (err != NO_ERROR) {
236 // We always bind the texture.
237 glBindTexture(mTexTarget, mTexName);
238 return err;
239 }
240
Andy McFadden97eba892012-12-11 15:21:45 -0800241 // Bind the new buffer to the GL texture, and wait until it's ready.
242 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700243}
244
Mathias Agopianad678e12013-07-23 17:28:53 -0700245
246status_t GLConsumer::releaseTexImage() {
247 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800248 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700249 Mutex::Autolock lock(mMutex);
250
251 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800252 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700253 return NO_INIT;
254 }
255
256 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700257 status_t err = NO_ERROR;
258
259 if (mAttached) {
260 err = checkAndUpdateEglStateLocked(true);
261 if (err != NO_ERROR) {
262 return err;
263 }
264 } else {
265 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700266 }
267
268 // Update the GLConsumer state.
269 int buf = mCurrentTexture;
270 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
271
Dan Stozad723bd72014-11-18 10:24:03 -0800272 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700273
Mathias Agopian45155962013-08-08 18:16:21 -0700274 if (mAttached) {
275 // Do whatever sync ops we need to do before releasing the slot.
276 err = syncForReleaseLocked(mEglDisplay);
277 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800278 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700279 return err;
280 }
281 } else {
282 // if we're detached, we just use the fence that was created in detachFromContext()
283 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700284 }
285
Mathias Agopian45155962013-08-08 18:16:21 -0700286 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700287 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800288 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700289 strerror(-err), err);
290 return err;
291 }
292
Eric Penner5c3d2432014-07-11 19:08:04 -0700293 if (mReleasedTexImage == NULL) {
294 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
295 }
296
Mathias Agopianad678e12013-07-23 17:28:53 -0700297 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700298 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700299 mCurrentCrop.makeInvalid();
300 mCurrentTransform = 0;
301 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
302 mCurrentTimestamp = 0;
303 mCurrentFence = Fence::NO_FENCE;
304
Mathias Agopian45155962013-08-08 18:16:21 -0700305 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700306 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800307 status_t result = bindTextureImageLocked();
308 if (result != NO_ERROR) {
309 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700310 }
Mathias Agopian45155962013-08-08 18:16:21 -0700311 } else {
312 // detached, don't touch the texture (and we may not even have an
313 // EGLDisplay here.
314 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700315 }
316
317 return NO_ERROR;
318}
319
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700320sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
321 Mutex::Autolock _l(sStaticInitLock);
322 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
323 // The first time, create the debug texture in case the application
324 // continues to use it.
325 sp<GraphicBuffer> buffer = new GraphicBuffer(
326 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
327 GraphicBuffer::USAGE_SW_WRITE_RARELY);
328 uint32_t* bits;
329 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800330 uint32_t stride = buffer->getStride();
331 uint32_t height = buffer->getHeight();
332 memset(bits, 0, stride * height * 4);
333 for (uint32_t y = 0; y < kDebugData.height; y++) {
334 for (uint32_t x = 0; x < kDebugData.width; x++) {
335 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
336 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700337 }
Dan Stozad723bd72014-11-18 10:24:03 -0800338 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700339 }
340 buffer->unlock();
341 sReleasedTexImageBuffer = buffer;
342 }
343 return sReleasedTexImageBuffer;
344}
345
Dan Stozadd264162015-03-12 13:58:47 -0700346status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700347 nsecs_t presentWhen) {
348 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700349 if (err != NO_ERROR) {
350 return err;
351 }
352
Eric Penner5c3d2432014-07-11 19:08:04 -0700353 // If item->mGraphicBuffer is not null, this buffer has not been acquired
354 // before, so any prior EglImage created is using a stale buffer. This
355 // replaces any old EglImage with a new one (using the new buffer).
356 if (item->mGraphicBuffer != NULL) {
357 int slot = item->mBuf;
358 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700359 }
360
Jamie Gennis9fea3422012-08-07 18:03:04 -0700361 return NO_ERROR;
362}
363
Dan Stozadd264162015-03-12 13:58:47 -0700364status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *outItem,
365 nsecs_t presentWhen) {
366 BufferItem item;
367 status_t result = acquireBufferLocked(&item, presentWhen);
368 if (result != NO_ERROR) {
369 return result;
370 }
371 *outItem = item;
372 return NO_ERROR;
373}
374
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700375status_t GLConsumer::releaseBufferLocked(int buf,
376 sp<GraphicBuffer> graphicBuffer,
377 EGLDisplay display, EGLSyncKHR eglFence) {
378 // release the buffer if it hasn't already been discarded by the
379 // BufferQueue. This can happen, for example, when the producer of this
380 // buffer has reallocated the original buffer slot after this buffer
381 // was acquired.
382 status_t err = ConsumerBase::releaseBufferLocked(
383 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700384 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700385 return err;
386}
387
Mathias Agopianad678e12013-07-23 17:28:53 -0700388status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800389{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700390 status_t err = NO_ERROR;
391
Andy McFadden87a67842014-04-04 15:37:08 -0700392 int buf = item.mBuf;
393
Jamie Gennis74bed552012-03-28 19:05:54 -0700394 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800395 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700396 "ES context");
Andy McFadden87a67842014-04-04 15:37:08 -0700397 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
398 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700399 return INVALID_OPERATION;
400 }
401
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800402 // Confirm state.
403 err = checkAndUpdateEglStateLocked();
404 if (err != NO_ERROR) {
Andy McFadden87a67842014-04-04 15:37:08 -0700405 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
406 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800407 return err;
408 }
409
Eric Penner5c3d2432014-07-11 19:08:04 -0700410 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
411 // if nessessary, for the gralloc buffer currently in the slot in
412 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800413 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700414 // means the buffer was previously acquired).
415 err = mEglSlots[buf].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
416 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800417 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700418 mEglDisplay, buf);
419 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
420 mEglDisplay, EGL_NO_SYNC_KHR);
421 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800422 }
423
424 // Do whatever sync ops we need to do before releasing the old slot.
425 err = syncForReleaseLocked(mEglDisplay);
426 if (err != NO_ERROR) {
427 // Release the buffer we just acquired. It's not safe to
428 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700429 // As we are still under lock since acquireBuffer, it is safe to
430 // release by slot.
431 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
432 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800433 return err;
434 }
435
Dan Stozad723bd72014-11-18 10:24:03 -0800436 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700437 mCurrentTexture, mCurrentTextureImage != NULL ?
438 mCurrentTextureImage->graphicBufferHandle() : 0,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800439 buf, mSlots[buf].mGraphicBuffer->handle);
440
441 // release old buffer
442 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700443 status_t status = releaseBufferLocked(
Eric Penner5c3d2432014-07-11 19:08:04 -0700444 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
445 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700446 if (status < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800447 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800448 strerror(-status), status);
449 err = status;
450 // keep going, with error raised [?]
451 }
452 }
453
Andy McFadden2adaf042012-12-18 09:49:45 -0800454 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800455 mCurrentTexture = buf;
Eric Penner5c3d2432014-07-11 19:08:04 -0700456 mCurrentTextureImage = mEglSlots[buf].mEglImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800457 mCurrentCrop = item.mCrop;
458 mCurrentTransform = item.mTransform;
459 mCurrentScalingMode = item.mScalingMode;
460 mCurrentTimestamp = item.mTimestamp;
461 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700462 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800463
464 computeCurrentTransformMatrixLocked();
465
466 return err;
467}
468
Andy McFadden2adaf042012-12-18 09:49:45 -0800469status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800470 if (mEglDisplay == EGL_NO_DISPLAY) {
471 ALOGE("bindTextureImage: invalid display");
472 return INVALID_OPERATION;
473 }
474
Dan Stozad723bd72014-11-18 10:24:03 -0800475 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800476 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800477 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800478 }
479
480 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700481 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
482 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800483 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700484 return NO_INIT;
485 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800486
Eric Penner5c3d2432014-07-11 19:08:04 -0700487 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700488 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700489 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800490 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700491 mEglDisplay, mCurrentTexture);
492 return UNKNOWN_ERROR;
493 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700494 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
495
Eric Penner2d14a0e2014-08-25 20:16:37 -0700496 // In the rare case that the display is terminated and then initialized
497 // again, we can't detect that the display changed (it didn't), but the
498 // image is invalid. In this case, repeat the exact same steps while
499 // forcing the creation of a new image.
500 if ((error = glGetError()) != GL_NO_ERROR) {
501 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800502 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
503 mCurrentCrop,
504 true);
505 if (result != NO_ERROR) {
506 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700507 mEglDisplay, mCurrentTexture);
508 return UNKNOWN_ERROR;
509 }
510 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
511 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800512 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700513 return UNKNOWN_ERROR;
514 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800515 }
Andy McFadden97eba892012-12-11 15:21:45 -0800516
517 // Wait for the new buffer to be ready.
518 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800519}
520
Mathias Agopian45155962013-08-08 18:16:21 -0700521status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700522 EGLDisplay dpy = eglGetCurrentDisplay();
523 EGLContext ctx = eglGetCurrentContext();
524
Mathias Agopian45155962013-08-08 18:16:21 -0700525 if (!contextCheck) {
526 // if this is the first time we're called, mEglDisplay/mEglContext have
527 // never been set, so don't error out (below).
528 if (mEglDisplay == EGL_NO_DISPLAY) {
529 mEglDisplay = dpy;
530 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700531 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700532 mEglContext = ctx;
533 }
534 }
535
536 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800537 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700538 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700539 }
540
Mathias Agopian45155962013-08-08 18:16:21 -0700541 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800542 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700543 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700544 }
545
546 mEglDisplay = dpy;
547 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800548 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800549}
550
Jesse Hall13f01cb2013-03-20 11:37:21 -0700551void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
552 if (fence->isValid() &&
553 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700554 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700555 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700556 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800557 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700558 strerror(-err), err);
559 }
Jesse Hallef194142012-06-14 14:45:17 -0700560 }
561}
562
Andy McFadden2adaf042012-12-18 09:49:45 -0800563status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700564 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800565 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700566 Mutex::Autolock lock(mMutex);
567
568 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800569 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700570 return NO_INIT;
571 }
572
573 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800574 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700575 "context");
576 return INVALID_OPERATION;
577 }
578
579 EGLDisplay dpy = eglGetCurrentDisplay();
580 EGLContext ctx = eglGetCurrentContext();
581
582 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800583 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700584 return INVALID_OPERATION;
585 }
586
587 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800588 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700589 return INVALID_OPERATION;
590 }
591
592 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
593 status_t err = syncForReleaseLocked(dpy);
594 if (err != OK) {
595 return err;
596 }
597
598 glDeleteTextures(1, &mTexName);
599 }
600
601 mEglDisplay = EGL_NO_DISPLAY;
602 mEglContext = EGL_NO_CONTEXT;
603 mAttached = false;
604
605 return OK;
606}
607
Mathias Agopian3f844832013-08-07 21:24:32 -0700608status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700609 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800610 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700611 Mutex::Autolock lock(mMutex);
612
613 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800614 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700615 return NO_INIT;
616 }
617
618 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800619 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700620 "context");
621 return INVALID_OPERATION;
622 }
623
624 EGLDisplay dpy = eglGetCurrentDisplay();
625 EGLContext ctx = eglGetCurrentContext();
626
627 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800628 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700629 return INVALID_OPERATION;
630 }
631
632 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800633 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700634 return INVALID_OPERATION;
635 }
636
637 // We need to bind the texture regardless of whether there's a current
638 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700639 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700640
Jamie Gennis74bed552012-03-28 19:05:54 -0700641 mEglDisplay = dpy;
642 mEglContext = ctx;
643 mTexName = tex;
644 mAttached = true;
645
Eric Penner5c3d2432014-07-11 19:08:04 -0700646 if (mCurrentTextureImage != NULL) {
647 // This may wait for a buffer a second time. This is likely required if
648 // this is a different context, since otherwise the wait could be skipped
649 // by bouncing through another context. For the same context the extra
650 // wait is redundant.
651 status_t err = bindTextureImageLocked();
652 if (err != NO_ERROR) {
653 return err;
654 }
655 }
656
Jamie Gennis74bed552012-03-28 19:05:54 -0700657 return OK;
658}
659
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800660
Andy McFadden2adaf042012-12-18 09:49:45 -0800661status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800662 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700663
Jamie Gennis01dbf552012-09-06 14:54:19 -0700664 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700665 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700666 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
667 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
668 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800669 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700670 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700671 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700672 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700673 glFlush();
674 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700675 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700676 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800677 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700678 "fd: %#x", eglGetError());
679 return UNKNOWN_ERROR;
680 }
681 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700682 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700683 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700684 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800685 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700686 "%s (%d)", strerror(-err), err);
687 return err;
688 }
Mathias Agopianca088332013-03-28 17:44:13 -0700689 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700690 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
691 if (fence != EGL_NO_SYNC_KHR) {
692 // There is already a fence for the current slot. We need to
693 // wait on that before replacing it with another fence to
694 // ensure that all outstanding buffer accesses have completed
695 // before the producer accesses it.
696 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
697 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800698 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700699 "fence: %#x", eglGetError());
700 return UNKNOWN_ERROR;
701 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800702 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700703 "fence");
704 return TIMED_OUT;
705 }
706 eglDestroySyncKHR(dpy, fence);
707 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700708
Jamie Gennis01dbf552012-09-06 14:54:19 -0700709 // Create a fence for the outstanding accesses in the current
710 // OpenGL ES context.
711 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
712 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800713 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700714 eglGetError());
715 return UNKNOWN_ERROR;
716 }
717 glFlush();
718 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700719 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700720 }
721
722 return OK;
723}
724
Dan Stozad723bd72014-11-18 10:24:03 -0800725bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700726{
727 switch (format) {
728 // supported YUV formats
729 case HAL_PIXEL_FORMAT_YV12:
730 // Legacy/deprecated YUV formats
731 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
732 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
733 case HAL_PIXEL_FORMAT_YCbCr_422_I:
734 return true;
735 }
736
737 // Any OEM format needs to be considered
738 if (format>=0x100 && format<=0x1FF)
739 return true;
740
741 return false;
742}
743
Mathias Agopian3f844832013-08-07 21:24:32 -0700744uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700745 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700746}
747
Andy McFadden2adaf042012-12-18 09:49:45 -0800748void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800749 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700750 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
751}
752
Andy McFadden2adaf042012-12-18 09:49:45 -0800753void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700754 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700755 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800756 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700757 return;
758 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700759 bool needsRecompute = mFilteringEnabled != enabled;
760 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700761
Eric Penner5c3d2432014-07-11 19:08:04 -0700762 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800763 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700764 }
765
Eric Penner5c3d2432014-07-11 19:08:04 -0700766 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700767 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700768 }
769}
770
Andy McFadden2adaf042012-12-18 09:49:45 -0800771void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800772 GLC_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800773
Jamie Gennisa214c642011-01-14 13:53:31 -0800774 float xform[16];
775 for (int i = 0; i < 16; i++) {
776 xform[i] = mtxIdentity[i];
777 }
778 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
779 float result[16];
780 mtxMul(result, xform, mtxFlipH);
781 for (int i = 0; i < 16; i++) {
782 xform[i] = result[i];
783 }
784 }
785 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
786 float result[16];
787 mtxMul(result, xform, mtxFlipV);
788 for (int i = 0; i < 16; i++) {
789 xform[i] = result[i];
790 }
791 }
792 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
793 float result[16];
794 mtxMul(result, xform, mtxRot90);
795 for (int i = 0; i < 16; i++) {
796 xform[i] = result[i];
797 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800798 }
799
Eric Penner5c3d2432014-07-11 19:08:04 -0700800 sp<GraphicBuffer> buf = (mCurrentTextureImage == NULL) ?
801 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700802
803 if (buf == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800804 GLC_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureImage is NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700805 }
806
Jamie Gennisdbe92452013-09-23 17:22:10 -0700807 float mtxBeforeFlipV[16];
808 if (!isEglImageCroppable(mCurrentCrop)) {
809 Rect cropRect = mCurrentCrop;
810 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
811 float bufferWidth = buf->getWidth();
812 float bufferHeight = buf->getHeight();
813 if (!cropRect.isEmpty()) {
814 float shrinkAmount = 0.0f;
815 if (mFilteringEnabled) {
816 // In order to prevent bilinear sampling beyond the edge of the
817 // crop rectangle we may need to shrink it by 2 texels in each
818 // dimension. Normally this would just need to take 1/2 a texel
819 // off each end, but because the chroma channels of YUV420 images
820 // are subsampled we may need to shrink the crop region by a whole
821 // texel on each side.
822 switch (buf->getPixelFormat()) {
823 case PIXEL_FORMAT_RGBA_8888:
824 case PIXEL_FORMAT_RGBX_8888:
825 case PIXEL_FORMAT_RGB_888:
826 case PIXEL_FORMAT_RGB_565:
827 case PIXEL_FORMAT_BGRA_8888:
828 // We know there's no subsampling of any channels, so we
829 // only need to shrink by a half a pixel.
830 shrinkAmount = 0.5;
831 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700832
Jamie Gennisdbe92452013-09-23 17:22:10 -0700833 default:
834 // If we don't recognize the format, we must assume the
835 // worst case (that we care about), which is YUV420.
836 shrinkAmount = 1.0;
837 break;
838 }
839 }
840
841 // Only shrink the dimensions that are not the size of the buffer.
842 if (cropRect.width() < bufferWidth) {
843 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
844 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
845 bufferWidth;
846 }
847 if (cropRect.height() < bufferHeight) {
848 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
849 bufferHeight;
850 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
851 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700852 }
853 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700854 float crop[16] = {
855 sx, 0, 0, 0,
856 0, sy, 0, 0,
857 0, 0, 1, 0,
858 tx, ty, 0, 1,
859 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700860
Jamie Gennisdbe92452013-09-23 17:22:10 -0700861 mtxMul(mtxBeforeFlipV, crop, xform);
862 } else {
863 for (int i = 0; i < 16; i++) {
864 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700865 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800866 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800867
868 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800869 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800870 // want to expose this to applications, however, so we must add an
871 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700872 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800873}
874
Andy McFadden2adaf042012-12-18 09:49:45 -0800875nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800876 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800877 Mutex::Autolock lock(mMutex);
878 return mCurrentTimestamp;
879}
880
Dan Stozad723bd72014-11-18 10:24:03 -0800881uint64_t GLConsumer::getFrameNumber() {
882 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700883 Mutex::Autolock lock(mMutex);
884 return mCurrentFrameNumber;
885}
886
Andy McFadden2adaf042012-12-18 09:49:45 -0800887sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700888 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700889 return (mCurrentTextureImage == NULL) ?
890 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700891}
892
Andy McFadden2adaf042012-12-18 09:49:45 -0800893Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700894 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700895
896 Rect outCrop = mCurrentCrop;
897 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800898 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
899 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700900
901 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
902 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800903 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700904 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
905 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800906 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700907 }
908
Dan Stozad723bd72014-11-18 10:24:03 -0800909 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
910 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
911
Daniel Lam016c8cb2012-04-03 15:54:58 -0700912 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800913 if (newWidth < currentWidth) {
Dan Stozaabf952c2015-03-04 14:58:02 -0800914 uint32_t dw = (currentWidth - newWidth) / 2;
915 outCrop.left += dw;
916 outCrop.right -= dw;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700917 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800918 } else if (newHeight < currentHeight) {
Dan Stozaabf952c2015-03-04 14:58:02 -0800919 uint32_t dh = (currentHeight - newHeight) / 2;
920 outCrop.top += dh;
921 outCrop.bottom -= dh;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700922 }
923
Dan Stozad723bd72014-11-18 10:24:03 -0800924 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700925 outCrop.left, outCrop.top,
926 outCrop.right,outCrop.bottom);
927 }
928
Daniel Lam016c8cb2012-04-03 15:54:58 -0700929 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700930}
931
Andy McFadden2adaf042012-12-18 09:49:45 -0800932uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700933 Mutex::Autolock lock(mMutex);
934 return mCurrentTransform;
935}
936
Andy McFadden2adaf042012-12-18 09:49:45 -0800937uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700938 Mutex::Autolock lock(mMutex);
939 return mCurrentScalingMode;
940}
941
Andy McFadden2adaf042012-12-18 09:49:45 -0800942sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700943 Mutex::Autolock lock(mMutex);
944 return mCurrentFence;
945}
946
Andy McFadden2adaf042012-12-18 09:49:45 -0800947status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700948 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700949 return doGLFenceWaitLocked();
950}
951
Andy McFadden2adaf042012-12-18 09:49:45 -0800952status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700953
954 EGLDisplay dpy = eglGetCurrentDisplay();
955 EGLContext ctx = eglGetCurrentContext();
956
957 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800958 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700959 return INVALID_OPERATION;
960 }
961
962 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800963 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700964 return INVALID_OPERATION;
965 }
966
Jamie Gennis1df8c342012-12-20 14:05:45 -0800967 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700968 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700969 // Create an EGLSyncKHR from the current fence.
970 int fenceFd = mCurrentFence->dup();
971 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800972 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700973 return -errno;
974 }
975 EGLint attribs[] = {
976 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
977 EGL_NONE
978 };
979 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
980 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
981 if (sync == EGL_NO_SYNC_KHR) {
982 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800983 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700984 eglGetError());
985 return UNKNOWN_ERROR;
986 }
987
988 // XXX: The spec draft is inconsistent as to whether this should
989 // return an EGLint or void. Ignore the return value for now, as
990 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700991 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700992 EGLint eglErr = eglGetError();
993 eglDestroySyncKHR(dpy, sync);
994 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800995 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700996 eglErr);
997 return UNKNOWN_ERROR;
998 }
999 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001000 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001001 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001002 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -08001003 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001004 return err;
1005 }
1006 }
1007 }
1008
1009 return NO_ERROR;
1010}
1011
Andy McFadden2adaf042012-12-18 09:49:45 -08001012void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001013 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001014 if (slotIndex == mCurrentTexture) {
1015 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1016 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001017 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001018 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001019}
Daniel Lameae59d22012-01-22 15:26:27 -08001020
Andy McFadden2adaf042012-12-18 09:49:45 -08001021void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001022 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001023 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001024 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001025}
1026
Andy McFadden2adaf042012-12-18 09:49:45 -08001027void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001028 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001029 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001030 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001031}
1032
Dan Stozad723bd72014-11-18 10:24:03 -08001033status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001034 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001035 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001036}
1037
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001038status_t GLConsumer::setDefaultBufferDataSpace(
1039 android_dataspace defaultDataSpace) {
1040 Mutex::Autolock lock(mMutex);
1041 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1042}
1043
Andy McFadden2adaf042012-12-18 09:49:45 -08001044status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001045 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001046 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001047 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001048}
1049
Andy McFadden2adaf042012-12-18 09:49:45 -08001050status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001051 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001052 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001053}
1054
Mathias Agopian74d211a2013-04-22 16:55:35 +02001055void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001056{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001057 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001058 "%smTexName=%d mCurrentTexture=%d\n"
1059 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1060 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1061 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1062 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001063
Mathias Agopian74d211a2013-04-22 16:55:35 +02001064 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001065}
1066
Jamie Gennisf238e282011-01-09 16:33:17 -08001067static void mtxMul(float out[16], const float a[16], const float b[16]) {
1068 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1069 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1070 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1071 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1072
1073 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1074 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1075 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1076 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1077
1078 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1079 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1080 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1081 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1082
1083 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1084 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1085 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1086 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1087}
1088
Eric Penner5c3d2432014-07-11 19:08:04 -07001089GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1090 mGraphicBuffer(graphicBuffer),
1091 mEglImage(EGL_NO_IMAGE_KHR),
1092 mEglDisplay(EGL_NO_DISPLAY) {
1093}
1094
1095GLConsumer::EglImage::~EglImage() {
1096 if (mEglImage != EGL_NO_IMAGE_KHR) {
1097 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1098 ALOGE("~EglImage: eglDestroyImageKHR failed");
1099 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001100 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001101 }
1102}
1103
1104status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001105 const Rect& cropRect,
1106 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001107 // If there's an image and it's no longer valid, destroy it.
1108 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1109 bool displayInvalid = mEglDisplay != eglDisplay;
1110 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001111 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001112 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1113 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1114 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001115 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001116 mEglImage = EGL_NO_IMAGE_KHR;
1117 mEglDisplay = EGL_NO_DISPLAY;
1118 }
1119
1120 // If there's no image, create one.
1121 if (mEglImage == EGL_NO_IMAGE_KHR) {
1122 mEglDisplay = eglDisplay;
1123 mCropRect = cropRect;
1124 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1125 }
1126
1127 // Fail if we can't create a valid image.
1128 if (mEglImage == EGL_NO_IMAGE_KHR) {
1129 mEglDisplay = EGL_NO_DISPLAY;
1130 mCropRect.makeInvalid();
1131 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1132 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1133 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1134 buffer->getUsage(), buffer->getPixelFormat());
1135 return UNKNOWN_ERROR;
1136 }
1137
1138 return OK;
1139}
1140
1141void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001142 glEGLImageTargetTexture2DOES(texTarget,
1143 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001144}
1145
1146EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1147 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001148 EGLClientBuffer cbuf =
1149 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Eric Penner5c3d2432014-07-11 19:08:04 -07001150 EGLint attrs[] = {
1151 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1152 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1153 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1154 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1155 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
1156 EGL_NONE,
1157 };
1158 if (!crop.isValid()) {
1159 // No crop rect to set, so terminate the attrib array before the crop.
1160 attrs[2] = EGL_NONE;
1161 } else if (!isEglImageCroppable(crop)) {
1162 // The crop rect is not at the origin, so we can't set the crop on the
1163 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1164 // extension. In the future we can add a layered extension that
1165 // removes this restriction if there is hardware that can support it.
1166 attrs[2] = EGL_NONE;
1167 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001168 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001169 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1170 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1171 if (image == EGL_NO_IMAGE_KHR) {
1172 EGLint error = eglGetError();
1173 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001174 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001175 }
1176 return image;
1177}
1178
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001179}; // namespace android