blob: bec0f9024a854ea2e92f6d490ce953d4afcbf167 [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>
28
Jamie Gennis9fea3422012-08-07 18:03:04 -070029#include <hardware/hardware.h>
30
Mathias Agopianca088332013-03-28 17:44:13 -070031#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080032#include <gui/IGraphicBufferAlloc.h>
33#include <gui/ISurfaceComposer.h>
34#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080035
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070037#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038
39#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070040#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080041#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080042
Andy McFadden97eba892012-12-11 15:21:45 -080043namespace android {
44
Andy McFadden2adaf042012-12-18 09:49:45 -080045// Macros for including the GLConsumer name in log messages
Steve Block6807e592011-10-20 11:56:00 +010046#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000047#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000048#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000049#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000050#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070051
Jamie Gennisf238e282011-01-09 16:33:17 -080052// Transform matrices
53static float mtxIdentity[16] = {
54 1, 0, 0, 0,
55 0, 1, 0, 0,
56 0, 0, 1, 0,
57 0, 0, 0, 1,
58};
59static float mtxFlipH[16] = {
60 -1, 0, 0, 0,
61 0, 1, 0, 0,
62 0, 0, 1, 0,
63 1, 0, 0, 1,
64};
65static float mtxFlipV[16] = {
66 1, 0, 0, 0,
67 0, -1, 0, 0,
68 0, 0, 1, 0,
69 0, 1, 0, 1,
70};
71static float mtxRot90[16] = {
72 0, 1, 0, 0,
73 -1, 0, 0, 0,
74 0, 0, 1, 0,
75 1, 0, 0, 1,
76};
Jamie Gennisf238e282011-01-09 16:33:17 -080077
78static void mtxMul(float out[16], const float a[16], const float b[16]);
79
Jamie Gennisfa28c352011-09-16 17:30:26 -070080
Andy McFadden2adaf042012-12-18 09:49:45 -080081GLConsumer::GLConsumer(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -080082 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070083 ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080084 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +020085 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -080086 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080087 mCurrentTimestamp(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +020088 mDefaultWidth(1),
89 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -070090 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070091 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -080092 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -080093 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -070094 mEglDisplay(EGL_NO_DISPLAY),
95 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -070096 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
97 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -080098{
Andy McFadden2adaf042012-12-18 09:49:45 -080099 ST_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800100
Jamie Gennisfa28c352011-09-16 17:30:26 -0700101 memcpy(mCurrentTransformMatrix, mtxIdentity,
102 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700103
Jamie Gennis9fea3422012-08-07 18:03:04 -0700104 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800105}
106
Andy McFadden2adaf042012-12-18 09:49:45 -0800107status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700108 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -0700109 return mBufferQueue->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700110}
111
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800112
Andy McFadden2adaf042012-12-18 09:49:45 -0800113status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700114{
Daniel Lamb2675792012-02-23 14:35:13 -0800115 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700116 mDefaultWidth = w;
117 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800118 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700119}
120
Andy McFadden2adaf042012-12-18 09:49:45 -0800121status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800122 ATRACE_CALL();
123 ST_LOGV("updateTexImage");
124 Mutex::Autolock lock(mMutex);
125
126 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800127 ST_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800128 return NO_INIT;
129 }
130
131 // Make sure the EGL state is the same as in previous calls.
132 status_t err = checkAndUpdateEglStateLocked();
133 if (err != NO_ERROR) {
134 return err;
135 }
136
137 BufferQueue::BufferItem item;
138
139 // Acquire the next buffer.
140 // In asynchronous mode the list is guaranteed to be one buffer
141 // deep, while in synchronous mode we use the oldest buffer.
142 err = acquireBufferLocked(&item);
143 if (err != NO_ERROR) {
144 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
145 // We always bind the texture even if we don't update its contents.
146 ST_LOGV("updateTexImage: no buffers were available");
147 glBindTexture(mTexTarget, mTexName);
148 err = NO_ERROR;
149 } else {
150 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
151 strerror(-err), err);
152 }
153 return err;
154 }
155
156 // Release the previous buffer.
157 err = releaseAndUpdateLocked(item);
158 if (err != NO_ERROR) {
159 // We always bind the texture.
160 glBindTexture(mTexTarget, mTexName);
161 return err;
162 }
163
Andy McFadden97eba892012-12-11 15:21:45 -0800164 // Bind the new buffer to the GL texture, and wait until it's ready.
165 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700166}
167
Andy McFadden2adaf042012-12-18 09:49:45 -0800168status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700169 status_t err = ConsumerBase::acquireBufferLocked(item);
170 if (err != NO_ERROR) {
171 return err;
172 }
173
174 int slot = item->mBuf;
175 if (item->mGraphicBuffer != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800176 // This buffer has not been acquired before, so we must assume
177 // that any EGLImage in mEglSlots is stale.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700178 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800179 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
180 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
181 slot);
182 // keep going
183 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700184 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
185 }
186 }
187
Jamie Gennis9fea3422012-08-07 18:03:04 -0700188 return NO_ERROR;
189}
190
Andy McFadden2adaf042012-12-18 09:49:45 -0800191status_t GLConsumer::releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700192 EGLSyncKHR eglFence) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800193 status_t err = ConsumerBase::releaseBufferLocked(buf, display, eglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700194
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700195 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700196
197 return err;
198}
199
Andy McFadden2adaf042012-12-18 09:49:45 -0800200status_t GLConsumer::releaseAndUpdateLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800201{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700202 status_t err = NO_ERROR;
203
Jamie Gennis74bed552012-03-28 19:05:54 -0700204 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800205 ST_LOGE("releaseAndUpdate: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700206 "ES context");
207 return INVALID_OPERATION;
208 }
209
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800210 // Confirm state.
211 err = checkAndUpdateEglStateLocked();
212 if (err != NO_ERROR) {
213 return err;
214 }
215
216 int buf = item.mBuf;
217
218 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
219 // buffer currently in the slot in ConsumerBase.
220 //
221 // We may have to do this even when item.mGraphicBuffer == NULL (which
222 // means the buffer was previously acquired), if we destroyed the
223 // EGLImage when detaching from a context but the buffer has not been
224 // re-allocated.
225 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
226 EGLImageKHR image = createImage(mEglDisplay, mSlots[buf].mGraphicBuffer);
227 if (image == EGL_NO_IMAGE_KHR) {
228 ST_LOGW("releaseAndUpdate: unable to createImage on display=%p slot=%d",
229 mEglDisplay, buf);
230 return UNKNOWN_ERROR;
231 }
232 mEglSlots[buf].mEglImage = image;
233 }
234
235 // Do whatever sync ops we need to do before releasing the old slot.
236 err = syncForReleaseLocked(mEglDisplay);
237 if (err != NO_ERROR) {
238 // Release the buffer we just acquired. It's not safe to
239 // release the old buffer, so instead we just drop the new frame.
240 releaseBufferLocked(buf, mEglDisplay, EGL_NO_SYNC_KHR);
241 return err;
242 }
243
244 ST_LOGV("releaseAndUpdate: (slot=%d buf=%p) -> (slot=%d buf=%p)",
245 mCurrentTexture,
246 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
247 buf, mSlots[buf].mGraphicBuffer->handle);
248
249 // release old buffer
250 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
251 status_t status = releaseBufferLocked(mCurrentTexture, mEglDisplay,
252 mEglSlots[mCurrentTexture].mEglFence);
253 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
254 ST_LOGE("releaseAndUpdate: failed to release buffer: %s (%d)",
255 strerror(-status), status);
256 err = status;
257 // keep going, with error raised [?]
258 }
259 }
260
Andy McFadden2adaf042012-12-18 09:49:45 -0800261 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800262 mCurrentTexture = buf;
263 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
264 mCurrentCrop = item.mCrop;
265 mCurrentTransform = item.mTransform;
266 mCurrentScalingMode = item.mScalingMode;
267 mCurrentTimestamp = item.mTimestamp;
268 mCurrentFence = item.mFence;
269
270 computeCurrentTransformMatrixLocked();
271
272 return err;
273}
274
Andy McFadden2adaf042012-12-18 09:49:45 -0800275status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800276 if (mEglDisplay == EGL_NO_DISPLAY) {
277 ALOGE("bindTextureImage: invalid display");
278 return INVALID_OPERATION;
279 }
280
281 GLint error;
282 while ((error = glGetError()) != GL_NO_ERROR) {
283 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
284 }
285
286 glBindTexture(mTexTarget, mTexName);
287 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
288 if (mCurrentTextureBuf == NULL) {
289 ST_LOGE("bindTextureImage: no currently-bound texture");
290 return NO_INIT;
291 }
Andy McFadden97eba892012-12-11 15:21:45 -0800292 status_t err = bindUnslottedBufferLocked(mEglDisplay);
293 if (err != NO_ERROR) {
294 return err;
295 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800296 } else {
297 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
298
299 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
300
301 while ((error = glGetError()) != GL_NO_ERROR) {
302 ST_LOGE("bindTextureImage: error binding external texture image %p"
303 ": %#04x", image, error);
304 return UNKNOWN_ERROR;
305 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800306 }
Andy McFadden97eba892012-12-11 15:21:45 -0800307
308 // Wait for the new buffer to be ready.
309 return doGLFenceWaitLocked();
310
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800311}
312
Andy McFadden2adaf042012-12-18 09:49:45 -0800313status_t GLConsumer::checkAndUpdateEglStateLocked() {
Jamie Gennisce561372012-03-19 18:33:05 -0700314 EGLDisplay dpy = eglGetCurrentDisplay();
315 EGLContext ctx = eglGetCurrentContext();
316
Jamie Gennis74bed552012-03-28 19:05:54 -0700317 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
318 dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800319 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700320 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700321 }
322
Jamie Gennis74bed552012-03-28 19:05:54 -0700323 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
324 ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800325 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700326 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700327 }
328
329 mEglDisplay = dpy;
330 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800331 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800332}
333
Jesse Hall13f01cb2013-03-20 11:37:21 -0700334void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
335 if (fence->isValid() &&
336 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
337 status_t err = addReleaseFence(mCurrentTexture, fence);
338 if (err != OK) {
339 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
340 strerror(-err), err);
341 }
Jesse Hallef194142012-06-14 14:45:17 -0700342 }
343}
344
Andy McFadden2adaf042012-12-18 09:49:45 -0800345status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700346 ATRACE_CALL();
347 ST_LOGV("detachFromContext");
348 Mutex::Autolock lock(mMutex);
349
350 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800351 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700352 return NO_INIT;
353 }
354
355 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800356 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700357 "context");
358 return INVALID_OPERATION;
359 }
360
361 EGLDisplay dpy = eglGetCurrentDisplay();
362 EGLContext ctx = eglGetCurrentContext();
363
364 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
365 ST_LOGE("detachFromContext: invalid current EGLDisplay");
366 return INVALID_OPERATION;
367 }
368
369 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
370 ST_LOGE("detachFromContext: invalid current EGLContext");
371 return INVALID_OPERATION;
372 }
373
374 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
375 status_t err = syncForReleaseLocked(dpy);
376 if (err != OK) {
377 return err;
378 }
379
380 glDeleteTextures(1, &mTexName);
381 }
382
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700383 // Because we're giving up the EGLDisplay we need to free all the EGLImages
384 // that are associated with it. They'll be recreated when the
Andy McFadden2adaf042012-12-18 09:49:45 -0800385 // GLConsumer gets attached to a new OpenGL ES context (and thus gets a
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700386 // new EGLDisplay).
387 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700388 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700389 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700390 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700391 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700392 }
393 }
394
Jamie Gennis74bed552012-03-28 19:05:54 -0700395 mEglDisplay = EGL_NO_DISPLAY;
396 mEglContext = EGL_NO_CONTEXT;
397 mAttached = false;
398
399 return OK;
400}
401
Andy McFadden2adaf042012-12-18 09:49:45 -0800402status_t GLConsumer::attachToContext(GLuint tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700403 ATRACE_CALL();
404 ST_LOGV("attachToContext");
405 Mutex::Autolock lock(mMutex);
406
407 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800408 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700409 return NO_INIT;
410 }
411
412 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800413 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700414 "context");
415 return INVALID_OPERATION;
416 }
417
418 EGLDisplay dpy = eglGetCurrentDisplay();
419 EGLContext ctx = eglGetCurrentContext();
420
421 if (dpy == EGL_NO_DISPLAY) {
422 ST_LOGE("attachToContext: invalid current EGLDisplay");
423 return INVALID_OPERATION;
424 }
425
426 if (ctx == EGL_NO_CONTEXT) {
427 ST_LOGE("attachToContext: invalid current EGLContext");
428 return INVALID_OPERATION;
429 }
430
431 // We need to bind the texture regardless of whether there's a current
432 // buffer.
433 glBindTexture(mTexTarget, tex);
434
435 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700436 // The EGLImageKHR that was associated with the slot was destroyed when
Andy McFadden2adaf042012-12-18 09:49:45 -0800437 // the GLConsumer was detached from the old context, so we need to
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700438 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800439 status_t err = bindUnslottedBufferLocked(dpy);
440 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700441 return err;
442 }
443 }
444
445 mEglDisplay = dpy;
446 mEglContext = ctx;
447 mTexName = tex;
448 mAttached = true;
449
450 return OK;
451}
452
Andy McFadden2adaf042012-12-18 09:49:45 -0800453status_t GLConsumer::bindUnslottedBufferLocked(EGLDisplay dpy) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800454 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
455 mCurrentTexture, mCurrentTextureBuf.get());
456
457 // Create a temporary EGLImageKHR.
458 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
459 if (image == EGL_NO_IMAGE_KHR) {
460 return UNKNOWN_ERROR;
461 }
462
463 // Attach the current buffer to the GL texture.
464 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
465
466 GLint error;
467 status_t err = OK;
468 while ((error = glGetError()) != GL_NO_ERROR) {
469 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
470 "(slot %d): %#04x", image, mCurrentTexture, error);
471 err = UNKNOWN_ERROR;
472 }
473
474 // We destroy the EGLImageKHR here because the current buffer may no
475 // longer be associated with one of the buffer slots, so we have
476 // nowhere to to store it. If the buffer is still associated with a
477 // slot then another EGLImageKHR will be created next time that buffer
478 // gets acquired in updateTexImage.
479 eglDestroyImageKHR(dpy, image);
480
481 return err;
482}
483
484
Andy McFadden2adaf042012-12-18 09:49:45 -0800485status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700486 ST_LOGV("syncForReleaseLocked");
487
Jamie Gennis01dbf552012-09-06 14:54:19 -0700488 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700489 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700490 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
491 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
492 if (sync == EGL_NO_SYNC_KHR) {
493 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
494 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700495 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700496 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700497 glFlush();
498 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700499 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700500 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
501 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
502 "fd: %#x", eglGetError());
503 return UNKNOWN_ERROR;
504 }
505 sp<Fence> fence(new Fence(fenceFd));
Jesse Hall9504eb92012-10-05 14:34:21 -0700506 status_t err = addReleaseFenceLocked(mCurrentTexture, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700507 if (err != OK) {
508 ST_LOGE("syncForReleaseLocked: error adding release fence: "
509 "%s (%d)", strerror(-err), err);
510 return err;
511 }
Mathias Agopianca088332013-03-28 17:44:13 -0700512 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700513 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
514 if (fence != EGL_NO_SYNC_KHR) {
515 // There is already a fence for the current slot. We need to
516 // wait on that before replacing it with another fence to
517 // ensure that all outstanding buffer accesses have completed
518 // before the producer accesses it.
519 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
520 if (result == EGL_FALSE) {
521 ST_LOGE("syncForReleaseLocked: error waiting for previous "
522 "fence: %#x", eglGetError());
523 return UNKNOWN_ERROR;
524 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
525 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
526 "fence");
527 return TIMED_OUT;
528 }
529 eglDestroySyncKHR(dpy, fence);
530 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700531
Jamie Gennis01dbf552012-09-06 14:54:19 -0700532 // Create a fence for the outstanding accesses in the current
533 // OpenGL ES context.
534 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
535 if (fence == EGL_NO_SYNC_KHR) {
536 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
537 eglGetError());
538 return UNKNOWN_ERROR;
539 }
540 glFlush();
541 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700542 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700543 }
544
545 return OK;
546}
547
Andy McFadden2adaf042012-12-18 09:49:45 -0800548bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700549{
550 switch (format) {
551 // supported YUV formats
552 case HAL_PIXEL_FORMAT_YV12:
553 // Legacy/deprecated YUV formats
554 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
555 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
556 case HAL_PIXEL_FORMAT_YCbCr_422_I:
557 return true;
558 }
559
560 // Any OEM format needs to be considered
561 if (format>=0x100 && format<=0x1FF)
562 return true;
563
564 return false;
565}
566
Andy McFadden2adaf042012-12-18 09:49:45 -0800567GLenum GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700568 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700569}
570
Andy McFadden2adaf042012-12-18 09:49:45 -0800571void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800572 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700573 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
574}
575
Andy McFadden2adaf042012-12-18 09:49:45 -0800576void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700577 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700578 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800579 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700580 return;
581 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700582 bool needsRecompute = mFilteringEnabled != enabled;
583 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700584
585 if (needsRecompute && mCurrentTextureBuf==NULL) {
586 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
587 }
588
589 if (needsRecompute && mCurrentTextureBuf != NULL) {
590 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700591 }
592}
593
Andy McFadden2adaf042012-12-18 09:49:45 -0800594void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700595 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800596
Jamie Gennisa214c642011-01-14 13:53:31 -0800597 float xform[16];
598 for (int i = 0; i < 16; i++) {
599 xform[i] = mtxIdentity[i];
600 }
601 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
602 float result[16];
603 mtxMul(result, xform, mtxFlipH);
604 for (int i = 0; i < 16; i++) {
605 xform[i] = result[i];
606 }
607 }
608 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
609 float result[16];
610 mtxMul(result, xform, mtxFlipV);
611 for (int i = 0; i < 16; i++) {
612 xform[i] = result[i];
613 }
614 }
615 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
616 float result[16];
617 mtxMul(result, xform, mtxRot90);
618 for (int i = 0; i < 16; i++) {
619 xform[i] = result[i];
620 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800621 }
622
Daniel Lameae59d22012-01-22 15:26:27 -0800623 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700624
625 if (buf == NULL) {
626 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
627 }
628
Jamie Gennisd72f2332012-05-07 13:50:11 -0700629 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700630 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
631 float bufferWidth = buf->getWidth();
632 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700633 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700634 float shrinkAmount = 0.0f;
635 if (mFilteringEnabled) {
636 // In order to prevent bilinear sampling beyond the edge of the
637 // crop rectangle we may need to shrink it by 2 texels in each
638 // dimension. Normally this would just need to take 1/2 a texel
639 // off each end, but because the chroma channels of YUV420 images
640 // are subsampled we may need to shrink the crop region by a whole
641 // texel on each side.
642 switch (buf->getPixelFormat()) {
643 case PIXEL_FORMAT_RGBA_8888:
644 case PIXEL_FORMAT_RGBX_8888:
645 case PIXEL_FORMAT_RGB_888:
646 case PIXEL_FORMAT_RGB_565:
647 case PIXEL_FORMAT_BGRA_8888:
648 case PIXEL_FORMAT_RGBA_5551:
649 case PIXEL_FORMAT_RGBA_4444:
650 // We know there's no subsampling of any channels, so we
651 // only need to shrink by a half a pixel.
652 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700653 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700654
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700655 default:
656 // If we don't recognize the format, we must assume the
657 // worst case (that we care about), which is YUV420.
658 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700659 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700660 }
661 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700662
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700663 // Only shrink the dimensions that are not the size of the buffer.
664 if (cropRect.width() < bufferWidth) {
665 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
666 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
667 bufferWidth;
668 }
669 if (cropRect.height() < bufferHeight) {
670 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
671 bufferHeight;
672 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
673 bufferHeight;
674 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800675 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800676 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800677 sx, 0, 0, 0,
678 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800679 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800680 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800681 };
682
Jamie Gennisa214c642011-01-14 13:53:31 -0800683 float mtxBeforeFlipV[16];
684 mtxMul(mtxBeforeFlipV, crop, xform);
685
686 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800687 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800688 // want to expose this to applications, however, so we must add an
689 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700690 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800691}
692
Andy McFadden2adaf042012-12-18 09:49:45 -0800693nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700694 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800695 Mutex::Autolock lock(mMutex);
696 return mCurrentTimestamp;
697}
698
Andy McFadden2adaf042012-12-18 09:49:45 -0800699EGLImageKHR GLConsumer::createImage(EGLDisplay dpy,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800700 const sp<GraphicBuffer>& graphicBuffer) {
701 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
702 EGLint attrs[] = {
703 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
704 EGL_NONE,
705 };
706 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
707 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700708 if (image == EGL_NO_IMAGE_KHR) {
709 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700710 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800711 }
712 return image;
713}
714
Andy McFadden2adaf042012-12-18 09:49:45 -0800715sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700716 Mutex::Autolock lock(mMutex);
717 return mCurrentTextureBuf;
718}
719
Andy McFadden2adaf042012-12-18 09:49:45 -0800720Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700721 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700722
723 Rect outCrop = mCurrentCrop;
724 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
725 int32_t newWidth = mCurrentCrop.width();
726 int32_t newHeight = mCurrentCrop.height();
727
728 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
729 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
730 ST_LOGV("too wide: newWidth = %d", newWidth);
731 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
732 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
733 ST_LOGV("too tall: newHeight = %d", newHeight);
734 }
735
736 // The crop is too wide
737 if (newWidth < mCurrentCrop.width()) {
738 int32_t dw = (newWidth - mCurrentCrop.width())/2;
739 outCrop.left -=dw;
740 outCrop.right += dw;
741 // The crop is too tall
742 } else if (newHeight < mCurrentCrop.height()) {
743 int32_t dh = (newHeight - mCurrentCrop.height())/2;
744 outCrop.top -= dh;
745 outCrop.bottom += dh;
746 }
747
748 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
749 outCrop.left, outCrop.top,
750 outCrop.right,outCrop.bottom);
751 }
752
Daniel Lam016c8cb2012-04-03 15:54:58 -0700753 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700754}
755
Andy McFadden2adaf042012-12-18 09:49:45 -0800756uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700757 Mutex::Autolock lock(mMutex);
758 return mCurrentTransform;
759}
760
Andy McFadden2adaf042012-12-18 09:49:45 -0800761uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700762 Mutex::Autolock lock(mMutex);
763 return mCurrentScalingMode;
764}
765
Andy McFadden2adaf042012-12-18 09:49:45 -0800766sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700767 Mutex::Autolock lock(mMutex);
768 return mCurrentFence;
769}
770
Andy McFadden2adaf042012-12-18 09:49:45 -0800771status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700772 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700773 return doGLFenceWaitLocked();
774}
775
Andy McFadden2adaf042012-12-18 09:49:45 -0800776status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700777
778 EGLDisplay dpy = eglGetCurrentDisplay();
779 EGLContext ctx = eglGetCurrentContext();
780
781 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
782 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
783 return INVALID_OPERATION;
784 }
785
786 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
787 ST_LOGE("doGLFenceWait: invalid current EGLContext");
788 return INVALID_OPERATION;
789 }
790
Jamie Gennis1df8c342012-12-20 14:05:45 -0800791 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700792 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700793 // Create an EGLSyncKHR from the current fence.
794 int fenceFd = mCurrentFence->dup();
795 if (fenceFd == -1) {
796 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
797 return -errno;
798 }
799 EGLint attribs[] = {
800 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
801 EGL_NONE
802 };
803 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
804 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
805 if (sync == EGL_NO_SYNC_KHR) {
806 close(fenceFd);
807 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
808 eglGetError());
809 return UNKNOWN_ERROR;
810 }
811
812 // XXX: The spec draft is inconsistent as to whether this should
813 // return an EGLint or void. Ignore the return value for now, as
814 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700815 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700816 EGLint eglErr = eglGetError();
817 eglDestroySyncKHR(dpy, sync);
818 if (eglErr != EGL_SUCCESS) {
819 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
820 eglErr);
821 return UNKNOWN_ERROR;
822 }
823 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700824 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800825 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700826 if (err != NO_ERROR) {
827 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
828 return err;
829 }
830 }
831 }
832
833 return NO_ERROR;
834}
835
Andy McFadden2adaf042012-12-18 09:49:45 -0800836bool GLConsumer::isSynchronousMode() const {
Jamie Gennis59769462011-11-19 18:04:43 -0800837 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800838 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800839}
840
Andy McFadden2adaf042012-12-18 09:49:45 -0800841void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700842 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700843 if (slotIndex == mCurrentTexture) {
844 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
845 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700846 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700847 if (img != EGL_NO_IMAGE_KHR) {
848 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
849 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800850 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700851 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
852 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700853}
Daniel Lameae59d22012-01-22 15:26:27 -0800854
Andy McFadden2adaf042012-12-18 09:49:45 -0800855void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700856 ST_LOGV("abandonLocked");
857 mCurrentTextureBuf.clear();
858 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700859}
860
Andy McFadden2adaf042012-12-18 09:49:45 -0800861void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800862 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700863 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800864 mBufferQueue->setConsumerName(name);
865}
866
Andy McFadden2adaf042012-12-18 09:49:45 -0800867status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -0800868 Mutex::Autolock lock(mMutex);
869 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
870}
871
Andy McFadden2adaf042012-12-18 09:49:45 -0800872status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -0800873 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700874 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800875 return mBufferQueue->setConsumerUsageBits(usage);
876}
877
Andy McFadden2adaf042012-12-18 09:49:45 -0800878status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -0800879 Mutex::Autolock lock(mMutex);
880 return mBufferQueue->setTransformHint(hint);
881}
882
Andy McFadden2adaf042012-12-18 09:49:45 -0800883// Used for refactoring BufferQueue from GLConsumer
884// Should not be in final interface once users of GLConsumer are clean up.
885status_t GLConsumer::setSynchronousMode(bool enabled) {
Daniel Lamb2675792012-02-23 14:35:13 -0800886 Mutex::Autolock lock(mMutex);
887 return mBufferQueue->setSynchronousMode(enabled);
888}
889
Andy McFadden2adaf042012-12-18 09:49:45 -0800890void GLConsumer::dumpLocked(String8& result, const char* prefix,
Jamie Gennis9fea3422012-08-07 18:03:04 -0700891 char* buffer, size_t size) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700892{
Jamie Gennis9fea3422012-08-07 18:03:04 -0700893 snprintf(buffer, size,
894 "%smTexName=%d mCurrentTexture=%d\n"
895 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
896 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
897 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
898 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700899 result.append(buffer);
900
Jamie Gennis9fea3422012-08-07 18:03:04 -0700901 ConsumerBase::dumpLocked(result, prefix, buffer, size);
Mathias Agopian68c77942011-05-09 19:08:33 -0700902}
903
Jamie Gennisf238e282011-01-09 16:33:17 -0800904static void mtxMul(float out[16], const float a[16], const float b[16]) {
905 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
906 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
907 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
908 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
909
910 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
911 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
912 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
913 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
914
915 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
916 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
917 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
918 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
919
920 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
921 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
922 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
923 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
924}
925
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800926}; // namespace android