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 | |
| 30 | #include <surfaceflinger/ISurfaceComposer.h> |
| 31 | #include <surfaceflinger/SurfaceComposerClient.h> |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/IGraphicBufferAlloc.h> |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 33 | |
| 34 | #include <utils/Log.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 38 | // Transform matrices |
| 39 | static float mtxIdentity[16] = { |
| 40 | 1, 0, 0, 0, |
| 41 | 0, 1, 0, 0, |
| 42 | 0, 0, 1, 0, |
| 43 | 0, 0, 0, 1, |
| 44 | }; |
| 45 | static float mtxFlipH[16] = { |
| 46 | -1, 0, 0, 0, |
| 47 | 0, 1, 0, 0, |
| 48 | 0, 0, 1, 0, |
| 49 | 1, 0, 0, 1, |
| 50 | }; |
| 51 | static float mtxFlipV[16] = { |
| 52 | 1, 0, 0, 0, |
| 53 | 0, -1, 0, 0, |
| 54 | 0, 0, 1, 0, |
| 55 | 0, 1, 0, 1, |
| 56 | }; |
| 57 | static float mtxRot90[16] = { |
| 58 | 0, 1, 0, 0, |
| 59 | -1, 0, 0, 0, |
| 60 | 0, 0, 1, 0, |
| 61 | 1, 0, 0, 1, |
| 62 | }; |
| 63 | static float mtxRot180[16] = { |
| 64 | -1, 0, 0, 0, |
| 65 | 0, -1, 0, 0, |
| 66 | 0, 0, 1, 0, |
| 67 | 1, 1, 0, 1, |
| 68 | }; |
| 69 | static float mtxRot270[16] = { |
| 70 | 0, -1, 0, 0, |
| 71 | 1, 0, 0, 0, |
| 72 | 0, 0, 1, 0, |
| 73 | 0, 1, 0, 1, |
| 74 | }; |
| 75 | |
| 76 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 77 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 78 | SurfaceTexture::SurfaceTexture(GLuint tex) : |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame^] | 79 | mBufferCount(MIN_BUFFER_SLOTS), |
| 80 | mCurrentTexture(INVALID_BUFFER_SLOT), |
| 81 | mCurrentTransform(0), |
| 82 | mCurrentTimestamp(0), |
| 83 | mLastQueued(INVALID_BUFFER_SLOT), |
| 84 | mLastQueuedTransform(0), |
| 85 | mLastQueuedTimestamp(0), |
| 86 | mNextTransform(0), |
| 87 | mTexName(tex) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 88 | LOGV("SurfaceTexture::SurfaceTexture"); |
Jamie Gennis | fd804f3 | 2011-01-07 16:05:47 -0800 | [diff] [blame] | 89 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 90 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 91 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 92 | mSlots[i].mOwnedByClient = false; |
| 93 | } |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 94 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 95 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 99 | LOGV("SurfaceTexture::~SurfaceTexture"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 100 | freeAllBuffers(); |
| 101 | } |
| 102 | |
| 103 | status_t SurfaceTexture::setBufferCount(int bufferCount) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 104 | LOGV("SurfaceTexture::setBufferCount"); |
Jamie Gennis | 96dcc97 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 105 | |
| 106 | if (bufferCount < MIN_BUFFER_SLOTS) { |
| 107 | return BAD_VALUE; |
| 108 | } |
| 109 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 110 | Mutex::Autolock lock(mMutex); |
| 111 | freeAllBuffers(); |
| 112 | mBufferCount = bufferCount; |
Jamie Gennis | d369dc4 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 113 | mCurrentTexture = INVALID_BUFFER_SLOT; |
| 114 | mLastQueued = INVALID_BUFFER_SLOT; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 115 | return OK; |
| 116 | } |
| 117 | |
| 118 | sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf, |
| 119 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 120 | LOGV("SurfaceTexture::requestBuffer"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 121 | Mutex::Autolock lock(mMutex); |
| 122 | if (buf < 0 || mBufferCount <= buf) { |
| 123 | LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
| 124 | mBufferCount, buf); |
| 125 | return 0; |
| 126 | } |
| 127 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 128 | sp<GraphicBuffer> graphicBuffer( |
| 129 | mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage)); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 130 | if (graphicBuffer == 0) { |
| 131 | LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed"); |
| 132 | } else { |
| 133 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
| 134 | if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { |
| 135 | eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage); |
| 136 | mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; |
| 137 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 138 | } |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 139 | mAllocdBuffers.add(graphicBuffer); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 140 | } |
| 141 | return graphicBuffer; |
| 142 | } |
| 143 | |
| 144 | status_t SurfaceTexture::dequeueBuffer(int *buf) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 145 | LOGV("SurfaceTexture::dequeueBuffer"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 146 | Mutex::Autolock lock(mMutex); |
| 147 | int found = INVALID_BUFFER_SLOT; |
| 148 | for (int i = 0; i < mBufferCount; i++) { |
Jamie Gennis | a7eacc1 | 2011-01-11 15:00:09 -0800 | [diff] [blame] | 149 | if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) { |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 150 | mSlots[i].mOwnedByClient = true; |
| 151 | found = i; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (found == INVALID_BUFFER_SLOT) { |
| 156 | return -EBUSY; |
| 157 | } |
| 158 | *buf = found; |
| 159 | return OK; |
| 160 | } |
| 161 | |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame^] | 162 | status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 163 | LOGV("SurfaceTexture::queueBuffer"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 164 | Mutex::Autolock lock(mMutex); |
| 165 | if (buf < 0 || mBufferCount <= buf) { |
| 166 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 167 | mBufferCount, buf); |
| 168 | return -EINVAL; |
| 169 | } else if (!mSlots[buf].mOwnedByClient) { |
| 170 | LOGE("queueBuffer: slot %d is not owned by the client", buf); |
| 171 | return -EINVAL; |
| 172 | } else if (mSlots[buf].mGraphicBuffer == 0) { |
| 173 | LOGE("queueBuffer: slot %d was enqueued without requesting a buffer", |
| 174 | buf); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | mSlots[buf].mOwnedByClient = false; |
| 178 | mLastQueued = buf; |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 179 | mLastQueuedCrop = mNextCrop; |
| 180 | mLastQueuedTransform = mNextTransform; |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame^] | 181 | mLastQueuedTimestamp = timestamp; |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 182 | if (mFrameAvailableListener != 0) { |
| 183 | mFrameAvailableListener->onFrameAvailable(); |
| 184 | } |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 185 | return OK; |
| 186 | } |
| 187 | |
| 188 | void SurfaceTexture::cancelBuffer(int buf) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 189 | LOGV("SurfaceTexture::cancelBuffer"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 190 | Mutex::Autolock lock(mMutex); |
| 191 | if (buf < 0 || mBufferCount <= buf) { |
| 192 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount, |
| 193 | buf); |
| 194 | return; |
| 195 | } else if (!mSlots[buf].mOwnedByClient) { |
| 196 | LOGE("cancelBuffer: slot %d is not owned by the client", buf); |
| 197 | return; |
| 198 | } |
| 199 | mSlots[buf].mOwnedByClient = false; |
| 200 | } |
| 201 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 202 | status_t SurfaceTexture::setCrop(const Rect& crop) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 203 | LOGV("SurfaceTexture::setCrop"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 204 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 205 | mNextCrop = crop; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 206 | return OK; |
| 207 | } |
| 208 | |
| 209 | status_t SurfaceTexture::setTransform(uint32_t transform) { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 210 | LOGV("SurfaceTexture::setTransform"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 211 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 212 | mNextTransform = transform; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 213 | return OK; |
| 214 | } |
| 215 | |
| 216 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | 7dc00d5 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 217 | LOGV("SurfaceTexture::updateTexImage"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 218 | Mutex::Autolock lock(mMutex); |
| 219 | |
| 220 | // We always bind the texture even if we don't update its contents. |
| 221 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName); |
| 222 | |
| 223 | // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT, |
| 224 | // so this check will fail until a buffer gets queued. |
| 225 | if (mCurrentTexture != mLastQueued) { |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 226 | // Update the GL texture object. |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 227 | EGLImageKHR image = mSlots[mLastQueued].mEglImage; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 228 | if (image == EGL_NO_IMAGE_KHR) { |
| 229 | EGLDisplay dpy = eglGetCurrentDisplay(); |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 230 | sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 231 | image = createImage(dpy, graphicBuffer); |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 232 | mSlots[mLastQueued].mEglImage = image; |
| 233 | mSlots[mLastQueued].mEglDisplay = dpy; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 234 | } |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 235 | |
| 236 | GLint error; |
| 237 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 238 | LOGE("GL error cleared before updating SurfaceTexture: %#04x", error); |
| 239 | } |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 240 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image); |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 241 | bool failed = false; |
| 242 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 243 | LOGE("error binding external texture image %p (slot %d): %#04x", |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 244 | image, mLastQueued, error); |
Jamie Gennis | 79d01fe | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 245 | failed = true; |
| 246 | } |
| 247 | if (failed) { |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 248 | return -EINVAL; |
| 249 | } |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 250 | |
| 251 | // Update the SurfaceTexture state. |
| 252 | mCurrentTexture = mLastQueued; |
| 253 | mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer; |
| 254 | mCurrentCrop = mLastQueuedCrop; |
| 255 | mCurrentTransform = mLastQueuedTransform; |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame^] | 256 | mCurrentTimestamp = mLastQueuedTimestamp; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 257 | } |
| 258 | return OK; |
| 259 | } |
| 260 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 261 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame^] | 262 | LOGV("SurfaceTexture::getTransformMatrix"); |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 263 | Mutex::Autolock lock(mMutex); |
| 264 | |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 265 | float xform[16]; |
| 266 | for (int i = 0; i < 16; i++) { |
| 267 | xform[i] = mtxIdentity[i]; |
| 268 | } |
| 269 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 270 | float result[16]; |
| 271 | mtxMul(result, xform, mtxFlipH); |
| 272 | for (int i = 0; i < 16; i++) { |
| 273 | xform[i] = result[i]; |
| 274 | } |
| 275 | } |
| 276 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 277 | float result[16]; |
| 278 | mtxMul(result, xform, mtxFlipV); |
| 279 | for (int i = 0; i < 16; i++) { |
| 280 | xform[i] = result[i]; |
| 281 | } |
| 282 | } |
| 283 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 284 | float result[16]; |
| 285 | mtxMul(result, xform, mtxRot90); |
| 286 | for (int i = 0; i < 16; i++) { |
| 287 | xform[i] = result[i]; |
| 288 | } |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 292 | float tx, ty, sx, sy; |
| 293 | if (!mCurrentCrop.isEmpty()) { |
Jamie Gennis | f3cedb6 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 294 | // In order to prevent bilinear sampling at the of the crop rectangle we |
| 295 | // may need to shrink it by 2 texels in each direction. Normally this |
| 296 | // would just need to take 1/2 a texel off each end, but because the |
| 297 | // chroma channels will likely be subsampled we need to chop off a whole |
| 298 | // texel. This will cause artifacts if someone does nearest sampling |
| 299 | // with 1:1 pixel:texel ratio, but it's impossible to simultaneously |
| 300 | // accomodate the bilinear and nearest sampling uses. |
| 301 | // |
| 302 | // If nearest sampling turns out to be a desirable usage of these |
| 303 | // textures then we could add the ability to switch a SurfaceTexture to |
| 304 | // nearest-mode. Preferably, however, the image producers (video |
| 305 | // decoder, camera, etc.) would simply not use a crop rectangle (or at |
| 306 | // least not tell the framework about it) so that the GPU can do the |
| 307 | // correct edge behavior. |
| 308 | int xshrink = 0, yshrink = 0; |
| 309 | if (mCurrentCrop.left > 0) { |
| 310 | tx = float(mCurrentCrop.left + 1) / float(buf->getWidth()); |
| 311 | xshrink++; |
| 312 | } else { |
| 313 | tx = 0.0f; |
| 314 | } |
| 315 | if (mCurrentCrop.right < buf->getWidth()) { |
| 316 | xshrink++; |
| 317 | } |
| 318 | if (mCurrentCrop.bottom < buf->getHeight()) { |
| 319 | ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) / |
| 320 | float(buf->getHeight()); |
| 321 | yshrink++; |
| 322 | } else { |
| 323 | ty = 0.0f; |
| 324 | } |
| 325 | if (mCurrentCrop.top > 0) { |
| 326 | yshrink++; |
| 327 | } |
| 328 | sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth()); |
| 329 | sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight()); |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 330 | } else { |
| 331 | tx = 0.0f; |
| 332 | ty = 0.0f; |
| 333 | sx = 1.0f; |
| 334 | sy = 1.0f; |
| 335 | } |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 336 | float crop[16] = { |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 337 | sx, 0, 0, 0, |
| 338 | 0, sy, 0, 0, |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 339 | 0, 0, 1, 0, |
Jamie Gennis | f3cedb6 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 340 | tx, ty, 0, 1, |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 341 | }; |
| 342 | |
Jamie Gennis | 0fb736c | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 343 | float mtxBeforeFlipV[16]; |
| 344 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 345 | |
| 346 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 347 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 348 | // want to expose this to applications, however, so we must add an |
| 349 | // additional vertical flip to the transform after all the other transforms. |
| 350 | mtxMul(mtx, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame^] | 353 | nsecs_t SurfaceTexture::getTimestamp() { |
| 354 | LOGV("SurfaceTexture::getTimestamp"); |
| 355 | Mutex::Autolock lock(mMutex); |
| 356 | return mCurrentTimestamp; |
| 357 | } |
| 358 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 359 | void SurfaceTexture::setFrameAvailableListener( |
| 360 | const sp<FrameAvailableListener>& l) { |
| 361 | LOGV("SurfaceTexture::setFrameAvailableListener"); |
| 362 | Mutex::Autolock lock(mMutex); |
| 363 | mFrameAvailableListener = l; |
| 364 | } |
| 365 | |
Jamie Gennis | 83bac21 | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 366 | sp<IBinder> SurfaceTexture::getAllocator() { |
| 367 | LOGV("SurfaceTexture::getAllocator"); |
| 368 | return mGraphicBufferAlloc->asBinder(); |
| 369 | } |
| 370 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 371 | void SurfaceTexture::freeAllBuffers() { |
| 372 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 373 | mSlots[i].mGraphicBuffer = 0; |
| 374 | mSlots[i].mOwnedByClient = false; |
| 375 | if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { |
| 376 | eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage); |
| 377 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 378 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 379 | } |
| 380 | } |
Jamie Gennis | f7acf16 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 381 | |
| 382 | int exceptBuf = -1; |
| 383 | for (size_t i = 0; i < mAllocdBuffers.size(); i++) { |
| 384 | if (mAllocdBuffers[i] == mCurrentTextureBuf) { |
| 385 | exceptBuf = i; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | mAllocdBuffers.clear(); |
| 390 | if (exceptBuf >= 0) { |
| 391 | mAllocdBuffers.add(mCurrentTextureBuf); |
| 392 | } |
| 393 | mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 397 | const sp<GraphicBuffer>& graphicBuffer) { |
| 398 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 399 | EGLint attrs[] = { |
| 400 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 401 | EGL_NONE, |
| 402 | }; |
| 403 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 404 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
| 405 | EGLint error = eglGetError(); |
| 406 | if (error != EGL_SUCCESS) { |
| 407 | LOGE("error creating EGLImage: %#x", error); |
| 408 | } else if (image == EGL_NO_IMAGE_KHR) { |
| 409 | LOGE("no error reported, but no image was returned by " |
| 410 | "eglCreateImageKHR"); |
| 411 | } |
| 412 | return image; |
| 413 | } |
| 414 | |
Jamie Gennis | b598fb9 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 415 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 416 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 417 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 418 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 419 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 420 | |
| 421 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 422 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 423 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 424 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 425 | |
| 426 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 427 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 428 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 429 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 430 | |
| 431 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 432 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 433 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 434 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 435 | } |
| 436 | |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 437 | }; // namespace android |