blob: 0f08570ef76f441a1ef124fa5172bd6dd3f5a3f6 [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
39namespace android {
40
Jamie Gennisb598fb92011-01-09 16:33:17 -080041// Transform matrices
42static float mtxIdentity[16] = {
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1,
47};
48static float mtxFlipH[16] = {
49 -1, 0, 0, 0,
50 0, 1, 0, 0,
51 0, 0, 1, 0,
52 1, 0, 0, 1,
53};
54static float mtxFlipV[16] = {
55 1, 0, 0, 0,
56 0, -1, 0, 0,
57 0, 0, 1, 0,
58 0, 1, 0, 1,
59};
60static float mtxRot90[16] = {
61 0, 1, 0, 0,
62 -1, 0, 0, 0,
63 0, 0, 1, 0,
64 1, 0, 0, 1,
65};
66static float mtxRot180[16] = {
67 -1, 0, 0, 0,
68 0, -1, 0, 0,
69 0, 0, 1, 0,
70 1, 1, 0, 1,
71};
72static float mtxRot270[16] = {
73 0, -1, 0, 0,
74 1, 0, 0, 0,
75 0, 0, 1, 0,
76 0, 1, 0, 1,
77};
78
79static void mtxMul(float out[16], const float a[16], const float b[16]);
80
Grace Kloba0904d0a2011-06-23 21:21:47 -070081SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiane5a1bff2011-03-31 19:10:24 -070082 mDefaultWidth(1),
83 mDefaultHeight(1),
84 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian402ff242011-05-02 19:51:12 -070085 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86 mClientBufferCount(0),
87 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080088 mCurrentTexture(INVALID_BUFFER_SLOT),
Mathias Agopian27cd07c2011-04-11 21:19:55 -070089 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080090 mCurrentTransform(0),
91 mCurrentTimestamp(0),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080092 mNextTransform(0),
Mathias Agopian09d7ed72011-07-13 15:24:42 -070093 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -070094 mTexName(tex),
Grace Kloba0904d0a2011-06-23 21:21:47 -070095 mSynchronousMode(false),
Jamie Gennis97096872011-07-13 19:12:20 -070096 mAllowSynchronousMode(allowSynchronousMode),
97 mConnectedApi(NO_CONNECTED_API) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -080098 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennisf7acf162011-01-12 18:30:40 -080099 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
100 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopian926340c2011-04-27 18:57:33 -0700101 mNextCrop.makeInvalid();
Jamie Genniseadfb672011-06-12 17:03:06 -0700102 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800103}
104
105SurfaceTexture::~SurfaceTexture() {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800106 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800107 freeAllBuffers();
108}
109
Mathias Agopian402ff242011-05-02 19:51:12 -0700110status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
111 if (bufferCount > NUM_BUFFER_SLOTS)
112 return BAD_VALUE;
113
114 // special-case, nothing to do
115 if (bufferCount == mBufferCount)
116 return OK;
117
118 if (!mClientBufferCount &&
119 bufferCount >= mBufferCount) {
120 // easy, we just have more buffers
121 mBufferCount = bufferCount;
122 mServerBufferCount = bufferCount;
123 mDequeueCondition.signal();
124 } else {
125 // we're here because we're either
126 // - reducing the number of available buffers
127 // - or there is a client-buffer-count in effect
128
129 // less than 2 buffers is never allowed
130 if (bufferCount < 2)
131 return BAD_VALUE;
132
133 // when there is non client-buffer-count in effect, the client is not
134 // allowed to dequeue more than one buffer at a time,
135 // so the next time they dequeue a buffer, we know that they don't
136 // own one. the actual resizing will happen during the next
137 // dequeueBuffer.
138
139 mServerBufferCount = bufferCount;
140 }
141 return OK;
142}
143
144status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
145 Mutex::Autolock lock(mMutex);
146 return setBufferCountServerLocked(bufferCount);
147}
148
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800149status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800150 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700151 Mutex::Autolock lock(mMutex);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800152
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700153 if (bufferCount > NUM_BUFFER_SLOTS) {
154 LOGE("setBufferCount: bufferCount larger than slots available");
155 return BAD_VALUE;
156 }
157
Mathias Agopian402ff242011-05-02 19:51:12 -0700158 // Error out if the user has dequeued buffers
159 for (int i=0 ; i<mBufferCount ; i++) {
160 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
161 LOGE("setBufferCount: client owns some buffers");
162 return -EINVAL;
163 }
164 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700165
Mathias Agopian402ff242011-05-02 19:51:12 -0700166 if (bufferCount == 0) {
167 const int minBufferSlots = mSynchronousMode ?
168 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
169 mClientBufferCount = 0;
170 bufferCount = (mServerBufferCount >= minBufferSlots) ?
171 mServerBufferCount : minBufferSlots;
172 return setBufferCountServerLocked(bufferCount);
173 }
174
175 // We don't allow the client to set a buffer-count less than
176 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
177 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis96dcc972011-02-27 14:10:20 -0800178 return BAD_VALUE;
179 }
180
Mathias Agopian402ff242011-05-02 19:51:12 -0700181 // here we're guaranteed that the client doesn't have dequeued buffers
182 // and will release all of its buffer references.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800183 freeAllBuffers();
184 mBufferCount = bufferCount;
Mathias Agopian402ff242011-05-02 19:51:12 -0700185 mClientBufferCount = bufferCount;
Jamie Gennisd369dc42011-01-09 13:25:39 -0800186 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700187 mQueue.clear();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700188 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800189 return OK;
190}
191
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700192status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
193{
194 Mutex::Autolock lock(mMutex);
195 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
196 mDefaultWidth = w;
197 mDefaultHeight = h;
198 }
199 return OK;
200}
201
Mathias Agopian0297dca2011-04-25 20:22:14 -0700202sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800203 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800204 Mutex::Autolock lock(mMutex);
205 if (buf < 0 || mBufferCount <= buf) {
206 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
207 mBufferCount, buf);
208 return 0;
209 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700210 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700211 return mSlots[buf].mGraphicBuffer;
212}
213
214status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
215 uint32_t format, uint32_t usage) {
216 LOGV("SurfaceTexture::dequeueBuffer");
217
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700218 if ((w && !h) || (!w && h)) {
Mathias Agopian0297dca2011-04-25 20:22:14 -0700219 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
220 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700221 }
222
Mathias Agopian0297dca2011-04-25 20:22:14 -0700223 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700224
225 status_t returnFlags(OK);
226
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700227 int found, foundSync;
228 int dequeuedCount = 0;
229 bool tryAgain = true;
230 while (tryAgain) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700231 // We need to wait for the FIFO to drain if the number of buffer
232 // needs to change.
233 //
234 // The condition "number of buffer needs to change" is true if
235 // - the client doesn't care about how many buffers there are
236 // - AND the actual number of buffer is different from what was
237 // set in the last setBufferCountServer()
238 // - OR -
239 // setBufferCountServer() was set to a value incompatible with
240 // the synchronization mode (for instance because the sync mode
241 // changed since)
242 //
243 // As long as this condition is true AND the FIFO is not empty, we
244 // wait on mDequeueCondition.
245
246 int minBufferCountNeeded = mSynchronousMode ?
247 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
248
249 if (!mClientBufferCount &&
250 ((mServerBufferCount != mBufferCount) ||
251 (mServerBufferCount < minBufferCountNeeded))) {
252 // wait for the FIFO to drain
253 while (!mQueue.isEmpty()) {
254 mDequeueCondition.wait(mMutex);
255 }
256 minBufferCountNeeded = mSynchronousMode ?
257 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
258 }
259
260
261 if (!mClientBufferCount &&
262 ((mServerBufferCount != mBufferCount) ||
263 (mServerBufferCount < minBufferCountNeeded))) {
264 // here we're guaranteed that mQueue is empty
265 freeAllBuffers();
266 mBufferCount = mServerBufferCount;
267 if (mBufferCount < minBufferCountNeeded)
268 mBufferCount = minBufferCountNeeded;
269 mCurrentTexture = INVALID_BUFFER_SLOT;
270 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
271 }
272
273 // look for a free buffer to give to the client
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700274 found = INVALID_BUFFER_SLOT;
275 foundSync = INVALID_BUFFER_SLOT;
276 dequeuedCount = 0;
277 for (int i = 0; i < mBufferCount; i++) {
278 const int state = mSlots[i].mBufferState;
279 if (state == BufferSlot::DEQUEUED) {
280 dequeuedCount++;
281 }
Mathias Agopian9b8e1962011-05-04 18:28:07 -0700282 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700283 foundSync = i;
284 if (i != mCurrentTexture) {
285 found = i;
286 break;
287 }
288 }
289 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700290
291 // clients are not allowed to dequeue more than one buffer
292 // if they didn't set a buffer count.
293 if (!mClientBufferCount && dequeuedCount) {
294 return -EINVAL;
295 }
296
Jamie Gennis44e9e0d2011-05-23 18:44:04 -0700297 // See whether a buffer has been queued since the last setBufferCount so
298 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
299 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
300 if (bufferHasBeenQueued) {
301 // make sure the client is not trying to dequeue more buffers
302 // than allowed.
303 const int avail = mBufferCount - (dequeuedCount+1);
304 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
305 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
306 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
307 dequeuedCount);
308 return -EBUSY;
309 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700310 }
311
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700312 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian402ff242011-05-02 19:51:12 -0700313 // for for some buffers to be consumed
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700314 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
315 if (tryAgain) {
316 mDequeueCondition.wait(mMutex);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700317 }
318 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700319
Mathias Agopian402ff242011-05-02 19:51:12 -0700320 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
321 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
322 found = foundSync;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700323 }
324
Mathias Agopian0297dca2011-04-25 20:22:14 -0700325 if (found == INVALID_BUFFER_SLOT) {
326 return -EBUSY;
327 }
328
329 const int buf = found;
330 *outBuf = found;
331
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700332 const bool useDefaultSize = !w && !h;
333 if (useDefaultSize) {
334 // use the default size
335 w = mDefaultWidth;
336 h = mDefaultHeight;
337 }
338
339 const bool updateFormat = (format != 0);
340 if (!updateFormat) {
341 // keep the current (or default) format
342 format = mPixelFormat;
343 }
344
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700345 // buffer is now in DEQUEUED (but can also be current at the same time,
346 // if we're in synchronous mode)
347 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
348
349 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700350 if ((buffer == NULL) ||
351 (uint32_t(buffer->width) != w) ||
352 (uint32_t(buffer->height) != h) ||
353 (uint32_t(buffer->format) != format) ||
354 ((uint32_t(buffer->usage) & usage) != usage))
355 {
356 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopianeec0f7e2011-07-01 14:53:49 -0700357 status_t error;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700358 sp<GraphicBuffer> graphicBuffer(
Mathias Agopianeec0f7e2011-07-01 14:53:49 -0700359 mGraphicBufferAlloc->createGraphicBuffer(
360 w, h, format, usage, &error));
Mathias Agopian0297dca2011-04-25 20:22:14 -0700361 if (graphicBuffer == 0) {
362 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopianeec0f7e2011-07-01 14:53:49 -0700363 return error;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700364 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700365 if (updateFormat) {
366 mPixelFormat = format;
367 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800368 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700369 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800370 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
371 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
372 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
373 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
374 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700375 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700376 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700377 return returnFlags;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800378}
379
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700380status_t SurfaceTexture::setSynchronousMode(bool enabled) {
381 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700382
383 status_t err = OK;
Grace Kloba0904d0a2011-06-23 21:21:47 -0700384 if (!mAllowSynchronousMode && enabled)
385 return err;
386
Mathias Agopian402ff242011-05-02 19:51:12 -0700387 if (!enabled) {
388 // going to asynchronous mode, drain the queue
389 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
390 mDequeueCondition.wait(mMutex);
391 }
392 }
393
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700394 if (mSynchronousMode != enabled) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700395 // - if we're going to asynchronous mode, the queue is guaranteed to be
396 // empty here
397 // - if the client set the number of buffers, we're guaranteed that
398 // we have at least 3 (because we don't allow less)
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700399 mSynchronousMode = enabled;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700400 mDequeueCondition.signal();
401 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700402 return err;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700403}
404
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700405status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
406 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800407 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiane845c352011-05-11 15:05:29 -0700408
409 sp<FrameAvailableListener> listener;
410
411 { // scope for the lock
Jamie Gennisa2187152011-05-19 13:33:00 -0700412 Mutex::Autolock lock(mMutex);
413 if (buf < 0 || buf >= mBufferCount) {
414 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
415 mBufferCount, buf);
416 return -EINVAL;
417 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
418 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
419 buf, mSlots[buf].mBufferState);
420 return -EINVAL;
421 } else if (buf == mCurrentTexture) {
422 LOGE("queueBuffer: slot %d is current!", buf);
423 return -EINVAL;
424 } else if (!mSlots[buf].mRequestBufferCalled) {
425 LOGE("queueBuffer: slot %d was enqueued without requesting a "
426 "buffer", buf);
427 return -EINVAL;
428 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700429
Jamie Gennisa2187152011-05-19 13:33:00 -0700430 if (mSynchronousMode) {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700431 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700432 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700433
434 // Synchronous mode always signals that an additional frame should
435 // be consumed.
436 listener = mFrameAvailableListener;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700437 } else {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700438 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennisa2187152011-05-19 13:33:00 -0700439 if (mQueue.empty()) {
440 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700441
442 // Asynchronous mode only signals that a frame should be
443 // consumed if no previous frame was pending. If a frame were
444 // pending then the consumer would have already been notified.
445 listener = mFrameAvailableListener;
Jamie Gennisa2187152011-05-19 13:33:00 -0700446 } else {
447 Fifo::iterator front(mQueue.begin());
448 // buffer currently queued is freed
449 mSlots[*front].mBufferState = BufferSlot::FREE;
450 // and we record the new buffer index in the queued list
451 *front = buf;
452 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700453 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700454
Jamie Gennisa2187152011-05-19 13:33:00 -0700455 mSlots[buf].mBufferState = BufferSlot::QUEUED;
456 mSlots[buf].mCrop = mNextCrop;
457 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700458 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700459 mSlots[buf].mTimestamp = timestamp;
460 mDequeueCondition.signal();
Mathias Agopiane845c352011-05-11 15:05:29 -0700461 } // scope for the lock
462
463 // call back without lock held
464 if (listener != 0) {
465 listener->onFrameAvailable();
466 }
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700467
468 *outWidth = mDefaultWidth;
469 *outHeight = mDefaultHeight;
470 *outTransform = 0;
471
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800472 return OK;
473}
474
475void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800476 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800477 Mutex::Autolock lock(mMutex);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700478 if (buf < 0 || buf >= mBufferCount) {
479 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
480 mBufferCount, buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800481 return;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700482 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
483 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
484 buf, mSlots[buf].mBufferState);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800485 return;
486 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700487 mSlots[buf].mBufferState = BufferSlot::FREE;
488 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800489}
490
Jamie Gennisb598fb92011-01-09 16:33:17 -0800491status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800492 LOGV("SurfaceTexture::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800493 Mutex::Autolock lock(mMutex);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800494 mNextCrop = crop;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800495 return OK;
496}
497
498status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800499 LOGV("SurfaceTexture::setTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800500 Mutex::Autolock lock(mMutex);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800501 mNextTransform = transform;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800502 return OK;
503}
504
Jamie Gennis97096872011-07-13 19:12:20 -0700505status_t SurfaceTexture::connect(int api) {
Mathias Agopian949be322011-07-13 17:39:11 -0700506 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700507 Mutex::Autolock lock(mMutex);
508 int err = NO_ERROR;
509 switch (api) {
510 case NATIVE_WINDOW_API_EGL:
511 case NATIVE_WINDOW_API_CPU:
512 case NATIVE_WINDOW_API_MEDIA:
513 case NATIVE_WINDOW_API_CAMERA:
514 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian949be322011-07-13 17:39:11 -0700515 LOGE("connect: already connected (cur=%d, req=%d)",
516 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700517 err = -EINVAL;
518 } else {
519 mConnectedApi = api;
520 }
521 break;
522 default:
523 err = -EINVAL;
524 break;
525 }
526 return err;
527}
528
529status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian949be322011-07-13 17:39:11 -0700530 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700531 Mutex::Autolock lock(mMutex);
532 int err = NO_ERROR;
533 switch (api) {
534 case NATIVE_WINDOW_API_EGL:
535 case NATIVE_WINDOW_API_CPU:
536 case NATIVE_WINDOW_API_MEDIA:
537 case NATIVE_WINDOW_API_CAMERA:
538 if (mConnectedApi == api) {
539 mConnectedApi = NO_CONNECTED_API;
540 } else {
Mathias Agopian949be322011-07-13 17:39:11 -0700541 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
542 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700543 err = -EINVAL;
544 }
545 break;
546 default:
547 err = -EINVAL;
548 break;
549 }
550 return err;
551}
552
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700553status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopianff86f372011-07-18 16:15:08 -0700554 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700555
556 switch (mode) {
557 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
558 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
559 break;
560 default:
561 return BAD_VALUE;
562 }
563
564 Mutex::Autolock lock(mMutex);
565 mNextScalingMode = mode;
566 return OK;
567}
568
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800569status_t SurfaceTexture::updateTexImage() {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800570 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800571 Mutex::Autolock lock(mMutex);
572
Jamie Gennis9fb59762011-06-27 15:41:52 -0700573 // In asynchronous mode the list is guaranteed to be one buffer
574 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700575 if (!mQueue.empty()) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700576 Fifo::iterator front(mQueue.begin());
Jamie Gennis9fb59762011-06-27 15:41:52 -0700577 int buf = *front;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700578
Jamie Gennisb598fb92011-01-09 16:33:17 -0800579 // Update the GL texture object.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700580 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800581 if (image == EGL_NO_IMAGE_KHR) {
582 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700583 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
584 mSlots[buf].mEglImage = image;
585 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700586 if (image == EGL_NO_IMAGE_KHR) {
587 // NOTE: if dpy was invalid, createImage() is guaranteed to
588 // fail. so we'd end up here.
589 return -EINVAL;
590 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800591 }
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800592
593 GLint error;
594 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiane845c352011-05-11 15:05:29 -0700595 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800596 }
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700597
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700598 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700599 if (target != mCurrentTextureTarget) {
600 glDeleteTextures(1, &mTexName);
601 }
602 glBindTexture(target, mTexName);
603 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
604
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800605 bool failed = false;
606 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800607 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700608 image, buf, error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800609 failed = true;
610 }
611 if (failed) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800612 return -EINVAL;
613 }
Jamie Gennisf7acf162011-01-12 18:30:40 -0800614
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700615 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis9fb59762011-06-27 15:41:52 -0700616 // The current buffer becomes FREE if it was still in the queued
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700617 // state. If it has already been given to the client
618 // (synchronous mode), then it stays in DEQUEUED state.
619 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
620 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
621 }
622
Jamie Gennisf7acf162011-01-12 18:30:40 -0800623 // Update the SurfaceTexture state.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700624 mCurrentTexture = buf;
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700625 mCurrentTextureTarget = target;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700626 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennisa2187152011-05-19 13:33:00 -0700627 mCurrentCrop = mSlots[buf].mCrop;
628 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700629 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700630 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Genniseadfb672011-06-12 17:03:06 -0700631 computeCurrentTransformMatrix();
Jamie Gennis9fb59762011-06-27 15:41:52 -0700632
633 // Now that we've passed the point at which failures can happen,
634 // it's safe to remove the buffer from the front of the queue.
635 mQueue.erase(front);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700636 mDequeueCondition.signal();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700637 } else {
638 // We always bind the texture even if we don't update its contents.
639 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800640 }
Jamie Gennis9fb59762011-06-27 15:41:52 -0700641
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800642 return OK;
643}
644
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700645bool SurfaceTexture::isExternalFormat(uint32_t format)
646{
647 switch (format) {
648 // supported YUV formats
649 case HAL_PIXEL_FORMAT_YV12:
650 // Legacy/deprecated YUV formats
651 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
652 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
653 case HAL_PIXEL_FORMAT_YCbCr_422_I:
654 return true;
655 }
656
657 // Any OEM format needs to be considered
658 if (format>=0x100 && format<=0x1FF)
659 return true;
660
661 return false;
662}
663
664GLenum SurfaceTexture::getTextureTarget(uint32_t format)
665{
666 GLenum target = GL_TEXTURE_2D;
667#if defined(GL_OES_EGL_image_external)
668 if (isExternalFormat(format)) {
669 target = GL_TEXTURE_EXTERNAL_OES;
670 }
671#endif
672 return target;
673}
674
675GLenum SurfaceTexture::getCurrentTextureTarget() const {
676 Mutex::Autolock lock(mMutex);
677 return mCurrentTextureTarget;
678}
679
Jamie Gennisb598fb92011-01-09 16:33:17 -0800680void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisb598fb92011-01-09 16:33:17 -0800681 Mutex::Autolock lock(mMutex);
Jamie Genniseadfb672011-06-12 17:03:06 -0700682 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
683}
684
685void SurfaceTexture::computeCurrentTransformMatrix() {
686 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisb598fb92011-01-09 16:33:17 -0800687
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800688 float xform[16];
689 for (int i = 0; i < 16; i++) {
690 xform[i] = mtxIdentity[i];
691 }
692 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
693 float result[16];
694 mtxMul(result, xform, mtxFlipH);
695 for (int i = 0; i < 16; i++) {
696 xform[i] = result[i];
697 }
698 }
699 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
700 float result[16];
701 mtxMul(result, xform, mtxFlipV);
702 for (int i = 0; i < 16; i++) {
703 xform[i] = result[i];
704 }
705 }
706 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
707 float result[16];
708 mtxMul(result, xform, mtxRot90);
709 for (int i = 0; i < 16; i++) {
710 xform[i] = result[i];
711 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800712 }
713
714 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800715 float tx, ty, sx, sy;
716 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800717 // In order to prevent bilinear sampling at the of the crop rectangle we
718 // may need to shrink it by 2 texels in each direction. Normally this
719 // would just need to take 1/2 a texel off each end, but because the
720 // chroma channels will likely be subsampled we need to chop off a whole
721 // texel. This will cause artifacts if someone does nearest sampling
722 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
723 // accomodate the bilinear and nearest sampling uses.
724 //
725 // If nearest sampling turns out to be a desirable usage of these
726 // textures then we could add the ability to switch a SurfaceTexture to
727 // nearest-mode. Preferably, however, the image producers (video
728 // decoder, camera, etc.) would simply not use a crop rectangle (or at
729 // least not tell the framework about it) so that the GPU can do the
730 // correct edge behavior.
731 int xshrink = 0, yshrink = 0;
732 if (mCurrentCrop.left > 0) {
733 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
734 xshrink++;
735 } else {
736 tx = 0.0f;
737 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700738 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800739 xshrink++;
740 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700741 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800742 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
743 float(buf->getHeight());
744 yshrink++;
745 } else {
746 ty = 0.0f;
747 }
748 if (mCurrentCrop.top > 0) {
749 yshrink++;
750 }
751 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
752 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800753 } else {
754 tx = 0.0f;
755 ty = 0.0f;
756 sx = 1.0f;
757 sy = 1.0f;
758 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800759 float crop[16] = {
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800760 sx, 0, 0, 0,
761 0, sy, 0, 0,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800762 0, 0, 1, 0,
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800763 tx, ty, 0, 1,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800764 };
765
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800766 float mtxBeforeFlipV[16];
767 mtxMul(mtxBeforeFlipV, crop, xform);
768
769 // SurfaceFlinger expects the top of its window textures to be at a Y
770 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
771 // want to expose this to applications, however, so we must add an
772 // additional vertical flip to the transform after all the other transforms.
Jamie Genniseadfb672011-06-12 17:03:06 -0700773 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800774}
775
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800776nsecs_t SurfaceTexture::getTimestamp() {
777 LOGV("SurfaceTexture::getTimestamp");
778 Mutex::Autolock lock(mMutex);
779 return mCurrentTimestamp;
780}
781
Jamie Gennis376590d2011-01-13 14:43:36 -0800782void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700783 const sp<FrameAvailableListener>& listener) {
Jamie Gennis376590d2011-01-13 14:43:36 -0800784 LOGV("SurfaceTexture::setFrameAvailableListener");
785 Mutex::Autolock lock(mMutex);
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700786 mFrameAvailableListener = listener;
Jamie Gennis376590d2011-01-13 14:43:36 -0800787}
788
Jamie Gennis83bac212011-02-02 15:31:47 -0800789sp<IBinder> SurfaceTexture::getAllocator() {
790 LOGV("SurfaceTexture::getAllocator");
791 return mGraphicBufferAlloc->asBinder();
792}
793
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800794void SurfaceTexture::freeAllBuffers() {
795 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
796 mSlots[i].mGraphicBuffer = 0;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700797 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800798 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
799 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
800 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
801 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
802 }
803 }
804}
805
806EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
807 const sp<GraphicBuffer>& graphicBuffer) {
808 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
809 EGLint attrs[] = {
810 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
811 EGL_NONE,
812 };
813 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
814 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700815 if (image == EGL_NO_IMAGE_KHR) {
816 EGLint error = eglGetError();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800817 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800818 }
819 return image;
820}
821
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700822sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
823 Mutex::Autolock lock(mMutex);
824 return mCurrentTextureBuf;
825}
826
827Rect SurfaceTexture::getCurrentCrop() const {
828 Mutex::Autolock lock(mMutex);
829 return mCurrentCrop;
830}
831
832uint32_t SurfaceTexture::getCurrentTransform() const {
833 Mutex::Autolock lock(mMutex);
834 return mCurrentTransform;
835}
836
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700837uint32_t SurfaceTexture::getCurrentScalingMode() const {
838 Mutex::Autolock lock(mMutex);
839 return mCurrentScalingMode;
840}
841
Mathias Agopianed3894c2011-04-20 14:20:59 -0700842int SurfaceTexture::query(int what, int* outValue)
843{
844 Mutex::Autolock lock(mMutex);
845 int value;
846 switch (what) {
847 case NATIVE_WINDOW_WIDTH:
848 value = mDefaultWidth;
849 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
850 value = mCurrentTextureBuf->width;
851 break;
852 case NATIVE_WINDOW_HEIGHT:
853 value = mDefaultHeight;
854 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
855 value = mCurrentTextureBuf->height;
856 break;
857 case NATIVE_WINDOW_FORMAT:
858 value = mPixelFormat;
859 break;
860 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
861 value = mSynchronousMode ?
862 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
863 break;
864 default:
865 return BAD_VALUE;
866 }
867 outValue[0] = value;
868 return NO_ERROR;
869}
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700870
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700871void SurfaceTexture::dump(String8& result) const
872{
873 char buffer[1024];
874 dump(result, "", buffer, 1024);
875}
876
877void SurfaceTexture::dump(String8& result, const char* prefix,
878 char* buffer, size_t SIZE) const
879{
880 Mutex::Autolock _l(mMutex);
881 snprintf(buffer, SIZE,
882 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
883 "mPixelFormat=%d, mTexName=%d\n",
884 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
885 mPixelFormat, mTexName);
886 result.append(buffer);
887
888 String8 fifo;
889 int fifoSize = 0;
890 Fifo::const_iterator i(mQueue.begin());
891 while (i != mQueue.end()) {
892 snprintf(buffer, SIZE, "%02d ", *i++);
893 fifoSize++;
894 fifo.append(buffer);
895 }
896
897 snprintf(buffer, SIZE,
898 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
899 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
900 ,
901 prefix, mCurrentCrop.left,
902 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
903 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
904 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
905 mCurrentTransform, fifoSize, fifo.string()
906 );
907 result.append(buffer);
908
909 struct {
910 const char * operator()(int state) const {
911 switch (state) {
912 case BufferSlot::DEQUEUED: return "DEQUEUED";
913 case BufferSlot::QUEUED: return "QUEUED";
914 case BufferSlot::FREE: return "FREE";
915 default: return "Unknown";
916 }
917 }
918 } stateName;
919
920 for (int i=0 ; i<mBufferCount ; i++) {
921 const BufferSlot& slot(mSlots[i]);
922 snprintf(buffer, SIZE,
923 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennisa2187152011-05-19 13:33:00 -0700924 "timestamp=%lld\n",
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700925 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennisa2187152011-05-19 13:33:00 -0700926 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
927 slot.mTransform, slot.mTimestamp
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700928 );
929 result.append(buffer);
930 }
931}
932
Jamie Gennisb598fb92011-01-09 16:33:17 -0800933static void mtxMul(float out[16], const float a[16], const float b[16]) {
934 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
935 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
936 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
937 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
938
939 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
940 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
941 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
942 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
943
944 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
945 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
946 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
947 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
948
949 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
950 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
951 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
952 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
953}
954
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800955}; // namespace android