Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 19 | |
| 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 Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 30 | #include <hardware/hardware.h> |
| 31 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/ISurfaceComposer.h> |
| 33 | #include <surfaceflinger/SurfaceComposerClient.h> |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 34 | #include <surfaceflinger/IGraphicBufferAlloc.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 35 | |
| 36 | #include <utils/Log.h> |
| 37 | |
| 38 | namespace android { |
| 39 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 40 | // Transform matrices |
| 41 | static float mtxIdentity[16] = { |
| 42 | 1, 0, 0, 0, |
| 43 | 0, 1, 0, 0, |
| 44 | 0, 0, 1, 0, |
| 45 | 0, 0, 0, 1, |
| 46 | }; |
| 47 | static float mtxFlipH[16] = { |
| 48 | -1, 0, 0, 0, |
| 49 | 0, 1, 0, 0, |
| 50 | 0, 0, 1, 0, |
| 51 | 1, 0, 0, 1, |
| 52 | }; |
| 53 | static float mtxFlipV[16] = { |
| 54 | 1, 0, 0, 0, |
| 55 | 0, -1, 0, 0, |
| 56 | 0, 0, 1, 0, |
| 57 | 0, 1, 0, 1, |
| 58 | }; |
| 59 | static float mtxRot90[16] = { |
| 60 | 0, 1, 0, 0, |
| 61 | -1, 0, 0, 0, |
| 62 | 0, 0, 1, 0, |
| 63 | 1, 0, 0, 1, |
| 64 | }; |
| 65 | static float mtxRot180[16] = { |
| 66 | -1, 0, 0, 0, |
| 67 | 0, -1, 0, 0, |
| 68 | 0, 0, 1, 0, |
| 69 | 1, 1, 0, 1, |
| 70 | }; |
| 71 | static 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 | |
| 78 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 79 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 80 | SurfaceTexture::SurfaceTexture(GLuint tex) : |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 81 | mDefaultWidth(1), |
| 82 | mDefaultHeight(1), |
| 83 | mPixelFormat(PIXEL_FORMAT_RGBA_8888), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 84 | mBufferCount(MIN_BUFFER_SLOTS), |
| 85 | mCurrentTexture(INVALID_BUFFER_SLOT), |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 86 | mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 87 | mCurrentTransform(0), |
| 88 | mCurrentTimestamp(0), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 89 | mNextTransform(0), |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 90 | mTexName(tex), |
| 91 | mSynchronousMode(false) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 92 | LOGV("SurfaceTexture::SurfaceTexture"); |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 93 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 94 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
Mathias Agopian | cc57d6f | 2011-04-27 18:57:33 -0700 | [diff] [blame] | 95 | mNextCrop.makeInvalid(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 99 | LOGV("SurfaceTexture::~SurfaceTexture"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 100 | freeAllBuffers(); |
| 101 | } |
| 102 | |
| 103 | status_t SurfaceTexture::setBufferCount(int bufferCount) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 104 | LOGV("SurfaceTexture::setBufferCount"); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 105 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 106 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 107 | const int minBufferSlots = mSynchronousMode ? |
| 108 | MIN_BUFFER_SLOTS-1 : MIN_BUFFER_SLOTS; |
| 109 | |
| 110 | if (bufferCount < minBufferSlots) { |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 111 | return BAD_VALUE; |
| 112 | } |
| 113 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 114 | freeAllBuffers(); |
| 115 | mBufferCount = bufferCount; |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 116 | mCurrentTexture = INVALID_BUFFER_SLOT; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 117 | mQueue.clear(); |
| 118 | mQueue.reserve(mSynchronousMode ? mBufferCount : 1); |
| 119 | mDequeueCondition.signal(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 120 | return OK; |
| 121 | } |
| 122 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 123 | status_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 Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 133 | sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 134 | LOGV("SurfaceTexture::requestBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 135 | 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 Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 141 | mSlots[buf].mRequestBufferCalled = true; |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 142 | return mSlots[buf].mGraphicBuffer; |
| 143 | } |
| 144 | |
| 145 | status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h, |
| 146 | uint32_t format, uint32_t usage) { |
| 147 | LOGV("SurfaceTexture::dequeueBuffer"); |
| 148 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 149 | if ((w && !h) || (!w & h)) { |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 150 | LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h); |
| 151 | return BAD_VALUE; |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 154 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 155 | 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 Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 181 | |
| 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 Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 193 | if (found == INVALID_BUFFER_SLOT) { |
| 194 | return -EBUSY; |
| 195 | } |
| 196 | |
| 197 | const int buf = found; |
| 198 | *outBuf = found; |
| 199 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 200 | 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 Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 213 | // 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 Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 218 | 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 Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 231 | if (updateFormat) { |
| 232 | mPixelFormat = format; |
| 233 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 234 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 235 | mSlots[buf].mRequestBufferCalled = false; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 236 | 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 Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 241 | return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION; |
| 242 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 243 | return OK; |
| 244 | } |
| 245 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 246 | status_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 Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 259 | status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 260 | LOGV("SurfaceTexture::queueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 261 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 262 | if (buf < 0 || buf >= mBufferCount) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 263 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 264 | mBufferCount, buf); |
| 265 | return -EINVAL; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 266 | } 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 269 | return -EINVAL; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 270 | } else if (!mSlots[buf].mRequestBufferCalled) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 271 | LOGE("queueBuffer: slot %d was enqueued without requesting a buffer", |
| 272 | buf); |
| 273 | return -EINVAL; |
| 274 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 275 | |
| 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 Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 297 | if (mFrameAvailableListener != 0) { |
| 298 | mFrameAvailableListener->onFrameAvailable(); |
| 299 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 300 | mDequeueCondition.signal(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 301 | return OK; |
| 302 | } |
| 303 | |
| 304 | void SurfaceTexture::cancelBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 305 | LOGV("SurfaceTexture::cancelBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 306 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 307 | if (buf < 0 || buf >= mBufferCount) { |
| 308 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", |
| 309 | mBufferCount, buf); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 310 | return; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 311 | } 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 314 | return; |
| 315 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 316 | mSlots[buf].mBufferState = BufferSlot::FREE; |
| 317 | mDequeueCondition.signal(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 320 | status_t SurfaceTexture::setCrop(const Rect& crop) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 321 | LOGV("SurfaceTexture::setCrop"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 322 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 323 | mNextCrop = crop; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 324 | return OK; |
| 325 | } |
| 326 | |
| 327 | status_t SurfaceTexture::setTransform(uint32_t transform) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 328 | LOGV("SurfaceTexture::setTransform"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 329 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 330 | mNextTransform = transform; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 331 | return OK; |
| 332 | } |
| 333 | |
| 334 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 335 | LOGV("SurfaceTexture::updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 336 | Mutex::Autolock lock(mMutex); |
| 337 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 338 | 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 348 | // so this check will fail until a buffer gets queued. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 349 | if (mCurrentTexture != buf) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 350 | // Update the GL texture object. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 351 | EGLImageKHR image = mSlots[buf].mEglImage; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 352 | if (image == EGL_NO_IMAGE_KHR) { |
| 353 | EGLDisplay dpy = eglGetCurrentDisplay(); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 354 | image = createImage(dpy, mSlots[buf].mGraphicBuffer); |
| 355 | mSlots[buf].mEglImage = image; |
| 356 | mSlots[buf].mEglDisplay = dpy; |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 357 | 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 362 | } |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 363 | |
| 364 | GLint error; |
| 365 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 366 | LOGE("GL error cleared before updating SurfaceTexture: %#04x", error); |
| 367 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 368 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 369 | GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 370 | if (target != mCurrentTextureTarget) { |
| 371 | glDeleteTextures(1, &mTexName); |
| 372 | } |
| 373 | glBindTexture(target, mTexName); |
| 374 | glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image); |
| 375 | |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 376 | bool failed = false; |
| 377 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 378 | LOGE("error binding external texture image %p (slot %d): %#04x", |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 379 | image, buf, error); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 380 | failed = true; |
| 381 | } |
| 382 | if (failed) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 383 | return -EINVAL; |
| 384 | } |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 385 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 386 | 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 Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 394 | // Update the SurfaceTexture state. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 395 | mCurrentTexture = buf; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 396 | mCurrentTextureTarget = target; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 397 | mCurrentTextureBuf = mSlots[buf].mGraphicBuffer; |
| 398 | mCurrentCrop = mSlots[buf].mLastQueuedCrop; |
| 399 | mCurrentTransform = mSlots[buf].mLastQueuedTransform; |
| 400 | mCurrentTimestamp = mSlots[buf].mLastQueuedTimestamp; |
| 401 | mDequeueCondition.signal(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 402 | } else { |
| 403 | // We always bind the texture even if we don't update its contents. |
| 404 | glBindTexture(mCurrentTextureTarget, mTexName); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 405 | } |
| 406 | return OK; |
| 407 | } |
| 408 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 409 | bool 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 | |
| 428 | GLenum 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 | |
| 439 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
| 440 | Mutex::Autolock lock(mMutex); |
| 441 | return mCurrentTextureTarget; |
| 442 | } |
| 443 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 444 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 445 | LOGV("SurfaceTexture::getTransformMatrix"); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 446 | Mutex::Autolock lock(mMutex); |
| 447 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 448 | 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 Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 475 | float tx, ty, sx, sy; |
| 476 | if (!mCurrentCrop.isEmpty()) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 477 | // 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 Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 498 | if (mCurrentCrop.right < int32_t(buf->getWidth())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 499 | xshrink++; |
| 500 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 501 | if (mCurrentCrop.bottom < int32_t(buf->getHeight())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 502 | 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 Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 513 | } else { |
| 514 | tx = 0.0f; |
| 515 | ty = 0.0f; |
| 516 | sx = 1.0f; |
| 517 | sy = 1.0f; |
| 518 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 519 | float crop[16] = { |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 520 | sx, 0, 0, 0, |
| 521 | 0, sy, 0, 0, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 522 | 0, 0, 1, 0, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 523 | tx, ty, 0, 1, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 524 | }; |
| 525 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 526 | 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 Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 536 | nsecs_t SurfaceTexture::getTimestamp() { |
| 537 | LOGV("SurfaceTexture::getTimestamp"); |
| 538 | Mutex::Autolock lock(mMutex); |
| 539 | return mCurrentTimestamp; |
| 540 | } |
| 541 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 542 | void SurfaceTexture::setFrameAvailableListener( |
| 543 | const sp<FrameAvailableListener>& l) { |
| 544 | LOGV("SurfaceTexture::setFrameAvailableListener"); |
| 545 | Mutex::Autolock lock(mMutex); |
| 546 | mFrameAvailableListener = l; |
| 547 | } |
| 548 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 549 | sp<IBinder> SurfaceTexture::getAllocator() { |
| 550 | LOGV("SurfaceTexture::getAllocator"); |
| 551 | return mGraphicBufferAlloc->asBinder(); |
| 552 | } |
| 553 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 554 | void SurfaceTexture::freeAllBuffers() { |
| 555 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 556 | mSlots[i].mGraphicBuffer = 0; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 557 | mSlots[i].mBufferState = BufferSlot::FREE; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 558 | 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 | |
| 566 | EGLImageKHR 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 Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 575 | if (image == EGL_NO_IMAGE_KHR) { |
| 576 | EGLint error = eglGetError(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 577 | LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 578 | } |
| 579 | return image; |
| 580 | } |
| 581 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 582 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 583 | Mutex::Autolock lock(mMutex); |
| 584 | return mCurrentTextureBuf; |
| 585 | } |
| 586 | |
| 587 | Rect SurfaceTexture::getCurrentCrop() const { |
| 588 | Mutex::Autolock lock(mMutex); |
| 589 | return mCurrentCrop; |
| 590 | } |
| 591 | |
| 592 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 593 | Mutex::Autolock lock(mMutex); |
| 594 | return mCurrentTransform; |
| 595 | } |
| 596 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 597 | int 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 Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 625 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 626 | static 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 648 | }; // namespace android |