blob: 065345c2b8c7744a7942bdb1b8594cec780b862f [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
Mathias Agopianca088332013-03-28 17:44:13 -070032#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/IGraphicBufferAlloc.h>
34#include <gui/ISurfaceComposer.h>
35#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080036
Mathias Agopian90ac7992012-02-25 18:48:35 -080037#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070038#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080039
40#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070041#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080042#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080043
Jamie Gennisdbe92452013-09-23 17:22:10 -070044EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
45#define CROP_EXT_STR "EGL_ANDROID_image_crop"
46
Andy McFadden97eba892012-12-11 15:21:45 -080047namespace android {
48
Andy McFadden2adaf042012-12-18 09:49:45 -080049// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080050#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
51#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
52//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
53#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070055
Mathias Agopianad678e12013-07-23 17:28:53 -070056static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080057 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070058 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070059} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080060 "_______________"
61 "_______________"
62 "_____XX_XX_____"
63 "__X_X_____X_X__"
64 "__X_XXXXXXX_X__"
65 "__XXXXXXXXXXX__"
66 "___XX_XXX_XX___"
67 "____XXXXXXX____"
68 "_____X___X_____"
69 "____X_____X____"
70 "_______________"
71 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070072};
Mathias Agopianad678e12013-07-23 17:28:53 -070073
Jamie Gennisf238e282011-01-09 16:33:17 -080074// Transform matrices
75static float mtxIdentity[16] = {
76 1, 0, 0, 0,
77 0, 1, 0, 0,
78 0, 0, 1, 0,
79 0, 0, 0, 1,
80};
81static float mtxFlipH[16] = {
82 -1, 0, 0, 0,
83 0, 1, 0, 0,
84 0, 0, 1, 0,
85 1, 0, 0, 1,
86};
87static float mtxFlipV[16] = {
88 1, 0, 0, 0,
89 0, -1, 0, 0,
90 0, 0, 1, 0,
91 0, 1, 0, 1,
92};
93static float mtxRot90[16] = {
94 0, 1, 0, 0,
95 -1, 0, 0, 0,
96 0, 0, 1, 0,
97 1, 0, 0, 1,
98};
Jamie Gennisf238e282011-01-09 16:33:17 -080099
100static void mtxMul(float out[16], const float a[16], const float b[16]);
101
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700102Mutex GLConsumer::sStaticInitLock;
103sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -0700104
Jamie Gennisdbe92452013-09-23 17:22:10 -0700105static bool hasEglAndroidImageCropImpl() {
106 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
107 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
108 size_t cropExtLen = strlen(CROP_EXT_STR);
109 size_t extsLen = strlen(exts);
110 bool equal = !strcmp(CROP_EXT_STR, exts);
111 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
112 bool atEnd = (cropExtLen+1) < extsLen &&
113 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
114 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
115 return equal || atStart || atEnd || inMiddle;
116}
117
118static bool hasEglAndroidImageCrop() {
119 // Only compute whether the extension is present once the first time this
120 // function is called.
121 static bool hasIt = hasEglAndroidImageCropImpl();
122 return hasIt;
123}
124
125static 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),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800132 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200133 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800134 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800135 mCurrentTimestamp(0),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700136 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200137 mDefaultWidth(1),
138 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700139 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700140 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800141 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800142 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700143 mEglDisplay(EGL_NO_DISPLAY),
144 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700145 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
146 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800147{
Dan Stozad723bd72014-11-18 10:24:03 -0800148 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800149
Jamie Gennisfa28c352011-09-16 17:30:26 -0700150 memcpy(mCurrentTransformMatrix, mtxIdentity,
151 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700152
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700153 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800154}
155
Dan Stozaab574912014-06-25 14:21:45 -0700156GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
157 bool useFenceSync, bool isControlledByApp) :
158 ConsumerBase(bq, isControlledByApp),
159 mCurrentTransform(0),
160 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
161 mCurrentFence(Fence::NO_FENCE),
162 mCurrentTimestamp(0),
163 mCurrentFrameNumber(0),
164 mDefaultWidth(1),
165 mDefaultHeight(1),
166 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800167 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700168 mUseFenceSync(useFenceSync),
169 mTexTarget(texTarget),
170 mEglDisplay(EGL_NO_DISPLAY),
171 mEglContext(EGL_NO_CONTEXT),
172 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
173 mAttached(false)
174{
Dan Stozad723bd72014-11-18 10:24:03 -0800175 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700176
177 memcpy(mCurrentTransformMatrix, mtxIdentity,
178 sizeof(mCurrentTransformMatrix));
179
180 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
181}
182
Andy McFadden2adaf042012-12-18 09:49:45 -0800183status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700184 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700185 return mConsumer->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700186}
187
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800188
Andy McFadden2adaf042012-12-18 09:49:45 -0800189status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700190{
Daniel Lamb2675792012-02-23 14:35:13 -0800191 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700192 mDefaultWidth = w;
193 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700194 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700195}
196
Andy McFadden2adaf042012-12-18 09:49:45 -0800197status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800198 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800199 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800200 Mutex::Autolock lock(mMutex);
201
202 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800203 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800204 return NO_INIT;
205 }
206
207 // Make sure the EGL state is the same as in previous calls.
208 status_t err = checkAndUpdateEglStateLocked();
209 if (err != NO_ERROR) {
210 return err;
211 }
212
213 BufferQueue::BufferItem item;
214
215 // Acquire the next buffer.
216 // In asynchronous mode the list is guaranteed to be one buffer
217 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700218 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800219 if (err != NO_ERROR) {
220 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
221 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800222 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800223 glBindTexture(mTexTarget, mTexName);
224 err = NO_ERROR;
225 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800226 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800227 strerror(-err), err);
228 }
229 return err;
230 }
231
232 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700233 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800234 if (err != NO_ERROR) {
235 // We always bind the texture.
236 glBindTexture(mTexTarget, mTexName);
237 return err;
238 }
239
Andy McFadden97eba892012-12-11 15:21:45 -0800240 // Bind the new buffer to the GL texture, and wait until it's ready.
241 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700242}
243
Mathias Agopianad678e12013-07-23 17:28:53 -0700244
245status_t GLConsumer::releaseTexImage() {
246 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800247 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700248 Mutex::Autolock lock(mMutex);
249
250 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800251 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700252 return NO_INIT;
253 }
254
255 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700256 status_t err = NO_ERROR;
257
258 if (mAttached) {
259 err = checkAndUpdateEglStateLocked(true);
260 if (err != NO_ERROR) {
261 return err;
262 }
263 } else {
264 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700265 }
266
267 // Update the GLConsumer state.
268 int buf = mCurrentTexture;
269 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
270
Dan Stozad723bd72014-11-18 10:24:03 -0800271 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700272
Mathias Agopian45155962013-08-08 18:16:21 -0700273 if (mAttached) {
274 // Do whatever sync ops we need to do before releasing the slot.
275 err = syncForReleaseLocked(mEglDisplay);
276 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800277 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700278 return err;
279 }
280 } else {
281 // if we're detached, we just use the fence that was created in detachFromContext()
282 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700283 }
284
Mathias Agopian45155962013-08-08 18:16:21 -0700285 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700286 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800287 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700288 strerror(-err), err);
289 return err;
290 }
291
Eric Penner5c3d2432014-07-11 19:08:04 -0700292 if (mReleasedTexImage == NULL) {
293 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
294 }
295
Mathias Agopianad678e12013-07-23 17:28:53 -0700296 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700297 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700298 mCurrentCrop.makeInvalid();
299 mCurrentTransform = 0;
300 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
301 mCurrentTimestamp = 0;
302 mCurrentFence = Fence::NO_FENCE;
303
Mathias Agopian45155962013-08-08 18:16:21 -0700304 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700305 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800306 status_t result = bindTextureImageLocked();
307 if (result != NO_ERROR) {
308 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700309 }
Mathias Agopian45155962013-08-08 18:16:21 -0700310 } else {
311 // detached, don't touch the texture (and we may not even have an
312 // EGLDisplay here.
313 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700314 }
315
316 return NO_ERROR;
317}
318
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700319sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
320 Mutex::Autolock _l(sStaticInitLock);
321 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
322 // The first time, create the debug texture in case the application
323 // continues to use it.
324 sp<GraphicBuffer> buffer = new GraphicBuffer(
325 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
326 GraphicBuffer::USAGE_SW_WRITE_RARELY);
327 uint32_t* bits;
328 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800329 uint32_t stride = buffer->getStride();
330 uint32_t height = buffer->getHeight();
331 memset(bits, 0, stride * height * 4);
332 for (uint32_t y = 0; y < kDebugData.height; y++) {
333 for (uint32_t x = 0; x < kDebugData.width; x++) {
334 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
335 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700336 }
Dan Stozad723bd72014-11-18 10:24:03 -0800337 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700338 }
339 buffer->unlock();
340 sReleasedTexImageBuffer = buffer;
341 }
342 return sReleasedTexImageBuffer;
343}
344
Andy McFadden1585c4d2013-06-28 13:52:40 -0700345status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item,
346 nsecs_t presentWhen) {
347 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700348 if (err != NO_ERROR) {
349 return err;
350 }
351
Eric Penner5c3d2432014-07-11 19:08:04 -0700352 // If item->mGraphicBuffer is not null, this buffer has not been acquired
353 // before, so any prior EglImage created is using a stale buffer. This
354 // replaces any old EglImage with a new one (using the new buffer).
355 if (item->mGraphicBuffer != NULL) {
356 int slot = item->mBuf;
357 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700358 }
359
Jamie Gennis9fea3422012-08-07 18:03:04 -0700360 return NO_ERROR;
361}
362
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700363status_t GLConsumer::releaseBufferLocked(int buf,
364 sp<GraphicBuffer> graphicBuffer,
365 EGLDisplay display, EGLSyncKHR eglFence) {
366 // release the buffer if it hasn't already been discarded by the
367 // BufferQueue. This can happen, for example, when the producer of this
368 // buffer has reallocated the original buffer slot after this buffer
369 // was acquired.
370 status_t err = ConsumerBase::releaseBufferLocked(
371 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700372 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700373 return err;
374}
375
Mathias Agopianad678e12013-07-23 17:28:53 -0700376status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800377{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700378 status_t err = NO_ERROR;
379
Andy McFadden87a67842014-04-04 15:37:08 -0700380 int buf = item.mBuf;
381
Jamie Gennis74bed552012-03-28 19:05:54 -0700382 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800383 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700384 "ES context");
Andy McFadden87a67842014-04-04 15:37:08 -0700385 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
386 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700387 return INVALID_OPERATION;
388 }
389
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800390 // Confirm state.
391 err = checkAndUpdateEglStateLocked();
392 if (err != NO_ERROR) {
Andy McFadden87a67842014-04-04 15:37:08 -0700393 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
394 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800395 return err;
396 }
397
Eric Penner5c3d2432014-07-11 19:08:04 -0700398 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
399 // if nessessary, for the gralloc buffer currently in the slot in
400 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800401 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700402 // means the buffer was previously acquired).
403 err = mEglSlots[buf].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
404 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800405 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700406 mEglDisplay, buf);
407 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
408 mEglDisplay, EGL_NO_SYNC_KHR);
409 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800410 }
411
412 // Do whatever sync ops we need to do before releasing the old slot.
413 err = syncForReleaseLocked(mEglDisplay);
414 if (err != NO_ERROR) {
415 // Release the buffer we just acquired. It's not safe to
416 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700417 // As we are still under lock since acquireBuffer, it is safe to
418 // release by slot.
419 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
420 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800421 return err;
422 }
423
Dan Stozad723bd72014-11-18 10:24:03 -0800424 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700425 mCurrentTexture, mCurrentTextureImage != NULL ?
426 mCurrentTextureImage->graphicBufferHandle() : 0,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800427 buf, mSlots[buf].mGraphicBuffer->handle);
428
429 // release old buffer
430 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700431 status_t status = releaseBufferLocked(
Eric Penner5c3d2432014-07-11 19:08:04 -0700432 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
433 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700434 if (status < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800435 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800436 strerror(-status), status);
437 err = status;
438 // keep going, with error raised [?]
439 }
440 }
441
Andy McFadden2adaf042012-12-18 09:49:45 -0800442 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800443 mCurrentTexture = buf;
Eric Penner5c3d2432014-07-11 19:08:04 -0700444 mCurrentTextureImage = mEglSlots[buf].mEglImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800445 mCurrentCrop = item.mCrop;
446 mCurrentTransform = item.mTransform;
447 mCurrentScalingMode = item.mScalingMode;
448 mCurrentTimestamp = item.mTimestamp;
449 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700450 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800451
452 computeCurrentTransformMatrixLocked();
453
454 return err;
455}
456
Andy McFadden2adaf042012-12-18 09:49:45 -0800457status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800458 if (mEglDisplay == EGL_NO_DISPLAY) {
459 ALOGE("bindTextureImage: invalid display");
460 return INVALID_OPERATION;
461 }
462
Dan Stozad723bd72014-11-18 10:24:03 -0800463 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800464 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800465 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800466 }
467
468 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700469 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
470 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800471 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700472 return NO_INIT;
473 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800474
Eric Penner5c3d2432014-07-11 19:08:04 -0700475 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700476 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700477 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800478 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700479 mEglDisplay, mCurrentTexture);
480 return UNKNOWN_ERROR;
481 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700482 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
483
Eric Penner2d14a0e2014-08-25 20:16:37 -0700484 // In the rare case that the display is terminated and then initialized
485 // again, we can't detect that the display changed (it didn't), but the
486 // image is invalid. In this case, repeat the exact same steps while
487 // forcing the creation of a new image.
488 if ((error = glGetError()) != GL_NO_ERROR) {
489 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800490 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
491 mCurrentCrop,
492 true);
493 if (result != NO_ERROR) {
494 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700495 mEglDisplay, mCurrentTexture);
496 return UNKNOWN_ERROR;
497 }
498 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
499 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800500 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700501 return UNKNOWN_ERROR;
502 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800503 }
Andy McFadden97eba892012-12-11 15:21:45 -0800504
505 // Wait for the new buffer to be ready.
506 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800507}
508
Mathias Agopian45155962013-08-08 18:16:21 -0700509status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700510 EGLDisplay dpy = eglGetCurrentDisplay();
511 EGLContext ctx = eglGetCurrentContext();
512
Mathias Agopian45155962013-08-08 18:16:21 -0700513 if (!contextCheck) {
514 // if this is the first time we're called, mEglDisplay/mEglContext have
515 // never been set, so don't error out (below).
516 if (mEglDisplay == EGL_NO_DISPLAY) {
517 mEglDisplay = dpy;
518 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700519 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700520 mEglContext = ctx;
521 }
522 }
523
524 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800525 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700526 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700527 }
528
Mathias Agopian45155962013-08-08 18:16:21 -0700529 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800530 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700531 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700532 }
533
534 mEglDisplay = dpy;
535 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800536 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800537}
538
Jesse Hall13f01cb2013-03-20 11:37:21 -0700539void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
540 if (fence->isValid() &&
541 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700542 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700543 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700544 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800545 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700546 strerror(-err), err);
547 }
Jesse Hallef194142012-06-14 14:45:17 -0700548 }
549}
550
Andy McFadden2adaf042012-12-18 09:49:45 -0800551status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700552 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800553 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700554 Mutex::Autolock lock(mMutex);
555
556 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800557 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700558 return NO_INIT;
559 }
560
561 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800562 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700563 "context");
564 return INVALID_OPERATION;
565 }
566
567 EGLDisplay dpy = eglGetCurrentDisplay();
568 EGLContext ctx = eglGetCurrentContext();
569
570 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800571 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700572 return INVALID_OPERATION;
573 }
574
575 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800576 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700577 return INVALID_OPERATION;
578 }
579
580 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
581 status_t err = syncForReleaseLocked(dpy);
582 if (err != OK) {
583 return err;
584 }
585
586 glDeleteTextures(1, &mTexName);
587 }
588
589 mEglDisplay = EGL_NO_DISPLAY;
590 mEglContext = EGL_NO_CONTEXT;
591 mAttached = false;
592
593 return OK;
594}
595
Mathias Agopian3f844832013-08-07 21:24:32 -0700596status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700597 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800598 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700599 Mutex::Autolock lock(mMutex);
600
601 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800602 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700603 return NO_INIT;
604 }
605
606 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800607 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700608 "context");
609 return INVALID_OPERATION;
610 }
611
612 EGLDisplay dpy = eglGetCurrentDisplay();
613 EGLContext ctx = eglGetCurrentContext();
614
615 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800616 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700617 return INVALID_OPERATION;
618 }
619
620 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800621 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700622 return INVALID_OPERATION;
623 }
624
625 // We need to bind the texture regardless of whether there's a current
626 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700627 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700628
Jamie Gennis74bed552012-03-28 19:05:54 -0700629 mEglDisplay = dpy;
630 mEglContext = ctx;
631 mTexName = tex;
632 mAttached = true;
633
Eric Penner5c3d2432014-07-11 19:08:04 -0700634 if (mCurrentTextureImage != NULL) {
635 // This may wait for a buffer a second time. This is likely required if
636 // this is a different context, since otherwise the wait could be skipped
637 // by bouncing through another context. For the same context the extra
638 // wait is redundant.
639 status_t err = bindTextureImageLocked();
640 if (err != NO_ERROR) {
641 return err;
642 }
643 }
644
Jamie Gennis74bed552012-03-28 19:05:54 -0700645 return OK;
646}
647
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800648
Andy McFadden2adaf042012-12-18 09:49:45 -0800649status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800650 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700651
Jamie Gennis01dbf552012-09-06 14:54:19 -0700652 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700653 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700654 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
655 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
656 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800657 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700658 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700659 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700660 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700661 glFlush();
662 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700663 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700664 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800665 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700666 "fd: %#x", eglGetError());
667 return UNKNOWN_ERROR;
668 }
669 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700670 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700671 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700672 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800673 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700674 "%s (%d)", strerror(-err), err);
675 return err;
676 }
Mathias Agopianca088332013-03-28 17:44:13 -0700677 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700678 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
679 if (fence != EGL_NO_SYNC_KHR) {
680 // There is already a fence for the current slot. We need to
681 // wait on that before replacing it with another fence to
682 // ensure that all outstanding buffer accesses have completed
683 // before the producer accesses it.
684 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
685 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800686 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700687 "fence: %#x", eglGetError());
688 return UNKNOWN_ERROR;
689 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800690 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700691 "fence");
692 return TIMED_OUT;
693 }
694 eglDestroySyncKHR(dpy, fence);
695 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700696
Jamie Gennis01dbf552012-09-06 14:54:19 -0700697 // Create a fence for the outstanding accesses in the current
698 // OpenGL ES context.
699 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
700 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800701 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700702 eglGetError());
703 return UNKNOWN_ERROR;
704 }
705 glFlush();
706 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700707 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700708 }
709
710 return OK;
711}
712
Dan Stozad723bd72014-11-18 10:24:03 -0800713bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700714{
715 switch (format) {
716 // supported YUV formats
717 case HAL_PIXEL_FORMAT_YV12:
718 // Legacy/deprecated YUV formats
719 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
720 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
721 case HAL_PIXEL_FORMAT_YCbCr_422_I:
722 return true;
723 }
724
725 // Any OEM format needs to be considered
726 if (format>=0x100 && format<=0x1FF)
727 return true;
728
729 return false;
730}
731
Mathias Agopian3f844832013-08-07 21:24:32 -0700732uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700733 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700734}
735
Andy McFadden2adaf042012-12-18 09:49:45 -0800736void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800737 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700738 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
739}
740
Andy McFadden2adaf042012-12-18 09:49:45 -0800741void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700742 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700743 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800744 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700745 return;
746 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700747 bool needsRecompute = mFilteringEnabled != enabled;
748 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700749
Eric Penner5c3d2432014-07-11 19:08:04 -0700750 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800751 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700752 }
753
Eric Penner5c3d2432014-07-11 19:08:04 -0700754 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700755 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700756 }
757}
758
Andy McFadden2adaf042012-12-18 09:49:45 -0800759void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800760 GLC_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800761
Jamie Gennisa214c642011-01-14 13:53:31 -0800762 float xform[16];
763 for (int i = 0; i < 16; i++) {
764 xform[i] = mtxIdentity[i];
765 }
766 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
767 float result[16];
768 mtxMul(result, xform, mtxFlipH);
769 for (int i = 0; i < 16; i++) {
770 xform[i] = result[i];
771 }
772 }
773 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
774 float result[16];
775 mtxMul(result, xform, mtxFlipV);
776 for (int i = 0; i < 16; i++) {
777 xform[i] = result[i];
778 }
779 }
780 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
781 float result[16];
782 mtxMul(result, xform, mtxRot90);
783 for (int i = 0; i < 16; i++) {
784 xform[i] = result[i];
785 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800786 }
787
Eric Penner5c3d2432014-07-11 19:08:04 -0700788 sp<GraphicBuffer> buf = (mCurrentTextureImage == NULL) ?
789 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700790
791 if (buf == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800792 GLC_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureImage is NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700793 }
794
Jamie Gennisdbe92452013-09-23 17:22:10 -0700795 float mtxBeforeFlipV[16];
796 if (!isEglImageCroppable(mCurrentCrop)) {
797 Rect cropRect = mCurrentCrop;
798 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
799 float bufferWidth = buf->getWidth();
800 float bufferHeight = buf->getHeight();
801 if (!cropRect.isEmpty()) {
802 float shrinkAmount = 0.0f;
803 if (mFilteringEnabled) {
804 // In order to prevent bilinear sampling beyond the edge of the
805 // crop rectangle we may need to shrink it by 2 texels in each
806 // dimension. Normally this would just need to take 1/2 a texel
807 // off each end, but because the chroma channels of YUV420 images
808 // are subsampled we may need to shrink the crop region by a whole
809 // texel on each side.
810 switch (buf->getPixelFormat()) {
811 case PIXEL_FORMAT_RGBA_8888:
812 case PIXEL_FORMAT_RGBX_8888:
813 case PIXEL_FORMAT_RGB_888:
814 case PIXEL_FORMAT_RGB_565:
815 case PIXEL_FORMAT_BGRA_8888:
816 // We know there's no subsampling of any channels, so we
817 // only need to shrink by a half a pixel.
818 shrinkAmount = 0.5;
819 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700820
Jamie Gennisdbe92452013-09-23 17:22:10 -0700821 default:
822 // If we don't recognize the format, we must assume the
823 // worst case (that we care about), which is YUV420.
824 shrinkAmount = 1.0;
825 break;
826 }
827 }
828
829 // Only shrink the dimensions that are not the size of the buffer.
830 if (cropRect.width() < bufferWidth) {
831 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
832 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
833 bufferWidth;
834 }
835 if (cropRect.height() < bufferHeight) {
836 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
837 bufferHeight;
838 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
839 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700840 }
841 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700842 float crop[16] = {
843 sx, 0, 0, 0,
844 0, sy, 0, 0,
845 0, 0, 1, 0,
846 tx, ty, 0, 1,
847 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700848
Jamie Gennisdbe92452013-09-23 17:22:10 -0700849 mtxMul(mtxBeforeFlipV, crop, xform);
850 } else {
851 for (int i = 0; i < 16; i++) {
852 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700853 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800854 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800855
856 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800857 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800858 // want to expose this to applications, however, so we must add an
859 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700860 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800861}
862
Andy McFadden2adaf042012-12-18 09:49:45 -0800863nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800864 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800865 Mutex::Autolock lock(mMutex);
866 return mCurrentTimestamp;
867}
868
Dan Stozad723bd72014-11-18 10:24:03 -0800869uint64_t GLConsumer::getFrameNumber() {
870 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700871 Mutex::Autolock lock(mMutex);
872 return mCurrentFrameNumber;
873}
874
Andy McFadden2adaf042012-12-18 09:49:45 -0800875sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700876 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700877 return (mCurrentTextureImage == NULL) ?
878 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700879}
880
Andy McFadden2adaf042012-12-18 09:49:45 -0800881Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700882 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700883
884 Rect outCrop = mCurrentCrop;
885 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800886 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
887 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700888
889 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
890 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800891 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700892 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
893 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800894 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700895 }
896
Dan Stozad723bd72014-11-18 10:24:03 -0800897 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
898 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
899
Daniel Lam016c8cb2012-04-03 15:54:58 -0700900 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800901 if (newWidth < currentWidth) {
902 uint32_t dw = (newWidth - currentWidth) / 2;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700903 outCrop.left -=dw;
904 outCrop.right += dw;
905 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800906 } else if (newHeight < currentHeight) {
907 uint32_t dh = (newHeight - currentHeight) / 2;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700908 outCrop.top -= dh;
909 outCrop.bottom += dh;
910 }
911
Dan Stozad723bd72014-11-18 10:24:03 -0800912 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700913 outCrop.left, outCrop.top,
914 outCrop.right,outCrop.bottom);
915 }
916
Daniel Lam016c8cb2012-04-03 15:54:58 -0700917 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700918}
919
Andy McFadden2adaf042012-12-18 09:49:45 -0800920uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700921 Mutex::Autolock lock(mMutex);
922 return mCurrentTransform;
923}
924
Andy McFadden2adaf042012-12-18 09:49:45 -0800925uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700926 Mutex::Autolock lock(mMutex);
927 return mCurrentScalingMode;
928}
929
Andy McFadden2adaf042012-12-18 09:49:45 -0800930sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700931 Mutex::Autolock lock(mMutex);
932 return mCurrentFence;
933}
934
Andy McFadden2adaf042012-12-18 09:49:45 -0800935status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700936 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700937 return doGLFenceWaitLocked();
938}
939
Andy McFadden2adaf042012-12-18 09:49:45 -0800940status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700941
942 EGLDisplay dpy = eglGetCurrentDisplay();
943 EGLContext ctx = eglGetCurrentContext();
944
945 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800946 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700947 return INVALID_OPERATION;
948 }
949
950 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800951 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700952 return INVALID_OPERATION;
953 }
954
Jamie Gennis1df8c342012-12-20 14:05:45 -0800955 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700956 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700957 // Create an EGLSyncKHR from the current fence.
958 int fenceFd = mCurrentFence->dup();
959 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800960 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700961 return -errno;
962 }
963 EGLint attribs[] = {
964 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
965 EGL_NONE
966 };
967 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
968 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
969 if (sync == EGL_NO_SYNC_KHR) {
970 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800971 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700972 eglGetError());
973 return UNKNOWN_ERROR;
974 }
975
976 // XXX: The spec draft is inconsistent as to whether this should
977 // return an EGLint or void. Ignore the return value for now, as
978 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700979 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700980 EGLint eglErr = eglGetError();
981 eglDestroySyncKHR(dpy, sync);
982 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800983 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700984 eglErr);
985 return UNKNOWN_ERROR;
986 }
987 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700988 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800989 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700990 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800991 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700992 return err;
993 }
994 }
995 }
996
997 return NO_ERROR;
998}
999
Andy McFadden2adaf042012-12-18 09:49:45 -08001000void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001001 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001002 if (slotIndex == mCurrentTexture) {
1003 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1004 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001005 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001006 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001007}
Daniel Lameae59d22012-01-22 15:26:27 -08001008
Andy McFadden2adaf042012-12-18 09:49:45 -08001009void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001010 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001011 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001012 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001013}
1014
Andy McFadden2adaf042012-12-18 09:49:45 -08001015void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001016 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001017 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001018 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001019}
1020
Dan Stozad723bd72014-11-18 10:24:03 -08001021status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001022 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001023 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001024}
1025
Andy McFadden2adaf042012-12-18 09:49:45 -08001026status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001027 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001028 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001029 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001030}
1031
Andy McFadden2adaf042012-12-18 09:49:45 -08001032status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001033 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001034 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001035}
1036
Mathias Agopian74d211a2013-04-22 16:55:35 +02001037void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001038{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001039 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001040 "%smTexName=%d mCurrentTexture=%d\n"
1041 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1042 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1043 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1044 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001045
Mathias Agopian74d211a2013-04-22 16:55:35 +02001046 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001047}
1048
Jamie Gennisf238e282011-01-09 16:33:17 -08001049static void mtxMul(float out[16], const float a[16], const float b[16]) {
1050 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1051 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1052 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1053 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1054
1055 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1056 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1057 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1058 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1059
1060 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1061 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1062 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1063 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1064
1065 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1066 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1067 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1068 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1069}
1070
Eric Penner5c3d2432014-07-11 19:08:04 -07001071GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1072 mGraphicBuffer(graphicBuffer),
1073 mEglImage(EGL_NO_IMAGE_KHR),
1074 mEglDisplay(EGL_NO_DISPLAY) {
1075}
1076
1077GLConsumer::EglImage::~EglImage() {
1078 if (mEglImage != EGL_NO_IMAGE_KHR) {
1079 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1080 ALOGE("~EglImage: eglDestroyImageKHR failed");
1081 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001082 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001083 }
1084}
1085
1086status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001087 const Rect& cropRect,
1088 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001089 // If there's an image and it's no longer valid, destroy it.
1090 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1091 bool displayInvalid = mEglDisplay != eglDisplay;
1092 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001093 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001094 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1095 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1096 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001097 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001098 mEglImage = EGL_NO_IMAGE_KHR;
1099 mEglDisplay = EGL_NO_DISPLAY;
1100 }
1101
1102 // If there's no image, create one.
1103 if (mEglImage == EGL_NO_IMAGE_KHR) {
1104 mEglDisplay = eglDisplay;
1105 mCropRect = cropRect;
1106 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1107 }
1108
1109 // Fail if we can't create a valid image.
1110 if (mEglImage == EGL_NO_IMAGE_KHR) {
1111 mEglDisplay = EGL_NO_DISPLAY;
1112 mCropRect.makeInvalid();
1113 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1114 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1115 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1116 buffer->getUsage(), buffer->getPixelFormat());
1117 return UNKNOWN_ERROR;
1118 }
1119
1120 return OK;
1121}
1122
1123void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001124 glEGLImageTargetTexture2DOES(texTarget,
1125 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001126}
1127
1128EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1129 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001130 EGLClientBuffer cbuf =
1131 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Eric Penner5c3d2432014-07-11 19:08:04 -07001132 EGLint attrs[] = {
1133 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1134 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1135 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1136 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1137 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
1138 EGL_NONE,
1139 };
1140 if (!crop.isValid()) {
1141 // No crop rect to set, so terminate the attrib array before the crop.
1142 attrs[2] = EGL_NONE;
1143 } else if (!isEglImageCroppable(crop)) {
1144 // The crop rect is not at the origin, so we can't set the crop on the
1145 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1146 // extension. In the future we can add a layered extension that
1147 // removes this restriction if there is hardware that can support it.
1148 attrs[2] = EGL_NONE;
1149 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001150 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001151 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1152 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1153 if (image == EGL_NO_IMAGE_KHR) {
1154 EGLint error = eglGetError();
1155 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001156 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001157 }
1158 return image;
1159}
1160
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001161}; // namespace android