blob: 1a036ee57cd2813ab6ba440c21bc0fb5f962441c [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
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700551status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700552 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700553 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700554
555 if (mAbandoned) {
556 LOGE("connect: SurfaceTexture has been abandoned!");
557 return NO_INIT;
558 }
559
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700560 int err = NO_ERROR;
561 switch (api) {
562 case NATIVE_WINDOW_API_EGL:
563 case NATIVE_WINDOW_API_CPU:
564 case NATIVE_WINDOW_API_MEDIA:
565 case NATIVE_WINDOW_API_CAMERA:
566 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700567 LOGE("connect: already connected (cur=%d, req=%d)",
568 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700569 err = -EINVAL;
570 } else {
571 mConnectedApi = api;
572 }
573 break;
574 default:
575 err = -EINVAL;
576 break;
577 }
578 return err;
579}
580
581status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700582 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700583 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700584
585 if (mAbandoned) {
586 LOGE("connect: SurfaceTexture has been abandoned!");
587 return NO_INIT;
588 }
589
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700590 int err = NO_ERROR;
591 switch (api) {
592 case NATIVE_WINDOW_API_EGL:
593 case NATIVE_WINDOW_API_CPU:
594 case NATIVE_WINDOW_API_MEDIA:
595 case NATIVE_WINDOW_API_CAMERA:
596 if (mConnectedApi == api) {
597 mConnectedApi = NO_CONNECTED_API;
598 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700599 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
600 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700601 err = -EINVAL;
602 }
603 break;
604 default:
605 err = -EINVAL;
606 break;
607 }
608 return err;
609}
610
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700611status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700612 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700613
614 switch (mode) {
615 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
616 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
617 break;
618 default:
619 return BAD_VALUE;
620 }
621
622 Mutex::Autolock lock(mMutex);
623 mNextScalingMode = mode;
624 return OK;
625}
626
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800627status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800628 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800629 Mutex::Autolock lock(mMutex);
630
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700631 // In asynchronous mode the list is guaranteed to be one buffer
632 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700633 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700634 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700635 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700636
Jamie Gennisf238e282011-01-09 16:33:17 -0800637 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700638 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800639 if (image == EGL_NO_IMAGE_KHR) {
640 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700641 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
642 mSlots[buf].mEglImage = image;
643 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700644 if (image == EGL_NO_IMAGE_KHR) {
645 // NOTE: if dpy was invalid, createImage() is guaranteed to
646 // fail. so we'd end up here.
647 return -EINVAL;
648 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800649 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800650
651 GLint error;
652 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700653 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800654 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700655
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700656 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
657 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700658
Jamie Gennis0eb88512011-01-26 11:52:02 -0800659 bool failed = false;
660 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800661 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700662 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800663 failed = true;
664 }
665 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800666 return -EINVAL;
667 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800668
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700669 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700670 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700671 // state. If it has already been given to the client
672 // (synchronous mode), then it stays in DEQUEUED state.
673 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
674 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
675 }
676
Jamie Gennis9a78c902011-01-12 18:30:40 -0800677 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700678 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700679 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700680 mCurrentCrop = mSlots[buf].mCrop;
681 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700682 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700683 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700684 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700685
686 // Now that we've passed the point at which failures can happen,
687 // it's safe to remove the buffer from the front of the queue.
688 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700689 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700690 } else {
691 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700692 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800693 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700694
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800695 return OK;
696}
697
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700698bool SurfaceTexture::isExternalFormat(uint32_t format)
699{
700 switch (format) {
701 // supported YUV formats
702 case HAL_PIXEL_FORMAT_YV12:
703 // Legacy/deprecated YUV formats
704 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
705 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
706 case HAL_PIXEL_FORMAT_YCbCr_422_I:
707 return true;
708 }
709
710 // Any OEM format needs to be considered
711 if (format>=0x100 && format<=0x1FF)
712 return true;
713
714 return false;
715}
716
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700717GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700718 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700719}
720
Jamie Gennisf238e282011-01-09 16:33:17 -0800721void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800722 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700723 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
724}
725
726void SurfaceTexture::computeCurrentTransformMatrix() {
727 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800728
Jamie Gennisa214c642011-01-14 13:53:31 -0800729 float xform[16];
730 for (int i = 0; i < 16; i++) {
731 xform[i] = mtxIdentity[i];
732 }
733 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
734 float result[16];
735 mtxMul(result, xform, mtxFlipH);
736 for (int i = 0; i < 16; i++) {
737 xform[i] = result[i];
738 }
739 }
740 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
741 float result[16];
742 mtxMul(result, xform, mtxFlipV);
743 for (int i = 0; i < 16; i++) {
744 xform[i] = result[i];
745 }
746 }
747 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
748 float result[16];
749 mtxMul(result, xform, mtxRot90);
750 for (int i = 0; i < 16; i++) {
751 xform[i] = result[i];
752 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800753 }
754
755 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800756 float tx, ty, sx, sy;
757 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800758 // In order to prevent bilinear sampling at the of the crop rectangle we
759 // may need to shrink it by 2 texels in each direction. Normally this
760 // would just need to take 1/2 a texel off each end, but because the
761 // chroma channels will likely be subsampled we need to chop off a whole
762 // texel. This will cause artifacts if someone does nearest sampling
763 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
764 // accomodate the bilinear and nearest sampling uses.
765 //
766 // If nearest sampling turns out to be a desirable usage of these
767 // textures then we could add the ability to switch a SurfaceTexture to
768 // nearest-mode. Preferably, however, the image producers (video
769 // decoder, camera, etc.) would simply not use a crop rectangle (or at
770 // least not tell the framework about it) so that the GPU can do the
771 // correct edge behavior.
772 int xshrink = 0, yshrink = 0;
773 if (mCurrentCrop.left > 0) {
774 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
775 xshrink++;
776 } else {
777 tx = 0.0f;
778 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700779 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800780 xshrink++;
781 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700782 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800783 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
784 float(buf->getHeight());
785 yshrink++;
786 } else {
787 ty = 0.0f;
788 }
789 if (mCurrentCrop.top > 0) {
790 yshrink++;
791 }
792 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
793 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800794 } else {
795 tx = 0.0f;
796 ty = 0.0f;
797 sx = 1.0f;
798 sy = 1.0f;
799 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800800 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800801 sx, 0, 0, 0,
802 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800803 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800804 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800805 };
806
Jamie Gennisa214c642011-01-14 13:53:31 -0800807 float mtxBeforeFlipV[16];
808 mtxMul(mtxBeforeFlipV, crop, xform);
809
810 // SurfaceFlinger expects the top of its window textures to be at a Y
811 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
812 // want to expose this to applications, however, so we must add an
813 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700814 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800815}
816
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800817nsecs_t SurfaceTexture::getTimestamp() {
818 LOGV("SurfaceTexture::getTimestamp");
819 Mutex::Autolock lock(mMutex);
820 return mCurrentTimestamp;
821}
822
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800823void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700824 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800825 LOGV("SurfaceTexture::setFrameAvailableListener");
826 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700827 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800828}
829
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800830void SurfaceTexture::freeAllBuffers() {
831 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
832 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700833 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800834 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
835 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
836 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
837 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
838 }
839 }
840}
841
842EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
843 const sp<GraphicBuffer>& graphicBuffer) {
844 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
845 EGLint attrs[] = {
846 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
847 EGL_NONE,
848 };
849 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
850 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700851 if (image == EGL_NO_IMAGE_KHR) {
852 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800853 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800854 }
855 return image;
856}
857
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700858sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
859 Mutex::Autolock lock(mMutex);
860 return mCurrentTextureBuf;
861}
862
863Rect SurfaceTexture::getCurrentCrop() const {
864 Mutex::Autolock lock(mMutex);
865 return mCurrentCrop;
866}
867
868uint32_t SurfaceTexture::getCurrentTransform() const {
869 Mutex::Autolock lock(mMutex);
870 return mCurrentTransform;
871}
872
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700873uint32_t SurfaceTexture::getCurrentScalingMode() const {
874 Mutex::Autolock lock(mMutex);
875 return mCurrentScalingMode;
876}
877
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700878int SurfaceTexture::query(int what, int* outValue)
879{
880 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700881
882 if (mAbandoned) {
883 LOGE("query: SurfaceTexture has been abandoned!");
884 return NO_INIT;
885 }
886
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700887 int value;
888 switch (what) {
889 case NATIVE_WINDOW_WIDTH:
890 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700891 break;
892 case NATIVE_WINDOW_HEIGHT:
893 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700894 break;
895 case NATIVE_WINDOW_FORMAT:
896 value = mPixelFormat;
897 break;
898 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
899 value = mSynchronousMode ?
900 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
901 break;
902 default:
903 return BAD_VALUE;
904 }
905 outValue[0] = value;
906 return NO_ERROR;
907}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700908
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700909void SurfaceTexture::abandon() {
910 Mutex::Autolock lock(mMutex);
911 freeAllBuffers();
912 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700913 mCurrentTextureBuf.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700914 mDequeueCondition.signal();
915}
916
Mathias Agopian68c77942011-05-09 19:08:33 -0700917void SurfaceTexture::dump(String8& result) const
918{
919 char buffer[1024];
920 dump(result, "", buffer, 1024);
921}
922
923void SurfaceTexture::dump(String8& result, const char* prefix,
924 char* buffer, size_t SIZE) const
925{
926 Mutex::Autolock _l(mMutex);
927 snprintf(buffer, SIZE,
928 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
929 "mPixelFormat=%d, mTexName=%d\n",
930 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
931 mPixelFormat, mTexName);
932 result.append(buffer);
933
934 String8 fifo;
935 int fifoSize = 0;
936 Fifo::const_iterator i(mQueue.begin());
937 while (i != mQueue.end()) {
938 snprintf(buffer, SIZE, "%02d ", *i++);
939 fifoSize++;
940 fifo.append(buffer);
941 }
942
943 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700944 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -0700945 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
946 ,
947 prefix, mCurrentCrop.left,
948 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700949 mCurrentTransform, mCurrentTexture,
Mathias Agopian68c77942011-05-09 19:08:33 -0700950 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
951 mCurrentTransform, fifoSize, fifo.string()
952 );
953 result.append(buffer);
954
955 struct {
956 const char * operator()(int state) const {
957 switch (state) {
958 case BufferSlot::DEQUEUED: return "DEQUEUED";
959 case BufferSlot::QUEUED: return "QUEUED";
960 case BufferSlot::FREE: return "FREE";
961 default: return "Unknown";
962 }
963 }
964 } stateName;
965
966 for (int i=0 ; i<mBufferCount ; i++) {
967 const BufferSlot& slot(mSlots[i]);
968 snprintf(buffer, SIZE,
969 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700970 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700971 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700972 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
973 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700974 );
975 result.append(buffer);
976 }
977}
978
Jamie Gennisf238e282011-01-09 16:33:17 -0800979static void mtxMul(float out[16], const float a[16], const float b[16]) {
980 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
981 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
982 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
983 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
984
985 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
986 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
987 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
988 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
989
990 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
991 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
992 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
993 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
994
995 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
996 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
997 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
998 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
999}
1000
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001001}; // namespace android