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 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 24 | #include "DispSync.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 25 | #include "Layer.h" |
| 26 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 27 | #include <inttypes.h> |
| 28 | |
| 29 | #include <EGL/egl.h> |
| 30 | #include <EGL/eglext.h> |
| 31 | #include <GLES2/gl2.h> |
| 32 | #include <GLES2/gl2ext.h> |
| 33 | #include <cutils/compiler.h> |
| 34 | |
| 35 | #include <hardware/hardware.h> |
| 36 | |
| 37 | #include <math/mat4.h> |
| 38 | |
| 39 | #include <gui/BufferItem.h> |
| 40 | #include <gui/GLConsumer.h> |
| 41 | #include <gui/ISurfaceComposer.h> |
| 42 | #include <gui/SurfaceComposerClient.h> |
| 43 | |
| 44 | #include <private/gui/ComposerService.h> |
| 45 | #include <private/gui/SyncFeatures.h> |
| 46 | |
| 47 | #include <utils/Log.h> |
| 48 | #include <utils/String8.h> |
| 49 | #include <utils/Trace.h> |
| 50 | |
| 51 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| 52 | #define CROP_EXT_STR "EGL_ANDROID_image_crop" |
| 53 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 54 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
| 55 | |
| 56 | namespace android { |
| 57 | |
| 58 | // Macros for including the BufferLayerConsumer name in log messages |
| 59 | #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 60 | #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 61 | //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 62 | #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 63 | #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 64 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 65 | static const mat4 mtxIdentity; |
| 66 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 67 | static bool hasEglAndroidImageCropImpl() { |
| 68 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 69 | const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS); |
| 70 | size_t cropExtLen = strlen(CROP_EXT_STR); |
| 71 | size_t extsLen = strlen(exts); |
| 72 | bool equal = !strcmp(CROP_EXT_STR, exts); |
| 73 | bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen + 1); |
| 74 | bool atEnd = (cropExtLen + 1) < extsLen && |
| 75 | !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen + 1)); |
| 76 | bool inMiddle = strstr(exts, " " CROP_EXT_STR " "); |
| 77 | return equal || atStart || atEnd || inMiddle; |
| 78 | } |
| 79 | |
| 80 | static bool hasEglAndroidImageCrop() { |
| 81 | // Only compute whether the extension is present once the first time this |
| 82 | // function is called. |
| 83 | static bool hasIt = hasEglAndroidImageCropImpl(); |
| 84 | return hasIt; |
| 85 | } |
| 86 | |
| 87 | static bool hasEglProtectedContentImpl() { |
| 88 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 89 | const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); |
| 90 | size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR); |
| 91 | size_t extsLen = strlen(exts); |
| 92 | bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts); |
| 93 | bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen + 1); |
| 94 | bool atEnd = (cropExtLen + 1) < extsLen && |
| 95 | !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen + 1)); |
| 96 | bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " "); |
| 97 | return equal || atStart || atEnd || inMiddle; |
| 98 | } |
| 99 | |
| 100 | static bool hasEglProtectedContent() { |
| 101 | // Only compute whether the extension is present once the first time this |
| 102 | // function is called. |
| 103 | static bool hasIt = hasEglProtectedContentImpl(); |
| 104 | return hasIt; |
| 105 | } |
| 106 | |
| 107 | static bool isEglImageCroppable(const Rect& crop) { |
| 108 | return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0); |
| 109 | } |
| 110 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 111 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, |
| 112 | Layer* layer) |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 113 | : ConsumerBase(bq, false), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 114 | mCurrentCrop(Rect::EMPTY_RECT), |
| 115 | mCurrentTransform(0), |
| 116 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 117 | mCurrentFence(Fence::NO_FENCE), |
| 118 | mCurrentTimestamp(0), |
| 119 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 120 | mCurrentFrameNumber(0), |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 121 | mCurrentTransformToDisplayInverse(false), |
| 122 | mCurrentSurfaceDamage(), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 123 | mDefaultWidth(1), |
| 124 | mDefaultHeight(1), |
| 125 | mFilteringEnabled(true), |
| 126 | mTexName(tex), |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 127 | mLayer(layer), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 128 | mEglDisplay(EGL_NO_DISPLAY), |
| 129 | mEglContext(EGL_NO_CONTEXT), |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 130 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 131 | BLC_LOGV("BufferLayerConsumer"); |
| 132 | |
| 133 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 134 | |
| 135 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 136 | } |
| 137 | |
| 138 | status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 139 | Mutex::Autolock lock(mMutex); |
| 140 | if (mAbandoned) { |
| 141 | BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!"); |
| 142 | return NO_INIT; |
| 143 | } |
| 144 | mDefaultWidth = w; |
| 145 | mDefaultHeight = h; |
| 146 | return mConsumer->setDefaultBufferSize(w, h); |
| 147 | } |
| 148 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 149 | void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) { |
| 150 | setFrameAvailableListener(listener); |
| 151 | Mutex::Autolock lock(mMutex); |
| 152 | mContentsChangedListener = listener; |
| 153 | } |
| 154 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 155 | // We need to determine the time when a buffer acquired now will be |
| 156 | // displayed. This can be calculated: |
| 157 | // time when previous buffer's actual-present fence was signaled |
| 158 | // + current display refresh rate * HWC latency |
| 159 | // + a little extra padding |
| 160 | // |
| 161 | // Buffer producers are expected to set their desired presentation time |
| 162 | // based on choreographer time stamps, which (coming from vsync events) |
| 163 | // will be slightly later then the actual-present timing. If we get a |
| 164 | // desired-present time that is unintentionally a hair after the next |
| 165 | // vsync, we'll hold the frame when we really want to display it. We |
| 166 | // need to take the offset between actual-present and reported-vsync |
| 167 | // into account. |
| 168 | // |
| 169 | // If the system is configured without a DispSync phase offset for the app, |
| 170 | // we also want to throw in a bit of padding to avoid edge cases where we |
| 171 | // just barely miss. We want to do it here, not in every app. A major |
| 172 | // source of trouble is the app's use of the display's ideal refresh time |
| 173 | // (via Display.getRefreshRate()), which could be off of the actual refresh |
| 174 | // by a few percent, with the error multiplied by the number of frames |
| 175 | // between now and when the buffer should be displayed. |
| 176 | // |
| 177 | // If the refresh reported to the app has a phase offset, we shouldn't need |
| 178 | // to tweak anything here. |
| 179 | nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) { |
| 180 | // The HWC doesn't currently have a way to report additional latency. |
| 181 | // Assume that whatever we submit now will appear right after the flip. |
| 182 | // For a smart panel this might be 1. This is expressed in frames, |
| 183 | // rather than time, because we expect to have a constant frame delay |
| 184 | // regardless of the refresh rate. |
| 185 | const uint32_t hwcLatency = 0; |
| 186 | |
| 187 | // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC). |
| 188 | const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency); |
| 189 | |
| 190 | // The DispSync time is already adjusted for the difference between |
| 191 | // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so |
| 192 | // we don't need to factor that in here. Pad a little to avoid |
| 193 | // weird effects if apps might be requesting times right on the edge. |
| 194 | nsecs_t extraPadding = 0; |
| 195 | if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) { |
| 196 | extraPadding = 1000000; // 1ms (6% of 60Hz) |
| 197 | } |
| 198 | |
| 199 | return nextRefresh + extraPadding; |
| 200 | } |
| 201 | |
| 202 | status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync, |
| 203 | bool* autoRefresh, bool* queuedBuffer, |
| 204 | uint64_t maxFrameNumber) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 205 | ATRACE_CALL(); |
| 206 | BLC_LOGV("updateTexImage"); |
| 207 | Mutex::Autolock lock(mMutex); |
| 208 | |
| 209 | if (mAbandoned) { |
| 210 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 211 | return NO_INIT; |
| 212 | } |
| 213 | |
| 214 | // Make sure the EGL state is the same as in previous calls. |
| 215 | status_t err = checkAndUpdateEglStateLocked(); |
| 216 | if (err != NO_ERROR) { |
| 217 | return err; |
| 218 | } |
| 219 | |
| 220 | BufferItem item; |
| 221 | |
| 222 | // Acquire the next buffer. |
| 223 | // In asynchronous mode the list is guaranteed to be one buffer |
| 224 | // deep, while in synchronous mode we use the oldest buffer. |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 225 | err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 226 | if (err != NO_ERROR) { |
| 227 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 228 | err = NO_ERROR; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 229 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 230 | // return the error, without logging |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 231 | } else { |
| 232 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 233 | } |
| 234 | return err; |
| 235 | } |
| 236 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 237 | if (autoRefresh) { |
| 238 | *autoRefresh = item.mAutoRefresh; |
| 239 | } |
| 240 | |
| 241 | if (queuedBuffer) { |
| 242 | *queuedBuffer = item.mQueuedBuffer; |
| 243 | } |
| 244 | |
| 245 | // We call the rejecter here, in case the caller has a reason to |
| 246 | // not accept this buffer. This is used by SurfaceFlinger to |
| 247 | // reject buffers which have the wrong size |
| 248 | int slot = item.mSlot; |
| 249 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 250 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
| 251 | return BUFFER_REJECTED; |
| 252 | } |
| 253 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 254 | // Release the previous buffer. |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 255 | err = updateAndReleaseLocked(item, &mPendingRelease); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 256 | if (err != NO_ERROR) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 257 | return err; |
| 258 | } |
| 259 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 260 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
| 261 | // Bind the new buffer to the GL texture. |
| 262 | // |
| 263 | // Older devices require the "implicit" synchronization provided |
| 264 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 265 | // devices will either call this in Layer::onDraw, or (if it's not |
| 266 | // a GL-composited layer) not at all. |
| 267 | err = bindTextureImageLocked(); |
| 268 | } |
| 269 | |
| 270 | return err; |
| 271 | } |
| 272 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 273 | status_t BufferLayerConsumer::bindTextureImage() { |
| 274 | Mutex::Autolock lock(mMutex); |
| 275 | return bindTextureImageLocked(); |
| 276 | } |
| 277 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 278 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 279 | if (!fence->isValid()) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture; |
| 284 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer |
| 289 | : mCurrentTextureImage->graphicBuffer(); |
| 290 | auto err = addReleaseFence(slot, buffer, fence); |
| 291 | if (err != OK) { |
| 292 | BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | bool BufferLayerConsumer::releasePendingBuffer() { |
| 297 | if (!mPendingRelease.isPending) { |
| 298 | BLC_LOGV("Pending buffer already released"); |
| 299 | return false; |
| 300 | } |
| 301 | BLC_LOGV("Releasing pending buffer"); |
| 302 | Mutex::Autolock lock(mMutex); |
| 303 | status_t result = |
| 304 | releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer); |
| 305 | if (result < NO_ERROR) { |
| 306 | BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result); |
| 307 | } |
| 308 | mPendingRelease = PendingRelease(); |
| 309 | return true; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 312 | sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const { |
| 313 | Mutex::Autolock lock(mMutex); |
| 314 | return ConsumerBase::mPrevFinalReleaseFence; |
| 315 | } |
| 316 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 317 | status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 318 | uint64_t maxFrameNumber) { |
| 319 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber); |
| 320 | if (err != NO_ERROR) { |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
| 325 | // before, so any prior EglImage created is using a stale buffer. This |
| 326 | // replaces any old EglImage with a new one (using the new buffer). |
| 327 | if (item->mGraphicBuffer != NULL) { |
| 328 | int slot = item->mSlot; |
| 329 | mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer); |
| 330 | } |
| 331 | |
| 332 | return NO_ERROR; |
| 333 | } |
| 334 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 335 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
| 336 | PendingRelease* pendingRelease) { |
| 337 | status_t err = NO_ERROR; |
| 338 | |
| 339 | int slot = item.mSlot; |
| 340 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 341 | // Confirm state. |
| 342 | err = checkAndUpdateEglStateLocked(); |
| 343 | if (err != NO_ERROR) { |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 344 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 345 | return err; |
| 346 | } |
| 347 | |
| 348 | // Ensure we have a valid EglImageKHR for the slot, creating an EglImage |
| 349 | // if nessessary, for the gralloc buffer currently in the slot in |
| 350 | // ConsumerBase. |
| 351 | // We may have to do this even when item.mGraphicBuffer == NULL (which |
| 352 | // means the buffer was previously acquired). |
| 353 | err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop); |
| 354 | if (err != NO_ERROR) { |
| 355 | BLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", mEglDisplay, |
| 356 | slot); |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 357 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 358 | return UNKNOWN_ERROR; |
| 359 | } |
| 360 | |
| 361 | // Do whatever sync ops we need to do before releasing the old slot. |
| 362 | if (slot != mCurrentTexture) { |
| 363 | err = syncForReleaseLocked(mEglDisplay); |
| 364 | if (err != NO_ERROR) { |
| 365 | // Release the buffer we just acquired. It's not safe to |
| 366 | // release the old buffer, so instead we just drop the new frame. |
| 367 | // As we are still under lock since acquireBuffer, it is safe to |
| 368 | // release by slot. |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 369 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 370 | return err; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
| 375 | mCurrentTextureImage != NULL ? mCurrentTextureImage->graphicBufferHandle() : 0, slot, |
| 376 | mSlots[slot].mGraphicBuffer->handle); |
| 377 | |
| 378 | // Hang onto the pointer so that it isn't freed in the call to |
| 379 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 380 | // the same. |
| 381 | sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage; |
| 382 | |
| 383 | // release old buffer |
| 384 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 385 | if (pendingRelease == nullptr) { |
| 386 | status_t status = |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 387 | releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 388 | if (status < NO_ERROR) { |
| 389 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 390 | status); |
| 391 | err = status; |
| 392 | // keep going, with error raised [?] |
| 393 | } |
| 394 | } else { |
| 395 | pendingRelease->currentTexture = mCurrentTexture; |
| 396 | pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 397 | pendingRelease->isPending = true; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Update the BufferLayerConsumer state. |
| 402 | mCurrentTexture = slot; |
| 403 | mCurrentTextureImage = nextTextureImage; |
| 404 | mCurrentCrop = item.mCrop; |
| 405 | mCurrentTransform = item.mTransform; |
| 406 | mCurrentScalingMode = item.mScalingMode; |
| 407 | mCurrentTimestamp = item.mTimestamp; |
| 408 | mCurrentDataSpace = item.mDataSpace; |
| 409 | mCurrentFence = item.mFence; |
| 410 | mCurrentFenceTime = item.mFenceTime; |
| 411 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 412 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 413 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 414 | |
| 415 | computeCurrentTransformMatrixLocked(); |
| 416 | |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
| 421 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 422 | ALOGE("bindTextureImage: invalid display"); |
| 423 | return INVALID_OPERATION; |
| 424 | } |
| 425 | |
| 426 | GLenum error; |
| 427 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 428 | BLC_LOGW("bindTextureImage: clearing GL error: %#04x", error); |
| 429 | } |
| 430 | |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 431 | glBindTexture(sTexTarget, mTexName); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 432 | if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == NULL) { |
| 433 | BLC_LOGE("bindTextureImage: no currently-bound texture"); |
| 434 | return NO_INIT; |
| 435 | } |
| 436 | |
| 437 | status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay, mCurrentCrop); |
| 438 | if (err != NO_ERROR) { |
| 439 | BLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay, |
| 440 | mCurrentTexture); |
| 441 | return UNKNOWN_ERROR; |
| 442 | } |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 443 | mCurrentTextureImage->bindToTextureTarget(sTexTarget); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 444 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 445 | // Wait for the new buffer to be ready. |
| 446 | return doGLFenceWaitLocked(); |
| 447 | } |
| 448 | |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 449 | status_t BufferLayerConsumer::checkAndUpdateEglStateLocked() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 450 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 451 | EGLContext ctx = eglGetCurrentContext(); |
| 452 | |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 453 | // if this is the first time we're called, mEglDisplay/mEglContext have |
| 454 | // never been set, so don't error out (below). |
| 455 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 456 | mEglDisplay = dpy; |
| 457 | } |
| 458 | if (mEglContext == EGL_NO_CONTEXT) { |
| 459 | mEglContext = ctx; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) { |
| 463 | BLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay"); |
| 464 | return INVALID_OPERATION; |
| 465 | } |
| 466 | |
| 467 | if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) { |
| 468 | BLC_LOGE("checkAndUpdateEglState: invalid current EGLContext"); |
| 469 | return INVALID_OPERATION; |
| 470 | } |
| 471 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 472 | return NO_ERROR; |
| 473 | } |
| 474 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 475 | status_t BufferLayerConsumer::syncForReleaseLocked(EGLDisplay dpy) { |
| 476 | BLC_LOGV("syncForReleaseLocked"); |
| 477 | |
| 478 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 479 | if (SyncFeatures::getInstance().useNativeFenceSync()) { |
| 480 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL); |
| 481 | if (sync == EGL_NO_SYNC_KHR) { |
| 482 | BLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", eglGetError()); |
| 483 | return UNKNOWN_ERROR; |
| 484 | } |
| 485 | glFlush(); |
| 486 | int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync); |
| 487 | eglDestroySyncKHR(dpy, sync); |
| 488 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 489 | BLC_LOGE("syncForReleaseLocked: error dup'ing native fence " |
| 490 | "fd: %#x", |
| 491 | eglGetError()); |
| 492 | return UNKNOWN_ERROR; |
| 493 | } |
| 494 | sp<Fence> fence(new Fence(fenceFd)); |
| 495 | status_t err = addReleaseFenceLocked(mCurrentTexture, |
| 496 | mCurrentTextureImage->graphicBuffer(), fence); |
| 497 | if (err != OK) { |
| 498 | BLC_LOGE("syncForReleaseLocked: error adding release fence: " |
| 499 | "%s (%d)", |
| 500 | strerror(-err), err); |
| 501 | return err; |
| 502 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | return OK; |
| 507 | } |
| 508 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 509 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 510 | Mutex::Autolock lock(mMutex); |
| 511 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 512 | } |
| 513 | |
| 514 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 515 | Mutex::Autolock lock(mMutex); |
| 516 | if (mAbandoned) { |
| 517 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 518 | return; |
| 519 | } |
| 520 | bool needsRecompute = mFilteringEnabled != enabled; |
| 521 | mFilteringEnabled = enabled; |
| 522 | |
| 523 | if (needsRecompute && mCurrentTextureImage == NULL) { |
| 524 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL"); |
| 525 | } |
| 526 | |
| 527 | if (needsRecompute && mCurrentTextureImage != NULL) { |
| 528 | computeCurrentTransformMatrixLocked(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 533 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
| 534 | sp<GraphicBuffer> buf = |
| 535 | (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer(); |
| 536 | if (buf == nullptr) { |
| 537 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
| 538 | "mCurrentTextureImage is NULL"); |
| 539 | } |
Chia-I Wu | 243b74a | 2017-11-27 14:24:14 -0800 | [diff] [blame] | 540 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf, |
| 541 | isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT |
| 542 | : mCurrentCrop, |
| 543 | mCurrentTransform, mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 546 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 547 | BLC_LOGV("getTimestamp"); |
| 548 | Mutex::Autolock lock(mMutex); |
| 549 | return mCurrentTimestamp; |
| 550 | } |
| 551 | |
| 552 | android_dataspace BufferLayerConsumer::getCurrentDataSpace() { |
| 553 | BLC_LOGV("getCurrentDataSpace"); |
| 554 | Mutex::Autolock lock(mMutex); |
| 555 | return mCurrentDataSpace; |
| 556 | } |
| 557 | |
| 558 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 559 | BLC_LOGV("getFrameNumber"); |
| 560 | Mutex::Autolock lock(mMutex); |
| 561 | return mCurrentFrameNumber; |
| 562 | } |
| 563 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 564 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 565 | Mutex::Autolock lock(mMutex); |
| 566 | return mCurrentTransformToDisplayInverse; |
| 567 | } |
| 568 | |
| 569 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 570 | return mCurrentSurfaceDamage; |
| 571 | } |
| 572 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 573 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const { |
| 574 | Mutex::Autolock lock(mMutex); |
| 575 | |
| 576 | if (outSlot != nullptr) { |
| 577 | *outSlot = mCurrentTexture; |
| 578 | } |
| 579 | |
| 580 | return (mCurrentTextureImage == nullptr) ? NULL : mCurrentTextureImage->graphicBuffer(); |
| 581 | } |
| 582 | |
| 583 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 584 | Mutex::Autolock lock(mMutex); |
| 585 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 586 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 587 | : mCurrentCrop; |
| 588 | } |
| 589 | |
| 590 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 591 | Mutex::Autolock lock(mMutex); |
| 592 | return mCurrentTransform; |
| 593 | } |
| 594 | |
| 595 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 596 | Mutex::Autolock lock(mMutex); |
| 597 | return mCurrentScalingMode; |
| 598 | } |
| 599 | |
| 600 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 601 | Mutex::Autolock lock(mMutex); |
| 602 | return mCurrentFence; |
| 603 | } |
| 604 | |
| 605 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 606 | Mutex::Autolock lock(mMutex); |
| 607 | return mCurrentFenceTime; |
| 608 | } |
| 609 | |
| 610 | status_t BufferLayerConsumer::doGLFenceWaitLocked() const { |
| 611 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 612 | EGLContext ctx = eglGetCurrentContext(); |
| 613 | |
| 614 | if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) { |
| 615 | BLC_LOGE("doGLFenceWait: invalid current EGLDisplay"); |
| 616 | return INVALID_OPERATION; |
| 617 | } |
| 618 | |
| 619 | if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) { |
| 620 | BLC_LOGE("doGLFenceWait: invalid current EGLContext"); |
| 621 | return INVALID_OPERATION; |
| 622 | } |
| 623 | |
| 624 | if (mCurrentFence->isValid()) { |
| 625 | if (SyncFeatures::getInstance().useWaitSync()) { |
| 626 | // Create an EGLSyncKHR from the current fence. |
| 627 | int fenceFd = mCurrentFence->dup(); |
| 628 | if (fenceFd == -1) { |
| 629 | BLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno); |
| 630 | return -errno; |
| 631 | } |
| 632 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| 633 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 634 | if (sync == EGL_NO_SYNC_KHR) { |
| 635 | close(fenceFd); |
| 636 | BLC_LOGE("doGLFenceWait: error creating EGL fence: %#x", eglGetError()); |
| 637 | return UNKNOWN_ERROR; |
| 638 | } |
| 639 | |
| 640 | // XXX: The spec draft is inconsistent as to whether this should |
| 641 | // return an EGLint or void. Ignore the return value for now, as |
| 642 | // it's not strictly needed. |
| 643 | eglWaitSyncKHR(dpy, sync, 0); |
| 644 | EGLint eglErr = eglGetError(); |
| 645 | eglDestroySyncKHR(dpy, sync); |
| 646 | if (eglErr != EGL_SUCCESS) { |
| 647 | BLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", eglErr); |
| 648 | return UNKNOWN_ERROR; |
| 649 | } |
| 650 | } else { |
| 651 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doGLFenceWaitLocked"); |
| 652 | if (err != NO_ERROR) { |
| 653 | BLC_LOGE("doGLFenceWait: error waiting for fence: %d", err); |
| 654 | return err; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return NO_ERROR; |
| 660 | } |
| 661 | |
| 662 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 663 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 664 | if (slotIndex == mCurrentTexture) { |
| 665 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 666 | } |
| 667 | mEglSlots[slotIndex].mEglImage.clear(); |
| 668 | ConsumerBase::freeBufferLocked(slotIndex); |
| 669 | } |
| 670 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 671 | void BufferLayerConsumer::onDisconnect() { |
| 672 | sp<Layer> l = mLayer.promote(); |
| 673 | if (l.get()) { |
| 674 | l->onDisconnect(); |
| 675 | } |
| 676 | } |
| 677 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 678 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 679 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 680 | { |
| 681 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 682 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 683 | } |
| 684 | sp<ContentsChangedListener> listener; |
| 685 | { // scope for the lock |
| 686 | Mutex::Autolock lock(mMutex); |
| 687 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 688 | listener = mContentsChangedListener.promote(); |
| 689 | } |
| 690 | |
| 691 | if (listener != NULL) { |
| 692 | listener->onSidebandStreamChanged(); |
| 693 | } |
| 694 | } |
| 695 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 696 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 697 | FrameEventHistoryDelta* outDelta) { |
| 698 | sp<Layer> l = mLayer.promote(); |
| 699 | if (l.get()) { |
| 700 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 701 | } |
| 702 | } |
| 703 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 704 | void BufferLayerConsumer::abandonLocked() { |
| 705 | BLC_LOGV("abandonLocked"); |
| 706 | mCurrentTextureImage.clear(); |
| 707 | ConsumerBase::abandonLocked(); |
| 708 | } |
| 709 | |
| 710 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 711 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 712 | } |
| 713 | |
| 714 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 715 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 716 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 717 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 718 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 719 | mCurrentTransform); |
| 720 | |
| 721 | ConsumerBase::dumpLocked(result, prefix); |
| 722 | } |
| 723 | |
| 724 | BufferLayerConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) |
| 725 | : mGraphicBuffer(graphicBuffer), |
| 726 | mEglImage(EGL_NO_IMAGE_KHR), |
| 727 | mEglDisplay(EGL_NO_DISPLAY), |
| 728 | mCropRect(Rect::EMPTY_RECT) {} |
| 729 | |
| 730 | BufferLayerConsumer::EglImage::~EglImage() { |
| 731 | if (mEglImage != EGL_NO_IMAGE_KHR) { |
| 732 | if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { |
| 733 | ALOGE("~EglImage: eglDestroyImageKHR failed"); |
| 734 | } |
| 735 | eglTerminate(mEglDisplay); |
| 736 | } |
| 737 | } |
| 738 | |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 739 | status_t BufferLayerConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay, |
| 740 | const Rect& cropRect) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 741 | // If there's an image and it's no longer valid, destroy it. |
| 742 | bool haveImage = mEglImage != EGL_NO_IMAGE_KHR; |
| 743 | bool displayInvalid = mEglDisplay != eglDisplay; |
| 744 | bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect; |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 745 | if (haveImage && (displayInvalid || cropInvalid)) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 746 | if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { |
| 747 | ALOGE("createIfNeeded: eglDestroyImageKHR failed"); |
| 748 | } |
| 749 | eglTerminate(mEglDisplay); |
| 750 | mEglImage = EGL_NO_IMAGE_KHR; |
| 751 | mEglDisplay = EGL_NO_DISPLAY; |
| 752 | } |
| 753 | |
| 754 | // If there's no image, create one. |
| 755 | if (mEglImage == EGL_NO_IMAGE_KHR) { |
| 756 | mEglDisplay = eglDisplay; |
| 757 | mCropRect = cropRect; |
| 758 | mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect); |
| 759 | } |
| 760 | |
| 761 | // Fail if we can't create a valid image. |
| 762 | if (mEglImage == EGL_NO_IMAGE_KHR) { |
| 763 | mEglDisplay = EGL_NO_DISPLAY; |
| 764 | mCropRect.makeInvalid(); |
| 765 | const sp<GraphicBuffer>& buffer = mGraphicBuffer; |
| 766 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", |
| 767 | buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(), |
| 768 | buffer->getPixelFormat()); |
| 769 | return UNKNOWN_ERROR; |
| 770 | } |
| 771 | |
| 772 | return OK; |
| 773 | } |
| 774 | |
| 775 | void BufferLayerConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) { |
| 776 | glEGLImageTargetTexture2DOES(texTarget, static_cast<GLeglImageOES>(mEglImage)); |
| 777 | } |
| 778 | |
| 779 | EGLImageKHR BufferLayerConsumer::EglImage::createImage(EGLDisplay dpy, |
| 780 | const sp<GraphicBuffer>& graphicBuffer, |
| 781 | const Rect& crop) { |
| 782 | EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer()); |
| 783 | const bool createProtectedImage = |
| 784 | (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && hasEglProtectedContent(); |
| 785 | EGLint attrs[] = { |
| 786 | EGL_IMAGE_PRESERVED_KHR, |
| 787 | EGL_TRUE, |
| 788 | EGL_IMAGE_CROP_LEFT_ANDROID, |
| 789 | crop.left, |
| 790 | EGL_IMAGE_CROP_TOP_ANDROID, |
| 791 | crop.top, |
| 792 | EGL_IMAGE_CROP_RIGHT_ANDROID, |
| 793 | crop.right, |
| 794 | EGL_IMAGE_CROP_BOTTOM_ANDROID, |
| 795 | crop.bottom, |
| 796 | createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, |
| 797 | createProtectedImage ? EGL_TRUE : EGL_NONE, |
| 798 | EGL_NONE, |
| 799 | }; |
| 800 | if (!crop.isValid()) { |
| 801 | // No crop rect to set, so leave the crop out of the attrib array. Make |
| 802 | // sure to propagate the protected content attrs if they are set. |
| 803 | attrs[2] = attrs[10]; |
| 804 | attrs[3] = attrs[11]; |
| 805 | attrs[4] = EGL_NONE; |
| 806 | } else if (!isEglImageCroppable(crop)) { |
| 807 | // The crop rect is not at the origin, so we can't set the crop on the |
| 808 | // EGLImage because that's not allowed by the EGL_ANDROID_image_crop |
| 809 | // extension. In the future we can add a layered extension that |
| 810 | // removes this restriction if there is hardware that can support it. |
| 811 | attrs[2] = attrs[10]; |
| 812 | attrs[3] = attrs[11]; |
| 813 | attrs[4] = EGL_NONE; |
| 814 | } |
| 815 | eglInitialize(dpy, 0, 0); |
| 816 | EGLImageKHR image = |
| 817 | eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
| 818 | if (image == EGL_NO_IMAGE_KHR) { |
| 819 | EGLint error = eglGetError(); |
| 820 | ALOGE("error creating EGLImage: %#x", error); |
| 821 | eglTerminate(dpy); |
| 822 | } |
| 823 | return image; |
| 824 | } |
| 825 | |
| 826 | }; // namespace android |