blob: 4bbf7ebcf6f5500259f83fdd50d0d02e31f6dace [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-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 Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038
39namespace android {
40
Jamie Gennisf238e282011-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 Kloba14a0e582011-06-23 21:21:47 -070081SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070082 mDefaultWidth(1),
83 mDefaultHeight(1),
84 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -070085 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86 mClientBufferCount(0),
87 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080088 mCurrentTexture(INVALID_BUFFER_SLOT),
89 mCurrentTransform(0),
90 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080091 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -070092 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070093 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -070094 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070095 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -070096 mConnectedApi(NO_CONNECTED_API),
97 mAbandoned(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080098 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080099 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
100 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700101 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700102 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103}
104
105SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800106 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107 freeAllBuffers();
108}
109
Mathias Agopian80727112011-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 Gennis8ba32fa2010-12-20 11:27:26 -0800149status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800150 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700151 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800152
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700153 if (mAbandoned) {
154 LOGE("setBufferCount: SurfaceTexture has been abandoned!");
155 return NO_INIT;
156 }
157
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700158 if (bufferCount > NUM_BUFFER_SLOTS) {
159 LOGE("setBufferCount: bufferCount larger than slots available");
160 return BAD_VALUE;
161 }
162
Mathias Agopian80727112011-05-02 19:51:12 -0700163 // Error out if the user has dequeued buffers
164 for (int i=0 ; i<mBufferCount ; i++) {
165 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
166 LOGE("setBufferCount: client owns some buffers");
167 return -EINVAL;
168 }
169 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700170
Jamie Gennis1c121f62011-07-30 16:00:11 -0700171 const int minBufferSlots = mSynchronousMode ?
172 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700173 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700174 mClientBufferCount = 0;
175 bufferCount = (mServerBufferCount >= minBufferSlots) ?
176 mServerBufferCount : minBufferSlots;
177 return setBufferCountServerLocked(bufferCount);
178 }
179
Jamie Gennis1c121f62011-07-30 16:00:11 -0700180 if (bufferCount < minBufferSlots) {
181 LOGE("setBufferCount: requested buffer count (%d) is less than "
182 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800183 return BAD_VALUE;
184 }
185
Mathias Agopian80727112011-05-02 19:51:12 -0700186 // here we're guaranteed that the client doesn't have dequeued buffers
187 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800188 freeAllBuffers();
189 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700190 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800191 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700192 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700193 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194 return OK;
195}
196
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700197status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
198{
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700199 if (!w || !h) {
200 LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", w, h);
201 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700202 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700203
204 Mutex::Autolock lock(mMutex);
205 mDefaultWidth = w;
206 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700207 return OK;
208}
209
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700210status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800211 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800212 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700213 if (mAbandoned) {
214 LOGE("requestBuffer: SurfaceTexture has been abandoned!");
215 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800216 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700217 if (slot < 0 || mBufferCount <= slot) {
218 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
219 mBufferCount, slot);
220 return BAD_VALUE;
221 }
222 mSlots[slot].mRequestBufferCalled = true;
223 *buf = mSlots[slot].mGraphicBuffer;
224 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700225}
226
227status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
228 uint32_t format, uint32_t usage) {
229 LOGV("SurfaceTexture::dequeueBuffer");
230
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700231 if (mAbandoned) {
232 LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
233 return NO_INIT;
234 }
235
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700236 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700237 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
238 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700239 }
240
Mathias Agopianc04f1532011-04-25 20:22:14 -0700241 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700242
243 status_t returnFlags(OK);
244
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700245 int found, foundSync;
246 int dequeuedCount = 0;
247 bool tryAgain = true;
248 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700249 // We need to wait for the FIFO to drain if the number of buffer
250 // needs to change.
251 //
252 // The condition "number of buffer needs to change" is true if
253 // - the client doesn't care about how many buffers there are
254 // - AND the actual number of buffer is different from what was
255 // set in the last setBufferCountServer()
256 // - OR -
257 // setBufferCountServer() was set to a value incompatible with
258 // the synchronization mode (for instance because the sync mode
259 // changed since)
260 //
261 // As long as this condition is true AND the FIFO is not empty, we
262 // wait on mDequeueCondition.
263
264 int minBufferCountNeeded = mSynchronousMode ?
265 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
266
267 if (!mClientBufferCount &&
268 ((mServerBufferCount != mBufferCount) ||
269 (mServerBufferCount < minBufferCountNeeded))) {
270 // wait for the FIFO to drain
271 while (!mQueue.isEmpty()) {
272 mDequeueCondition.wait(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700273 if (mAbandoned) {
274 LOGE("dequeueBuffer: SurfaceTexture was abandoned while "
275 "blocked!");
276 return NO_INIT;
277 }
Mathias Agopian80727112011-05-02 19:51:12 -0700278 }
279 minBufferCountNeeded = mSynchronousMode ?
280 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
281 }
282
283
284 if (!mClientBufferCount &&
285 ((mServerBufferCount != mBufferCount) ||
286 (mServerBufferCount < minBufferCountNeeded))) {
287 // here we're guaranteed that mQueue is empty
288 freeAllBuffers();
289 mBufferCount = mServerBufferCount;
290 if (mBufferCount < minBufferCountNeeded)
291 mBufferCount = minBufferCountNeeded;
292 mCurrentTexture = INVALID_BUFFER_SLOT;
293 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
294 }
295
296 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700297 found = INVALID_BUFFER_SLOT;
298 foundSync = INVALID_BUFFER_SLOT;
299 dequeuedCount = 0;
300 for (int i = 0; i < mBufferCount; i++) {
301 const int state = mSlots[i].mBufferState;
302 if (state == BufferSlot::DEQUEUED) {
303 dequeuedCount++;
304 }
Mathias Agopiane1220792011-05-04 18:28:07 -0700305 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700306 foundSync = i;
307 if (i != mCurrentTexture) {
308 found = i;
309 break;
310 }
311 }
312 }
Mathias Agopian80727112011-05-02 19:51:12 -0700313
314 // clients are not allowed to dequeue more than one buffer
315 // if they didn't set a buffer count.
316 if (!mClientBufferCount && dequeuedCount) {
317 return -EINVAL;
318 }
319
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700320 // See whether a buffer has been queued since the last setBufferCount so
321 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
322 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
323 if (bufferHasBeenQueued) {
324 // make sure the client is not trying to dequeue more buffers
325 // than allowed.
326 const int avail = mBufferCount - (dequeuedCount+1);
327 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
328 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
329 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
330 dequeuedCount);
331 return -EBUSY;
332 }
Mathias Agopian80727112011-05-02 19:51:12 -0700333 }
334
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700335 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700336 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700337 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
338 if (tryAgain) {
339 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700340 }
341 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700342
Mathias Agopian80727112011-05-02 19:51:12 -0700343 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
344 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
345 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700346 }
347
Mathias Agopianc04f1532011-04-25 20:22:14 -0700348 if (found == INVALID_BUFFER_SLOT) {
349 return -EBUSY;
350 }
351
352 const int buf = found;
353 *outBuf = found;
354
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700355 const bool useDefaultSize = !w && !h;
356 if (useDefaultSize) {
357 // use the default size
358 w = mDefaultWidth;
359 h = mDefaultHeight;
360 }
361
362 const bool updateFormat = (format != 0);
363 if (!updateFormat) {
364 // keep the current (or default) format
365 format = mPixelFormat;
366 }
367
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700368 // buffer is now in DEQUEUED (but can also be current at the same time,
369 // if we're in synchronous mode)
370 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
371
372 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700373 if ((buffer == NULL) ||
374 (uint32_t(buffer->width) != w) ||
375 (uint32_t(buffer->height) != h) ||
376 (uint32_t(buffer->format) != format) ||
377 ((uint32_t(buffer->usage) & usage) != usage))
378 {
379 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700380 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700381 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700382 mGraphicBufferAlloc->createGraphicBuffer(
383 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700384 if (graphicBuffer == 0) {
385 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700386 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700387 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700388 if (updateFormat) {
389 mPixelFormat = format;
390 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800391 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700392 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
394 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
395 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
396 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
397 }
Mathias Agopian80727112011-05-02 19:51:12 -0700398 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700399 }
Mathias Agopian80727112011-05-02 19:51:12 -0700400 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800401}
402
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700403status_t SurfaceTexture::setSynchronousMode(bool enabled) {
404 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700405
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700406 if (mAbandoned) {
407 LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
408 return NO_INIT;
409 }
410
Mathias Agopian80727112011-05-02 19:51:12 -0700411 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700412 if (!mAllowSynchronousMode && enabled)
413 return err;
414
Mathias Agopian80727112011-05-02 19:51:12 -0700415 if (!enabled) {
416 // going to asynchronous mode, drain the queue
417 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
418 mDequeueCondition.wait(mMutex);
419 }
420 }
421
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700422 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700423 // - if we're going to asynchronous mode, the queue is guaranteed to be
424 // empty here
425 // - if the client set the number of buffers, we're guaranteed that
426 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700427 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700428 mDequeueCondition.signal();
429 }
Mathias Agopian80727112011-05-02 19:51:12 -0700430 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700431}
432
Mathias Agopian97c602c2011-07-19 15:24:46 -0700433status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
434 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800435 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700436
437 sp<FrameAvailableListener> listener;
438
439 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700440 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700441 if (mAbandoned) {
442 LOGE("queueBuffer: SurfaceTexture has been abandoned!");
443 return NO_INIT;
444 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700445 if (buf < 0 || buf >= mBufferCount) {
446 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
447 mBufferCount, buf);
448 return -EINVAL;
449 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
450 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
451 buf, mSlots[buf].mBufferState);
452 return -EINVAL;
453 } else if (buf == mCurrentTexture) {
454 LOGE("queueBuffer: slot %d is current!", buf);
455 return -EINVAL;
456 } else if (!mSlots[buf].mRequestBufferCalled) {
457 LOGE("queueBuffer: slot %d was enqueued without requesting a "
458 "buffer", buf);
459 return -EINVAL;
460 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700461
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700462 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700463 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700464 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700465
466 // Synchronous mode always signals that an additional frame should
467 // be consumed.
468 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700469 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700470 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700471 if (mQueue.empty()) {
472 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700473
474 // Asynchronous mode only signals that a frame should be
475 // consumed if no previous frame was pending. If a frame were
476 // pending then the consumer would have already been notified.
477 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700478 } else {
479 Fifo::iterator front(mQueue.begin());
480 // buffer currently queued is freed
481 mSlots[*front].mBufferState = BufferSlot::FREE;
482 // and we record the new buffer index in the queued list
483 *front = buf;
484 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700485 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700486
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700487 mSlots[buf].mBufferState = BufferSlot::QUEUED;
488 mSlots[buf].mCrop = mNextCrop;
489 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700490 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700491 mSlots[buf].mTimestamp = timestamp;
492 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700493 } // scope for the lock
494
495 // call back without lock held
496 if (listener != 0) {
497 listener->onFrameAvailable();
498 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700499
500 *outWidth = mDefaultWidth;
501 *outHeight = mDefaultHeight;
502 *outTransform = 0;
503
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800504 return OK;
505}
506
507void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800508 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800509 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700510
511 if (mAbandoned) {
512 LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
513 return;
514 }
515
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700516 if (buf < 0 || buf >= mBufferCount) {
517 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
518 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800519 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700520 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
521 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
522 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800523 return;
524 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700525 mSlots[buf].mBufferState = BufferSlot::FREE;
526 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800527}
528
Jamie Gennisf238e282011-01-09 16:33:17 -0800529status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800530 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800531 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700532 if (mAbandoned) {
533 LOGE("setCrop: SurfaceTexture has been abandoned!");
534 return NO_INIT;
535 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800536 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800537 return OK;
538}
539
540status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800541 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800542 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700543 if (mAbandoned) {
544 LOGE("setTransform: SurfaceTexture has been abandoned!");
545 return NO_INIT;
546 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800547 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800548 return OK;
549}
550
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700551status_t SurfaceTexture::connect(int api,
552 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700553 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700554 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700555
556 if (mAbandoned) {
557 LOGE("connect: SurfaceTexture has been abandoned!");
558 return NO_INIT;
559 }
560
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700561 int err = NO_ERROR;
562 switch (api) {
563 case NATIVE_WINDOW_API_EGL:
564 case NATIVE_WINDOW_API_CPU:
565 case NATIVE_WINDOW_API_MEDIA:
566 case NATIVE_WINDOW_API_CAMERA:
567 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700568 LOGE("connect: already connected (cur=%d, req=%d)",
569 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700570 err = -EINVAL;
571 } else {
572 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700573 *outWidth = mDefaultWidth;
574 *outHeight = mDefaultHeight;
575 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700576 }
577 break;
578 default:
579 err = -EINVAL;
580 break;
581 }
582 return err;
583}
584
585status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700586 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700587 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700588
589 if (mAbandoned) {
590 LOGE("connect: SurfaceTexture has been abandoned!");
591 return NO_INIT;
592 }
593
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700594 int err = NO_ERROR;
595 switch (api) {
596 case NATIVE_WINDOW_API_EGL:
597 case NATIVE_WINDOW_API_CPU:
598 case NATIVE_WINDOW_API_MEDIA:
599 case NATIVE_WINDOW_API_CAMERA:
600 if (mConnectedApi == api) {
601 mConnectedApi = NO_CONNECTED_API;
Mathias Agopianb0c15c92011-08-08 19:14:57 -0700602 freeAllBuffers();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700603 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700604 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
605 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700606 err = -EINVAL;
607 }
608 break;
609 default:
610 err = -EINVAL;
611 break;
612 }
613 return err;
614}
615
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700616status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700617 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700618
619 switch (mode) {
620 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
621 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
622 break;
623 default:
624 return BAD_VALUE;
625 }
626
627 Mutex::Autolock lock(mMutex);
628 mNextScalingMode = mode;
629 return OK;
630}
631
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800632status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800633 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800634 Mutex::Autolock lock(mMutex);
635
Mathias Agopiane47498f2011-08-08 19:35:15 -0700636 if (mAbandoned) {
637 LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
638 //return NO_INIT;
639 }
640
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700641 // In asynchronous mode the list is guaranteed to be one buffer
642 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700643 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700644 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700645 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700646
Mathias Agopiane47498f2011-08-08 19:35:15 -0700647 if (uint32_t(buf) >= NUM_BUFFER_SLOTS) {
648 LOGE("buffer index out of range (index=%d)", buf);
649 //return BAD_VALUE;
650 }
651
Jamie Gennisf238e282011-01-09 16:33:17 -0800652 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700653 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800654 if (image == EGL_NO_IMAGE_KHR) {
655 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700656
657 if (mSlots[buf].mGraphicBuffer == 0) {
658 LOGE("buffer at slot %d is null", buf);
659 //return BAD_VALUE;
660 }
661
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700662 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
663 mSlots[buf].mEglImage = image;
664 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700665 if (image == EGL_NO_IMAGE_KHR) {
666 // NOTE: if dpy was invalid, createImage() is guaranteed to
667 // fail. so we'd end up here.
668 return -EINVAL;
669 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800670 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800671
672 GLint error;
673 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700674 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800675 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700676
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700677 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
678 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700679
Jamie Gennis0eb88512011-01-26 11:52:02 -0800680 bool failed = false;
681 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800682 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700683 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800684 failed = true;
685 }
686 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800687 return -EINVAL;
688 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800689
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700690 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700691 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700692 // state. If it has already been given to the client
693 // (synchronous mode), then it stays in DEQUEUED state.
694 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
695 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
696 }
697
Jamie Gennis9a78c902011-01-12 18:30:40 -0800698 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700699 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700700 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700701 mCurrentCrop = mSlots[buf].mCrop;
702 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700703 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700704 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700705 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700706
707 // Now that we've passed the point at which failures can happen,
708 // it's safe to remove the buffer from the front of the queue.
709 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700710 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700711 } else {
712 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700713 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800714 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700715
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800716 return OK;
717}
718
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700719bool SurfaceTexture::isExternalFormat(uint32_t format)
720{
721 switch (format) {
722 // supported YUV formats
723 case HAL_PIXEL_FORMAT_YV12:
724 // Legacy/deprecated YUV formats
725 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
726 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
727 case HAL_PIXEL_FORMAT_YCbCr_422_I:
728 return true;
729 }
730
731 // Any OEM format needs to be considered
732 if (format>=0x100 && format<=0x1FF)
733 return true;
734
735 return false;
736}
737
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700738GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700739 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700740}
741
Jamie Gennisf238e282011-01-09 16:33:17 -0800742void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800743 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700744 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
745}
746
747void SurfaceTexture::computeCurrentTransformMatrix() {
748 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800749
Jamie Gennisa214c642011-01-14 13:53:31 -0800750 float xform[16];
751 for (int i = 0; i < 16; i++) {
752 xform[i] = mtxIdentity[i];
753 }
754 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
755 float result[16];
756 mtxMul(result, xform, mtxFlipH);
757 for (int i = 0; i < 16; i++) {
758 xform[i] = result[i];
759 }
760 }
761 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
762 float result[16];
763 mtxMul(result, xform, mtxFlipV);
764 for (int i = 0; i < 16; i++) {
765 xform[i] = result[i];
766 }
767 }
768 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
769 float result[16];
770 mtxMul(result, xform, mtxRot90);
771 for (int i = 0; i < 16; i++) {
772 xform[i] = result[i];
773 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800774 }
775
776 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800777 float tx, ty, sx, sy;
778 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800779 // In order to prevent bilinear sampling at the of the crop rectangle we
780 // may need to shrink it by 2 texels in each direction. Normally this
781 // would just need to take 1/2 a texel off each end, but because the
782 // chroma channels will likely be subsampled we need to chop off a whole
783 // texel. This will cause artifacts if someone does nearest sampling
784 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
785 // accomodate the bilinear and nearest sampling uses.
786 //
787 // If nearest sampling turns out to be a desirable usage of these
788 // textures then we could add the ability to switch a SurfaceTexture to
789 // nearest-mode. Preferably, however, the image producers (video
790 // decoder, camera, etc.) would simply not use a crop rectangle (or at
791 // least not tell the framework about it) so that the GPU can do the
792 // correct edge behavior.
793 int xshrink = 0, yshrink = 0;
794 if (mCurrentCrop.left > 0) {
795 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
796 xshrink++;
797 } else {
798 tx = 0.0f;
799 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700800 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800801 xshrink++;
802 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700803 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800804 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
805 float(buf->getHeight());
806 yshrink++;
807 } else {
808 ty = 0.0f;
809 }
810 if (mCurrentCrop.top > 0) {
811 yshrink++;
812 }
813 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
814 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800815 } else {
816 tx = 0.0f;
817 ty = 0.0f;
818 sx = 1.0f;
819 sy = 1.0f;
820 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800821 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800822 sx, 0, 0, 0,
823 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800824 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800825 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800826 };
827
Jamie Gennisa214c642011-01-14 13:53:31 -0800828 float mtxBeforeFlipV[16];
829 mtxMul(mtxBeforeFlipV, crop, xform);
830
831 // SurfaceFlinger expects the top of its window textures to be at a Y
832 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
833 // want to expose this to applications, however, so we must add an
834 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700835 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800836}
837
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800838nsecs_t SurfaceTexture::getTimestamp() {
839 LOGV("SurfaceTexture::getTimestamp");
840 Mutex::Autolock lock(mMutex);
841 return mCurrentTimestamp;
842}
843
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800844void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700845 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800846 LOGV("SurfaceTexture::setFrameAvailableListener");
847 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700848 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800849}
850
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800851void SurfaceTexture::freeAllBuffers() {
852 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
853 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700854 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800855 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
856 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
857 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
858 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
859 }
860 }
861}
862
863EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
864 const sp<GraphicBuffer>& graphicBuffer) {
865 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
866 EGLint attrs[] = {
867 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
868 EGL_NONE,
869 };
870 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
871 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700872 if (image == EGL_NO_IMAGE_KHR) {
873 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800874 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800875 }
876 return image;
877}
878
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700879sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
880 Mutex::Autolock lock(mMutex);
881 return mCurrentTextureBuf;
882}
883
884Rect SurfaceTexture::getCurrentCrop() const {
885 Mutex::Autolock lock(mMutex);
886 return mCurrentCrop;
887}
888
889uint32_t SurfaceTexture::getCurrentTransform() const {
890 Mutex::Autolock lock(mMutex);
891 return mCurrentTransform;
892}
893
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700894uint32_t SurfaceTexture::getCurrentScalingMode() const {
895 Mutex::Autolock lock(mMutex);
896 return mCurrentScalingMode;
897}
898
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700899int SurfaceTexture::query(int what, int* outValue)
900{
901 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700902
903 if (mAbandoned) {
904 LOGE("query: SurfaceTexture has been abandoned!");
905 return NO_INIT;
906 }
907
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700908 int value;
909 switch (what) {
910 case NATIVE_WINDOW_WIDTH:
911 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700912 break;
913 case NATIVE_WINDOW_HEIGHT:
914 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700915 break;
916 case NATIVE_WINDOW_FORMAT:
917 value = mPixelFormat;
918 break;
919 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
920 value = mSynchronousMode ?
921 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
922 break;
923 default:
924 return BAD_VALUE;
925 }
926 outValue[0] = value;
927 return NO_ERROR;
928}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700929
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700930void SurfaceTexture::abandon() {
931 Mutex::Autolock lock(mMutex);
932 freeAllBuffers();
933 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700934 mCurrentTextureBuf.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700935 mDequeueCondition.signal();
936}
937
Mathias Agopian68c77942011-05-09 19:08:33 -0700938void SurfaceTexture::dump(String8& result) const
939{
940 char buffer[1024];
941 dump(result, "", buffer, 1024);
942}
943
944void SurfaceTexture::dump(String8& result, const char* prefix,
945 char* buffer, size_t SIZE) const
946{
947 Mutex::Autolock _l(mMutex);
948 snprintf(buffer, SIZE,
949 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
950 "mPixelFormat=%d, mTexName=%d\n",
951 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
952 mPixelFormat, mTexName);
953 result.append(buffer);
954
955 String8 fifo;
956 int fifoSize = 0;
957 Fifo::const_iterator i(mQueue.begin());
958 while (i != mQueue.end()) {
959 snprintf(buffer, SIZE, "%02d ", *i++);
960 fifoSize++;
961 fifo.append(buffer);
962 }
963
964 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700965 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -0700966 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
967 ,
968 prefix, mCurrentCrop.left,
969 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700970 mCurrentTransform, mCurrentTexture,
Mathias Agopian68c77942011-05-09 19:08:33 -0700971 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
972 mCurrentTransform, fifoSize, fifo.string()
973 );
974 result.append(buffer);
975
976 struct {
977 const char * operator()(int state) const {
978 switch (state) {
979 case BufferSlot::DEQUEUED: return "DEQUEUED";
980 case BufferSlot::QUEUED: return "QUEUED";
981 case BufferSlot::FREE: return "FREE";
982 default: return "Unknown";
983 }
984 }
985 } stateName;
986
987 for (int i=0 ; i<mBufferCount ; i++) {
988 const BufferSlot& slot(mSlots[i]);
989 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -0700990 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700991 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -0700992 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -0700993 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -0700994 stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700995 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
996 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700997 );
998 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -0700999
1000 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1001 if (buf != NULL) {
1002 snprintf(buffer, SIZE,
1003 ", %p [%4ux%4u:%4u,%3X]",
1004 buf->handle, buf->width, buf->height, buf->stride, buf->format);
1005 result.append(buffer);
1006 }
1007 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001008 }
1009}
1010
Jamie Gennisf238e282011-01-09 16:33:17 -08001011static void mtxMul(float out[16], const float a[16], const float b[16]) {
1012 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1013 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1014 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1015 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1016
1017 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1018 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1019 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1020 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1021
1022 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1023 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1024 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1025 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1026
1027 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1028 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1029 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1030 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1031}
1032
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001033}; // namespace android