Jamie Gennis | 68e4a7a | 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 | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 68e4a7a | 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 | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 30 | #include <hardware/hardware.h> |
| 31 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/ISurfaceComposer.h> |
| 33 | #include <surfaceflinger/SurfaceComposerClient.h> |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 34 | #include <surfaceflinger/IGraphicBufferAlloc.h> |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 35 | |
| 36 | #include <utils/Log.h> |
Mathias Agopian | 7f3289c | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 37 | #include <utils/String8.h> |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 41 | // Transform matrices |
| 42 | static float mtxIdentity[16] = { |
| 43 | 1, 0, 0, 0, |
| 44 | 0, 1, 0, 0, |
| 45 | 0, 0, 1, 0, |
| 46 | 0, 0, 0, 1, |
| 47 | }; |
| 48 | static float mtxFlipH[16] = { |
| 49 | -1, 0, 0, 0, |
| 50 | 0, 1, 0, 0, |
| 51 | 0, 0, 1, 0, |
| 52 | 1, 0, 0, 1, |
| 53 | }; |
| 54 | static float mtxFlipV[16] = { |
| 55 | 1, 0, 0, 0, |
| 56 | 0, -1, 0, 0, |
| 57 | 0, 0, 1, 0, |
| 58 | 0, 1, 0, 1, |
| 59 | }; |
| 60 | static float mtxRot90[16] = { |
| 61 | 0, 1, 0, 0, |
| 62 | -1, 0, 0, 0, |
| 63 | 0, 0, 1, 0, |
| 64 | 1, 0, 0, 1, |
| 65 | }; |
| 66 | static float mtxRot180[16] = { |
| 67 | -1, 0, 0, 0, |
| 68 | 0, -1, 0, 0, |
| 69 | 0, 0, 1, 0, |
| 70 | 1, 1, 0, 1, |
| 71 | }; |
| 72 | static float mtxRot270[16] = { |
| 73 | 0, -1, 0, 0, |
| 74 | 1, 0, 0, 0, |
| 75 | 0, 0, 1, 0, |
| 76 | 0, 1, 0, 1, |
| 77 | }; |
| 78 | |
| 79 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 80 | |
Grace Kloba | 0904d0a | 2011-06-23 21:21:47 -0700 | [diff] [blame] | 81 | SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) : |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 82 | mDefaultWidth(1), |
| 83 | mDefaultHeight(1), |
| 84 | mPixelFormat(PIXEL_FORMAT_RGBA_8888), |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 85 | mBufferCount(MIN_ASYNC_BUFFER_SLOTS), |
| 86 | mClientBufferCount(0), |
| 87 | mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS), |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 88 | mCurrentTexture(INVALID_BUFFER_SLOT), |
| 89 | mCurrentTransform(0), |
| 90 | mCurrentTimestamp(0), |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 91 | mNextTransform(0), |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 92 | mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 93 | mTexName(tex), |
Grace Kloba | 0904d0a | 2011-06-23 21:21:47 -0700 | [diff] [blame] | 94 | mSynchronousMode(false), |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 95 | mAllowSynchronousMode(allowSynchronousMode), |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 96 | mConnectedApi(NO_CONNECTED_API), |
| 97 | mAbandoned(false) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 98 | LOGV("SurfaceTexture::SurfaceTexture"); |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 99 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 100 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
Mathias Agopian | 926340c | 2011-04-27 18:57:33 -0700 | [diff] [blame] | 101 | mNextCrop.makeInvalid(); |
Jamie Gennis | eadfb67 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 102 | memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix)); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 106 | LOGV("SurfaceTexture::~SurfaceTexture"); |
Mathias Agopian | a04cda9 | 2011-08-10 15:28:58 -0700 | [diff] [blame] | 107 | freeAllBuffersLocked(); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 110 | status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) { |
| 111 | if (bufferCount > NUM_BUFFER_SLOTS) |
| 112 | return BAD_VALUE; |
| 113 | |
| 114 | // special-case, nothing to do |
| 115 | if (bufferCount == mBufferCount) |
| 116 | return OK; |
| 117 | |
| 118 | if (!mClientBufferCount && |
| 119 | bufferCount >= mBufferCount) { |
| 120 | // easy, we just have more buffers |
| 121 | mBufferCount = bufferCount; |
| 122 | mServerBufferCount = bufferCount; |
| 123 | mDequeueCondition.signal(); |
| 124 | } else { |
| 125 | // we're here because we're either |
| 126 | // - reducing the number of available buffers |
| 127 | // - or there is a client-buffer-count in effect |
| 128 | |
| 129 | // less than 2 buffers is never allowed |
| 130 | if (bufferCount < 2) |
| 131 | return BAD_VALUE; |
| 132 | |
| 133 | // when there is non client-buffer-count in effect, the client is not |
| 134 | // allowed to dequeue more than one buffer at a time, |
| 135 | // so the next time they dequeue a buffer, we know that they don't |
| 136 | // own one. the actual resizing will happen during the next |
| 137 | // dequeueBuffer. |
| 138 | |
| 139 | mServerBufferCount = bufferCount; |
| 140 | } |
| 141 | return OK; |
| 142 | } |
| 143 | |
| 144 | status_t SurfaceTexture::setBufferCountServer(int bufferCount) { |
| 145 | Mutex::Autolock lock(mMutex); |
| 146 | return setBufferCountServerLocked(bufferCount); |
| 147 | } |
| 148 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 149 | status_t SurfaceTexture::setBufferCount(int bufferCount) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 150 | LOGV("SurfaceTexture::setBufferCount"); |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 151 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 96dcc97 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 152 | |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 153 | if (mAbandoned) { |
| 154 | LOGE("setBufferCount: SurfaceTexture has been abandoned!"); |
| 155 | return NO_INIT; |
| 156 | } |
Pannag Sanketi | c9ec69e | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 157 | if (bufferCount > NUM_BUFFER_SLOTS) { |
| 158 | LOGE("setBufferCount: bufferCount larger than slots available"); |
| 159 | return BAD_VALUE; |
| 160 | } |
| 161 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 162 | // Error out if the user has dequeued buffers |
| 163 | for (int i=0 ; i<mBufferCount ; i++) { |
| 164 | if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) { |
| 165 | LOGE("setBufferCount: client owns some buffers"); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 169 | |
Jamie Gennis | d995b08 | 2011-07-30 16:00:11 -0700 | [diff] [blame] | 170 | const int minBufferSlots = mSynchronousMode ? |
| 171 | MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS; |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 172 | if (bufferCount == 0) { |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 173 | mClientBufferCount = 0; |
| 174 | bufferCount = (mServerBufferCount >= minBufferSlots) ? |
| 175 | mServerBufferCount : minBufferSlots; |
| 176 | return setBufferCountServerLocked(bufferCount); |
| 177 | } |
| 178 | |
Jamie Gennis | d995b08 | 2011-07-30 16:00:11 -0700 | [diff] [blame] | 179 | if (bufferCount < minBufferSlots) { |
| 180 | LOGE("setBufferCount: requested buffer count (%d) is less than " |
| 181 | "minimum (%d)", bufferCount, minBufferSlots); |
Jamie Gennis | 96dcc97 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 182 | return BAD_VALUE; |
| 183 | } |
| 184 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 185 | // here we're guaranteed that the client doesn't have dequeued buffers |
| 186 | // and will release all of its buffer references. |
Mathias Agopian | a04cda9 | 2011-08-10 15:28:58 -0700 | [diff] [blame] | 187 | freeAllBuffersLocked(); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 188 | mBufferCount = bufferCount; |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 189 | mClientBufferCount = bufferCount; |
Jamie Gennis | d369dc4 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 190 | mCurrentTexture = INVALID_BUFFER_SLOT; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 191 | mQueue.clear(); |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 192 | mDequeueCondition.signal(); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 193 | return OK; |
| 194 | } |
| 195 | |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 196 | status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 197 | { |
Mathias Agopian | f3503c2 | 2011-07-25 19:56:08 -0700 | [diff] [blame] | 198 | if (!w || !h) { |
| 199 | LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", w, h); |
| 200 | return BAD_VALUE; |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 201 | } |
Mathias Agopian | f3503c2 | 2011-07-25 19:56:08 -0700 | [diff] [blame] | 202 | |
| 203 | Mutex::Autolock lock(mMutex); |
| 204 | mDefaultWidth = w; |
| 205 | mDefaultHeight = h; |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 206 | return OK; |
| 207 | } |
| 208 | |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 209 | status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 210 | LOGV("SurfaceTexture::requestBuffer"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 211 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 212 | if (mAbandoned) { |
| 213 | LOGE("requestBuffer: SurfaceTexture has been abandoned!"); |
| 214 | return NO_INIT; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 215 | } |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 216 | if (slot < 0 || mBufferCount <= slot) { |
| 217 | LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
| 218 | mBufferCount, slot); |
| 219 | return BAD_VALUE; |
| 220 | } |
| 221 | mSlots[slot].mRequestBufferCalled = true; |
| 222 | *buf = mSlots[slot].mGraphicBuffer; |
| 223 | return NO_ERROR; |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h, |
| 227 | uint32_t format, uint32_t usage) { |
| 228 | LOGV("SurfaceTexture::dequeueBuffer"); |
| 229 | |
Pannag Sanketi | c9ec69e | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 230 | if ((w && !h) || (!w && h)) { |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 231 | LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h); |
| 232 | return BAD_VALUE; |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 235 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 236 | |
| 237 | status_t returnFlags(OK); |
| 238 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 239 | int found, foundSync; |
| 240 | int dequeuedCount = 0; |
| 241 | bool tryAgain = true; |
| 242 | while (tryAgain) { |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 243 | if (mAbandoned) { |
| 244 | LOGE("dequeueBuffer: SurfaceTexture has been abandoned!"); |
| 245 | return NO_INIT; |
| 246 | } |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 247 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 248 | // We need to wait for the FIFO to drain if the number of buffer |
| 249 | // needs to change. |
| 250 | // |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 251 | // The condition "number of buffers needs to change" is true if |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 252 | // - the client doesn't care about how many buffers there are |
| 253 | // - AND the actual number of buffer is different from what was |
| 254 | // set in the last setBufferCountServer() |
| 255 | // - OR - |
| 256 | // setBufferCountServer() was set to a value incompatible with |
| 257 | // the synchronization mode (for instance because the sync mode |
| 258 | // changed since) |
| 259 | // |
| 260 | // As long as this condition is true AND the FIFO is not empty, we |
| 261 | // wait on mDequeueCondition. |
| 262 | |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 263 | const int minBufferCountNeeded = mSynchronousMode ? |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 264 | MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS; |
| 265 | |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 266 | const bool numberOfBuffersNeedsToChange = !mClientBufferCount && |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 267 | ((mServerBufferCount != mBufferCount) || |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 268 | (mServerBufferCount < minBufferCountNeeded)); |
| 269 | |
| 270 | if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) { |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 271 | // wait for the FIFO to drain |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 272 | mDequeueCondition.wait(mMutex); |
| 273 | // NOTE: we continue here because we need to reevaluate our |
| 274 | // whole state (eg: we could be abandoned or disconnected) |
| 275 | continue; |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 278 | if (numberOfBuffersNeedsToChange) { |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 279 | // here we're guaranteed that mQueue is empty |
Mathias Agopian | a04cda9 | 2011-08-10 15:28:58 -0700 | [diff] [blame] | 280 | freeAllBuffersLocked(); |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 281 | mBufferCount = mServerBufferCount; |
| 282 | if (mBufferCount < minBufferCountNeeded) |
| 283 | mBufferCount = minBufferCountNeeded; |
| 284 | mCurrentTexture = INVALID_BUFFER_SLOT; |
| 285 | returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS; |
| 286 | } |
| 287 | |
| 288 | // look for a free buffer to give to the client |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 289 | found = INVALID_BUFFER_SLOT; |
| 290 | foundSync = INVALID_BUFFER_SLOT; |
| 291 | dequeuedCount = 0; |
| 292 | for (int i = 0; i < mBufferCount; i++) { |
| 293 | const int state = mSlots[i].mBufferState; |
| 294 | if (state == BufferSlot::DEQUEUED) { |
| 295 | dequeuedCount++; |
| 296 | } |
Mathias Agopian | 9b8e196 | 2011-05-04 18:28:07 -0700 | [diff] [blame] | 297 | if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) { |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 298 | foundSync = i; |
| 299 | if (i != mCurrentTexture) { |
| 300 | found = i; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 305 | |
| 306 | // clients are not allowed to dequeue more than one buffer |
| 307 | // if they didn't set a buffer count. |
| 308 | if (!mClientBufferCount && dequeuedCount) { |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | |
Jamie Gennis | 44e9e0d | 2011-05-23 18:44:04 -0700 | [diff] [blame] | 312 | // See whether a buffer has been queued since the last setBufferCount so |
| 313 | // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below. |
| 314 | bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT; |
| 315 | if (bufferHasBeenQueued) { |
| 316 | // make sure the client is not trying to dequeue more buffers |
| 317 | // than allowed. |
| 318 | const int avail = mBufferCount - (dequeuedCount+1); |
| 319 | if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) { |
| 320 | LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)", |
| 321 | MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode), |
| 322 | dequeuedCount); |
| 323 | return -EBUSY; |
| 324 | } |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 327 | // we're in synchronous mode and didn't find a buffer, we need to wait |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 328 | // for for some buffers to be consumed |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 329 | tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT); |
| 330 | if (tryAgain) { |
| 331 | mDequeueCondition.wait(mMutex); |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 334 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 335 | if (mSynchronousMode && found == INVALID_BUFFER_SLOT) { |
| 336 | // foundSync guaranteed to be != INVALID_BUFFER_SLOT |
| 337 | found = foundSync; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 340 | if (found == INVALID_BUFFER_SLOT) { |
| 341 | return -EBUSY; |
| 342 | } |
| 343 | |
| 344 | const int buf = found; |
| 345 | *outBuf = found; |
| 346 | |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 347 | const bool useDefaultSize = !w && !h; |
| 348 | if (useDefaultSize) { |
| 349 | // use the default size |
| 350 | w = mDefaultWidth; |
| 351 | h = mDefaultHeight; |
| 352 | } |
| 353 | |
| 354 | const bool updateFormat = (format != 0); |
| 355 | if (!updateFormat) { |
| 356 | // keep the current (or default) format |
| 357 | format = mPixelFormat; |
| 358 | } |
| 359 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 360 | // buffer is now in DEQUEUED (but can also be current at the same time, |
| 361 | // if we're in synchronous mode) |
| 362 | mSlots[buf].mBufferState = BufferSlot::DEQUEUED; |
| 363 | |
| 364 | const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer); |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 365 | if ((buffer == NULL) || |
| 366 | (uint32_t(buffer->width) != w) || |
| 367 | (uint32_t(buffer->height) != h) || |
| 368 | (uint32_t(buffer->format) != format) || |
| 369 | ((uint32_t(buffer->usage) & usage) != usage)) |
| 370 | { |
| 371 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Mathias Agopian | eec0f7e | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 372 | status_t error; |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 373 | sp<GraphicBuffer> graphicBuffer( |
Mathias Agopian | eec0f7e | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 374 | mGraphicBufferAlloc->createGraphicBuffer( |
| 375 | w, h, format, usage, &error)); |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 376 | if (graphicBuffer == 0) { |
| 377 | LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed"); |
Mathias Agopian | eec0f7e | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 378 | return error; |
Mathias Agopian | 0297dca | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 379 | } |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 380 | if (updateFormat) { |
| 381 | mPixelFormat = format; |
| 382 | } |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 383 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 384 | mSlots[buf].mRequestBufferCalled = false; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 385 | if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { |
| 386 | eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage); |
| 387 | mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; |
| 388 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 389 | } |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 390 | returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION; |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 391 | } |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 392 | return returnFlags; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 395 | status_t SurfaceTexture::setSynchronousMode(bool enabled) { |
| 396 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 397 | |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 398 | if (mAbandoned) { |
| 399 | LOGE("setSynchronousMode: SurfaceTexture has been abandoned!"); |
| 400 | return NO_INIT; |
| 401 | } |
| 402 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 403 | status_t err = OK; |
Grace Kloba | 0904d0a | 2011-06-23 21:21:47 -0700 | [diff] [blame] | 404 | if (!mAllowSynchronousMode && enabled) |
| 405 | return err; |
| 406 | |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 407 | if (!enabled) { |
| 408 | // going to asynchronous mode, drain the queue |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 409 | err = drainQueueLocked(); |
| 410 | if (err != NO_ERROR) |
| 411 | return err; |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 414 | if (mSynchronousMode != enabled) { |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 415 | // - if we're going to asynchronous mode, the queue is guaranteed to be |
| 416 | // empty here |
| 417 | // - if the client set the number of buffers, we're guaranteed that |
| 418 | // we have at least 3 (because we don't allow less) |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 419 | mSynchronousMode = enabled; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 420 | mDequeueCondition.signal(); |
| 421 | } |
Mathias Agopian | 402ff24 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 422 | return err; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Mathias Agopian | f07b8a3 | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 425 | status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp, |
| 426 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 427 | LOGV("SurfaceTexture::queueBuffer"); |
Mathias Agopian | e845c35 | 2011-05-11 15:05:29 -0700 | [diff] [blame] | 428 | |
| 429 | sp<FrameAvailableListener> listener; |
| 430 | |
| 431 | { // scope for the lock |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 432 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 433 | if (mAbandoned) { |
| 434 | LOGE("queueBuffer: SurfaceTexture has been abandoned!"); |
| 435 | return NO_INIT; |
| 436 | } |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 437 | if (buf < 0 || buf >= mBufferCount) { |
| 438 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 439 | mBufferCount, buf); |
| 440 | return -EINVAL; |
| 441 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 442 | LOGE("queueBuffer: slot %d is not owned by the client (state=%d)", |
| 443 | buf, mSlots[buf].mBufferState); |
| 444 | return -EINVAL; |
| 445 | } else if (buf == mCurrentTexture) { |
| 446 | LOGE("queueBuffer: slot %d is current!", buf); |
| 447 | return -EINVAL; |
| 448 | } else if (!mSlots[buf].mRequestBufferCalled) { |
| 449 | LOGE("queueBuffer: slot %d was enqueued without requesting a " |
| 450 | "buffer", buf); |
| 451 | return -EINVAL; |
| 452 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 453 | |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 454 | if (mSynchronousMode) { |
Jamie Gennis | bd5404d | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 455 | // In synchronous mode we queue all buffers in a FIFO. |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 456 | mQueue.push_back(buf); |
Jamie Gennis | bd5404d | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 457 | |
| 458 | // Synchronous mode always signals that an additional frame should |
| 459 | // be consumed. |
| 460 | listener = mFrameAvailableListener; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 461 | } else { |
Jamie Gennis | bd5404d | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 462 | // In asynchronous mode we only keep the most recent buffer. |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 463 | if (mQueue.empty()) { |
| 464 | mQueue.push_back(buf); |
Jamie Gennis | bd5404d | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 465 | |
| 466 | // Asynchronous mode only signals that a frame should be |
| 467 | // consumed if no previous frame was pending. If a frame were |
| 468 | // pending then the consumer would have already been notified. |
| 469 | listener = mFrameAvailableListener; |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 470 | } else { |
| 471 | Fifo::iterator front(mQueue.begin()); |
| 472 | // buffer currently queued is freed |
| 473 | mSlots[*front].mBufferState = BufferSlot::FREE; |
| 474 | // and we record the new buffer index in the queued list |
| 475 | *front = buf; |
| 476 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 477 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 478 | |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 479 | mSlots[buf].mBufferState = BufferSlot::QUEUED; |
| 480 | mSlots[buf].mCrop = mNextCrop; |
| 481 | mSlots[buf].mTransform = mNextTransform; |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 482 | mSlots[buf].mScalingMode = mNextScalingMode; |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 483 | mSlots[buf].mTimestamp = timestamp; |
| 484 | mDequeueCondition.signal(); |
Mathias Agopian | e845c35 | 2011-05-11 15:05:29 -0700 | [diff] [blame] | 485 | } // scope for the lock |
| 486 | |
| 487 | // call back without lock held |
| 488 | if (listener != 0) { |
| 489 | listener->onFrameAvailable(); |
| 490 | } |
Mathias Agopian | f07b8a3 | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 491 | |
| 492 | *outWidth = mDefaultWidth; |
| 493 | *outHeight = mDefaultHeight; |
| 494 | *outTransform = 0; |
| 495 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 496 | return OK; |
| 497 | } |
| 498 | |
| 499 | void SurfaceTexture::cancelBuffer(int buf) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 500 | LOGV("SurfaceTexture::cancelBuffer"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 501 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 502 | |
| 503 | if (mAbandoned) { |
| 504 | LOGW("cancelBuffer: SurfaceTexture has been abandoned!"); |
| 505 | return; |
| 506 | } |
| 507 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 508 | if (buf < 0 || buf >= mBufferCount) { |
| 509 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", |
| 510 | mBufferCount, buf); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 511 | return; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 512 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 513 | LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)", |
| 514 | buf, mSlots[buf].mBufferState); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 515 | return; |
| 516 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 517 | mSlots[buf].mBufferState = BufferSlot::FREE; |
| 518 | mDequeueCondition.signal(); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 521 | status_t SurfaceTexture::setCrop(const Rect& crop) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 522 | LOGV("SurfaceTexture::setCrop"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 523 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 524 | if (mAbandoned) { |
| 525 | LOGE("setCrop: SurfaceTexture has been abandoned!"); |
| 526 | return NO_INIT; |
| 527 | } |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 528 | mNextCrop = crop; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 529 | return OK; |
| 530 | } |
| 531 | |
| 532 | status_t SurfaceTexture::setTransform(uint32_t transform) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 533 | LOGV("SurfaceTexture::setTransform"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 534 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 535 | if (mAbandoned) { |
| 536 | LOGE("setTransform: SurfaceTexture has been abandoned!"); |
| 537 | return NO_INIT; |
| 538 | } |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 539 | mNextTransform = transform; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 540 | return OK; |
| 541 | } |
| 542 | |
Mathias Agopian | 053b02d | 2011-08-08 19:14:03 -0700 | [diff] [blame] | 543 | status_t SurfaceTexture::connect(int api, |
| 544 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 545 | LOGV("SurfaceTexture::connect(this=%p, %d)", this, api); |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 546 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 547 | |
| 548 | if (mAbandoned) { |
| 549 | LOGE("connect: SurfaceTexture has been abandoned!"); |
| 550 | return NO_INIT; |
| 551 | } |
| 552 | |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 553 | int err = NO_ERROR; |
| 554 | switch (api) { |
| 555 | case NATIVE_WINDOW_API_EGL: |
| 556 | case NATIVE_WINDOW_API_CPU: |
| 557 | case NATIVE_WINDOW_API_MEDIA: |
| 558 | case NATIVE_WINDOW_API_CAMERA: |
| 559 | if (mConnectedApi != NO_CONNECTED_API) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 560 | LOGE("connect: already connected (cur=%d, req=%d)", |
| 561 | mConnectedApi, api); |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 562 | err = -EINVAL; |
| 563 | } else { |
| 564 | mConnectedApi = api; |
Mathias Agopian | 053b02d | 2011-08-08 19:14:03 -0700 | [diff] [blame] | 565 | *outWidth = mDefaultWidth; |
| 566 | *outHeight = mDefaultHeight; |
| 567 | *outTransform = 0; |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 568 | } |
| 569 | break; |
| 570 | default: |
| 571 | err = -EINVAL; |
| 572 | break; |
| 573 | } |
| 574 | return err; |
| 575 | } |
| 576 | |
| 577 | status_t SurfaceTexture::disconnect(int api) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 578 | LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api); |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 579 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 580 | |
| 581 | if (mAbandoned) { |
| 582 | LOGE("connect: SurfaceTexture has been abandoned!"); |
| 583 | return NO_INIT; |
| 584 | } |
| 585 | |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 586 | int err = NO_ERROR; |
| 587 | switch (api) { |
| 588 | case NATIVE_WINDOW_API_EGL: |
| 589 | case NATIVE_WINDOW_API_CPU: |
| 590 | case NATIVE_WINDOW_API_MEDIA: |
| 591 | case NATIVE_WINDOW_API_CAMERA: |
| 592 | if (mConnectedApi == api) { |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 593 | drainQueueAndFreeBuffersLocked(); |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 594 | mConnectedApi = NO_CONNECTED_API; |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 595 | mDequeueCondition.signal(); |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 596 | } else { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 597 | LOGE("disconnect: connected to another api (cur=%d, req=%d)", |
| 598 | mConnectedApi, api); |
Jamie Gennis | 9709687 | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 599 | err = -EINVAL; |
| 600 | } |
| 601 | break; |
| 602 | default: |
| 603 | err = -EINVAL; |
| 604 | break; |
| 605 | } |
| 606 | return err; |
| 607 | } |
| 608 | |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 609 | status_t SurfaceTexture::setScalingMode(int mode) { |
Mathias Agopian | ff86f37 | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 610 | LOGV("SurfaceTexture::setScalingMode(%d)", mode); |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 611 | |
| 612 | switch (mode) { |
| 613 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 614 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 615 | break; |
| 616 | default: |
| 617 | return BAD_VALUE; |
| 618 | } |
| 619 | |
| 620 | Mutex::Autolock lock(mMutex); |
| 621 | mNextScalingMode = mode; |
| 622 | return OK; |
| 623 | } |
| 624 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 625 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 626 | LOGV("SurfaceTexture::updateTexImage"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 627 | Mutex::Autolock lock(mMutex); |
| 628 | |
Mathias Agopian | 95dfd05 | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 629 | if (mAbandoned) { |
| 630 | LOGE("calling updateTexImage() on an abandoned SurfaceTexture"); |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 631 | return NO_INIT; |
Mathias Agopian | 95dfd05 | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Jamie Gennis | 9fb5976 | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 634 | // In asynchronous mode the list is guaranteed to be one buffer |
| 635 | // deep, while in synchronous mode we use the oldest buffer. |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 636 | if (!mQueue.empty()) { |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 637 | Fifo::iterator front(mQueue.begin()); |
Jamie Gennis | 9fb5976 | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 638 | int buf = *front; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 639 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 640 | // Update the GL texture object. |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 641 | EGLImageKHR image = mSlots[buf].mEglImage; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 642 | if (image == EGL_NO_IMAGE_KHR) { |
| 643 | EGLDisplay dpy = eglGetCurrentDisplay(); |
Mathias Agopian | 95dfd05 | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 644 | if (mSlots[buf].mGraphicBuffer == 0) { |
| 645 | LOGE("buffer at slot %d is null", buf); |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 646 | return BAD_VALUE; |
Mathias Agopian | 95dfd05 | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 647 | } |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 648 | image = createImage(dpy, mSlots[buf].mGraphicBuffer); |
| 649 | mSlots[buf].mEglImage = image; |
| 650 | mSlots[buf].mEglDisplay = dpy; |
Mathias Agopian | 6fad64c | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 651 | if (image == EGL_NO_IMAGE_KHR) { |
| 652 | // NOTE: if dpy was invalid, createImage() is guaranteed to |
| 653 | // fail. so we'd end up here. |
| 654 | return -EINVAL; |
| 655 | } |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 656 | } |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 657 | |
| 658 | GLint error; |
| 659 | while ((error = glGetError()) != GL_NO_ERROR) { |
Mathias Agopian | e845c35 | 2011-05-11 15:05:29 -0700 | [diff] [blame] | 660 | LOGW("updateTexImage: clearing GL error: %#04x", error); |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 661 | } |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 662 | |
Jamie Gennis | ecfa1d3 | 2011-07-19 17:58:43 -0700 | [diff] [blame] | 663 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName); |
| 664 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image); |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 665 | |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 666 | bool failed = false; |
| 667 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 668 | LOGE("error binding external texture image %p (slot %d): %#04x", |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 669 | image, buf, error); |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 670 | failed = true; |
| 671 | } |
| 672 | if (failed) { |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 673 | return -EINVAL; |
| 674 | } |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 675 | |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 676 | if (mCurrentTexture != INVALID_BUFFER_SLOT) { |
Jamie Gennis | 9fb5976 | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 677 | // The current buffer becomes FREE if it was still in the queued |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 678 | // state. If it has already been given to the client |
| 679 | // (synchronous mode), then it stays in DEQUEUED state. |
| 680 | if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) |
| 681 | mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE; |
| 682 | } |
| 683 | |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 684 | // Update the SurfaceTexture state. |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 685 | mCurrentTexture = buf; |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 686 | mCurrentTextureBuf = mSlots[buf].mGraphicBuffer; |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 687 | mCurrentCrop = mSlots[buf].mCrop; |
| 688 | mCurrentTransform = mSlots[buf].mTransform; |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 689 | mCurrentScalingMode = mSlots[buf].mScalingMode; |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 690 | mCurrentTimestamp = mSlots[buf].mTimestamp; |
Jamie Gennis | eadfb67 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 691 | computeCurrentTransformMatrix(); |
Jamie Gennis | 9fb5976 | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 692 | |
| 693 | // Now that we've passed the point at which failures can happen, |
| 694 | // it's safe to remove the buffer from the front of the queue. |
| 695 | mQueue.erase(front); |
Mathias Agopian | 5bbb1cf | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 696 | mDequeueCondition.signal(); |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 697 | } else { |
| 698 | // We always bind the texture even if we don't update its contents. |
Jamie Gennis | ecfa1d3 | 2011-07-19 17:58:43 -0700 | [diff] [blame] | 699 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 700 | } |
Jamie Gennis | 9fb5976 | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 701 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 702 | return OK; |
| 703 | } |
| 704 | |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 705 | bool SurfaceTexture::isExternalFormat(uint32_t format) |
| 706 | { |
| 707 | switch (format) { |
| 708 | // supported YUV formats |
| 709 | case HAL_PIXEL_FORMAT_YV12: |
| 710 | // Legacy/deprecated YUV formats |
| 711 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 712 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 713 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | // Any OEM format needs to be considered |
| 718 | if (format>=0x100 && format<=0x1FF) |
| 719 | return true; |
| 720 | |
| 721 | return false; |
| 722 | } |
| 723 | |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 724 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
Jamie Gennis | ecfa1d3 | 2011-07-19 17:58:43 -0700 | [diff] [blame] | 725 | return GL_TEXTURE_EXTERNAL_OES; |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 728 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 729 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | eadfb67 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 730 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 731 | } |
| 732 | |
| 733 | void SurfaceTexture::computeCurrentTransformMatrix() { |
| 734 | LOGV("SurfaceTexture::computeCurrentTransformMatrix"); |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 735 | |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 736 | float xform[16]; |
| 737 | for (int i = 0; i < 16; i++) { |
| 738 | xform[i] = mtxIdentity[i]; |
| 739 | } |
| 740 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 741 | float result[16]; |
| 742 | mtxMul(result, xform, mtxFlipH); |
| 743 | for (int i = 0; i < 16; i++) { |
| 744 | xform[i] = result[i]; |
| 745 | } |
| 746 | } |
| 747 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 748 | float result[16]; |
| 749 | mtxMul(result, xform, mtxFlipV); |
| 750 | for (int i = 0; i < 16; i++) { |
| 751 | xform[i] = result[i]; |
| 752 | } |
| 753 | } |
| 754 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 755 | float result[16]; |
| 756 | mtxMul(result, xform, mtxRot90); |
| 757 | for (int i = 0; i < 16; i++) { |
| 758 | xform[i] = result[i]; |
| 759 | } |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 763 | float tx, ty, sx, sy; |
| 764 | if (!mCurrentCrop.isEmpty()) { |
Jamie Gennis | f3cedb6 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 765 | // In order to prevent bilinear sampling at the of the crop rectangle we |
| 766 | // may need to shrink it by 2 texels in each direction. Normally this |
| 767 | // would just need to take 1/2 a texel off each end, but because the |
| 768 | // chroma channels will likely be subsampled we need to chop off a whole |
| 769 | // texel. This will cause artifacts if someone does nearest sampling |
| 770 | // with 1:1 pixel:texel ratio, but it's impossible to simultaneously |
| 771 | // accomodate the bilinear and nearest sampling uses. |
| 772 | // |
| 773 | // If nearest sampling turns out to be a desirable usage of these |
| 774 | // textures then we could add the ability to switch a SurfaceTexture to |
| 775 | // nearest-mode. Preferably, however, the image producers (video |
| 776 | // decoder, camera, etc.) would simply not use a crop rectangle (or at |
| 777 | // least not tell the framework about it) so that the GPU can do the |
| 778 | // correct edge behavior. |
| 779 | int xshrink = 0, yshrink = 0; |
| 780 | if (mCurrentCrop.left > 0) { |
| 781 | tx = float(mCurrentCrop.left + 1) / float(buf->getWidth()); |
| 782 | xshrink++; |
| 783 | } else { |
| 784 | tx = 0.0f; |
| 785 | } |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 786 | if (mCurrentCrop.right < int32_t(buf->getWidth())) { |
Jamie Gennis | f3cedb6 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 787 | xshrink++; |
| 788 | } |
Mathias Agopian | e5a1bff | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 789 | if (mCurrentCrop.bottom < int32_t(buf->getHeight())) { |
Jamie Gennis | f3cedb6 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 790 | ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) / |
| 791 | float(buf->getHeight()); |
| 792 | yshrink++; |
| 793 | } else { |
| 794 | ty = 0.0f; |
| 795 | } |
| 796 | if (mCurrentCrop.top > 0) { |
| 797 | yshrink++; |
| 798 | } |
| 799 | sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth()); |
| 800 | sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight()); |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 801 | } else { |
| 802 | tx = 0.0f; |
| 803 | ty = 0.0f; |
| 804 | sx = 1.0f; |
| 805 | sy = 1.0f; |
| 806 | } |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 807 | float crop[16] = { |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 808 | sx, 0, 0, 0, |
| 809 | 0, sy, 0, 0, |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 810 | 0, 0, 1, 0, |
Jamie Gennis | f3cedb6 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 811 | tx, ty, 0, 1, |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 812 | }; |
| 813 | |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 814 | float mtxBeforeFlipV[16]; |
| 815 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 816 | |
| 817 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 818 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 819 | // want to expose this to applications, however, so we must add an |
| 820 | // additional vertical flip to the transform after all the other transforms. |
Jamie Gennis | eadfb67 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 821 | mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 824 | nsecs_t SurfaceTexture::getTimestamp() { |
| 825 | LOGV("SurfaceTexture::getTimestamp"); |
| 826 | Mutex::Autolock lock(mMutex); |
| 827 | return mCurrentTimestamp; |
| 828 | } |
| 829 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 830 | void SurfaceTexture::setFrameAvailableListener( |
Pannag Sanketi | c9ec69e | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 831 | const sp<FrameAvailableListener>& listener) { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 832 | LOGV("SurfaceTexture::setFrameAvailableListener"); |
| 833 | Mutex::Autolock lock(mMutex); |
Pannag Sanketi | c9ec69e | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 834 | mFrameAvailableListener = listener; |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 837 | void SurfaceTexture::freeBufferLocked(int i) { |
| 838 | mSlots[i].mGraphicBuffer = 0; |
| 839 | mSlots[i].mBufferState = BufferSlot::FREE; |
| 840 | if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { |
| 841 | eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage); |
| 842 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 843 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 844 | } |
| 845 | } |
| 846 | |
Mathias Agopian | a04cda9 | 2011-08-10 15:28:58 -0700 | [diff] [blame] | 847 | void SurfaceTexture::freeAllBuffersLocked() { |
| 848 | LOGW_IF(!mQueue.isEmpty(), |
| 849 | "freeAllBuffersLocked called but mQueue is not empty"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 850 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 851 | freeBufferLocked(i); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | void SurfaceTexture::freeAllBuffersExceptHeadLocked() { |
| 856 | LOGW_IF(!mQueue.isEmpty(), |
| 857 | "freeAllBuffersExceptCurrentLocked called but mQueue is not empty"); |
| 858 | int head = -1; |
| 859 | if (!mQueue.empty()) { |
| 860 | Fifo::iterator front(mQueue.begin()); |
| 861 | head = *front; |
| 862 | } |
| 863 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 864 | if (i != head) { |
| 865 | freeBufferLocked(i); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 870 | status_t SurfaceTexture::drainQueueLocked() { |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 871 | while (mSynchronousMode && !mQueue.isEmpty()) { |
| 872 | mDequeueCondition.wait(mMutex); |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 873 | if (mAbandoned) { |
| 874 | LOGE("drainQueueLocked: SurfaceTexture has been abandoned!"); |
| 875 | return NO_INIT; |
| 876 | } |
| 877 | if (mConnectedApi == NO_CONNECTED_API) { |
| 878 | LOGE("drainQueueLocked: SurfaceTexture is not connected!"); |
| 879 | return NO_INIT; |
| 880 | } |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 881 | } |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 882 | return NO_ERROR; |
| 883 | } |
| 884 | |
| 885 | status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() { |
| 886 | status_t err = drainQueueLocked(); |
| 887 | if (err == NO_ERROR) { |
| 888 | if (mSynchronousMode) { |
| 889 | freeAllBuffersLocked(); |
| 890 | } else { |
| 891 | freeAllBuffersExceptHeadLocked(); |
| 892 | } |
| 893 | } |
| 894 | return err; |
Mathias Agopian | 71fd121 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 897 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 898 | const sp<GraphicBuffer>& graphicBuffer) { |
| 899 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 900 | EGLint attrs[] = { |
| 901 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 902 | EGL_NONE, |
| 903 | }; |
| 904 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 905 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
Mathias Agopian | 6fad64c | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 906 | if (image == EGL_NO_IMAGE_KHR) { |
| 907 | EGLint error = eglGetError(); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 908 | LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 909 | } |
| 910 | return image; |
| 911 | } |
| 912 | |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 913 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 914 | Mutex::Autolock lock(mMutex); |
| 915 | return mCurrentTextureBuf; |
| 916 | } |
| 917 | |
| 918 | Rect SurfaceTexture::getCurrentCrop() const { |
| 919 | Mutex::Autolock lock(mMutex); |
| 920 | return mCurrentCrop; |
| 921 | } |
| 922 | |
| 923 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 924 | Mutex::Autolock lock(mMutex); |
| 925 | return mCurrentTransform; |
| 926 | } |
| 927 | |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 928 | uint32_t SurfaceTexture::getCurrentScalingMode() const { |
| 929 | Mutex::Autolock lock(mMutex); |
| 930 | return mCurrentScalingMode; |
| 931 | } |
| 932 | |
Mathias Agopian | ed3894c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 933 | int SurfaceTexture::query(int what, int* outValue) |
| 934 | { |
| 935 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 936 | |
| 937 | if (mAbandoned) { |
| 938 | LOGE("query: SurfaceTexture has been abandoned!"); |
| 939 | return NO_INIT; |
| 940 | } |
| 941 | |
Mathias Agopian | ed3894c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 942 | int value; |
| 943 | switch (what) { |
| 944 | case NATIVE_WINDOW_WIDTH: |
| 945 | value = mDefaultWidth; |
Mathias Agopian | ed3894c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 946 | break; |
| 947 | case NATIVE_WINDOW_HEIGHT: |
| 948 | value = mDefaultHeight; |
Mathias Agopian | ed3894c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 949 | break; |
| 950 | case NATIVE_WINDOW_FORMAT: |
| 951 | value = mPixelFormat; |
| 952 | break; |
| 953 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
| 954 | value = mSynchronousMode ? |
| 955 | (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS; |
| 956 | break; |
| 957 | default: |
| 958 | return BAD_VALUE; |
| 959 | } |
| 960 | outValue[0] = value; |
| 961 | return NO_ERROR; |
| 962 | } |
Mathias Agopian | 27cd07c | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 963 | |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 964 | void SurfaceTexture::abandon() { |
| 965 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 966 | mQueue.clear(); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 967 | mAbandoned = true; |
Mathias Agopian | ec46b4e | 2011-08-03 15:18:36 -0700 | [diff] [blame] | 968 | mCurrentTextureBuf.clear(); |
Mathias Agopian | 5c71575 | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 969 | freeAllBuffersLocked(); |
Jamie Gennis | 5ef59bc | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 970 | mDequeueCondition.signal(); |
| 971 | } |
| 972 | |
Mathias Agopian | 7f3289c | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 973 | void SurfaceTexture::dump(String8& result) const |
| 974 | { |
| 975 | char buffer[1024]; |
| 976 | dump(result, "", buffer, 1024); |
| 977 | } |
| 978 | |
| 979 | void SurfaceTexture::dump(String8& result, const char* prefix, |
| 980 | char* buffer, size_t SIZE) const |
| 981 | { |
| 982 | Mutex::Autolock _l(mMutex); |
| 983 | snprintf(buffer, SIZE, |
| 984 | "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], " |
| 985 | "mPixelFormat=%d, mTexName=%d\n", |
| 986 | prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight, |
| 987 | mPixelFormat, mTexName); |
| 988 | result.append(buffer); |
| 989 | |
| 990 | String8 fifo; |
| 991 | int fifoSize = 0; |
| 992 | Fifo::const_iterator i(mQueue.begin()); |
| 993 | while (i != mQueue.end()) { |
| 994 | snprintf(buffer, SIZE, "%02d ", *i++); |
| 995 | fifoSize++; |
| 996 | fifo.append(buffer); |
| 997 | } |
| 998 | |
| 999 | snprintf(buffer, SIZE, |
Jamie Gennis | ecfa1d3 | 2011-07-19 17:58:43 -0700 | [diff] [blame] | 1000 | "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n" |
Mathias Agopian | 7f3289c | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1001 | "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n" |
| 1002 | , |
| 1003 | prefix, mCurrentCrop.left, |
| 1004 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
Jamie Gennis | ecfa1d3 | 2011-07-19 17:58:43 -0700 | [diff] [blame] | 1005 | mCurrentTransform, mCurrentTexture, |
Mathias Agopian | 7f3289c | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1006 | prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom, |
| 1007 | mCurrentTransform, fifoSize, fifo.string() |
| 1008 | ); |
| 1009 | result.append(buffer); |
| 1010 | |
| 1011 | struct { |
| 1012 | const char * operator()(int state) const { |
| 1013 | switch (state) { |
| 1014 | case BufferSlot::DEQUEUED: return "DEQUEUED"; |
| 1015 | case BufferSlot::QUEUED: return "QUEUED"; |
| 1016 | case BufferSlot::FREE: return "FREE"; |
| 1017 | default: return "Unknown"; |
| 1018 | } |
| 1019 | } |
| 1020 | } stateName; |
| 1021 | |
| 1022 | for (int i=0 ; i<mBufferCount ; i++) { |
| 1023 | const BufferSlot& slot(mSlots[i]); |
| 1024 | snprintf(buffer, SIZE, |
Mathias Agopian | 0c3367f | 2011-08-08 16:02:13 -0700 | [diff] [blame] | 1025 | "%s%s[%02d] " |
Mathias Agopian | 0c3367f | 2011-08-08 16:02:13 -0700 | [diff] [blame] | 1026 | "state=%-8s, crop=[%d,%d,%d,%d], " |
Mathias Agopian | 4954193 | 2011-08-09 15:48:43 -0700 | [diff] [blame] | 1027 | "transform=0x%02x, timestamp=%lld", |
Mathias Agopian | 0c3367f | 2011-08-08 16:02:13 -0700 | [diff] [blame] | 1028 | prefix, (i==mCurrentTexture)?">":" ", i, |
Mathias Agopian | 0c3367f | 2011-08-08 16:02:13 -0700 | [diff] [blame] | 1029 | stateName(slot.mBufferState), |
Jamie Gennis | a218715 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 1030 | slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom, |
| 1031 | slot.mTransform, slot.mTimestamp |
Mathias Agopian | 7f3289c | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1032 | ); |
| 1033 | result.append(buffer); |
Mathias Agopian | 4954193 | 2011-08-09 15:48:43 -0700 | [diff] [blame] | 1034 | |
| 1035 | const sp<GraphicBuffer>& buf(slot.mGraphicBuffer); |
| 1036 | if (buf != NULL) { |
| 1037 | snprintf(buffer, SIZE, |
| 1038 | ", %p [%4ux%4u:%4u,%3X]", |
| 1039 | buf->handle, buf->width, buf->height, buf->stride, buf->format); |
| 1040 | result.append(buffer); |
| 1041 | } |
| 1042 | result.append("\n"); |
Mathias Agopian | 7f3289c | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 1046 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 1047 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 1048 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 1049 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 1050 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 1051 | |
| 1052 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 1053 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 1054 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 1055 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 1056 | |
| 1057 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 1058 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 1059 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 1060 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 1061 | |
| 1062 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 1063 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 1064 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 1065 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 1066 | } |
| 1067 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1068 | }; // namespace android |