blob: 96c0841bdebe32f95a8c2a7394936902c5716d9b [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andy McFadden2adaf042012-12-18 09:49:45 -080017#define LOG_TAG "GLConsumer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070028#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029
Jamie Gennis9fea3422012-08-07 18:03:04 -070030#include <hardware/hardware.h>
31
Dan Stozadd264162015-03-12 13:58:47 -070032#include <gui/BufferItem.h>
Mathias Agopianca088332013-03-28 17:44:13 -070033#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/IGraphicBufferAlloc.h>
35#include <gui/ISurfaceComposer.h>
36#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080037
Mathias Agopian90ac7992012-02-25 18:48:35 -080038#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070039#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080040
41#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070042#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044
Jamie Gennisdbe92452013-09-23 17:22:10 -070045EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
46#define CROP_EXT_STR "EGL_ANDROID_image_crop"
47
Andy McFadden97eba892012-12-11 15:21:45 -080048namespace android {
49
Andy McFadden2adaf042012-12-18 09:49:45 -080050// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080051#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
53//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070056
Mathias Agopianad678e12013-07-23 17:28:53 -070057static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080058 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070059 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070060} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080061 "_______________"
62 "_______________"
63 "_____XX_XX_____"
64 "__X_X_____X_X__"
65 "__X_XXXXXXX_X__"
66 "__XXXXXXXXXXX__"
67 "___XX_XXX_XX___"
68 "____XXXXXXX____"
69 "_____X___X_____"
70 "____X_____X____"
71 "_______________"
72 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070073};
Mathias Agopianad678e12013-07-23 17:28:53 -070074
Jamie Gennisf238e282011-01-09 16:33:17 -080075// Transform matrices
76static float mtxIdentity[16] = {
77 1, 0, 0, 0,
78 0, 1, 0, 0,
79 0, 0, 1, 0,
80 0, 0, 0, 1,
81};
82static float mtxFlipH[16] = {
83 -1, 0, 0, 0,
84 0, 1, 0, 0,
85 0, 0, 1, 0,
86 1, 0, 0, 1,
87};
88static float mtxFlipV[16] = {
89 1, 0, 0, 0,
90 0, -1, 0, 0,
91 0, 0, 1, 0,
92 0, 1, 0, 1,
93};
94static float mtxRot90[16] = {
95 0, 1, 0, 0,
96 -1, 0, 0, 0,
97 0, 0, 1, 0,
98 1, 0, 0, 1,
99};
Jamie Gennisf238e282011-01-09 16:33:17 -0800100
101static void mtxMul(float out[16], const float a[16], const float b[16]);
102
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700103Mutex GLConsumer::sStaticInitLock;
104sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -0700105
Jamie Gennisdbe92452013-09-23 17:22:10 -0700106static bool hasEglAndroidImageCropImpl() {
107 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
108 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
109 size_t cropExtLen = strlen(CROP_EXT_STR);
110 size_t extsLen = strlen(exts);
111 bool equal = !strcmp(CROP_EXT_STR, exts);
112 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
113 bool atEnd = (cropExtLen+1) < extsLen &&
114 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
115 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
116 return equal || atStart || atEnd || inMiddle;
117}
118
119static bool hasEglAndroidImageCrop() {
120 // Only compute whether the extension is present once the first time this
121 // function is called.
122 static bool hasIt = hasEglAndroidImageCropImpl();
123 return hasIt;
124}
125
126static bool isEglImageCroppable(const Rect& crop) {
127 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
128}
129
Mathias Agopian3f844832013-08-07 21:24:32 -0700130GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
131 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700132 ConsumerBase(bq, isControlledByApp),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800133 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200134 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800135 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800136 mCurrentTimestamp(0),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700137 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200138 mDefaultWidth(1),
139 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700140 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700141 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800142 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800143 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700144 mEglDisplay(EGL_NO_DISPLAY),
145 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700146 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
147 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800148{
Dan Stozad723bd72014-11-18 10:24:03 -0800149 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800150
Jamie Gennisfa28c352011-09-16 17:30:26 -0700151 memcpy(mCurrentTransformMatrix, mtxIdentity,
152 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700153
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700154 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800155}
156
Dan Stozaab574912014-06-25 14:21:45 -0700157GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
158 bool useFenceSync, bool isControlledByApp) :
159 ConsumerBase(bq, isControlledByApp),
160 mCurrentTransform(0),
161 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
162 mCurrentFence(Fence::NO_FENCE),
163 mCurrentTimestamp(0),
164 mCurrentFrameNumber(0),
165 mDefaultWidth(1),
166 mDefaultHeight(1),
167 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800168 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700169 mUseFenceSync(useFenceSync),
170 mTexTarget(texTarget),
171 mEglDisplay(EGL_NO_DISPLAY),
172 mEglContext(EGL_NO_CONTEXT),
173 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
174 mAttached(false)
175{
Dan Stozad723bd72014-11-18 10:24:03 -0800176 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700177
178 memcpy(mCurrentTransformMatrix, mtxIdentity,
179 sizeof(mCurrentTransformMatrix));
180
181 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
182}
183
Andy McFadden2adaf042012-12-18 09:49:45 -0800184status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700185 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700186 return mConsumer->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700187}
188
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189
Andy McFadden2adaf042012-12-18 09:49:45 -0800190status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700191{
Daniel Lamb2675792012-02-23 14:35:13 -0800192 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700193 mDefaultWidth = w;
194 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700195 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700196}
197
Andy McFadden2adaf042012-12-18 09:49:45 -0800198status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800199 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800200 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800201 Mutex::Autolock lock(mMutex);
202
203 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800204 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800205 return NO_INIT;
206 }
207
208 // Make sure the EGL state is the same as in previous calls.
209 status_t err = checkAndUpdateEglStateLocked();
210 if (err != NO_ERROR) {
211 return err;
212 }
213
Dan Stozadd264162015-03-12 13:58:47 -0700214 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800215
216 // Acquire the next buffer.
217 // In asynchronous mode the list is guaranteed to be one buffer
218 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700219 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800220 if (err != NO_ERROR) {
221 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
222 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800223 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800224 glBindTexture(mTexTarget, mTexName);
225 err = NO_ERROR;
226 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800227 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800228 strerror(-err), err);
229 }
230 return err;
231 }
232
233 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700234 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800235 if (err != NO_ERROR) {
236 // We always bind the texture.
237 glBindTexture(mTexTarget, mTexName);
238 return err;
239 }
240
Andy McFadden97eba892012-12-11 15:21:45 -0800241 // Bind the new buffer to the GL texture, and wait until it's ready.
242 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700243}
244
Mathias Agopianad678e12013-07-23 17:28:53 -0700245
246status_t GLConsumer::releaseTexImage() {
247 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800248 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700249 Mutex::Autolock lock(mMutex);
250
251 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800252 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700253 return NO_INIT;
254 }
255
256 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700257 status_t err = NO_ERROR;
258
259 if (mAttached) {
260 err = checkAndUpdateEglStateLocked(true);
261 if (err != NO_ERROR) {
262 return err;
263 }
264 } else {
265 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700266 }
267
268 // Update the GLConsumer state.
269 int buf = mCurrentTexture;
270 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
271
Dan Stozad723bd72014-11-18 10:24:03 -0800272 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700273
Mathias Agopian45155962013-08-08 18:16:21 -0700274 if (mAttached) {
275 // Do whatever sync ops we need to do before releasing the slot.
276 err = syncForReleaseLocked(mEglDisplay);
277 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800278 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700279 return err;
280 }
281 } else {
282 // if we're detached, we just use the fence that was created in detachFromContext()
283 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700284 }
285
Mathias Agopian45155962013-08-08 18:16:21 -0700286 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700287 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800288 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700289 strerror(-err), err);
290 return err;
291 }
292
Eric Penner5c3d2432014-07-11 19:08:04 -0700293 if (mReleasedTexImage == NULL) {
294 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
295 }
296
Mathias Agopianad678e12013-07-23 17:28:53 -0700297 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700298 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700299 mCurrentCrop.makeInvalid();
300 mCurrentTransform = 0;
301 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
302 mCurrentTimestamp = 0;
303 mCurrentFence = Fence::NO_FENCE;
304
Mathias Agopian45155962013-08-08 18:16:21 -0700305 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700306 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800307 status_t result = bindTextureImageLocked();
308 if (result != NO_ERROR) {
309 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700310 }
Mathias Agopian45155962013-08-08 18:16:21 -0700311 } else {
312 // detached, don't touch the texture (and we may not even have an
313 // EGLDisplay here.
314 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700315 }
316
317 return NO_ERROR;
318}
319
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700320sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
321 Mutex::Autolock _l(sStaticInitLock);
322 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
323 // The first time, create the debug texture in case the application
324 // continues to use it.
325 sp<GraphicBuffer> buffer = new GraphicBuffer(
326 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
327 GraphicBuffer::USAGE_SW_WRITE_RARELY);
328 uint32_t* bits;
329 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800330 uint32_t stride = buffer->getStride();
331 uint32_t height = buffer->getHeight();
332 memset(bits, 0, stride * height * 4);
333 for (uint32_t y = 0; y < kDebugData.height; y++) {
334 for (uint32_t x = 0; x < kDebugData.width; x++) {
335 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
336 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700337 }
Dan Stozad723bd72014-11-18 10:24:03 -0800338 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700339 }
340 buffer->unlock();
341 sReleasedTexImageBuffer = buffer;
342 }
343 return sReleasedTexImageBuffer;
344}
345
Dan Stozadd264162015-03-12 13:58:47 -0700346status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700347 nsecs_t presentWhen) {
348 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700349 if (err != NO_ERROR) {
350 return err;
351 }
352
Eric Penner5c3d2432014-07-11 19:08:04 -0700353 // If item->mGraphicBuffer is not null, this buffer has not been acquired
354 // before, so any prior EglImage created is using a stale buffer. This
355 // replaces any old EglImage with a new one (using the new buffer).
356 if (item->mGraphicBuffer != NULL) {
357 int slot = item->mBuf;
358 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700359 }
360
Jamie Gennis9fea3422012-08-07 18:03:04 -0700361 return NO_ERROR;
362}
363
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700364status_t GLConsumer::releaseBufferLocked(int buf,
365 sp<GraphicBuffer> graphicBuffer,
366 EGLDisplay display, EGLSyncKHR eglFence) {
367 // release the buffer if it hasn't already been discarded by the
368 // BufferQueue. This can happen, for example, when the producer of this
369 // buffer has reallocated the original buffer slot after this buffer
370 // was acquired.
371 status_t err = ConsumerBase::releaseBufferLocked(
372 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700373 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700374 return err;
375}
376
Dan Stoza54716312015-03-13 14:40:34 -0700377status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800378{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700379 status_t err = NO_ERROR;
380
Andy McFadden87a67842014-04-04 15:37:08 -0700381 int buf = item.mBuf;
382
Jamie Gennis74bed552012-03-28 19:05:54 -0700383 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800384 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700385 "ES context");
Andy McFadden87a67842014-04-04 15:37:08 -0700386 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
387 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700388 return INVALID_OPERATION;
389 }
390
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800391 // Confirm state.
392 err = checkAndUpdateEglStateLocked();
393 if (err != NO_ERROR) {
Andy McFadden87a67842014-04-04 15:37:08 -0700394 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
395 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800396 return err;
397 }
398
Eric Penner5c3d2432014-07-11 19:08:04 -0700399 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
400 // if nessessary, for the gralloc buffer currently in the slot in
401 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800402 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700403 // means the buffer was previously acquired).
404 err = mEglSlots[buf].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
405 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800406 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700407 mEglDisplay, buf);
408 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
409 mEglDisplay, EGL_NO_SYNC_KHR);
410 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800411 }
412
413 // Do whatever sync ops we need to do before releasing the old slot.
414 err = syncForReleaseLocked(mEglDisplay);
415 if (err != NO_ERROR) {
416 // Release the buffer we just acquired. It's not safe to
417 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700418 // As we are still under lock since acquireBuffer, it is safe to
419 // release by slot.
420 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
421 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800422 return err;
423 }
424
Dan Stozad723bd72014-11-18 10:24:03 -0800425 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700426 mCurrentTexture, mCurrentTextureImage != NULL ?
427 mCurrentTextureImage->graphicBufferHandle() : 0,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800428 buf, mSlots[buf].mGraphicBuffer->handle);
429
430 // release old buffer
431 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700432 status_t status = releaseBufferLocked(
Eric Penner5c3d2432014-07-11 19:08:04 -0700433 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
434 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700435 if (status < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800436 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800437 strerror(-status), status);
438 err = status;
439 // keep going, with error raised [?]
440 }
441 }
442
Andy McFadden2adaf042012-12-18 09:49:45 -0800443 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800444 mCurrentTexture = buf;
Eric Penner5c3d2432014-07-11 19:08:04 -0700445 mCurrentTextureImage = mEglSlots[buf].mEglImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800446 mCurrentCrop = item.mCrop;
447 mCurrentTransform = item.mTransform;
448 mCurrentScalingMode = item.mScalingMode;
449 mCurrentTimestamp = item.mTimestamp;
450 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700451 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800452
453 computeCurrentTransformMatrixLocked();
454
455 return err;
456}
457
Andy McFadden2adaf042012-12-18 09:49:45 -0800458status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800459 if (mEglDisplay == EGL_NO_DISPLAY) {
460 ALOGE("bindTextureImage: invalid display");
461 return INVALID_OPERATION;
462 }
463
Dan Stozad723bd72014-11-18 10:24:03 -0800464 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800465 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800466 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800467 }
468
469 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700470 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
471 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800472 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700473 return NO_INIT;
474 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800475
Eric Penner5c3d2432014-07-11 19:08:04 -0700476 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700477 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700478 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800479 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700480 mEglDisplay, mCurrentTexture);
481 return UNKNOWN_ERROR;
482 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700483 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
484
Eric Penner2d14a0e2014-08-25 20:16:37 -0700485 // In the rare case that the display is terminated and then initialized
486 // again, we can't detect that the display changed (it didn't), but the
487 // image is invalid. In this case, repeat the exact same steps while
488 // forcing the creation of a new image.
489 if ((error = glGetError()) != GL_NO_ERROR) {
490 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800491 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
492 mCurrentCrop,
493 true);
494 if (result != NO_ERROR) {
495 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700496 mEglDisplay, mCurrentTexture);
497 return UNKNOWN_ERROR;
498 }
499 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
500 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800501 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700502 return UNKNOWN_ERROR;
503 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800504 }
Andy McFadden97eba892012-12-11 15:21:45 -0800505
506 // Wait for the new buffer to be ready.
507 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800508}
509
Mathias Agopian45155962013-08-08 18:16:21 -0700510status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700511 EGLDisplay dpy = eglGetCurrentDisplay();
512 EGLContext ctx = eglGetCurrentContext();
513
Mathias Agopian45155962013-08-08 18:16:21 -0700514 if (!contextCheck) {
515 // if this is the first time we're called, mEglDisplay/mEglContext have
516 // never been set, so don't error out (below).
517 if (mEglDisplay == EGL_NO_DISPLAY) {
518 mEglDisplay = dpy;
519 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700520 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700521 mEglContext = ctx;
522 }
523 }
524
525 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800526 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700527 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700528 }
529
Mathias Agopian45155962013-08-08 18:16:21 -0700530 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800531 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700532 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700533 }
534
535 mEglDisplay = dpy;
536 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800537 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800538}
539
Jesse Hall13f01cb2013-03-20 11:37:21 -0700540void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
541 if (fence->isValid() &&
542 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700543 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700544 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700545 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800546 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700547 strerror(-err), err);
548 }
Jesse Hallef194142012-06-14 14:45:17 -0700549 }
550}
551
Andy McFadden2adaf042012-12-18 09:49:45 -0800552status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700553 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800554 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700555 Mutex::Autolock lock(mMutex);
556
557 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800558 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700559 return NO_INIT;
560 }
561
562 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800563 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700564 "context");
565 return INVALID_OPERATION;
566 }
567
568 EGLDisplay dpy = eglGetCurrentDisplay();
569 EGLContext ctx = eglGetCurrentContext();
570
571 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800572 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700573 return INVALID_OPERATION;
574 }
575
576 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800577 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 return INVALID_OPERATION;
579 }
580
581 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
582 status_t err = syncForReleaseLocked(dpy);
583 if (err != OK) {
584 return err;
585 }
586
587 glDeleteTextures(1, &mTexName);
588 }
589
590 mEglDisplay = EGL_NO_DISPLAY;
591 mEglContext = EGL_NO_CONTEXT;
592 mAttached = false;
593
594 return OK;
595}
596
Mathias Agopian3f844832013-08-07 21:24:32 -0700597status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700598 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800599 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700600 Mutex::Autolock lock(mMutex);
601
602 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800603 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700604 return NO_INIT;
605 }
606
607 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800608 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700609 "context");
610 return INVALID_OPERATION;
611 }
612
613 EGLDisplay dpy = eglGetCurrentDisplay();
614 EGLContext ctx = eglGetCurrentContext();
615
616 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800617 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700618 return INVALID_OPERATION;
619 }
620
621 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800622 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700623 return INVALID_OPERATION;
624 }
625
626 // We need to bind the texture regardless of whether there's a current
627 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700628 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700629
Jamie Gennis74bed552012-03-28 19:05:54 -0700630 mEglDisplay = dpy;
631 mEglContext = ctx;
632 mTexName = tex;
633 mAttached = true;
634
Eric Penner5c3d2432014-07-11 19:08:04 -0700635 if (mCurrentTextureImage != NULL) {
636 // This may wait for a buffer a second time. This is likely required if
637 // this is a different context, since otherwise the wait could be skipped
638 // by bouncing through another context. For the same context the extra
639 // wait is redundant.
640 status_t err = bindTextureImageLocked();
641 if (err != NO_ERROR) {
642 return err;
643 }
644 }
645
Jamie Gennis74bed552012-03-28 19:05:54 -0700646 return OK;
647}
648
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800649
Andy McFadden2adaf042012-12-18 09:49:45 -0800650status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800651 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700652
Jamie Gennis01dbf552012-09-06 14:54:19 -0700653 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700654 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700655 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
656 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
657 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800658 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700659 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700660 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700661 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700662 glFlush();
663 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700664 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700665 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800666 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700667 "fd: %#x", eglGetError());
668 return UNKNOWN_ERROR;
669 }
670 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700671 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700672 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700673 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800674 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700675 "%s (%d)", strerror(-err), err);
676 return err;
677 }
Mathias Agopianca088332013-03-28 17:44:13 -0700678 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700679 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
680 if (fence != EGL_NO_SYNC_KHR) {
681 // There is already a fence for the current slot. We need to
682 // wait on that before replacing it with another fence to
683 // ensure that all outstanding buffer accesses have completed
684 // before the producer accesses it.
685 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
686 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800687 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700688 "fence: %#x", eglGetError());
689 return UNKNOWN_ERROR;
690 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800691 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700692 "fence");
693 return TIMED_OUT;
694 }
695 eglDestroySyncKHR(dpy, fence);
696 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700697
Jamie Gennis01dbf552012-09-06 14:54:19 -0700698 // Create a fence for the outstanding accesses in the current
699 // OpenGL ES context.
700 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
701 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800702 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700703 eglGetError());
704 return UNKNOWN_ERROR;
705 }
706 glFlush();
707 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700708 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700709 }
710
711 return OK;
712}
713
Dan Stozad723bd72014-11-18 10:24:03 -0800714bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700715{
716 switch (format) {
717 // supported YUV formats
718 case HAL_PIXEL_FORMAT_YV12:
719 // Legacy/deprecated YUV formats
720 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
721 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
722 case HAL_PIXEL_FORMAT_YCbCr_422_I:
723 return true;
724 }
725
726 // Any OEM format needs to be considered
727 if (format>=0x100 && format<=0x1FF)
728 return true;
729
730 return false;
731}
732
Mathias Agopian3f844832013-08-07 21:24:32 -0700733uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700734 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700735}
736
Andy McFadden2adaf042012-12-18 09:49:45 -0800737void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800738 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700739 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
740}
741
Andy McFadden2adaf042012-12-18 09:49:45 -0800742void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700743 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700744 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800745 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700746 return;
747 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700748 bool needsRecompute = mFilteringEnabled != enabled;
749 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700750
Eric Penner5c3d2432014-07-11 19:08:04 -0700751 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800752 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700753 }
754
Eric Penner5c3d2432014-07-11 19:08:04 -0700755 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700756 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700757 }
758}
759
Andy McFadden2adaf042012-12-18 09:49:45 -0800760void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800761 GLC_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800762
Jamie Gennisa214c642011-01-14 13:53:31 -0800763 float xform[16];
764 for (int i = 0; i < 16; i++) {
765 xform[i] = mtxIdentity[i];
766 }
767 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
768 float result[16];
769 mtxMul(result, xform, mtxFlipH);
770 for (int i = 0; i < 16; i++) {
771 xform[i] = result[i];
772 }
773 }
774 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
775 float result[16];
776 mtxMul(result, xform, mtxFlipV);
777 for (int i = 0; i < 16; i++) {
778 xform[i] = result[i];
779 }
780 }
781 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
782 float result[16];
783 mtxMul(result, xform, mtxRot90);
784 for (int i = 0; i < 16; i++) {
785 xform[i] = result[i];
786 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800787 }
788
Eric Penner5c3d2432014-07-11 19:08:04 -0700789 sp<GraphicBuffer> buf = (mCurrentTextureImage == NULL) ?
790 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700791
792 if (buf == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800793 GLC_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureImage is NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700794 }
795
Jamie Gennisdbe92452013-09-23 17:22:10 -0700796 float mtxBeforeFlipV[16];
797 if (!isEglImageCroppable(mCurrentCrop)) {
798 Rect cropRect = mCurrentCrop;
799 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
800 float bufferWidth = buf->getWidth();
801 float bufferHeight = buf->getHeight();
802 if (!cropRect.isEmpty()) {
803 float shrinkAmount = 0.0f;
804 if (mFilteringEnabled) {
805 // In order to prevent bilinear sampling beyond the edge of the
806 // crop rectangle we may need to shrink it by 2 texels in each
807 // dimension. Normally this would just need to take 1/2 a texel
808 // off each end, but because the chroma channels of YUV420 images
809 // are subsampled we may need to shrink the crop region by a whole
810 // texel on each side.
811 switch (buf->getPixelFormat()) {
812 case PIXEL_FORMAT_RGBA_8888:
813 case PIXEL_FORMAT_RGBX_8888:
814 case PIXEL_FORMAT_RGB_888:
815 case PIXEL_FORMAT_RGB_565:
816 case PIXEL_FORMAT_BGRA_8888:
817 // We know there's no subsampling of any channels, so we
818 // only need to shrink by a half a pixel.
819 shrinkAmount = 0.5;
820 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700821
Jamie Gennisdbe92452013-09-23 17:22:10 -0700822 default:
823 // If we don't recognize the format, we must assume the
824 // worst case (that we care about), which is YUV420.
825 shrinkAmount = 1.0;
826 break;
827 }
828 }
829
830 // Only shrink the dimensions that are not the size of the buffer.
831 if (cropRect.width() < bufferWidth) {
832 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
833 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
834 bufferWidth;
835 }
836 if (cropRect.height() < bufferHeight) {
837 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
838 bufferHeight;
839 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
840 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700841 }
842 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700843 float crop[16] = {
844 sx, 0, 0, 0,
845 0, sy, 0, 0,
846 0, 0, 1, 0,
847 tx, ty, 0, 1,
848 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700849
Jamie Gennisdbe92452013-09-23 17:22:10 -0700850 mtxMul(mtxBeforeFlipV, crop, xform);
851 } else {
852 for (int i = 0; i < 16; i++) {
853 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700854 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800855 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800856
857 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800858 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800859 // want to expose this to applications, however, so we must add an
860 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700861 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800862}
863
Andy McFadden2adaf042012-12-18 09:49:45 -0800864nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800865 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800866 Mutex::Autolock lock(mMutex);
867 return mCurrentTimestamp;
868}
869
Dan Stozad723bd72014-11-18 10:24:03 -0800870uint64_t GLConsumer::getFrameNumber() {
871 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700872 Mutex::Autolock lock(mMutex);
873 return mCurrentFrameNumber;
874}
875
Andy McFadden2adaf042012-12-18 09:49:45 -0800876sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700877 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700878 return (mCurrentTextureImage == NULL) ?
879 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700880}
881
Andy McFadden2adaf042012-12-18 09:49:45 -0800882Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700883 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700884
885 Rect outCrop = mCurrentCrop;
886 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800887 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
888 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700889
890 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
891 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800892 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700893 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
894 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800895 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700896 }
897
Dan Stozad723bd72014-11-18 10:24:03 -0800898 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
899 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
900
Daniel Lam016c8cb2012-04-03 15:54:58 -0700901 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800902 if (newWidth < currentWidth) {
Dan Stozaabf952c2015-03-04 14:58:02 -0800903 uint32_t dw = (currentWidth - newWidth) / 2;
904 outCrop.left += dw;
905 outCrop.right -= dw;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700906 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800907 } else if (newHeight < currentHeight) {
Dan Stozaabf952c2015-03-04 14:58:02 -0800908 uint32_t dh = (currentHeight - newHeight) / 2;
909 outCrop.top += dh;
910 outCrop.bottom -= dh;
Daniel Lam016c8cb2012-04-03 15:54:58 -0700911 }
912
Dan Stozad723bd72014-11-18 10:24:03 -0800913 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700914 outCrop.left, outCrop.top,
915 outCrop.right,outCrop.bottom);
916 }
917
Daniel Lam016c8cb2012-04-03 15:54:58 -0700918 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700919}
920
Andy McFadden2adaf042012-12-18 09:49:45 -0800921uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700922 Mutex::Autolock lock(mMutex);
923 return mCurrentTransform;
924}
925
Andy McFadden2adaf042012-12-18 09:49:45 -0800926uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700927 Mutex::Autolock lock(mMutex);
928 return mCurrentScalingMode;
929}
930
Andy McFadden2adaf042012-12-18 09:49:45 -0800931sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700932 Mutex::Autolock lock(mMutex);
933 return mCurrentFence;
934}
935
Andy McFadden2adaf042012-12-18 09:49:45 -0800936status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700937 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700938 return doGLFenceWaitLocked();
939}
940
Andy McFadden2adaf042012-12-18 09:49:45 -0800941status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700942
943 EGLDisplay dpy = eglGetCurrentDisplay();
944 EGLContext ctx = eglGetCurrentContext();
945
946 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800947 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700948 return INVALID_OPERATION;
949 }
950
951 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800952 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700953 return INVALID_OPERATION;
954 }
955
Jamie Gennis1df8c342012-12-20 14:05:45 -0800956 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700957 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700958 // Create an EGLSyncKHR from the current fence.
959 int fenceFd = mCurrentFence->dup();
960 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800961 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700962 return -errno;
963 }
964 EGLint attribs[] = {
965 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
966 EGL_NONE
967 };
968 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
969 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
970 if (sync == EGL_NO_SYNC_KHR) {
971 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800972 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700973 eglGetError());
974 return UNKNOWN_ERROR;
975 }
976
977 // XXX: The spec draft is inconsistent as to whether this should
978 // return an EGLint or void. Ignore the return value for now, as
979 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700980 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700981 EGLint eglErr = eglGetError();
982 eglDestroySyncKHR(dpy, sync);
983 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800984 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700985 eglErr);
986 return UNKNOWN_ERROR;
987 }
988 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700989 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800990 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700991 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800992 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700993 return err;
994 }
995 }
996 }
997
998 return NO_ERROR;
999}
1000
Andy McFadden2adaf042012-12-18 09:49:45 -08001001void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001002 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001003 if (slotIndex == mCurrentTexture) {
1004 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1005 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001006 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001007 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001008}
Daniel Lameae59d22012-01-22 15:26:27 -08001009
Andy McFadden2adaf042012-12-18 09:49:45 -08001010void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001011 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001012 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001013 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001014}
1015
Andy McFadden2adaf042012-12-18 09:49:45 -08001016void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001017 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001018 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001019 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001020}
1021
Dan Stozad723bd72014-11-18 10:24:03 -08001022status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001023 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001024 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001025}
1026
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001027status_t GLConsumer::setDefaultBufferDataSpace(
1028 android_dataspace defaultDataSpace) {
1029 Mutex::Autolock lock(mMutex);
1030 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1031}
1032
Andy McFadden2adaf042012-12-18 09:49:45 -08001033status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001034 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001035 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001036 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001037}
1038
Andy McFadden2adaf042012-12-18 09:49:45 -08001039status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001040 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001041 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001042}
1043
Mathias Agopian74d211a2013-04-22 16:55:35 +02001044void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001045{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001046 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001047 "%smTexName=%d mCurrentTexture=%d\n"
1048 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1049 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1050 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1051 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001052
Mathias Agopian74d211a2013-04-22 16:55:35 +02001053 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001054}
1055
Jamie Gennisf238e282011-01-09 16:33:17 -08001056static void mtxMul(float out[16], const float a[16], const float b[16]) {
1057 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1058 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1059 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1060 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1061
1062 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1063 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1064 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1065 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1066
1067 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1068 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1069 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1070 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1071
1072 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1073 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1074 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1075 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1076}
1077
Eric Penner5c3d2432014-07-11 19:08:04 -07001078GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1079 mGraphicBuffer(graphicBuffer),
1080 mEglImage(EGL_NO_IMAGE_KHR),
1081 mEglDisplay(EGL_NO_DISPLAY) {
1082}
1083
1084GLConsumer::EglImage::~EglImage() {
1085 if (mEglImage != EGL_NO_IMAGE_KHR) {
1086 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1087 ALOGE("~EglImage: eglDestroyImageKHR failed");
1088 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001089 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001090 }
1091}
1092
1093status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001094 const Rect& cropRect,
1095 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001096 // If there's an image and it's no longer valid, destroy it.
1097 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1098 bool displayInvalid = mEglDisplay != eglDisplay;
1099 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001100 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001101 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1102 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1103 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001104 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001105 mEglImage = EGL_NO_IMAGE_KHR;
1106 mEglDisplay = EGL_NO_DISPLAY;
1107 }
1108
1109 // If there's no image, create one.
1110 if (mEglImage == EGL_NO_IMAGE_KHR) {
1111 mEglDisplay = eglDisplay;
1112 mCropRect = cropRect;
1113 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1114 }
1115
1116 // Fail if we can't create a valid image.
1117 if (mEglImage == EGL_NO_IMAGE_KHR) {
1118 mEglDisplay = EGL_NO_DISPLAY;
1119 mCropRect.makeInvalid();
1120 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1121 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1122 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1123 buffer->getUsage(), buffer->getPixelFormat());
1124 return UNKNOWN_ERROR;
1125 }
1126
1127 return OK;
1128}
1129
1130void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001131 glEGLImageTargetTexture2DOES(texTarget,
1132 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001133}
1134
1135EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1136 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001137 EGLClientBuffer cbuf =
1138 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Eric Penner5c3d2432014-07-11 19:08:04 -07001139 EGLint attrs[] = {
1140 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1141 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1142 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1143 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1144 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
1145 EGL_NONE,
1146 };
1147 if (!crop.isValid()) {
1148 // No crop rect to set, so terminate the attrib array before the crop.
1149 attrs[2] = EGL_NONE;
1150 } else if (!isEglImageCroppable(crop)) {
1151 // The crop rect is not at the origin, so we can't set the crop on the
1152 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1153 // extension. In the future we can add a layered extension that
1154 // removes this restriction if there is hardware that can support it.
1155 attrs[2] = EGL_NONE;
1156 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001157 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001158 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1159 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1160 if (image == EGL_NO_IMAGE_KHR) {
1161 EGLint error = eglGetError();
1162 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001163 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001164 }
1165 return image;
1166}
1167
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001168}; // namespace android