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), |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 84 | mBufferCount(MIN_ASYNC_BUFFER_SLOTS), |
| 85 | mClientBufferCount(0), |
| 86 | mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 87 | mCurrentTexture(INVALID_BUFFER_SLOT), |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 88 | mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 89 | mCurrentTransform(0), |
| 90 | mCurrentTimestamp(0), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 91 | mNextTransform(0), |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 92 | mTexName(tex), |
| 93 | mSynchronousMode(false) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 94 | LOGV("SurfaceTexture::SurfaceTexture"); |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 95 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 96 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
Mathias Agopian | cc57d6f | 2011-04-27 18:57:33 -0700 | [diff] [blame] | 97 | mNextCrop.makeInvalid(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 101 | LOGV("SurfaceTexture::~SurfaceTexture"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 102 | freeAllBuffers(); |
| 103 | } |
| 104 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 105 | status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) { |
| 106 | if (bufferCount > NUM_BUFFER_SLOTS) |
| 107 | return BAD_VALUE; |
| 108 | |
| 109 | // special-case, nothing to do |
| 110 | if (bufferCount == mBufferCount) |
| 111 | return OK; |
| 112 | |
| 113 | if (!mClientBufferCount && |
| 114 | bufferCount >= mBufferCount) { |
| 115 | // easy, we just have more buffers |
| 116 | mBufferCount = bufferCount; |
| 117 | mServerBufferCount = bufferCount; |
| 118 | mDequeueCondition.signal(); |
| 119 | } else { |
| 120 | // we're here because we're either |
| 121 | // - reducing the number of available buffers |
| 122 | // - or there is a client-buffer-count in effect |
| 123 | |
| 124 | // less than 2 buffers is never allowed |
| 125 | if (bufferCount < 2) |
| 126 | return BAD_VALUE; |
| 127 | |
| 128 | // when there is non client-buffer-count in effect, the client is not |
| 129 | // allowed to dequeue more than one buffer at a time, |
| 130 | // so the next time they dequeue a buffer, we know that they don't |
| 131 | // own one. the actual resizing will happen during the next |
| 132 | // dequeueBuffer. |
| 133 | |
| 134 | mServerBufferCount = bufferCount; |
| 135 | } |
| 136 | return OK; |
| 137 | } |
| 138 | |
| 139 | status_t SurfaceTexture::setBufferCountServer(int bufferCount) { |
| 140 | Mutex::Autolock lock(mMutex); |
| 141 | return setBufferCountServerLocked(bufferCount); |
| 142 | } |
| 143 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 144 | status_t SurfaceTexture::setBufferCount(int bufferCount) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 145 | LOGV("SurfaceTexture::setBufferCount"); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 146 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 147 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 148 | // Error out if the user has dequeued buffers |
| 149 | for (int i=0 ; i<mBufferCount ; i++) { |
| 150 | if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) { |
| 151 | LOGE("setBufferCount: client owns some buffers"); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 155 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 156 | if (bufferCount == 0) { |
| 157 | const int minBufferSlots = mSynchronousMode ? |
| 158 | MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS; |
| 159 | mClientBufferCount = 0; |
| 160 | bufferCount = (mServerBufferCount >= minBufferSlots) ? |
| 161 | mServerBufferCount : minBufferSlots; |
| 162 | return setBufferCountServerLocked(bufferCount); |
| 163 | } |
| 164 | |
| 165 | // We don't allow the client to set a buffer-count less than |
| 166 | // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it. |
| 167 | if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) { |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 168 | return BAD_VALUE; |
| 169 | } |
| 170 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 171 | // here we're guaranteed that the client doesn't have dequeued buffers |
| 172 | // and will release all of its buffer references. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 173 | freeAllBuffers(); |
| 174 | mBufferCount = bufferCount; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 175 | mClientBufferCount = bufferCount; |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 176 | mCurrentTexture = INVALID_BUFFER_SLOT; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 177 | mQueue.clear(); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 178 | mDequeueCondition.signal(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 179 | return OK; |
| 180 | } |
| 181 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 182 | status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 183 | { |
| 184 | Mutex::Autolock lock(mMutex); |
| 185 | if ((w != mDefaultWidth) || (h != mDefaultHeight)) { |
| 186 | mDefaultWidth = w; |
| 187 | mDefaultHeight = h; |
| 188 | } |
| 189 | return OK; |
| 190 | } |
| 191 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 192 | sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 193 | LOGV("SurfaceTexture::requestBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 194 | Mutex::Autolock lock(mMutex); |
| 195 | if (buf < 0 || mBufferCount <= buf) { |
| 196 | LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
| 197 | mBufferCount, buf); |
| 198 | return 0; |
| 199 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 200 | mSlots[buf].mRequestBufferCalled = true; |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 201 | return mSlots[buf].mGraphicBuffer; |
| 202 | } |
| 203 | |
| 204 | status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h, |
| 205 | uint32_t format, uint32_t usage) { |
| 206 | LOGV("SurfaceTexture::dequeueBuffer"); |
| 207 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 208 | if ((w && !h) || (!w & h)) { |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 209 | LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h); |
| 210 | return BAD_VALUE; |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 213 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 214 | |
| 215 | status_t returnFlags(OK); |
| 216 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 217 | int found, foundSync; |
| 218 | int dequeuedCount = 0; |
| 219 | bool tryAgain = true; |
| 220 | while (tryAgain) { |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 221 | // We need to wait for the FIFO to drain if the number of buffer |
| 222 | // needs to change. |
| 223 | // |
| 224 | // The condition "number of buffer needs to change" is true if |
| 225 | // - the client doesn't care about how many buffers there are |
| 226 | // - AND the actual number of buffer is different from what was |
| 227 | // set in the last setBufferCountServer() |
| 228 | // - OR - |
| 229 | // setBufferCountServer() was set to a value incompatible with |
| 230 | // the synchronization mode (for instance because the sync mode |
| 231 | // changed since) |
| 232 | // |
| 233 | // As long as this condition is true AND the FIFO is not empty, we |
| 234 | // wait on mDequeueCondition. |
| 235 | |
| 236 | int minBufferCountNeeded = mSynchronousMode ? |
| 237 | MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS; |
| 238 | |
| 239 | if (!mClientBufferCount && |
| 240 | ((mServerBufferCount != mBufferCount) || |
| 241 | (mServerBufferCount < minBufferCountNeeded))) { |
| 242 | // wait for the FIFO to drain |
| 243 | while (!mQueue.isEmpty()) { |
| 244 | mDequeueCondition.wait(mMutex); |
| 245 | } |
| 246 | minBufferCountNeeded = mSynchronousMode ? |
| 247 | MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | if (!mClientBufferCount && |
| 252 | ((mServerBufferCount != mBufferCount) || |
| 253 | (mServerBufferCount < minBufferCountNeeded))) { |
| 254 | // here we're guaranteed that mQueue is empty |
| 255 | freeAllBuffers(); |
| 256 | mBufferCount = mServerBufferCount; |
| 257 | if (mBufferCount < minBufferCountNeeded) |
| 258 | mBufferCount = minBufferCountNeeded; |
| 259 | mCurrentTexture = INVALID_BUFFER_SLOT; |
| 260 | returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS; |
| 261 | } |
| 262 | |
| 263 | // look for a free buffer to give to the client |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 264 | found = INVALID_BUFFER_SLOT; |
| 265 | foundSync = INVALID_BUFFER_SLOT; |
| 266 | dequeuedCount = 0; |
| 267 | for (int i = 0; i < mBufferCount; i++) { |
| 268 | const int state = mSlots[i].mBufferState; |
| 269 | if (state == BufferSlot::DEQUEUED) { |
| 270 | dequeuedCount++; |
| 271 | } |
| 272 | if (state == BufferSlot::FREE || i == mCurrentTexture) { |
| 273 | foundSync = i; |
| 274 | if (i != mCurrentTexture) { |
| 275 | found = i; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | } |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 280 | |
| 281 | // clients are not allowed to dequeue more than one buffer |
| 282 | // if they didn't set a buffer count. |
| 283 | if (!mClientBufferCount && dequeuedCount) { |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
| 287 | // make sure the client is not trying to dequeue more buffers |
| 288 | // than allowed. |
| 289 | const int avail = mBufferCount - (dequeuedCount+1); |
| 290 | if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) { |
| 291 | LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)", |
| 292 | MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode), |
| 293 | dequeuedCount); |
| 294 | return -EBUSY; |
| 295 | } |
| 296 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 297 | // we're in synchronous mode and didn't find a buffer, we need to wait |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 298 | // for for some buffers to be consumed |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 299 | tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT); |
| 300 | if (tryAgain) { |
| 301 | mDequeueCondition.wait(mMutex); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 304 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 305 | if (mSynchronousMode && found == INVALID_BUFFER_SLOT) { |
| 306 | // foundSync guaranteed to be != INVALID_BUFFER_SLOT |
| 307 | found = foundSync; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 310 | if (found == INVALID_BUFFER_SLOT) { |
| 311 | return -EBUSY; |
| 312 | } |
| 313 | |
| 314 | const int buf = found; |
| 315 | *outBuf = found; |
| 316 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 317 | const bool useDefaultSize = !w && !h; |
| 318 | if (useDefaultSize) { |
| 319 | // use the default size |
| 320 | w = mDefaultWidth; |
| 321 | h = mDefaultHeight; |
| 322 | } |
| 323 | |
| 324 | const bool updateFormat = (format != 0); |
| 325 | if (!updateFormat) { |
| 326 | // keep the current (or default) format |
| 327 | format = mPixelFormat; |
| 328 | } |
| 329 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 330 | // buffer is now in DEQUEUED (but can also be current at the same time, |
| 331 | // if we're in synchronous mode) |
| 332 | mSlots[buf].mBufferState = BufferSlot::DEQUEUED; |
| 333 | |
| 334 | const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 335 | if ((buffer == NULL) || |
| 336 | (uint32_t(buffer->width) != w) || |
| 337 | (uint32_t(buffer->height) != h) || |
| 338 | (uint32_t(buffer->format) != format) || |
| 339 | ((uint32_t(buffer->usage) & usage) != usage)) |
| 340 | { |
| 341 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 342 | sp<GraphicBuffer> graphicBuffer( |
| 343 | mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage)); |
| 344 | if (graphicBuffer == 0) { |
| 345 | LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed"); |
| 346 | return NO_MEMORY; |
| 347 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 348 | if (updateFormat) { |
| 349 | mPixelFormat = format; |
| 350 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 351 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 352 | mSlots[buf].mRequestBufferCalled = false; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 353 | if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { |
| 354 | eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage); |
| 355 | mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; |
| 356 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 357 | } |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 358 | returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION; |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 359 | } |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 360 | return returnFlags; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 363 | status_t SurfaceTexture::setSynchronousMode(bool enabled) { |
| 364 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 365 | |
| 366 | status_t err = OK; |
| 367 | if (!enabled) { |
| 368 | // going to asynchronous mode, drain the queue |
| 369 | while (mSynchronousMode != enabled && !mQueue.isEmpty()) { |
| 370 | mDequeueCondition.wait(mMutex); |
| 371 | } |
| 372 | } |
| 373 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 374 | if (mSynchronousMode != enabled) { |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 375 | // - if we're going to asynchronous mode, the queue is guaranteed to be |
| 376 | // empty here |
| 377 | // - if the client set the number of buffers, we're guaranteed that |
| 378 | // we have at least 3 (because we don't allow less) |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 379 | mSynchronousMode = enabled; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 380 | mDequeueCondition.signal(); |
| 381 | } |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 382 | return err; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 385 | status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 386 | LOGV("SurfaceTexture::queueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 387 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 388 | if (buf < 0 || buf >= mBufferCount) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 389 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 390 | mBufferCount, buf); |
| 391 | return -EINVAL; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 392 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 393 | LOGE("queueBuffer: slot %d is not owned by the client (state=%d)", |
| 394 | buf, mSlots[buf].mBufferState); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 395 | return -EINVAL; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 396 | } else if (buf == mCurrentTexture) { |
| 397 | LOGE("queueBuffer: slot %d is current!", buf); |
| 398 | return -EINVAL; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 399 | } else if (!mSlots[buf].mRequestBufferCalled) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 400 | LOGE("queueBuffer: slot %d was enqueued without requesting a buffer", |
| 401 | buf); |
| 402 | return -EINVAL; |
| 403 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 404 | |
| 405 | if (mSynchronousMode) { |
| 406 | // in synchronous mode we queue all buffers in a FIFO |
| 407 | mQueue.push_back(buf); |
| 408 | } else { |
| 409 | // in asynchronous mode we only keep the most recent buffer |
| 410 | if (mQueue.empty()) { |
| 411 | mQueue.push_back(buf); |
| 412 | } else { |
| 413 | Fifo::iterator front(mQueue.begin()); |
| 414 | // buffer currently queued is freed |
| 415 | mSlots[*front].mBufferState = BufferSlot::FREE; |
| 416 | // and we record the new buffer index in the queued list |
| 417 | *front = buf; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | mSlots[buf].mBufferState = BufferSlot::QUEUED; |
| 422 | mSlots[buf].mLastQueuedCrop = mNextCrop; |
| 423 | mSlots[buf].mLastQueuedTransform = mNextTransform; |
| 424 | mSlots[buf].mLastQueuedTimestamp = timestamp; |
| 425 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 426 | if (mFrameAvailableListener != 0) { |
| 427 | mFrameAvailableListener->onFrameAvailable(); |
| 428 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 429 | mDequeueCondition.signal(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 430 | return OK; |
| 431 | } |
| 432 | |
| 433 | void SurfaceTexture::cancelBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 434 | LOGV("SurfaceTexture::cancelBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 435 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 436 | if (buf < 0 || buf >= mBufferCount) { |
| 437 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", |
| 438 | mBufferCount, buf); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 439 | return; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 440 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 441 | LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)", |
| 442 | buf, mSlots[buf].mBufferState); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 443 | return; |
| 444 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 445 | mSlots[buf].mBufferState = BufferSlot::FREE; |
| 446 | mDequeueCondition.signal(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 449 | status_t SurfaceTexture::setCrop(const Rect& crop) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 450 | LOGV("SurfaceTexture::setCrop"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 451 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 452 | mNextCrop = crop; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 453 | return OK; |
| 454 | } |
| 455 | |
| 456 | status_t SurfaceTexture::setTransform(uint32_t transform) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 457 | LOGV("SurfaceTexture::setTransform"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 458 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 459 | mNextTransform = transform; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 460 | return OK; |
| 461 | } |
| 462 | |
| 463 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 464 | LOGV("SurfaceTexture::updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 465 | Mutex::Autolock lock(mMutex); |
| 466 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 467 | int buf = mCurrentTexture; |
| 468 | if (!mQueue.empty()) { |
| 469 | // in asynchronous mode the list is guaranteed to be one buffer deep, |
| 470 | // while in synchronous mode we use the oldest buffer |
| 471 | Fifo::iterator front(mQueue.begin()); |
| 472 | buf = *front; |
| 473 | mQueue.erase(front); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 474 | if (mQueue.isEmpty()) { |
| 475 | mDequeueCondition.signal(); |
| 476 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // Initially both mCurrentTexture and buf are INVALID_BUFFER_SLOT, |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 480 | // so this check will fail until a buffer gets queued. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 481 | if (mCurrentTexture != buf) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 482 | // Update the GL texture object. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 483 | EGLImageKHR image = mSlots[buf].mEglImage; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 484 | if (image == EGL_NO_IMAGE_KHR) { |
| 485 | EGLDisplay dpy = eglGetCurrentDisplay(); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 486 | image = createImage(dpy, mSlots[buf].mGraphicBuffer); |
| 487 | mSlots[buf].mEglImage = image; |
| 488 | mSlots[buf].mEglDisplay = dpy; |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 489 | if (image == EGL_NO_IMAGE_KHR) { |
| 490 | // NOTE: if dpy was invalid, createImage() is guaranteed to |
| 491 | // fail. so we'd end up here. |
| 492 | return -EINVAL; |
| 493 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 494 | } |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 495 | |
| 496 | GLint error; |
| 497 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 498 | LOGE("GL error cleared before updating SurfaceTexture: %#04x", error); |
| 499 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 500 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 501 | GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 502 | if (target != mCurrentTextureTarget) { |
| 503 | glDeleteTextures(1, &mTexName); |
| 504 | } |
| 505 | glBindTexture(target, mTexName); |
| 506 | glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image); |
| 507 | |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 508 | bool failed = false; |
| 509 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 510 | LOGE("error binding external texture image %p (slot %d): %#04x", |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 511 | image, buf, error); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 512 | failed = true; |
| 513 | } |
| 514 | if (failed) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 515 | return -EINVAL; |
| 516 | } |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 517 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 518 | if (mCurrentTexture != INVALID_BUFFER_SLOT) { |
| 519 | // the current buffer becomes FREE if it was still in the queued |
| 520 | // state. If it has already been given to the client |
| 521 | // (synchronous mode), then it stays in DEQUEUED state. |
| 522 | if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) |
| 523 | mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE; |
| 524 | } |
| 525 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 526 | // Update the SurfaceTexture state. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 527 | mCurrentTexture = buf; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 528 | mCurrentTextureTarget = target; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 529 | mCurrentTextureBuf = mSlots[buf].mGraphicBuffer; |
| 530 | mCurrentCrop = mSlots[buf].mLastQueuedCrop; |
| 531 | mCurrentTransform = mSlots[buf].mLastQueuedTransform; |
| 532 | mCurrentTimestamp = mSlots[buf].mLastQueuedTimestamp; |
| 533 | mDequeueCondition.signal(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 534 | } else { |
| 535 | // We always bind the texture even if we don't update its contents. |
| 536 | glBindTexture(mCurrentTextureTarget, mTexName); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 537 | } |
| 538 | return OK; |
| 539 | } |
| 540 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 541 | bool SurfaceTexture::isExternalFormat(uint32_t format) |
| 542 | { |
| 543 | switch (format) { |
| 544 | // supported YUV formats |
| 545 | case HAL_PIXEL_FORMAT_YV12: |
| 546 | // Legacy/deprecated YUV formats |
| 547 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 548 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 549 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | // Any OEM format needs to be considered |
| 554 | if (format>=0x100 && format<=0x1FF) |
| 555 | return true; |
| 556 | |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | GLenum SurfaceTexture::getTextureTarget(uint32_t format) |
| 561 | { |
| 562 | GLenum target = GL_TEXTURE_2D; |
| 563 | #if defined(GL_OES_EGL_image_external) |
| 564 | if (isExternalFormat(format)) { |
| 565 | target = GL_TEXTURE_EXTERNAL_OES; |
| 566 | } |
| 567 | #endif |
| 568 | return target; |
| 569 | } |
| 570 | |
| 571 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
| 572 | Mutex::Autolock lock(mMutex); |
| 573 | return mCurrentTextureTarget; |
| 574 | } |
| 575 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 576 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 577 | LOGV("SurfaceTexture::getTransformMatrix"); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 578 | Mutex::Autolock lock(mMutex); |
| 579 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 580 | float xform[16]; |
| 581 | for (int i = 0; i < 16; i++) { |
| 582 | xform[i] = mtxIdentity[i]; |
| 583 | } |
| 584 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 585 | float result[16]; |
| 586 | mtxMul(result, xform, mtxFlipH); |
| 587 | for (int i = 0; i < 16; i++) { |
| 588 | xform[i] = result[i]; |
| 589 | } |
| 590 | } |
| 591 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 592 | float result[16]; |
| 593 | mtxMul(result, xform, mtxFlipV); |
| 594 | for (int i = 0; i < 16; i++) { |
| 595 | xform[i] = result[i]; |
| 596 | } |
| 597 | } |
| 598 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 599 | float result[16]; |
| 600 | mtxMul(result, xform, mtxRot90); |
| 601 | for (int i = 0; i < 16; i++) { |
| 602 | xform[i] = result[i]; |
| 603 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 607 | float tx, ty, sx, sy; |
| 608 | if (!mCurrentCrop.isEmpty()) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 609 | // In order to prevent bilinear sampling at the of the crop rectangle we |
| 610 | // may need to shrink it by 2 texels in each direction. Normally this |
| 611 | // would just need to take 1/2 a texel off each end, but because the |
| 612 | // chroma channels will likely be subsampled we need to chop off a whole |
| 613 | // texel. This will cause artifacts if someone does nearest sampling |
| 614 | // with 1:1 pixel:texel ratio, but it's impossible to simultaneously |
| 615 | // accomodate the bilinear and nearest sampling uses. |
| 616 | // |
| 617 | // If nearest sampling turns out to be a desirable usage of these |
| 618 | // textures then we could add the ability to switch a SurfaceTexture to |
| 619 | // nearest-mode. Preferably, however, the image producers (video |
| 620 | // decoder, camera, etc.) would simply not use a crop rectangle (or at |
| 621 | // least not tell the framework about it) so that the GPU can do the |
| 622 | // correct edge behavior. |
| 623 | int xshrink = 0, yshrink = 0; |
| 624 | if (mCurrentCrop.left > 0) { |
| 625 | tx = float(mCurrentCrop.left + 1) / float(buf->getWidth()); |
| 626 | xshrink++; |
| 627 | } else { |
| 628 | tx = 0.0f; |
| 629 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 630 | if (mCurrentCrop.right < int32_t(buf->getWidth())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 631 | xshrink++; |
| 632 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 633 | if (mCurrentCrop.bottom < int32_t(buf->getHeight())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 634 | ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) / |
| 635 | float(buf->getHeight()); |
| 636 | yshrink++; |
| 637 | } else { |
| 638 | ty = 0.0f; |
| 639 | } |
| 640 | if (mCurrentCrop.top > 0) { |
| 641 | yshrink++; |
| 642 | } |
| 643 | sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth()); |
| 644 | sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight()); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 645 | } else { |
| 646 | tx = 0.0f; |
| 647 | ty = 0.0f; |
| 648 | sx = 1.0f; |
| 649 | sy = 1.0f; |
| 650 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 651 | float crop[16] = { |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 652 | sx, 0, 0, 0, |
| 653 | 0, sy, 0, 0, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 654 | 0, 0, 1, 0, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 655 | tx, ty, 0, 1, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 656 | }; |
| 657 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 658 | float mtxBeforeFlipV[16]; |
| 659 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 660 | |
| 661 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 662 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 663 | // want to expose this to applications, however, so we must add an |
| 664 | // additional vertical flip to the transform after all the other transforms. |
| 665 | mtxMul(mtx, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 668 | nsecs_t SurfaceTexture::getTimestamp() { |
| 669 | LOGV("SurfaceTexture::getTimestamp"); |
| 670 | Mutex::Autolock lock(mMutex); |
| 671 | return mCurrentTimestamp; |
| 672 | } |
| 673 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 674 | void SurfaceTexture::setFrameAvailableListener( |
| 675 | const sp<FrameAvailableListener>& l) { |
| 676 | LOGV("SurfaceTexture::setFrameAvailableListener"); |
| 677 | Mutex::Autolock lock(mMutex); |
| 678 | mFrameAvailableListener = l; |
| 679 | } |
| 680 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 681 | sp<IBinder> SurfaceTexture::getAllocator() { |
| 682 | LOGV("SurfaceTexture::getAllocator"); |
| 683 | return mGraphicBufferAlloc->asBinder(); |
| 684 | } |
| 685 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 686 | void SurfaceTexture::freeAllBuffers() { |
| 687 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 688 | mSlots[i].mGraphicBuffer = 0; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 689 | mSlots[i].mBufferState = BufferSlot::FREE; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 690 | if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { |
| 691 | eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage); |
| 692 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 693 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 699 | const sp<GraphicBuffer>& graphicBuffer) { |
| 700 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 701 | EGLint attrs[] = { |
| 702 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 703 | EGL_NONE, |
| 704 | }; |
| 705 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 706 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 707 | if (image == EGL_NO_IMAGE_KHR) { |
| 708 | EGLint error = eglGetError(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 709 | LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 710 | } |
| 711 | return image; |
| 712 | } |
| 713 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 714 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 715 | Mutex::Autolock lock(mMutex); |
| 716 | return mCurrentTextureBuf; |
| 717 | } |
| 718 | |
| 719 | Rect SurfaceTexture::getCurrentCrop() const { |
| 720 | Mutex::Autolock lock(mMutex); |
| 721 | return mCurrentCrop; |
| 722 | } |
| 723 | |
| 724 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 725 | Mutex::Autolock lock(mMutex); |
| 726 | return mCurrentTransform; |
| 727 | } |
| 728 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 729 | int SurfaceTexture::query(int what, int* outValue) |
| 730 | { |
| 731 | Mutex::Autolock lock(mMutex); |
| 732 | int value; |
| 733 | switch (what) { |
| 734 | case NATIVE_WINDOW_WIDTH: |
| 735 | value = mDefaultWidth; |
| 736 | if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0) |
| 737 | value = mCurrentTextureBuf->width; |
| 738 | break; |
| 739 | case NATIVE_WINDOW_HEIGHT: |
| 740 | value = mDefaultHeight; |
| 741 | if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0) |
| 742 | value = mCurrentTextureBuf->height; |
| 743 | break; |
| 744 | case NATIVE_WINDOW_FORMAT: |
| 745 | value = mPixelFormat; |
| 746 | break; |
| 747 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
| 748 | value = mSynchronousMode ? |
| 749 | (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS; |
| 750 | break; |
| 751 | default: |
| 752 | return BAD_VALUE; |
| 753 | } |
| 754 | outValue[0] = value; |
| 755 | return NO_ERROR; |
| 756 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 757 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 758 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 759 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 760 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 761 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 762 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 763 | |
| 764 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 765 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 766 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 767 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 768 | |
| 769 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 770 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 771 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 772 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 773 | |
| 774 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 775 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 776 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 777 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 778 | } |
| 779 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 780 | }; // namespace android |