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