blob: adb468cf904618f9f5b0a98c556acb348dfc0b6c [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
37
38namespace android {
39
Jamie Gennisf238e282011-01-09 16:33:17 -080040// Transform matrices
41static float mtxIdentity[16] = {
42 1, 0, 0, 0,
43 0, 1, 0, 0,
44 0, 0, 1, 0,
45 0, 0, 0, 1,
46};
47static float mtxFlipH[16] = {
48 -1, 0, 0, 0,
49 0, 1, 0, 0,
50 0, 0, 1, 0,
51 1, 0, 0, 1,
52};
53static float mtxFlipV[16] = {
54 1, 0, 0, 0,
55 0, -1, 0, 0,
56 0, 0, 1, 0,
57 0, 1, 0, 1,
58};
59static float mtxRot90[16] = {
60 0, 1, 0, 0,
61 -1, 0, 0, 0,
62 0, 0, 1, 0,
63 1, 0, 0, 1,
64};
65static float mtxRot180[16] = {
66 -1, 0, 0, 0,
67 0, -1, 0, 0,
68 0, 0, 1, 0,
69 1, 1, 0, 1,
70};
71static float mtxRot270[16] = {
72 0, -1, 0, 0,
73 1, 0, 0, 0,
74 0, 0, 1, 0,
75 0, 1, 0, 1,
76};
77
78static void mtxMul(float out[16], const float a[16], const float b[16]);
79
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080080SurfaceTexture::SurfaceTexture(GLuint tex) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070081 mDefaultWidth(1),
82 mDefaultHeight(1),
83 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080084 mBufferCount(MIN_BUFFER_SLOTS),
85 mCurrentTexture(INVALID_BUFFER_SLOT),
Mathias Agopian7a042bf2011-04-11 21:19:55 -070086 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080087 mCurrentTransform(0),
88 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080089 mNextTransform(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070090 mTexName(tex),
91 mSynchronousMode(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080092 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080093 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
94 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -070095 mNextCrop.makeInvalid();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080096}
97
98SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -080099 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800100 freeAllBuffers();
101}
102
103status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800104 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700105 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800106
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700107 const int minBufferSlots = mSynchronousMode ?
108 MIN_BUFFER_SLOTS-1 : MIN_BUFFER_SLOTS;
109
110 if (bufferCount < minBufferSlots) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800111 return BAD_VALUE;
112 }
113
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800114 freeAllBuffers();
115 mBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800116 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700117 mQueue.clear();
118 mQueue.reserve(mSynchronousMode ? mBufferCount : 1);
119 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800120 return OK;
121}
122
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700123status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
124{
125 Mutex::Autolock lock(mMutex);
126 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
127 mDefaultWidth = w;
128 mDefaultHeight = h;
129 }
130 return OK;
131}
132
Mathias Agopianc04f1532011-04-25 20:22:14 -0700133sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800134 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135 Mutex::Autolock lock(mMutex);
136 if (buf < 0 || mBufferCount <= buf) {
137 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
138 mBufferCount, buf);
139 return 0;
140 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700141 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700142 return mSlots[buf].mGraphicBuffer;
143}
144
145status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
146 uint32_t format, uint32_t usage) {
147 LOGV("SurfaceTexture::dequeueBuffer");
148
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700149 if ((w && !h) || (!w & h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700150 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
151 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700152 }
153
Mathias Agopianc04f1532011-04-25 20:22:14 -0700154 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700155 int found, foundSync;
156 int dequeuedCount = 0;
157 bool tryAgain = true;
158 while (tryAgain) {
159 found = INVALID_BUFFER_SLOT;
160 foundSync = INVALID_BUFFER_SLOT;
161 dequeuedCount = 0;
162 for (int i = 0; i < mBufferCount; i++) {
163 const int state = mSlots[i].mBufferState;
164 if (state == BufferSlot::DEQUEUED) {
165 dequeuedCount++;
166 }
167 if (state == BufferSlot::FREE || i == mCurrentTexture) {
168 foundSync = i;
169 if (i != mCurrentTexture) {
170 found = i;
171 break;
172 }
173 }
174 }
175 // we're in synchronous mode and didn't find a buffer, we need to wait
176 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
177 if (tryAgain) {
178 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700179 }
180 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700181
182 if (mSynchronousMode) {
183 // we're dequeuing more buffers than allowed in synchronous mode
184 if ((mBufferCount - (dequeuedCount+1)) < MIN_UNDEQUEUED_BUFFERS-1)
185 return -EBUSY;
186
187 if (found == INVALID_BUFFER_SLOT) {
188 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
189 found = foundSync;
190 }
191 }
192
Mathias Agopianc04f1532011-04-25 20:22:14 -0700193 if (found == INVALID_BUFFER_SLOT) {
194 return -EBUSY;
195 }
196
197 const int buf = found;
198 *outBuf = found;
199
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700200 const bool useDefaultSize = !w && !h;
201 if (useDefaultSize) {
202 // use the default size
203 w = mDefaultWidth;
204 h = mDefaultHeight;
205 }
206
207 const bool updateFormat = (format != 0);
208 if (!updateFormat) {
209 // keep the current (or default) format
210 format = mPixelFormat;
211 }
212
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700213 // buffer is now in DEQUEUED (but can also be current at the same time,
214 // if we're in synchronous mode)
215 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
216
217 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700218 if ((buffer == NULL) ||
219 (uint32_t(buffer->width) != w) ||
220 (uint32_t(buffer->height) != h) ||
221 (uint32_t(buffer->format) != format) ||
222 ((uint32_t(buffer->usage) & usage) != usage))
223 {
224 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
225 sp<GraphicBuffer> graphicBuffer(
226 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
227 if (graphicBuffer == 0) {
228 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
229 return NO_MEMORY;
230 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700231 if (updateFormat) {
232 mPixelFormat = format;
233 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800234 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700235 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
237 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
238 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
239 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
240 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700241 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
242 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243 return OK;
244}
245
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700246status_t SurfaceTexture::setSynchronousMode(bool enabled) {
247 Mutex::Autolock lock(mMutex);
248 if (mSynchronousMode != enabled) {
249 mSynchronousMode = enabled;
250 freeAllBuffers();
251 mCurrentTexture = INVALID_BUFFER_SLOT;
252 mQueue.clear();
253 mQueue.reserve(mSynchronousMode ? mBufferCount : 1);
254 mDequeueCondition.signal();
255 }
256 return NO_ERROR;
257}
258
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800259status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800260 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800261 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700262 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800263 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
264 mBufferCount, buf);
265 return -EINVAL;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700266 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
267 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
268 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800269 return -EINVAL;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700270 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800271 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
272 buf);
273 return -EINVAL;
274 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700275
276 if (mSynchronousMode) {
277 // in synchronous mode we queue all buffers in a FIFO
278 mQueue.push_back(buf);
279 } else {
280 // in asynchronous mode we only keep the most recent buffer
281 if (mQueue.empty()) {
282 mQueue.push_back(buf);
283 } else {
284 Fifo::iterator front(mQueue.begin());
285 // buffer currently queued is freed
286 mSlots[*front].mBufferState = BufferSlot::FREE;
287 // and we record the new buffer index in the queued list
288 *front = buf;
289 }
290 }
291
292 mSlots[buf].mBufferState = BufferSlot::QUEUED;
293 mSlots[buf].mLastQueuedCrop = mNextCrop;
294 mSlots[buf].mLastQueuedTransform = mNextTransform;
295 mSlots[buf].mLastQueuedTimestamp = timestamp;
296
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800297 if (mFrameAvailableListener != 0) {
298 mFrameAvailableListener->onFrameAvailable();
299 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700300 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800301 return OK;
302}
303
304void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800305 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800306 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700307 if (buf < 0 || buf >= mBufferCount) {
308 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
309 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800310 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700311 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
312 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
313 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800314 return;
315 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700316 mSlots[buf].mBufferState = BufferSlot::FREE;
317 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800318}
319
Jamie Gennisf238e282011-01-09 16:33:17 -0800320status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800321 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800322 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800323 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800324 return OK;
325}
326
327status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800328 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800329 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800330 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800331 return OK;
332}
333
334status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800335 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800336 Mutex::Autolock lock(mMutex);
337
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700338 int buf = mCurrentTexture;
339 if (!mQueue.empty()) {
340 // in asynchronous mode the list is guaranteed to be one buffer deep,
341 // while in synchronous mode we use the oldest buffer
342 Fifo::iterator front(mQueue.begin());
343 buf = *front;
344 mQueue.erase(front);
345 }
346
347 // Initially both mCurrentTexture and buf are INVALID_BUFFER_SLOT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800348 // so this check will fail until a buffer gets queued.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700349 if (mCurrentTexture != buf) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800350 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700351 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800352 if (image == EGL_NO_IMAGE_KHR) {
353 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700354 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
355 mSlots[buf].mEglImage = image;
356 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700357 if (image == EGL_NO_IMAGE_KHR) {
358 // NOTE: if dpy was invalid, createImage() is guaranteed to
359 // fail. so we'd end up here.
360 return -EINVAL;
361 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800362 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800363
364 GLint error;
365 while ((error = glGetError()) != GL_NO_ERROR) {
366 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
367 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700368
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700369 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700370 if (target != mCurrentTextureTarget) {
371 glDeleteTextures(1, &mTexName);
372 }
373 glBindTexture(target, mTexName);
374 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
375
Jamie Gennis0eb88512011-01-26 11:52:02 -0800376 bool failed = false;
377 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800378 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700379 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800380 failed = true;
381 }
382 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800383 return -EINVAL;
384 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800385
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700386 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
387 // the current buffer becomes FREE if it was still in the queued
388 // state. If it has already been given to the client
389 // (synchronous mode), then it stays in DEQUEUED state.
390 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
391 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
392 }
393
Jamie Gennis9a78c902011-01-12 18:30:40 -0800394 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700395 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700396 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700397 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
398 mCurrentCrop = mSlots[buf].mLastQueuedCrop;
399 mCurrentTransform = mSlots[buf].mLastQueuedTransform;
400 mCurrentTimestamp = mSlots[buf].mLastQueuedTimestamp;
401 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700402 } else {
403 // We always bind the texture even if we don't update its contents.
404 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800405 }
406 return OK;
407}
408
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700409bool SurfaceTexture::isExternalFormat(uint32_t format)
410{
411 switch (format) {
412 // supported YUV formats
413 case HAL_PIXEL_FORMAT_YV12:
414 // Legacy/deprecated YUV formats
415 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
416 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
417 case HAL_PIXEL_FORMAT_YCbCr_422_I:
418 return true;
419 }
420
421 // Any OEM format needs to be considered
422 if (format>=0x100 && format<=0x1FF)
423 return true;
424
425 return false;
426}
427
428GLenum SurfaceTexture::getTextureTarget(uint32_t format)
429{
430 GLenum target = GL_TEXTURE_2D;
431#if defined(GL_OES_EGL_image_external)
432 if (isExternalFormat(format)) {
433 target = GL_TEXTURE_EXTERNAL_OES;
434 }
435#endif
436 return target;
437}
438
439GLenum SurfaceTexture::getCurrentTextureTarget() const {
440 Mutex::Autolock lock(mMutex);
441 return mCurrentTextureTarget;
442}
443
Jamie Gennisf238e282011-01-09 16:33:17 -0800444void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800445 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800446 Mutex::Autolock lock(mMutex);
447
Jamie Gennisa214c642011-01-14 13:53:31 -0800448 float xform[16];
449 for (int i = 0; i < 16; i++) {
450 xform[i] = mtxIdentity[i];
451 }
452 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
453 float result[16];
454 mtxMul(result, xform, mtxFlipH);
455 for (int i = 0; i < 16; i++) {
456 xform[i] = result[i];
457 }
458 }
459 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
460 float result[16];
461 mtxMul(result, xform, mtxFlipV);
462 for (int i = 0; i < 16; i++) {
463 xform[i] = result[i];
464 }
465 }
466 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
467 float result[16];
468 mtxMul(result, xform, mtxRot90);
469 for (int i = 0; i < 16; i++) {
470 xform[i] = result[i];
471 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800472 }
473
474 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800475 float tx, ty, sx, sy;
476 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800477 // In order to prevent bilinear sampling at the of the crop rectangle we
478 // may need to shrink it by 2 texels in each direction. Normally this
479 // would just need to take 1/2 a texel off each end, but because the
480 // chroma channels will likely be subsampled we need to chop off a whole
481 // texel. This will cause artifacts if someone does nearest sampling
482 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
483 // accomodate the bilinear and nearest sampling uses.
484 //
485 // If nearest sampling turns out to be a desirable usage of these
486 // textures then we could add the ability to switch a SurfaceTexture to
487 // nearest-mode. Preferably, however, the image producers (video
488 // decoder, camera, etc.) would simply not use a crop rectangle (or at
489 // least not tell the framework about it) so that the GPU can do the
490 // correct edge behavior.
491 int xshrink = 0, yshrink = 0;
492 if (mCurrentCrop.left > 0) {
493 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
494 xshrink++;
495 } else {
496 tx = 0.0f;
497 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700498 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800499 xshrink++;
500 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700501 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800502 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
503 float(buf->getHeight());
504 yshrink++;
505 } else {
506 ty = 0.0f;
507 }
508 if (mCurrentCrop.top > 0) {
509 yshrink++;
510 }
511 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
512 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800513 } else {
514 tx = 0.0f;
515 ty = 0.0f;
516 sx = 1.0f;
517 sy = 1.0f;
518 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800519 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800520 sx, 0, 0, 0,
521 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800522 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800523 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800524 };
525
Jamie Gennisa214c642011-01-14 13:53:31 -0800526 float mtxBeforeFlipV[16];
527 mtxMul(mtxBeforeFlipV, crop, xform);
528
529 // SurfaceFlinger expects the top of its window textures to be at a Y
530 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
531 // want to expose this to applications, however, so we must add an
532 // additional vertical flip to the transform after all the other transforms.
533 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800534}
535
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800536nsecs_t SurfaceTexture::getTimestamp() {
537 LOGV("SurfaceTexture::getTimestamp");
538 Mutex::Autolock lock(mMutex);
539 return mCurrentTimestamp;
540}
541
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800542void SurfaceTexture::setFrameAvailableListener(
543 const sp<FrameAvailableListener>& l) {
544 LOGV("SurfaceTexture::setFrameAvailableListener");
545 Mutex::Autolock lock(mMutex);
546 mFrameAvailableListener = l;
547}
548
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800549sp<IBinder> SurfaceTexture::getAllocator() {
550 LOGV("SurfaceTexture::getAllocator");
551 return mGraphicBufferAlloc->asBinder();
552}
553
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800554void SurfaceTexture::freeAllBuffers() {
555 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
556 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700557 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800558 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
559 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
560 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
561 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
562 }
563 }
564}
565
566EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
567 const sp<GraphicBuffer>& graphicBuffer) {
568 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
569 EGLint attrs[] = {
570 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
571 EGL_NONE,
572 };
573 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
574 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700575 if (image == EGL_NO_IMAGE_KHR) {
576 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800577 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800578 }
579 return image;
580}
581
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700582sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
583 Mutex::Autolock lock(mMutex);
584 return mCurrentTextureBuf;
585}
586
587Rect SurfaceTexture::getCurrentCrop() const {
588 Mutex::Autolock lock(mMutex);
589 return mCurrentCrop;
590}
591
592uint32_t SurfaceTexture::getCurrentTransform() const {
593 Mutex::Autolock lock(mMutex);
594 return mCurrentTransform;
595}
596
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700597int SurfaceTexture::query(int what, int* outValue)
598{
599 Mutex::Autolock lock(mMutex);
600 int value;
601 switch (what) {
602 case NATIVE_WINDOW_WIDTH:
603 value = mDefaultWidth;
604 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
605 value = mCurrentTextureBuf->width;
606 break;
607 case NATIVE_WINDOW_HEIGHT:
608 value = mDefaultHeight;
609 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
610 value = mCurrentTextureBuf->height;
611 break;
612 case NATIVE_WINDOW_FORMAT:
613 value = mPixelFormat;
614 break;
615 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
616 value = mSynchronousMode ?
617 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
618 break;
619 default:
620 return BAD_VALUE;
621 }
622 outValue[0] = value;
623 return NO_ERROR;
624}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700625
Jamie Gennisf238e282011-01-09 16:33:17 -0800626static void mtxMul(float out[16], const float a[16], const float b[16]) {
627 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
628 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
629 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
630 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
631
632 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
633 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
634 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
635 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
636
637 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
638 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
639 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
640 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
641
642 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
643 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
644 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
645 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
646}
647
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800648}; // namespace android