blob: 9fcac2dbed8f9a83b9a2f66a662b8697365212e5 [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,
Dan Stozaa4650a52015-05-12 12:56:16 -0700347 nsecs_t presentWhen, uint64_t maxFrameNumber) {
348 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
349 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700350 if (err != NO_ERROR) {
351 return err;
352 }
353
Eric Penner5c3d2432014-07-11 19:08:04 -0700354 // If item->mGraphicBuffer is not null, this buffer has not been acquired
355 // before, so any prior EglImage created is using a stale buffer. This
356 // replaces any old EglImage with a new one (using the new buffer).
357 if (item->mGraphicBuffer != NULL) {
358 int slot = item->mBuf;
359 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700360 }
361
Jamie Gennis9fea3422012-08-07 18:03:04 -0700362 return NO_ERROR;
363}
364
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700365status_t GLConsumer::releaseBufferLocked(int buf,
366 sp<GraphicBuffer> graphicBuffer,
367 EGLDisplay display, EGLSyncKHR eglFence) {
368 // release the buffer if it hasn't already been discarded by the
369 // BufferQueue. This can happen, for example, when the producer of this
370 // buffer has reallocated the original buffer slot after this buffer
371 // was acquired.
372 status_t err = ConsumerBase::releaseBufferLocked(
373 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700374 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700375 return err;
376}
377
Dan Stoza54716312015-03-13 14:40:34 -0700378status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800379{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700380 status_t err = NO_ERROR;
381
Andy McFadden87a67842014-04-04 15:37:08 -0700382 int buf = item.mBuf;
383
Jamie Gennis74bed552012-03-28 19:05:54 -0700384 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800385 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700386 "ES context");
Andy McFadden87a67842014-04-04 15:37:08 -0700387 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
388 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700389 return INVALID_OPERATION;
390 }
391
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800392 // Confirm state.
393 err = checkAndUpdateEglStateLocked();
394 if (err != NO_ERROR) {
Andy McFadden87a67842014-04-04 15:37:08 -0700395 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
396 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800397 return err;
398 }
399
Eric Penner5c3d2432014-07-11 19:08:04 -0700400 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
401 // if nessessary, for the gralloc buffer currently in the slot in
402 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800403 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700404 // means the buffer was previously acquired).
405 err = mEglSlots[buf].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
406 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800407 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700408 mEglDisplay, buf);
409 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
410 mEglDisplay, EGL_NO_SYNC_KHR);
411 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800412 }
413
414 // Do whatever sync ops we need to do before releasing the old slot.
415 err = syncForReleaseLocked(mEglDisplay);
416 if (err != NO_ERROR) {
417 // Release the buffer we just acquired. It's not safe to
418 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700419 // As we are still under lock since acquireBuffer, it is safe to
420 // release by slot.
421 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
422 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800423 return err;
424 }
425
Dan Stozad723bd72014-11-18 10:24:03 -0800426 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700427 mCurrentTexture, mCurrentTextureImage != NULL ?
428 mCurrentTextureImage->graphicBufferHandle() : 0,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800429 buf, mSlots[buf].mGraphicBuffer->handle);
430
431 // release old buffer
432 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700433 status_t status = releaseBufferLocked(
Eric Penner5c3d2432014-07-11 19:08:04 -0700434 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
435 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700436 if (status < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800437 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800438 strerror(-status), status);
439 err = status;
440 // keep going, with error raised [?]
441 }
442 }
443
Andy McFadden2adaf042012-12-18 09:49:45 -0800444 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800445 mCurrentTexture = buf;
Eric Penner5c3d2432014-07-11 19:08:04 -0700446 mCurrentTextureImage = mEglSlots[buf].mEglImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800447 mCurrentCrop = item.mCrop;
448 mCurrentTransform = item.mTransform;
449 mCurrentScalingMode = item.mScalingMode;
450 mCurrentTimestamp = item.mTimestamp;
451 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700452 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800453
454 computeCurrentTransformMatrixLocked();
455
456 return err;
457}
458
Andy McFadden2adaf042012-12-18 09:49:45 -0800459status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800460 if (mEglDisplay == EGL_NO_DISPLAY) {
461 ALOGE("bindTextureImage: invalid display");
462 return INVALID_OPERATION;
463 }
464
Dan Stozad723bd72014-11-18 10:24:03 -0800465 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800466 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800467 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800468 }
469
470 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700471 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
472 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800473 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700474 return NO_INIT;
475 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800476
Eric Penner5c3d2432014-07-11 19:08:04 -0700477 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700478 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700479 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800480 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700481 mEglDisplay, mCurrentTexture);
482 return UNKNOWN_ERROR;
483 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700484 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
485
Eric Penner2d14a0e2014-08-25 20:16:37 -0700486 // In the rare case that the display is terminated and then initialized
487 // again, we can't detect that the display changed (it didn't), but the
488 // image is invalid. In this case, repeat the exact same steps while
489 // forcing the creation of a new image.
490 if ((error = glGetError()) != GL_NO_ERROR) {
491 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800492 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
493 mCurrentCrop,
494 true);
495 if (result != NO_ERROR) {
496 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700497 mEglDisplay, mCurrentTexture);
498 return UNKNOWN_ERROR;
499 }
500 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
501 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800502 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700503 return UNKNOWN_ERROR;
504 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800505 }
Andy McFadden97eba892012-12-11 15:21:45 -0800506
507 // Wait for the new buffer to be ready.
508 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800509}
510
Mathias Agopian45155962013-08-08 18:16:21 -0700511status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700512 EGLDisplay dpy = eglGetCurrentDisplay();
513 EGLContext ctx = eglGetCurrentContext();
514
Mathias Agopian45155962013-08-08 18:16:21 -0700515 if (!contextCheck) {
516 // if this is the first time we're called, mEglDisplay/mEglContext have
517 // never been set, so don't error out (below).
518 if (mEglDisplay == EGL_NO_DISPLAY) {
519 mEglDisplay = dpy;
520 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700521 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700522 mEglContext = ctx;
523 }
524 }
525
526 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800527 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700528 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700529 }
530
Mathias Agopian45155962013-08-08 18:16:21 -0700531 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800532 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700533 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700534 }
535
536 mEglDisplay = dpy;
537 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800538 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800539}
540
Jesse Hall13f01cb2013-03-20 11:37:21 -0700541void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
542 if (fence->isValid() &&
543 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700544 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700545 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700546 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800547 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700548 strerror(-err), err);
549 }
Jesse Hallef194142012-06-14 14:45:17 -0700550 }
551}
552
Andy McFadden2adaf042012-12-18 09:49:45 -0800553status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700554 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800555 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700556 Mutex::Autolock lock(mMutex);
557
558 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800559 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700560 return NO_INIT;
561 }
562
563 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800564 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700565 "context");
566 return INVALID_OPERATION;
567 }
568
569 EGLDisplay dpy = eglGetCurrentDisplay();
570 EGLContext ctx = eglGetCurrentContext();
571
572 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800573 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700574 return INVALID_OPERATION;
575 }
576
577 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800578 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700579 return INVALID_OPERATION;
580 }
581
582 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
583 status_t err = syncForReleaseLocked(dpy);
584 if (err != OK) {
585 return err;
586 }
587
588 glDeleteTextures(1, &mTexName);
589 }
590
591 mEglDisplay = EGL_NO_DISPLAY;
592 mEglContext = EGL_NO_CONTEXT;
593 mAttached = false;
594
595 return OK;
596}
597
Mathias Agopian3f844832013-08-07 21:24:32 -0700598status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700599 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800600 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700601 Mutex::Autolock lock(mMutex);
602
603 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800604 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700605 return NO_INIT;
606 }
607
608 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800609 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700610 "context");
611 return INVALID_OPERATION;
612 }
613
614 EGLDisplay dpy = eglGetCurrentDisplay();
615 EGLContext ctx = eglGetCurrentContext();
616
617 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800618 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700619 return INVALID_OPERATION;
620 }
621
622 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800623 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700624 return INVALID_OPERATION;
625 }
626
627 // We need to bind the texture regardless of whether there's a current
628 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700629 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700630
Jamie Gennis74bed552012-03-28 19:05:54 -0700631 mEglDisplay = dpy;
632 mEglContext = ctx;
633 mTexName = tex;
634 mAttached = true;
635
Eric Penner5c3d2432014-07-11 19:08:04 -0700636 if (mCurrentTextureImage != NULL) {
637 // This may wait for a buffer a second time. This is likely required if
638 // this is a different context, since otherwise the wait could be skipped
639 // by bouncing through another context. For the same context the extra
640 // wait is redundant.
641 status_t err = bindTextureImageLocked();
642 if (err != NO_ERROR) {
643 return err;
644 }
645 }
646
Jamie Gennis74bed552012-03-28 19:05:54 -0700647 return OK;
648}
649
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800650
Andy McFadden2adaf042012-12-18 09:49:45 -0800651status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800652 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700653
Jamie Gennis01dbf552012-09-06 14:54:19 -0700654 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700655 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700656 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
657 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
658 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800659 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700660 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700661 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700662 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700663 glFlush();
664 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700665 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700666 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800667 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700668 "fd: %#x", eglGetError());
669 return UNKNOWN_ERROR;
670 }
671 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700672 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700673 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700674 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800675 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700676 "%s (%d)", strerror(-err), err);
677 return err;
678 }
Mathias Agopianca088332013-03-28 17:44:13 -0700679 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700680 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
681 if (fence != EGL_NO_SYNC_KHR) {
682 // There is already a fence for the current slot. We need to
683 // wait on that before replacing it with another fence to
684 // ensure that all outstanding buffer accesses have completed
685 // before the producer accesses it.
686 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
687 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800688 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700689 "fence: %#x", eglGetError());
690 return UNKNOWN_ERROR;
691 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800692 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700693 "fence");
694 return TIMED_OUT;
695 }
696 eglDestroySyncKHR(dpy, fence);
697 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700698
Jamie Gennis01dbf552012-09-06 14:54:19 -0700699 // Create a fence for the outstanding accesses in the current
700 // OpenGL ES context.
701 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
702 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800703 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700704 eglGetError());
705 return UNKNOWN_ERROR;
706 }
707 glFlush();
708 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700709 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700710 }
711
712 return OK;
713}
714
Dan Stozad723bd72014-11-18 10:24:03 -0800715bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700716{
717 switch (format) {
718 // supported YUV formats
719 case HAL_PIXEL_FORMAT_YV12:
720 // Legacy/deprecated YUV formats
721 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
722 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
723 case HAL_PIXEL_FORMAT_YCbCr_422_I:
724 return true;
725 }
726
727 // Any OEM format needs to be considered
728 if (format>=0x100 && format<=0x1FF)
729 return true;
730
731 return false;
732}
733
Mathias Agopian3f844832013-08-07 21:24:32 -0700734uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700735 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700736}
737
Andy McFadden2adaf042012-12-18 09:49:45 -0800738void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800739 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700740 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
741}
742
Andy McFadden2adaf042012-12-18 09:49:45 -0800743void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700744 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700745 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800746 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700747 return;
748 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700749 bool needsRecompute = mFilteringEnabled != enabled;
750 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700751
Eric Penner5c3d2432014-07-11 19:08:04 -0700752 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800753 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700754 }
755
Eric Penner5c3d2432014-07-11 19:08:04 -0700756 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700757 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700758 }
759}
760
Andy McFadden2adaf042012-12-18 09:49:45 -0800761void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800762 GLC_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800763
Jamie Gennisa214c642011-01-14 13:53:31 -0800764 float xform[16];
765 for (int i = 0; i < 16; i++) {
766 xform[i] = mtxIdentity[i];
767 }
768 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
769 float result[16];
770 mtxMul(result, xform, mtxFlipH);
771 for (int i = 0; i < 16; i++) {
772 xform[i] = result[i];
773 }
774 }
775 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
776 float result[16];
777 mtxMul(result, xform, mtxFlipV);
778 for (int i = 0; i < 16; i++) {
779 xform[i] = result[i];
780 }
781 }
782 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
783 float result[16];
784 mtxMul(result, xform, mtxRot90);
785 for (int i = 0; i < 16; i++) {
786 xform[i] = result[i];
787 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800788 }
789
Eric Penner5c3d2432014-07-11 19:08:04 -0700790 sp<GraphicBuffer> buf = (mCurrentTextureImage == NULL) ?
791 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700792
793 if (buf == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800794 GLC_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureImage is NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700795 }
796
Jamie Gennisdbe92452013-09-23 17:22:10 -0700797 float mtxBeforeFlipV[16];
798 if (!isEglImageCroppable(mCurrentCrop)) {
799 Rect cropRect = mCurrentCrop;
800 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
801 float bufferWidth = buf->getWidth();
802 float bufferHeight = buf->getHeight();
803 if (!cropRect.isEmpty()) {
804 float shrinkAmount = 0.0f;
805 if (mFilteringEnabled) {
806 // In order to prevent bilinear sampling beyond the edge of the
807 // crop rectangle we may need to shrink it by 2 texels in each
808 // dimension. Normally this would just need to take 1/2 a texel
809 // off each end, but because the chroma channels of YUV420 images
810 // are subsampled we may need to shrink the crop region by a whole
811 // texel on each side.
812 switch (buf->getPixelFormat()) {
813 case PIXEL_FORMAT_RGBA_8888:
814 case PIXEL_FORMAT_RGBX_8888:
815 case PIXEL_FORMAT_RGB_888:
816 case PIXEL_FORMAT_RGB_565:
817 case PIXEL_FORMAT_BGRA_8888:
818 // We know there's no subsampling of any channels, so we
819 // only need to shrink by a half a pixel.
820 shrinkAmount = 0.5;
821 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700822
Jamie Gennisdbe92452013-09-23 17:22:10 -0700823 default:
824 // If we don't recognize the format, we must assume the
825 // worst case (that we care about), which is YUV420.
826 shrinkAmount = 1.0;
827 break;
828 }
829 }
830
831 // Only shrink the dimensions that are not the size of the buffer.
832 if (cropRect.width() < bufferWidth) {
833 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
834 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
835 bufferWidth;
836 }
837 if (cropRect.height() < bufferHeight) {
838 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
839 bufferHeight;
840 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
841 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700842 }
843 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700844 float crop[16] = {
845 sx, 0, 0, 0,
846 0, sy, 0, 0,
847 0, 0, 1, 0,
848 tx, ty, 0, 1,
849 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700850
Jamie Gennisdbe92452013-09-23 17:22:10 -0700851 mtxMul(mtxBeforeFlipV, crop, xform);
852 } else {
853 for (int i = 0; i < 16; i++) {
854 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700855 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800856 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800857
858 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800859 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800860 // want to expose this to applications, however, so we must add an
861 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700862 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800863}
864
Andy McFadden2adaf042012-12-18 09:49:45 -0800865nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800866 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800867 Mutex::Autolock lock(mMutex);
868 return mCurrentTimestamp;
869}
870
Dan Stozad723bd72014-11-18 10:24:03 -0800871uint64_t GLConsumer::getFrameNumber() {
872 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700873 Mutex::Autolock lock(mMutex);
874 return mCurrentFrameNumber;
875}
876
Andy McFadden2adaf042012-12-18 09:49:45 -0800877sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700878 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700879 return (mCurrentTextureImage == NULL) ?
880 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700881}
882
Andy McFadden2adaf042012-12-18 09:49:45 -0800883Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700884 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700885
886 Rect outCrop = mCurrentCrop;
887 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800888 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
889 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700890
891 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
892 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800893 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700894 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
895 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800896 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700897 }
898
Dan Stozad723bd72014-11-18 10:24:03 -0800899 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
900 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
901
Daniel Lam016c8cb2012-04-03 15:54:58 -0700902 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800903 if (newWidth < currentWidth) {
Dan Stozaabf952c2015-03-04 14:58:02 -0800904 uint32_t dw = (currentWidth - newWidth) / 2;
905 outCrop.left += dw;
906 outCrop.right -= dw;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700907 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800908 } else if (newHeight < currentHeight) {
Dan Stozaabf952c2015-03-04 14:58:02 -0800909 uint32_t dh = (currentHeight - newHeight) / 2;
910 outCrop.top += dh;
911 outCrop.bottom -= dh;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700912 }
913
Dan Stozad723bd72014-11-18 10:24:03 -0800914 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700915 outCrop.left, outCrop.top,
916 outCrop.right,outCrop.bottom);
917 }
918
Daniel Lam016c8cb2012-04-03 15:54:58 -0700919 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700920}
921
Andy McFadden2adaf042012-12-18 09:49:45 -0800922uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700923 Mutex::Autolock lock(mMutex);
924 return mCurrentTransform;
925}
926
Andy McFadden2adaf042012-12-18 09:49:45 -0800927uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700928 Mutex::Autolock lock(mMutex);
929 return mCurrentScalingMode;
930}
931
Andy McFadden2adaf042012-12-18 09:49:45 -0800932sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700933 Mutex::Autolock lock(mMutex);
934 return mCurrentFence;
935}
936
Andy McFadden2adaf042012-12-18 09:49:45 -0800937status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700938 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700939 return doGLFenceWaitLocked();
940}
941
Andy McFadden2adaf042012-12-18 09:49:45 -0800942status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700943
944 EGLDisplay dpy = eglGetCurrentDisplay();
945 EGLContext ctx = eglGetCurrentContext();
946
947 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800948 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700949 return INVALID_OPERATION;
950 }
951
952 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800953 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700954 return INVALID_OPERATION;
955 }
956
Jamie Gennis1df8c342012-12-20 14:05:45 -0800957 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700958 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700959 // Create an EGLSyncKHR from the current fence.
960 int fenceFd = mCurrentFence->dup();
961 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800962 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700963 return -errno;
964 }
965 EGLint attribs[] = {
966 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
967 EGL_NONE
968 };
969 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
970 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
971 if (sync == EGL_NO_SYNC_KHR) {
972 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800973 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700974 eglGetError());
975 return UNKNOWN_ERROR;
976 }
977
978 // XXX: The spec draft is inconsistent as to whether this should
979 // return an EGLint or void. Ignore the return value for now, as
980 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700981 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700982 EGLint eglErr = eglGetError();
983 eglDestroySyncKHR(dpy, sync);
984 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800985 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700986 eglErr);
987 return UNKNOWN_ERROR;
988 }
989 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700990 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800991 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700992 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800993 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700994 return err;
995 }
996 }
997 }
998
999 return NO_ERROR;
1000}
1001
Andy McFadden2adaf042012-12-18 09:49:45 -08001002void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001003 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001004 if (slotIndex == mCurrentTexture) {
1005 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1006 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001007 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001008 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001009}
Daniel Lameae59d22012-01-22 15:26:27 -08001010
Andy McFadden2adaf042012-12-18 09:49:45 -08001011void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001012 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001013 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001014 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001015}
1016
Andy McFadden2adaf042012-12-18 09:49:45 -08001017void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001018 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001019 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001020 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001021}
1022
Dan Stozad723bd72014-11-18 10:24:03 -08001023status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001024 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001025 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001026}
1027
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001028status_t GLConsumer::setDefaultBufferDataSpace(
1029 android_dataspace defaultDataSpace) {
1030 Mutex::Autolock lock(mMutex);
1031 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1032}
1033
Andy McFadden2adaf042012-12-18 09:49:45 -08001034status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001035 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001036 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001037 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001038}
1039
Andy McFadden2adaf042012-12-18 09:49:45 -08001040status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001041 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001042 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001043}
1044
Mathias Agopian74d211a2013-04-22 16:55:35 +02001045void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001046{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001047 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001048 "%smTexName=%d mCurrentTexture=%d\n"
1049 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1050 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1051 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1052 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001053
Mathias Agopian74d211a2013-04-22 16:55:35 +02001054 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001055}
1056
Jamie Gennisf238e282011-01-09 16:33:17 -08001057static void mtxMul(float out[16], const float a[16], const float b[16]) {
1058 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1059 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1060 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1061 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1062
1063 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1064 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1065 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1066 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1067
1068 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1069 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1070 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1071 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1072
1073 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1074 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1075 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1076 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1077}
1078
Eric Penner5c3d2432014-07-11 19:08:04 -07001079GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1080 mGraphicBuffer(graphicBuffer),
1081 mEglImage(EGL_NO_IMAGE_KHR),
1082 mEglDisplay(EGL_NO_DISPLAY) {
1083}
1084
1085GLConsumer::EglImage::~EglImage() {
1086 if (mEglImage != EGL_NO_IMAGE_KHR) {
1087 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1088 ALOGE("~EglImage: eglDestroyImageKHR failed");
1089 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001090 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001091 }
1092}
1093
1094status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001095 const Rect& cropRect,
1096 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001097 // If there's an image and it's no longer valid, destroy it.
1098 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1099 bool displayInvalid = mEglDisplay != eglDisplay;
1100 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001101 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001102 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1103 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1104 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001105 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001106 mEglImage = EGL_NO_IMAGE_KHR;
1107 mEglDisplay = EGL_NO_DISPLAY;
1108 }
1109
1110 // If there's no image, create one.
1111 if (mEglImage == EGL_NO_IMAGE_KHR) {
1112 mEglDisplay = eglDisplay;
1113 mCropRect = cropRect;
1114 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1115 }
1116
1117 // Fail if we can't create a valid image.
1118 if (mEglImage == EGL_NO_IMAGE_KHR) {
1119 mEglDisplay = EGL_NO_DISPLAY;
1120 mCropRect.makeInvalid();
1121 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1122 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1123 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1124 buffer->getUsage(), buffer->getPixelFormat());
1125 return UNKNOWN_ERROR;
1126 }
1127
1128 return OK;
1129}
1130
1131void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001132 glEGLImageTargetTexture2DOES(texTarget,
1133 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001134}
1135
1136EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1137 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001138 EGLClientBuffer cbuf =
1139 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Eric Penner5c3d2432014-07-11 19:08:04 -07001140 EGLint attrs[] = {
1141 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1142 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1143 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1144 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1145 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
1146 EGL_NONE,
1147 };
1148 if (!crop.isValid()) {
1149 // No crop rect to set, so terminate the attrib array before the crop.
1150 attrs[2] = EGL_NONE;
1151 } else if (!isEglImageCroppable(crop)) {
1152 // The crop rect is not at the origin, so we can't set the crop on the
1153 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1154 // extension. In the future we can add a layered extension that
1155 // removes this restriction if there is hardware that can support it.
1156 attrs[2] = EGL_NONE;
1157 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001158 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001159 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1160 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1161 if (image == EGL_NO_IMAGE_KHR) {
1162 EGLint error = eglGetError();
1163 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001164 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001165 }
1166 return image;
1167}
1168
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001169}; // namespace android