blob: d8821e2762a6ea467cb901cac60d1db2a1ab21bc [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>
37
38namespace android {
39
Jamie Gennisf238e282011-01-09 16:33:17 -080040// Transform matrices
41static float mtxIdentity[16] = {
42 1, 0, 0, 0,
43 0, 1, 0, 0,
44 0, 0, 1, 0,
45 0, 0, 0, 1,
46};
47static float mtxFlipH[16] = {
48 -1, 0, 0, 0,
49 0, 1, 0, 0,
50 0, 0, 1, 0,
51 1, 0, 0, 1,
52};
53static float mtxFlipV[16] = {
54 1, 0, 0, 0,
55 0, -1, 0, 0,
56 0, 0, 1, 0,
57 0, 1, 0, 1,
58};
59static float mtxRot90[16] = {
60 0, 1, 0, 0,
61 -1, 0, 0, 0,
62 0, 0, 1, 0,
63 1, 0, 0, 1,
64};
65static float mtxRot180[16] = {
66 -1, 0, 0, 0,
67 0, -1, 0, 0,
68 0, 0, 1, 0,
69 1, 1, 0, 1,
70};
71static float mtxRot270[16] = {
72 0, -1, 0, 0,
73 1, 0, 0, 0,
74 0, 0, 1, 0,
75 0, 1, 0, 1,
76};
77
78static void mtxMul(float out[16], const float a[16], const float b[16]);
79
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080080SurfaceTexture::SurfaceTexture(GLuint tex) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070081 mDefaultWidth(1),
82 mDefaultHeight(1),
83 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -070084 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
85 mClientBufferCount(0),
86 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080087 mCurrentTexture(INVALID_BUFFER_SLOT),
Mathias Agopian7a042bf2011-04-11 21:19:55 -070088 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080089 mCurrentTransform(0),
90 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080091 mNextTransform(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070092 mTexName(tex),
93 mSynchronousMode(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080094 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080095 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
96 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -070097 mNextCrop.makeInvalid();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080098}
99
100SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800101 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800102 freeAllBuffers();
103}
104
Mathias Agopian80727112011-05-02 19:51:12 -0700105status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
106 if (bufferCount > NUM_BUFFER_SLOTS)
107 return BAD_VALUE;
108
109 // special-case, nothing to do
110 if (bufferCount == mBufferCount)
111 return OK;
112
113 if (!mClientBufferCount &&
114 bufferCount >= mBufferCount) {
115 // easy, we just have more buffers
116 mBufferCount = bufferCount;
117 mServerBufferCount = bufferCount;
118 mDequeueCondition.signal();
119 } else {
120 // we're here because we're either
121 // - reducing the number of available buffers
122 // - or there is a client-buffer-count in effect
123
124 // less than 2 buffers is never allowed
125 if (bufferCount < 2)
126 return BAD_VALUE;
127
128 // when there is non client-buffer-count in effect, the client is not
129 // allowed to dequeue more than one buffer at a time,
130 // so the next time they dequeue a buffer, we know that they don't
131 // own one. the actual resizing will happen during the next
132 // dequeueBuffer.
133
134 mServerBufferCount = bufferCount;
135 }
136 return OK;
137}
138
139status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
140 Mutex::Autolock lock(mMutex);
141 return setBufferCountServerLocked(bufferCount);
142}
143
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800144status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800145 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700146 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800147
Mathias Agopian80727112011-05-02 19:51:12 -0700148 // Error out if the user has dequeued buffers
149 for (int i=0 ; i<mBufferCount ; i++) {
150 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
151 LOGE("setBufferCount: client owns some buffers");
152 return -EINVAL;
153 }
154 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700155
Mathias Agopian80727112011-05-02 19:51:12 -0700156 if (bufferCount == 0) {
157 const int minBufferSlots = mSynchronousMode ?
158 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
159 mClientBufferCount = 0;
160 bufferCount = (mServerBufferCount >= minBufferSlots) ?
161 mServerBufferCount : minBufferSlots;
162 return setBufferCountServerLocked(bufferCount);
163 }
164
165 // We don't allow the client to set a buffer-count less than
166 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
167 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800168 return BAD_VALUE;
169 }
170
Mathias Agopian80727112011-05-02 19:51:12 -0700171 // here we're guaranteed that the client doesn't have dequeued buffers
172 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173 freeAllBuffers();
174 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700175 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800176 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700177 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700178 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800179 return OK;
180}
181
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700182status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
183{
184 Mutex::Autolock lock(mMutex);
185 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
186 mDefaultWidth = w;
187 mDefaultHeight = h;
188 }
189 return OK;
190}
191
Mathias Agopianc04f1532011-04-25 20:22:14 -0700192sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800193 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194 Mutex::Autolock lock(mMutex);
195 if (buf < 0 || mBufferCount <= buf) {
196 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
197 mBufferCount, buf);
198 return 0;
199 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700200 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700201 return mSlots[buf].mGraphicBuffer;
202}
203
204status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
205 uint32_t format, uint32_t usage) {
206 LOGV("SurfaceTexture::dequeueBuffer");
207
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700208 if ((w && !h) || (!w & h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700209 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
210 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700211 }
212
Mathias Agopianc04f1532011-04-25 20:22:14 -0700213 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700214
215 status_t returnFlags(OK);
216
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700217 int found, foundSync;
218 int dequeuedCount = 0;
219 bool tryAgain = true;
220 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700221 // We need to wait for the FIFO to drain if the number of buffer
222 // needs to change.
223 //
224 // The condition "number of buffer needs to change" is true if
225 // - the client doesn't care about how many buffers there are
226 // - AND the actual number of buffer is different from what was
227 // set in the last setBufferCountServer()
228 // - OR -
229 // setBufferCountServer() was set to a value incompatible with
230 // the synchronization mode (for instance because the sync mode
231 // changed since)
232 //
233 // As long as this condition is true AND the FIFO is not empty, we
234 // wait on mDequeueCondition.
235
236 int minBufferCountNeeded = mSynchronousMode ?
237 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
238
239 if (!mClientBufferCount &&
240 ((mServerBufferCount != mBufferCount) ||
241 (mServerBufferCount < minBufferCountNeeded))) {
242 // wait for the FIFO to drain
243 while (!mQueue.isEmpty()) {
244 mDequeueCondition.wait(mMutex);
245 }
246 minBufferCountNeeded = mSynchronousMode ?
247 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
248 }
249
250
251 if (!mClientBufferCount &&
252 ((mServerBufferCount != mBufferCount) ||
253 (mServerBufferCount < minBufferCountNeeded))) {
254 // here we're guaranteed that mQueue is empty
255 freeAllBuffers();
256 mBufferCount = mServerBufferCount;
257 if (mBufferCount < minBufferCountNeeded)
258 mBufferCount = minBufferCountNeeded;
259 mCurrentTexture = INVALID_BUFFER_SLOT;
260 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
261 }
262
263 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700264 found = INVALID_BUFFER_SLOT;
265 foundSync = INVALID_BUFFER_SLOT;
266 dequeuedCount = 0;
267 for (int i = 0; i < mBufferCount; i++) {
268 const int state = mSlots[i].mBufferState;
269 if (state == BufferSlot::DEQUEUED) {
270 dequeuedCount++;
271 }
272 if (state == BufferSlot::FREE || i == mCurrentTexture) {
273 foundSync = i;
274 if (i != mCurrentTexture) {
275 found = i;
276 break;
277 }
278 }
279 }
Mathias Agopian80727112011-05-02 19:51:12 -0700280
281 // clients are not allowed to dequeue more than one buffer
282 // if they didn't set a buffer count.
283 if (!mClientBufferCount && dequeuedCount) {
284 return -EINVAL;
285 }
286
287 // make sure the client is not trying to dequeue more buffers
288 // than allowed.
289 const int avail = mBufferCount - (dequeuedCount+1);
290 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
291 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
292 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
293 dequeuedCount);
294 return -EBUSY;
295 }
296
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700297 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700298 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700299 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
300 if (tryAgain) {
301 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700302 }
303 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700304
Mathias Agopian80727112011-05-02 19:51:12 -0700305 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
306 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
307 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700308 }
309
Mathias Agopianc04f1532011-04-25 20:22:14 -0700310 if (found == INVALID_BUFFER_SLOT) {
311 return -EBUSY;
312 }
313
314 const int buf = found;
315 *outBuf = found;
316
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700317 const bool useDefaultSize = !w && !h;
318 if (useDefaultSize) {
319 // use the default size
320 w = mDefaultWidth;
321 h = mDefaultHeight;
322 }
323
324 const bool updateFormat = (format != 0);
325 if (!updateFormat) {
326 // keep the current (or default) format
327 format = mPixelFormat;
328 }
329
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700330 // buffer is now in DEQUEUED (but can also be current at the same time,
331 // if we're in synchronous mode)
332 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
333
334 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700335 if ((buffer == NULL) ||
336 (uint32_t(buffer->width) != w) ||
337 (uint32_t(buffer->height) != h) ||
338 (uint32_t(buffer->format) != format) ||
339 ((uint32_t(buffer->usage) & usage) != usage))
340 {
341 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
342 sp<GraphicBuffer> graphicBuffer(
343 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
344 if (graphicBuffer == 0) {
345 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
346 return NO_MEMORY;
347 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700348 if (updateFormat) {
349 mPixelFormat = format;
350 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800351 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700352 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800353 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
354 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
355 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
356 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
357 }
Mathias Agopian80727112011-05-02 19:51:12 -0700358 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700359 }
Mathias Agopian80727112011-05-02 19:51:12 -0700360 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800361}
362
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700363status_t SurfaceTexture::setSynchronousMode(bool enabled) {
364 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700365
366 status_t err = OK;
367 if (!enabled) {
368 // going to asynchronous mode, drain the queue
369 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
370 mDequeueCondition.wait(mMutex);
371 }
372 }
373
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700374 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700375 // - if we're going to asynchronous mode, the queue is guaranteed to be
376 // empty here
377 // - if the client set the number of buffers, we're guaranteed that
378 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700379 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700380 mDequeueCondition.signal();
381 }
Mathias Agopian80727112011-05-02 19:51:12 -0700382 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700383}
384
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800385status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800386 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800387 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700388 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800389 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
390 mBufferCount, buf);
391 return -EINVAL;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700392 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
393 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
394 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800395 return -EINVAL;
Mathias Agopian80727112011-05-02 19:51:12 -0700396 } else if (buf == mCurrentTexture) {
397 LOGE("queueBuffer: slot %d is current!", buf);
398 return -EINVAL;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700399 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800400 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
401 buf);
402 return -EINVAL;
403 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700404
405 if (mSynchronousMode) {
406 // in synchronous mode we queue all buffers in a FIFO
407 mQueue.push_back(buf);
408 } else {
409 // in asynchronous mode we only keep the most recent buffer
410 if (mQueue.empty()) {
411 mQueue.push_back(buf);
412 } else {
413 Fifo::iterator front(mQueue.begin());
414 // buffer currently queued is freed
415 mSlots[*front].mBufferState = BufferSlot::FREE;
416 // and we record the new buffer index in the queued list
417 *front = buf;
418 }
419 }
420
421 mSlots[buf].mBufferState = BufferSlot::QUEUED;
422 mSlots[buf].mLastQueuedCrop = mNextCrop;
423 mSlots[buf].mLastQueuedTransform = mNextTransform;
424 mSlots[buf].mLastQueuedTimestamp = timestamp;
425
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800426 if (mFrameAvailableListener != 0) {
427 mFrameAvailableListener->onFrameAvailable();
428 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700429 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800430 return OK;
431}
432
433void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800434 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800435 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700436 if (buf < 0 || buf >= mBufferCount) {
437 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
438 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800439 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700440 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
441 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
442 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800443 return;
444 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700445 mSlots[buf].mBufferState = BufferSlot::FREE;
446 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800447}
448
Jamie Gennisf238e282011-01-09 16:33:17 -0800449status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800450 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800451 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800452 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800453 return OK;
454}
455
456status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800457 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800458 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800459 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800460 return OK;
461}
462
463status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800464 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800465 Mutex::Autolock lock(mMutex);
466
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700467 int buf = mCurrentTexture;
468 if (!mQueue.empty()) {
469 // in asynchronous mode the list is guaranteed to be one buffer deep,
470 // while in synchronous mode we use the oldest buffer
471 Fifo::iterator front(mQueue.begin());
472 buf = *front;
473 mQueue.erase(front);
Mathias Agopian80727112011-05-02 19:51:12 -0700474 if (mQueue.isEmpty()) {
475 mDequeueCondition.signal();
476 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700477 }
478
479 // Initially both mCurrentTexture and buf are INVALID_BUFFER_SLOT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800480 // so this check will fail until a buffer gets queued.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700481 if (mCurrentTexture != buf) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800482 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700483 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800484 if (image == EGL_NO_IMAGE_KHR) {
485 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700486 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
487 mSlots[buf].mEglImage = image;
488 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700489 if (image == EGL_NO_IMAGE_KHR) {
490 // NOTE: if dpy was invalid, createImage() is guaranteed to
491 // fail. so we'd end up here.
492 return -EINVAL;
493 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800495
496 GLint error;
497 while ((error = glGetError()) != GL_NO_ERROR) {
498 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
499 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700500
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700501 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700502 if (target != mCurrentTextureTarget) {
503 glDeleteTextures(1, &mTexName);
504 }
505 glBindTexture(target, mTexName);
506 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
507
Jamie Gennis0eb88512011-01-26 11:52:02 -0800508 bool failed = false;
509 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800510 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700511 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800512 failed = true;
513 }
514 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800515 return -EINVAL;
516 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800517
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700518 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
519 // the current buffer becomes FREE if it was still in the queued
520 // state. If it has already been given to the client
521 // (synchronous mode), then it stays in DEQUEUED state.
522 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
523 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
524 }
525
Jamie Gennis9a78c902011-01-12 18:30:40 -0800526 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700527 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700528 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700529 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
530 mCurrentCrop = mSlots[buf].mLastQueuedCrop;
531 mCurrentTransform = mSlots[buf].mLastQueuedTransform;
532 mCurrentTimestamp = mSlots[buf].mLastQueuedTimestamp;
533 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700534 } else {
535 // We always bind the texture even if we don't update its contents.
536 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800537 }
538 return OK;
539}
540
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700541bool SurfaceTexture::isExternalFormat(uint32_t format)
542{
543 switch (format) {
544 // supported YUV formats
545 case HAL_PIXEL_FORMAT_YV12:
546 // Legacy/deprecated YUV formats
547 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
548 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
549 case HAL_PIXEL_FORMAT_YCbCr_422_I:
550 return true;
551 }
552
553 // Any OEM format needs to be considered
554 if (format>=0x100 && format<=0x1FF)
555 return true;
556
557 return false;
558}
559
560GLenum SurfaceTexture::getTextureTarget(uint32_t format)
561{
562 GLenum target = GL_TEXTURE_2D;
563#if defined(GL_OES_EGL_image_external)
564 if (isExternalFormat(format)) {
565 target = GL_TEXTURE_EXTERNAL_OES;
566 }
567#endif
568 return target;
569}
570
571GLenum SurfaceTexture::getCurrentTextureTarget() const {
572 Mutex::Autolock lock(mMutex);
573 return mCurrentTextureTarget;
574}
575
Jamie Gennisf238e282011-01-09 16:33:17 -0800576void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800577 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800578 Mutex::Autolock lock(mMutex);
579
Jamie Gennisa214c642011-01-14 13:53:31 -0800580 float xform[16];
581 for (int i = 0; i < 16; i++) {
582 xform[i] = mtxIdentity[i];
583 }
584 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
585 float result[16];
586 mtxMul(result, xform, mtxFlipH);
587 for (int i = 0; i < 16; i++) {
588 xform[i] = result[i];
589 }
590 }
591 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
592 float result[16];
593 mtxMul(result, xform, mtxFlipV);
594 for (int i = 0; i < 16; i++) {
595 xform[i] = result[i];
596 }
597 }
598 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
599 float result[16];
600 mtxMul(result, xform, mtxRot90);
601 for (int i = 0; i < 16; i++) {
602 xform[i] = result[i];
603 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800604 }
605
606 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800607 float tx, ty, sx, sy;
608 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800609 // In order to prevent bilinear sampling at the of the crop rectangle we
610 // may need to shrink it by 2 texels in each direction. Normally this
611 // would just need to take 1/2 a texel off each end, but because the
612 // chroma channels will likely be subsampled we need to chop off a whole
613 // texel. This will cause artifacts if someone does nearest sampling
614 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
615 // accomodate the bilinear and nearest sampling uses.
616 //
617 // If nearest sampling turns out to be a desirable usage of these
618 // textures then we could add the ability to switch a SurfaceTexture to
619 // nearest-mode. Preferably, however, the image producers (video
620 // decoder, camera, etc.) would simply not use a crop rectangle (or at
621 // least not tell the framework about it) so that the GPU can do the
622 // correct edge behavior.
623 int xshrink = 0, yshrink = 0;
624 if (mCurrentCrop.left > 0) {
625 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
626 xshrink++;
627 } else {
628 tx = 0.0f;
629 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700630 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800631 xshrink++;
632 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700633 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800634 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
635 float(buf->getHeight());
636 yshrink++;
637 } else {
638 ty = 0.0f;
639 }
640 if (mCurrentCrop.top > 0) {
641 yshrink++;
642 }
643 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
644 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800645 } else {
646 tx = 0.0f;
647 ty = 0.0f;
648 sx = 1.0f;
649 sy = 1.0f;
650 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800651 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800652 sx, 0, 0, 0,
653 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800654 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800655 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800656 };
657
Jamie Gennisa214c642011-01-14 13:53:31 -0800658 float mtxBeforeFlipV[16];
659 mtxMul(mtxBeforeFlipV, crop, xform);
660
661 // SurfaceFlinger expects the top of its window textures to be at a Y
662 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
663 // want to expose this to applications, however, so we must add an
664 // additional vertical flip to the transform after all the other transforms.
665 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800666}
667
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800668nsecs_t SurfaceTexture::getTimestamp() {
669 LOGV("SurfaceTexture::getTimestamp");
670 Mutex::Autolock lock(mMutex);
671 return mCurrentTimestamp;
672}
673
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800674void SurfaceTexture::setFrameAvailableListener(
675 const sp<FrameAvailableListener>& l) {
676 LOGV("SurfaceTexture::setFrameAvailableListener");
677 Mutex::Autolock lock(mMutex);
678 mFrameAvailableListener = l;
679}
680
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800681sp<IBinder> SurfaceTexture::getAllocator() {
682 LOGV("SurfaceTexture::getAllocator");
683 return mGraphicBufferAlloc->asBinder();
684}
685
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800686void SurfaceTexture::freeAllBuffers() {
687 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
688 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700689 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800690 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
691 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
692 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
693 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
694 }
695 }
696}
697
698EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
699 const sp<GraphicBuffer>& graphicBuffer) {
700 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
701 EGLint attrs[] = {
702 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
703 EGL_NONE,
704 };
705 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
706 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700707 if (image == EGL_NO_IMAGE_KHR) {
708 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800709 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800710 }
711 return image;
712}
713
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700714sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
715 Mutex::Autolock lock(mMutex);
716 return mCurrentTextureBuf;
717}
718
719Rect SurfaceTexture::getCurrentCrop() const {
720 Mutex::Autolock lock(mMutex);
721 return mCurrentCrop;
722}
723
724uint32_t SurfaceTexture::getCurrentTransform() const {
725 Mutex::Autolock lock(mMutex);
726 return mCurrentTransform;
727}
728
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700729int SurfaceTexture::query(int what, int* outValue)
730{
731 Mutex::Autolock lock(mMutex);
732 int value;
733 switch (what) {
734 case NATIVE_WINDOW_WIDTH:
735 value = mDefaultWidth;
736 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
737 value = mCurrentTextureBuf->width;
738 break;
739 case NATIVE_WINDOW_HEIGHT:
740 value = mDefaultHeight;
741 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
742 value = mCurrentTextureBuf->height;
743 break;
744 case NATIVE_WINDOW_FORMAT:
745 value = mPixelFormat;
746 break;
747 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
748 value = mSynchronousMode ?
749 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
750 break;
751 default:
752 return BAD_VALUE;
753 }
754 outValue[0] = value;
755 return NO_ERROR;
756}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700757
Jamie Gennisf238e282011-01-09 16:33:17 -0800758static void mtxMul(float out[16], const float a[16], const float b[16]) {
759 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
760 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
761 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
762 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
763
764 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
765 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
766 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
767 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
768
769 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
770 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
771 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
772 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
773
774 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
775 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
776 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
777 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
778}
779
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800780}; // namespace android