Chia-I Wu | f140518 | 2017-11-27 11:29:21 -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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "BufferLayerConsumer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include "BufferLayerConsumer.h" |
| 23 | |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include <EGL/egl.h> |
| 27 | #include <EGL/eglext.h> |
| 28 | #include <GLES2/gl2.h> |
| 29 | #include <GLES2/gl2ext.h> |
| 30 | #include <cutils/compiler.h> |
| 31 | |
| 32 | #include <hardware/hardware.h> |
| 33 | |
| 34 | #include <math/mat4.h> |
| 35 | |
| 36 | #include <gui/BufferItem.h> |
| 37 | #include <gui/GLConsumer.h> |
| 38 | #include <gui/ISurfaceComposer.h> |
| 39 | #include <gui/SurfaceComposerClient.h> |
| 40 | |
| 41 | #include <private/gui/ComposerService.h> |
| 42 | #include <private/gui/SyncFeatures.h> |
| 43 | |
| 44 | #include <utils/Log.h> |
| 45 | #include <utils/String8.h> |
| 46 | #include <utils/Trace.h> |
| 47 | |
| 48 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| 49 | #define CROP_EXT_STR "EGL_ANDROID_image_crop" |
| 50 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 51 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
| 52 | |
| 53 | namespace android { |
| 54 | |
| 55 | // Macros for including the BufferLayerConsumer name in log messages |
| 56 | #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 57 | #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 58 | //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 59 | #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 60 | #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 61 | |
| 62 | static const struct { |
| 63 | uint32_t width, height; |
| 64 | char const* bits; |
| 65 | } kDebugData = {15, 12, |
| 66 | "_______________" |
| 67 | "_______________" |
| 68 | "_____XX_XX_____" |
| 69 | "__X_X_____X_X__" |
| 70 | "__X_XXXXXXX_X__" |
| 71 | "__XXXXXXXXXXX__" |
| 72 | "___XX_XXX_XX___" |
| 73 | "____XXXXXXX____" |
| 74 | "_____X___X_____" |
| 75 | "____X_____X____" |
| 76 | "_______________" |
| 77 | "_______________"}; |
| 78 | |
| 79 | static const mat4 mtxIdentity; |
| 80 | |
| 81 | Mutex BufferLayerConsumer::sStaticInitLock; |
| 82 | sp<GraphicBuffer> BufferLayerConsumer::sReleasedTexImageBuffer; |
| 83 | |
| 84 | static bool hasEglAndroidImageCropImpl() { |
| 85 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 86 | const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS); |
| 87 | size_t cropExtLen = strlen(CROP_EXT_STR); |
| 88 | size_t extsLen = strlen(exts); |
| 89 | bool equal = !strcmp(CROP_EXT_STR, exts); |
| 90 | bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen + 1); |
| 91 | bool atEnd = (cropExtLen + 1) < extsLen && |
| 92 | !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen + 1)); |
| 93 | bool inMiddle = strstr(exts, " " CROP_EXT_STR " "); |
| 94 | return equal || atStart || atEnd || inMiddle; |
| 95 | } |
| 96 | |
| 97 | static bool hasEglAndroidImageCrop() { |
| 98 | // Only compute whether the extension is present once the first time this |
| 99 | // function is called. |
| 100 | static bool hasIt = hasEglAndroidImageCropImpl(); |
| 101 | return hasIt; |
| 102 | } |
| 103 | |
| 104 | static bool hasEglProtectedContentImpl() { |
| 105 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 106 | const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); |
| 107 | size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR); |
| 108 | size_t extsLen = strlen(exts); |
| 109 | bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts); |
| 110 | bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen + 1); |
| 111 | bool atEnd = (cropExtLen + 1) < extsLen && |
| 112 | !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen + 1)); |
| 113 | bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " "); |
| 114 | return equal || atStart || atEnd || inMiddle; |
| 115 | } |
| 116 | |
| 117 | static bool hasEglProtectedContent() { |
| 118 | // Only compute whether the extension is present once the first time this |
| 119 | // function is called. |
| 120 | static bool hasIt = hasEglProtectedContentImpl(); |
| 121 | return hasIt; |
| 122 | } |
| 123 | |
| 124 | static bool isEglImageCroppable(const Rect& crop) { |
| 125 | return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0); |
| 126 | } |
| 127 | |
| 128 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, |
| 129 | uint32_t texTarget, bool useFenceSync, |
| 130 | bool isControlledByApp) |
| 131 | : ConsumerBase(bq, isControlledByApp), |
| 132 | mCurrentCrop(Rect::EMPTY_RECT), |
| 133 | mCurrentTransform(0), |
| 134 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 135 | mCurrentFence(Fence::NO_FENCE), |
| 136 | mCurrentTimestamp(0), |
| 137 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 138 | mCurrentFrameNumber(0), |
| 139 | mDefaultWidth(1), |
| 140 | mDefaultHeight(1), |
| 141 | mFilteringEnabled(true), |
| 142 | mTexName(tex), |
| 143 | mUseFenceSync(useFenceSync), |
| 144 | mTexTarget(texTarget), |
| 145 | mEglDisplay(EGL_NO_DISPLAY), |
| 146 | mEglContext(EGL_NO_CONTEXT), |
| 147 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 148 | mAttached(true) { |
| 149 | BLC_LOGV("BufferLayerConsumer"); |
| 150 | |
| 151 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 152 | |
| 153 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 154 | } |
| 155 | |
| 156 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget, |
| 157 | bool useFenceSync, bool isControlledByApp) |
| 158 | : ConsumerBase(bq, isControlledByApp), |
| 159 | mCurrentCrop(Rect::EMPTY_RECT), |
| 160 | mCurrentTransform(0), |
| 161 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 162 | mCurrentFence(Fence::NO_FENCE), |
| 163 | mCurrentTimestamp(0), |
| 164 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 165 | mCurrentFrameNumber(0), |
| 166 | mDefaultWidth(1), |
| 167 | mDefaultHeight(1), |
| 168 | mFilteringEnabled(true), |
| 169 | mTexName(0), |
| 170 | mUseFenceSync(useFenceSync), |
| 171 | mTexTarget(texTarget), |
| 172 | mEglDisplay(EGL_NO_DISPLAY), |
| 173 | mEglContext(EGL_NO_CONTEXT), |
| 174 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 175 | mAttached(false) { |
| 176 | BLC_LOGV("BufferLayerConsumer"); |
| 177 | |
| 178 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 179 | |
| 180 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 181 | } |
| 182 | |
| 183 | status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 184 | Mutex::Autolock lock(mMutex); |
| 185 | if (mAbandoned) { |
| 186 | BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!"); |
| 187 | return NO_INIT; |
| 188 | } |
| 189 | mDefaultWidth = w; |
| 190 | mDefaultHeight = h; |
| 191 | return mConsumer->setDefaultBufferSize(w, h); |
| 192 | } |
| 193 | |
| 194 | status_t BufferLayerConsumer::updateTexImage() { |
| 195 | ATRACE_CALL(); |
| 196 | BLC_LOGV("updateTexImage"); |
| 197 | Mutex::Autolock lock(mMutex); |
| 198 | |
| 199 | if (mAbandoned) { |
| 200 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 201 | return NO_INIT; |
| 202 | } |
| 203 | |
| 204 | // Make sure the EGL state is the same as in previous calls. |
| 205 | status_t err = checkAndUpdateEglStateLocked(); |
| 206 | if (err != NO_ERROR) { |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | BufferItem item; |
| 211 | |
| 212 | // Acquire the next buffer. |
| 213 | // In asynchronous mode the list is guaranteed to be one buffer |
| 214 | // deep, while in synchronous mode we use the oldest buffer. |
| 215 | err = acquireBufferLocked(&item, 0); |
| 216 | if (err != NO_ERROR) { |
| 217 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 218 | // We always bind the texture even if we don't update its contents. |
| 219 | BLC_LOGV("updateTexImage: no buffers were available"); |
| 220 | glBindTexture(mTexTarget, mTexName); |
| 221 | err = NO_ERROR; |
| 222 | } else { |
| 223 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 224 | } |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | // Release the previous buffer. |
| 229 | err = updateAndReleaseLocked(item); |
| 230 | if (err != NO_ERROR) { |
| 231 | // We always bind the texture. |
| 232 | glBindTexture(mTexTarget, mTexName); |
| 233 | return err; |
| 234 | } |
| 235 | |
| 236 | // Bind the new buffer to the GL texture, and wait until it's ready. |
| 237 | return bindTextureImageLocked(); |
| 238 | } |
| 239 | |
| 240 | status_t BufferLayerConsumer::releaseTexImage() { |
| 241 | ATRACE_CALL(); |
| 242 | BLC_LOGV("releaseTexImage"); |
| 243 | Mutex::Autolock lock(mMutex); |
| 244 | |
| 245 | if (mAbandoned) { |
| 246 | BLC_LOGE("releaseTexImage: BufferLayerConsumer is abandoned!"); |
| 247 | return NO_INIT; |
| 248 | } |
| 249 | |
| 250 | // Make sure the EGL state is the same as in previous calls. |
| 251 | status_t err = NO_ERROR; |
| 252 | |
| 253 | if (mAttached) { |
| 254 | err = checkAndUpdateEglStateLocked(true); |
| 255 | if (err != NO_ERROR) { |
| 256 | return err; |
| 257 | } |
| 258 | } else { |
| 259 | // if we're detached, no need to validate EGL's state -- we won't use it. |
| 260 | } |
| 261 | |
| 262 | // Update the BufferLayerConsumer state. |
| 263 | int buf = mCurrentTexture; |
| 264 | if (buf != BufferQueue::INVALID_BUFFER_SLOT) { |
| 265 | BLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached); |
| 266 | |
| 267 | if (mAttached) { |
| 268 | // Do whatever sync ops we need to do before releasing the slot. |
| 269 | err = syncForReleaseLocked(mEglDisplay); |
| 270 | if (err != NO_ERROR) { |
| 271 | BLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err); |
| 272 | return err; |
| 273 | } |
| 274 | } else { |
| 275 | // if we're detached, we just use the fence that was created in detachFromContext() |
| 276 | // so... basically, nothing more to do here. |
| 277 | } |
| 278 | |
| 279 | err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR); |
| 280 | if (err < NO_ERROR) { |
| 281 | BLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)", strerror(-err), err); |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | if (mReleasedTexImage == NULL) { |
| 286 | mReleasedTexImage = new EglImage(getDebugTexImageBuffer()); |
| 287 | } |
| 288 | |
| 289 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 290 | mCurrentTextureImage = mReleasedTexImage; |
| 291 | mCurrentCrop.makeInvalid(); |
| 292 | mCurrentTransform = 0; |
| 293 | mCurrentTimestamp = 0; |
| 294 | mCurrentDataSpace = HAL_DATASPACE_UNKNOWN; |
| 295 | mCurrentFence = Fence::NO_FENCE; |
| 296 | mCurrentFenceTime = FenceTime::NO_FENCE; |
| 297 | |
| 298 | if (mAttached) { |
| 299 | // This binds a dummy buffer (mReleasedTexImage). |
| 300 | status_t result = bindTextureImageLocked(); |
| 301 | if (result != NO_ERROR) { |
| 302 | return result; |
| 303 | } |
| 304 | } else { |
| 305 | // detached, don't touch the texture (and we may not even have an |
| 306 | // EGLDisplay here. |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return NO_ERROR; |
| 311 | } |
| 312 | |
| 313 | sp<GraphicBuffer> BufferLayerConsumer::getDebugTexImageBuffer() { |
| 314 | Mutex::Autolock _l(sStaticInitLock); |
| 315 | if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) { |
| 316 | // The first time, create the debug texture in case the application |
| 317 | // continues to use it. |
| 318 | sp<GraphicBuffer> buffer = |
| 319 | new GraphicBuffer(kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888, |
| 320 | GraphicBuffer::USAGE_SW_WRITE_RARELY, |
| 321 | "[BufferLayerConsumer debug texture]"); |
| 322 | uint32_t* bits; |
| 323 | buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits)); |
| 324 | uint32_t stride = buffer->getStride(); |
| 325 | uint32_t height = buffer->getHeight(); |
| 326 | memset(bits, 0, stride * height * 4); |
| 327 | for (uint32_t y = 0; y < kDebugData.height; y++) { |
| 328 | for (uint32_t x = 0; x < kDebugData.width; x++) { |
| 329 | bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ? 0xFF000000 |
| 330 | : 0xFFFFFFFF; |
| 331 | } |
| 332 | bits += stride; |
| 333 | } |
| 334 | buffer->unlock(); |
| 335 | sReleasedTexImageBuffer = buffer; |
| 336 | } |
| 337 | return sReleasedTexImageBuffer; |
| 338 | } |
| 339 | |
| 340 | status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 341 | uint64_t maxFrameNumber) { |
| 342 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber); |
| 343 | if (err != NO_ERROR) { |
| 344 | return err; |
| 345 | } |
| 346 | |
| 347 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
| 348 | // before, so any prior EglImage created is using a stale buffer. This |
| 349 | // replaces any old EglImage with a new one (using the new buffer). |
| 350 | if (item->mGraphicBuffer != NULL) { |
| 351 | int slot = item->mSlot; |
| 352 | mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer); |
| 353 | } |
| 354 | |
| 355 | return NO_ERROR; |
| 356 | } |
| 357 | |
| 358 | status_t BufferLayerConsumer::releaseBufferLocked(int buf, sp<GraphicBuffer> graphicBuffer, |
| 359 | EGLDisplay display, EGLSyncKHR eglFence) { |
| 360 | // release the buffer if it hasn't already been discarded by the |
| 361 | // BufferQueue. This can happen, for example, when the producer of this |
| 362 | // buffer has reallocated the original buffer slot after this buffer |
| 363 | // was acquired. |
| 364 | status_t err = ConsumerBase::releaseBufferLocked(buf, graphicBuffer, display, eglFence); |
| 365 | mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR; |
| 366 | return err; |
| 367 | } |
| 368 | |
| 369 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
| 370 | PendingRelease* pendingRelease) { |
| 371 | status_t err = NO_ERROR; |
| 372 | |
| 373 | int slot = item.mSlot; |
| 374 | |
| 375 | if (!mAttached) { |
| 376 | BLC_LOGE("updateAndRelease: BufferLayerConsumer is not attached to an OpenGL " |
| 377 | "ES context"); |
| 378 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR); |
| 379 | return INVALID_OPERATION; |
| 380 | } |
| 381 | |
| 382 | // Confirm state. |
| 383 | err = checkAndUpdateEglStateLocked(); |
| 384 | if (err != NO_ERROR) { |
| 385 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR); |
| 386 | return err; |
| 387 | } |
| 388 | |
| 389 | // Ensure we have a valid EglImageKHR for the slot, creating an EglImage |
| 390 | // if nessessary, for the gralloc buffer currently in the slot in |
| 391 | // ConsumerBase. |
| 392 | // We may have to do this even when item.mGraphicBuffer == NULL (which |
| 393 | // means the buffer was previously acquired). |
| 394 | err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop); |
| 395 | if (err != NO_ERROR) { |
| 396 | BLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", mEglDisplay, |
| 397 | slot); |
| 398 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR); |
| 399 | return UNKNOWN_ERROR; |
| 400 | } |
| 401 | |
| 402 | // Do whatever sync ops we need to do before releasing the old slot. |
| 403 | if (slot != mCurrentTexture) { |
| 404 | err = syncForReleaseLocked(mEglDisplay); |
| 405 | if (err != NO_ERROR) { |
| 406 | // Release the buffer we just acquired. It's not safe to |
| 407 | // release the old buffer, so instead we just drop the new frame. |
| 408 | // As we are still under lock since acquireBuffer, it is safe to |
| 409 | // release by slot. |
| 410 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR); |
| 411 | return err; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
| 416 | mCurrentTextureImage != NULL ? mCurrentTextureImage->graphicBufferHandle() : 0, slot, |
| 417 | mSlots[slot].mGraphicBuffer->handle); |
| 418 | |
| 419 | // Hang onto the pointer so that it isn't freed in the call to |
| 420 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 421 | // the same. |
| 422 | sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage; |
| 423 | |
| 424 | // release old buffer |
| 425 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 426 | if (pendingRelease == nullptr) { |
| 427 | status_t status = |
| 428 | releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer(), |
| 429 | mEglDisplay, mEglSlots[mCurrentTexture].mEglFence); |
| 430 | if (status < NO_ERROR) { |
| 431 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 432 | status); |
| 433 | err = status; |
| 434 | // keep going, with error raised [?] |
| 435 | } |
| 436 | } else { |
| 437 | pendingRelease->currentTexture = mCurrentTexture; |
| 438 | pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer(); |
| 439 | pendingRelease->display = mEglDisplay; |
| 440 | pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence; |
| 441 | pendingRelease->isPending = true; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // Update the BufferLayerConsumer state. |
| 446 | mCurrentTexture = slot; |
| 447 | mCurrentTextureImage = nextTextureImage; |
| 448 | mCurrentCrop = item.mCrop; |
| 449 | mCurrentTransform = item.mTransform; |
| 450 | mCurrentScalingMode = item.mScalingMode; |
| 451 | mCurrentTimestamp = item.mTimestamp; |
| 452 | mCurrentDataSpace = item.mDataSpace; |
| 453 | mCurrentFence = item.mFence; |
| 454 | mCurrentFenceTime = item.mFenceTime; |
| 455 | mCurrentFrameNumber = item.mFrameNumber; |
| 456 | |
| 457 | computeCurrentTransformMatrixLocked(); |
| 458 | |
| 459 | return err; |
| 460 | } |
| 461 | |
| 462 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
| 463 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 464 | ALOGE("bindTextureImage: invalid display"); |
| 465 | return INVALID_OPERATION; |
| 466 | } |
| 467 | |
| 468 | GLenum error; |
| 469 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 470 | BLC_LOGW("bindTextureImage: clearing GL error: %#04x", error); |
| 471 | } |
| 472 | |
| 473 | glBindTexture(mTexTarget, mTexName); |
| 474 | if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == NULL) { |
| 475 | BLC_LOGE("bindTextureImage: no currently-bound texture"); |
| 476 | return NO_INIT; |
| 477 | } |
| 478 | |
| 479 | status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay, mCurrentCrop); |
| 480 | if (err != NO_ERROR) { |
| 481 | BLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay, |
| 482 | mCurrentTexture); |
| 483 | return UNKNOWN_ERROR; |
| 484 | } |
| 485 | mCurrentTextureImage->bindToTextureTarget(mTexTarget); |
| 486 | |
| 487 | // In the rare case that the display is terminated and then initialized |
| 488 | // again, we can't detect that the display changed (it didn't), but the |
| 489 | // image is invalid. In this case, repeat the exact same steps while |
| 490 | // forcing the creation of a new image. |
| 491 | if ((error = glGetError()) != GL_NO_ERROR) { |
| 492 | glBindTexture(mTexTarget, mTexName); |
| 493 | status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, mCurrentCrop, true); |
| 494 | if (result != NO_ERROR) { |
| 495 | BLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay, |
| 496 | mCurrentTexture); |
| 497 | return UNKNOWN_ERROR; |
| 498 | } |
| 499 | mCurrentTextureImage->bindToTextureTarget(mTexTarget); |
| 500 | if ((error = glGetError()) != GL_NO_ERROR) { |
| 501 | BLC_LOGE("bindTextureImage: error binding external image: %#04x", error); |
| 502 | return UNKNOWN_ERROR; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Wait for the new buffer to be ready. |
| 507 | return doGLFenceWaitLocked(); |
| 508 | } |
| 509 | |
| 510 | status_t BufferLayerConsumer::checkAndUpdateEglStateLocked(bool contextCheck) { |
| 511 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 512 | EGLContext ctx = eglGetCurrentContext(); |
| 513 | |
| 514 | if (!contextCheck) { |
| 515 | // if this is the first time we're called, mEglDisplay/mEglContext have |
| 516 | // never been set, so don't error out (below). |
| 517 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 518 | mEglDisplay = dpy; |
| 519 | } |
| 520 | if (mEglContext == EGL_NO_CONTEXT) { |
| 521 | mEglContext = ctx; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) { |
| 526 | BLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay"); |
| 527 | return INVALID_OPERATION; |
| 528 | } |
| 529 | |
| 530 | if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) { |
| 531 | BLC_LOGE("checkAndUpdateEglState: invalid current EGLContext"); |
| 532 | return INVALID_OPERATION; |
| 533 | } |
| 534 | |
| 535 | mEglDisplay = dpy; |
| 536 | mEglContext = ctx; |
| 537 | return NO_ERROR; |
| 538 | } |
| 539 | |
| 540 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 541 | if (fence->isValid() && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 542 | status_t err = |
| 543 | addReleaseFence(mCurrentTexture, mCurrentTextureImage->graphicBuffer(), fence); |
| 544 | if (err != OK) { |
| 545 | BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | status_t BufferLayerConsumer::detachFromContext() { |
| 551 | ATRACE_CALL(); |
| 552 | BLC_LOGV("detachFromContext"); |
| 553 | Mutex::Autolock lock(mMutex); |
| 554 | |
| 555 | if (mAbandoned) { |
| 556 | BLC_LOGE("detachFromContext: abandoned BufferLayerConsumer"); |
| 557 | return NO_INIT; |
| 558 | } |
| 559 | |
| 560 | if (!mAttached) { |
| 561 | BLC_LOGE("detachFromContext: BufferLayerConsumer is not attached to a " |
| 562 | "context"); |
| 563 | return INVALID_OPERATION; |
| 564 | } |
| 565 | |
| 566 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 567 | EGLContext ctx = eglGetCurrentContext(); |
| 568 | |
| 569 | if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) { |
| 570 | BLC_LOGE("detachFromContext: invalid current EGLDisplay"); |
| 571 | return INVALID_OPERATION; |
| 572 | } |
| 573 | |
| 574 | if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) { |
| 575 | BLC_LOGE("detachFromContext: invalid current EGLContext"); |
| 576 | return INVALID_OPERATION; |
| 577 | } |
| 578 | |
| 579 | if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) { |
| 580 | status_t err = syncForReleaseLocked(dpy); |
| 581 | if (err != OK) { |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | glDeleteTextures(1, &mTexName); |
| 586 | } |
| 587 | |
| 588 | mEglDisplay = EGL_NO_DISPLAY; |
| 589 | mEglContext = EGL_NO_CONTEXT; |
| 590 | mAttached = false; |
| 591 | |
| 592 | return OK; |
| 593 | } |
| 594 | |
| 595 | status_t BufferLayerConsumer::attachToContext(uint32_t tex) { |
| 596 | ATRACE_CALL(); |
| 597 | BLC_LOGV("attachToContext"); |
| 598 | Mutex::Autolock lock(mMutex); |
| 599 | |
| 600 | if (mAbandoned) { |
| 601 | BLC_LOGE("attachToContext: abandoned BufferLayerConsumer"); |
| 602 | return NO_INIT; |
| 603 | } |
| 604 | |
| 605 | if (mAttached) { |
| 606 | BLC_LOGE("attachToContext: BufferLayerConsumer is already attached to a " |
| 607 | "context"); |
| 608 | return INVALID_OPERATION; |
| 609 | } |
| 610 | |
| 611 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 612 | EGLContext ctx = eglGetCurrentContext(); |
| 613 | |
| 614 | if (dpy == EGL_NO_DISPLAY) { |
| 615 | BLC_LOGE("attachToContext: invalid current EGLDisplay"); |
| 616 | return INVALID_OPERATION; |
| 617 | } |
| 618 | |
| 619 | if (ctx == EGL_NO_CONTEXT) { |
| 620 | BLC_LOGE("attachToContext: invalid current EGLContext"); |
| 621 | return INVALID_OPERATION; |
| 622 | } |
| 623 | |
| 624 | // We need to bind the texture regardless of whether there's a current |
| 625 | // buffer. |
| 626 | glBindTexture(mTexTarget, GLuint(tex)); |
| 627 | |
| 628 | mEglDisplay = dpy; |
| 629 | mEglContext = ctx; |
| 630 | mTexName = tex; |
| 631 | mAttached = true; |
| 632 | |
| 633 | if (mCurrentTextureImage != NULL) { |
| 634 | // This may wait for a buffer a second time. This is likely required if |
| 635 | // this is a different context, since otherwise the wait could be skipped |
| 636 | // by bouncing through another context. For the same context the extra |
| 637 | // wait is redundant. |
| 638 | status_t err = bindTextureImageLocked(); |
| 639 | if (err != NO_ERROR) { |
| 640 | return err; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return OK; |
| 645 | } |
| 646 | |
| 647 | status_t BufferLayerConsumer::syncForReleaseLocked(EGLDisplay dpy) { |
| 648 | BLC_LOGV("syncForReleaseLocked"); |
| 649 | |
| 650 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 651 | if (SyncFeatures::getInstance().useNativeFenceSync()) { |
| 652 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL); |
| 653 | if (sync == EGL_NO_SYNC_KHR) { |
| 654 | BLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", eglGetError()); |
| 655 | return UNKNOWN_ERROR; |
| 656 | } |
| 657 | glFlush(); |
| 658 | int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync); |
| 659 | eglDestroySyncKHR(dpy, sync); |
| 660 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 661 | BLC_LOGE("syncForReleaseLocked: error dup'ing native fence " |
| 662 | "fd: %#x", |
| 663 | eglGetError()); |
| 664 | return UNKNOWN_ERROR; |
| 665 | } |
| 666 | sp<Fence> fence(new Fence(fenceFd)); |
| 667 | status_t err = addReleaseFenceLocked(mCurrentTexture, |
| 668 | mCurrentTextureImage->graphicBuffer(), fence); |
| 669 | if (err != OK) { |
| 670 | BLC_LOGE("syncForReleaseLocked: error adding release fence: " |
| 671 | "%s (%d)", |
| 672 | strerror(-err), err); |
| 673 | return err; |
| 674 | } |
| 675 | } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) { |
| 676 | EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence; |
| 677 | if (fence != EGL_NO_SYNC_KHR) { |
| 678 | // There is already a fence for the current slot. We need to |
| 679 | // wait on that before replacing it with another fence to |
| 680 | // ensure that all outstanding buffer accesses have completed |
| 681 | // before the producer accesses it. |
| 682 | EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000); |
| 683 | if (result == EGL_FALSE) { |
| 684 | BLC_LOGE("syncForReleaseLocked: error waiting for previous " |
| 685 | "fence: %#x", |
| 686 | eglGetError()); |
| 687 | return UNKNOWN_ERROR; |
| 688 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 689 | BLC_LOGE("syncForReleaseLocked: timeout waiting for previous " |
| 690 | "fence"); |
| 691 | return TIMED_OUT; |
| 692 | } |
| 693 | eglDestroySyncKHR(dpy, fence); |
| 694 | } |
| 695 | |
| 696 | // Create a fence for the outstanding accesses in the current |
| 697 | // OpenGL ES context. |
| 698 | fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); |
| 699 | if (fence == EGL_NO_SYNC_KHR) { |
| 700 | BLC_LOGE("syncForReleaseLocked: error creating fence: %#x", eglGetError()); |
| 701 | return UNKNOWN_ERROR; |
| 702 | } |
| 703 | glFlush(); |
| 704 | mEglSlots[mCurrentTexture].mEglFence = fence; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | return OK; |
| 709 | } |
| 710 | |
| 711 | uint32_t BufferLayerConsumer::getCurrentTextureTarget() const { |
| 712 | return mTexTarget; |
| 713 | } |
| 714 | |
| 715 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 716 | Mutex::Autolock lock(mMutex); |
| 717 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 718 | } |
| 719 | |
| 720 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 721 | Mutex::Autolock lock(mMutex); |
| 722 | if (mAbandoned) { |
| 723 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 724 | return; |
| 725 | } |
| 726 | bool needsRecompute = mFilteringEnabled != enabled; |
| 727 | mFilteringEnabled = enabled; |
| 728 | |
| 729 | if (needsRecompute && mCurrentTextureImage == NULL) { |
| 730 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL"); |
| 731 | } |
| 732 | |
| 733 | if (needsRecompute && mCurrentTextureImage != NULL) { |
| 734 | computeCurrentTransformMatrixLocked(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 739 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
| 740 | sp<GraphicBuffer> buf = |
| 741 | (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer(); |
| 742 | if (buf == nullptr) { |
| 743 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
| 744 | "mCurrentTextureImage is NULL"); |
| 745 | } |
| 746 | computeTransformMatrix(mCurrentTransformMatrix, buf, |
| 747 | isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop, |
| 748 | mCurrentTransform, mFilteringEnabled); |
| 749 | } |
| 750 | |
| 751 | void BufferLayerConsumer::computeTransformMatrix(float outTransform[16], |
| 752 | const sp<GraphicBuffer>& buf, const Rect& cropRect, |
| 753 | uint32_t transform, bool filtering) { |
| 754 | // Transform matrices |
| 755 | static const mat4 mtxFlipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
| 756 | static const mat4 mtxFlipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1); |
| 757 | static const mat4 mtxRot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
| 758 | |
| 759 | mat4 xform; |
| 760 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 761 | xform *= mtxFlipH; |
| 762 | } |
| 763 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 764 | xform *= mtxFlipV; |
| 765 | } |
| 766 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 767 | xform *= mtxRot90; |
| 768 | } |
| 769 | |
| 770 | if (!cropRect.isEmpty()) { |
| 771 | float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f; |
| 772 | float bufferWidth = buf->getWidth(); |
| 773 | float bufferHeight = buf->getHeight(); |
| 774 | float shrinkAmount = 0.0f; |
| 775 | if (filtering) { |
| 776 | // In order to prevent bilinear sampling beyond the edge of the |
| 777 | // crop rectangle we may need to shrink it by 2 texels in each |
| 778 | // dimension. Normally this would just need to take 1/2 a texel |
| 779 | // off each end, but because the chroma channels of YUV420 images |
| 780 | // are subsampled we may need to shrink the crop region by a whole |
| 781 | // texel on each side. |
| 782 | switch (buf->getPixelFormat()) { |
| 783 | case PIXEL_FORMAT_RGBA_8888: |
| 784 | case PIXEL_FORMAT_RGBX_8888: |
| 785 | case PIXEL_FORMAT_RGBA_FP16: |
| 786 | case PIXEL_FORMAT_RGBA_1010102: |
| 787 | case PIXEL_FORMAT_RGB_888: |
| 788 | case PIXEL_FORMAT_RGB_565: |
| 789 | case PIXEL_FORMAT_BGRA_8888: |
| 790 | // We know there's no subsampling of any channels, so we |
| 791 | // only need to shrink by a half a pixel. |
| 792 | shrinkAmount = 0.5; |
| 793 | break; |
| 794 | |
| 795 | default: |
| 796 | // If we don't recognize the format, we must assume the |
| 797 | // worst case (that we care about), which is YUV420. |
| 798 | shrinkAmount = 1.0; |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | // Only shrink the dimensions that are not the size of the buffer. |
| 804 | if (cropRect.width() < bufferWidth) { |
| 805 | tx = (float(cropRect.left) + shrinkAmount) / bufferWidth; |
| 806 | sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) / bufferWidth; |
| 807 | } |
| 808 | if (cropRect.height() < bufferHeight) { |
| 809 | ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) / bufferHeight; |
| 810 | sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) / bufferHeight; |
| 811 | } |
| 812 | |
| 813 | mat4 crop(sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1); |
| 814 | xform = crop * xform; |
| 815 | } |
| 816 | |
| 817 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 818 | // coordinate of 0, so BufferLayerConsumer must behave the same way. We don't |
| 819 | // want to expose this to applications, however, so we must add an |
| 820 | // additional vertical flip to the transform after all the other transforms. |
| 821 | xform = mtxFlipV * xform; |
| 822 | |
| 823 | memcpy(outTransform, xform.asArray(), sizeof(xform)); |
| 824 | } |
| 825 | |
| 826 | Rect BufferLayerConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, |
| 827 | uint32_t bufferHeight) { |
| 828 | Rect outCrop = crop; |
| 829 | |
| 830 | uint32_t newWidth = static_cast<uint32_t>(crop.width()); |
| 831 | uint32_t newHeight = static_cast<uint32_t>(crop.height()); |
| 832 | |
| 833 | if (newWidth * bufferHeight > newHeight * bufferWidth) { |
| 834 | newWidth = newHeight * bufferWidth / bufferHeight; |
| 835 | ALOGV("too wide: newWidth = %d", newWidth); |
| 836 | } else if (newWidth * bufferHeight < newHeight * bufferWidth) { |
| 837 | newHeight = newWidth * bufferHeight / bufferWidth; |
| 838 | ALOGV("too tall: newHeight = %d", newHeight); |
| 839 | } |
| 840 | |
| 841 | uint32_t currentWidth = static_cast<uint32_t>(crop.width()); |
| 842 | uint32_t currentHeight = static_cast<uint32_t>(crop.height()); |
| 843 | |
| 844 | // The crop is too wide |
| 845 | if (newWidth < currentWidth) { |
| 846 | uint32_t dw = currentWidth - newWidth; |
| 847 | auto halfdw = dw / 2; |
| 848 | outCrop.left += halfdw; |
| 849 | // Not halfdw because it would subtract 1 too few when dw is odd |
| 850 | outCrop.right -= (dw - halfdw); |
| 851 | // The crop is too tall |
| 852 | } else if (newHeight < currentHeight) { |
| 853 | uint32_t dh = currentHeight - newHeight; |
| 854 | auto halfdh = dh / 2; |
| 855 | outCrop.top += halfdh; |
| 856 | // Not halfdh because it would subtract 1 too few when dh is odd |
| 857 | outCrop.bottom -= (dh - halfdh); |
| 858 | } |
| 859 | |
| 860 | ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]", outCrop.left, outCrop.top, outCrop.right, |
| 861 | outCrop.bottom); |
| 862 | |
| 863 | return outCrop; |
| 864 | } |
| 865 | |
| 866 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 867 | BLC_LOGV("getTimestamp"); |
| 868 | Mutex::Autolock lock(mMutex); |
| 869 | return mCurrentTimestamp; |
| 870 | } |
| 871 | |
| 872 | android_dataspace BufferLayerConsumer::getCurrentDataSpace() { |
| 873 | BLC_LOGV("getCurrentDataSpace"); |
| 874 | Mutex::Autolock lock(mMutex); |
| 875 | return mCurrentDataSpace; |
| 876 | } |
| 877 | |
| 878 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 879 | BLC_LOGV("getFrameNumber"); |
| 880 | Mutex::Autolock lock(mMutex); |
| 881 | return mCurrentFrameNumber; |
| 882 | } |
| 883 | |
| 884 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const { |
| 885 | Mutex::Autolock lock(mMutex); |
| 886 | |
| 887 | if (outSlot != nullptr) { |
| 888 | *outSlot = mCurrentTexture; |
| 889 | } |
| 890 | |
| 891 | return (mCurrentTextureImage == nullptr) ? NULL : mCurrentTextureImage->graphicBuffer(); |
| 892 | } |
| 893 | |
| 894 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 895 | Mutex::Autolock lock(mMutex); |
| 896 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
| 897 | ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
| 898 | : mCurrentCrop; |
| 899 | } |
| 900 | |
| 901 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 902 | Mutex::Autolock lock(mMutex); |
| 903 | return mCurrentTransform; |
| 904 | } |
| 905 | |
| 906 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 907 | Mutex::Autolock lock(mMutex); |
| 908 | return mCurrentScalingMode; |
| 909 | } |
| 910 | |
| 911 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 912 | Mutex::Autolock lock(mMutex); |
| 913 | return mCurrentFence; |
| 914 | } |
| 915 | |
| 916 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 917 | Mutex::Autolock lock(mMutex); |
| 918 | return mCurrentFenceTime; |
| 919 | } |
| 920 | |
| 921 | status_t BufferLayerConsumer::doGLFenceWaitLocked() const { |
| 922 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 923 | EGLContext ctx = eglGetCurrentContext(); |
| 924 | |
| 925 | if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) { |
| 926 | BLC_LOGE("doGLFenceWait: invalid current EGLDisplay"); |
| 927 | return INVALID_OPERATION; |
| 928 | } |
| 929 | |
| 930 | if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) { |
| 931 | BLC_LOGE("doGLFenceWait: invalid current EGLContext"); |
| 932 | return INVALID_OPERATION; |
| 933 | } |
| 934 | |
| 935 | if (mCurrentFence->isValid()) { |
| 936 | if (SyncFeatures::getInstance().useWaitSync()) { |
| 937 | // Create an EGLSyncKHR from the current fence. |
| 938 | int fenceFd = mCurrentFence->dup(); |
| 939 | if (fenceFd == -1) { |
| 940 | BLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno); |
| 941 | return -errno; |
| 942 | } |
| 943 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| 944 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 945 | if (sync == EGL_NO_SYNC_KHR) { |
| 946 | close(fenceFd); |
| 947 | BLC_LOGE("doGLFenceWait: error creating EGL fence: %#x", eglGetError()); |
| 948 | return UNKNOWN_ERROR; |
| 949 | } |
| 950 | |
| 951 | // XXX: The spec draft is inconsistent as to whether this should |
| 952 | // return an EGLint or void. Ignore the return value for now, as |
| 953 | // it's not strictly needed. |
| 954 | eglWaitSyncKHR(dpy, sync, 0); |
| 955 | EGLint eglErr = eglGetError(); |
| 956 | eglDestroySyncKHR(dpy, sync); |
| 957 | if (eglErr != EGL_SUCCESS) { |
| 958 | BLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", eglErr); |
| 959 | return UNKNOWN_ERROR; |
| 960 | } |
| 961 | } else { |
| 962 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doGLFenceWaitLocked"); |
| 963 | if (err != NO_ERROR) { |
| 964 | BLC_LOGE("doGLFenceWait: error waiting for fence: %d", err); |
| 965 | return err; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | return NO_ERROR; |
| 971 | } |
| 972 | |
| 973 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 974 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 975 | if (slotIndex == mCurrentTexture) { |
| 976 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 977 | } |
| 978 | mEglSlots[slotIndex].mEglImage.clear(); |
| 979 | ConsumerBase::freeBufferLocked(slotIndex); |
| 980 | } |
| 981 | |
| 982 | void BufferLayerConsumer::abandonLocked() { |
| 983 | BLC_LOGV("abandonLocked"); |
| 984 | mCurrentTextureImage.clear(); |
| 985 | ConsumerBase::abandonLocked(); |
| 986 | } |
| 987 | |
| 988 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 989 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 990 | } |
| 991 | |
| 992 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 993 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 994 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 995 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 996 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 997 | mCurrentTransform); |
| 998 | |
| 999 | ConsumerBase::dumpLocked(result, prefix); |
| 1000 | } |
| 1001 | |
| 1002 | BufferLayerConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) |
| 1003 | : mGraphicBuffer(graphicBuffer), |
| 1004 | mEglImage(EGL_NO_IMAGE_KHR), |
| 1005 | mEglDisplay(EGL_NO_DISPLAY), |
| 1006 | mCropRect(Rect::EMPTY_RECT) {} |
| 1007 | |
| 1008 | BufferLayerConsumer::EglImage::~EglImage() { |
| 1009 | if (mEglImage != EGL_NO_IMAGE_KHR) { |
| 1010 | if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { |
| 1011 | ALOGE("~EglImage: eglDestroyImageKHR failed"); |
| 1012 | } |
| 1013 | eglTerminate(mEglDisplay); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | status_t BufferLayerConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay, const Rect& cropRect, |
| 1018 | bool forceCreation) { |
| 1019 | // If there's an image and it's no longer valid, destroy it. |
| 1020 | bool haveImage = mEglImage != EGL_NO_IMAGE_KHR; |
| 1021 | bool displayInvalid = mEglDisplay != eglDisplay; |
| 1022 | bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect; |
| 1023 | if (haveImage && (displayInvalid || cropInvalid || forceCreation)) { |
| 1024 | if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { |
| 1025 | ALOGE("createIfNeeded: eglDestroyImageKHR failed"); |
| 1026 | } |
| 1027 | eglTerminate(mEglDisplay); |
| 1028 | mEglImage = EGL_NO_IMAGE_KHR; |
| 1029 | mEglDisplay = EGL_NO_DISPLAY; |
| 1030 | } |
| 1031 | |
| 1032 | // If there's no image, create one. |
| 1033 | if (mEglImage == EGL_NO_IMAGE_KHR) { |
| 1034 | mEglDisplay = eglDisplay; |
| 1035 | mCropRect = cropRect; |
| 1036 | mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect); |
| 1037 | } |
| 1038 | |
| 1039 | // Fail if we can't create a valid image. |
| 1040 | if (mEglImage == EGL_NO_IMAGE_KHR) { |
| 1041 | mEglDisplay = EGL_NO_DISPLAY; |
| 1042 | mCropRect.makeInvalid(); |
| 1043 | const sp<GraphicBuffer>& buffer = mGraphicBuffer; |
| 1044 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", |
| 1045 | buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(), |
| 1046 | buffer->getPixelFormat()); |
| 1047 | return UNKNOWN_ERROR; |
| 1048 | } |
| 1049 | |
| 1050 | return OK; |
| 1051 | } |
| 1052 | |
| 1053 | void BufferLayerConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) { |
| 1054 | glEGLImageTargetTexture2DOES(texTarget, static_cast<GLeglImageOES>(mEglImage)); |
| 1055 | } |
| 1056 | |
| 1057 | EGLImageKHR BufferLayerConsumer::EglImage::createImage(EGLDisplay dpy, |
| 1058 | const sp<GraphicBuffer>& graphicBuffer, |
| 1059 | const Rect& crop) { |
| 1060 | EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer()); |
| 1061 | const bool createProtectedImage = |
| 1062 | (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && hasEglProtectedContent(); |
| 1063 | EGLint attrs[] = { |
| 1064 | EGL_IMAGE_PRESERVED_KHR, |
| 1065 | EGL_TRUE, |
| 1066 | EGL_IMAGE_CROP_LEFT_ANDROID, |
| 1067 | crop.left, |
| 1068 | EGL_IMAGE_CROP_TOP_ANDROID, |
| 1069 | crop.top, |
| 1070 | EGL_IMAGE_CROP_RIGHT_ANDROID, |
| 1071 | crop.right, |
| 1072 | EGL_IMAGE_CROP_BOTTOM_ANDROID, |
| 1073 | crop.bottom, |
| 1074 | createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, |
| 1075 | createProtectedImage ? EGL_TRUE : EGL_NONE, |
| 1076 | EGL_NONE, |
| 1077 | }; |
| 1078 | if (!crop.isValid()) { |
| 1079 | // No crop rect to set, so leave the crop out of the attrib array. Make |
| 1080 | // sure to propagate the protected content attrs if they are set. |
| 1081 | attrs[2] = attrs[10]; |
| 1082 | attrs[3] = attrs[11]; |
| 1083 | attrs[4] = EGL_NONE; |
| 1084 | } else if (!isEglImageCroppable(crop)) { |
| 1085 | // The crop rect is not at the origin, so we can't set the crop on the |
| 1086 | // EGLImage because that's not allowed by the EGL_ANDROID_image_crop |
| 1087 | // extension. In the future we can add a layered extension that |
| 1088 | // removes this restriction if there is hardware that can support it. |
| 1089 | attrs[2] = attrs[10]; |
| 1090 | attrs[3] = attrs[11]; |
| 1091 | attrs[4] = EGL_NONE; |
| 1092 | } |
| 1093 | eglInitialize(dpy, 0, 0); |
| 1094 | EGLImageKHR image = |
| 1095 | eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
| 1096 | if (image == EGL_NO_IMAGE_KHR) { |
| 1097 | EGLint error = eglGetError(); |
| 1098 | ALOGE("error creating EGLImage: %#x", error); |
| 1099 | eglTerminate(dpy); |
| 1100 | } |
| 1101 | return image; |
| 1102 | } |
| 1103 | |
| 1104 | }; // namespace android |