blob: 8c07d24012f182fd8940491cd88a1a25dae24f88 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038
39namespace android {
40
Jamie Gennisf238e282011-01-09 16:33:17 -080041// Transform matrices
42static float mtxIdentity[16] = {
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1,
47};
48static float mtxFlipH[16] = {
49 -1, 0, 0, 0,
50 0, 1, 0, 0,
51 0, 0, 1, 0,
52 1, 0, 0, 1,
53};
54static float mtxFlipV[16] = {
55 1, 0, 0, 0,
56 0, -1, 0, 0,
57 0, 0, 1, 0,
58 0, 1, 0, 1,
59};
60static float mtxRot90[16] = {
61 0, 1, 0, 0,
62 -1, 0, 0, 0,
63 0, 0, 1, 0,
64 1, 0, 0, 1,
65};
66static float mtxRot180[16] = {
67 -1, 0, 0, 0,
68 0, -1, 0, 0,
69 0, 0, 1, 0,
70 1, 1, 0, 1,
71};
72static float mtxRot270[16] = {
73 0, -1, 0, 0,
74 1, 0, 0, 0,
75 0, 0, 1, 0,
76 0, 1, 0, 1,
77};
78
79static void mtxMul(float out[16], const float a[16], const float b[16]);
80
Grace Kloba14a0e582011-06-23 21:21:47 -070081SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070082 mDefaultWidth(1),
83 mDefaultHeight(1),
84 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -070085 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86 mClientBufferCount(0),
87 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080088 mCurrentTexture(INVALID_BUFFER_SLOT),
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 Agopian7734ebf2011-07-13 15:24:42 -070093 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070094 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -070095 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070096 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -070097 mConnectedApi(NO_CONNECTED_API),
98 mAbandoned(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080099 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800100 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
101 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700102 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700103 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104}
105
106SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800107 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800108 freeAllBuffers();
109}
110
Mathias Agopian80727112011-05-02 19:51:12 -0700111status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
112 if (bufferCount > NUM_BUFFER_SLOTS)
113 return BAD_VALUE;
114
115 // special-case, nothing to do
116 if (bufferCount == mBufferCount)
117 return OK;
118
119 if (!mClientBufferCount &&
120 bufferCount >= mBufferCount) {
121 // easy, we just have more buffers
122 mBufferCount = bufferCount;
123 mServerBufferCount = bufferCount;
124 mDequeueCondition.signal();
125 } else {
126 // we're here because we're either
127 // - reducing the number of available buffers
128 // - or there is a client-buffer-count in effect
129
130 // less than 2 buffers is never allowed
131 if (bufferCount < 2)
132 return BAD_VALUE;
133
134 // when there is non client-buffer-count in effect, the client is not
135 // allowed to dequeue more than one buffer at a time,
136 // so the next time they dequeue a buffer, we know that they don't
137 // own one. the actual resizing will happen during the next
138 // dequeueBuffer.
139
140 mServerBufferCount = bufferCount;
141 }
142 return OK;
143}
144
145status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
146 Mutex::Autolock lock(mMutex);
147 return setBufferCountServerLocked(bufferCount);
148}
149
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800150status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800151 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700152 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800153
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700154 if (mAbandoned) {
155 LOGE("setBufferCount: SurfaceTexture has been abandoned!");
156 return NO_INIT;
157 }
158
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700159 if (bufferCount > NUM_BUFFER_SLOTS) {
160 LOGE("setBufferCount: bufferCount larger than slots available");
161 return BAD_VALUE;
162 }
163
Mathias Agopian80727112011-05-02 19:51:12 -0700164 // Error out if the user has dequeued buffers
165 for (int i=0 ; i<mBufferCount ; i++) {
166 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
167 LOGE("setBufferCount: client owns some buffers");
168 return -EINVAL;
169 }
170 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700171
Mathias Agopian80727112011-05-02 19:51:12 -0700172 if (bufferCount == 0) {
173 const int minBufferSlots = mSynchronousMode ?
174 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
175 mClientBufferCount = 0;
176 bufferCount = (mServerBufferCount >= minBufferSlots) ?
177 mServerBufferCount : minBufferSlots;
178 return setBufferCountServerLocked(bufferCount);
179 }
180
181 // We don't allow the client to set a buffer-count less than
182 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
183 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800184 return BAD_VALUE;
185 }
186
Mathias Agopian80727112011-05-02 19:51:12 -0700187 // here we're guaranteed that the client doesn't have dequeued buffers
188 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189 freeAllBuffers();
190 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700191 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800192 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700193 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700194 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 return OK;
196}
197
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700198status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
199{
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700200 if (!w || !h) {
201 LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", w, h);
202 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700203 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700204
205 Mutex::Autolock lock(mMutex);
206 mDefaultWidth = w;
207 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700208 return OK;
209}
210
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700211status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800212 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700214 if (mAbandoned) {
215 LOGE("requestBuffer: SurfaceTexture has been abandoned!");
216 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800217 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700218 if (slot < 0 || mBufferCount <= slot) {
219 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
220 mBufferCount, slot);
221 return BAD_VALUE;
222 }
223 mSlots[slot].mRequestBufferCalled = true;
224 *buf = mSlots[slot].mGraphicBuffer;
225 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700226}
227
228status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
229 uint32_t format, uint32_t usage) {
230 LOGV("SurfaceTexture::dequeueBuffer");
231
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700232 if (mAbandoned) {
233 LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
234 return NO_INIT;
235 }
236
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700237 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700238 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
239 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700240 }
241
Mathias Agopianc04f1532011-04-25 20:22:14 -0700242 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700243
244 status_t returnFlags(OK);
245
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700246 int found, foundSync;
247 int dequeuedCount = 0;
248 bool tryAgain = true;
249 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700250 // We need to wait for the FIFO to drain if the number of buffer
251 // needs to change.
252 //
253 // The condition "number of buffer needs to change" is true if
254 // - the client doesn't care about how many buffers there are
255 // - AND the actual number of buffer is different from what was
256 // set in the last setBufferCountServer()
257 // - OR -
258 // setBufferCountServer() was set to a value incompatible with
259 // the synchronization mode (for instance because the sync mode
260 // changed since)
261 //
262 // As long as this condition is true AND the FIFO is not empty, we
263 // wait on mDequeueCondition.
264
265 int minBufferCountNeeded = mSynchronousMode ?
266 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
267
268 if (!mClientBufferCount &&
269 ((mServerBufferCount != mBufferCount) ||
270 (mServerBufferCount < minBufferCountNeeded))) {
271 // wait for the FIFO to drain
272 while (!mQueue.isEmpty()) {
273 mDequeueCondition.wait(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700274 if (mAbandoned) {
275 LOGE("dequeueBuffer: SurfaceTexture was abandoned while "
276 "blocked!");
277 return NO_INIT;
278 }
Mathias Agopian80727112011-05-02 19:51:12 -0700279 }
280 minBufferCountNeeded = mSynchronousMode ?
281 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
282 }
283
284
285 if (!mClientBufferCount &&
286 ((mServerBufferCount != mBufferCount) ||
287 (mServerBufferCount < minBufferCountNeeded))) {
288 // here we're guaranteed that mQueue is empty
289 freeAllBuffers();
290 mBufferCount = mServerBufferCount;
291 if (mBufferCount < minBufferCountNeeded)
292 mBufferCount = minBufferCountNeeded;
293 mCurrentTexture = INVALID_BUFFER_SLOT;
294 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
295 }
296
297 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700298 found = INVALID_BUFFER_SLOT;
299 foundSync = INVALID_BUFFER_SLOT;
300 dequeuedCount = 0;
301 for (int i = 0; i < mBufferCount; i++) {
302 const int state = mSlots[i].mBufferState;
303 if (state == BufferSlot::DEQUEUED) {
304 dequeuedCount++;
305 }
Mathias Agopiane1220792011-05-04 18:28:07 -0700306 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700307 foundSync = i;
308 if (i != mCurrentTexture) {
309 found = i;
310 break;
311 }
312 }
313 }
Mathias Agopian80727112011-05-02 19:51:12 -0700314
315 // clients are not allowed to dequeue more than one buffer
316 // if they didn't set a buffer count.
317 if (!mClientBufferCount && dequeuedCount) {
318 return -EINVAL;
319 }
320
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700321 // See whether a buffer has been queued since the last setBufferCount so
322 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
323 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
324 if (bufferHasBeenQueued) {
325 // make sure the client is not trying to dequeue more buffers
326 // than allowed.
327 const int avail = mBufferCount - (dequeuedCount+1);
328 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
329 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
330 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
331 dequeuedCount);
332 return -EBUSY;
333 }
Mathias Agopian80727112011-05-02 19:51:12 -0700334 }
335
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700336 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700337 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700338 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
339 if (tryAgain) {
340 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700341 }
342 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700343
Mathias Agopian80727112011-05-02 19:51:12 -0700344 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
345 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
346 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700347 }
348
Mathias Agopianc04f1532011-04-25 20:22:14 -0700349 if (found == INVALID_BUFFER_SLOT) {
350 return -EBUSY;
351 }
352
353 const int buf = found;
354 *outBuf = found;
355
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700356 const bool useDefaultSize = !w && !h;
357 if (useDefaultSize) {
358 // use the default size
359 w = mDefaultWidth;
360 h = mDefaultHeight;
361 }
362
363 const bool updateFormat = (format != 0);
364 if (!updateFormat) {
365 // keep the current (or default) format
366 format = mPixelFormat;
367 }
368
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700369 // buffer is now in DEQUEUED (but can also be current at the same time,
370 // if we're in synchronous mode)
371 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
372
373 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700374 if ((buffer == NULL) ||
375 (uint32_t(buffer->width) != w) ||
376 (uint32_t(buffer->height) != h) ||
377 (uint32_t(buffer->format) != format) ||
378 ((uint32_t(buffer->usage) & usage) != usage))
379 {
380 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700381 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700382 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700383 mGraphicBufferAlloc->createGraphicBuffer(
384 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700385 if (graphicBuffer == 0) {
386 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700387 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700388 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700389 if (updateFormat) {
390 mPixelFormat = format;
391 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800392 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700393 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800394 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
395 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
396 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
397 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
398 }
Mathias Agopian80727112011-05-02 19:51:12 -0700399 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700400 }
Mathias Agopian80727112011-05-02 19:51:12 -0700401 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800402}
403
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700404status_t SurfaceTexture::setSynchronousMode(bool enabled) {
405 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700406
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700407 if (mAbandoned) {
408 LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
409 return NO_INIT;
410 }
411
Mathias Agopian80727112011-05-02 19:51:12 -0700412 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700413 if (!mAllowSynchronousMode && enabled)
414 return err;
415
Mathias Agopian80727112011-05-02 19:51:12 -0700416 if (!enabled) {
417 // going to asynchronous mode, drain the queue
418 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
419 mDequeueCondition.wait(mMutex);
420 }
421 }
422
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700423 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700424 // - if we're going to asynchronous mode, the queue is guaranteed to be
425 // empty here
426 // - if the client set the number of buffers, we're guaranteed that
427 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700428 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700429 mDequeueCondition.signal();
430 }
Mathias Agopian80727112011-05-02 19:51:12 -0700431 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700432}
433
Mathias Agopian97c602c2011-07-19 15:24:46 -0700434status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
435 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800436 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700437
438 sp<FrameAvailableListener> listener;
439
440 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700441 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700442 if (mAbandoned) {
443 LOGE("queueBuffer: SurfaceTexture has been abandoned!");
444 return NO_INIT;
445 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700446 if (buf < 0 || buf >= mBufferCount) {
447 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
448 mBufferCount, buf);
449 return -EINVAL;
450 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
451 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
452 buf, mSlots[buf].mBufferState);
453 return -EINVAL;
454 } else if (buf == mCurrentTexture) {
455 LOGE("queueBuffer: slot %d is current!", buf);
456 return -EINVAL;
457 } else if (!mSlots[buf].mRequestBufferCalled) {
458 LOGE("queueBuffer: slot %d was enqueued without requesting a "
459 "buffer", buf);
460 return -EINVAL;
461 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700462
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700463 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700464 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700465 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700466
467 // Synchronous mode always signals that an additional frame should
468 // be consumed.
469 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700470 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700471 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700472 if (mQueue.empty()) {
473 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700474
475 // Asynchronous mode only signals that a frame should be
476 // consumed if no previous frame was pending. If a frame were
477 // pending then the consumer would have already been notified.
478 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700479 } else {
480 Fifo::iterator front(mQueue.begin());
481 // buffer currently queued is freed
482 mSlots[*front].mBufferState = BufferSlot::FREE;
483 // and we record the new buffer index in the queued list
484 *front = buf;
485 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700486 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700487
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700488 mSlots[buf].mBufferState = BufferSlot::QUEUED;
489 mSlots[buf].mCrop = mNextCrop;
490 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700491 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700492 mSlots[buf].mTimestamp = timestamp;
493 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700494 } // scope for the lock
495
496 // call back without lock held
497 if (listener != 0) {
498 listener->onFrameAvailable();
499 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700500
501 *outWidth = mDefaultWidth;
502 *outHeight = mDefaultHeight;
503 *outTransform = 0;
504
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800505 return OK;
506}
507
508void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800509 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800510 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700511
512 if (mAbandoned) {
513 LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
514 return;
515 }
516
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700517 if (buf < 0 || buf >= mBufferCount) {
518 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
519 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800520 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700521 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
522 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
523 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800524 return;
525 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700526 mSlots[buf].mBufferState = BufferSlot::FREE;
527 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800528}
529
Jamie Gennisf238e282011-01-09 16:33:17 -0800530status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800531 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800532 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700533 if (mAbandoned) {
534 LOGE("setCrop: SurfaceTexture has been abandoned!");
535 return NO_INIT;
536 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800537 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800538 return OK;
539}
540
541status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800542 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800543 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700544 if (mAbandoned) {
545 LOGE("setTransform: SurfaceTexture has been abandoned!");
546 return NO_INIT;
547 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800548 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800549 return OK;
550}
551
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700552status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700553 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700554 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700555
556 if (mAbandoned) {
557 LOGE("connect: SurfaceTexture has been abandoned!");
558 return NO_INIT;
559 }
560
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700561 int err = NO_ERROR;
562 switch (api) {
563 case NATIVE_WINDOW_API_EGL:
564 case NATIVE_WINDOW_API_CPU:
565 case NATIVE_WINDOW_API_MEDIA:
566 case NATIVE_WINDOW_API_CAMERA:
567 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700568 LOGE("connect: already connected (cur=%d, req=%d)",
569 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700570 err = -EINVAL;
571 } else {
572 mConnectedApi = api;
573 }
574 break;
575 default:
576 err = -EINVAL;
577 break;
578 }
579 return err;
580}
581
582status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700583 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700584 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700585
586 if (mAbandoned) {
587 LOGE("connect: SurfaceTexture has been abandoned!");
588 return NO_INIT;
589 }
590
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700591 int err = NO_ERROR;
592 switch (api) {
593 case NATIVE_WINDOW_API_EGL:
594 case NATIVE_WINDOW_API_CPU:
595 case NATIVE_WINDOW_API_MEDIA:
596 case NATIVE_WINDOW_API_CAMERA:
597 if (mConnectedApi == api) {
598 mConnectedApi = NO_CONNECTED_API;
599 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700600 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
601 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700602 err = -EINVAL;
603 }
604 break;
605 default:
606 err = -EINVAL;
607 break;
608 }
609 return err;
610}
611
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700612status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700613 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700614
615 switch (mode) {
616 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
617 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
618 break;
619 default:
620 return BAD_VALUE;
621 }
622
623 Mutex::Autolock lock(mMutex);
624 mNextScalingMode = mode;
625 return OK;
626}
627
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800628status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800629 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800630 Mutex::Autolock lock(mMutex);
631
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700632 // In asynchronous mode the list is guaranteed to be one buffer
633 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700634 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700635 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700636 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700637
Jamie Gennisf238e282011-01-09 16:33:17 -0800638 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700639 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800640 if (image == EGL_NO_IMAGE_KHR) {
641 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700642 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
643 mSlots[buf].mEglImage = image;
644 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700645 if (image == EGL_NO_IMAGE_KHR) {
646 // NOTE: if dpy was invalid, createImage() is guaranteed to
647 // fail. so we'd end up here.
648 return -EINVAL;
649 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800650 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800651
652 GLint error;
653 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700654 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800655 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700656
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700657 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700658 if (target != mCurrentTextureTarget) {
659 glDeleteTextures(1, &mTexName);
660 }
661 glBindTexture(target, mTexName);
662 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
663
Jamie Gennis0eb88512011-01-26 11:52:02 -0800664 bool failed = false;
665 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800666 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700667 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800668 failed = true;
669 }
670 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800671 return -EINVAL;
672 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800673
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700674 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700675 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700676 // state. If it has already been given to the client
677 // (synchronous mode), then it stays in DEQUEUED state.
678 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
679 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
680 }
681
Jamie Gennis9a78c902011-01-12 18:30:40 -0800682 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700683 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700684 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700685 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700686 mCurrentCrop = mSlots[buf].mCrop;
687 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700688 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700689 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700690 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700691
692 // Now that we've passed the point at which failures can happen,
693 // it's safe to remove the buffer from the front of the queue.
694 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700695 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700696 } else {
697 // We always bind the texture even if we don't update its contents.
698 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800699 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700700
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800701 return OK;
702}
703
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700704bool SurfaceTexture::isExternalFormat(uint32_t format)
705{
706 switch (format) {
707 // supported YUV formats
708 case HAL_PIXEL_FORMAT_YV12:
709 // Legacy/deprecated YUV formats
710 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
711 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
712 case HAL_PIXEL_FORMAT_YCbCr_422_I:
713 return true;
714 }
715
716 // Any OEM format needs to be considered
717 if (format>=0x100 && format<=0x1FF)
718 return true;
719
720 return false;
721}
722
723GLenum SurfaceTexture::getTextureTarget(uint32_t format)
724{
725 GLenum target = GL_TEXTURE_2D;
726#if defined(GL_OES_EGL_image_external)
727 if (isExternalFormat(format)) {
728 target = GL_TEXTURE_EXTERNAL_OES;
729 }
730#endif
731 return target;
732}
733
734GLenum SurfaceTexture::getCurrentTextureTarget() const {
735 Mutex::Autolock lock(mMutex);
736 return mCurrentTextureTarget;
737}
738
Jamie Gennisf238e282011-01-09 16:33:17 -0800739void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800740 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700741 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
742}
743
744void SurfaceTexture::computeCurrentTransformMatrix() {
745 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800746
Jamie Gennisa214c642011-01-14 13:53:31 -0800747 float xform[16];
748 for (int i = 0; i < 16; i++) {
749 xform[i] = mtxIdentity[i];
750 }
751 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
752 float result[16];
753 mtxMul(result, xform, mtxFlipH);
754 for (int i = 0; i < 16; i++) {
755 xform[i] = result[i];
756 }
757 }
758 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
759 float result[16];
760 mtxMul(result, xform, mtxFlipV);
761 for (int i = 0; i < 16; i++) {
762 xform[i] = result[i];
763 }
764 }
765 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
766 float result[16];
767 mtxMul(result, xform, mtxRot90);
768 for (int i = 0; i < 16; i++) {
769 xform[i] = result[i];
770 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800771 }
772
773 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800774 float tx, ty, sx, sy;
775 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800776 // In order to prevent bilinear sampling at the of the crop rectangle we
777 // may need to shrink it by 2 texels in each direction. Normally this
778 // would just need to take 1/2 a texel off each end, but because the
779 // chroma channels will likely be subsampled we need to chop off a whole
780 // texel. This will cause artifacts if someone does nearest sampling
781 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
782 // accomodate the bilinear and nearest sampling uses.
783 //
784 // If nearest sampling turns out to be a desirable usage of these
785 // textures then we could add the ability to switch a SurfaceTexture to
786 // nearest-mode. Preferably, however, the image producers (video
787 // decoder, camera, etc.) would simply not use a crop rectangle (or at
788 // least not tell the framework about it) so that the GPU can do the
789 // correct edge behavior.
790 int xshrink = 0, yshrink = 0;
791 if (mCurrentCrop.left > 0) {
792 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
793 xshrink++;
794 } else {
795 tx = 0.0f;
796 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700797 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800798 xshrink++;
799 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700800 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800801 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
802 float(buf->getHeight());
803 yshrink++;
804 } else {
805 ty = 0.0f;
806 }
807 if (mCurrentCrop.top > 0) {
808 yshrink++;
809 }
810 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
811 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800812 } else {
813 tx = 0.0f;
814 ty = 0.0f;
815 sx = 1.0f;
816 sy = 1.0f;
817 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800818 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800819 sx, 0, 0, 0,
820 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800821 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800822 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800823 };
824
Jamie Gennisa214c642011-01-14 13:53:31 -0800825 float mtxBeforeFlipV[16];
826 mtxMul(mtxBeforeFlipV, crop, xform);
827
828 // SurfaceFlinger expects the top of its window textures to be at a Y
829 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
830 // want to expose this to applications, however, so we must add an
831 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700832 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800833}
834
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800835nsecs_t SurfaceTexture::getTimestamp() {
836 LOGV("SurfaceTexture::getTimestamp");
837 Mutex::Autolock lock(mMutex);
838 return mCurrentTimestamp;
839}
840
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800841void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700842 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800843 LOGV("SurfaceTexture::setFrameAvailableListener");
844 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700845 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800846}
847
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800848void SurfaceTexture::freeAllBuffers() {
849 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
850 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700851 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800852 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
853 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
854 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
855 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
856 }
857 }
858}
859
860EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
861 const sp<GraphicBuffer>& graphicBuffer) {
862 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
863 EGLint attrs[] = {
864 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
865 EGL_NONE,
866 };
867 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
868 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700869 if (image == EGL_NO_IMAGE_KHR) {
870 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800871 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800872 }
873 return image;
874}
875
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700876sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
877 Mutex::Autolock lock(mMutex);
878 return mCurrentTextureBuf;
879}
880
881Rect SurfaceTexture::getCurrentCrop() const {
882 Mutex::Autolock lock(mMutex);
883 return mCurrentCrop;
884}
885
886uint32_t SurfaceTexture::getCurrentTransform() const {
887 Mutex::Autolock lock(mMutex);
888 return mCurrentTransform;
889}
890
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700891uint32_t SurfaceTexture::getCurrentScalingMode() const {
892 Mutex::Autolock lock(mMutex);
893 return mCurrentScalingMode;
894}
895
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700896int SurfaceTexture::query(int what, int* outValue)
897{
898 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700899
900 if (mAbandoned) {
901 LOGE("query: SurfaceTexture has been abandoned!");
902 return NO_INIT;
903 }
904
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700905 int value;
906 switch (what) {
907 case NATIVE_WINDOW_WIDTH:
908 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700909 break;
910 case NATIVE_WINDOW_HEIGHT:
911 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700912 break;
913 case NATIVE_WINDOW_FORMAT:
914 value = mPixelFormat;
915 break;
916 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
917 value = mSynchronousMode ?
918 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
919 break;
920 default:
921 return BAD_VALUE;
922 }
923 outValue[0] = value;
924 return NO_ERROR;
925}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700926
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700927void SurfaceTexture::abandon() {
928 Mutex::Autolock lock(mMutex);
929 freeAllBuffers();
930 mAbandoned = true;
931 mDequeueCondition.signal();
932}
933
Mathias Agopian68c77942011-05-09 19:08:33 -0700934void SurfaceTexture::dump(String8& result) const
935{
936 char buffer[1024];
937 dump(result, "", buffer, 1024);
938}
939
940void SurfaceTexture::dump(String8& result, const char* prefix,
941 char* buffer, size_t SIZE) const
942{
943 Mutex::Autolock _l(mMutex);
944 snprintf(buffer, SIZE,
945 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
946 "mPixelFormat=%d, mTexName=%d\n",
947 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
948 mPixelFormat, mTexName);
949 result.append(buffer);
950
951 String8 fifo;
952 int fifoSize = 0;
953 Fifo::const_iterator i(mQueue.begin());
954 while (i != mQueue.end()) {
955 snprintf(buffer, SIZE, "%02d ", *i++);
956 fifoSize++;
957 fifo.append(buffer);
958 }
959
960 snprintf(buffer, SIZE,
961 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
962 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
963 ,
964 prefix, mCurrentCrop.left,
965 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
966 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
967 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
968 mCurrentTransform, fifoSize, fifo.string()
969 );
970 result.append(buffer);
971
972 struct {
973 const char * operator()(int state) const {
974 switch (state) {
975 case BufferSlot::DEQUEUED: return "DEQUEUED";
976 case BufferSlot::QUEUED: return "QUEUED";
977 case BufferSlot::FREE: return "FREE";
978 default: return "Unknown";
979 }
980 }
981 } stateName;
982
983 for (int i=0 ; i<mBufferCount ; i++) {
984 const BufferSlot& slot(mSlots[i]);
985 snprintf(buffer, SIZE,
986 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700987 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700988 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700989 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
990 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700991 );
992 result.append(buffer);
993 }
994}
995
Jamie Gennisf238e282011-01-09 16:33:17 -0800996static void mtxMul(float out[16], const float a[16], const float b[16]) {
997 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
998 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
999 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1000 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1001
1002 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1003 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1004 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1005 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1006
1007 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1008 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1009 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1010 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1011
1012 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1013 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1014 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1015 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1016}
1017
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001018}; // namespace android