blob: ee97dcfa9c63bf144a6ee51264ca7909d601689f [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
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080081SurfaceTexture::SurfaceTexture(GLuint tex) :
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),
Mathias Agopian7a042bf2011-04-11 21:19:55 -070089 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080090 mCurrentTransform(0),
91 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080092 mNextTransform(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070093 mTexName(tex),
94 mSynchronousMode(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080095 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080096 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
97 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -070098 mNextCrop.makeInvalid();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080099}
100
101SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800102 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103 freeAllBuffers();
104}
105
Mathias Agopian80727112011-05-02 19:51:12 -0700106status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
107 if (bufferCount > NUM_BUFFER_SLOTS)
108 return BAD_VALUE;
109
110 // special-case, nothing to do
111 if (bufferCount == mBufferCount)
112 return OK;
113
114 if (!mClientBufferCount &&
115 bufferCount >= mBufferCount) {
116 // easy, we just have more buffers
117 mBufferCount = bufferCount;
118 mServerBufferCount = bufferCount;
119 mDequeueCondition.signal();
120 } else {
121 // we're here because we're either
122 // - reducing the number of available buffers
123 // - or there is a client-buffer-count in effect
124
125 // less than 2 buffers is never allowed
126 if (bufferCount < 2)
127 return BAD_VALUE;
128
129 // when there is non client-buffer-count in effect, the client is not
130 // allowed to dequeue more than one buffer at a time,
131 // so the next time they dequeue a buffer, we know that they don't
132 // own one. the actual resizing will happen during the next
133 // dequeueBuffer.
134
135 mServerBufferCount = bufferCount;
136 }
137 return OK;
138}
139
140status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
141 Mutex::Autolock lock(mMutex);
142 return setBufferCountServerLocked(bufferCount);
143}
144
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800145status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800146 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700147 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800148
Mathias Agopian80727112011-05-02 19:51:12 -0700149 // Error out if the user has dequeued buffers
150 for (int i=0 ; i<mBufferCount ; i++) {
151 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
152 LOGE("setBufferCount: client owns some buffers");
153 return -EINVAL;
154 }
155 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700156
Mathias Agopian80727112011-05-02 19:51:12 -0700157 if (bufferCount == 0) {
158 const int minBufferSlots = mSynchronousMode ?
159 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
160 mClientBufferCount = 0;
161 bufferCount = (mServerBufferCount >= minBufferSlots) ?
162 mServerBufferCount : minBufferSlots;
163 return setBufferCountServerLocked(bufferCount);
164 }
165
166 // We don't allow the client to set a buffer-count less than
167 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
168 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800169 return BAD_VALUE;
170 }
171
Mathias Agopian80727112011-05-02 19:51:12 -0700172 // here we're guaranteed that the client doesn't have dequeued buffers
173 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800174 freeAllBuffers();
175 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700176 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800177 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700178 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700179 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800180 return OK;
181}
182
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700183status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
184{
185 Mutex::Autolock lock(mMutex);
186 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
187 mDefaultWidth = w;
188 mDefaultHeight = h;
189 }
190 return OK;
191}
192
Mathias Agopianc04f1532011-04-25 20:22:14 -0700193sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800194 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 Mutex::Autolock lock(mMutex);
196 if (buf < 0 || mBufferCount <= buf) {
197 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
198 mBufferCount, buf);
199 return 0;
200 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700201 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700202 return mSlots[buf].mGraphicBuffer;
203}
204
205status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
206 uint32_t format, uint32_t usage) {
207 LOGV("SurfaceTexture::dequeueBuffer");
208
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700209 if ((w && !h) || (!w & h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700210 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
211 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700212 }
213
Mathias Agopianc04f1532011-04-25 20:22:14 -0700214 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700215
216 status_t returnFlags(OK);
217
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700218 int found, foundSync;
219 int dequeuedCount = 0;
220 bool tryAgain = true;
221 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700222 // We need to wait for the FIFO to drain if the number of buffer
223 // needs to change.
224 //
225 // The condition "number of buffer needs to change" is true if
226 // - the client doesn't care about how many buffers there are
227 // - AND the actual number of buffer is different from what was
228 // set in the last setBufferCountServer()
229 // - OR -
230 // setBufferCountServer() was set to a value incompatible with
231 // the synchronization mode (for instance because the sync mode
232 // changed since)
233 //
234 // As long as this condition is true AND the FIFO is not empty, we
235 // wait on mDequeueCondition.
236
237 int minBufferCountNeeded = mSynchronousMode ?
238 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
239
240 if (!mClientBufferCount &&
241 ((mServerBufferCount != mBufferCount) ||
242 (mServerBufferCount < minBufferCountNeeded))) {
243 // wait for the FIFO to drain
244 while (!mQueue.isEmpty()) {
245 mDequeueCondition.wait(mMutex);
246 }
247 minBufferCountNeeded = mSynchronousMode ?
248 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
249 }
250
251
252 if (!mClientBufferCount &&
253 ((mServerBufferCount != mBufferCount) ||
254 (mServerBufferCount < minBufferCountNeeded))) {
255 // here we're guaranteed that mQueue is empty
256 freeAllBuffers();
257 mBufferCount = mServerBufferCount;
258 if (mBufferCount < minBufferCountNeeded)
259 mBufferCount = minBufferCountNeeded;
260 mCurrentTexture = INVALID_BUFFER_SLOT;
261 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
262 }
263
264 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700265 found = INVALID_BUFFER_SLOT;
266 foundSync = INVALID_BUFFER_SLOT;
267 dequeuedCount = 0;
268 for (int i = 0; i < mBufferCount; i++) {
269 const int state = mSlots[i].mBufferState;
270 if (state == BufferSlot::DEQUEUED) {
271 dequeuedCount++;
272 }
273 if (state == BufferSlot::FREE || i == mCurrentTexture) {
274 foundSync = i;
275 if (i != mCurrentTexture) {
276 found = i;
277 break;
278 }
279 }
280 }
Mathias Agopian80727112011-05-02 19:51:12 -0700281
282 // clients are not allowed to dequeue more than one buffer
283 // if they didn't set a buffer count.
284 if (!mClientBufferCount && dequeuedCount) {
285 return -EINVAL;
286 }
287
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700288 // See whether a buffer has been queued since the last setBufferCount so
289 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
290 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
291 if (bufferHasBeenQueued) {
292 // make sure the client is not trying to dequeue more buffers
293 // than allowed.
294 const int avail = mBufferCount - (dequeuedCount+1);
295 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
296 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
297 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
298 dequeuedCount);
299 return -EBUSY;
300 }
Mathias Agopian80727112011-05-02 19:51:12 -0700301 }
302
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700303 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700304 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700305 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
306 if (tryAgain) {
307 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700308 }
309 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700310
Mathias Agopian80727112011-05-02 19:51:12 -0700311 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
312 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
313 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700314 }
315
Mathias Agopianc04f1532011-04-25 20:22:14 -0700316 if (found == INVALID_BUFFER_SLOT) {
317 return -EBUSY;
318 }
319
320 const int buf = found;
321 *outBuf = found;
322
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700323 const bool useDefaultSize = !w && !h;
324 if (useDefaultSize) {
325 // use the default size
326 w = mDefaultWidth;
327 h = mDefaultHeight;
328 }
329
330 const bool updateFormat = (format != 0);
331 if (!updateFormat) {
332 // keep the current (or default) format
333 format = mPixelFormat;
334 }
335
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700336 // buffer is now in DEQUEUED (but can also be current at the same time,
337 // if we're in synchronous mode)
338 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
339
340 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700341 if ((buffer == NULL) ||
342 (uint32_t(buffer->width) != w) ||
343 (uint32_t(buffer->height) != h) ||
344 (uint32_t(buffer->format) != format) ||
345 ((uint32_t(buffer->usage) & usage) != usage))
346 {
347 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
348 sp<GraphicBuffer> graphicBuffer(
349 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
350 if (graphicBuffer == 0) {
351 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
352 return NO_MEMORY;
353 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700354 if (updateFormat) {
355 mPixelFormat = format;
356 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800357 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700358 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800359 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
360 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
361 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
362 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
363 }
Mathias Agopian80727112011-05-02 19:51:12 -0700364 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700365 }
Mathias Agopian80727112011-05-02 19:51:12 -0700366 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800367}
368
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700369status_t SurfaceTexture::setSynchronousMode(bool enabled) {
370 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700371
372 status_t err = OK;
373 if (!enabled) {
374 // going to asynchronous mode, drain the queue
375 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
376 mDequeueCondition.wait(mMutex);
377 }
378 }
379
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700380 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700381 // - if we're going to asynchronous mode, the queue is guaranteed to be
382 // empty here
383 // - if the client set the number of buffers, we're guaranteed that
384 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700385 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700386 mDequeueCondition.signal();
387 }
Mathias Agopian80727112011-05-02 19:51:12 -0700388 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700389}
390
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800391status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800392 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700393
394 sp<FrameAvailableListener> listener;
395
396 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700397 Mutex::Autolock lock(mMutex);
398 if (buf < 0 || buf >= mBufferCount) {
399 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
400 mBufferCount, buf);
401 return -EINVAL;
402 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
403 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
404 buf, mSlots[buf].mBufferState);
405 return -EINVAL;
406 } else if (buf == mCurrentTexture) {
407 LOGE("queueBuffer: slot %d is current!", buf);
408 return -EINVAL;
409 } else if (!mSlots[buf].mRequestBufferCalled) {
410 LOGE("queueBuffer: slot %d was enqueued without requesting a "
411 "buffer", buf);
412 return -EINVAL;
413 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700414
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700415 if (mQueue.empty()) {
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700416 listener = mFrameAvailableListener;
417 }
418
419 if (mSynchronousMode) {
420 // in synchronous mode we queue all buffers in a FIFO
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700421 mQueue.push_back(buf);
422 } else {
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700423 // in asynchronous mode we only keep the most recent buffer
424 if (mQueue.empty()) {
425 mQueue.push_back(buf);
426 } else {
427 Fifo::iterator front(mQueue.begin());
428 // buffer currently queued is freed
429 mSlots[*front].mBufferState = BufferSlot::FREE;
430 // and we record the new buffer index in the queued list
431 *front = buf;
432 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700433 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700434
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700435 mSlots[buf].mBufferState = BufferSlot::QUEUED;
436 mSlots[buf].mCrop = mNextCrop;
437 mSlots[buf].mTransform = mNextTransform;
438 mSlots[buf].mTimestamp = timestamp;
439 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700440 } // scope for the lock
441
442 // call back without lock held
443 if (listener != 0) {
444 listener->onFrameAvailable();
445 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800446 return OK;
447}
448
449void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800450 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800451 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700452 if (buf < 0 || buf >= mBufferCount) {
453 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
454 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800455 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700456 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
457 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
458 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459 return;
460 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700461 mSlots[buf].mBufferState = BufferSlot::FREE;
462 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800463}
464
Jamie Gennisf238e282011-01-09 16:33:17 -0800465status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800466 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800467 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800468 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800469 return OK;
470}
471
472status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800473 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800474 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800475 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800476 return OK;
477}
478
479status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800480 LOGV("SurfaceTexture::updateTexImage");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700481
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800482 Mutex::Autolock lock(mMutex);
483
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700484 int buf = mCurrentTexture;
485 if (!mQueue.empty()) {
486 // in asynchronous mode the list is guaranteed to be one buffer deep,
487 // while in synchronous mode we use the oldest buffer
488 Fifo::iterator front(mQueue.begin());
489 buf = *front;
490 mQueue.erase(front);
Mathias Agopian80727112011-05-02 19:51:12 -0700491 if (mQueue.isEmpty()) {
492 mDequeueCondition.signal();
493 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700494 }
495
496 // Initially both mCurrentTexture and buf are INVALID_BUFFER_SLOT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800497 // so this check will fail until a buffer gets queued.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700498 if (mCurrentTexture != buf) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800499 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700500 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800501 if (image == EGL_NO_IMAGE_KHR) {
502 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700503 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
504 mSlots[buf].mEglImage = image;
505 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700506 if (image == EGL_NO_IMAGE_KHR) {
507 // NOTE: if dpy was invalid, createImage() is guaranteed to
508 // fail. so we'd end up here.
509 return -EINVAL;
510 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800511 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800512
513 GLint error;
514 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700515 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800516 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700517
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700518 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700519 if (target != mCurrentTextureTarget) {
520 glDeleteTextures(1, &mTexName);
521 }
522 glBindTexture(target, mTexName);
523 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
524
Jamie Gennis0eb88512011-01-26 11:52:02 -0800525 bool failed = false;
526 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800527 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700528 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800529 failed = true;
530 }
531 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800532 return -EINVAL;
533 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800534
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700535 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
536 // the current buffer becomes FREE if it was still in the queued
537 // state. If it has already been given to the client
538 // (synchronous mode), then it stays in DEQUEUED state.
539 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
540 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
541 }
542
Jamie Gennis9a78c902011-01-12 18:30:40 -0800543 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700544 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700545 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700546 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700547 mCurrentCrop = mSlots[buf].mCrop;
548 mCurrentTransform = mSlots[buf].mTransform;
549 mCurrentTimestamp = mSlots[buf].mTimestamp;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700550 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700551 } else {
552 // We always bind the texture even if we don't update its contents.
553 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800554 }
555 return OK;
556}
557
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700558size_t SurfaceTexture::getQueuedCount() const {
559 Mutex::Autolock lock(mMutex);
560 return mQueue.size();
561}
562
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700563bool SurfaceTexture::isExternalFormat(uint32_t format)
564{
565 switch (format) {
566 // supported YUV formats
567 case HAL_PIXEL_FORMAT_YV12:
568 // Legacy/deprecated YUV formats
569 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
570 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
571 case HAL_PIXEL_FORMAT_YCbCr_422_I:
572 return true;
573 }
574
575 // Any OEM format needs to be considered
576 if (format>=0x100 && format<=0x1FF)
577 return true;
578
579 return false;
580}
581
582GLenum SurfaceTexture::getTextureTarget(uint32_t format)
583{
584 GLenum target = GL_TEXTURE_2D;
585#if defined(GL_OES_EGL_image_external)
586 if (isExternalFormat(format)) {
587 target = GL_TEXTURE_EXTERNAL_OES;
588 }
589#endif
590 return target;
591}
592
593GLenum SurfaceTexture::getCurrentTextureTarget() const {
594 Mutex::Autolock lock(mMutex);
595 return mCurrentTextureTarget;
596}
597
Jamie Gennisf238e282011-01-09 16:33:17 -0800598void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800599 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800600 Mutex::Autolock lock(mMutex);
601
Jamie Gennisa214c642011-01-14 13:53:31 -0800602 float xform[16];
603 for (int i = 0; i < 16; i++) {
604 xform[i] = mtxIdentity[i];
605 }
606 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
607 float result[16];
608 mtxMul(result, xform, mtxFlipH);
609 for (int i = 0; i < 16; i++) {
610 xform[i] = result[i];
611 }
612 }
613 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
614 float result[16];
615 mtxMul(result, xform, mtxFlipV);
616 for (int i = 0; i < 16; i++) {
617 xform[i] = result[i];
618 }
619 }
620 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
621 float result[16];
622 mtxMul(result, xform, mtxRot90);
623 for (int i = 0; i < 16; i++) {
624 xform[i] = result[i];
625 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800626 }
627
628 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800629 float tx, ty, sx, sy;
630 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800631 // In order to prevent bilinear sampling at the of the crop rectangle we
632 // may need to shrink it by 2 texels in each direction. Normally this
633 // would just need to take 1/2 a texel off each end, but because the
634 // chroma channels will likely be subsampled we need to chop off a whole
635 // texel. This will cause artifacts if someone does nearest sampling
636 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
637 // accomodate the bilinear and nearest sampling uses.
638 //
639 // If nearest sampling turns out to be a desirable usage of these
640 // textures then we could add the ability to switch a SurfaceTexture to
641 // nearest-mode. Preferably, however, the image producers (video
642 // decoder, camera, etc.) would simply not use a crop rectangle (or at
643 // least not tell the framework about it) so that the GPU can do the
644 // correct edge behavior.
645 int xshrink = 0, yshrink = 0;
646 if (mCurrentCrop.left > 0) {
647 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
648 xshrink++;
649 } else {
650 tx = 0.0f;
651 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700652 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800653 xshrink++;
654 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700655 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800656 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
657 float(buf->getHeight());
658 yshrink++;
659 } else {
660 ty = 0.0f;
661 }
662 if (mCurrentCrop.top > 0) {
663 yshrink++;
664 }
665 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
666 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800667 } else {
668 tx = 0.0f;
669 ty = 0.0f;
670 sx = 1.0f;
671 sy = 1.0f;
672 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800673 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800674 sx, 0, 0, 0,
675 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800676 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800677 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800678 };
679
Jamie Gennisa214c642011-01-14 13:53:31 -0800680 float mtxBeforeFlipV[16];
681 mtxMul(mtxBeforeFlipV, crop, xform);
682
683 // SurfaceFlinger expects the top of its window textures to be at a Y
684 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
685 // want to expose this to applications, however, so we must add an
686 // additional vertical flip to the transform after all the other transforms.
687 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800688}
689
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800690nsecs_t SurfaceTexture::getTimestamp() {
691 LOGV("SurfaceTexture::getTimestamp");
692 Mutex::Autolock lock(mMutex);
693 return mCurrentTimestamp;
694}
695
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800696void SurfaceTexture::setFrameAvailableListener(
697 const sp<FrameAvailableListener>& l) {
698 LOGV("SurfaceTexture::setFrameAvailableListener");
699 Mutex::Autolock lock(mMutex);
700 mFrameAvailableListener = l;
701}
702
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800703sp<IBinder> SurfaceTexture::getAllocator() {
704 LOGV("SurfaceTexture::getAllocator");
705 return mGraphicBufferAlloc->asBinder();
706}
707
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800708void SurfaceTexture::freeAllBuffers() {
709 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
710 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700711 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800712 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
713 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
714 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
715 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
716 }
717 }
718}
719
720EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
721 const sp<GraphicBuffer>& graphicBuffer) {
722 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
723 EGLint attrs[] = {
724 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
725 EGL_NONE,
726 };
727 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
728 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700729 if (image == EGL_NO_IMAGE_KHR) {
730 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800731 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800732 }
733 return image;
734}
735
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700736sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
737 Mutex::Autolock lock(mMutex);
738 return mCurrentTextureBuf;
739}
740
741Rect SurfaceTexture::getCurrentCrop() const {
742 Mutex::Autolock lock(mMutex);
743 return mCurrentCrop;
744}
745
746uint32_t SurfaceTexture::getCurrentTransform() const {
747 Mutex::Autolock lock(mMutex);
748 return mCurrentTransform;
749}
750
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700751int SurfaceTexture::query(int what, int* outValue)
752{
753 Mutex::Autolock lock(mMutex);
754 int value;
755 switch (what) {
756 case NATIVE_WINDOW_WIDTH:
757 value = mDefaultWidth;
758 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
759 value = mCurrentTextureBuf->width;
760 break;
761 case NATIVE_WINDOW_HEIGHT:
762 value = mDefaultHeight;
763 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
764 value = mCurrentTextureBuf->height;
765 break;
766 case NATIVE_WINDOW_FORMAT:
767 value = mPixelFormat;
768 break;
769 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
770 value = mSynchronousMode ?
771 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
772 break;
773 default:
774 return BAD_VALUE;
775 }
776 outValue[0] = value;
777 return NO_ERROR;
778}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700779
Mathias Agopian68c77942011-05-09 19:08:33 -0700780void SurfaceTexture::dump(String8& result) const
781{
782 char buffer[1024];
783 dump(result, "", buffer, 1024);
784}
785
786void SurfaceTexture::dump(String8& result, const char* prefix,
787 char* buffer, size_t SIZE) const
788{
789 Mutex::Autolock _l(mMutex);
790 snprintf(buffer, SIZE,
791 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
792 "mPixelFormat=%d, mTexName=%d\n",
793 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
794 mPixelFormat, mTexName);
795 result.append(buffer);
796
797 String8 fifo;
798 int fifoSize = 0;
799 Fifo::const_iterator i(mQueue.begin());
800 while (i != mQueue.end()) {
801 snprintf(buffer, SIZE, "%02d ", *i++);
802 fifoSize++;
803 fifo.append(buffer);
804 }
805
806 snprintf(buffer, SIZE,
807 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
808 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
809 ,
810 prefix, mCurrentCrop.left,
811 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
812 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
813 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
814 mCurrentTransform, fifoSize, fifo.string()
815 );
816 result.append(buffer);
817
818 struct {
819 const char * operator()(int state) const {
820 switch (state) {
821 case BufferSlot::DEQUEUED: return "DEQUEUED";
822 case BufferSlot::QUEUED: return "QUEUED";
823 case BufferSlot::FREE: return "FREE";
824 default: return "Unknown";
825 }
826 }
827 } stateName;
828
829 for (int i=0 ; i<mBufferCount ; i++) {
830 const BufferSlot& slot(mSlots[i]);
831 snprintf(buffer, SIZE,
832 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700833 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700834 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700835 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
836 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700837 );
838 result.append(buffer);
839 }
840}
841
Jamie Gennisf238e282011-01-09 16:33:17 -0800842static void mtxMul(float out[16], const float a[16], const float b[16]) {
843 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
844 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
845 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
846 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
847
848 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
849 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
850 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
851 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
852
853 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
854 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
855 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
856 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
857
858 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
859 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
860 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
861 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
862}
863
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800864}; // namespace android