blob: bcf7a63b1592d6cb8180aa27d3a8d27b4ff43dfe [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
288 // make sure the client is not trying to dequeue more buffers
289 // than allowed.
290 const int avail = mBufferCount - (dequeuedCount+1);
291 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
292 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
293 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
294 dequeuedCount);
Teng-Hui Zhud7e291f2011-05-18 10:38:40 -0700295 // TODO: Enable this error report after we fix issue 4435022
296 // return -EBUSY;
Mathias Agopian80727112011-05-02 19:51:12 -0700297 }
298
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700299 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700300 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700301 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
302 if (tryAgain) {
303 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700304 }
305 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700306
Mathias Agopian80727112011-05-02 19:51:12 -0700307 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
308 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
309 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700310 }
311
Mathias Agopianc04f1532011-04-25 20:22:14 -0700312 if (found == INVALID_BUFFER_SLOT) {
313 return -EBUSY;
314 }
315
316 const int buf = found;
317 *outBuf = found;
318
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700319 const bool useDefaultSize = !w && !h;
320 if (useDefaultSize) {
321 // use the default size
322 w = mDefaultWidth;
323 h = mDefaultHeight;
324 }
325
326 const bool updateFormat = (format != 0);
327 if (!updateFormat) {
328 // keep the current (or default) format
329 format = mPixelFormat;
330 }
331
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700332 // buffer is now in DEQUEUED (but can also be current at the same time,
333 // if we're in synchronous mode)
334 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
335
336 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700337 if ((buffer == NULL) ||
338 (uint32_t(buffer->width) != w) ||
339 (uint32_t(buffer->height) != h) ||
340 (uint32_t(buffer->format) != format) ||
341 ((uint32_t(buffer->usage) & usage) != usage))
342 {
343 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
344 sp<GraphicBuffer> graphicBuffer(
345 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
346 if (graphicBuffer == 0) {
347 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
348 return NO_MEMORY;
349 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700350 if (updateFormat) {
351 mPixelFormat = format;
352 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800353 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700354 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800355 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
356 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
357 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
358 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
359 }
Mathias Agopian80727112011-05-02 19:51:12 -0700360 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700361 }
Mathias Agopian80727112011-05-02 19:51:12 -0700362 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800363}
364
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700365status_t SurfaceTexture::setSynchronousMode(bool enabled) {
366 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700367
368 status_t err = OK;
369 if (!enabled) {
370 // going to asynchronous mode, drain the queue
371 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
372 mDequeueCondition.wait(mMutex);
373 }
374 }
375
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700376 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700377 // - if we're going to asynchronous mode, the queue is guaranteed to be
378 // empty here
379 // - if the client set the number of buffers, we're guaranteed that
380 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700381 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700382 mDequeueCondition.signal();
383 }
Mathias Agopian80727112011-05-02 19:51:12 -0700384 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700385}
386
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800387status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800388 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700389
390 sp<FrameAvailableListener> listener;
391
392 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700393 Mutex::Autolock lock(mMutex);
394 if (buf < 0 || buf >= mBufferCount) {
395 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
396 mBufferCount, buf);
397 return -EINVAL;
398 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
399 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
400 buf, mSlots[buf].mBufferState);
401 return -EINVAL;
402 } else if (buf == mCurrentTexture) {
403 LOGE("queueBuffer: slot %d is current!", buf);
404 return -EINVAL;
405 } else if (!mSlots[buf].mRequestBufferCalled) {
406 LOGE("queueBuffer: slot %d was enqueued without requesting a "
407 "buffer", buf);
408 return -EINVAL;
409 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700410
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700411 if (mQueue.empty()) {
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700412 listener = mFrameAvailableListener;
413 }
414
415 if (mSynchronousMode) {
416 // in synchronous mode we queue all buffers in a FIFO
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700417 mQueue.push_back(buf);
418 } else {
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700419 // in asynchronous mode we only keep the most recent buffer
420 if (mQueue.empty()) {
421 mQueue.push_back(buf);
422 } else {
423 Fifo::iterator front(mQueue.begin());
424 // buffer currently queued is freed
425 mSlots[*front].mBufferState = BufferSlot::FREE;
426 // and we record the new buffer index in the queued list
427 *front = buf;
428 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700429 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700430
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700431 mSlots[buf].mBufferState = BufferSlot::QUEUED;
432 mSlots[buf].mCrop = mNextCrop;
433 mSlots[buf].mTransform = mNextTransform;
434 mSlots[buf].mTimestamp = timestamp;
435 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700436 } // scope for the lock
437
438 // call back without lock held
439 if (listener != 0) {
440 listener->onFrameAvailable();
441 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800442 return OK;
443}
444
445void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800446 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800447 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700448 if (buf < 0 || buf >= mBufferCount) {
449 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
450 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800451 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700452 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
453 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
454 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800455 return;
456 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700457 mSlots[buf].mBufferState = BufferSlot::FREE;
458 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459}
460
Jamie Gennisf238e282011-01-09 16:33:17 -0800461status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800462 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800463 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800464 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800465 return OK;
466}
467
468status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800469 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800470 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800471 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800472 return OK;
473}
474
475status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800476 LOGV("SurfaceTexture::updateTexImage");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700477
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800478 Mutex::Autolock lock(mMutex);
479
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700480 int buf = mCurrentTexture;
481 if (!mQueue.empty()) {
482 // in asynchronous mode the list is guaranteed to be one buffer deep,
483 // while in synchronous mode we use the oldest buffer
484 Fifo::iterator front(mQueue.begin());
485 buf = *front;
486 mQueue.erase(front);
Mathias Agopian80727112011-05-02 19:51:12 -0700487 if (mQueue.isEmpty()) {
488 mDequeueCondition.signal();
489 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700490 }
491
492 // Initially both mCurrentTexture and buf are INVALID_BUFFER_SLOT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800493 // so this check will fail until a buffer gets queued.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700494 if (mCurrentTexture != buf) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800495 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700496 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800497 if (image == EGL_NO_IMAGE_KHR) {
498 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700499 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
500 mSlots[buf].mEglImage = image;
501 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700502 if (image == EGL_NO_IMAGE_KHR) {
503 // NOTE: if dpy was invalid, createImage() is guaranteed to
504 // fail. so we'd end up here.
505 return -EINVAL;
506 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800507 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800508
509 GLint error;
510 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700511 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800512 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700513
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700514 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700515 if (target != mCurrentTextureTarget) {
516 glDeleteTextures(1, &mTexName);
517 }
518 glBindTexture(target, mTexName);
519 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
520
Jamie Gennis0eb88512011-01-26 11:52:02 -0800521 bool failed = false;
522 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800523 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700524 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800525 failed = true;
526 }
527 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800528 return -EINVAL;
529 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800530
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700531 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
532 // the current buffer becomes FREE if it was still in the queued
533 // state. If it has already been given to the client
534 // (synchronous mode), then it stays in DEQUEUED state.
535 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
536 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
537 }
538
Jamie Gennis9a78c902011-01-12 18:30:40 -0800539 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700540 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700541 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700542 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700543 mCurrentCrop = mSlots[buf].mCrop;
544 mCurrentTransform = mSlots[buf].mTransform;
545 mCurrentTimestamp = mSlots[buf].mTimestamp;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700546 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700547 } else {
548 // We always bind the texture even if we don't update its contents.
549 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800550 }
551 return OK;
552}
553
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700554size_t SurfaceTexture::getQueuedCount() const {
555 Mutex::Autolock lock(mMutex);
556 return mQueue.size();
557}
558
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700559bool SurfaceTexture::isExternalFormat(uint32_t format)
560{
561 switch (format) {
562 // supported YUV formats
563 case HAL_PIXEL_FORMAT_YV12:
564 // Legacy/deprecated YUV formats
565 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
566 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
567 case HAL_PIXEL_FORMAT_YCbCr_422_I:
568 return true;
569 }
570
571 // Any OEM format needs to be considered
572 if (format>=0x100 && format<=0x1FF)
573 return true;
574
575 return false;
576}
577
578GLenum SurfaceTexture::getTextureTarget(uint32_t format)
579{
580 GLenum target = GL_TEXTURE_2D;
581#if defined(GL_OES_EGL_image_external)
582 if (isExternalFormat(format)) {
583 target = GL_TEXTURE_EXTERNAL_OES;
584 }
585#endif
586 return target;
587}
588
589GLenum SurfaceTexture::getCurrentTextureTarget() const {
590 Mutex::Autolock lock(mMutex);
591 return mCurrentTextureTarget;
592}
593
Jamie Gennisf238e282011-01-09 16:33:17 -0800594void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800595 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800596 Mutex::Autolock lock(mMutex);
597
Jamie Gennisa214c642011-01-14 13:53:31 -0800598 float xform[16];
599 for (int i = 0; i < 16; i++) {
600 xform[i] = mtxIdentity[i];
601 }
602 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
603 float result[16];
604 mtxMul(result, xform, mtxFlipH);
605 for (int i = 0; i < 16; i++) {
606 xform[i] = result[i];
607 }
608 }
609 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
610 float result[16];
611 mtxMul(result, xform, mtxFlipV);
612 for (int i = 0; i < 16; i++) {
613 xform[i] = result[i];
614 }
615 }
616 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
617 float result[16];
618 mtxMul(result, xform, mtxRot90);
619 for (int i = 0; i < 16; i++) {
620 xform[i] = result[i];
621 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800622 }
623
624 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800625 float tx, ty, sx, sy;
626 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800627 // In order to prevent bilinear sampling at the of the crop rectangle we
628 // may need to shrink it by 2 texels in each direction. Normally this
629 // would just need to take 1/2 a texel off each end, but because the
630 // chroma channels will likely be subsampled we need to chop off a whole
631 // texel. This will cause artifacts if someone does nearest sampling
632 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
633 // accomodate the bilinear and nearest sampling uses.
634 //
635 // If nearest sampling turns out to be a desirable usage of these
636 // textures then we could add the ability to switch a SurfaceTexture to
637 // nearest-mode. Preferably, however, the image producers (video
638 // decoder, camera, etc.) would simply not use a crop rectangle (or at
639 // least not tell the framework about it) so that the GPU can do the
640 // correct edge behavior.
641 int xshrink = 0, yshrink = 0;
642 if (mCurrentCrop.left > 0) {
643 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
644 xshrink++;
645 } else {
646 tx = 0.0f;
647 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700648 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800649 xshrink++;
650 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700651 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800652 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
653 float(buf->getHeight());
654 yshrink++;
655 } else {
656 ty = 0.0f;
657 }
658 if (mCurrentCrop.top > 0) {
659 yshrink++;
660 }
661 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
662 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800663 } else {
664 tx = 0.0f;
665 ty = 0.0f;
666 sx = 1.0f;
667 sy = 1.0f;
668 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800669 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800670 sx, 0, 0, 0,
671 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800672 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800673 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800674 };
675
Jamie Gennisa214c642011-01-14 13:53:31 -0800676 float mtxBeforeFlipV[16];
677 mtxMul(mtxBeforeFlipV, crop, xform);
678
679 // SurfaceFlinger expects the top of its window textures to be at a Y
680 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
681 // want to expose this to applications, however, so we must add an
682 // additional vertical flip to the transform after all the other transforms.
683 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800684}
685
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800686nsecs_t SurfaceTexture::getTimestamp() {
687 LOGV("SurfaceTexture::getTimestamp");
688 Mutex::Autolock lock(mMutex);
689 return mCurrentTimestamp;
690}
691
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800692void SurfaceTexture::setFrameAvailableListener(
693 const sp<FrameAvailableListener>& l) {
694 LOGV("SurfaceTexture::setFrameAvailableListener");
695 Mutex::Autolock lock(mMutex);
696 mFrameAvailableListener = l;
697}
698
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800699sp<IBinder> SurfaceTexture::getAllocator() {
700 LOGV("SurfaceTexture::getAllocator");
701 return mGraphicBufferAlloc->asBinder();
702}
703
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800704void SurfaceTexture::freeAllBuffers() {
705 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
706 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700707 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800708 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
709 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
710 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
711 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
712 }
713 }
714}
715
716EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
717 const sp<GraphicBuffer>& graphicBuffer) {
718 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
719 EGLint attrs[] = {
720 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
721 EGL_NONE,
722 };
723 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
724 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700725 if (image == EGL_NO_IMAGE_KHR) {
726 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800727 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800728 }
729 return image;
730}
731
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700732sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
733 Mutex::Autolock lock(mMutex);
734 return mCurrentTextureBuf;
735}
736
737Rect SurfaceTexture::getCurrentCrop() const {
738 Mutex::Autolock lock(mMutex);
739 return mCurrentCrop;
740}
741
742uint32_t SurfaceTexture::getCurrentTransform() const {
743 Mutex::Autolock lock(mMutex);
744 return mCurrentTransform;
745}
746
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700747int SurfaceTexture::query(int what, int* outValue)
748{
749 Mutex::Autolock lock(mMutex);
750 int value;
751 switch (what) {
752 case NATIVE_WINDOW_WIDTH:
753 value = mDefaultWidth;
754 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
755 value = mCurrentTextureBuf->width;
756 break;
757 case NATIVE_WINDOW_HEIGHT:
758 value = mDefaultHeight;
759 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
760 value = mCurrentTextureBuf->height;
761 break;
762 case NATIVE_WINDOW_FORMAT:
763 value = mPixelFormat;
764 break;
765 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
766 value = mSynchronousMode ?
767 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
768 break;
769 default:
770 return BAD_VALUE;
771 }
772 outValue[0] = value;
773 return NO_ERROR;
774}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700775
Mathias Agopian68c77942011-05-09 19:08:33 -0700776void SurfaceTexture::dump(String8& result) const
777{
778 char buffer[1024];
779 dump(result, "", buffer, 1024);
780}
781
782void SurfaceTexture::dump(String8& result, const char* prefix,
783 char* buffer, size_t SIZE) const
784{
785 Mutex::Autolock _l(mMutex);
786 snprintf(buffer, SIZE,
787 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
788 "mPixelFormat=%d, mTexName=%d\n",
789 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
790 mPixelFormat, mTexName);
791 result.append(buffer);
792
793 String8 fifo;
794 int fifoSize = 0;
795 Fifo::const_iterator i(mQueue.begin());
796 while (i != mQueue.end()) {
797 snprintf(buffer, SIZE, "%02d ", *i++);
798 fifoSize++;
799 fifo.append(buffer);
800 }
801
802 snprintf(buffer, SIZE,
803 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
804 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
805 ,
806 prefix, mCurrentCrop.left,
807 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
808 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
809 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
810 mCurrentTransform, fifoSize, fifo.string()
811 );
812 result.append(buffer);
813
814 struct {
815 const char * operator()(int state) const {
816 switch (state) {
817 case BufferSlot::DEQUEUED: return "DEQUEUED";
818 case BufferSlot::QUEUED: return "QUEUED";
819 case BufferSlot::FREE: return "FREE";
820 default: return "Unknown";
821 }
822 }
823 } stateName;
824
825 for (int i=0 ; i<mBufferCount ; i++) {
826 const BufferSlot& slot(mSlots[i]);
827 snprintf(buffer, SIZE,
828 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700829 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700830 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700831 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
832 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700833 );
834 result.append(buffer);
835 }
836}
837
Jamie Gennisf238e282011-01-09 16:33:17 -0800838static void mtxMul(float out[16], const float a[16], const float b[16]) {
839 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
840 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
841 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
842 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
843
844 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
845 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
846 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
847 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
848
849 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
850 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
851 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
852 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
853
854 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
855 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
856 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
857 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
858}
859
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800860}; // namespace android