blob: 0d7c72e606bc02b348026f874e9a02c1b2b5022c [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),
Jamie Gennis1df8c342012-12-20 14:05:45 -080085 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080086 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -070087 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070088 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -080089 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -080090 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -070091 mEglDisplay(EGL_NO_DISPLAY),
92 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -070093 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
94 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -080095{
Andy McFadden2adaf042012-12-18 09:49:45 -080096 ST_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -080097
Jamie Gennisfa28c352011-09-16 17:30:26 -070098 memcpy(mCurrentTransformMatrix, mtxIdentity,
99 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700100
Jamie Gennis9fea3422012-08-07 18:03:04 -0700101 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800102}
103
Andy McFadden2adaf042012-12-18 09:49:45 -0800104status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700105 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -0700106 return mBufferQueue->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700107}
108
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800109
Andy McFadden2adaf042012-12-18 09:49:45 -0800110status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700111{
Daniel Lamb2675792012-02-23 14:35:13 -0800112 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700113 mDefaultWidth = w;
114 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800115 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700116}
117
Andy McFadden2adaf042012-12-18 09:49:45 -0800118status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800119 ATRACE_CALL();
120 ST_LOGV("updateTexImage");
121 Mutex::Autolock lock(mMutex);
122
123 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800124 ST_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800125 return NO_INIT;
126 }
127
128 // Make sure the EGL state is the same as in previous calls.
129 status_t err = checkAndUpdateEglStateLocked();
130 if (err != NO_ERROR) {
131 return err;
132 }
133
134 BufferQueue::BufferItem item;
135
136 // Acquire the next buffer.
137 // In asynchronous mode the list is guaranteed to be one buffer
138 // deep, while in synchronous mode we use the oldest buffer.
139 err = acquireBufferLocked(&item);
140 if (err != NO_ERROR) {
141 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
142 // We always bind the texture even if we don't update its contents.
143 ST_LOGV("updateTexImage: no buffers were available");
144 glBindTexture(mTexTarget, mTexName);
145 err = NO_ERROR;
146 } else {
147 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
148 strerror(-err), err);
149 }
150 return err;
151 }
152
153 // Release the previous buffer.
154 err = releaseAndUpdateLocked(item);
155 if (err != NO_ERROR) {
156 // We always bind the texture.
157 glBindTexture(mTexTarget, mTexName);
158 return err;
159 }
160
Andy McFadden97eba892012-12-11 15:21:45 -0800161 // Bind the new buffer to the GL texture, and wait until it's ready.
162 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700163}
164
Andy McFadden2adaf042012-12-18 09:49:45 -0800165status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700166 status_t err = ConsumerBase::acquireBufferLocked(item);
167 if (err != NO_ERROR) {
168 return err;
169 }
170
171 int slot = item->mBuf;
172 if (item->mGraphicBuffer != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800173 // This buffer has not been acquired before, so we must assume
174 // that any EGLImage in mEglSlots is stale.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700175 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800176 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
177 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
178 slot);
179 // keep going
180 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700181 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
182 }
183 }
184
Jamie Gennis9fea3422012-08-07 18:03:04 -0700185 return NO_ERROR;
186}
187
Andy McFadden2adaf042012-12-18 09:49:45 -0800188status_t GLConsumer::releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700189 EGLSyncKHR eglFence) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800190 status_t err = ConsumerBase::releaseBufferLocked(buf, display, eglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700191
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700192 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700193
194 return err;
195}
196
Andy McFadden2adaf042012-12-18 09:49:45 -0800197status_t GLConsumer::releaseAndUpdateLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800198{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700199 status_t err = NO_ERROR;
200
Jamie Gennis74bed552012-03-28 19:05:54 -0700201 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800202 ST_LOGE("releaseAndUpdate: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700203 "ES context");
204 return INVALID_OPERATION;
205 }
206
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800207 // Confirm state.
208 err = checkAndUpdateEglStateLocked();
209 if (err != NO_ERROR) {
210 return err;
211 }
212
213 int buf = item.mBuf;
214
215 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
216 // buffer currently in the slot in ConsumerBase.
217 //
218 // We may have to do this even when item.mGraphicBuffer == NULL (which
219 // means the buffer was previously acquired), if we destroyed the
220 // EGLImage when detaching from a context but the buffer has not been
221 // re-allocated.
222 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
223 EGLImageKHR image = createImage(mEglDisplay, mSlots[buf].mGraphicBuffer);
224 if (image == EGL_NO_IMAGE_KHR) {
225 ST_LOGW("releaseAndUpdate: unable to createImage on display=%p slot=%d",
226 mEglDisplay, buf);
227 return UNKNOWN_ERROR;
228 }
229 mEglSlots[buf].mEglImage = image;
230 }
231
232 // Do whatever sync ops we need to do before releasing the old slot.
233 err = syncForReleaseLocked(mEglDisplay);
234 if (err != NO_ERROR) {
235 // Release the buffer we just acquired. It's not safe to
236 // release the old buffer, so instead we just drop the new frame.
237 releaseBufferLocked(buf, mEglDisplay, EGL_NO_SYNC_KHR);
238 return err;
239 }
240
241 ST_LOGV("releaseAndUpdate: (slot=%d buf=%p) -> (slot=%d buf=%p)",
242 mCurrentTexture,
243 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
244 buf, mSlots[buf].mGraphicBuffer->handle);
245
246 // release old buffer
247 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
248 status_t status = releaseBufferLocked(mCurrentTexture, mEglDisplay,
249 mEglSlots[mCurrentTexture].mEglFence);
250 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
251 ST_LOGE("releaseAndUpdate: failed to release buffer: %s (%d)",
252 strerror(-status), status);
253 err = status;
254 // keep going, with error raised [?]
255 }
256 }
257
Andy McFadden2adaf042012-12-18 09:49:45 -0800258 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800259 mCurrentTexture = buf;
260 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
261 mCurrentCrop = item.mCrop;
262 mCurrentTransform = item.mTransform;
263 mCurrentScalingMode = item.mScalingMode;
264 mCurrentTimestamp = item.mTimestamp;
265 mCurrentFence = item.mFence;
266
267 computeCurrentTransformMatrixLocked();
268
269 return err;
270}
271
Andy McFadden2adaf042012-12-18 09:49:45 -0800272status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800273 if (mEglDisplay == EGL_NO_DISPLAY) {
274 ALOGE("bindTextureImage: invalid display");
275 return INVALID_OPERATION;
276 }
277
278 GLint error;
279 while ((error = glGetError()) != GL_NO_ERROR) {
280 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
281 }
282
283 glBindTexture(mTexTarget, mTexName);
284 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
285 if (mCurrentTextureBuf == NULL) {
286 ST_LOGE("bindTextureImage: no currently-bound texture");
287 return NO_INIT;
288 }
Andy McFadden97eba892012-12-11 15:21:45 -0800289 status_t err = bindUnslottedBufferLocked(mEglDisplay);
290 if (err != NO_ERROR) {
291 return err;
292 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800293 } else {
294 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
295
296 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
297
298 while ((error = glGetError()) != GL_NO_ERROR) {
299 ST_LOGE("bindTextureImage: error binding external texture image %p"
300 ": %#04x", image, error);
301 return UNKNOWN_ERROR;
302 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800303 }
Andy McFadden97eba892012-12-11 15:21:45 -0800304
305 // Wait for the new buffer to be ready.
306 return doGLFenceWaitLocked();
307
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800308}
309
Andy McFadden2adaf042012-12-18 09:49:45 -0800310status_t GLConsumer::checkAndUpdateEglStateLocked() {
Jamie Gennisce561372012-03-19 18:33:05 -0700311 EGLDisplay dpy = eglGetCurrentDisplay();
312 EGLContext ctx = eglGetCurrentContext();
313
Jamie Gennis74bed552012-03-28 19:05:54 -0700314 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
315 dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800316 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700317 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700318 }
319
Jamie Gennis74bed552012-03-28 19:05:54 -0700320 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
321 ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800322 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700323 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700324 }
325
326 mEglDisplay = dpy;
327 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800328 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800329}
330
Jesse Hall13f01cb2013-03-20 11:37:21 -0700331void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
332 if (fence->isValid() &&
333 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
334 status_t err = addReleaseFence(mCurrentTexture, fence);
335 if (err != OK) {
336 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
337 strerror(-err), err);
338 }
Jesse Hallef194142012-06-14 14:45:17 -0700339 }
340}
341
Andy McFadden2adaf042012-12-18 09:49:45 -0800342status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700343 ATRACE_CALL();
344 ST_LOGV("detachFromContext");
345 Mutex::Autolock lock(mMutex);
346
347 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800348 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700349 return NO_INIT;
350 }
351
352 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800353 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700354 "context");
355 return INVALID_OPERATION;
356 }
357
358 EGLDisplay dpy = eglGetCurrentDisplay();
359 EGLContext ctx = eglGetCurrentContext();
360
361 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
362 ST_LOGE("detachFromContext: invalid current EGLDisplay");
363 return INVALID_OPERATION;
364 }
365
366 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
367 ST_LOGE("detachFromContext: invalid current EGLContext");
368 return INVALID_OPERATION;
369 }
370
371 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
372 status_t err = syncForReleaseLocked(dpy);
373 if (err != OK) {
374 return err;
375 }
376
377 glDeleteTextures(1, &mTexName);
378 }
379
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700380 // Because we're giving up the EGLDisplay we need to free all the EGLImages
381 // that are associated with it. They'll be recreated when the
Andy McFadden2adaf042012-12-18 09:49:45 -0800382 // GLConsumer gets attached to a new OpenGL ES context (and thus gets a
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700383 // new EGLDisplay).
384 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700385 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700386 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700387 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700388 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700389 }
390 }
391
Jamie Gennis74bed552012-03-28 19:05:54 -0700392 mEglDisplay = EGL_NO_DISPLAY;
393 mEglContext = EGL_NO_CONTEXT;
394 mAttached = false;
395
396 return OK;
397}
398
Andy McFadden2adaf042012-12-18 09:49:45 -0800399status_t GLConsumer::attachToContext(GLuint tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700400 ATRACE_CALL();
401 ST_LOGV("attachToContext");
402 Mutex::Autolock lock(mMutex);
403
404 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800405 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700406 return NO_INIT;
407 }
408
409 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800410 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700411 "context");
412 return INVALID_OPERATION;
413 }
414
415 EGLDisplay dpy = eglGetCurrentDisplay();
416 EGLContext ctx = eglGetCurrentContext();
417
418 if (dpy == EGL_NO_DISPLAY) {
419 ST_LOGE("attachToContext: invalid current EGLDisplay");
420 return INVALID_OPERATION;
421 }
422
423 if (ctx == EGL_NO_CONTEXT) {
424 ST_LOGE("attachToContext: invalid current EGLContext");
425 return INVALID_OPERATION;
426 }
427
428 // We need to bind the texture regardless of whether there's a current
429 // buffer.
430 glBindTexture(mTexTarget, tex);
431
432 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700433 // The EGLImageKHR that was associated with the slot was destroyed when
Andy McFadden2adaf042012-12-18 09:49:45 -0800434 // the GLConsumer was detached from the old context, so we need to
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700435 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800436 status_t err = bindUnslottedBufferLocked(dpy);
437 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700438 return err;
439 }
440 }
441
442 mEglDisplay = dpy;
443 mEglContext = ctx;
444 mTexName = tex;
445 mAttached = true;
446
447 return OK;
448}
449
Andy McFadden2adaf042012-12-18 09:49:45 -0800450status_t GLConsumer::bindUnslottedBufferLocked(EGLDisplay dpy) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800451 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
452 mCurrentTexture, mCurrentTextureBuf.get());
453
454 // Create a temporary EGLImageKHR.
455 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
456 if (image == EGL_NO_IMAGE_KHR) {
457 return UNKNOWN_ERROR;
458 }
459
460 // Attach the current buffer to the GL texture.
461 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
462
463 GLint error;
464 status_t err = OK;
465 while ((error = glGetError()) != GL_NO_ERROR) {
466 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
467 "(slot %d): %#04x", image, mCurrentTexture, error);
468 err = UNKNOWN_ERROR;
469 }
470
471 // We destroy the EGLImageKHR here because the current buffer may no
472 // longer be associated with one of the buffer slots, so we have
473 // nowhere to to store it. If the buffer is still associated with a
474 // slot then another EGLImageKHR will be created next time that buffer
475 // gets acquired in updateTexImage.
476 eglDestroyImageKHR(dpy, image);
477
478 return err;
479}
480
481
Andy McFadden2adaf042012-12-18 09:49:45 -0800482status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700483 ST_LOGV("syncForReleaseLocked");
484
Jamie Gennis01dbf552012-09-06 14:54:19 -0700485 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700486 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700487 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
488 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
489 if (sync == EGL_NO_SYNC_KHR) {
490 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
491 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700492 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700493 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700494 glFlush();
495 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700496 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700497 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
498 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
499 "fd: %#x", eglGetError());
500 return UNKNOWN_ERROR;
501 }
502 sp<Fence> fence(new Fence(fenceFd));
Jesse Hall9504eb92012-10-05 14:34:21 -0700503 status_t err = addReleaseFenceLocked(mCurrentTexture, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700504 if (err != OK) {
505 ST_LOGE("syncForReleaseLocked: error adding release fence: "
506 "%s (%d)", strerror(-err), err);
507 return err;
508 }
Mathias Agopianca088332013-03-28 17:44:13 -0700509 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700510 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
511 if (fence != EGL_NO_SYNC_KHR) {
512 // There is already a fence for the current slot. We need to
513 // wait on that before replacing it with another fence to
514 // ensure that all outstanding buffer accesses have completed
515 // before the producer accesses it.
516 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
517 if (result == EGL_FALSE) {
518 ST_LOGE("syncForReleaseLocked: error waiting for previous "
519 "fence: %#x", eglGetError());
520 return UNKNOWN_ERROR;
521 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
522 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
523 "fence");
524 return TIMED_OUT;
525 }
526 eglDestroySyncKHR(dpy, fence);
527 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700528
Jamie Gennis01dbf552012-09-06 14:54:19 -0700529 // Create a fence for the outstanding accesses in the current
530 // OpenGL ES context.
531 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
532 if (fence == EGL_NO_SYNC_KHR) {
533 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
534 eglGetError());
535 return UNKNOWN_ERROR;
536 }
537 glFlush();
538 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700539 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700540 }
541
542 return OK;
543}
544
Andy McFadden2adaf042012-12-18 09:49:45 -0800545bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700546{
547 switch (format) {
548 // supported YUV formats
549 case HAL_PIXEL_FORMAT_YV12:
550 // Legacy/deprecated YUV formats
551 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
552 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
553 case HAL_PIXEL_FORMAT_YCbCr_422_I:
554 return true;
555 }
556
557 // Any OEM format needs to be considered
558 if (format>=0x100 && format<=0x1FF)
559 return true;
560
561 return false;
562}
563
Andy McFadden2adaf042012-12-18 09:49:45 -0800564GLenum GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700565 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700566}
567
Andy McFadden2adaf042012-12-18 09:49:45 -0800568void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800569 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700570 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
571}
572
Andy McFadden2adaf042012-12-18 09:49:45 -0800573void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700574 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700575 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800576 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700577 return;
578 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700579 bool needsRecompute = mFilteringEnabled != enabled;
580 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700581
582 if (needsRecompute && mCurrentTextureBuf==NULL) {
583 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
584 }
585
586 if (needsRecompute && mCurrentTextureBuf != NULL) {
587 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700588 }
589}
590
Andy McFadden2adaf042012-12-18 09:49:45 -0800591void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700592 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800593
Jamie Gennisa214c642011-01-14 13:53:31 -0800594 float xform[16];
595 for (int i = 0; i < 16; i++) {
596 xform[i] = mtxIdentity[i];
597 }
598 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
599 float result[16];
600 mtxMul(result, xform, mtxFlipH);
601 for (int i = 0; i < 16; i++) {
602 xform[i] = result[i];
603 }
604 }
605 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
606 float result[16];
607 mtxMul(result, xform, mtxFlipV);
608 for (int i = 0; i < 16; i++) {
609 xform[i] = result[i];
610 }
611 }
612 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
613 float result[16];
614 mtxMul(result, xform, mtxRot90);
615 for (int i = 0; i < 16; i++) {
616 xform[i] = result[i];
617 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800618 }
619
Daniel Lameae59d22012-01-22 15:26:27 -0800620 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700621
622 if (buf == NULL) {
623 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
624 }
625
Jamie Gennisd72f2332012-05-07 13:50:11 -0700626 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700627 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
628 float bufferWidth = buf->getWidth();
629 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700630 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700631 float shrinkAmount = 0.0f;
632 if (mFilteringEnabled) {
633 // In order to prevent bilinear sampling beyond the edge of the
634 // crop rectangle we may need to shrink it by 2 texels in each
635 // dimension. Normally this would just need to take 1/2 a texel
636 // off each end, but because the chroma channels of YUV420 images
637 // are subsampled we may need to shrink the crop region by a whole
638 // texel on each side.
639 switch (buf->getPixelFormat()) {
640 case PIXEL_FORMAT_RGBA_8888:
641 case PIXEL_FORMAT_RGBX_8888:
642 case PIXEL_FORMAT_RGB_888:
643 case PIXEL_FORMAT_RGB_565:
644 case PIXEL_FORMAT_BGRA_8888:
645 case PIXEL_FORMAT_RGBA_5551:
646 case PIXEL_FORMAT_RGBA_4444:
647 // We know there's no subsampling of any channels, so we
648 // only need to shrink by a half a pixel.
649 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700650 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700651
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700652 default:
653 // If we don't recognize the format, we must assume the
654 // worst case (that we care about), which is YUV420.
655 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700656 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700657 }
658 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700659
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700660 // Only shrink the dimensions that are not the size of the buffer.
661 if (cropRect.width() < bufferWidth) {
662 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
663 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
664 bufferWidth;
665 }
666 if (cropRect.height() < bufferHeight) {
667 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
668 bufferHeight;
669 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
670 bufferHeight;
671 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800672 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800673 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800674 sx, 0, 0, 0,
675 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800676 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800677 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800678 };
679
Jamie Gennisa214c642011-01-14 13:53:31 -0800680 float mtxBeforeFlipV[16];
681 mtxMul(mtxBeforeFlipV, crop, xform);
682
683 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800684 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800685 // want to expose this to applications, however, so we must add an
686 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700687 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800688}
689
Andy McFadden2adaf042012-12-18 09:49:45 -0800690nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700691 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800692 Mutex::Autolock lock(mMutex);
693 return mCurrentTimestamp;
694}
695
Andy McFadden2adaf042012-12-18 09:49:45 -0800696EGLImageKHR GLConsumer::createImage(EGLDisplay dpy,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800697 const sp<GraphicBuffer>& graphicBuffer) {
698 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
699 EGLint attrs[] = {
700 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
701 EGL_NONE,
702 };
703 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
704 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700705 if (image == EGL_NO_IMAGE_KHR) {
706 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700707 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800708 }
709 return image;
710}
711
Andy McFadden2adaf042012-12-18 09:49:45 -0800712sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700713 Mutex::Autolock lock(mMutex);
714 return mCurrentTextureBuf;
715}
716
Andy McFadden2adaf042012-12-18 09:49:45 -0800717Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700718 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700719
720 Rect outCrop = mCurrentCrop;
721 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
722 int32_t newWidth = mCurrentCrop.width();
723 int32_t newHeight = mCurrentCrop.height();
724
725 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
726 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
727 ST_LOGV("too wide: newWidth = %d", newWidth);
728 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
729 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
730 ST_LOGV("too tall: newHeight = %d", newHeight);
731 }
732
733 // The crop is too wide
734 if (newWidth < mCurrentCrop.width()) {
735 int32_t dw = (newWidth - mCurrentCrop.width())/2;
736 outCrop.left -=dw;
737 outCrop.right += dw;
738 // The crop is too tall
739 } else if (newHeight < mCurrentCrop.height()) {
740 int32_t dh = (newHeight - mCurrentCrop.height())/2;
741 outCrop.top -= dh;
742 outCrop.bottom += dh;
743 }
744
745 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
746 outCrop.left, outCrop.top,
747 outCrop.right,outCrop.bottom);
748 }
749
Daniel Lam016c8cb2012-04-03 15:54:58 -0700750 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700751}
752
Andy McFadden2adaf042012-12-18 09:49:45 -0800753uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700754 Mutex::Autolock lock(mMutex);
755 return mCurrentTransform;
756}
757
Andy McFadden2adaf042012-12-18 09:49:45 -0800758uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700759 Mutex::Autolock lock(mMutex);
760 return mCurrentScalingMode;
761}
762
Andy McFadden2adaf042012-12-18 09:49:45 -0800763sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700764 Mutex::Autolock lock(mMutex);
765 return mCurrentFence;
766}
767
Andy McFadden2adaf042012-12-18 09:49:45 -0800768status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700769 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700770 return doGLFenceWaitLocked();
771}
772
Andy McFadden2adaf042012-12-18 09:49:45 -0800773status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700774
775 EGLDisplay dpy = eglGetCurrentDisplay();
776 EGLContext ctx = eglGetCurrentContext();
777
778 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
779 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
780 return INVALID_OPERATION;
781 }
782
783 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
784 ST_LOGE("doGLFenceWait: invalid current EGLContext");
785 return INVALID_OPERATION;
786 }
787
Jamie Gennis1df8c342012-12-20 14:05:45 -0800788 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700789 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700790 // Create an EGLSyncKHR from the current fence.
791 int fenceFd = mCurrentFence->dup();
792 if (fenceFd == -1) {
793 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
794 return -errno;
795 }
796 EGLint attribs[] = {
797 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
798 EGL_NONE
799 };
800 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
801 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
802 if (sync == EGL_NO_SYNC_KHR) {
803 close(fenceFd);
804 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
805 eglGetError());
806 return UNKNOWN_ERROR;
807 }
808
809 // XXX: The spec draft is inconsistent as to whether this should
810 // return an EGLint or void. Ignore the return value for now, as
811 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700812 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700813 EGLint eglErr = eglGetError();
814 eglDestroySyncKHR(dpy, sync);
815 if (eglErr != EGL_SUCCESS) {
816 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
817 eglErr);
818 return UNKNOWN_ERROR;
819 }
820 } else {
Jesse Hallba607d52012-10-01 14:05:20 -0700821 status_t err = mCurrentFence->waitForever(1000,
Andy McFadden2adaf042012-12-18 09:49:45 -0800822 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700823 if (err != NO_ERROR) {
824 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
825 return err;
826 }
827 }
828 }
829
830 return NO_ERROR;
831}
832
Andy McFadden2adaf042012-12-18 09:49:45 -0800833bool GLConsumer::isSynchronousMode() const {
Jamie Gennis59769462011-11-19 18:04:43 -0800834 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800835 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800836}
837
Andy McFadden2adaf042012-12-18 09:49:45 -0800838void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700839 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700840 if (slotIndex == mCurrentTexture) {
841 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
842 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700843 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700844 if (img != EGL_NO_IMAGE_KHR) {
845 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
846 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800847 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700848 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
849 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700850}
Daniel Lameae59d22012-01-22 15:26:27 -0800851
Andy McFadden2adaf042012-12-18 09:49:45 -0800852void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700853 ST_LOGV("abandonLocked");
854 mCurrentTextureBuf.clear();
855 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700856}
857
Andy McFadden2adaf042012-12-18 09:49:45 -0800858void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800859 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700860 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800861 mBufferQueue->setConsumerName(name);
862}
863
Andy McFadden2adaf042012-12-18 09:49:45 -0800864status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -0800865 Mutex::Autolock lock(mMutex);
866 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
867}
868
Andy McFadden2adaf042012-12-18 09:49:45 -0800869status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -0800870 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700871 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800872 return mBufferQueue->setConsumerUsageBits(usage);
873}
874
Andy McFadden2adaf042012-12-18 09:49:45 -0800875status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -0800876 Mutex::Autolock lock(mMutex);
877 return mBufferQueue->setTransformHint(hint);
878}
879
Andy McFadden2adaf042012-12-18 09:49:45 -0800880// Used for refactoring BufferQueue from GLConsumer
881// Should not be in final interface once users of GLConsumer are clean up.
882status_t GLConsumer::setSynchronousMode(bool enabled) {
Daniel Lamb2675792012-02-23 14:35:13 -0800883 Mutex::Autolock lock(mMutex);
884 return mBufferQueue->setSynchronousMode(enabled);
885}
886
Mathias Agopian74d211a2013-04-22 16:55:35 +0200887void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700888{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200889 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -0700890 "%smTexName=%d mCurrentTexture=%d\n"
891 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
892 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
893 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
894 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700895
Mathias Agopian74d211a2013-04-22 16:55:35 +0200896 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -0700897}
898
Jamie Gennisf238e282011-01-09 16:33:17 -0800899static void mtxMul(float out[16], const float a[16], const float b[16]) {
900 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
901 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
902 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
903 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
904
905 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
906 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
907 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
908 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
909
910 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
911 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
912 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
913 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
914
915 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
916 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
917 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
918 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
919}
920
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800921}; // namespace android