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