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