blob: fcd287c022767d1f8fd5b82f53a2f08e08e54059 [file] [log] [blame]
Jamie Gennis68e4a7a2010-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
17#define LOG_TAG "SurfaceTexture"
Jamie Gennis7dc00d52011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian27cd07c2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennisf7acf162011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian7f3289c2011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080038
Jamie Gennis77cec6132011-11-14 14:51:01 -080039// This compile option causes SurfaceTexture to return the buffer that is currently
40// attached to the GL texture from dequeueBuffer when no other buffers are
41// available. It requires the drivers (Gralloc, GL, OMX IL, and Camera) to do
42// implicit cross-process synchronization to prevent the buffer from being
43// written to before the buffer has (a) been detached from the GL texture and
44// (b) all GL reads from the buffer have completed.
Mathias Agopiane2fa30c2011-11-14 19:17:37 -080045#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
46#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER true
47#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
48#else
49#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER false
50#endif
Mathias Agopian8618ebc2011-08-17 15:42:04 -070051
Jamie Gennis77cec6132011-11-14 14:51:01 -080052// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
53// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
54// waiting for the GL reads for the buffer being dequeued to complete before
55// allowing the buffer to be dequeued.
56#ifdef USE_FENCE_SYNC
57#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
58#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
59#endif
60#endif
61
Jamie Gennis44a05222011-09-16 17:30:26 -070062// Macros for including the SurfaceTexture name in log messages
63#define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
64#define ST_LOGD(x, ...) LOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
65#define ST_LOGI(x, ...) LOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
66#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
67#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian8618ebc2011-08-17 15:42:04 -070068
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080069namespace android {
70
Jamie Gennisb598fb92011-01-09 16:33:17 -080071// Transform matrices
72static float mtxIdentity[16] = {
73 1, 0, 0, 0,
74 0, 1, 0, 0,
75 0, 0, 1, 0,
76 0, 0, 0, 1,
77};
78static float mtxFlipH[16] = {
79 -1, 0, 0, 0,
80 0, 1, 0, 0,
81 0, 0, 1, 0,
82 1, 0, 0, 1,
83};
84static float mtxFlipV[16] = {
85 1, 0, 0, 0,
86 0, -1, 0, 0,
87 0, 0, 1, 0,
88 0, 1, 0, 1,
89};
90static float mtxRot90[16] = {
91 0, 1, 0, 0,
92 -1, 0, 0, 0,
93 0, 0, 1, 0,
94 1, 0, 0, 1,
95};
96static float mtxRot180[16] = {
97 -1, 0, 0, 0,
98 0, -1, 0, 0,
99 0, 0, 1, 0,
100 1, 1, 0, 1,
101};
102static float mtxRot270[16] = {
103 0, -1, 0, 0,
104 1, 0, 0, 0,
105 0, 0, 1, 0,
106 0, 1, 0, 1,
107};
108
109static void mtxMul(float out[16], const float a[16], const float b[16]);
110
Jamie Gennis44a05222011-09-16 17:30:26 -0700111// Get an ID that's unique within this process.
112static int32_t createProcessUniqueId() {
113 static volatile int32_t globalCounter = 0;
114 return android_atomic_inc(&globalCounter);
115}
116
Jamie Gennis8606fef2011-09-28 12:13:31 -0700117SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Jamie Gennis77cec6132011-11-14 14:51:01 -0800118 GLenum texTarget, bool useFenceSync) :
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700119 mDefaultWidth(1),
120 mDefaultHeight(1),
121 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian402ff242011-05-02 19:51:12 -0700122 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
123 mClientBufferCount(0),
124 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800125 mCurrentTexture(INVALID_BUFFER_SLOT),
126 mCurrentTransform(0),
127 mCurrentTimestamp(0),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800128 mNextTransform(0),
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700129 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700130 mTexName(tex),
Grace Kloba0904d0a2011-06-23 21:21:47 -0700131 mSynchronousMode(false),
Jamie Gennis97096872011-07-13 19:12:20 -0700132 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700133 mConnectedApi(NO_CONNECTED_API),
Jamie Gennis8606fef2011-09-28 12:13:31 -0700134 mAbandoned(false),
Jamie Gennis77cec6132011-11-14 14:51:01 -0800135#ifdef USE_FENCE_SYNC
136 mUseFenceSync(useFenceSync),
137#else
138 mUseFenceSync(false),
139#endif
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600140 mTexTarget(texTarget),
141 mFrameCounter(0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700142 // Choose a name using the PID and a process-unique ID.
143 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
144
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700145 ST_LOGV("SurfaceTexture");
Jamie Gennisf7acf162011-01-12 18:30:40 -0800146 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
147 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopian926340c2011-04-27 18:57:33 -0700148 mNextCrop.makeInvalid();
Jamie Gennis44a05222011-09-16 17:30:26 -0700149 memcpy(mCurrentTransformMatrix, mtxIdentity,
150 sizeof(mCurrentTransformMatrix));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800151}
152
153SurfaceTexture::~SurfaceTexture() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700154 ST_LOGV("~SurfaceTexture");
Mathias Agopiana04cda92011-08-10 15:28:58 -0700155 freeAllBuffersLocked();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800156}
157
Mathias Agopian402ff242011-05-02 19:51:12 -0700158status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
159 if (bufferCount > NUM_BUFFER_SLOTS)
160 return BAD_VALUE;
161
162 // special-case, nothing to do
163 if (bufferCount == mBufferCount)
164 return OK;
165
166 if (!mClientBufferCount &&
167 bufferCount >= mBufferCount) {
168 // easy, we just have more buffers
169 mBufferCount = bufferCount;
170 mServerBufferCount = bufferCount;
171 mDequeueCondition.signal();
172 } else {
173 // we're here because we're either
174 // - reducing the number of available buffers
175 // - or there is a client-buffer-count in effect
176
177 // less than 2 buffers is never allowed
178 if (bufferCount < 2)
179 return BAD_VALUE;
180
181 // when there is non client-buffer-count in effect, the client is not
182 // allowed to dequeue more than one buffer at a time,
183 // so the next time they dequeue a buffer, we know that they don't
184 // own one. the actual resizing will happen during the next
185 // dequeueBuffer.
186
187 mServerBufferCount = bufferCount;
188 }
189 return OK;
190}
191
192status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
193 Mutex::Autolock lock(mMutex);
194 return setBufferCountServerLocked(bufferCount);
195}
196
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800197status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700198 ST_LOGV("setBufferCount: count=%d", bufferCount);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700199 Mutex::Autolock lock(mMutex);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800200
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700201 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700202 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700203 return NO_INIT;
204 }
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700205 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700206 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700207 return BAD_VALUE;
208 }
209
Mathias Agopian402ff242011-05-02 19:51:12 -0700210 // Error out if the user has dequeued buffers
211 for (int i=0 ; i<mBufferCount ; i++) {
212 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700213 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian402ff242011-05-02 19:51:12 -0700214 return -EINVAL;
215 }
216 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700217
Jamie Gennisd995b082011-07-30 16:00:11 -0700218 const int minBufferSlots = mSynchronousMode ?
219 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian402ff242011-05-02 19:51:12 -0700220 if (bufferCount == 0) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700221 mClientBufferCount = 0;
222 bufferCount = (mServerBufferCount >= minBufferSlots) ?
223 mServerBufferCount : minBufferSlots;
224 return setBufferCountServerLocked(bufferCount);
225 }
226
Jamie Gennisd995b082011-07-30 16:00:11 -0700227 if (bufferCount < minBufferSlots) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700228 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennisd995b082011-07-30 16:00:11 -0700229 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800230 return BAD_VALUE;
231 }
232
Mathias Agopian402ff242011-05-02 19:51:12 -0700233 // here we're guaranteed that the client doesn't have dequeued buffers
234 // and will release all of its buffer references.
Mathias Agopiana04cda92011-08-10 15:28:58 -0700235 freeAllBuffersLocked();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800236 mBufferCount = bufferCount;
Mathias Agopian402ff242011-05-02 19:51:12 -0700237 mClientBufferCount = bufferCount;
Jamie Gennisd369dc42011-01-09 13:25:39 -0800238 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700239 mQueue.clear();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700240 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800241 return OK;
242}
243
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700244status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
245{
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700246 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopianf3503c22011-07-25 19:56:08 -0700247 if (!w || !h) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700248 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
249 w, h);
Mathias Agopianf3503c22011-07-25 19:56:08 -0700250 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700251 }
Mathias Agopianf3503c22011-07-25 19:56:08 -0700252
253 Mutex::Autolock lock(mMutex);
254 mDefaultWidth = w;
255 mDefaultHeight = h;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700256 return OK;
257}
258
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700259status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700260 ST_LOGV("requestBuffer: slot=%d", slot);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800261 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700262 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700263 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700264 return NO_INIT;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800265 }
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700266 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700267 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700268 mBufferCount, slot);
269 return BAD_VALUE;
270 }
271 mSlots[slot].mRequestBufferCalled = true;
272 *buf = mSlots[slot].mGraphicBuffer;
273 return NO_ERROR;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700274}
275
276status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
277 uint32_t format, uint32_t usage) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700278 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700279
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700280 if ((w && !h) || (!w && h)) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700281 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700282 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700283 }
284
Mathias Agopian402ff242011-05-02 19:51:12 -0700285 status_t returnFlags(OK);
Jamie Gennis77cec6132011-11-14 14:51:01 -0800286 EGLDisplay dpy = EGL_NO_DISPLAY;
287 EGLSyncKHR fence = EGL_NO_SYNC_KHR;
Mathias Agopian402ff242011-05-02 19:51:12 -0700288
Jamie Gennis77cec6132011-11-14 14:51:01 -0800289 { // Scope for the lock
290 Mutex::Autolock lock(mMutex);
Mathias Agopian71fd1212011-08-10 16:33:23 -0700291
Jamie Gennis77cec6132011-11-14 14:51:01 -0800292 int found = -1;
293 int foundSync = -1;
294 int dequeuedCount = 0;
295 bool tryAgain = true;
296 while (tryAgain) {
297 if (mAbandoned) {
298 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
299 return NO_INIT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700300 }
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700301
Jamie Gennis77cec6132011-11-14 14:51:01 -0800302 // We need to wait for the FIFO to drain if the number of buffer
303 // needs to change.
304 //
305 // The condition "number of buffers needs to change" is true if
306 // - the client doesn't care about how many buffers there are
307 // - AND the actual number of buffer is different from what was
308 // set in the last setBufferCountServer()
309 // - OR -
310 // setBufferCountServer() was set to a value incompatible with
311 // the synchronization mode (for instance because the sync mode
312 // changed since)
313 //
314 // As long as this condition is true AND the FIFO is not empty, we
315 // wait on mDequeueCondition.
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700316
Jamie Gennis77cec6132011-11-14 14:51:01 -0800317 const int minBufferCountNeeded = mSynchronousMode ?
318 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
319
320 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
321 ((mServerBufferCount != mBufferCount) ||
322 (mServerBufferCount < minBufferCountNeeded));
323
324 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
325 // wait for the FIFO to drain
326 mDequeueCondition.wait(mMutex);
327 // NOTE: we continue here because we need to reevaluate our
328 // whole state (eg: we could be abandoned or disconnected)
329 continue;
330 }
331
332 if (numberOfBuffersNeedsToChange) {
333 // here we're guaranteed that mQueue is empty
334 freeAllBuffersLocked();
335 mBufferCount = mServerBufferCount;
336 if (mBufferCount < minBufferCountNeeded)
337 mBufferCount = minBufferCountNeeded;
338 mCurrentTexture = INVALID_BUFFER_SLOT;
339 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
340 }
341
342 // look for a free buffer to give to the client
343 found = INVALID_BUFFER_SLOT;
344 foundSync = INVALID_BUFFER_SLOT;
345 dequeuedCount = 0;
346 for (int i = 0; i < mBufferCount; i++) {
347 const int state = mSlots[i].mBufferState;
348 if (state == BufferSlot::DEQUEUED) {
349 dequeuedCount++;
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700350 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800351
352 // if buffer is FREE it CANNOT be current
353 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
354 "dequeueBuffer: buffer %d is both FREE and current!",
355 i);
356
357 if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) {
358 if (state == BufferSlot::FREE || i == mCurrentTexture) {
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600359 foundSync = i;
Jamie Gennis77cec6132011-11-14 14:51:01 -0800360 if (i != mCurrentTexture) {
361 found = i;
362 break;
363 }
364 }
365 } else {
366 if (state == BufferSlot::FREE) {
367 /* We return the oldest of the free buffers to avoid
368 * stalling the producer if possible. This is because
369 * the consumer may still have pending reads of the
370 * buffers in flight.
371 */
372 bool isOlder = mSlots[i].mFrameNumber <
373 mSlots[found].mFrameNumber;
374 if (found < 0 || isOlder) {
375 foundSync = i;
376 found = i;
377 }
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600378 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700379 }
380 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700381
Jamie Gennis77cec6132011-11-14 14:51:01 -0800382 // clients are not allowed to dequeue more than one buffer
383 // if they didn't set a buffer count.
384 if (!mClientBufferCount && dequeuedCount) {
385 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
386 "setting the buffer count");
387 return -EINVAL;
388 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700389
Jamie Gennis77cec6132011-11-14 14:51:01 -0800390 // See whether a buffer has been queued since the last
391 // setBufferCount so we know whether to perform the
392 // MIN_UNDEQUEUED_BUFFERS check below.
393 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
394 if (bufferHasBeenQueued) {
395 // make sure the client is not trying to dequeue more buffers
396 // than allowed.
397 const int avail = mBufferCount - (dequeuedCount+1);
398 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
399 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
400 "(dequeued=%d)",
401 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
402 dequeuedCount);
403 return -EBUSY;
404 }
405 }
406
407 // we're in synchronous mode and didn't find a buffer, we need to
408 // wait for some buffers to be consumed
409 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
410 if (tryAgain) {
411 mDequeueCondition.wait(mMutex);
Jamie Gennis44e9e0d2011-05-23 18:44:04 -0700412 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700413 }
414
Jamie Gennis77cec6132011-11-14 14:51:01 -0800415 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
416 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
417 found = foundSync;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700418 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700419
Jamie Gennis77cec6132011-11-14 14:51:01 -0800420 if (found == INVALID_BUFFER_SLOT) {
421 // This should not happen.
422 ST_LOGE("dequeueBuffer: no available buffer slots");
423 return -EBUSY;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700424 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800425
426 const int buf = found;
427 *outBuf = found;
428
429 const bool useDefaultSize = !w && !h;
430 if (useDefaultSize) {
431 // use the default size
432 w = mDefaultWidth;
433 h = mDefaultHeight;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700434 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800435
436 const bool updateFormat = (format != 0);
437 if (!updateFormat) {
438 // keep the current (or default) format
439 format = mPixelFormat;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800440 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800441
442 // buffer is now in DEQUEUED (but can also be current at the same time,
443 // if we're in synchronous mode)
444 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
445
446 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
447 if ((buffer == NULL) ||
448 (uint32_t(buffer->width) != w) ||
449 (uint32_t(buffer->height) != h) ||
450 (uint32_t(buffer->format) != format) ||
451 ((uint32_t(buffer->usage) & usage) != usage))
452 {
453 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
454 status_t error;
455 sp<GraphicBuffer> graphicBuffer(
456 mGraphicBufferAlloc->createGraphicBuffer(
457 w, h, format, usage, &error));
458 if (graphicBuffer == 0) {
459 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
460 "failed");
461 return error;
462 }
463 if (updateFormat) {
464 mPixelFormat = format;
465 }
466 mSlots[buf].mGraphicBuffer = graphicBuffer;
467 mSlots[buf].mRequestBufferCalled = false;
468 mSlots[buf].mFence = EGL_NO_SYNC_KHR;
469 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
470 eglDestroyImageKHR(mSlots[buf].mEglDisplay,
471 mSlots[buf].mEglImage);
472 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
473 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
474 }
475 if (mCurrentTexture == buf) {
476 // The current texture no longer references the buffer in this slot
477 // since we just allocated a new buffer.
478 mCurrentTexture = INVALID_BUFFER_SLOT;
479 }
480 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Jamie Gennise36d05482011-11-17 16:00:44 -0800481 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800482
483 dpy = mSlots[buf].mEglDisplay;
484 fence = mSlots[buf].mFence;
485 mSlots[buf].mFence = EGL_NO_SYNC_KHR;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700486 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800487
488 if (fence != EGL_NO_SYNC_KHR) {
489 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
490 // If something goes wrong, log the error, but return the buffer without
491 // synchronizing access to it. It's too late at this point to abort the
492 // dequeue operation.
493 if (result == EGL_FALSE) {
494 LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
495 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
496 LOGE("dequeueBuffer: timeout waiting for fence");
497 }
498 eglDestroySyncKHR(dpy, fence);
499 }
500
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700501 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
502 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Jamie Gennis77cec6132011-11-14 14:51:01 -0800503
Mathias Agopian402ff242011-05-02 19:51:12 -0700504 return returnFlags;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800505}
506
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700507status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700508 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700509 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700510
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700511 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700512 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700513 return NO_INIT;
514 }
515
Mathias Agopian402ff242011-05-02 19:51:12 -0700516 status_t err = OK;
Grace Kloba0904d0a2011-06-23 21:21:47 -0700517 if (!mAllowSynchronousMode && enabled)
518 return err;
519
Mathias Agopian402ff242011-05-02 19:51:12 -0700520 if (!enabled) {
521 // going to asynchronous mode, drain the queue
Mathias Agopian5c715752011-08-10 17:35:09 -0700522 err = drainQueueLocked();
523 if (err != NO_ERROR)
524 return err;
Mathias Agopian402ff242011-05-02 19:51:12 -0700525 }
526
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700527 if (mSynchronousMode != enabled) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700528 // - if we're going to asynchronous mode, the queue is guaranteed to be
529 // empty here
530 // - if the client set the number of buffers, we're guaranteed that
531 // we have at least 3 (because we don't allow less)
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700532 mSynchronousMode = enabled;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700533 mDequeueCondition.signal();
534 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700535 return err;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700536}
537
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700538status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
539 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700540 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiane845c352011-05-11 15:05:29 -0700541
542 sp<FrameAvailableListener> listener;
543
544 { // scope for the lock
Jamie Gennisa2187152011-05-19 13:33:00 -0700545 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700546 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700547 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700548 return NO_INIT;
549 }
Jamie Gennisa2187152011-05-19 13:33:00 -0700550 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700551 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennisa2187152011-05-19 13:33:00 -0700552 mBufferCount, buf);
553 return -EINVAL;
554 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700555 ST_LOGE("queueBuffer: slot %d is not owned by the client "
556 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennisa2187152011-05-19 13:33:00 -0700557 return -EINVAL;
558 } else if (buf == mCurrentTexture) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700559 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennisa2187152011-05-19 13:33:00 -0700560 return -EINVAL;
561 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700562 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennisa2187152011-05-19 13:33:00 -0700563 "buffer", buf);
564 return -EINVAL;
565 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700566
Jamie Gennisa2187152011-05-19 13:33:00 -0700567 if (mSynchronousMode) {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700568 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700569 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700570
571 // Synchronous mode always signals that an additional frame should
572 // be consumed.
573 listener = mFrameAvailableListener;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700574 } else {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700575 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennisa2187152011-05-19 13:33:00 -0700576 if (mQueue.empty()) {
577 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700578
579 // Asynchronous mode only signals that a frame should be
580 // consumed if no previous frame was pending. If a frame were
581 // pending then the consumer would have already been notified.
582 listener = mFrameAvailableListener;
Jamie Gennisa2187152011-05-19 13:33:00 -0700583 } else {
584 Fifo::iterator front(mQueue.begin());
585 // buffer currently queued is freed
586 mSlots[*front].mBufferState = BufferSlot::FREE;
587 // and we record the new buffer index in the queued list
588 *front = buf;
589 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700590 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700591
Jamie Gennisa2187152011-05-19 13:33:00 -0700592 mSlots[buf].mBufferState = BufferSlot::QUEUED;
593 mSlots[buf].mCrop = mNextCrop;
594 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700595 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700596 mSlots[buf].mTimestamp = timestamp;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600597 mFrameCounter++;
598 mSlots[buf].mFrameNumber = mFrameCounter;
599
Jamie Gennisa2187152011-05-19 13:33:00 -0700600 mDequeueCondition.signal();
Mathias Agopian1a227432011-08-17 12:45:40 -0700601
602 *outWidth = mDefaultWidth;
603 *outHeight = mDefaultHeight;
604 *outTransform = 0;
Mathias Agopiane845c352011-05-11 15:05:29 -0700605 } // scope for the lock
606
607 // call back without lock held
608 if (listener != 0) {
609 listener->onFrameAvailable();
610 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800611 return OK;
612}
613
614void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700615 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800616 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700617
618 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700619 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700620 return;
621 }
622
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700623 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700624 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700625 mBufferCount, buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800626 return;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700627 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700628 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700629 buf, mSlots[buf].mBufferState);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800630 return;
631 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700632 mSlots[buf].mBufferState = BufferSlot::FREE;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600633 mSlots[buf].mFrameNumber = 0;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700634 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800635}
636
Jamie Gennisb598fb92011-01-09 16:33:17 -0800637status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700638 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
639 crop.bottom);
640
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800641 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700642 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700643 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700644 return NO_INIT;
645 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800646 mNextCrop = crop;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800647 return OK;
648}
649
650status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700651 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800652 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700653 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700654 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700655 return NO_INIT;
656 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800657 mNextTransform = transform;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800658 return OK;
659}
660
Mathias Agopian053b02d2011-08-08 19:14:03 -0700661status_t SurfaceTexture::connect(int api,
662 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700663 ST_LOGV("connect: api=%d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700664 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700665
666 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700667 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700668 return NO_INIT;
669 }
670
Jamie Gennis97096872011-07-13 19:12:20 -0700671 int err = NO_ERROR;
672 switch (api) {
673 case NATIVE_WINDOW_API_EGL:
674 case NATIVE_WINDOW_API_CPU:
675 case NATIVE_WINDOW_API_MEDIA:
676 case NATIVE_WINDOW_API_CAMERA:
677 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700678 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian949be322011-07-13 17:39:11 -0700679 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700680 err = -EINVAL;
681 } else {
682 mConnectedApi = api;
Mathias Agopian053b02d2011-08-08 19:14:03 -0700683 *outWidth = mDefaultWidth;
684 *outHeight = mDefaultHeight;
685 *outTransform = 0;
Jamie Gennis97096872011-07-13 19:12:20 -0700686 }
687 break;
688 default:
689 err = -EINVAL;
690 break;
691 }
692 return err;
693}
694
695status_t SurfaceTexture::disconnect(int api) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700696 ST_LOGV("disconnect: api=%d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700697 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700698
699 if (mAbandoned) {
Mathias Agopianb05bb172011-11-18 14:30:20 -0800700 // it is not really an error to disconnect after the surface
701 // has been abandoned, it should just be a no-op.
702 return NO_ERROR;
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700703 }
704
Jamie Gennis97096872011-07-13 19:12:20 -0700705 int err = NO_ERROR;
706 switch (api) {
707 case NATIVE_WINDOW_API_EGL:
708 case NATIVE_WINDOW_API_CPU:
709 case NATIVE_WINDOW_API_MEDIA:
710 case NATIVE_WINDOW_API_CAMERA:
711 if (mConnectedApi == api) {
Mathias Agopian5c715752011-08-10 17:35:09 -0700712 drainQueueAndFreeBuffersLocked();
Jamie Gennis97096872011-07-13 19:12:20 -0700713 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700714 mNextCrop.makeInvalid();
715 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
716 mNextTransform = 0;
Mathias Agopian5c715752011-08-10 17:35:09 -0700717 mDequeueCondition.signal();
Jamie Gennis97096872011-07-13 19:12:20 -0700718 } else {
Jamie Gennis44a05222011-09-16 17:30:26 -0700719 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian949be322011-07-13 17:39:11 -0700720 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700721 err = -EINVAL;
722 }
723 break;
724 default:
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700725 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700726 err = -EINVAL;
727 break;
728 }
729 return err;
730}
731
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700732status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700733 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700734
735 switch (mode) {
736 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
737 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
738 break;
739 default:
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700740 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700741 return BAD_VALUE;
742 }
743
744 Mutex::Autolock lock(mMutex);
745 mNextScalingMode = mode;
746 return OK;
747}
748
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800749status_t SurfaceTexture::updateTexImage() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700750 ST_LOGV("updateTexImage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800751 Mutex::Autolock lock(mMutex);
752
Mathias Agopian95dfd052011-08-08 19:35:15 -0700753 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700754 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian5c715752011-08-10 17:35:09 -0700755 return NO_INIT;
Mathias Agopian95dfd052011-08-08 19:35:15 -0700756 }
757
Jamie Gennis9fb59762011-06-27 15:41:52 -0700758 // In asynchronous mode the list is guaranteed to be one buffer
759 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700760 if (!mQueue.empty()) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700761 Fifo::iterator front(mQueue.begin());
Jamie Gennis9fb59762011-06-27 15:41:52 -0700762 int buf = *front;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700763
Jamie Gennisb598fb92011-01-09 16:33:17 -0800764 // Update the GL texture object.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700765 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis77cec6132011-11-14 14:51:01 -0800766 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800767 if (image == EGL_NO_IMAGE_KHR) {
Mathias Agopian95dfd052011-08-08 19:35:15 -0700768 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700769 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian5c715752011-08-10 17:35:09 -0700770 return BAD_VALUE;
Mathias Agopian95dfd052011-08-08 19:35:15 -0700771 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700772 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
773 mSlots[buf].mEglImage = image;
774 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700775 if (image == EGL_NO_IMAGE_KHR) {
776 // NOTE: if dpy was invalid, createImage() is guaranteed to
777 // fail. so we'd end up here.
778 return -EINVAL;
779 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800780 }
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800781
782 GLint error;
783 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700784 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800785 }
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700786
Jamie Gennis8606fef2011-09-28 12:13:31 -0700787 glBindTexture(mTexTarget, mTexName);
788 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700789
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800790 bool failed = false;
791 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700792 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700793 image, buf, error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800794 failed = true;
795 }
796 if (failed) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800797 return -EINVAL;
798 }
Jamie Gennisf7acf162011-01-12 18:30:40 -0800799
Jamie Gennis77cec6132011-11-14 14:51:01 -0800800 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
801 if (mUseFenceSync) {
802 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
803 NULL);
804 if (fence == EGL_NO_SYNC_KHR) {
805 LOGE("updateTexImage: error creating fence: %#x",
806 eglGetError());
807 return -EINVAL;
808 }
809 glFlush();
810 mSlots[mCurrentTexture].mFence = fence;
811 }
812 }
813
814 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
815 mCurrentTexture,
816 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
817 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700818
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700819 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis9fb59762011-06-27 15:41:52 -0700820 // The current buffer becomes FREE if it was still in the queued
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700821 // state. If it has already been given to the client
822 // (synchronous mode), then it stays in DEQUEUED state.
Jamie Gennis77cec6132011-11-14 14:51:01 -0800823 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700824 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
Jamie Gennis77cec6132011-11-14 14:51:01 -0800825 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700826 }
827
Jamie Gennisf7acf162011-01-12 18:30:40 -0800828 // Update the SurfaceTexture state.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700829 mCurrentTexture = buf;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700830 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennisa2187152011-05-19 13:33:00 -0700831 mCurrentCrop = mSlots[buf].mCrop;
832 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700833 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700834 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Genniseadfb672011-06-12 17:03:06 -0700835 computeCurrentTransformMatrix();
Jamie Gennis9fb59762011-06-27 15:41:52 -0700836
837 // Now that we've passed the point at which failures can happen,
838 // it's safe to remove the buffer from the front of the queue.
839 mQueue.erase(front);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700840 mDequeueCondition.signal();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700841 } else {
842 // We always bind the texture even if we don't update its contents.
Jamie Gennis8606fef2011-09-28 12:13:31 -0700843 glBindTexture(mTexTarget, mTexName);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800844 }
Jamie Gennis9fb59762011-06-27 15:41:52 -0700845
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800846 return OK;
847}
848
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700849bool SurfaceTexture::isExternalFormat(uint32_t format)
850{
851 switch (format) {
852 // supported YUV formats
853 case HAL_PIXEL_FORMAT_YV12:
854 // Legacy/deprecated YUV formats
855 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
856 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
857 case HAL_PIXEL_FORMAT_YCbCr_422_I:
858 return true;
859 }
860
861 // Any OEM format needs to be considered
862 if (format>=0x100 && format<=0x1FF)
863 return true;
864
865 return false;
866}
867
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700868GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis8606fef2011-09-28 12:13:31 -0700869 return mTexTarget;
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700870}
871
Jamie Gennisb598fb92011-01-09 16:33:17 -0800872void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisb598fb92011-01-09 16:33:17 -0800873 Mutex::Autolock lock(mMutex);
Jamie Genniseadfb672011-06-12 17:03:06 -0700874 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
875}
876
877void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700878 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisb598fb92011-01-09 16:33:17 -0800879
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800880 float xform[16];
881 for (int i = 0; i < 16; i++) {
882 xform[i] = mtxIdentity[i];
883 }
884 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
885 float result[16];
886 mtxMul(result, xform, mtxFlipH);
887 for (int i = 0; i < 16; i++) {
888 xform[i] = result[i];
889 }
890 }
891 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
892 float result[16];
893 mtxMul(result, xform, mtxFlipV);
894 for (int i = 0; i < 16; i++) {
895 xform[i] = result[i];
896 }
897 }
898 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
899 float result[16];
900 mtxMul(result, xform, mtxRot90);
901 for (int i = 0; i < 16; i++) {
902 xform[i] = result[i];
903 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800904 }
905
906 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800907 float tx, ty, sx, sy;
908 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800909 // In order to prevent bilinear sampling at the of the crop rectangle we
910 // may need to shrink it by 2 texels in each direction. Normally this
911 // would just need to take 1/2 a texel off each end, but because the
912 // chroma channels will likely be subsampled we need to chop off a whole
913 // texel. This will cause artifacts if someone does nearest sampling
914 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
915 // accomodate the bilinear and nearest sampling uses.
916 //
917 // If nearest sampling turns out to be a desirable usage of these
918 // textures then we could add the ability to switch a SurfaceTexture to
919 // nearest-mode. Preferably, however, the image producers (video
920 // decoder, camera, etc.) would simply not use a crop rectangle (or at
921 // least not tell the framework about it) so that the GPU can do the
922 // correct edge behavior.
923 int xshrink = 0, yshrink = 0;
924 if (mCurrentCrop.left > 0) {
925 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
926 xshrink++;
927 } else {
928 tx = 0.0f;
929 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700930 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800931 xshrink++;
932 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700933 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800934 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
935 float(buf->getHeight());
936 yshrink++;
937 } else {
938 ty = 0.0f;
939 }
940 if (mCurrentCrop.top > 0) {
941 yshrink++;
942 }
943 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
944 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800945 } else {
946 tx = 0.0f;
947 ty = 0.0f;
948 sx = 1.0f;
949 sy = 1.0f;
950 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800951 float crop[16] = {
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800952 sx, 0, 0, 0,
953 0, sy, 0, 0,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800954 0, 0, 1, 0,
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800955 tx, ty, 0, 1,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800956 };
957
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800958 float mtxBeforeFlipV[16];
959 mtxMul(mtxBeforeFlipV, crop, xform);
960
961 // SurfaceFlinger expects the top of its window textures to be at a Y
962 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
963 // want to expose this to applications, however, so we must add an
964 // additional vertical flip to the transform after all the other transforms.
Jamie Genniseadfb672011-06-12 17:03:06 -0700965 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800966}
967
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800968nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700969 ST_LOGV("getTimestamp");
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800970 Mutex::Autolock lock(mMutex);
971 return mCurrentTimestamp;
972}
973
Jamie Gennis376590d2011-01-13 14:43:36 -0800974void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700975 const sp<FrameAvailableListener>& listener) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700976 ST_LOGV("setFrameAvailableListener");
Jamie Gennis376590d2011-01-13 14:43:36 -0800977 Mutex::Autolock lock(mMutex);
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700978 mFrameAvailableListener = listener;
Jamie Gennis376590d2011-01-13 14:43:36 -0800979}
980
Mathias Agopian5c715752011-08-10 17:35:09 -0700981void SurfaceTexture::freeBufferLocked(int i) {
982 mSlots[i].mGraphicBuffer = 0;
983 mSlots[i].mBufferState = BufferSlot::FREE;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600984 mSlots[i].mFrameNumber = 0;
Mathias Agopian5c715752011-08-10 17:35:09 -0700985 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
986 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
987 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
988 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
989 }
990}
991
Mathias Agopiana04cda92011-08-10 15:28:58 -0700992void SurfaceTexture::freeAllBuffersLocked() {
993 LOGW_IF(!mQueue.isEmpty(),
994 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700995 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800996 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian5c715752011-08-10 17:35:09 -0700997 freeBufferLocked(i);
998 }
999}
1000
1001void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
1002 LOGW_IF(!mQueue.isEmpty(),
1003 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
1004 int head = -1;
1005 if (!mQueue.empty()) {
1006 Fifo::iterator front(mQueue.begin());
1007 head = *front;
1008 }
Mathias Agopian8618ebc2011-08-17 15:42:04 -07001009 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5c715752011-08-10 17:35:09 -07001010 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1011 if (i != head) {
1012 freeBufferLocked(i);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001013 }
1014 }
1015}
1016
Mathias Agopian5c715752011-08-10 17:35:09 -07001017status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian71fd1212011-08-10 16:33:23 -07001018 while (mSynchronousMode && !mQueue.isEmpty()) {
1019 mDequeueCondition.wait(mMutex);
Mathias Agopian5c715752011-08-10 17:35:09 -07001020 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001021 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian5c715752011-08-10 17:35:09 -07001022 return NO_INIT;
1023 }
1024 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001025 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian5c715752011-08-10 17:35:09 -07001026 return NO_INIT;
1027 }
Mathias Agopian71fd1212011-08-10 16:33:23 -07001028 }
Mathias Agopian5c715752011-08-10 17:35:09 -07001029 return NO_ERROR;
1030}
1031
1032status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
1033 status_t err = drainQueueLocked();
1034 if (err == NO_ERROR) {
1035 if (mSynchronousMode) {
1036 freeAllBuffersLocked();
1037 } else {
1038 freeAllBuffersExceptHeadLocked();
1039 }
1040 }
1041 return err;
Mathias Agopian71fd1212011-08-10 16:33:23 -07001042}
1043
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001044EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
1045 const sp<GraphicBuffer>& graphicBuffer) {
1046 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
1047 EGLint attrs[] = {
1048 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1049 EGL_NONE,
1050 };
1051 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1052 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian6fad64c2011-04-26 14:57:40 -07001053 if (image == EGL_NO_IMAGE_KHR) {
1054 EGLint error = eglGetError();
Jamie Gennis44a05222011-09-16 17:30:26 -07001055 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001056 }
1057 return image;
1058}
1059
Mathias Agopian27cd07c2011-04-11 21:19:55 -07001060sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
1061 Mutex::Autolock lock(mMutex);
1062 return mCurrentTextureBuf;
1063}
1064
1065Rect SurfaceTexture::getCurrentCrop() const {
1066 Mutex::Autolock lock(mMutex);
1067 return mCurrentCrop;
1068}
1069
1070uint32_t SurfaceTexture::getCurrentTransform() const {
1071 Mutex::Autolock lock(mMutex);
1072 return mCurrentTransform;
1073}
1074
Mathias Agopian09d7ed72011-07-13 15:24:42 -07001075uint32_t SurfaceTexture::getCurrentScalingMode() const {
1076 Mutex::Autolock lock(mMutex);
1077 return mCurrentScalingMode;
1078}
1079
Jamie Gennis87f32652011-11-19 18:04:43 -08001080bool SurfaceTexture::isSynchronousMode() const {
1081 Mutex::Autolock lock(mMutex);
1082 return mSynchronousMode;
1083}
1084
Mathias Agopianed3894c2011-04-20 14:20:59 -07001085int SurfaceTexture::query(int what, int* outValue)
1086{
1087 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001088
1089 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001090 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001091 return NO_INIT;
1092 }
1093
Mathias Agopianed3894c2011-04-20 14:20:59 -07001094 int value;
1095 switch (what) {
1096 case NATIVE_WINDOW_WIDTH:
1097 value = mDefaultWidth;
Mathias Agopianed3894c2011-04-20 14:20:59 -07001098 break;
1099 case NATIVE_WINDOW_HEIGHT:
1100 value = mDefaultHeight;
Mathias Agopianed3894c2011-04-20 14:20:59 -07001101 break;
1102 case NATIVE_WINDOW_FORMAT:
1103 value = mPixelFormat;
1104 break;
1105 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1106 value = mSynchronousMode ?
1107 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1108 break;
1109 default:
1110 return BAD_VALUE;
1111 }
1112 outValue[0] = value;
1113 return NO_ERROR;
1114}
Mathias Agopian27cd07c2011-04-11 21:19:55 -07001115
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001116void SurfaceTexture::abandon() {
1117 Mutex::Autolock lock(mMutex);
Mathias Agopian5c715752011-08-10 17:35:09 -07001118 mQueue.clear();
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001119 mAbandoned = true;
Mathias Agopianec46b4e2011-08-03 15:18:36 -07001120 mCurrentTextureBuf.clear();
Mathias Agopian5c715752011-08-10 17:35:09 -07001121 freeAllBuffersLocked();
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001122 mDequeueCondition.signal();
1123}
1124
Jamie Gennis44a05222011-09-16 17:30:26 -07001125void SurfaceTexture::setName(const String8& name) {
1126 mName = name;
1127}
1128
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001129void SurfaceTexture::dump(String8& result) const
1130{
1131 char buffer[1024];
1132 dump(result, "", buffer, 1024);
1133}
1134
1135void SurfaceTexture::dump(String8& result, const char* prefix,
1136 char* buffer, size_t SIZE) const
1137{
1138 Mutex::Autolock _l(mMutex);
1139 snprintf(buffer, SIZE,
1140 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1141 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennis44a05222011-09-16 17:30:26 -07001142 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1143 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001144 result.append(buffer);
1145
1146 String8 fifo;
1147 int fifoSize = 0;
1148 Fifo::const_iterator i(mQueue.begin());
1149 while (i != mQueue.end()) {
1150 snprintf(buffer, SIZE, "%02d ", *i++);
1151 fifoSize++;
1152 fifo.append(buffer);
1153 }
1154
1155 snprintf(buffer, SIZE,
Jamie Gennisecfa1d32011-07-19 17:58:43 -07001156 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001157 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1158 ,
1159 prefix, mCurrentCrop.left,
1160 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennisecfa1d32011-07-19 17:58:43 -07001161 mCurrentTransform, mCurrentTexture,
Jamie Gennis44a05222011-09-16 17:30:26 -07001162 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1163 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001164 );
1165 result.append(buffer);
1166
1167 struct {
1168 const char * operator()(int state) const {
1169 switch (state) {
1170 case BufferSlot::DEQUEUED: return "DEQUEUED";
1171 case BufferSlot::QUEUED: return "QUEUED";
1172 case BufferSlot::FREE: return "FREE";
1173 default: return "Unknown";
1174 }
1175 }
1176 } stateName;
1177
1178 for (int i=0 ; i<mBufferCount ; i++) {
1179 const BufferSlot& slot(mSlots[i]);
1180 snprintf(buffer, SIZE,
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001181 "%s%s[%02d] "
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001182 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian49541932011-08-09 15:48:43 -07001183 "transform=0x%02x, timestamp=%lld",
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001184 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001185 stateName(slot.mBufferState),
Jamie Gennis44a05222011-09-16 17:30:26 -07001186 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1187 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001188 );
1189 result.append(buffer);
Mathias Agopian49541932011-08-09 15:48:43 -07001190
1191 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1192 if (buf != NULL) {
1193 snprintf(buffer, SIZE,
1194 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennis44a05222011-09-16 17:30:26 -07001195 buf->handle, buf->width, buf->height, buf->stride,
1196 buf->format);
Mathias Agopian49541932011-08-09 15:48:43 -07001197 result.append(buffer);
1198 }
1199 result.append("\n");
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001200 }
1201}
1202
Jamie Gennisb598fb92011-01-09 16:33:17 -08001203static void mtxMul(float out[16], const float a[16], const float b[16]) {
1204 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1205 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1206 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1207 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1208
1209 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1210 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1211 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1212 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1213
1214 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1215 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1216 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1217 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1218
1219 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1220 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1221 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1222 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1223}
1224
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001225}; // namespace android