blob: 630eb7c0442a0f102c540b9983501cc0b8351e6c [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 Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/IGraphicBufferAlloc.h>
32#include <gui/ISurfaceComposer.h>
33#include <gui/SurfaceComposerClient.h>
Andy McFadden2adaf042012-12-18 09:49:45 -080034#include <gui/GLConsumer.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080035
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <private/gui/ComposerService.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080037
38#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070039#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080040#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080041
Andy McFadden97eba892012-12-11 15:21:45 -080042namespace android {
43
Andy McFadden2adaf042012-12-18 09:49:45 -080044// This compile option makes GLConsumer use the
Jamie Gennis01dbf552012-09-06 14:54:19 -070045// EGL_ANDROID_native_fence_sync extension to create Android native fences to
46// signal when all GLES reads for a given buffer have completed. It is not
47// compatible with using the EGL_KHR_fence_sync extension for the same
48// purpose.
49#ifdef USE_NATIVE_FENCE_SYNC
50#ifdef USE_FENCE_SYNC
51#error "USE_NATIVE_FENCE_SYNC and USE_FENCE_SYNC are incompatible"
52#endif
Andy McFadden2adaf042012-12-18 09:49:45 -080053const bool GLConsumer::sUseNativeFenceSync = true;
Jamie Gennis01dbf552012-09-06 14:54:19 -070054#else
Andy McFadden2adaf042012-12-18 09:49:45 -080055const bool GLConsumer::sUseNativeFenceSync = false;
Jamie Gennis61e04b92012-09-09 17:48:42 -070056#endif
57
Andy McFadden2adaf042012-12-18 09:49:45 -080058// This compile option makes GLConsumer use the EGL_ANDROID_sync_wait
Jamie Gennis61e04b92012-09-09 17:48:42 -070059// extension to insert server-side waits into the GLES command stream. This
60// feature requires the EGL_ANDROID_native_fence_sync and
61// EGL_ANDROID_wait_sync extensions.
62#ifdef USE_WAIT_SYNC
63static const bool useWaitSync = true;
64#else
65static const bool useWaitSync = false;
Jamie Gennis01dbf552012-09-06 14:54:19 -070066#endif
67
Andy McFadden2adaf042012-12-18 09:49:45 -080068// Macros for including the GLConsumer name in log messages
Steve Block6807e592011-10-20 11:56:00 +010069#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000070#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000071#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000072#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000073#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070074
Jamie Gennisf238e282011-01-09 16:33:17 -080075// Transform matrices
76static float mtxIdentity[16] = {
77 1, 0, 0, 0,
78 0, 1, 0, 0,
79 0, 0, 1, 0,
80 0, 0, 0, 1,
81};
82static float mtxFlipH[16] = {
83 -1, 0, 0, 0,
84 0, 1, 0, 0,
85 0, 0, 1, 0,
86 1, 0, 0, 1,
87};
88static float mtxFlipV[16] = {
89 1, 0, 0, 0,
90 0, -1, 0, 0,
91 0, 0, 1, 0,
92 0, 1, 0, 1,
93};
94static float mtxRot90[16] = {
95 0, 1, 0, 0,
96 -1, 0, 0, 0,
97 0, 0, 1, 0,
98 1, 0, 0, 1,
99};
100static float mtxRot180[16] = {
101 -1, 0, 0, 0,
102 0, -1, 0, 0,
103 0, 0, 1, 0,
104 1, 1, 0, 1,
105};
106static float mtxRot270[16] = {
107 0, -1, 0, 0,
108 1, 0, 0, 0,
109 0, 0, 1, 0,
110 0, 1, 0, 1,
111};
112
113static void mtxMul(float out[16], const float a[16], const float b[16]);
114
Jamie Gennisfa28c352011-09-16 17:30:26 -0700115
Andy McFadden2adaf042012-12-18 09:49:45 -0800116GLConsumer::GLConsumer(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -0800117 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -0700118 ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800119 mCurrentTransform(0),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800120 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800121 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700122 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700123 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800124#ifdef USE_FENCE_SYNC
125 mUseFenceSync(useFenceSync),
126#else
127 mUseFenceSync(false),
128#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800129 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700130 mEglDisplay(EGL_NO_DISPLAY),
131 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700132 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
133 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800134{
Andy McFadden2adaf042012-12-18 09:49:45 -0800135 ST_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800136
Jamie Gennisfa28c352011-09-16 17:30:26 -0700137 memcpy(mCurrentTransformMatrix, mtxIdentity,
138 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700139
Jamie Gennis9fea3422012-08-07 18:03:04 -0700140 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800141}
142
Andy McFadden2adaf042012-12-18 09:49:45 -0800143status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700144 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -0700145 return mBufferQueue->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700146}
147
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800148
Andy McFadden2adaf042012-12-18 09:49:45 -0800149status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700150{
Daniel Lamb2675792012-02-23 14:35:13 -0800151 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700152 mDefaultWidth = w;
153 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800154 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700155}
156
Andy McFadden2adaf042012-12-18 09:49:45 -0800157status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800158 ATRACE_CALL();
159 ST_LOGV("updateTexImage");
160 Mutex::Autolock lock(mMutex);
161
162 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800163 ST_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800164 return NO_INIT;
165 }
166
167 // Make sure the EGL state is the same as in previous calls.
168 status_t err = checkAndUpdateEglStateLocked();
169 if (err != NO_ERROR) {
170 return err;
171 }
172
173 BufferQueue::BufferItem item;
174
175 // Acquire the next buffer.
176 // In asynchronous mode the list is guaranteed to be one buffer
177 // deep, while in synchronous mode we use the oldest buffer.
178 err = acquireBufferLocked(&item);
179 if (err != NO_ERROR) {
180 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
181 // We always bind the texture even if we don't update its contents.
182 ST_LOGV("updateTexImage: no buffers were available");
183 glBindTexture(mTexTarget, mTexName);
184 err = NO_ERROR;
185 } else {
186 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
187 strerror(-err), err);
188 }
189 return err;
190 }
191
192 // Release the previous buffer.
193 err = releaseAndUpdateLocked(item);
194 if (err != NO_ERROR) {
195 // We always bind the texture.
196 glBindTexture(mTexTarget, mTexName);
197 return err;
198 }
199
Andy McFadden97eba892012-12-11 15:21:45 -0800200 // Bind the new buffer to the GL texture, and wait until it's ready.
201 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700202}
203
Andy McFadden2adaf042012-12-18 09:49:45 -0800204status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700205 status_t err = ConsumerBase::acquireBufferLocked(item);
206 if (err != NO_ERROR) {
207 return err;
208 }
209
210 int slot = item->mBuf;
211 if (item->mGraphicBuffer != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800212 // This buffer has not been acquired before, so we must assume
213 // that any EGLImage in mEglSlots is stale.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700214 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800215 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
216 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
217 slot);
218 // keep going
219 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700220 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
221 }
222 }
223
Jamie Gennis9fea3422012-08-07 18:03:04 -0700224 return NO_ERROR;
225}
226
Andy McFadden2adaf042012-12-18 09:49:45 -0800227status_t GLConsumer::releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700228 EGLSyncKHR eglFence) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800229 status_t err = ConsumerBase::releaseBufferLocked(buf, display, eglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700230
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700231 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700232
233 return err;
234}
235
Andy McFadden2adaf042012-12-18 09:49:45 -0800236status_t GLConsumer::releaseAndUpdateLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800237{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700238 status_t err = NO_ERROR;
239
Jamie Gennis74bed552012-03-28 19:05:54 -0700240 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800241 ST_LOGE("releaseAndUpdate: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700242 "ES context");
243 return INVALID_OPERATION;
244 }
245
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800246 // Confirm state.
247 err = checkAndUpdateEglStateLocked();
248 if (err != NO_ERROR) {
249 return err;
250 }
251
252 int buf = item.mBuf;
253
254 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
255 // buffer currently in the slot in ConsumerBase.
256 //
257 // We may have to do this even when item.mGraphicBuffer == NULL (which
258 // means the buffer was previously acquired), if we destroyed the
259 // EGLImage when detaching from a context but the buffer has not been
260 // re-allocated.
261 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
262 EGLImageKHR image = createImage(mEglDisplay, mSlots[buf].mGraphicBuffer);
263 if (image == EGL_NO_IMAGE_KHR) {
264 ST_LOGW("releaseAndUpdate: unable to createImage on display=%p slot=%d",
265 mEglDisplay, buf);
266 return UNKNOWN_ERROR;
267 }
268 mEglSlots[buf].mEglImage = image;
269 }
270
271 // Do whatever sync ops we need to do before releasing the old slot.
272 err = syncForReleaseLocked(mEglDisplay);
273 if (err != NO_ERROR) {
274 // Release the buffer we just acquired. It's not safe to
275 // release the old buffer, so instead we just drop the new frame.
276 releaseBufferLocked(buf, mEglDisplay, EGL_NO_SYNC_KHR);
277 return err;
278 }
279
280 ST_LOGV("releaseAndUpdate: (slot=%d buf=%p) -> (slot=%d buf=%p)",
281 mCurrentTexture,
282 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
283 buf, mSlots[buf].mGraphicBuffer->handle);
284
285 // release old buffer
286 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
287 status_t status = releaseBufferLocked(mCurrentTexture, mEglDisplay,
288 mEglSlots[mCurrentTexture].mEglFence);
289 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
290 ST_LOGE("releaseAndUpdate: failed to release buffer: %s (%d)",
291 strerror(-status), status);
292 err = status;
293 // keep going, with error raised [?]
294 }
295 }
296
Andy McFadden2adaf042012-12-18 09:49:45 -0800297 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800298 mCurrentTexture = buf;
299 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
300 mCurrentCrop = item.mCrop;
301 mCurrentTransform = item.mTransform;
302 mCurrentScalingMode = item.mScalingMode;
303 mCurrentTimestamp = item.mTimestamp;
304 mCurrentFence = item.mFence;
305
306 computeCurrentTransformMatrixLocked();
307
308 return err;
309}
310
Andy McFadden2adaf042012-12-18 09:49:45 -0800311status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800312 if (mEglDisplay == EGL_NO_DISPLAY) {
313 ALOGE("bindTextureImage: invalid display");
314 return INVALID_OPERATION;
315 }
316
317 GLint error;
318 while ((error = glGetError()) != GL_NO_ERROR) {
319 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
320 }
321
322 glBindTexture(mTexTarget, mTexName);
323 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
324 if (mCurrentTextureBuf == NULL) {
325 ST_LOGE("bindTextureImage: no currently-bound texture");
326 return NO_INIT;
327 }
Andy McFadden97eba892012-12-11 15:21:45 -0800328 status_t err = bindUnslottedBufferLocked(mEglDisplay);
329 if (err != NO_ERROR) {
330 return err;
331 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800332 } else {
333 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
334
335 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
336
337 while ((error = glGetError()) != GL_NO_ERROR) {
338 ST_LOGE("bindTextureImage: error binding external texture image %p"
339 ": %#04x", image, error);
340 return UNKNOWN_ERROR;
341 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800342 }
Andy McFadden97eba892012-12-11 15:21:45 -0800343
344 // Wait for the new buffer to be ready.
345 return doGLFenceWaitLocked();
346
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800347}
348
Andy McFadden2adaf042012-12-18 09:49:45 -0800349status_t GLConsumer::checkAndUpdateEglStateLocked() {
Jamie Gennisce561372012-03-19 18:33:05 -0700350 EGLDisplay dpy = eglGetCurrentDisplay();
351 EGLContext ctx = eglGetCurrentContext();
352
Jamie Gennis74bed552012-03-28 19:05:54 -0700353 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
354 dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800355 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700356 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700357 }
358
Jamie Gennis74bed552012-03-28 19:05:54 -0700359 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
360 ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800361 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700362 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700363 }
364
365 mEglDisplay = dpy;
366 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800367 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800368}
369
Jesse Hall13f01cb2013-03-20 11:37:21 -0700370void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
371 if (fence->isValid() &&
372 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
373 status_t err = addReleaseFence(mCurrentTexture, fence);
374 if (err != OK) {
375 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
376 strerror(-err), err);
377 }
Jesse Hallef194142012-06-14 14:45:17 -0700378 }
379}
380
Andy McFadden2adaf042012-12-18 09:49:45 -0800381status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700382 ATRACE_CALL();
383 ST_LOGV("detachFromContext");
384 Mutex::Autolock lock(mMutex);
385
386 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800387 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700388 return NO_INIT;
389 }
390
391 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800392 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700393 "context");
394 return INVALID_OPERATION;
395 }
396
397 EGLDisplay dpy = eglGetCurrentDisplay();
398 EGLContext ctx = eglGetCurrentContext();
399
400 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
401 ST_LOGE("detachFromContext: invalid current EGLDisplay");
402 return INVALID_OPERATION;
403 }
404
405 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
406 ST_LOGE("detachFromContext: invalid current EGLContext");
407 return INVALID_OPERATION;
408 }
409
410 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
411 status_t err = syncForReleaseLocked(dpy);
412 if (err != OK) {
413 return err;
414 }
415
416 glDeleteTextures(1, &mTexName);
417 }
418
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700419 // Because we're giving up the EGLDisplay we need to free all the EGLImages
420 // that are associated with it. They'll be recreated when the
Andy McFadden2adaf042012-12-18 09:49:45 -0800421 // GLConsumer gets attached to a new OpenGL ES context (and thus gets a
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700422 // new EGLDisplay).
423 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700424 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700425 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700426 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700427 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700428 }
429 }
430
Jamie Gennis74bed552012-03-28 19:05:54 -0700431 mEglDisplay = EGL_NO_DISPLAY;
432 mEglContext = EGL_NO_CONTEXT;
433 mAttached = false;
434
435 return OK;
436}
437
Andy McFadden2adaf042012-12-18 09:49:45 -0800438status_t GLConsumer::attachToContext(GLuint tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700439 ATRACE_CALL();
440 ST_LOGV("attachToContext");
441 Mutex::Autolock lock(mMutex);
442
443 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800444 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700445 return NO_INIT;
446 }
447
448 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800449 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700450 "context");
451 return INVALID_OPERATION;
452 }
453
454 EGLDisplay dpy = eglGetCurrentDisplay();
455 EGLContext ctx = eglGetCurrentContext();
456
457 if (dpy == EGL_NO_DISPLAY) {
458 ST_LOGE("attachToContext: invalid current EGLDisplay");
459 return INVALID_OPERATION;
460 }
461
462 if (ctx == EGL_NO_CONTEXT) {
463 ST_LOGE("attachToContext: invalid current EGLContext");
464 return INVALID_OPERATION;
465 }
466
467 // We need to bind the texture regardless of whether there's a current
468 // buffer.
469 glBindTexture(mTexTarget, tex);
470
471 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700472 // The EGLImageKHR that was associated with the slot was destroyed when
Andy McFadden2adaf042012-12-18 09:49:45 -0800473 // the GLConsumer was detached from the old context, so we need to
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700474 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800475 status_t err = bindUnslottedBufferLocked(dpy);
476 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700477 return err;
478 }
479 }
480
481 mEglDisplay = dpy;
482 mEglContext = ctx;
483 mTexName = tex;
484 mAttached = true;
485
486 return OK;
487}
488
Andy McFadden2adaf042012-12-18 09:49:45 -0800489status_t GLConsumer::bindUnslottedBufferLocked(EGLDisplay dpy) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800490 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
491 mCurrentTexture, mCurrentTextureBuf.get());
492
493 // Create a temporary EGLImageKHR.
494 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
495 if (image == EGL_NO_IMAGE_KHR) {
496 return UNKNOWN_ERROR;
497 }
498
499 // Attach the current buffer to the GL texture.
500 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
501
502 GLint error;
503 status_t err = OK;
504 while ((error = glGetError()) != GL_NO_ERROR) {
505 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
506 "(slot %d): %#04x", image, mCurrentTexture, error);
507 err = UNKNOWN_ERROR;
508 }
509
510 // We destroy the EGLImageKHR here because the current buffer may no
511 // longer be associated with one of the buffer slots, so we have
512 // nowhere to to store it. If the buffer is still associated with a
513 // slot then another EGLImageKHR will be created next time that buffer
514 // gets acquired in updateTexImage.
515 eglDestroyImageKHR(dpy, image);
516
517 return err;
518}
519
520
Andy McFadden2adaf042012-12-18 09:49:45 -0800521status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700522 ST_LOGV("syncForReleaseLocked");
523
Jamie Gennis01dbf552012-09-06 14:54:19 -0700524 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Andy McFadden97eba892012-12-11 15:21:45 -0800525 if (sUseNativeFenceSync) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700526 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
527 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
528 if (sync == EGL_NO_SYNC_KHR) {
529 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
530 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700531 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700532 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700533 glFlush();
534 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700535 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700536 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
537 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
538 "fd: %#x", eglGetError());
539 return UNKNOWN_ERROR;
540 }
541 sp<Fence> fence(new Fence(fenceFd));
Jesse Hall9504eb92012-10-05 14:34:21 -0700542 status_t err = addReleaseFenceLocked(mCurrentTexture, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700543 if (err != OK) {
544 ST_LOGE("syncForReleaseLocked: error adding release fence: "
545 "%s (%d)", strerror(-err), err);
546 return err;
547 }
548 } else if (mUseFenceSync) {
549 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
550 if (fence != EGL_NO_SYNC_KHR) {
551 // There is already a fence for the current slot. We need to
552 // wait on that before replacing it with another fence to
553 // ensure that all outstanding buffer accesses have completed
554 // before the producer accesses it.
555 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
556 if (result == EGL_FALSE) {
557 ST_LOGE("syncForReleaseLocked: error waiting for previous "
558 "fence: %#x", eglGetError());
559 return UNKNOWN_ERROR;
560 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
561 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
562 "fence");
563 return TIMED_OUT;
564 }
565 eglDestroySyncKHR(dpy, fence);
566 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700567
Jamie Gennis01dbf552012-09-06 14:54:19 -0700568 // Create a fence for the outstanding accesses in the current
569 // OpenGL ES context.
570 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
571 if (fence == EGL_NO_SYNC_KHR) {
572 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
573 eglGetError());
574 return UNKNOWN_ERROR;
575 }
576 glFlush();
577 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700579 }
580
581 return OK;
582}
583
Andy McFadden2adaf042012-12-18 09:49:45 -0800584bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700585{
586 switch (format) {
587 // supported YUV formats
588 case HAL_PIXEL_FORMAT_YV12:
589 // Legacy/deprecated YUV formats
590 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
591 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
592 case HAL_PIXEL_FORMAT_YCbCr_422_I:
593 return true;
594 }
595
596 // Any OEM format needs to be considered
597 if (format>=0x100 && format<=0x1FF)
598 return true;
599
600 return false;
601}
602
Andy McFadden2adaf042012-12-18 09:49:45 -0800603GLenum GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700604 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700605}
606
Andy McFadden2adaf042012-12-18 09:49:45 -0800607void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800608 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700609 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
610}
611
Andy McFadden2adaf042012-12-18 09:49:45 -0800612void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700613 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700614 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800615 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700616 return;
617 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700618 bool needsRecompute = mFilteringEnabled != enabled;
619 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700620
621 if (needsRecompute && mCurrentTextureBuf==NULL) {
622 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
623 }
624
625 if (needsRecompute && mCurrentTextureBuf != NULL) {
626 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700627 }
628}
629
Andy McFadden2adaf042012-12-18 09:49:45 -0800630void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700631 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800632
Jamie Gennisa214c642011-01-14 13:53:31 -0800633 float xform[16];
634 for (int i = 0; i < 16; i++) {
635 xform[i] = mtxIdentity[i];
636 }
637 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
638 float result[16];
639 mtxMul(result, xform, mtxFlipH);
640 for (int i = 0; i < 16; i++) {
641 xform[i] = result[i];
642 }
643 }
644 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
645 float result[16];
646 mtxMul(result, xform, mtxFlipV);
647 for (int i = 0; i < 16; i++) {
648 xform[i] = result[i];
649 }
650 }
651 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
652 float result[16];
653 mtxMul(result, xform, mtxRot90);
654 for (int i = 0; i < 16; i++) {
655 xform[i] = result[i];
656 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800657 }
658
Daniel Lameae59d22012-01-22 15:26:27 -0800659 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700660
661 if (buf == NULL) {
662 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
663 }
664
Jamie Gennisd72f2332012-05-07 13:50:11 -0700665 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700666 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
667 float bufferWidth = buf->getWidth();
668 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700669 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700670 float shrinkAmount = 0.0f;
671 if (mFilteringEnabled) {
672 // In order to prevent bilinear sampling beyond the edge of the
673 // crop rectangle we may need to shrink it by 2 texels in each
674 // dimension. Normally this would just need to take 1/2 a texel
675 // off each end, but because the chroma channels of YUV420 images
676 // are subsampled we may need to shrink the crop region by a whole
677 // texel on each side.
678 switch (buf->getPixelFormat()) {
679 case PIXEL_FORMAT_RGBA_8888:
680 case PIXEL_FORMAT_RGBX_8888:
681 case PIXEL_FORMAT_RGB_888:
682 case PIXEL_FORMAT_RGB_565:
683 case PIXEL_FORMAT_BGRA_8888:
684 case PIXEL_FORMAT_RGBA_5551:
685 case PIXEL_FORMAT_RGBA_4444:
686 // We know there's no subsampling of any channels, so we
687 // only need to shrink by a half a pixel.
688 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700689 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700690
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700691 default:
692 // If we don't recognize the format, we must assume the
693 // worst case (that we care about), which is YUV420.
694 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700695 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700696 }
697 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700698
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700699 // Only shrink the dimensions that are not the size of the buffer.
700 if (cropRect.width() < bufferWidth) {
701 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
702 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
703 bufferWidth;
704 }
705 if (cropRect.height() < bufferHeight) {
706 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
707 bufferHeight;
708 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
709 bufferHeight;
710 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800711 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800712 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800713 sx, 0, 0, 0,
714 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800715 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800716 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800717 };
718
Jamie Gennisa214c642011-01-14 13:53:31 -0800719 float mtxBeforeFlipV[16];
720 mtxMul(mtxBeforeFlipV, crop, xform);
721
722 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800723 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800724 // want to expose this to applications, however, so we must add an
725 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700726 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800727}
728
Andy McFadden2adaf042012-12-18 09:49:45 -0800729nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700730 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800731 Mutex::Autolock lock(mMutex);
732 return mCurrentTimestamp;
733}
734
Andy McFadden2adaf042012-12-18 09:49:45 -0800735EGLImageKHR GLConsumer::createImage(EGLDisplay dpy,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800736 const sp<GraphicBuffer>& graphicBuffer) {
737 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
738 EGLint attrs[] = {
739 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
740 EGL_NONE,
741 };
742 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
743 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700744 if (image == EGL_NO_IMAGE_KHR) {
745 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700746 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800747 }
748 return image;
749}
750
Andy McFadden2adaf042012-12-18 09:49:45 -0800751sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700752 Mutex::Autolock lock(mMutex);
753 return mCurrentTextureBuf;
754}
755
Andy McFadden2adaf042012-12-18 09:49:45 -0800756Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700757 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700758
759 Rect outCrop = mCurrentCrop;
760 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
761 int32_t newWidth = mCurrentCrop.width();
762 int32_t newHeight = mCurrentCrop.height();
763
764 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
765 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
766 ST_LOGV("too wide: newWidth = %d", newWidth);
767 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
768 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
769 ST_LOGV("too tall: newHeight = %d", newHeight);
770 }
771
772 // The crop is too wide
773 if (newWidth < mCurrentCrop.width()) {
774 int32_t dw = (newWidth - mCurrentCrop.width())/2;
775 outCrop.left -=dw;
776 outCrop.right += dw;
777 // The crop is too tall
778 } else if (newHeight < mCurrentCrop.height()) {
779 int32_t dh = (newHeight - mCurrentCrop.height())/2;
780 outCrop.top -= dh;
781 outCrop.bottom += dh;
782 }
783
784 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
785 outCrop.left, outCrop.top,
786 outCrop.right,outCrop.bottom);
787 }
788
Daniel Lam016c8cb2012-04-03 15:54:58 -0700789 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700790}
791
Andy McFadden2adaf042012-12-18 09:49:45 -0800792uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700793 Mutex::Autolock lock(mMutex);
794 return mCurrentTransform;
795}
796
Andy McFadden2adaf042012-12-18 09:49:45 -0800797uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700798 Mutex::Autolock lock(mMutex);
799 return mCurrentScalingMode;
800}
801
Andy McFadden2adaf042012-12-18 09:49:45 -0800802sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700803 Mutex::Autolock lock(mMutex);
804 return mCurrentFence;
805}
806
Andy McFadden2adaf042012-12-18 09:49:45 -0800807status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700808 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700809 return doGLFenceWaitLocked();
810}
811
Andy McFadden2adaf042012-12-18 09:49:45 -0800812status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700813
814 EGLDisplay dpy = eglGetCurrentDisplay();
815 EGLContext ctx = eglGetCurrentContext();
816
817 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
818 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
819 return INVALID_OPERATION;
820 }
821
822 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
823 ST_LOGE("doGLFenceWait: invalid current EGLContext");
824 return INVALID_OPERATION;
825 }
826
Jamie Gennis1df8c342012-12-20 14:05:45 -0800827 if (mCurrentFence->isValid()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700828 if (useWaitSync) {
829 // Create an EGLSyncKHR from the current fence.
830 int fenceFd = mCurrentFence->dup();
831 if (fenceFd == -1) {
832 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
833 return -errno;
834 }
835 EGLint attribs[] = {
836 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
837 EGL_NONE
838 };
839 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
840 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
841 if (sync == EGL_NO_SYNC_KHR) {
842 close(fenceFd);
843 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
844 eglGetError());
845 return UNKNOWN_ERROR;
846 }
847
848 // XXX: The spec draft is inconsistent as to whether this should
849 // return an EGLint or void. Ignore the return value for now, as
850 // it's not strictly needed.
851 eglWaitSyncANDROID(dpy, sync, 0);
852 EGLint eglErr = eglGetError();
853 eglDestroySyncKHR(dpy, sync);
854 if (eglErr != EGL_SUCCESS) {
855 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
856 eglErr);
857 return UNKNOWN_ERROR;
858 }
859 } else {
Jesse Hallba607d52012-10-01 14:05:20 -0700860 status_t err = mCurrentFence->waitForever(1000,
Andy McFadden2adaf042012-12-18 09:49:45 -0800861 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700862 if (err != NO_ERROR) {
863 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
864 return err;
865 }
866 }
867 }
868
869 return NO_ERROR;
870}
871
Andy McFadden2adaf042012-12-18 09:49:45 -0800872bool GLConsumer::isSynchronousMode() const {
Jamie Gennis59769462011-11-19 18:04:43 -0800873 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800874 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800875}
876
Andy McFadden2adaf042012-12-18 09:49:45 -0800877void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700878 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700879 if (slotIndex == mCurrentTexture) {
880 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
881 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700882 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700883 if (img != EGL_NO_IMAGE_KHR) {
884 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
885 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800886 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700887 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
888 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700889}
Daniel Lameae59d22012-01-22 15:26:27 -0800890
Andy McFadden2adaf042012-12-18 09:49:45 -0800891void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700892 ST_LOGV("abandonLocked");
893 mCurrentTextureBuf.clear();
894 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700895}
896
Andy McFadden2adaf042012-12-18 09:49:45 -0800897void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800898 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700899 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800900 mBufferQueue->setConsumerName(name);
901}
902
Andy McFadden2adaf042012-12-18 09:49:45 -0800903status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -0800904 Mutex::Autolock lock(mMutex);
905 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
906}
907
Andy McFadden2adaf042012-12-18 09:49:45 -0800908status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -0800909 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700910 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800911 return mBufferQueue->setConsumerUsageBits(usage);
912}
913
Andy McFadden2adaf042012-12-18 09:49:45 -0800914status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -0800915 Mutex::Autolock lock(mMutex);
916 return mBufferQueue->setTransformHint(hint);
917}
918
Andy McFadden2adaf042012-12-18 09:49:45 -0800919// Used for refactoring BufferQueue from GLConsumer
920// Should not be in final interface once users of GLConsumer are clean up.
921status_t GLConsumer::setSynchronousMode(bool enabled) {
Daniel Lamb2675792012-02-23 14:35:13 -0800922 Mutex::Autolock lock(mMutex);
923 return mBufferQueue->setSynchronousMode(enabled);
924}
925
Andy McFadden2adaf042012-12-18 09:49:45 -0800926void GLConsumer::dumpLocked(String8& result, const char* prefix,
Jamie Gennis9fea3422012-08-07 18:03:04 -0700927 char* buffer, size_t size) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700928{
Jamie Gennis9fea3422012-08-07 18:03:04 -0700929 snprintf(buffer, size,
930 "%smTexName=%d mCurrentTexture=%d\n"
931 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
932 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
933 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
934 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700935 result.append(buffer);
936
Jamie Gennis9fea3422012-08-07 18:03:04 -0700937 ConsumerBase::dumpLocked(result, prefix, buffer, size);
Mathias Agopian68c77942011-05-09 19:08:33 -0700938}
939
Jamie Gennisf238e282011-01-09 16:33:17 -0800940static void mtxMul(float out[16], const float a[16], const float b[16]) {
941 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
942 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
943 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
944 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
945
946 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
947 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
948 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
949 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
950
951 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
952 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
953 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
954 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
955
956 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
957 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
958 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
959 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
960}
961
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800962}; // namespace android