blob: 104cefb329dd2b674d0388e41a8bb023a77c62c3 [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
Mathias Agopian3f157002011-11-17 17:48:35 -080032#include <private/gui/ComposerService.h>
33
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080034#include <surfaceflinger/ISurfaceComposer.h>
35#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennisf7acf162011-01-12 18:30:40 -080036#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080037
38#include <utils/Log.h>
Mathias Agopian7f3289c2011-05-09 19:08:33 -070039#include <utils/String8.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080040
Jamie Gennis77cec6132011-11-14 14:51:01 -080041// This compile option causes SurfaceTexture to return the buffer that is currently
42// attached to the GL texture from dequeueBuffer when no other buffers are
43// available. It requires the drivers (Gralloc, GL, OMX IL, and Camera) to do
44// implicit cross-process synchronization to prevent the buffer from being
45// written to before the buffer has (a) been detached from the GL texture and
46// (b) all GL reads from the buffer have completed.
Mathias Agopiane2fa30c2011-11-14 19:17:37 -080047#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
48#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER true
49#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
50#else
51#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER false
52#endif
Mathias Agopian8618ebc2011-08-17 15:42:04 -070053
Jamie Gennis77cec6132011-11-14 14:51:01 -080054// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
55// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
56// waiting for the GL reads for the buffer being dequeued to complete before
57// allowing the buffer to be dequeued.
58#ifdef USE_FENCE_SYNC
59#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
60#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
61#endif
62#endif
63
Jamie Gennis44a05222011-09-16 17:30:26 -070064// Macros for including the SurfaceTexture name in log messages
Steve Block71f2cf12011-10-20 11:56:00 +010065#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block5baa3a62011-12-20 16:23:08 +000066#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block6215d3f2012-01-04 20:05:49 +000067#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Jamie Gennis44a05222011-09-16 17:30:26 -070068#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
69#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian8618ebc2011-08-17 15:42:04 -070070
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080071namespace android {
72
Jamie Gennisb598fb92011-01-09 16:33:17 -080073// Transform matrices
74static float mtxIdentity[16] = {
75 1, 0, 0, 0,
76 0, 1, 0, 0,
77 0, 0, 1, 0,
78 0, 0, 0, 1,
79};
80static float mtxFlipH[16] = {
81 -1, 0, 0, 0,
82 0, 1, 0, 0,
83 0, 0, 1, 0,
84 1, 0, 0, 1,
85};
86static float mtxFlipV[16] = {
87 1, 0, 0, 0,
88 0, -1, 0, 0,
89 0, 0, 1, 0,
90 0, 1, 0, 1,
91};
92static float mtxRot90[16] = {
93 0, 1, 0, 0,
94 -1, 0, 0, 0,
95 0, 0, 1, 0,
96 1, 0, 0, 1,
97};
98static float mtxRot180[16] = {
99 -1, 0, 0, 0,
100 0, -1, 0, 0,
101 0, 0, 1, 0,
102 1, 1, 0, 1,
103};
104static float mtxRot270[16] = {
105 0, -1, 0, 0,
106 1, 0, 0, 0,
107 0, 0, 1, 0,
108 0, 1, 0, 1,
109};
110
111static void mtxMul(float out[16], const float a[16], const float b[16]);
112
Jamie Gennis44a05222011-09-16 17:30:26 -0700113// Get an ID that's unique within this process.
114static int32_t createProcessUniqueId() {
115 static volatile int32_t globalCounter = 0;
116 return android_atomic_inc(&globalCounter);
117}
118
Jamie Gennis8606fef2011-09-28 12:13:31 -0700119SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Jamie Gennis77cec6132011-11-14 14:51:01 -0800120 GLenum texTarget, bool useFenceSync) :
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700121 mDefaultWidth(1),
122 mDefaultHeight(1),
123 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian402ff242011-05-02 19:51:12 -0700124 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
125 mClientBufferCount(0),
126 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800127 mCurrentTexture(INVALID_BUFFER_SLOT),
128 mCurrentTransform(0),
129 mCurrentTimestamp(0),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800130 mNextTransform(0),
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700131 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700132 mTexName(tex),
Grace Kloba0904d0a2011-06-23 21:21:47 -0700133 mSynchronousMode(false),
Jamie Gennis97096872011-07-13 19:12:20 -0700134 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700135 mConnectedApi(NO_CONNECTED_API),
Jamie Gennis8606fef2011-09-28 12:13:31 -0700136 mAbandoned(false),
Jamie Gennis77cec6132011-11-14 14:51:01 -0800137#ifdef USE_FENCE_SYNC
138 mUseFenceSync(useFenceSync),
139#else
140 mUseFenceSync(false),
141#endif
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600142 mTexTarget(texTarget),
143 mFrameCounter(0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700144 // Choose a name using the PID and a process-unique ID.
145 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
146
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700147 ST_LOGV("SurfaceTexture");
Jamie Gennisf7acf162011-01-12 18:30:40 -0800148 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
149 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopian926340c2011-04-27 18:57:33 -0700150 mNextCrop.makeInvalid();
Jamie Gennis44a05222011-09-16 17:30:26 -0700151 memcpy(mCurrentTransformMatrix, mtxIdentity,
152 sizeof(mCurrentTransformMatrix));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800153}
154
155SurfaceTexture::~SurfaceTexture() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700156 ST_LOGV("~SurfaceTexture");
Mathias Agopiana04cda92011-08-10 15:28:58 -0700157 freeAllBuffersLocked();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800158}
159
Mathias Agopian402ff242011-05-02 19:51:12 -0700160status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
161 if (bufferCount > NUM_BUFFER_SLOTS)
162 return BAD_VALUE;
163
164 // special-case, nothing to do
165 if (bufferCount == mBufferCount)
166 return OK;
167
168 if (!mClientBufferCount &&
169 bufferCount >= mBufferCount) {
170 // easy, we just have more buffers
171 mBufferCount = bufferCount;
172 mServerBufferCount = bufferCount;
173 mDequeueCondition.signal();
174 } else {
175 // we're here because we're either
176 // - reducing the number of available buffers
177 // - or there is a client-buffer-count in effect
178
179 // less than 2 buffers is never allowed
180 if (bufferCount < 2)
181 return BAD_VALUE;
182
183 // when there is non client-buffer-count in effect, the client is not
184 // allowed to dequeue more than one buffer at a time,
185 // so the next time they dequeue a buffer, we know that they don't
186 // own one. the actual resizing will happen during the next
187 // dequeueBuffer.
188
189 mServerBufferCount = bufferCount;
190 }
191 return OK;
192}
193
194status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
195 Mutex::Autolock lock(mMutex);
196 return setBufferCountServerLocked(bufferCount);
197}
198
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800199status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700200 ST_LOGV("setBufferCount: count=%d", bufferCount);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700201 Mutex::Autolock lock(mMutex);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800202
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700203 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700204 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700205 return NO_INIT;
206 }
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700207 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700208 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700209 return BAD_VALUE;
210 }
211
Mathias Agopian402ff242011-05-02 19:51:12 -0700212 // Error out if the user has dequeued buffers
213 for (int i=0 ; i<mBufferCount ; i++) {
214 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700215 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian402ff242011-05-02 19:51:12 -0700216 return -EINVAL;
217 }
218 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700219
Jamie Gennisd995b082011-07-30 16:00:11 -0700220 const int minBufferSlots = mSynchronousMode ?
221 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian402ff242011-05-02 19:51:12 -0700222 if (bufferCount == 0) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700223 mClientBufferCount = 0;
224 bufferCount = (mServerBufferCount >= minBufferSlots) ?
225 mServerBufferCount : minBufferSlots;
226 return setBufferCountServerLocked(bufferCount);
227 }
228
Jamie Gennisd995b082011-07-30 16:00:11 -0700229 if (bufferCount < minBufferSlots) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700230 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennisd995b082011-07-30 16:00:11 -0700231 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800232 return BAD_VALUE;
233 }
234
Mathias Agopian402ff242011-05-02 19:51:12 -0700235 // here we're guaranteed that the client doesn't have dequeued buffers
236 // and will release all of its buffer references.
Mathias Agopiana04cda92011-08-10 15:28:58 -0700237 freeAllBuffersLocked();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800238 mBufferCount = bufferCount;
Mathias Agopian402ff242011-05-02 19:51:12 -0700239 mClientBufferCount = bufferCount;
Jamie Gennisd369dc42011-01-09 13:25:39 -0800240 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700241 mQueue.clear();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700242 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800243 return OK;
244}
245
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700246status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
247{
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700248 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopianf3503c22011-07-25 19:56:08 -0700249 if (!w || !h) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700250 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
251 w, h);
Mathias Agopianf3503c22011-07-25 19:56:08 -0700252 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700253 }
Mathias Agopianf3503c22011-07-25 19:56:08 -0700254
255 Mutex::Autolock lock(mMutex);
256 mDefaultWidth = w;
257 mDefaultHeight = h;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700258 return OK;
259}
260
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700261status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700262 ST_LOGV("requestBuffer: slot=%d", slot);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800263 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700264 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700265 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700266 return NO_INIT;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800267 }
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700268 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700269 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700270 mBufferCount, slot);
271 return BAD_VALUE;
272 }
273 mSlots[slot].mRequestBufferCalled = true;
274 *buf = mSlots[slot].mGraphicBuffer;
275 return NO_ERROR;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700276}
277
278status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
279 uint32_t format, uint32_t usage) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700280 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700281
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700282 if ((w && !h) || (!w && h)) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700283 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700284 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700285 }
286
Mathias Agopian402ff242011-05-02 19:51:12 -0700287 status_t returnFlags(OK);
Jamie Gennis77cec6132011-11-14 14:51:01 -0800288 EGLDisplay dpy = EGL_NO_DISPLAY;
289 EGLSyncKHR fence = EGL_NO_SYNC_KHR;
Mathias Agopian402ff242011-05-02 19:51:12 -0700290
Jamie Gennis77cec6132011-11-14 14:51:01 -0800291 { // Scope for the lock
292 Mutex::Autolock lock(mMutex);
Mathias Agopian71fd1212011-08-10 16:33:23 -0700293
Jamie Gennis77cec6132011-11-14 14:51:01 -0800294 int found = -1;
295 int foundSync = -1;
296 int dequeuedCount = 0;
297 bool tryAgain = true;
298 while (tryAgain) {
299 if (mAbandoned) {
300 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
301 return NO_INIT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700302 }
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700303
Jamie Gennis77cec6132011-11-14 14:51:01 -0800304 // We need to wait for the FIFO to drain if the number of buffer
305 // needs to change.
306 //
307 // The condition "number of buffers needs to change" is true if
308 // - the client doesn't care about how many buffers there are
309 // - AND the actual number of buffer is different from what was
310 // set in the last setBufferCountServer()
311 // - OR -
312 // setBufferCountServer() was set to a value incompatible with
313 // the synchronization mode (for instance because the sync mode
314 // changed since)
315 //
316 // As long as this condition is true AND the FIFO is not empty, we
317 // wait on mDequeueCondition.
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700318
Jamie Gennis77cec6132011-11-14 14:51:01 -0800319 const int minBufferCountNeeded = mSynchronousMode ?
320 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
321
322 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
323 ((mServerBufferCount != mBufferCount) ||
324 (mServerBufferCount < minBufferCountNeeded));
325
326 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
327 // wait for the FIFO to drain
328 mDequeueCondition.wait(mMutex);
329 // NOTE: we continue here because we need to reevaluate our
330 // whole state (eg: we could be abandoned or disconnected)
331 continue;
332 }
333
334 if (numberOfBuffersNeedsToChange) {
335 // here we're guaranteed that mQueue is empty
336 freeAllBuffersLocked();
337 mBufferCount = mServerBufferCount;
338 if (mBufferCount < minBufferCountNeeded)
339 mBufferCount = minBufferCountNeeded;
340 mCurrentTexture = INVALID_BUFFER_SLOT;
341 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
342 }
343
344 // look for a free buffer to give to the client
345 found = INVALID_BUFFER_SLOT;
346 foundSync = INVALID_BUFFER_SLOT;
347 dequeuedCount = 0;
348 for (int i = 0; i < mBufferCount; i++) {
349 const int state = mSlots[i].mBufferState;
350 if (state == BufferSlot::DEQUEUED) {
351 dequeuedCount++;
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700352 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800353
354 // if buffer is FREE it CANNOT be current
355 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
356 "dequeueBuffer: buffer %d is both FREE and current!",
357 i);
358
359 if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) {
360 if (state == BufferSlot::FREE || i == mCurrentTexture) {
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600361 foundSync = i;
Jamie Gennis77cec6132011-11-14 14:51:01 -0800362 if (i != mCurrentTexture) {
363 found = i;
364 break;
365 }
366 }
367 } else {
368 if (state == BufferSlot::FREE) {
369 /* We return the oldest of the free buffers to avoid
370 * stalling the producer if possible. This is because
371 * the consumer may still have pending reads of the
372 * buffers in flight.
373 */
374 bool isOlder = mSlots[i].mFrameNumber <
375 mSlots[found].mFrameNumber;
376 if (found < 0 || isOlder) {
377 foundSync = i;
378 found = i;
379 }
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600380 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700381 }
382 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700383
Jamie Gennis77cec6132011-11-14 14:51:01 -0800384 // clients are not allowed to dequeue more than one buffer
385 // if they didn't set a buffer count.
386 if (!mClientBufferCount && dequeuedCount) {
387 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
388 "setting the buffer count");
389 return -EINVAL;
390 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700391
Jamie Gennis77cec6132011-11-14 14:51:01 -0800392 // See whether a buffer has been queued since the last
393 // setBufferCount so we know whether to perform the
394 // MIN_UNDEQUEUED_BUFFERS check below.
395 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
396 if (bufferHasBeenQueued) {
397 // make sure the client is not trying to dequeue more buffers
398 // than allowed.
399 const int avail = mBufferCount - (dequeuedCount+1);
400 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
401 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
402 "(dequeued=%d)",
403 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
404 dequeuedCount);
405 return -EBUSY;
406 }
407 }
408
409 // we're in synchronous mode and didn't find a buffer, we need to
410 // wait for some buffers to be consumed
411 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
412 if (tryAgain) {
413 mDequeueCondition.wait(mMutex);
Jamie Gennis44e9e0d2011-05-23 18:44:04 -0700414 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700415 }
416
Jamie Gennis77cec6132011-11-14 14:51:01 -0800417 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
418 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
419 found = foundSync;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700420 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700421
Jamie Gennis77cec6132011-11-14 14:51:01 -0800422 if (found == INVALID_BUFFER_SLOT) {
423 // This should not happen.
424 ST_LOGE("dequeueBuffer: no available buffer slots");
425 return -EBUSY;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700426 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800427
428 const int buf = found;
429 *outBuf = found;
430
431 const bool useDefaultSize = !w && !h;
432 if (useDefaultSize) {
433 // use the default size
434 w = mDefaultWidth;
435 h = mDefaultHeight;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700436 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800437
438 const bool updateFormat = (format != 0);
439 if (!updateFormat) {
440 // keep the current (or default) format
441 format = mPixelFormat;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800442 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800443
444 // buffer is now in DEQUEUED (but can also be current at the same time,
445 // if we're in synchronous mode)
446 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
447
448 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
449 if ((buffer == NULL) ||
450 (uint32_t(buffer->width) != w) ||
451 (uint32_t(buffer->height) != h) ||
452 (uint32_t(buffer->format) != format) ||
453 ((uint32_t(buffer->usage) & usage) != usage))
454 {
455 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
456 status_t error;
457 sp<GraphicBuffer> graphicBuffer(
458 mGraphicBufferAlloc->createGraphicBuffer(
459 w, h, format, usage, &error));
460 if (graphicBuffer == 0) {
461 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
462 "failed");
463 return error;
464 }
465 if (updateFormat) {
466 mPixelFormat = format;
467 }
468 mSlots[buf].mGraphicBuffer = graphicBuffer;
469 mSlots[buf].mRequestBufferCalled = false;
470 mSlots[buf].mFence = EGL_NO_SYNC_KHR;
471 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
472 eglDestroyImageKHR(mSlots[buf].mEglDisplay,
473 mSlots[buf].mEglImage);
474 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
475 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
476 }
477 if (mCurrentTexture == buf) {
478 // The current texture no longer references the buffer in this slot
479 // since we just allocated a new buffer.
480 mCurrentTexture = INVALID_BUFFER_SLOT;
481 }
482 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Jamie Gennise36d05482011-11-17 16:00:44 -0800483 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800484
485 dpy = mSlots[buf].mEglDisplay;
486 fence = mSlots[buf].mFence;
487 mSlots[buf].mFence = EGL_NO_SYNC_KHR;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700488 }
Jamie Gennis77cec6132011-11-14 14:51:01 -0800489
490 if (fence != EGL_NO_SYNC_KHR) {
491 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
492 // If something goes wrong, log the error, but return the buffer without
493 // synchronizing access to it. It's too late at this point to abort the
494 // dequeue operation.
495 if (result == EGL_FALSE) {
496 LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
497 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
498 LOGE("dequeueBuffer: timeout waiting for fence");
499 }
500 eglDestroySyncKHR(dpy, fence);
501 }
502
Jamie Gennis798b9cd2011-12-09 15:07:44 -0800503 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf,
504 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
Jamie Gennis77cec6132011-11-14 14:51:01 -0800505
Mathias Agopian402ff242011-05-02 19:51:12 -0700506 return returnFlags;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800507}
508
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700509status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700510 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700511 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700512
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700513 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700514 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700515 return NO_INIT;
516 }
517
Mathias Agopian402ff242011-05-02 19:51:12 -0700518 status_t err = OK;
Grace Kloba0904d0a2011-06-23 21:21:47 -0700519 if (!mAllowSynchronousMode && enabled)
520 return err;
521
Mathias Agopian402ff242011-05-02 19:51:12 -0700522 if (!enabled) {
523 // going to asynchronous mode, drain the queue
Mathias Agopian5c715752011-08-10 17:35:09 -0700524 err = drainQueueLocked();
525 if (err != NO_ERROR)
526 return err;
Mathias Agopian402ff242011-05-02 19:51:12 -0700527 }
528
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700529 if (mSynchronousMode != enabled) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700530 // - if we're going to asynchronous mode, the queue is guaranteed to be
531 // empty here
532 // - if the client set the number of buffers, we're guaranteed that
533 // we have at least 3 (because we don't allow less)
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700534 mSynchronousMode = enabled;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700535 mDequeueCondition.signal();
536 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700537 return err;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700538}
539
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700540status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
541 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700542 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiane845c352011-05-11 15:05:29 -0700543
544 sp<FrameAvailableListener> listener;
545
546 { // scope for the lock
Jamie Gennisa2187152011-05-19 13:33:00 -0700547 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700548 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700549 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700550 return NO_INIT;
551 }
Jamie Gennisa2187152011-05-19 13:33:00 -0700552 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700553 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennisa2187152011-05-19 13:33:00 -0700554 mBufferCount, buf);
555 return -EINVAL;
556 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700557 ST_LOGE("queueBuffer: slot %d is not owned by the client "
558 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennisa2187152011-05-19 13:33:00 -0700559 return -EINVAL;
560 } else if (buf == mCurrentTexture) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700561 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennisa2187152011-05-19 13:33:00 -0700562 return -EINVAL;
563 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700564 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennisa2187152011-05-19 13:33:00 -0700565 "buffer", buf);
566 return -EINVAL;
567 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700568
Jamie Gennisa2187152011-05-19 13:33:00 -0700569 if (mSynchronousMode) {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700570 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700571 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700572
573 // Synchronous mode always signals that an additional frame should
574 // be consumed.
575 listener = mFrameAvailableListener;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700576 } else {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700577 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennisa2187152011-05-19 13:33:00 -0700578 if (mQueue.empty()) {
579 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700580
581 // Asynchronous mode only signals that a frame should be
582 // consumed if no previous frame was pending. If a frame were
583 // pending then the consumer would have already been notified.
584 listener = mFrameAvailableListener;
Jamie Gennisa2187152011-05-19 13:33:00 -0700585 } else {
586 Fifo::iterator front(mQueue.begin());
587 // buffer currently queued is freed
588 mSlots[*front].mBufferState = BufferSlot::FREE;
589 // and we record the new buffer index in the queued list
590 *front = buf;
591 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700592 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700593
Jamie Gennisa2187152011-05-19 13:33:00 -0700594 mSlots[buf].mBufferState = BufferSlot::QUEUED;
595 mSlots[buf].mCrop = mNextCrop;
596 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700597 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700598 mSlots[buf].mTimestamp = timestamp;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600599 mFrameCounter++;
600 mSlots[buf].mFrameNumber = mFrameCounter;
601
Jamie Gennisa2187152011-05-19 13:33:00 -0700602 mDequeueCondition.signal();
Mathias Agopian1a227432011-08-17 12:45:40 -0700603
604 *outWidth = mDefaultWidth;
605 *outHeight = mDefaultHeight;
606 *outTransform = 0;
Mathias Agopiane845c352011-05-11 15:05:29 -0700607 } // scope for the lock
608
609 // call back without lock held
610 if (listener != 0) {
611 listener->onFrameAvailable();
612 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800613 return OK;
614}
615
616void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700617 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800618 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700619
620 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700621 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700622 return;
623 }
624
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700625 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700626 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700627 mBufferCount, buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800628 return;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700629 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700630 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700631 buf, mSlots[buf].mBufferState);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800632 return;
633 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700634 mSlots[buf].mBufferState = BufferSlot::FREE;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600635 mSlots[buf].mFrameNumber = 0;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700636 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800637}
638
Jamie Gennisb598fb92011-01-09 16:33:17 -0800639status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700640 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
641 crop.bottom);
642
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800643 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700644 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700645 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700646 return NO_INIT;
647 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800648 mNextCrop = crop;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800649 return OK;
650}
651
652status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700653 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800654 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700655 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700656 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700657 return NO_INIT;
658 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800659 mNextTransform = transform;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800660 return OK;
661}
662
Mathias Agopian053b02d2011-08-08 19:14:03 -0700663status_t SurfaceTexture::connect(int api,
664 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700665 ST_LOGV("connect: api=%d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700666 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700667
668 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700669 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700670 return NO_INIT;
671 }
672
Jamie Gennis97096872011-07-13 19:12:20 -0700673 int err = NO_ERROR;
674 switch (api) {
675 case NATIVE_WINDOW_API_EGL:
676 case NATIVE_WINDOW_API_CPU:
677 case NATIVE_WINDOW_API_MEDIA:
678 case NATIVE_WINDOW_API_CAMERA:
679 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700680 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian949be322011-07-13 17:39:11 -0700681 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700682 err = -EINVAL;
683 } else {
684 mConnectedApi = api;
Mathias Agopian053b02d2011-08-08 19:14:03 -0700685 *outWidth = mDefaultWidth;
686 *outHeight = mDefaultHeight;
687 *outTransform = 0;
Jamie Gennis97096872011-07-13 19:12:20 -0700688 }
689 break;
690 default:
691 err = -EINVAL;
692 break;
693 }
694 return err;
695}
696
697status_t SurfaceTexture::disconnect(int api) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700698 ST_LOGV("disconnect: api=%d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700699 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700700
701 if (mAbandoned) {
Mathias Agopianb05bb172011-11-18 14:30:20 -0800702 // it is not really an error to disconnect after the surface
703 // has been abandoned, it should just be a no-op.
704 return NO_ERROR;
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700705 }
706
Jamie Gennis97096872011-07-13 19:12:20 -0700707 int err = NO_ERROR;
708 switch (api) {
709 case NATIVE_WINDOW_API_EGL:
710 case NATIVE_WINDOW_API_CPU:
711 case NATIVE_WINDOW_API_MEDIA:
712 case NATIVE_WINDOW_API_CAMERA:
713 if (mConnectedApi == api) {
Mathias Agopian5c715752011-08-10 17:35:09 -0700714 drainQueueAndFreeBuffersLocked();
Jamie Gennis97096872011-07-13 19:12:20 -0700715 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700716 mNextCrop.makeInvalid();
717 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
718 mNextTransform = 0;
Mathias Agopian5c715752011-08-10 17:35:09 -0700719 mDequeueCondition.signal();
Jamie Gennis97096872011-07-13 19:12:20 -0700720 } else {
Jamie Gennis44a05222011-09-16 17:30:26 -0700721 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian949be322011-07-13 17:39:11 -0700722 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700723 err = -EINVAL;
724 }
725 break;
726 default:
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700727 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700728 err = -EINVAL;
729 break;
730 }
731 return err;
732}
733
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700734status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700735 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700736
737 switch (mode) {
738 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
739 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
740 break;
741 default:
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700742 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700743 return BAD_VALUE;
744 }
745
746 Mutex::Autolock lock(mMutex);
747 mNextScalingMode = mode;
748 return OK;
749}
750
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800751status_t SurfaceTexture::updateTexImage() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700752 ST_LOGV("updateTexImage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800753 Mutex::Autolock lock(mMutex);
754
Mathias Agopian95dfd052011-08-08 19:35:15 -0700755 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700756 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian5c715752011-08-10 17:35:09 -0700757 return NO_INIT;
Mathias Agopian95dfd052011-08-08 19:35:15 -0700758 }
759
Jamie Gennis9fb59762011-06-27 15:41:52 -0700760 // In asynchronous mode the list is guaranteed to be one buffer
761 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700762 if (!mQueue.empty()) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700763 Fifo::iterator front(mQueue.begin());
Jamie Gennis9fb59762011-06-27 15:41:52 -0700764 int buf = *front;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700765
Jamie Gennisb598fb92011-01-09 16:33:17 -0800766 // Update the GL texture object.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700767 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis77cec6132011-11-14 14:51:01 -0800768 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800769 if (image == EGL_NO_IMAGE_KHR) {
Mathias Agopian95dfd052011-08-08 19:35:15 -0700770 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700771 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian5c715752011-08-10 17:35:09 -0700772 return BAD_VALUE;
Mathias Agopian95dfd052011-08-08 19:35:15 -0700773 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700774 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
775 mSlots[buf].mEglImage = image;
776 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700777 if (image == EGL_NO_IMAGE_KHR) {
778 // NOTE: if dpy was invalid, createImage() is guaranteed to
779 // fail. so we'd end up here.
780 return -EINVAL;
781 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800782 }
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800783
784 GLint error;
785 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700786 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800787 }
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700788
Jamie Gennis8606fef2011-09-28 12:13:31 -0700789 glBindTexture(mTexTarget, mTexName);
790 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700791
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800792 bool failed = false;
793 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700794 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700795 image, buf, error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800796 failed = true;
797 }
798 if (failed) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800799 return -EINVAL;
800 }
Jamie Gennisf7acf162011-01-12 18:30:40 -0800801
Jamie Gennis77cec6132011-11-14 14:51:01 -0800802 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
803 if (mUseFenceSync) {
804 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
805 NULL);
806 if (fence == EGL_NO_SYNC_KHR) {
807 LOGE("updateTexImage: error creating fence: %#x",
808 eglGetError());
809 return -EINVAL;
810 }
811 glFlush();
812 mSlots[mCurrentTexture].mFence = fence;
813 }
814 }
815
816 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
817 mCurrentTexture,
818 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
819 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700820
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700821 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis9fb59762011-06-27 15:41:52 -0700822 // The current buffer becomes FREE if it was still in the queued
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700823 // state. If it has already been given to the client
824 // (synchronous mode), then it stays in DEQUEUED state.
Jamie Gennis77cec6132011-11-14 14:51:01 -0800825 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700826 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
Jamie Gennis77cec6132011-11-14 14:51:01 -0800827 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700828 }
829
Jamie Gennisf7acf162011-01-12 18:30:40 -0800830 // Update the SurfaceTexture state.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700831 mCurrentTexture = buf;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700832 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennisa2187152011-05-19 13:33:00 -0700833 mCurrentCrop = mSlots[buf].mCrop;
834 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700835 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700836 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Genniseadfb672011-06-12 17:03:06 -0700837 computeCurrentTransformMatrix();
Jamie Gennis9fb59762011-06-27 15:41:52 -0700838
839 // Now that we've passed the point at which failures can happen,
840 // it's safe to remove the buffer from the front of the queue.
841 mQueue.erase(front);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700842 mDequeueCondition.signal();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700843 } else {
844 // We always bind the texture even if we don't update its contents.
Jamie Gennis8606fef2011-09-28 12:13:31 -0700845 glBindTexture(mTexTarget, mTexName);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800846 }
Jamie Gennis9fb59762011-06-27 15:41:52 -0700847
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800848 return OK;
849}
850
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700851bool SurfaceTexture::isExternalFormat(uint32_t format)
852{
853 switch (format) {
854 // supported YUV formats
855 case HAL_PIXEL_FORMAT_YV12:
856 // Legacy/deprecated YUV formats
857 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
858 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
859 case HAL_PIXEL_FORMAT_YCbCr_422_I:
860 return true;
861 }
862
863 // Any OEM format needs to be considered
864 if (format>=0x100 && format<=0x1FF)
865 return true;
866
867 return false;
868}
869
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700870GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis8606fef2011-09-28 12:13:31 -0700871 return mTexTarget;
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700872}
873
Jamie Gennisb598fb92011-01-09 16:33:17 -0800874void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisb598fb92011-01-09 16:33:17 -0800875 Mutex::Autolock lock(mMutex);
Jamie Genniseadfb672011-06-12 17:03:06 -0700876 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
877}
878
879void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700880 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisb598fb92011-01-09 16:33:17 -0800881
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800882 float xform[16];
883 for (int i = 0; i < 16; i++) {
884 xform[i] = mtxIdentity[i];
885 }
886 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
887 float result[16];
888 mtxMul(result, xform, mtxFlipH);
889 for (int i = 0; i < 16; i++) {
890 xform[i] = result[i];
891 }
892 }
893 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
894 float result[16];
895 mtxMul(result, xform, mtxFlipV);
896 for (int i = 0; i < 16; i++) {
897 xform[i] = result[i];
898 }
899 }
900 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
901 float result[16];
902 mtxMul(result, xform, mtxRot90);
903 for (int i = 0; i < 16; i++) {
904 xform[i] = result[i];
905 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800906 }
907
908 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800909 float tx, ty, sx, sy;
910 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800911 // In order to prevent bilinear sampling at the of the crop rectangle we
912 // may need to shrink it by 2 texels in each direction. Normally this
913 // would just need to take 1/2 a texel off each end, but because the
914 // chroma channels will likely be subsampled we need to chop off a whole
915 // texel. This will cause artifacts if someone does nearest sampling
916 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
917 // accomodate the bilinear and nearest sampling uses.
918 //
919 // If nearest sampling turns out to be a desirable usage of these
920 // textures then we could add the ability to switch a SurfaceTexture to
921 // nearest-mode. Preferably, however, the image producers (video
922 // decoder, camera, etc.) would simply not use a crop rectangle (or at
923 // least not tell the framework about it) so that the GPU can do the
924 // correct edge behavior.
925 int xshrink = 0, yshrink = 0;
926 if (mCurrentCrop.left > 0) {
927 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
928 xshrink++;
929 } else {
930 tx = 0.0f;
931 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700932 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800933 xshrink++;
934 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700935 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800936 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
937 float(buf->getHeight());
938 yshrink++;
939 } else {
940 ty = 0.0f;
941 }
942 if (mCurrentCrop.top > 0) {
943 yshrink++;
944 }
945 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
946 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800947 } else {
948 tx = 0.0f;
949 ty = 0.0f;
950 sx = 1.0f;
951 sy = 1.0f;
952 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800953 float crop[16] = {
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800954 sx, 0, 0, 0,
955 0, sy, 0, 0,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800956 0, 0, 1, 0,
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800957 tx, ty, 0, 1,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800958 };
959
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800960 float mtxBeforeFlipV[16];
961 mtxMul(mtxBeforeFlipV, crop, xform);
962
963 // SurfaceFlinger expects the top of its window textures to be at a Y
964 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
965 // want to expose this to applications, however, so we must add an
966 // additional vertical flip to the transform after all the other transforms.
Jamie Genniseadfb672011-06-12 17:03:06 -0700967 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800968}
969
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800970nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700971 ST_LOGV("getTimestamp");
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800972 Mutex::Autolock lock(mMutex);
973 return mCurrentTimestamp;
974}
975
Jamie Gennis376590d2011-01-13 14:43:36 -0800976void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700977 const sp<FrameAvailableListener>& listener) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700978 ST_LOGV("setFrameAvailableListener");
Jamie Gennis376590d2011-01-13 14:43:36 -0800979 Mutex::Autolock lock(mMutex);
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700980 mFrameAvailableListener = listener;
Jamie Gennis376590d2011-01-13 14:43:36 -0800981}
982
Mathias Agopian5c715752011-08-10 17:35:09 -0700983void SurfaceTexture::freeBufferLocked(int i) {
984 mSlots[i].mGraphicBuffer = 0;
985 mSlots[i].mBufferState = BufferSlot::FREE;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600986 mSlots[i].mFrameNumber = 0;
Mathias Agopian5c715752011-08-10 17:35:09 -0700987 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
988 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
989 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
990 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
991 }
992}
993
Mathias Agopiana04cda92011-08-10 15:28:58 -0700994void SurfaceTexture::freeAllBuffersLocked() {
995 LOGW_IF(!mQueue.isEmpty(),
996 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700997 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800998 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian5c715752011-08-10 17:35:09 -0700999 freeBufferLocked(i);
1000 }
1001}
1002
1003void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
1004 LOGW_IF(!mQueue.isEmpty(),
1005 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
1006 int head = -1;
1007 if (!mQueue.empty()) {
1008 Fifo::iterator front(mQueue.begin());
1009 head = *front;
1010 }
Mathias Agopian8618ebc2011-08-17 15:42:04 -07001011 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5c715752011-08-10 17:35:09 -07001012 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1013 if (i != head) {
1014 freeBufferLocked(i);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001015 }
1016 }
1017}
1018
Mathias Agopian5c715752011-08-10 17:35:09 -07001019status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian71fd1212011-08-10 16:33:23 -07001020 while (mSynchronousMode && !mQueue.isEmpty()) {
1021 mDequeueCondition.wait(mMutex);
Mathias Agopian5c715752011-08-10 17:35:09 -07001022 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001023 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian5c715752011-08-10 17:35:09 -07001024 return NO_INIT;
1025 }
1026 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001027 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian5c715752011-08-10 17:35:09 -07001028 return NO_INIT;
1029 }
Mathias Agopian71fd1212011-08-10 16:33:23 -07001030 }
Mathias Agopian5c715752011-08-10 17:35:09 -07001031 return NO_ERROR;
1032}
1033
1034status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
1035 status_t err = drainQueueLocked();
1036 if (err == NO_ERROR) {
1037 if (mSynchronousMode) {
1038 freeAllBuffersLocked();
1039 } else {
1040 freeAllBuffersExceptHeadLocked();
1041 }
1042 }
1043 return err;
Mathias Agopian71fd1212011-08-10 16:33:23 -07001044}
1045
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001046EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
1047 const sp<GraphicBuffer>& graphicBuffer) {
1048 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
1049 EGLint attrs[] = {
1050 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1051 EGL_NONE,
1052 };
1053 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1054 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian6fad64c2011-04-26 14:57:40 -07001055 if (image == EGL_NO_IMAGE_KHR) {
1056 EGLint error = eglGetError();
Jamie Gennis44a05222011-09-16 17:30:26 -07001057 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001058 }
1059 return image;
1060}
1061
Mathias Agopian27cd07c2011-04-11 21:19:55 -07001062sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
1063 Mutex::Autolock lock(mMutex);
1064 return mCurrentTextureBuf;
1065}
1066
1067Rect SurfaceTexture::getCurrentCrop() const {
1068 Mutex::Autolock lock(mMutex);
1069 return mCurrentCrop;
1070}
1071
1072uint32_t SurfaceTexture::getCurrentTransform() const {
1073 Mutex::Autolock lock(mMutex);
1074 return mCurrentTransform;
1075}
1076
Mathias Agopian09d7ed72011-07-13 15:24:42 -07001077uint32_t SurfaceTexture::getCurrentScalingMode() const {
1078 Mutex::Autolock lock(mMutex);
1079 return mCurrentScalingMode;
1080}
1081
Jamie Gennis87f32652011-11-19 18:04:43 -08001082bool SurfaceTexture::isSynchronousMode() const {
1083 Mutex::Autolock lock(mMutex);
1084 return mSynchronousMode;
1085}
1086
Mathias Agopianed3894c2011-04-20 14:20:59 -07001087int SurfaceTexture::query(int what, int* outValue)
1088{
1089 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001090
1091 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001092 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001093 return NO_INIT;
1094 }
1095
Mathias Agopianed3894c2011-04-20 14:20:59 -07001096 int value;
1097 switch (what) {
1098 case NATIVE_WINDOW_WIDTH:
1099 value = mDefaultWidth;
Mathias Agopianed3894c2011-04-20 14:20:59 -07001100 break;
1101 case NATIVE_WINDOW_HEIGHT:
1102 value = mDefaultHeight;
Mathias Agopianed3894c2011-04-20 14:20:59 -07001103 break;
1104 case NATIVE_WINDOW_FORMAT:
1105 value = mPixelFormat;
1106 break;
1107 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1108 value = mSynchronousMode ?
1109 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1110 break;
1111 default:
1112 return BAD_VALUE;
1113 }
1114 outValue[0] = value;
1115 return NO_ERROR;
1116}
Mathias Agopian27cd07c2011-04-11 21:19:55 -07001117
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001118void SurfaceTexture::abandon() {
1119 Mutex::Autolock lock(mMutex);
Mathias Agopian5c715752011-08-10 17:35:09 -07001120 mQueue.clear();
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001121 mAbandoned = true;
Mathias Agopianec46b4e2011-08-03 15:18:36 -07001122 mCurrentTextureBuf.clear();
Mathias Agopian5c715752011-08-10 17:35:09 -07001123 freeAllBuffersLocked();
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001124 mDequeueCondition.signal();
1125}
1126
Jamie Gennis44a05222011-09-16 17:30:26 -07001127void SurfaceTexture::setName(const String8& name) {
1128 mName = name;
1129}
1130
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001131void SurfaceTexture::dump(String8& result) const
1132{
1133 char buffer[1024];
1134 dump(result, "", buffer, 1024);
1135}
1136
1137void SurfaceTexture::dump(String8& result, const char* prefix,
1138 char* buffer, size_t SIZE) const
1139{
1140 Mutex::Autolock _l(mMutex);
1141 snprintf(buffer, SIZE,
1142 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1143 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennis44a05222011-09-16 17:30:26 -07001144 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1145 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001146 result.append(buffer);
1147
1148 String8 fifo;
1149 int fifoSize = 0;
1150 Fifo::const_iterator i(mQueue.begin());
1151 while (i != mQueue.end()) {
1152 snprintf(buffer, SIZE, "%02d ", *i++);
1153 fifoSize++;
1154 fifo.append(buffer);
1155 }
1156
1157 snprintf(buffer, SIZE,
Jamie Gennisecfa1d32011-07-19 17:58:43 -07001158 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001159 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1160 ,
1161 prefix, mCurrentCrop.left,
1162 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennisecfa1d32011-07-19 17:58:43 -07001163 mCurrentTransform, mCurrentTexture,
Jamie Gennis44a05222011-09-16 17:30:26 -07001164 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1165 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001166 );
1167 result.append(buffer);
1168
1169 struct {
1170 const char * operator()(int state) const {
1171 switch (state) {
1172 case BufferSlot::DEQUEUED: return "DEQUEUED";
1173 case BufferSlot::QUEUED: return "QUEUED";
1174 case BufferSlot::FREE: return "FREE";
1175 default: return "Unknown";
1176 }
1177 }
1178 } stateName;
1179
1180 for (int i=0 ; i<mBufferCount ; i++) {
1181 const BufferSlot& slot(mSlots[i]);
1182 snprintf(buffer, SIZE,
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001183 "%s%s[%02d] "
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001184 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian49541932011-08-09 15:48:43 -07001185 "transform=0x%02x, timestamp=%lld",
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001186 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001187 stateName(slot.mBufferState),
Jamie Gennis44a05222011-09-16 17:30:26 -07001188 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1189 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001190 );
1191 result.append(buffer);
Mathias Agopian49541932011-08-09 15:48:43 -07001192
1193 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1194 if (buf != NULL) {
1195 snprintf(buffer, SIZE,
1196 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennis44a05222011-09-16 17:30:26 -07001197 buf->handle, buf->width, buf->height, buf->stride,
1198 buf->format);
Mathias Agopian49541932011-08-09 15:48:43 -07001199 result.append(buffer);
1200 }
1201 result.append("\n");
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001202 }
1203}
1204
Jamie Gennisb598fb92011-01-09 16:33:17 -08001205static void mtxMul(float out[16], const float a[16], const float b[16]) {
1206 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1207 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1208 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1209 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1210
1211 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1212 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1213 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1214 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1215
1216 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1217 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1218 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1219 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1220
1221 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1222 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1223 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1224 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1225}
1226
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001227}; // namespace android