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> |
| 32 | |
| 33 | #include <utils/Log.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 37 | // Transform matrices |
| 38 | static float mtxIdentity[16] = { |
| 39 | 1, 0, 0, 0, |
| 40 | 0, 1, 0, 0, |
| 41 | 0, 0, 1, 0, |
| 42 | 0, 0, 0, 1, |
| 43 | }; |
| 44 | static float mtxFlipH[16] = { |
| 45 | -1, 0, 0, 0, |
| 46 | 0, 1, 0, 0, |
| 47 | 0, 0, 1, 0, |
| 48 | 1, 0, 0, 1, |
| 49 | }; |
| 50 | static float mtxFlipV[16] = { |
| 51 | 1, 0, 0, 0, |
| 52 | 0, -1, 0, 0, |
| 53 | 0, 0, 1, 0, |
| 54 | 0, 1, 0, 1, |
| 55 | }; |
| 56 | static float mtxRot90[16] = { |
| 57 | 0, 1, 0, 0, |
| 58 | -1, 0, 0, 0, |
| 59 | 0, 0, 1, 0, |
| 60 | 1, 0, 0, 1, |
| 61 | }; |
| 62 | static float mtxRot180[16] = { |
| 63 | -1, 0, 0, 0, |
| 64 | 0, -1, 0, 0, |
| 65 | 0, 0, 1, 0, |
| 66 | 1, 1, 0, 1, |
| 67 | }; |
| 68 | static float mtxRot270[16] = { |
| 69 | 0, -1, 0, 0, |
| 70 | 1, 0, 0, 0, |
| 71 | 0, 0, 1, 0, |
| 72 | 0, 1, 0, 1, |
| 73 | }; |
| 74 | |
| 75 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 76 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 77 | SurfaceTexture::SurfaceTexture(GLuint tex) : |
| 78 | mBufferCount(MIN_BUFFER_SLOTS), mCurrentTexture(INVALID_BUFFER_SLOT), |
| 79 | mLastQueued(INVALID_BUFFER_SLOT), mTexName(tex) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 80 | LOGV("SurfaceTexture::SurfaceTexture"); |
Jamie Gennis | 3461e0f | 2011-01-07 16:05:47 -0800 | [diff] [blame] | 81 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 82 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 83 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 84 | mSlots[i].mOwnedByClient = false; |
| 85 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 89 | LOGV("SurfaceTexture::~SurfaceTexture"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 90 | freeAllBuffers(); |
| 91 | } |
| 92 | |
| 93 | status_t SurfaceTexture::setBufferCount(int bufferCount) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 94 | LOGV("SurfaceTexture::setBufferCount"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 95 | Mutex::Autolock lock(mMutex); |
| 96 | freeAllBuffers(); |
| 97 | mBufferCount = bufferCount; |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 98 | mCurrentTexture = INVALID_BUFFER_SLOT; |
| 99 | mLastQueued = INVALID_BUFFER_SLOT; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 100 | return OK; |
| 101 | } |
| 102 | |
| 103 | sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf, |
| 104 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 105 | LOGV("SurfaceTexture::requestBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 106 | Mutex::Autolock lock(mMutex); |
| 107 | if (buf < 0 || mBufferCount <= buf) { |
| 108 | LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
| 109 | mBufferCount, buf); |
| 110 | return 0; |
| 111 | } |
| 112 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 113 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 114 | sp<GraphicBuffer> graphicBuffer(composer->createGraphicBuffer(w, h, |
| 115 | format, usage)); |
| 116 | if (graphicBuffer == 0) { |
| 117 | LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed"); |
| 118 | } else { |
| 119 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
| 120 | if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { |
| 121 | eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage); |
| 122 | mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; |
| 123 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 124 | } |
| 125 | } |
| 126 | return graphicBuffer; |
| 127 | } |
| 128 | |
| 129 | status_t SurfaceTexture::dequeueBuffer(int *buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 130 | LOGV("SurfaceTexture::dequeueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 131 | Mutex::Autolock lock(mMutex); |
| 132 | int found = INVALID_BUFFER_SLOT; |
| 133 | for (int i = 0; i < mBufferCount; i++) { |
Jamie Gennis | 235c424 | 2011-01-11 15:00:09 -0800 | [diff] [blame^] | 134 | if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 135 | mSlots[i].mOwnedByClient = true; |
| 136 | found = i; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | if (found == INVALID_BUFFER_SLOT) { |
| 141 | return -EBUSY; |
| 142 | } |
| 143 | *buf = found; |
| 144 | return OK; |
| 145 | } |
| 146 | |
| 147 | status_t SurfaceTexture::queueBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 148 | LOGV("SurfaceTexture::queueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 149 | Mutex::Autolock lock(mMutex); |
| 150 | if (buf < 0 || mBufferCount <= buf) { |
| 151 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 152 | mBufferCount, buf); |
| 153 | return -EINVAL; |
| 154 | } else if (!mSlots[buf].mOwnedByClient) { |
| 155 | LOGE("queueBuffer: slot %d is not owned by the client", buf); |
| 156 | return -EINVAL; |
| 157 | } else if (mSlots[buf].mGraphicBuffer == 0) { |
| 158 | LOGE("queueBuffer: slot %d was enqueued without requesting a buffer", |
| 159 | buf); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | mSlots[buf].mOwnedByClient = false; |
| 163 | mLastQueued = buf; |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 164 | mLastQueuedCrop = mNextCrop; |
| 165 | mLastQueuedTransform = mNextTransform; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 166 | return OK; |
| 167 | } |
| 168 | |
| 169 | void SurfaceTexture::cancelBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 170 | LOGV("SurfaceTexture::cancelBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 171 | Mutex::Autolock lock(mMutex); |
| 172 | if (buf < 0 || mBufferCount <= buf) { |
| 173 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount, |
| 174 | buf); |
| 175 | return; |
| 176 | } else if (!mSlots[buf].mOwnedByClient) { |
| 177 | LOGE("cancelBuffer: slot %d is not owned by the client", buf); |
| 178 | return; |
| 179 | } |
| 180 | mSlots[buf].mOwnedByClient = false; |
| 181 | } |
| 182 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 183 | status_t SurfaceTexture::setCrop(const Rect& crop) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 184 | LOGV("SurfaceTexture::setCrop"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 185 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 186 | mNextCrop = crop; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 187 | return OK; |
| 188 | } |
| 189 | |
| 190 | status_t SurfaceTexture::setTransform(uint32_t transform) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 191 | LOGV("SurfaceTexture::setTransform"); |
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 | mNextTransform = transform; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 194 | return OK; |
| 195 | } |
| 196 | |
| 197 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 198 | LOGV("SurfaceTexture::updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 199 | Mutex::Autolock lock(mMutex); |
| 200 | |
| 201 | // We always bind the texture even if we don't update its contents. |
| 202 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName); |
| 203 | |
| 204 | // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT, |
| 205 | // so this check will fail until a buffer gets queued. |
| 206 | if (mCurrentTexture != mLastQueued) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 207 | // Update the SurfaceTexture state. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 208 | mCurrentTexture = mLastQueued; |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 209 | mCurrentCrop = mLastQueuedCrop; |
| 210 | mCurrentTransform = mLastQueuedTransform; |
| 211 | |
| 212 | // Update the GL texture object. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 213 | EGLImageKHR image = mSlots[mCurrentTexture].mEglImage; |
| 214 | if (image == EGL_NO_IMAGE_KHR) { |
| 215 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 216 | sp<GraphicBuffer> graphicBuffer = mSlots[mCurrentTexture].mGraphicBuffer; |
| 217 | image = createImage(dpy, graphicBuffer); |
| 218 | mSlots[mCurrentTexture].mEglImage = image; |
| 219 | mSlots[mCurrentTexture].mEglDisplay = dpy; |
| 220 | } |
| 221 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image); |
| 222 | GLint error = glGetError(); |
| 223 | if (error != GL_NO_ERROR) { |
| 224 | LOGE("error binding external texture image %p (slot %d): %#04x", |
| 225 | image, mCurrentTexture, error); |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | } |
| 229 | return OK; |
| 230 | } |
| 231 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 232 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
| 233 | LOGV("SurfaceTexture::updateTexImage"); |
| 234 | Mutex::Autolock lock(mMutex); |
| 235 | |
| 236 | float* xform = mtxIdentity; |
| 237 | switch (mCurrentTransform) { |
| 238 | case 0: |
| 239 | xform = mtxIdentity; |
| 240 | break; |
| 241 | case NATIVE_WINDOW_TRANSFORM_FLIP_H: |
| 242 | xform = mtxFlipH; |
| 243 | break; |
| 244 | case NATIVE_WINDOW_TRANSFORM_FLIP_V: |
| 245 | xform = mtxFlipV; |
| 246 | break; |
| 247 | case NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 248 | xform = mtxRot90; |
| 249 | break; |
| 250 | case NATIVE_WINDOW_TRANSFORM_ROT_180: |
| 251 | xform = mtxRot180; |
| 252 | break; |
| 253 | case NATIVE_WINDOW_TRANSFORM_ROT_270: |
| 254 | xform = mtxRot270; |
| 255 | break; |
| 256 | default: |
| 257 | LOGE("getTransformMatrix: unknown transform: %d", mCurrentTransform); |
| 258 | } |
| 259 | |
| 260 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); |
| 261 | float tx = float(mCurrentCrop.left) / float(buf->getWidth()); |
| 262 | float ty = float(mCurrentCrop.bottom) / float(buf->getHeight()); |
| 263 | float sx = float(mCurrentCrop.width()) / float(buf->getWidth()); |
| 264 | float sy = float(mCurrentCrop.height()) / float(buf->getHeight()); |
| 265 | float crop[16] = { |
| 266 | sx, 0, 0, sx*tx, |
| 267 | 0, sy, 0, sy*ty, |
| 268 | 0, 0, 1, 0, |
| 269 | 0, 0, 0, 1, |
| 270 | }; |
| 271 | |
| 272 | mtxMul(mtx, crop, xform); |
| 273 | } |
| 274 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 275 | void SurfaceTexture::freeAllBuffers() { |
| 276 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 277 | mSlots[i].mGraphicBuffer = 0; |
| 278 | mSlots[i].mOwnedByClient = false; |
| 279 | if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { |
| 280 | eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage); |
| 281 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 282 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 288 | const sp<GraphicBuffer>& graphicBuffer) { |
| 289 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 290 | EGLint attrs[] = { |
| 291 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 292 | EGL_NONE, |
| 293 | }; |
| 294 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 295 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
| 296 | EGLint error = eglGetError(); |
| 297 | if (error != EGL_SUCCESS) { |
| 298 | LOGE("error creating EGLImage: %#x", error); |
| 299 | } else if (image == EGL_NO_IMAGE_KHR) { |
| 300 | LOGE("no error reported, but no image was returned by " |
| 301 | "eglCreateImageKHR"); |
| 302 | } |
| 303 | return image; |
| 304 | } |
| 305 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 306 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 307 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 308 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 309 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 310 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 311 | |
| 312 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 313 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 314 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 315 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 316 | |
| 317 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 318 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 319 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 320 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 321 | |
| 322 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 323 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 324 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 325 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 326 | } |
| 327 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 328 | }; // namespace android |