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