| 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 |  | 
|  | 30 | #include <surfaceflinger/ISurfaceComposer.h> | 
|  | 31 | #include <surfaceflinger/SurfaceComposerClient.h> | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/IGraphicBufferAlloc.h> | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | #include <utils/Log.h> | 
|  | 35 |  | 
|  | 36 | namespace android { | 
|  | 37 |  | 
| Jamie Gennis | f238e28 | 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 | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 78 | SurfaceTexture::SurfaceTexture(GLuint tex) : | 
|  | 79 | mBufferCount(MIN_BUFFER_SLOTS), mCurrentTexture(INVALID_BUFFER_SLOT), | 
| Jamie Gennis | f73935b | 2011-02-04 13:46:38 -0800 | [diff] [blame] | 80 | mCurrentTransform(0), mLastQueued(INVALID_BUFFER_SLOT), | 
|  | 81 | mLastQueuedTransform(0), mNextTransform(0), mTexName(tex) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 82 | LOGV("SurfaceTexture::SurfaceTexture"); | 
| Jamie Gennis | 3461e0f | 2011-01-07 16:05:47 -0800 | [diff] [blame] | 83 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
|  | 84 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; | 
|  | 85 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; | 
|  | 86 | mSlots[i].mOwnedByClient = false; | 
|  | 87 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 88 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); | 
|  | 89 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | SurfaceTexture::~SurfaceTexture() { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 93 | LOGV("SurfaceTexture::~SurfaceTexture"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 94 | freeAllBuffers(); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | status_t SurfaceTexture::setBufferCount(int bufferCount) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 98 | LOGV("SurfaceTexture::setBufferCount"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 99 | Mutex::Autolock lock(mMutex); | 
|  | 100 | freeAllBuffers(); | 
|  | 101 | mBufferCount = bufferCount; | 
| Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 102 | mCurrentTexture = INVALID_BUFFER_SLOT; | 
|  | 103 | mLastQueued = INVALID_BUFFER_SLOT; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 104 | return OK; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf, | 
|  | 108 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 109 | LOGV("SurfaceTexture::requestBuffer"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 110 | Mutex::Autolock lock(mMutex); | 
|  | 111 | if (buf < 0 || mBufferCount <= buf) { | 
|  | 112 | LOGE("requestBuffer: slot index out of range [0, %d]: %d", | 
|  | 113 | mBufferCount, buf); | 
|  | 114 | return 0; | 
|  | 115 | } | 
|  | 116 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 117 | sp<GraphicBuffer> graphicBuffer( | 
|  | 118 | mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage)); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 119 | if (graphicBuffer == 0) { | 
|  | 120 | LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed"); | 
|  | 121 | } else { | 
|  | 122 | mSlots[buf].mGraphicBuffer = graphicBuffer; | 
|  | 123 | if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { | 
|  | 124 | eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage); | 
|  | 125 | mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; | 
|  | 126 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; | 
|  | 127 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 128 | mAllocdBuffers.add(graphicBuffer); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 129 | } | 
|  | 130 | return graphicBuffer; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | status_t SurfaceTexture::dequeueBuffer(int *buf) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 134 | LOGV("SurfaceTexture::dequeueBuffer"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 135 | Mutex::Autolock lock(mMutex); | 
|  | 136 | int found = INVALID_BUFFER_SLOT; | 
|  | 137 | for (int i = 0; i < mBufferCount; i++) { | 
| Jamie Gennis | 235c424 | 2011-01-11 15:00:09 -0800 | [diff] [blame] | 138 | if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) { | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 139 | mSlots[i].mOwnedByClient = true; | 
|  | 140 | found = i; | 
|  | 141 | break; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 | if (found == INVALID_BUFFER_SLOT) { | 
|  | 145 | return -EBUSY; | 
|  | 146 | } | 
|  | 147 | *buf = found; | 
|  | 148 | return OK; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | status_t SurfaceTexture::queueBuffer(int buf) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 152 | LOGV("SurfaceTexture::queueBuffer"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 153 | Mutex::Autolock lock(mMutex); | 
|  | 154 | if (buf < 0 || mBufferCount <= buf) { | 
|  | 155 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", | 
|  | 156 | mBufferCount, buf); | 
|  | 157 | return -EINVAL; | 
|  | 158 | } else if (!mSlots[buf].mOwnedByClient) { | 
|  | 159 | LOGE("queueBuffer: slot %d is not owned by the client", buf); | 
|  | 160 | return -EINVAL; | 
|  | 161 | } else if (mSlots[buf].mGraphicBuffer == 0) { | 
|  | 162 | LOGE("queueBuffer: slot %d was enqueued without requesting a buffer", | 
|  | 163 | buf); | 
|  | 164 | return -EINVAL; | 
|  | 165 | } | 
|  | 166 | mSlots[buf].mOwnedByClient = false; | 
|  | 167 | mLastQueued = buf; | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 168 | mLastQueuedCrop = mNextCrop; | 
|  | 169 | mLastQueuedTransform = mNextTransform; | 
| Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 170 | if (mFrameAvailableListener != 0) { | 
|  | 171 | mFrameAvailableListener->onFrameAvailable(); | 
|  | 172 | } | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 173 | return OK; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | void SurfaceTexture::cancelBuffer(int buf) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 177 | LOGV("SurfaceTexture::cancelBuffer"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 178 | Mutex::Autolock lock(mMutex); | 
|  | 179 | if (buf < 0 || mBufferCount <= buf) { | 
|  | 180 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount, | 
|  | 181 | buf); | 
|  | 182 | return; | 
|  | 183 | } else if (!mSlots[buf].mOwnedByClient) { | 
|  | 184 | LOGE("cancelBuffer: slot %d is not owned by the client", buf); | 
|  | 185 | return; | 
|  | 186 | } | 
|  | 187 | mSlots[buf].mOwnedByClient = false; | 
|  | 188 | } | 
|  | 189 |  | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 190 | status_t SurfaceTexture::setCrop(const Rect& crop) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 191 | LOGV("SurfaceTexture::setCrop"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 192 | Mutex::Autolock lock(mMutex); | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 193 | mNextCrop = crop; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 194 | return OK; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | status_t SurfaceTexture::setTransform(uint32_t transform) { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 198 | LOGV("SurfaceTexture::setTransform"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 199 | Mutex::Autolock lock(mMutex); | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 200 | mNextTransform = transform; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 201 | return OK; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | status_t SurfaceTexture::updateTexImage() { | 
| Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 205 | LOGV("SurfaceTexture::updateTexImage"); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 206 | Mutex::Autolock lock(mMutex); | 
|  | 207 |  | 
|  | 208 | // We always bind the texture even if we don't update its contents. | 
|  | 209 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName); | 
|  | 210 |  | 
|  | 211 | // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT, | 
|  | 212 | // so this check will fail until a buffer gets queued. | 
|  | 213 | if (mCurrentTexture != mLastQueued) { | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 214 | // Update the GL texture object. | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 215 | EGLImageKHR image = mSlots[mLastQueued].mEglImage; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 216 | if (image == EGL_NO_IMAGE_KHR) { | 
|  | 217 | EGLDisplay dpy = eglGetCurrentDisplay(); | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 218 | sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 219 | image = createImage(dpy, graphicBuffer); | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 220 | mSlots[mLastQueued].mEglImage = image; | 
|  | 221 | mSlots[mLastQueued].mEglDisplay = dpy; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 222 | } | 
| Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 223 |  | 
|  | 224 | GLint error; | 
|  | 225 | while ((error = glGetError()) != GL_NO_ERROR) { | 
|  | 226 | LOGE("GL error cleared before updating SurfaceTexture: %#04x", error); | 
|  | 227 | } | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 228 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image); | 
| Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 229 | bool failed = false; | 
|  | 230 | while ((error = glGetError()) != GL_NO_ERROR) { | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 231 | LOGE("error binding external texture image %p (slot %d): %#04x", | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 232 | image, mLastQueued, error); | 
| Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 233 | failed = true; | 
|  | 234 | } | 
|  | 235 | if (failed) { | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 236 | return -EINVAL; | 
|  | 237 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 238 |  | 
|  | 239 | // Update the SurfaceTexture state. | 
|  | 240 | mCurrentTexture = mLastQueued; | 
|  | 241 | mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer; | 
|  | 242 | mCurrentCrop = mLastQueuedCrop; | 
|  | 243 | mCurrentTransform = mLastQueuedTransform; | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 244 | } | 
|  | 245 | return OK; | 
|  | 246 | } | 
|  | 247 |  | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 248 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { | 
|  | 249 | LOGV("SurfaceTexture::updateTexImage"); | 
|  | 250 | Mutex::Autolock lock(mMutex); | 
|  | 251 |  | 
| Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 252 | float xform[16]; | 
|  | 253 | for (int i = 0; i < 16; i++) { | 
|  | 254 | xform[i] = mtxIdentity[i]; | 
|  | 255 | } | 
|  | 256 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { | 
|  | 257 | float result[16]; | 
|  | 258 | mtxMul(result, xform, mtxFlipH); | 
|  | 259 | for (int i = 0; i < 16; i++) { | 
|  | 260 | xform[i] = result[i]; | 
|  | 261 | } | 
|  | 262 | } | 
|  | 263 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { | 
|  | 264 | float result[16]; | 
|  | 265 | mtxMul(result, xform, mtxFlipV); | 
|  | 266 | for (int i = 0; i < 16; i++) { | 
|  | 267 | xform[i] = result[i]; | 
|  | 268 | } | 
|  | 269 | } | 
|  | 270 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { | 
|  | 271 | float result[16]; | 
|  | 272 | mtxMul(result, xform, mtxRot90); | 
|  | 273 | for (int i = 0; i < 16; i++) { | 
|  | 274 | xform[i] = result[i]; | 
|  | 275 | } | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
|  | 278 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); | 
| Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 279 | float tx, ty, sx, sy; | 
|  | 280 | if (!mCurrentCrop.isEmpty()) { | 
|  | 281 | tx = float(mCurrentCrop.left) / float(buf->getWidth()); | 
|  | 282 | ty = float(buf->getHeight() - mCurrentCrop.bottom) / | 
|  | 283 | float(buf->getHeight()); | 
|  | 284 | sx = float(mCurrentCrop.width()) / float(buf->getWidth()); | 
|  | 285 | sy = float(mCurrentCrop.height()) / float(buf->getHeight()); | 
|  | 286 | } else { | 
|  | 287 | tx = 0.0f; | 
|  | 288 | ty = 0.0f; | 
|  | 289 | sx = 1.0f; | 
|  | 290 | sy = 1.0f; | 
|  | 291 | } | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 292 | float crop[16] = { | 
| Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 293 | sx, 0, 0, 0, | 
|  | 294 | 0, sy, 0, 0, | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 295 | 0, 0, 1, 0, | 
| Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 296 | sx*tx, sy*ty, 0, 1, | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 297 | }; | 
|  | 298 |  | 
| Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 299 | float mtxBeforeFlipV[16]; | 
|  | 300 | mtxMul(mtxBeforeFlipV, crop, xform); | 
|  | 301 |  | 
|  | 302 | // SurfaceFlinger expects the top of its window textures to be at a Y | 
|  | 303 | // coordinate of 0, so SurfaceTexture must behave the same way.  We don't | 
|  | 304 | // want to expose this to applications, however, so we must add an | 
|  | 305 | // additional vertical flip to the transform after all the other transforms. | 
|  | 306 | mtxMul(mtx, mtxFlipV, mtxBeforeFlipV); | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
| Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 309 | void SurfaceTexture::setFrameAvailableListener( | 
|  | 310 | const sp<FrameAvailableListener>& l) { | 
|  | 311 | LOGV("SurfaceTexture::setFrameAvailableListener"); | 
|  | 312 | Mutex::Autolock lock(mMutex); | 
|  | 313 | mFrameAvailableListener = l; | 
|  | 314 | } | 
|  | 315 |  | 
| Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 316 | sp<IBinder> SurfaceTexture::getAllocator() { | 
|  | 317 | LOGV("SurfaceTexture::getAllocator"); | 
|  | 318 | return mGraphicBufferAlloc->asBinder(); | 
|  | 319 | } | 
|  | 320 |  | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 321 | void SurfaceTexture::freeAllBuffers() { | 
|  | 322 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
|  | 323 | mSlots[i].mGraphicBuffer = 0; | 
|  | 324 | mSlots[i].mOwnedByClient = false; | 
|  | 325 | if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { | 
|  | 326 | eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage); | 
|  | 327 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; | 
|  | 328 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; | 
|  | 329 | } | 
|  | 330 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 331 |  | 
|  | 332 | int exceptBuf = -1; | 
|  | 333 | for (size_t i = 0; i < mAllocdBuffers.size(); i++) { | 
|  | 334 | if (mAllocdBuffers[i] == mCurrentTextureBuf) { | 
|  | 335 | exceptBuf = i; | 
|  | 336 | break; | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 | mAllocdBuffers.clear(); | 
|  | 340 | if (exceptBuf >= 0) { | 
|  | 341 | mAllocdBuffers.add(mCurrentTextureBuf); | 
|  | 342 | } | 
|  | 343 | mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf); | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 344 | } | 
|  | 345 |  | 
|  | 346 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, | 
|  | 347 | const sp<GraphicBuffer>& graphicBuffer) { | 
|  | 348 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); | 
|  | 349 | EGLint attrs[] = { | 
|  | 350 | EGL_IMAGE_PRESERVED_KHR,    EGL_TRUE, | 
|  | 351 | EGL_NONE, | 
|  | 352 | }; | 
|  | 353 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, | 
|  | 354 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); | 
|  | 355 | EGLint error = eglGetError(); | 
|  | 356 | if (error != EGL_SUCCESS) { | 
|  | 357 | LOGE("error creating EGLImage: %#x", error); | 
|  | 358 | } else if (image == EGL_NO_IMAGE_KHR) { | 
|  | 359 | LOGE("no error reported, but no image was returned by " | 
|  | 360 | "eglCreateImageKHR"); | 
|  | 361 | } | 
|  | 362 | return image; | 
|  | 363 | } | 
|  | 364 |  | 
| Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 365 | static void mtxMul(float out[16], const float a[16], const float b[16]) { | 
|  | 366 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; | 
|  | 367 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; | 
|  | 368 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; | 
|  | 369 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; | 
|  | 370 |  | 
|  | 371 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; | 
|  | 372 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; | 
|  | 373 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; | 
|  | 374 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; | 
|  | 375 |  | 
|  | 376 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; | 
|  | 377 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; | 
|  | 378 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; | 
|  | 379 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; | 
|  | 380 |  | 
|  | 381 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; | 
|  | 382 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; | 
|  | 383 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; | 
|  | 384 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; | 
|  | 385 | } | 
|  | 386 |  | 
| Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 387 | }; // namespace android |