blob: cc96a92d21ac2caf0f8e7a594bbb55911322c832 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andy McFadden2adaf042012-12-18 09:49:45 -080017#define LOG_TAG "GLConsumer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070028#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029
Jamie Gennis9fea3422012-08-07 18:03:04 -070030#include <hardware/hardware.h>
31
Mathias Agopianca088332013-03-28 17:44:13 -070032#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/IGraphicBufferAlloc.h>
34#include <gui/ISurfaceComposer.h>
35#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080036
Mathias Agopian90ac7992012-02-25 18:48:35 -080037#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070038#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080039
40#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070041#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080042#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080043
Jamie Gennisdbe92452013-09-23 17:22:10 -070044EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
45#define CROP_EXT_STR "EGL_ANDROID_image_crop"
46
Andy McFadden97eba892012-12-11 15:21:45 -080047namespace android {
48
Andy McFadden2adaf042012-12-18 09:49:45 -080049// Macros for including the GLConsumer name in log messages
Steve Block6807e592011-10-20 11:56:00 +010050#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000051#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000052#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000053#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000054#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070055
Mathias Agopianad678e12013-07-23 17:28:53 -070056static const struct {
57 size_t width, height;
58 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070059} kDebugData = { 15, 12,
60 "___________________________________XX_XX_______X_X_____X_X____X_XXXXXXX_X____XXXXXXXXXXX__"
61 "___XX_XXX_XX_______XXXXXXX_________X___X_________X_____X__________________________________"
62};
Mathias Agopianad678e12013-07-23 17:28:53 -070063
Jamie Gennisf238e282011-01-09 16:33:17 -080064// Transform matrices
65static float mtxIdentity[16] = {
66 1, 0, 0, 0,
67 0, 1, 0, 0,
68 0, 0, 1, 0,
69 0, 0, 0, 1,
70};
71static float mtxFlipH[16] = {
72 -1, 0, 0, 0,
73 0, 1, 0, 0,
74 0, 0, 1, 0,
75 1, 0, 0, 1,
76};
77static float mtxFlipV[16] = {
78 1, 0, 0, 0,
79 0, -1, 0, 0,
80 0, 0, 1, 0,
81 0, 1, 0, 1,
82};
83static float mtxRot90[16] = {
84 0, 1, 0, 0,
85 -1, 0, 0, 0,
86 0, 0, 1, 0,
87 1, 0, 0, 1,
88};
Jamie Gennisf238e282011-01-09 16:33:17 -080089
90static void mtxMul(float out[16], const float a[16], const float b[16]);
91
Mathias Agopian9870c9b2013-08-08 17:46:48 -070092Mutex GLConsumer::sStaticInitLock;
93sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -070094
Jamie Gennisdbe92452013-09-23 17:22:10 -070095static bool hasEglAndroidImageCropImpl() {
96 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
97 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
98 size_t cropExtLen = strlen(CROP_EXT_STR);
99 size_t extsLen = strlen(exts);
100 bool equal = !strcmp(CROP_EXT_STR, exts);
101 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
102 bool atEnd = (cropExtLen+1) < extsLen &&
103 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
104 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
105 return equal || atStart || atEnd || inMiddle;
106}
107
108static bool hasEglAndroidImageCrop() {
109 // Only compute whether the extension is present once the first time this
110 // function is called.
111 static bool hasIt = hasEglAndroidImageCropImpl();
112 return hasIt;
113}
114
115static bool isEglImageCroppable(const Rect& crop) {
116 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
117}
118
Mathias Agopian3f844832013-08-07 21:24:32 -0700119GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
120 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700121 ConsumerBase(bq, isControlledByApp),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800122 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200123 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800124 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800125 mCurrentTimestamp(0),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700126 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200127 mDefaultWidth(1),
128 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700129 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700130 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800131 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800132 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700133 mEglDisplay(EGL_NO_DISPLAY),
134 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700135 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
136 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800137{
Andy McFadden2adaf042012-12-18 09:49:45 -0800138 ST_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800139
Jamie Gennisfa28c352011-09-16 17:30:26 -0700140 memcpy(mCurrentTransformMatrix, mtxIdentity,
141 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700142
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700143 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800144}
145
Dan Stozaab574912014-06-25 14:21:45 -0700146GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
147 bool useFenceSync, bool isControlledByApp) :
148 ConsumerBase(bq, isControlledByApp),
149 mCurrentTransform(0),
150 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
151 mCurrentFence(Fence::NO_FENCE),
152 mCurrentTimestamp(0),
153 mCurrentFrameNumber(0),
154 mDefaultWidth(1),
155 mDefaultHeight(1),
156 mFilteringEnabled(true),
157 mTexName(-1),
158 mUseFenceSync(useFenceSync),
159 mTexTarget(texTarget),
160 mEglDisplay(EGL_NO_DISPLAY),
161 mEglContext(EGL_NO_CONTEXT),
162 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
163 mAttached(false)
164{
165 ST_LOGV("GLConsumer");
166
167 memcpy(mCurrentTransformMatrix, mtxIdentity,
168 sizeof(mCurrentTransformMatrix));
169
170 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
171}
172
Andy McFadden2adaf042012-12-18 09:49:45 -0800173status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700174 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700175 return mConsumer->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700176}
177
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178
Andy McFadden2adaf042012-12-18 09:49:45 -0800179status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700180{
Daniel Lamb2675792012-02-23 14:35:13 -0800181 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700182 mDefaultWidth = w;
183 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700184 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700185}
186
Andy McFadden2adaf042012-12-18 09:49:45 -0800187status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800188 ATRACE_CALL();
189 ST_LOGV("updateTexImage");
190 Mutex::Autolock lock(mMutex);
191
192 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800193 ST_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800194 return NO_INIT;
195 }
196
197 // Make sure the EGL state is the same as in previous calls.
198 status_t err = checkAndUpdateEglStateLocked();
199 if (err != NO_ERROR) {
200 return err;
201 }
202
203 BufferQueue::BufferItem item;
204
205 // Acquire the next buffer.
206 // In asynchronous mode the list is guaranteed to be one buffer
207 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700208 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800209 if (err != NO_ERROR) {
210 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
211 // We always bind the texture even if we don't update its contents.
212 ST_LOGV("updateTexImage: no buffers were available");
213 glBindTexture(mTexTarget, mTexName);
214 err = NO_ERROR;
215 } else {
216 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
217 strerror(-err), err);
218 }
219 return err;
220 }
221
222 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700223 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800224 if (err != NO_ERROR) {
225 // We always bind the texture.
226 glBindTexture(mTexTarget, mTexName);
227 return err;
228 }
229
Andy McFadden97eba892012-12-11 15:21:45 -0800230 // Bind the new buffer to the GL texture, and wait until it's ready.
231 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700232}
233
Mathias Agopianad678e12013-07-23 17:28:53 -0700234
235status_t GLConsumer::releaseTexImage() {
236 ATRACE_CALL();
237 ST_LOGV("releaseTexImage");
238 Mutex::Autolock lock(mMutex);
239
240 if (mAbandoned) {
241 ST_LOGE("releaseTexImage: GLConsumer is abandoned!");
242 return NO_INIT;
243 }
244
245 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700246 status_t err = NO_ERROR;
247
248 if (mAttached) {
249 err = checkAndUpdateEglStateLocked(true);
250 if (err != NO_ERROR) {
251 return err;
252 }
253 } else {
254 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700255 }
256
257 // Update the GLConsumer state.
258 int buf = mCurrentTexture;
259 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
260
Mathias Agopian45155962013-08-08 18:16:21 -0700261 ST_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700262
Mathias Agopian45155962013-08-08 18:16:21 -0700263 if (mAttached) {
264 // Do whatever sync ops we need to do before releasing the slot.
265 err = syncForReleaseLocked(mEglDisplay);
266 if (err != NO_ERROR) {
267 ST_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
268 return err;
269 }
270 } else {
271 // if we're detached, we just use the fence that was created in detachFromContext()
272 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700273 }
274
Mathias Agopian45155962013-08-08 18:16:21 -0700275 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700276 if (err < NO_ERROR) {
277 ST_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
278 strerror(-err), err);
279 return err;
280 }
281
Eric Penner5c3d2432014-07-11 19:08:04 -0700282 if (mReleasedTexImage == NULL) {
283 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
284 }
285
Mathias Agopianad678e12013-07-23 17:28:53 -0700286 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700287 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700288 mCurrentCrop.makeInvalid();
289 mCurrentTransform = 0;
290 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
291 mCurrentTimestamp = 0;
292 mCurrentFence = Fence::NO_FENCE;
293
Mathias Agopian45155962013-08-08 18:16:21 -0700294 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700295 // This binds a dummy buffer (mReleasedTexImage).
296 status_t err = bindTextureImageLocked();
297 if (err != NO_ERROR) {
298 return err;
299 }
Mathias Agopian45155962013-08-08 18:16:21 -0700300 } else {
301 // detached, don't touch the texture (and we may not even have an
302 // EGLDisplay here.
303 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700304 }
305
306 return NO_ERROR;
307}
308
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700309sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
310 Mutex::Autolock _l(sStaticInitLock);
311 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
312 // The first time, create the debug texture in case the application
313 // continues to use it.
314 sp<GraphicBuffer> buffer = new GraphicBuffer(
315 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
316 GraphicBuffer::USAGE_SW_WRITE_RARELY);
317 uint32_t* bits;
318 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
319 size_t w = buffer->getStride();
320 size_t h = buffer->getHeight();
321 memset(bits, 0, w*h*4);
322 for (size_t y=0 ; y<kDebugData.height ; y++) {
323 for (size_t x=0 ; x<kDebugData.width ; x++) {
324 bits[x] = (kDebugData.bits[y*kDebugData.width+x] == 'X') ? 0xFF000000 : 0xFFFFFFFF;
325 }
326 bits += w;
327 }
328 buffer->unlock();
329 sReleasedTexImageBuffer = buffer;
330 }
331 return sReleasedTexImageBuffer;
332}
333
Andy McFadden1585c4d2013-06-28 13:52:40 -0700334status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item,
335 nsecs_t presentWhen) {
336 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700337 if (err != NO_ERROR) {
338 return err;
339 }
340
Eric Penner5c3d2432014-07-11 19:08:04 -0700341 // If item->mGraphicBuffer is not null, this buffer has not been acquired
342 // before, so any prior EglImage created is using a stale buffer. This
343 // replaces any old EglImage with a new one (using the new buffer).
344 if (item->mGraphicBuffer != NULL) {
345 int slot = item->mBuf;
346 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700347 }
348
Jamie Gennis9fea3422012-08-07 18:03:04 -0700349 return NO_ERROR;
350}
351
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700352status_t GLConsumer::releaseBufferLocked(int buf,
353 sp<GraphicBuffer> graphicBuffer,
354 EGLDisplay display, EGLSyncKHR eglFence) {
355 // release the buffer if it hasn't already been discarded by the
356 // BufferQueue. This can happen, for example, when the producer of this
357 // buffer has reallocated the original buffer slot after this buffer
358 // was acquired.
359 status_t err = ConsumerBase::releaseBufferLocked(
360 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700361 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700362 return err;
363}
364
Mathias Agopianad678e12013-07-23 17:28:53 -0700365status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800366{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700367 status_t err = NO_ERROR;
368
Andy McFadden87a67842014-04-04 15:37:08 -0700369 int buf = item.mBuf;
370
Jamie Gennis74bed552012-03-28 19:05:54 -0700371 if (!mAttached) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700372 ST_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700373 "ES context");
Andy McFadden87a67842014-04-04 15:37:08 -0700374 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
375 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700376 return INVALID_OPERATION;
377 }
378
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800379 // Confirm state.
380 err = checkAndUpdateEglStateLocked();
381 if (err != NO_ERROR) {
Andy McFadden87a67842014-04-04 15:37:08 -0700382 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
383 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800384 return err;
385 }
386
Eric Penner5c3d2432014-07-11 19:08:04 -0700387 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
388 // if nessessary, for the gralloc buffer currently in the slot in
389 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800390 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700391 // means the buffer was previously acquired).
392 err = mEglSlots[buf].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
393 if (err != NO_ERROR) {
394 ST_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
395 mEglDisplay, buf);
396 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
397 mEglDisplay, EGL_NO_SYNC_KHR);
398 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800399 }
400
401 // Do whatever sync ops we need to do before releasing the old slot.
402 err = syncForReleaseLocked(mEglDisplay);
403 if (err != NO_ERROR) {
404 // Release the buffer we just acquired. It's not safe to
405 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700406 // As we are still under lock since acquireBuffer, it is safe to
407 // release by slot.
408 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
409 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800410 return err;
411 }
412
Mathias Agopianad678e12013-07-23 17:28:53 -0700413 ST_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700414 mCurrentTexture, mCurrentTextureImage != NULL ?
415 mCurrentTextureImage->graphicBufferHandle() : 0,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800416 buf, mSlots[buf].mGraphicBuffer->handle);
417
418 // release old buffer
419 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700420 status_t status = releaseBufferLocked(
Eric Penner5c3d2432014-07-11 19:08:04 -0700421 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
422 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700423 if (status < NO_ERROR) {
424 ST_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800425 strerror(-status), status);
426 err = status;
427 // keep going, with error raised [?]
428 }
429 }
430
Andy McFadden2adaf042012-12-18 09:49:45 -0800431 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800432 mCurrentTexture = buf;
Eric Penner5c3d2432014-07-11 19:08:04 -0700433 mCurrentTextureImage = mEglSlots[buf].mEglImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800434 mCurrentCrop = item.mCrop;
435 mCurrentTransform = item.mTransform;
436 mCurrentScalingMode = item.mScalingMode;
437 mCurrentTimestamp = item.mTimestamp;
438 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700439 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800440
441 computeCurrentTransformMatrixLocked();
442
443 return err;
444}
445
Andy McFadden2adaf042012-12-18 09:49:45 -0800446status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800447 if (mEglDisplay == EGL_NO_DISPLAY) {
448 ALOGE("bindTextureImage: invalid display");
449 return INVALID_OPERATION;
450 }
451
452 GLint error;
453 while ((error = glGetError()) != GL_NO_ERROR) {
454 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
455 }
456
457 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700458 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
459 mCurrentTextureImage == NULL) {
460 ST_LOGE("bindTextureImage: no currently-bound texture");
461 return NO_INIT;
462 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800463
Eric Penner5c3d2432014-07-11 19:08:04 -0700464 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700465 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700466 if (err != NO_ERROR) {
467 ST_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
468 mEglDisplay, mCurrentTexture);
469 return UNKNOWN_ERROR;
470 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700471 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
472
Eric Penner2d14a0e2014-08-25 20:16:37 -0700473 // In the rare case that the display is terminated and then initialized
474 // again, we can't detect that the display changed (it didn't), but the
475 // image is invalid. In this case, repeat the exact same steps while
476 // forcing the creation of a new image.
477 if ((error = glGetError()) != GL_NO_ERROR) {
478 glBindTexture(mTexTarget, mTexName);
479 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
480 mCurrentCrop,
481 true);
482 if (err != NO_ERROR) {
483 ST_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
484 mEglDisplay, mCurrentTexture);
485 return UNKNOWN_ERROR;
486 }
487 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
488 if ((error = glGetError()) != GL_NO_ERROR) {
489 ST_LOGE("bindTextureImage: error binding external image: %#04x", error);
490 return UNKNOWN_ERROR;
491 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800492 }
Andy McFadden97eba892012-12-11 15:21:45 -0800493
494 // Wait for the new buffer to be ready.
495 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800496}
497
Mathias Agopian45155962013-08-08 18:16:21 -0700498status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700499 EGLDisplay dpy = eglGetCurrentDisplay();
500 EGLContext ctx = eglGetCurrentContext();
501
Mathias Agopian45155962013-08-08 18:16:21 -0700502 if (!contextCheck) {
503 // if this is the first time we're called, mEglDisplay/mEglContext have
504 // never been set, so don't error out (below).
505 if (mEglDisplay == EGL_NO_DISPLAY) {
506 mEglDisplay = dpy;
507 }
508 if (mEglContext == EGL_NO_DISPLAY) {
509 mEglContext = ctx;
510 }
511 }
512
513 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800514 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700515 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700516 }
517
Mathias Agopian45155962013-08-08 18:16:21 -0700518 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800519 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700520 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700521 }
522
523 mEglDisplay = dpy;
524 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800525 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800526}
527
Jesse Hall13f01cb2013-03-20 11:37:21 -0700528void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
529 if (fence->isValid() &&
530 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700531 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700532 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700533 if (err != OK) {
534 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
535 strerror(-err), err);
536 }
Jesse Hallef194142012-06-14 14:45:17 -0700537 }
538}
539
Andy McFadden2adaf042012-12-18 09:49:45 -0800540status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700541 ATRACE_CALL();
542 ST_LOGV("detachFromContext");
543 Mutex::Autolock lock(mMutex);
544
545 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800546 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700547 return NO_INIT;
548 }
549
550 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800551 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700552 "context");
553 return INVALID_OPERATION;
554 }
555
556 EGLDisplay dpy = eglGetCurrentDisplay();
557 EGLContext ctx = eglGetCurrentContext();
558
559 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
560 ST_LOGE("detachFromContext: invalid current EGLDisplay");
561 return INVALID_OPERATION;
562 }
563
564 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
565 ST_LOGE("detachFromContext: invalid current EGLContext");
566 return INVALID_OPERATION;
567 }
568
569 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
570 status_t err = syncForReleaseLocked(dpy);
571 if (err != OK) {
572 return err;
573 }
574
575 glDeleteTextures(1, &mTexName);
576 }
577
578 mEglDisplay = EGL_NO_DISPLAY;
579 mEglContext = EGL_NO_CONTEXT;
580 mAttached = false;
581
582 return OK;
583}
584
Mathias Agopian3f844832013-08-07 21:24:32 -0700585status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700586 ATRACE_CALL();
587 ST_LOGV("attachToContext");
588 Mutex::Autolock lock(mMutex);
589
590 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800591 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700592 return NO_INIT;
593 }
594
595 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800596 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700597 "context");
598 return INVALID_OPERATION;
599 }
600
601 EGLDisplay dpy = eglGetCurrentDisplay();
602 EGLContext ctx = eglGetCurrentContext();
603
604 if (dpy == EGL_NO_DISPLAY) {
605 ST_LOGE("attachToContext: invalid current EGLDisplay");
606 return INVALID_OPERATION;
607 }
608
609 if (ctx == EGL_NO_CONTEXT) {
610 ST_LOGE("attachToContext: invalid current EGLContext");
611 return INVALID_OPERATION;
612 }
613
614 // We need to bind the texture regardless of whether there's a current
615 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700616 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700617
Jamie Gennis74bed552012-03-28 19:05:54 -0700618 mEglDisplay = dpy;
619 mEglContext = ctx;
620 mTexName = tex;
621 mAttached = true;
622
Eric Penner5c3d2432014-07-11 19:08:04 -0700623 if (mCurrentTextureImage != NULL) {
624 // This may wait for a buffer a second time. This is likely required if
625 // this is a different context, since otherwise the wait could be skipped
626 // by bouncing through another context. For the same context the extra
627 // wait is redundant.
628 status_t err = bindTextureImageLocked();
629 if (err != NO_ERROR) {
630 return err;
631 }
632 }
633
Jamie Gennis74bed552012-03-28 19:05:54 -0700634 return OK;
635}
636
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800637
Andy McFadden2adaf042012-12-18 09:49:45 -0800638status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700639 ST_LOGV("syncForReleaseLocked");
640
Jamie Gennis01dbf552012-09-06 14:54:19 -0700641 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700642 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700643 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
644 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
645 if (sync == EGL_NO_SYNC_KHR) {
646 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
647 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700648 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700649 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700650 glFlush();
651 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700652 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700653 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
654 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
655 "fd: %#x", eglGetError());
656 return UNKNOWN_ERROR;
657 }
658 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700659 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700660 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700661 if (err != OK) {
662 ST_LOGE("syncForReleaseLocked: error adding release fence: "
663 "%s (%d)", strerror(-err), err);
664 return err;
665 }
Mathias Agopianca088332013-03-28 17:44:13 -0700666 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700667 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
668 if (fence != EGL_NO_SYNC_KHR) {
669 // There is already a fence for the current slot. We need to
670 // wait on that before replacing it with another fence to
671 // ensure that all outstanding buffer accesses have completed
672 // before the producer accesses it.
673 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
674 if (result == EGL_FALSE) {
675 ST_LOGE("syncForReleaseLocked: error waiting for previous "
676 "fence: %#x", eglGetError());
677 return UNKNOWN_ERROR;
678 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
679 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
680 "fence");
681 return TIMED_OUT;
682 }
683 eglDestroySyncKHR(dpy, fence);
684 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700685
Jamie Gennis01dbf552012-09-06 14:54:19 -0700686 // Create a fence for the outstanding accesses in the current
687 // OpenGL ES context.
688 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
689 if (fence == EGL_NO_SYNC_KHR) {
690 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
691 eglGetError());
692 return UNKNOWN_ERROR;
693 }
694 glFlush();
695 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700696 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700697 }
698
699 return OK;
700}
701
Andy McFadden2adaf042012-12-18 09:49:45 -0800702bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700703{
704 switch (format) {
705 // supported YUV formats
706 case HAL_PIXEL_FORMAT_YV12:
707 // Legacy/deprecated YUV formats
708 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
709 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
710 case HAL_PIXEL_FORMAT_YCbCr_422_I:
711 return true;
712 }
713
714 // Any OEM format needs to be considered
715 if (format>=0x100 && format<=0x1FF)
716 return true;
717
718 return false;
719}
720
Mathias Agopian3f844832013-08-07 21:24:32 -0700721uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700722 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700723}
724
Andy McFadden2adaf042012-12-18 09:49:45 -0800725void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800726 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700727 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
728}
729
Andy McFadden2adaf042012-12-18 09:49:45 -0800730void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700731 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700732 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800733 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700734 return;
735 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700736 bool needsRecompute = mFilteringEnabled != enabled;
737 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700738
Eric Penner5c3d2432014-07-11 19:08:04 -0700739 if (needsRecompute && mCurrentTextureImage==NULL) {
740 ST_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700741 }
742
Eric Penner5c3d2432014-07-11 19:08:04 -0700743 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700744 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700745 }
746}
747
Andy McFadden2adaf042012-12-18 09:49:45 -0800748void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700749 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800750
Jamie Gennisa214c642011-01-14 13:53:31 -0800751 float xform[16];
752 for (int i = 0; i < 16; i++) {
753 xform[i] = mtxIdentity[i];
754 }
755 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
756 float result[16];
757 mtxMul(result, xform, mtxFlipH);
758 for (int i = 0; i < 16; i++) {
759 xform[i] = result[i];
760 }
761 }
762 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
763 float result[16];
764 mtxMul(result, xform, mtxFlipV);
765 for (int i = 0; i < 16; i++) {
766 xform[i] = result[i];
767 }
768 }
769 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
770 float result[16];
771 mtxMul(result, xform, mtxRot90);
772 for (int i = 0; i < 16; i++) {
773 xform[i] = result[i];
774 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800775 }
776
Eric Penner5c3d2432014-07-11 19:08:04 -0700777 sp<GraphicBuffer> buf = (mCurrentTextureImage == NULL) ?
778 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700779
780 if (buf == NULL) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700781 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureImage is NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700782 }
783
Jamie Gennisdbe92452013-09-23 17:22:10 -0700784 float mtxBeforeFlipV[16];
785 if (!isEglImageCroppable(mCurrentCrop)) {
786 Rect cropRect = mCurrentCrop;
787 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
788 float bufferWidth = buf->getWidth();
789 float bufferHeight = buf->getHeight();
790 if (!cropRect.isEmpty()) {
791 float shrinkAmount = 0.0f;
792 if (mFilteringEnabled) {
793 // In order to prevent bilinear sampling beyond the edge of the
794 // crop rectangle we may need to shrink it by 2 texels in each
795 // dimension. Normally this would just need to take 1/2 a texel
796 // off each end, but because the chroma channels of YUV420 images
797 // are subsampled we may need to shrink the crop region by a whole
798 // texel on each side.
799 switch (buf->getPixelFormat()) {
800 case PIXEL_FORMAT_RGBA_8888:
801 case PIXEL_FORMAT_RGBX_8888:
802 case PIXEL_FORMAT_RGB_888:
803 case PIXEL_FORMAT_RGB_565:
804 case PIXEL_FORMAT_BGRA_8888:
805 // We know there's no subsampling of any channels, so we
806 // only need to shrink by a half a pixel.
807 shrinkAmount = 0.5;
808 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700809
Jamie Gennisdbe92452013-09-23 17:22:10 -0700810 default:
811 // If we don't recognize the format, we must assume the
812 // worst case (that we care about), which is YUV420.
813 shrinkAmount = 1.0;
814 break;
815 }
816 }
817
818 // Only shrink the dimensions that are not the size of the buffer.
819 if (cropRect.width() < bufferWidth) {
820 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
821 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
822 bufferWidth;
823 }
824 if (cropRect.height() < bufferHeight) {
825 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
826 bufferHeight;
827 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
828 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700829 }
830 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700831 float crop[16] = {
832 sx, 0, 0, 0,
833 0, sy, 0, 0,
834 0, 0, 1, 0,
835 tx, ty, 0, 1,
836 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700837
Jamie Gennisdbe92452013-09-23 17:22:10 -0700838 mtxMul(mtxBeforeFlipV, crop, xform);
839 } else {
840 for (int i = 0; i < 16; i++) {
841 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700842 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800843 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800844
845 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800846 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800847 // want to expose this to applications, however, so we must add an
848 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700849 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800850}
851
Andy McFadden2adaf042012-12-18 09:49:45 -0800852nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700853 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800854 Mutex::Autolock lock(mMutex);
855 return mCurrentTimestamp;
856}
857
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700858nsecs_t GLConsumer::getFrameNumber() {
859 ST_LOGV("getFrameNumber");
860 Mutex::Autolock lock(mMutex);
861 return mCurrentFrameNumber;
862}
863
Andy McFadden2adaf042012-12-18 09:49:45 -0800864sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700865 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700866 return (mCurrentTextureImage == NULL) ?
867 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700868}
869
Andy McFadden2adaf042012-12-18 09:49:45 -0800870Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700871 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700872
873 Rect outCrop = mCurrentCrop;
874 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
875 int32_t newWidth = mCurrentCrop.width();
876 int32_t newHeight = mCurrentCrop.height();
877
878 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
879 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
880 ST_LOGV("too wide: newWidth = %d", newWidth);
881 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
882 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
883 ST_LOGV("too tall: newHeight = %d", newHeight);
884 }
885
886 // The crop is too wide
887 if (newWidth < mCurrentCrop.width()) {
888 int32_t dw = (newWidth - mCurrentCrop.width())/2;
889 outCrop.left -=dw;
890 outCrop.right += dw;
891 // The crop is too tall
892 } else if (newHeight < mCurrentCrop.height()) {
893 int32_t dh = (newHeight - mCurrentCrop.height())/2;
894 outCrop.top -= dh;
895 outCrop.bottom += dh;
896 }
897
898 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
899 outCrop.left, outCrop.top,
900 outCrop.right,outCrop.bottom);
901 }
902
Daniel Lam016c8cb2012-04-03 15:54:58 -0700903 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700904}
905
Andy McFadden2adaf042012-12-18 09:49:45 -0800906uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700907 Mutex::Autolock lock(mMutex);
908 return mCurrentTransform;
909}
910
Andy McFadden2adaf042012-12-18 09:49:45 -0800911uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700912 Mutex::Autolock lock(mMutex);
913 return mCurrentScalingMode;
914}
915
Andy McFadden2adaf042012-12-18 09:49:45 -0800916sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700917 Mutex::Autolock lock(mMutex);
918 return mCurrentFence;
919}
920
Andy McFadden2adaf042012-12-18 09:49:45 -0800921status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700922 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700923 return doGLFenceWaitLocked();
924}
925
Andy McFadden2adaf042012-12-18 09:49:45 -0800926status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700927
928 EGLDisplay dpy = eglGetCurrentDisplay();
929 EGLContext ctx = eglGetCurrentContext();
930
931 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
932 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
933 return INVALID_OPERATION;
934 }
935
936 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
937 ST_LOGE("doGLFenceWait: invalid current EGLContext");
938 return INVALID_OPERATION;
939 }
940
Jamie Gennis1df8c342012-12-20 14:05:45 -0800941 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700942 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700943 // Create an EGLSyncKHR from the current fence.
944 int fenceFd = mCurrentFence->dup();
945 if (fenceFd == -1) {
946 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
947 return -errno;
948 }
949 EGLint attribs[] = {
950 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
951 EGL_NONE
952 };
953 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
954 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
955 if (sync == EGL_NO_SYNC_KHR) {
956 close(fenceFd);
957 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
958 eglGetError());
959 return UNKNOWN_ERROR;
960 }
961
962 // XXX: The spec draft is inconsistent as to whether this should
963 // return an EGLint or void. Ignore the return value for now, as
964 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700965 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700966 EGLint eglErr = eglGetError();
967 eglDestroySyncKHR(dpy, sync);
968 if (eglErr != EGL_SUCCESS) {
969 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
970 eglErr);
971 return UNKNOWN_ERROR;
972 }
973 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700974 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800975 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700976 if (err != NO_ERROR) {
977 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
978 return err;
979 }
980 }
981 }
982
983 return NO_ERROR;
984}
985
Andy McFadden2adaf042012-12-18 09:49:45 -0800986void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700987 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700988 if (slotIndex == mCurrentTexture) {
989 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
990 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700991 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700992 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700993}
Daniel Lameae59d22012-01-22 15:26:27 -0800994
Andy McFadden2adaf042012-12-18 09:49:45 -0800995void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700996 ST_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -0700997 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700998 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700999}
1000
Andy McFadden2adaf042012-12-18 09:49:45 -08001001void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001002 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001003 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001004 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001005}
1006
Andy McFadden2adaf042012-12-18 09:49:45 -08001007status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001008 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001009 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001010}
1011
Andy McFadden2adaf042012-12-18 09:49:45 -08001012status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001013 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001014 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001015 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001016}
1017
Andy McFadden2adaf042012-12-18 09:49:45 -08001018status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001019 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001020 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001021}
1022
Mathias Agopian74d211a2013-04-22 16:55:35 +02001023void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001024{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001025 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001026 "%smTexName=%d mCurrentTexture=%d\n"
1027 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1028 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1029 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1030 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001031
Mathias Agopian74d211a2013-04-22 16:55:35 +02001032 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001033}
1034
Jamie Gennisf238e282011-01-09 16:33:17 -08001035static void mtxMul(float out[16], const float a[16], const float b[16]) {
1036 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1037 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1038 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1039 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1040
1041 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1042 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1043 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1044 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1045
1046 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1047 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1048 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1049 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1050
1051 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1052 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1053 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1054 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1055}
1056
Eric Penner5c3d2432014-07-11 19:08:04 -07001057GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1058 mGraphicBuffer(graphicBuffer),
1059 mEglImage(EGL_NO_IMAGE_KHR),
1060 mEglDisplay(EGL_NO_DISPLAY) {
1061}
1062
1063GLConsumer::EglImage::~EglImage() {
1064 if (mEglImage != EGL_NO_IMAGE_KHR) {
1065 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1066 ALOGE("~EglImage: eglDestroyImageKHR failed");
1067 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001068 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001069 }
1070}
1071
1072status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001073 const Rect& cropRect,
1074 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001075 // If there's an image and it's no longer valid, destroy it.
1076 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1077 bool displayInvalid = mEglDisplay != eglDisplay;
1078 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001079 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001080 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1081 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1082 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001083 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001084 mEglImage = EGL_NO_IMAGE_KHR;
1085 mEglDisplay = EGL_NO_DISPLAY;
1086 }
1087
1088 // If there's no image, create one.
1089 if (mEglImage == EGL_NO_IMAGE_KHR) {
1090 mEglDisplay = eglDisplay;
1091 mCropRect = cropRect;
1092 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1093 }
1094
1095 // Fail if we can't create a valid image.
1096 if (mEglImage == EGL_NO_IMAGE_KHR) {
1097 mEglDisplay = EGL_NO_DISPLAY;
1098 mCropRect.makeInvalid();
1099 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1100 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1101 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1102 buffer->getUsage(), buffer->getPixelFormat());
1103 return UNKNOWN_ERROR;
1104 }
1105
1106 return OK;
1107}
1108
1109void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
1110 glEGLImageTargetTexture2DOES(texTarget, (GLeglImageOES)mEglImage);
1111}
1112
1113EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1114 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
1115 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
1116 EGLint attrs[] = {
1117 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1118 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1119 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1120 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1121 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
1122 EGL_NONE,
1123 };
1124 if (!crop.isValid()) {
1125 // No crop rect to set, so terminate the attrib array before the crop.
1126 attrs[2] = EGL_NONE;
1127 } else if (!isEglImageCroppable(crop)) {
1128 // The crop rect is not at the origin, so we can't set the crop on the
1129 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1130 // extension. In the future we can add a layered extension that
1131 // removes this restriction if there is hardware that can support it.
1132 attrs[2] = EGL_NONE;
1133 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001134 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001135 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1136 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1137 if (image == EGL_NO_IMAGE_KHR) {
1138 EGLint error = eglGetError();
1139 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001140 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001141 }
1142 return image;
1143}
1144
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001145}; // namespace android