blob: 3cecdb401ab17b794a21ed8b046e0968667c4a9e [file] [log] [blame]
Jamie Gennis68e4a7a2010-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 Gennis7dc00d52011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-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 Agopian27cd07c2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennisf7acf162011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian7f3289c2011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080038
39namespace android {
40
Jamie Gennisb598fb92011-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 Gennis68e4a7a2010-12-20 11:27:26 -080081SurfaceTexture::SurfaceTexture(GLuint tex) :
Mathias Agopiane5a1bff2011-03-31 19:10:24 -070082 mDefaultWidth(1),
83 mDefaultHeight(1),
84 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian402ff242011-05-02 19:51:12 -070085 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86 mClientBufferCount(0),
87 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080088 mCurrentTexture(INVALID_BUFFER_SLOT),
Mathias Agopian27cd07c2011-04-11 21:19:55 -070089 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080090 mCurrentTransform(0),
91 mCurrentTimestamp(0),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080092 mNextTransform(0),
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -070093 mTexName(tex),
94 mSynchronousMode(false) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -080095 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennisf7acf162011-01-12 18:30:40 -080096 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
97 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopian926340c2011-04-27 18:57:33 -070098 mNextCrop.makeInvalid();
Jamie Genniseadfb672011-06-12 17:03:06 -070099 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800100}
101
102SurfaceTexture::~SurfaceTexture() {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800103 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800104 freeAllBuffers();
105}
106
Mathias Agopian402ff242011-05-02 19:51:12 -0700107status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
108 if (bufferCount > NUM_BUFFER_SLOTS)
109 return BAD_VALUE;
110
111 // special-case, nothing to do
112 if (bufferCount == mBufferCount)
113 return OK;
114
115 if (!mClientBufferCount &&
116 bufferCount >= mBufferCount) {
117 // easy, we just have more buffers
118 mBufferCount = bufferCount;
119 mServerBufferCount = bufferCount;
120 mDequeueCondition.signal();
121 } else {
122 // we're here because we're either
123 // - reducing the number of available buffers
124 // - or there is a client-buffer-count in effect
125
126 // less than 2 buffers is never allowed
127 if (bufferCount < 2)
128 return BAD_VALUE;
129
130 // when there is non client-buffer-count in effect, the client is not
131 // allowed to dequeue more than one buffer at a time,
132 // so the next time they dequeue a buffer, we know that they don't
133 // own one. the actual resizing will happen during the next
134 // dequeueBuffer.
135
136 mServerBufferCount = bufferCount;
137 }
138 return OK;
139}
140
141status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
142 Mutex::Autolock lock(mMutex);
143 return setBufferCountServerLocked(bufferCount);
144}
145
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800146status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800147 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700148 Mutex::Autolock lock(mMutex);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800149
Mathias Agopian402ff242011-05-02 19:51:12 -0700150 // Error out if the user has dequeued buffers
151 for (int i=0 ; i<mBufferCount ; i++) {
152 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
153 LOGE("setBufferCount: client owns some buffers");
154 return -EINVAL;
155 }
156 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700157
Mathias Agopian402ff242011-05-02 19:51:12 -0700158 if (bufferCount == 0) {
159 const int minBufferSlots = mSynchronousMode ?
160 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
161 mClientBufferCount = 0;
162 bufferCount = (mServerBufferCount >= minBufferSlots) ?
163 mServerBufferCount : minBufferSlots;
164 return setBufferCountServerLocked(bufferCount);
165 }
166
167 // We don't allow the client to set a buffer-count less than
168 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
169 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis96dcc972011-02-27 14:10:20 -0800170 return BAD_VALUE;
171 }
172
Mathias Agopian402ff242011-05-02 19:51:12 -0700173 // here we're guaranteed that the client doesn't have dequeued buffers
174 // and will release all of its buffer references.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800175 freeAllBuffers();
176 mBufferCount = bufferCount;
Mathias Agopian402ff242011-05-02 19:51:12 -0700177 mClientBufferCount = bufferCount;
Jamie Gennisd369dc42011-01-09 13:25:39 -0800178 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700179 mQueue.clear();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700180 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800181 return OK;
182}
183
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700184status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
185{
186 Mutex::Autolock lock(mMutex);
187 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
188 mDefaultWidth = w;
189 mDefaultHeight = h;
190 }
191 return OK;
192}
193
Mathias Agopian0297dca2011-04-25 20:22:14 -0700194sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800195 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800196 Mutex::Autolock lock(mMutex);
197 if (buf < 0 || mBufferCount <= buf) {
198 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
199 mBufferCount, buf);
200 return 0;
201 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700202 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700203 return mSlots[buf].mGraphicBuffer;
204}
205
206status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
207 uint32_t format, uint32_t usage) {
208 LOGV("SurfaceTexture::dequeueBuffer");
209
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700210 if ((w && !h) || (!w & h)) {
Mathias Agopian0297dca2011-04-25 20:22:14 -0700211 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
212 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700213 }
214
Mathias Agopian0297dca2011-04-25 20:22:14 -0700215 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700216
217 status_t returnFlags(OK);
218
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700219 int found, foundSync;
220 int dequeuedCount = 0;
221 bool tryAgain = true;
222 while (tryAgain) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700223 // We need to wait for the FIFO to drain if the number of buffer
224 // needs to change.
225 //
226 // The condition "number of buffer needs to change" is true if
227 // - the client doesn't care about how many buffers there are
228 // - AND the actual number of buffer is different from what was
229 // set in the last setBufferCountServer()
230 // - OR -
231 // setBufferCountServer() was set to a value incompatible with
232 // the synchronization mode (for instance because the sync mode
233 // changed since)
234 //
235 // As long as this condition is true AND the FIFO is not empty, we
236 // wait on mDequeueCondition.
237
238 int minBufferCountNeeded = mSynchronousMode ?
239 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
240
241 if (!mClientBufferCount &&
242 ((mServerBufferCount != mBufferCount) ||
243 (mServerBufferCount < minBufferCountNeeded))) {
244 // wait for the FIFO to drain
245 while (!mQueue.isEmpty()) {
246 mDequeueCondition.wait(mMutex);
247 }
248 minBufferCountNeeded = mSynchronousMode ?
249 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
250 }
251
252
253 if (!mClientBufferCount &&
254 ((mServerBufferCount != mBufferCount) ||
255 (mServerBufferCount < minBufferCountNeeded))) {
256 // here we're guaranteed that mQueue is empty
257 freeAllBuffers();
258 mBufferCount = mServerBufferCount;
259 if (mBufferCount < minBufferCountNeeded)
260 mBufferCount = minBufferCountNeeded;
261 mCurrentTexture = INVALID_BUFFER_SLOT;
262 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
263 }
264
265 // look for a free buffer to give to the client
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700266 found = INVALID_BUFFER_SLOT;
267 foundSync = INVALID_BUFFER_SLOT;
268 dequeuedCount = 0;
269 for (int i = 0; i < mBufferCount; i++) {
270 const int state = mSlots[i].mBufferState;
271 if (state == BufferSlot::DEQUEUED) {
272 dequeuedCount++;
273 }
Mathias Agopian9b8e1962011-05-04 18:28:07 -0700274 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700275 foundSync = i;
276 if (i != mCurrentTexture) {
277 found = i;
278 break;
279 }
280 }
281 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700282
283 // clients are not allowed to dequeue more than one buffer
284 // if they didn't set a buffer count.
285 if (!mClientBufferCount && dequeuedCount) {
286 return -EINVAL;
287 }
288
Jamie Gennis44e9e0d2011-05-23 18:44:04 -0700289 // See whether a buffer has been queued since the last setBufferCount so
290 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
291 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
292 if (bufferHasBeenQueued) {
293 // make sure the client is not trying to dequeue more buffers
294 // than allowed.
295 const int avail = mBufferCount - (dequeuedCount+1);
296 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
297 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
298 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
299 dequeuedCount);
300 return -EBUSY;
301 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700302 }
303
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700304 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian402ff242011-05-02 19:51:12 -0700305 // for for some buffers to be consumed
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700306 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
307 if (tryAgain) {
308 mDequeueCondition.wait(mMutex);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700309 }
310 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700311
Mathias Agopian402ff242011-05-02 19:51:12 -0700312 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
313 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
314 found = foundSync;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700315 }
316
Mathias Agopian0297dca2011-04-25 20:22:14 -0700317 if (found == INVALID_BUFFER_SLOT) {
318 return -EBUSY;
319 }
320
321 const int buf = found;
322 *outBuf = found;
323
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700324 const bool useDefaultSize = !w && !h;
325 if (useDefaultSize) {
326 // use the default size
327 w = mDefaultWidth;
328 h = mDefaultHeight;
329 }
330
331 const bool updateFormat = (format != 0);
332 if (!updateFormat) {
333 // keep the current (or default) format
334 format = mPixelFormat;
335 }
336
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700337 // buffer is now in DEQUEUED (but can also be current at the same time,
338 // if we're in synchronous mode)
339 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
340
341 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700342 if ((buffer == NULL) ||
343 (uint32_t(buffer->width) != w) ||
344 (uint32_t(buffer->height) != h) ||
345 (uint32_t(buffer->format) != format) ||
346 ((uint32_t(buffer->usage) & usage) != usage))
347 {
348 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
349 sp<GraphicBuffer> graphicBuffer(
350 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
351 if (graphicBuffer == 0) {
352 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
353 return NO_MEMORY;
354 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700355 if (updateFormat) {
356 mPixelFormat = format;
357 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800358 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700359 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800360 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
361 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
362 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
363 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
364 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700365 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700366 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700367 return returnFlags;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800368}
369
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700370status_t SurfaceTexture::setSynchronousMode(bool enabled) {
371 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700372
373 status_t err = OK;
374 if (!enabled) {
375 // going to asynchronous mode, drain the queue
376 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
377 mDequeueCondition.wait(mMutex);
378 }
379 }
380
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700381 if (mSynchronousMode != enabled) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700382 // - if we're going to asynchronous mode, the queue is guaranteed to be
383 // empty here
384 // - if the client set the number of buffers, we're guaranteed that
385 // we have at least 3 (because we don't allow less)
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700386 mSynchronousMode = enabled;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700387 mDequeueCondition.signal();
388 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700389 return err;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700390}
391
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800392status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800393 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiane845c352011-05-11 15:05:29 -0700394
395 sp<FrameAvailableListener> listener;
396
397 { // scope for the lock
Jamie Gennisa2187152011-05-19 13:33:00 -0700398 Mutex::Autolock lock(mMutex);
399 if (buf < 0 || buf >= mBufferCount) {
400 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
401 mBufferCount, buf);
402 return -EINVAL;
403 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
404 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
405 buf, mSlots[buf].mBufferState);
406 return -EINVAL;
407 } else if (buf == mCurrentTexture) {
408 LOGE("queueBuffer: slot %d is current!", buf);
409 return -EINVAL;
410 } else if (!mSlots[buf].mRequestBufferCalled) {
411 LOGE("queueBuffer: slot %d was enqueued without requesting a "
412 "buffer", buf);
413 return -EINVAL;
414 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700415
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700416 if (mQueue.empty()) {
Jamie Gennisa2187152011-05-19 13:33:00 -0700417 listener = mFrameAvailableListener;
418 }
419
420 if (mSynchronousMode) {
421 // in synchronous mode we queue all buffers in a FIFO
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700422 mQueue.push_back(buf);
423 } else {
Jamie Gennisa2187152011-05-19 13:33:00 -0700424 // in asynchronous mode we only keep the most recent buffer
425 if (mQueue.empty()) {
426 mQueue.push_back(buf);
427 } else {
428 Fifo::iterator front(mQueue.begin());
429 // buffer currently queued is freed
430 mSlots[*front].mBufferState = BufferSlot::FREE;
431 // and we record the new buffer index in the queued list
432 *front = buf;
433 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700434 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700435
Jamie Gennisa2187152011-05-19 13:33:00 -0700436 mSlots[buf].mBufferState = BufferSlot::QUEUED;
437 mSlots[buf].mCrop = mNextCrop;
438 mSlots[buf].mTransform = mNextTransform;
439 mSlots[buf].mTimestamp = timestamp;
440 mDequeueCondition.signal();
Mathias Agopiane845c352011-05-11 15:05:29 -0700441 } // scope for the lock
442
443 // call back without lock held
444 if (listener != 0) {
445 listener->onFrameAvailable();
446 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800447 return OK;
448}
449
450void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800451 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800452 Mutex::Autolock lock(mMutex);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700453 if (buf < 0 || buf >= mBufferCount) {
454 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
455 mBufferCount, buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800456 return;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700457 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
458 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
459 buf, mSlots[buf].mBufferState);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800460 return;
461 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700462 mSlots[buf].mBufferState = BufferSlot::FREE;
463 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800464}
465
Jamie Gennisb598fb92011-01-09 16:33:17 -0800466status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800467 LOGV("SurfaceTexture::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800468 Mutex::Autolock lock(mMutex);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800469 mNextCrop = crop;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800470 return OK;
471}
472
473status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800474 LOGV("SurfaceTexture::setTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800475 Mutex::Autolock lock(mMutex);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800476 mNextTransform = transform;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800477 return OK;
478}
479
480status_t SurfaceTexture::updateTexImage() {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800481 LOGV("SurfaceTexture::updateTexImage");
Mathias Agopiane845c352011-05-11 15:05:29 -0700482
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800483 Mutex::Autolock lock(mMutex);
484
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700485 int buf = mCurrentTexture;
486 if (!mQueue.empty()) {
487 // in asynchronous mode the list is guaranteed to be one buffer deep,
488 // while in synchronous mode we use the oldest buffer
489 Fifo::iterator front(mQueue.begin());
490 buf = *front;
491 mQueue.erase(front);
Mathias Agopian402ff242011-05-02 19:51:12 -0700492 if (mQueue.isEmpty()) {
493 mDequeueCondition.signal();
494 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700495 }
496
497 // Initially both mCurrentTexture and buf are INVALID_BUFFER_SLOT,
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800498 // so this check will fail until a buffer gets queued.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700499 if (mCurrentTexture != buf) {
Jamie Gennisb598fb92011-01-09 16:33:17 -0800500 // Update the GL texture object.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700501 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800502 if (image == EGL_NO_IMAGE_KHR) {
503 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700504 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
505 mSlots[buf].mEglImage = image;
506 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700507 if (image == EGL_NO_IMAGE_KHR) {
508 // NOTE: if dpy was invalid, createImage() is guaranteed to
509 // fail. so we'd end up here.
510 return -EINVAL;
511 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800512 }
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800513
514 GLint error;
515 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiane845c352011-05-11 15:05:29 -0700516 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800517 }
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700518
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700519 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700520 if (target != mCurrentTextureTarget) {
521 glDeleteTextures(1, &mTexName);
522 }
523 glBindTexture(target, mTexName);
524 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
525
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800526 bool failed = false;
527 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800528 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700529 image, buf, error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800530 failed = true;
531 }
532 if (failed) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800533 return -EINVAL;
534 }
Jamie Gennisf7acf162011-01-12 18:30:40 -0800535
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700536 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
537 // the current buffer becomes FREE if it was still in the queued
538 // state. If it has already been given to the client
539 // (synchronous mode), then it stays in DEQUEUED state.
540 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
541 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
542 }
543
Jamie Gennisf7acf162011-01-12 18:30:40 -0800544 // Update the SurfaceTexture state.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700545 mCurrentTexture = buf;
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700546 mCurrentTextureTarget = target;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700547 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennisa2187152011-05-19 13:33:00 -0700548 mCurrentCrop = mSlots[buf].mCrop;
549 mCurrentTransform = mSlots[buf].mTransform;
550 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Genniseadfb672011-06-12 17:03:06 -0700551 computeCurrentTransformMatrix();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700552 mDequeueCondition.signal();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700553 } else {
554 // We always bind the texture even if we don't update its contents.
555 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800556 }
557 return OK;
558}
559
Mathias Agopiane845c352011-05-11 15:05:29 -0700560size_t SurfaceTexture::getQueuedCount() const {
561 Mutex::Autolock lock(mMutex);
562 return mQueue.size();
563}
564
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700565bool SurfaceTexture::isExternalFormat(uint32_t format)
566{
567 switch (format) {
568 // supported YUV formats
569 case HAL_PIXEL_FORMAT_YV12:
570 // Legacy/deprecated YUV formats
571 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
572 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
573 case HAL_PIXEL_FORMAT_YCbCr_422_I:
574 return true;
575 }
576
577 // Any OEM format needs to be considered
578 if (format>=0x100 && format<=0x1FF)
579 return true;
580
581 return false;
582}
583
584GLenum SurfaceTexture::getTextureTarget(uint32_t format)
585{
586 GLenum target = GL_TEXTURE_2D;
587#if defined(GL_OES_EGL_image_external)
588 if (isExternalFormat(format)) {
589 target = GL_TEXTURE_EXTERNAL_OES;
590 }
591#endif
592 return target;
593}
594
595GLenum SurfaceTexture::getCurrentTextureTarget() const {
596 Mutex::Autolock lock(mMutex);
597 return mCurrentTextureTarget;
598}
599
Jamie Gennisb598fb92011-01-09 16:33:17 -0800600void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisb598fb92011-01-09 16:33:17 -0800601 Mutex::Autolock lock(mMutex);
Jamie Genniseadfb672011-06-12 17:03:06 -0700602 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
603}
604
605void SurfaceTexture::computeCurrentTransformMatrix() {
606 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisb598fb92011-01-09 16:33:17 -0800607
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800608 float xform[16];
609 for (int i = 0; i < 16; i++) {
610 xform[i] = mtxIdentity[i];
611 }
612 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
613 float result[16];
614 mtxMul(result, xform, mtxFlipH);
615 for (int i = 0; i < 16; i++) {
616 xform[i] = result[i];
617 }
618 }
619 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
620 float result[16];
621 mtxMul(result, xform, mtxFlipV);
622 for (int i = 0; i < 16; i++) {
623 xform[i] = result[i];
624 }
625 }
626 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
627 float result[16];
628 mtxMul(result, xform, mtxRot90);
629 for (int i = 0; i < 16; i++) {
630 xform[i] = result[i];
631 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800632 }
633
634 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800635 float tx, ty, sx, sy;
636 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800637 // In order to prevent bilinear sampling at the of the crop rectangle we
638 // may need to shrink it by 2 texels in each direction. Normally this
639 // would just need to take 1/2 a texel off each end, but because the
640 // chroma channels will likely be subsampled we need to chop off a whole
641 // texel. This will cause artifacts if someone does nearest sampling
642 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
643 // accomodate the bilinear and nearest sampling uses.
644 //
645 // If nearest sampling turns out to be a desirable usage of these
646 // textures then we could add the ability to switch a SurfaceTexture to
647 // nearest-mode. Preferably, however, the image producers (video
648 // decoder, camera, etc.) would simply not use a crop rectangle (or at
649 // least not tell the framework about it) so that the GPU can do the
650 // correct edge behavior.
651 int xshrink = 0, yshrink = 0;
652 if (mCurrentCrop.left > 0) {
653 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
654 xshrink++;
655 } else {
656 tx = 0.0f;
657 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700658 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800659 xshrink++;
660 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700661 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800662 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
663 float(buf->getHeight());
664 yshrink++;
665 } else {
666 ty = 0.0f;
667 }
668 if (mCurrentCrop.top > 0) {
669 yshrink++;
670 }
671 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
672 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800673 } else {
674 tx = 0.0f;
675 ty = 0.0f;
676 sx = 1.0f;
677 sy = 1.0f;
678 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800679 float crop[16] = {
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800680 sx, 0, 0, 0,
681 0, sy, 0, 0,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800682 0, 0, 1, 0,
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800683 tx, ty, 0, 1,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800684 };
685
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800686 float mtxBeforeFlipV[16];
687 mtxMul(mtxBeforeFlipV, crop, xform);
688
689 // SurfaceFlinger expects the top of its window textures to be at a Y
690 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
691 // want to expose this to applications, however, so we must add an
692 // additional vertical flip to the transform after all the other transforms.
Jamie Genniseadfb672011-06-12 17:03:06 -0700693 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800694}
695
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800696nsecs_t SurfaceTexture::getTimestamp() {
697 LOGV("SurfaceTexture::getTimestamp");
698 Mutex::Autolock lock(mMutex);
699 return mCurrentTimestamp;
700}
701
Jamie Gennis376590d2011-01-13 14:43:36 -0800702void SurfaceTexture::setFrameAvailableListener(
703 const sp<FrameAvailableListener>& l) {
704 LOGV("SurfaceTexture::setFrameAvailableListener");
705 Mutex::Autolock lock(mMutex);
706 mFrameAvailableListener = l;
707}
708
Jamie Gennis83bac212011-02-02 15:31:47 -0800709sp<IBinder> SurfaceTexture::getAllocator() {
710 LOGV("SurfaceTexture::getAllocator");
711 return mGraphicBufferAlloc->asBinder();
712}
713
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800714void SurfaceTexture::freeAllBuffers() {
715 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
716 mSlots[i].mGraphicBuffer = 0;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700717 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800718 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
719 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
720 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
721 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
722 }
723 }
724}
725
726EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
727 const sp<GraphicBuffer>& graphicBuffer) {
728 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
729 EGLint attrs[] = {
730 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
731 EGL_NONE,
732 };
733 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
734 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700735 if (image == EGL_NO_IMAGE_KHR) {
736 EGLint error = eglGetError();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800737 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800738 }
739 return image;
740}
741
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700742sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
743 Mutex::Autolock lock(mMutex);
744 return mCurrentTextureBuf;
745}
746
747Rect SurfaceTexture::getCurrentCrop() const {
748 Mutex::Autolock lock(mMutex);
749 return mCurrentCrop;
750}
751
752uint32_t SurfaceTexture::getCurrentTransform() const {
753 Mutex::Autolock lock(mMutex);
754 return mCurrentTransform;
755}
756
Mathias Agopianed3894c2011-04-20 14:20:59 -0700757int SurfaceTexture::query(int what, int* outValue)
758{
759 Mutex::Autolock lock(mMutex);
760 int value;
761 switch (what) {
762 case NATIVE_WINDOW_WIDTH:
763 value = mDefaultWidth;
764 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
765 value = mCurrentTextureBuf->width;
766 break;
767 case NATIVE_WINDOW_HEIGHT:
768 value = mDefaultHeight;
769 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
770 value = mCurrentTextureBuf->height;
771 break;
772 case NATIVE_WINDOW_FORMAT:
773 value = mPixelFormat;
774 break;
775 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
776 value = mSynchronousMode ?
777 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
778 break;
779 default:
780 return BAD_VALUE;
781 }
782 outValue[0] = value;
783 return NO_ERROR;
784}
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700785
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700786void SurfaceTexture::dump(String8& result) const
787{
788 char buffer[1024];
789 dump(result, "", buffer, 1024);
790}
791
792void SurfaceTexture::dump(String8& result, const char* prefix,
793 char* buffer, size_t SIZE) const
794{
795 Mutex::Autolock _l(mMutex);
796 snprintf(buffer, SIZE,
797 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
798 "mPixelFormat=%d, mTexName=%d\n",
799 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
800 mPixelFormat, mTexName);
801 result.append(buffer);
802
803 String8 fifo;
804 int fifoSize = 0;
805 Fifo::const_iterator i(mQueue.begin());
806 while (i != mQueue.end()) {
807 snprintf(buffer, SIZE, "%02d ", *i++);
808 fifoSize++;
809 fifo.append(buffer);
810 }
811
812 snprintf(buffer, SIZE,
813 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
814 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
815 ,
816 prefix, mCurrentCrop.left,
817 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
818 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
819 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
820 mCurrentTransform, fifoSize, fifo.string()
821 );
822 result.append(buffer);
823
824 struct {
825 const char * operator()(int state) const {
826 switch (state) {
827 case BufferSlot::DEQUEUED: return "DEQUEUED";
828 case BufferSlot::QUEUED: return "QUEUED";
829 case BufferSlot::FREE: return "FREE";
830 default: return "Unknown";
831 }
832 }
833 } stateName;
834
835 for (int i=0 ; i<mBufferCount ; i++) {
836 const BufferSlot& slot(mSlots[i]);
837 snprintf(buffer, SIZE,
838 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennisa2187152011-05-19 13:33:00 -0700839 "timestamp=%lld\n",
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700840 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennisa2187152011-05-19 13:33:00 -0700841 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
842 slot.mTransform, slot.mTimestamp
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700843 );
844 result.append(buffer);
845 }
846}
847
Jamie Gennisb598fb92011-01-09 16:33:17 -0800848static void mtxMul(float out[16], const float a[16], const float b[16]) {
849 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
850 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
851 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
852 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
853
854 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
855 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
856 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
857 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
858
859 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
860 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
861 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
862 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
863
864 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
865 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
866 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
867 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
868}
869
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800870}; // namespace android