blob: 02b851c95e56986767e7c86d461f5549f07b1b38 [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;
602 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700603 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
604 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700605 err = -EINVAL;
606 }
607 break;
608 default:
609 err = -EINVAL;
610 break;
611 }
612 return err;
613}
614
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700615status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700616 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700617
618 switch (mode) {
619 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
620 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
621 break;
622 default:
623 return BAD_VALUE;
624 }
625
626 Mutex::Autolock lock(mMutex);
627 mNextScalingMode = mode;
628 return OK;
629}
630
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800631status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800632 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800633 Mutex::Autolock lock(mMutex);
634
Mathias Agopiane47498f2011-08-08 19:35:15 -0700635 if (mAbandoned) {
636 LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
637 //return NO_INIT;
638 }
639
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700640 // In asynchronous mode the list is guaranteed to be one buffer
641 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700642 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700643 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700644 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700645
Mathias Agopiane47498f2011-08-08 19:35:15 -0700646 if (uint32_t(buf) >= NUM_BUFFER_SLOTS) {
647 LOGE("buffer index out of range (index=%d)", buf);
648 //return BAD_VALUE;
649 }
650
Jamie Gennisf238e282011-01-09 16:33:17 -0800651 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700652 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800653 if (image == EGL_NO_IMAGE_KHR) {
654 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700655
656 if (mSlots[buf].mGraphicBuffer == 0) {
657 LOGE("buffer at slot %d is null", buf);
658 //return BAD_VALUE;
659 }
660
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700661 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
662 mSlots[buf].mEglImage = image;
663 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700664 if (image == EGL_NO_IMAGE_KHR) {
665 // NOTE: if dpy was invalid, createImage() is guaranteed to
666 // fail. so we'd end up here.
667 return -EINVAL;
668 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800669 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800670
671 GLint error;
672 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700673 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800674 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700675
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700676 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
677 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700678
Jamie Gennis0eb88512011-01-26 11:52:02 -0800679 bool failed = false;
680 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800681 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700682 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800683 failed = true;
684 }
685 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800686 return -EINVAL;
687 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800688
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700689 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700690 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700691 // state. If it has already been given to the client
692 // (synchronous mode), then it stays in DEQUEUED state.
693 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
694 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
695 }
696
Jamie Gennis9a78c902011-01-12 18:30:40 -0800697 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700698 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700699 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700700 mCurrentCrop = mSlots[buf].mCrop;
701 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700702 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700703 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700704 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700705
706 // Now that we've passed the point at which failures can happen,
707 // it's safe to remove the buffer from the front of the queue.
708 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700709 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700710 } else {
711 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700712 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800713 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700714
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800715 return OK;
716}
717
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700718bool SurfaceTexture::isExternalFormat(uint32_t format)
719{
720 switch (format) {
721 // supported YUV formats
722 case HAL_PIXEL_FORMAT_YV12:
723 // Legacy/deprecated YUV formats
724 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
725 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
726 case HAL_PIXEL_FORMAT_YCbCr_422_I:
727 return true;
728 }
729
730 // Any OEM format needs to be considered
731 if (format>=0x100 && format<=0x1FF)
732 return true;
733
734 return false;
735}
736
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700737GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700738 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700739}
740
Jamie Gennisf238e282011-01-09 16:33:17 -0800741void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800742 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700743 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
744}
745
746void SurfaceTexture::computeCurrentTransformMatrix() {
747 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800748
Jamie Gennisa214c642011-01-14 13:53:31 -0800749 float xform[16];
750 for (int i = 0; i < 16; i++) {
751 xform[i] = mtxIdentity[i];
752 }
753 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
754 float result[16];
755 mtxMul(result, xform, mtxFlipH);
756 for (int i = 0; i < 16; i++) {
757 xform[i] = result[i];
758 }
759 }
760 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
761 float result[16];
762 mtxMul(result, xform, mtxFlipV);
763 for (int i = 0; i < 16; i++) {
764 xform[i] = result[i];
765 }
766 }
767 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
768 float result[16];
769 mtxMul(result, xform, mtxRot90);
770 for (int i = 0; i < 16; i++) {
771 xform[i] = result[i];
772 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800773 }
774
775 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800776 float tx, ty, sx, sy;
777 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800778 // In order to prevent bilinear sampling at the of the crop rectangle we
779 // may need to shrink it by 2 texels in each direction. Normally this
780 // would just need to take 1/2 a texel off each end, but because the
781 // chroma channels will likely be subsampled we need to chop off a whole
782 // texel. This will cause artifacts if someone does nearest sampling
783 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
784 // accomodate the bilinear and nearest sampling uses.
785 //
786 // If nearest sampling turns out to be a desirable usage of these
787 // textures then we could add the ability to switch a SurfaceTexture to
788 // nearest-mode. Preferably, however, the image producers (video
789 // decoder, camera, etc.) would simply not use a crop rectangle (or at
790 // least not tell the framework about it) so that the GPU can do the
791 // correct edge behavior.
792 int xshrink = 0, yshrink = 0;
793 if (mCurrentCrop.left > 0) {
794 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
795 xshrink++;
796 } else {
797 tx = 0.0f;
798 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700799 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800800 xshrink++;
801 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700802 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800803 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
804 float(buf->getHeight());
805 yshrink++;
806 } else {
807 ty = 0.0f;
808 }
809 if (mCurrentCrop.top > 0) {
810 yshrink++;
811 }
812 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
813 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800814 } else {
815 tx = 0.0f;
816 ty = 0.0f;
817 sx = 1.0f;
818 sy = 1.0f;
819 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800820 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800821 sx, 0, 0, 0,
822 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800823 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800824 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800825 };
826
Jamie Gennisa214c642011-01-14 13:53:31 -0800827 float mtxBeforeFlipV[16];
828 mtxMul(mtxBeforeFlipV, crop, xform);
829
830 // SurfaceFlinger expects the top of its window textures to be at a Y
831 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
832 // want to expose this to applications, however, so we must add an
833 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700834 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800835}
836
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800837nsecs_t SurfaceTexture::getTimestamp() {
838 LOGV("SurfaceTexture::getTimestamp");
839 Mutex::Autolock lock(mMutex);
840 return mCurrentTimestamp;
841}
842
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800843void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700844 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800845 LOGV("SurfaceTexture::setFrameAvailableListener");
846 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700847 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800848}
849
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800850void SurfaceTexture::freeAllBuffers() {
851 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
852 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700853 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800854 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
855 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
856 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
857 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
858 }
859 }
860}
861
862EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
863 const sp<GraphicBuffer>& graphicBuffer) {
864 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
865 EGLint attrs[] = {
866 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
867 EGL_NONE,
868 };
869 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
870 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700871 if (image == EGL_NO_IMAGE_KHR) {
872 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800873 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800874 }
875 return image;
876}
877
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700878sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
879 Mutex::Autolock lock(mMutex);
880 return mCurrentTextureBuf;
881}
882
883Rect SurfaceTexture::getCurrentCrop() const {
884 Mutex::Autolock lock(mMutex);
885 return mCurrentCrop;
886}
887
888uint32_t SurfaceTexture::getCurrentTransform() const {
889 Mutex::Autolock lock(mMutex);
890 return mCurrentTransform;
891}
892
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700893uint32_t SurfaceTexture::getCurrentScalingMode() const {
894 Mutex::Autolock lock(mMutex);
895 return mCurrentScalingMode;
896}
897
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700898int SurfaceTexture::query(int what, int* outValue)
899{
900 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700901
902 if (mAbandoned) {
903 LOGE("query: SurfaceTexture has been abandoned!");
904 return NO_INIT;
905 }
906
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700907 int value;
908 switch (what) {
909 case NATIVE_WINDOW_WIDTH:
910 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700911 break;
912 case NATIVE_WINDOW_HEIGHT:
913 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700914 break;
915 case NATIVE_WINDOW_FORMAT:
916 value = mPixelFormat;
917 break;
918 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
919 value = mSynchronousMode ?
920 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
921 break;
922 default:
923 return BAD_VALUE;
924 }
925 outValue[0] = value;
926 return NO_ERROR;
927}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700928
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700929void SurfaceTexture::abandon() {
930 Mutex::Autolock lock(mMutex);
931 freeAllBuffers();
932 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700933 mCurrentTextureBuf.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700934 mDequeueCondition.signal();
935}
936
Mathias Agopian68c77942011-05-09 19:08:33 -0700937void SurfaceTexture::dump(String8& result) const
938{
939 char buffer[1024];
940 dump(result, "", buffer, 1024);
941}
942
943void SurfaceTexture::dump(String8& result, const char* prefix,
944 char* buffer, size_t SIZE) const
945{
946 Mutex::Autolock _l(mMutex);
947 snprintf(buffer, SIZE,
948 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
949 "mPixelFormat=%d, mTexName=%d\n",
950 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
951 mPixelFormat, mTexName);
952 result.append(buffer);
953
954 String8 fifo;
955 int fifoSize = 0;
956 Fifo::const_iterator i(mQueue.begin());
957 while (i != mQueue.end()) {
958 snprintf(buffer, SIZE, "%02d ", *i++);
959 fifoSize++;
960 fifo.append(buffer);
961 }
962
963 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700964 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -0700965 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
966 ,
967 prefix, mCurrentCrop.left,
968 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700969 mCurrentTransform, mCurrentTexture,
Mathias Agopian68c77942011-05-09 19:08:33 -0700970 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
971 mCurrentTransform, fifoSize, fifo.string()
972 );
973 result.append(buffer);
974
975 struct {
976 const char * operator()(int state) const {
977 switch (state) {
978 case BufferSlot::DEQUEUED: return "DEQUEUED";
979 case BufferSlot::QUEUED: return "QUEUED";
980 case BufferSlot::FREE: return "FREE";
981 default: return "Unknown";
982 }
983 }
984 } stateName;
985
986 for (int i=0 ; i<mBufferCount ; i++) {
987 const BufferSlot& slot(mSlots[i]);
Mathias Agopianad795ba2011-08-08 16:02:13 -0700988 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
Mathias Agopian68c77942011-05-09 19:08:33 -0700989 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -0700990 "%s%s[%02d] "
991 "%p [%4ux%4u:%4u,%3X] "
992 "state=%-8s, crop=[%d,%d,%d,%d], "
993 "transform=0x%02x, timestamp=%lld\n",
994 prefix, (i==mCurrentTexture)?">":" ", i,
995 buf->handle, buf->width, buf->height, buf->stride, buf->format,
996 stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700997 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
998 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700999 );
1000 result.append(buffer);
1001 }
1002}
1003
Jamie Gennisf238e282011-01-09 16:33:17 -08001004static void mtxMul(float out[16], const float a[16], const float b[16]) {
1005 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1006 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1007 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1008 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1009
1010 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1011 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1012 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1013 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1014
1015 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1016 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1017 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1018 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1019
1020 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1021 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1022 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1023 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1024}
1025
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001026}; // namespace android