Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "SurfaceTexture" |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 19 | |
| 20 | #define GL_GLEXT_PROTOTYPES |
| 21 | #define EGL_EGLEXT_PROTOTYPES |
| 22 | |
| 23 | #include <EGL/egl.h> |
| 24 | #include <EGL/eglext.h> |
| 25 | #include <GLES2/gl2.h> |
| 26 | #include <GLES2/gl2ext.h> |
| 27 | |
| 28 | #include <gui/SurfaceTexture.h> |
| 29 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 30 | #include <hardware/hardware.h> |
| 31 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/ISurfaceComposer.h> |
| 33 | #include <surfaceflinger/SurfaceComposerClient.h> |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 34 | #include <surfaceflinger/IGraphicBufferAlloc.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 35 | |
| 36 | #include <utils/Log.h> |
| 37 | |
| 38 | namespace android { |
| 39 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 40 | // Transform matrices |
| 41 | static float mtxIdentity[16] = { |
| 42 | 1, 0, 0, 0, |
| 43 | 0, 1, 0, 0, |
| 44 | 0, 0, 1, 0, |
| 45 | 0, 0, 0, 1, |
| 46 | }; |
| 47 | static float mtxFlipH[16] = { |
| 48 | -1, 0, 0, 0, |
| 49 | 0, 1, 0, 0, |
| 50 | 0, 0, 1, 0, |
| 51 | 1, 0, 0, 1, |
| 52 | }; |
| 53 | static float mtxFlipV[16] = { |
| 54 | 1, 0, 0, 0, |
| 55 | 0, -1, 0, 0, |
| 56 | 0, 0, 1, 0, |
| 57 | 0, 1, 0, 1, |
| 58 | }; |
| 59 | static float mtxRot90[16] = { |
| 60 | 0, 1, 0, 0, |
| 61 | -1, 0, 0, 0, |
| 62 | 0, 0, 1, 0, |
| 63 | 1, 0, 0, 1, |
| 64 | }; |
| 65 | static float mtxRot180[16] = { |
| 66 | -1, 0, 0, 0, |
| 67 | 0, -1, 0, 0, |
| 68 | 0, 0, 1, 0, |
| 69 | 1, 1, 0, 1, |
| 70 | }; |
| 71 | static float mtxRot270[16] = { |
| 72 | 0, -1, 0, 0, |
| 73 | 1, 0, 0, 0, |
| 74 | 0, 0, 1, 0, |
| 75 | 0, 1, 0, 1, |
| 76 | }; |
| 77 | |
| 78 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 79 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 80 | SurfaceTexture::SurfaceTexture(GLuint tex) : |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 81 | mDefaultWidth(1), |
| 82 | mDefaultHeight(1), |
| 83 | mPixelFormat(PIXEL_FORMAT_RGBA_8888), |
| 84 | mUseDefaultSize(true), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 85 | mBufferCount(MIN_BUFFER_SLOTS), |
| 86 | mCurrentTexture(INVALID_BUFFER_SLOT), |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 87 | mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 88 | mCurrentTransform(0), |
| 89 | mCurrentTimestamp(0), |
| 90 | mLastQueued(INVALID_BUFFER_SLOT), |
| 91 | mLastQueuedTransform(0), |
| 92 | mLastQueuedTimestamp(0), |
| 93 | mNextTransform(0), |
| 94 | mTexName(tex) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 95 | LOGV("SurfaceTexture::SurfaceTexture"); |
Jamie Gennis | 3461e0f | 2011-01-07 16:05:47 -0800 | [diff] [blame] | 96 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 97 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 98 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 99 | mSlots[i].mOwnedByClient = false; |
| 100 | } |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 101 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 102 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
Mathias Agopian | cc57d6f | 2011-04-27 18:57:33 -0700 | [diff] [blame^] | 103 | mNextCrop.makeInvalid(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 107 | LOGV("SurfaceTexture::~SurfaceTexture"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 108 | freeAllBuffers(); |
| 109 | } |
| 110 | |
| 111 | status_t SurfaceTexture::setBufferCount(int bufferCount) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 112 | LOGV("SurfaceTexture::setBufferCount"); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 113 | |
| 114 | if (bufferCount < MIN_BUFFER_SLOTS) { |
| 115 | return BAD_VALUE; |
| 116 | } |
| 117 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 118 | Mutex::Autolock lock(mMutex); |
| 119 | freeAllBuffers(); |
| 120 | mBufferCount = bufferCount; |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 121 | mCurrentTexture = INVALID_BUFFER_SLOT; |
| 122 | mLastQueued = INVALID_BUFFER_SLOT; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 123 | return OK; |
| 124 | } |
| 125 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 126 | status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 127 | { |
| 128 | Mutex::Autolock lock(mMutex); |
| 129 | if ((w != mDefaultWidth) || (h != mDefaultHeight)) { |
| 130 | mDefaultWidth = w; |
| 131 | mDefaultHeight = h; |
| 132 | } |
| 133 | return OK; |
| 134 | } |
| 135 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 136 | sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf, |
| 137 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 138 | LOGV("SurfaceTexture::requestBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 139 | Mutex::Autolock lock(mMutex); |
| 140 | if (buf < 0 || mBufferCount <= buf) { |
| 141 | LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
| 142 | mBufferCount, buf); |
| 143 | return 0; |
| 144 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 145 | if ((w && !h) || (!w & h)) { |
| 146 | LOGE("requestBuffer: invalid size: w=%u, h=%u: %d", w, h, buf); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | const bool useDefaultSize = !w && !h; |
| 151 | if (useDefaultSize) { |
| 152 | // use the default size |
| 153 | w = mDefaultWidth; |
| 154 | h = mDefaultHeight; |
| 155 | } |
| 156 | |
| 157 | const bool updateFormat = (format != 0); |
| 158 | if (!updateFormat) { |
| 159 | // keep the current (or default) format |
| 160 | format = mPixelFormat; |
| 161 | } |
| 162 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 163 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 164 | sp<GraphicBuffer> graphicBuffer( |
| 165 | mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage)); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 166 | if (graphicBuffer == 0) { |
| 167 | LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed"); |
| 168 | } else { |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 169 | mUseDefaultSize = useDefaultSize; |
| 170 | if (updateFormat) { |
| 171 | mPixelFormat = format; |
| 172 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 173 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
| 174 | if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { |
| 175 | eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage); |
| 176 | mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; |
| 177 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 178 | } |
| 179 | } |
| 180 | return graphicBuffer; |
| 181 | } |
| 182 | |
| 183 | status_t SurfaceTexture::dequeueBuffer(int *buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 184 | LOGV("SurfaceTexture::dequeueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 185 | Mutex::Autolock lock(mMutex); |
| 186 | int found = INVALID_BUFFER_SLOT; |
| 187 | for (int i = 0; i < mBufferCount; i++) { |
Jamie Gennis | 235c424 | 2011-01-11 15:00:09 -0800 | [diff] [blame] | 188 | if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 189 | mSlots[i].mOwnedByClient = true; |
| 190 | found = i; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | if (found == INVALID_BUFFER_SLOT) { |
| 195 | return -EBUSY; |
| 196 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 197 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 198 | *buf = found; |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 199 | |
| 200 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
| 201 | if (buffer == NULL) { |
| 202 | return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION; |
| 203 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 204 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 205 | if ((mUseDefaultSize) && |
| 206 | ((uint32_t(buffer->width) != mDefaultWidth) || |
| 207 | (uint32_t(buffer->height) != mDefaultHeight))) { |
| 208 | return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION; |
| 209 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 210 | return OK; |
| 211 | } |
| 212 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 213 | status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 214 | LOGV("SurfaceTexture::queueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 215 | Mutex::Autolock lock(mMutex); |
| 216 | if (buf < 0 || mBufferCount <= buf) { |
| 217 | LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 218 | mBufferCount, buf); |
| 219 | return -EINVAL; |
| 220 | } else if (!mSlots[buf].mOwnedByClient) { |
| 221 | LOGE("queueBuffer: slot %d is not owned by the client", buf); |
| 222 | return -EINVAL; |
| 223 | } else if (mSlots[buf].mGraphicBuffer == 0) { |
| 224 | LOGE("queueBuffer: slot %d was enqueued without requesting a buffer", |
| 225 | buf); |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | mSlots[buf].mOwnedByClient = false; |
| 229 | mLastQueued = buf; |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 230 | mLastQueuedCrop = mNextCrop; |
| 231 | mLastQueuedTransform = mNextTransform; |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 232 | mLastQueuedTimestamp = timestamp; |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 233 | if (mFrameAvailableListener != 0) { |
| 234 | mFrameAvailableListener->onFrameAvailable(); |
| 235 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 236 | return OK; |
| 237 | } |
| 238 | |
| 239 | void SurfaceTexture::cancelBuffer(int buf) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 240 | LOGV("SurfaceTexture::cancelBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 241 | Mutex::Autolock lock(mMutex); |
| 242 | if (buf < 0 || mBufferCount <= buf) { |
| 243 | LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount, |
| 244 | buf); |
| 245 | return; |
| 246 | } else if (!mSlots[buf].mOwnedByClient) { |
| 247 | LOGE("cancelBuffer: slot %d is not owned by the client", buf); |
| 248 | return; |
| 249 | } |
| 250 | mSlots[buf].mOwnedByClient = false; |
| 251 | } |
| 252 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 253 | status_t SurfaceTexture::setCrop(const Rect& crop) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 254 | LOGV("SurfaceTexture::setCrop"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 255 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 256 | mNextCrop = crop; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 257 | return OK; |
| 258 | } |
| 259 | |
| 260 | status_t SurfaceTexture::setTransform(uint32_t transform) { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 261 | LOGV("SurfaceTexture::setTransform"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 262 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 263 | mNextTransform = transform; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 264 | return OK; |
| 265 | } |
| 266 | |
| 267 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 268 | LOGV("SurfaceTexture::updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 269 | Mutex::Autolock lock(mMutex); |
| 270 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 271 | // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT, |
| 272 | // so this check will fail until a buffer gets queued. |
| 273 | if (mCurrentTexture != mLastQueued) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 274 | // Update the GL texture object. |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 275 | EGLImageKHR image = mSlots[mLastQueued].mEglImage; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 276 | if (image == EGL_NO_IMAGE_KHR) { |
| 277 | EGLDisplay dpy = eglGetCurrentDisplay(); |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 278 | image = createImage(dpy, mSlots[mLastQueued].mGraphicBuffer); |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 279 | mSlots[mLastQueued].mEglImage = image; |
| 280 | mSlots[mLastQueued].mEglDisplay = dpy; |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 281 | if (image == EGL_NO_IMAGE_KHR) { |
| 282 | // NOTE: if dpy was invalid, createImage() is guaranteed to |
| 283 | // fail. so we'd end up here. |
| 284 | return -EINVAL; |
| 285 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 286 | } |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 287 | |
| 288 | GLint error; |
| 289 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 290 | LOGE("GL error cleared before updating SurfaceTexture: %#04x", error); |
| 291 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 292 | |
| 293 | GLenum target = getTextureTarget( |
| 294 | mSlots[mLastQueued].mGraphicBuffer->format); |
| 295 | if (target != mCurrentTextureTarget) { |
| 296 | glDeleteTextures(1, &mTexName); |
| 297 | } |
| 298 | glBindTexture(target, mTexName); |
| 299 | glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image); |
| 300 | |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 301 | bool failed = false; |
| 302 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 303 | LOGE("error binding external texture image %p (slot %d): %#04x", |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 304 | image, mLastQueued, error); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 305 | failed = true; |
| 306 | } |
| 307 | if (failed) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 308 | return -EINVAL; |
| 309 | } |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 310 | |
| 311 | // Update the SurfaceTexture state. |
| 312 | mCurrentTexture = mLastQueued; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 313 | mCurrentTextureTarget = target; |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 314 | mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer; |
| 315 | mCurrentCrop = mLastQueuedCrop; |
| 316 | mCurrentTransform = mLastQueuedTransform; |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 317 | mCurrentTimestamp = mLastQueuedTimestamp; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 318 | } else { |
| 319 | // We always bind the texture even if we don't update its contents. |
| 320 | glBindTexture(mCurrentTextureTarget, mTexName); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 321 | } |
| 322 | return OK; |
| 323 | } |
| 324 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 325 | bool SurfaceTexture::isExternalFormat(uint32_t format) |
| 326 | { |
| 327 | switch (format) { |
| 328 | // supported YUV formats |
| 329 | case HAL_PIXEL_FORMAT_YV12: |
| 330 | // Legacy/deprecated YUV formats |
| 331 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 332 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 333 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | // Any OEM format needs to be considered |
| 338 | if (format>=0x100 && format<=0x1FF) |
| 339 | return true; |
| 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | GLenum SurfaceTexture::getTextureTarget(uint32_t format) |
| 345 | { |
| 346 | GLenum target = GL_TEXTURE_2D; |
| 347 | #if defined(GL_OES_EGL_image_external) |
| 348 | if (isExternalFormat(format)) { |
| 349 | target = GL_TEXTURE_EXTERNAL_OES; |
| 350 | } |
| 351 | #endif |
| 352 | return target; |
| 353 | } |
| 354 | |
| 355 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
| 356 | Mutex::Autolock lock(mMutex); |
| 357 | return mCurrentTextureTarget; |
| 358 | } |
| 359 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 360 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 361 | LOGV("SurfaceTexture::getTransformMatrix"); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 362 | Mutex::Autolock lock(mMutex); |
| 363 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 364 | float xform[16]; |
| 365 | for (int i = 0; i < 16; i++) { |
| 366 | xform[i] = mtxIdentity[i]; |
| 367 | } |
| 368 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 369 | float result[16]; |
| 370 | mtxMul(result, xform, mtxFlipH); |
| 371 | for (int i = 0; i < 16; i++) { |
| 372 | xform[i] = result[i]; |
| 373 | } |
| 374 | } |
| 375 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 376 | float result[16]; |
| 377 | mtxMul(result, xform, mtxFlipV); |
| 378 | for (int i = 0; i < 16; i++) { |
| 379 | xform[i] = result[i]; |
| 380 | } |
| 381 | } |
| 382 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 383 | float result[16]; |
| 384 | mtxMul(result, xform, mtxRot90); |
| 385 | for (int i = 0; i < 16; i++) { |
| 386 | xform[i] = result[i]; |
| 387 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 391 | float tx, ty, sx, sy; |
| 392 | if (!mCurrentCrop.isEmpty()) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 393 | // In order to prevent bilinear sampling at the of the crop rectangle we |
| 394 | // may need to shrink it by 2 texels in each direction. Normally this |
| 395 | // would just need to take 1/2 a texel off each end, but because the |
| 396 | // chroma channels will likely be subsampled we need to chop off a whole |
| 397 | // texel. This will cause artifacts if someone does nearest sampling |
| 398 | // with 1:1 pixel:texel ratio, but it's impossible to simultaneously |
| 399 | // accomodate the bilinear and nearest sampling uses. |
| 400 | // |
| 401 | // If nearest sampling turns out to be a desirable usage of these |
| 402 | // textures then we could add the ability to switch a SurfaceTexture to |
| 403 | // nearest-mode. Preferably, however, the image producers (video |
| 404 | // decoder, camera, etc.) would simply not use a crop rectangle (or at |
| 405 | // least not tell the framework about it) so that the GPU can do the |
| 406 | // correct edge behavior. |
| 407 | int xshrink = 0, yshrink = 0; |
| 408 | if (mCurrentCrop.left > 0) { |
| 409 | tx = float(mCurrentCrop.left + 1) / float(buf->getWidth()); |
| 410 | xshrink++; |
| 411 | } else { |
| 412 | tx = 0.0f; |
| 413 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 414 | if (mCurrentCrop.right < int32_t(buf->getWidth())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 415 | xshrink++; |
| 416 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 417 | if (mCurrentCrop.bottom < int32_t(buf->getHeight())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 418 | ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) / |
| 419 | float(buf->getHeight()); |
| 420 | yshrink++; |
| 421 | } else { |
| 422 | ty = 0.0f; |
| 423 | } |
| 424 | if (mCurrentCrop.top > 0) { |
| 425 | yshrink++; |
| 426 | } |
| 427 | sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth()); |
| 428 | sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight()); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 429 | } else { |
| 430 | tx = 0.0f; |
| 431 | ty = 0.0f; |
| 432 | sx = 1.0f; |
| 433 | sy = 1.0f; |
| 434 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 435 | float crop[16] = { |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 436 | sx, 0, 0, 0, |
| 437 | 0, sy, 0, 0, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 438 | 0, 0, 1, 0, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 439 | tx, ty, 0, 1, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 440 | }; |
| 441 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 442 | float mtxBeforeFlipV[16]; |
| 443 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 444 | |
| 445 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 446 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 447 | // want to expose this to applications, however, so we must add an |
| 448 | // additional vertical flip to the transform after all the other transforms. |
| 449 | mtxMul(mtx, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 452 | nsecs_t SurfaceTexture::getTimestamp() { |
| 453 | LOGV("SurfaceTexture::getTimestamp"); |
| 454 | Mutex::Autolock lock(mMutex); |
| 455 | return mCurrentTimestamp; |
| 456 | } |
| 457 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 458 | void SurfaceTexture::setFrameAvailableListener( |
| 459 | const sp<FrameAvailableListener>& l) { |
| 460 | LOGV("SurfaceTexture::setFrameAvailableListener"); |
| 461 | Mutex::Autolock lock(mMutex); |
| 462 | mFrameAvailableListener = l; |
| 463 | } |
| 464 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 465 | sp<IBinder> SurfaceTexture::getAllocator() { |
| 466 | LOGV("SurfaceTexture::getAllocator"); |
| 467 | return mGraphicBufferAlloc->asBinder(); |
| 468 | } |
| 469 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 470 | void SurfaceTexture::freeAllBuffers() { |
| 471 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 472 | mSlots[i].mGraphicBuffer = 0; |
| 473 | mSlots[i].mOwnedByClient = false; |
| 474 | if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { |
| 475 | eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage); |
| 476 | mSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 477 | mSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 483 | const sp<GraphicBuffer>& graphicBuffer) { |
| 484 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 485 | EGLint attrs[] = { |
| 486 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 487 | EGL_NONE, |
| 488 | }; |
| 489 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 490 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 491 | if (image == EGL_NO_IMAGE_KHR) { |
| 492 | EGLint error = eglGetError(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 493 | LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 494 | } |
| 495 | return image; |
| 496 | } |
| 497 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 498 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 499 | Mutex::Autolock lock(mMutex); |
| 500 | return mCurrentTextureBuf; |
| 501 | } |
| 502 | |
| 503 | Rect SurfaceTexture::getCurrentCrop() const { |
| 504 | Mutex::Autolock lock(mMutex); |
| 505 | return mCurrentCrop; |
| 506 | } |
| 507 | |
| 508 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 509 | Mutex::Autolock lock(mMutex); |
| 510 | return mCurrentTransform; |
| 511 | } |
| 512 | |
| 513 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 514 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 515 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 516 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 517 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 518 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 519 | |
| 520 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 521 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 522 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 523 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 524 | |
| 525 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 526 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 527 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 528 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 529 | |
| 530 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 531 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 532 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 533 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 534 | } |
| 535 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 536 | }; // namespace android |