Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
| 20 | #include <android/native_window.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 21 | #include <common/trace.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 22 | #include <compositionengine/CompositionEngine.h> |
| 23 | #include <compositionengine/Display.h> |
| 24 | #include <compositionengine/DisplaySurface.h> |
| 25 | #include <compositionengine/RenderSurfaceCreationArgs.h> |
| 26 | #include <compositionengine/impl/DumpHelpers.h> |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 27 | #include <compositionengine/impl/OutputCompositionState.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 28 | #include <compositionengine/impl/RenderSurface.h> |
| 29 | #include <log/log.h> |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 30 | #include <renderengine/ExternalTexture.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 31 | #include <renderengine/RenderEngine.h> |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 32 | #include <renderengine/impl/ExternalTexture.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 33 | #include <system/window.h> |
| 34 | #include <ui/GraphicBuffer.h> |
| 35 | #include <ui/Rect.h> |
| 36 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 37 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 38 | #pragma clang diagnostic push |
| 39 | #pragma clang diagnostic ignored "-Wconversion" |
| 40 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 41 | #include "DisplayHardware/HWComposer.h" |
| 42 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 43 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 44 | #pragma clang diagnostic pop // ignored "-Wconversion" |
| 45 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 46 | namespace android::compositionengine { |
| 47 | |
| 48 | RenderSurface::~RenderSurface() = default; |
| 49 | |
| 50 | namespace impl { |
| 51 | |
John Reck | 44418f5 | 2020-09-15 18:02:17 -0700 | [diff] [blame] | 52 | constexpr auto DEFAULT_USAGE = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
| 53 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 54 | std::unique_ptr<compositionengine::RenderSurface> createRenderSurface( |
| 55 | const compositionengine::CompositionEngine& compositionEngine, |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 56 | compositionengine::Display& display, |
| 57 | const compositionengine::RenderSurfaceCreationArgs& args) { |
| 58 | return std::make_unique<RenderSurface>(compositionEngine, display, args); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display, |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 62 | const RenderSurfaceCreationArgs& args) |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 63 | : mCompositionEngine(compositionEngine), |
| 64 | mDisplay(display), |
| 65 | mNativeWindow(args.nativeWindow), |
| 66 | mDisplaySurface(args.displaySurface), |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 67 | mSize(args.displayWidth, args.displayHeight), |
| 68 | mMaxTextureCacheSize(args.maxTextureCacheSize) { |
chaviw | 8beb414 | 2019-04-11 13:09:05 -0700 | [diff] [blame] | 69 | LOG_ALWAYS_FATAL_IF(!mNativeWindow); |
| 70 | } |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 71 | |
chaviw | 8beb414 | 2019-04-11 13:09:05 -0700 | [diff] [blame] | 72 | RenderSurface::~RenderSurface() { |
| 73 | ANativeWindow* const window = mNativeWindow.get(); |
| 74 | native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); |
| 75 | } |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 76 | |
| 77 | bool RenderSurface::isValid() const { |
| 78 | return mSize.isValid(); |
| 79 | } |
| 80 | |
| 81 | void RenderSurface::initialize() { |
| 82 | ANativeWindow* const window = mNativeWindow.get(); |
| 83 | |
| 84 | int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL); |
| 85 | ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status); |
| 86 | status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888); |
| 87 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status); |
John Reck | 44418f5 | 2020-09-15 18:02:17 -0700 | [diff] [blame] | 88 | status = native_window_set_usage(window, DEFAULT_USAGE); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 89 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status); |
| 90 | } |
| 91 | |
| 92 | const ui::Size& RenderSurface::getSize() const { |
| 93 | return mSize; |
| 94 | } |
| 95 | |
| 96 | const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const { |
| 97 | return mDisplaySurface->getClientTargetAcquireFence(); |
| 98 | } |
| 99 | |
| 100 | void RenderSurface::setDisplaySize(const ui::Size& size) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 101 | mDisplaySurface->resizeBuffers(size); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 102 | mSize = size; |
| 103 | } |
| 104 | |
| 105 | void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) { |
| 106 | native_window_set_buffers_data_space(mNativeWindow.get(), |
| 107 | static_cast<android_dataspace>(dataspace)); |
| 108 | } |
| 109 | |
Peiyong Lin | dfc3f7c | 2020-05-07 20:15:50 -0700 | [diff] [blame] | 110 | void RenderSurface::setBufferPixelFormat(ui::PixelFormat pixelFormat) { |
| 111 | native_window_set_buffers_format(mNativeWindow.get(), static_cast<int32_t>(pixelFormat)); |
| 112 | } |
| 113 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 114 | void RenderSurface::setProtected(bool useProtected) { |
John Reck | 44418f5 | 2020-09-15 18:02:17 -0700 | [diff] [blame] | 115 | uint64_t usageFlags = DEFAULT_USAGE; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 116 | if (useProtected) { |
| 117 | usageFlags |= GRALLOC_USAGE_PROTECTED; |
| 118 | } |
| 119 | const int status = native_window_set_usage(mNativeWindow.get(), usageFlags); |
| 120 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status); |
Peiyong Lin | 5201031 | 2019-05-02 14:22:16 -0700 | [diff] [blame] | 121 | if (status == NO_ERROR) { |
| 122 | mProtected = useProtected; |
| 123 | } |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | status_t RenderSurface::beginFrame(bool mustRecompose) { |
| 127 | return mDisplaySurface->beginFrame(mustRecompose); |
| 128 | } |
| 129 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 130 | void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) { |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 131 | const auto compositionType = [=] { |
| 132 | using CompositionType = DisplaySurface::CompositionType; |
| 133 | |
| 134 | if (usesClientComposition && usesDeviceComposition) return CompositionType::Mixed; |
| 135 | if (usesClientComposition) return CompositionType::Gpu; |
| 136 | if (usesDeviceComposition) return CompositionType::Hwc; |
| 137 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 138 | // Nothing to do -- when turning the screen off we get a frame like |
Peiyong Lin | f3ffc4e | 2019-12-13 00:46:24 -0800 | [diff] [blame] | 139 | // this. Call it a HWC frame since we won't be doing any GPU work but |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 140 | // will do a prepare/set cycle. |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 141 | return CompositionType::Hwc; |
| 142 | }(); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 143 | |
| 144 | if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) { |
| 145 | ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result, |
| 146 | strerror(-result)); |
| 147 | } |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 150 | std::shared_ptr<renderengine::ExternalTexture> RenderSurface::dequeueBuffer( |
| 151 | base::unique_fd* bufferFence) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 152 | SFTRACE_CALL(); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 153 | int fd = -1; |
| 154 | ANativeWindowBuffer* buffer = nullptr; |
| 155 | |
| 156 | status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd); |
| 157 | |
| 158 | if (result != NO_ERROR) { |
| 159 | ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d", |
| 160 | mDisplay.getName().c_str(), result); |
| 161 | // Return fast here as we can't do much more - any rendering we do |
| 162 | // now will just be wrong. |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 163 | return mTexture; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 166 | ALOGW_IF(mTexture != nullptr, "Clobbering a non-null pointer to a buffer [%p].", |
| 167 | mTexture->getBuffer()->getNativeBuffer()->handle); |
| 168 | |
| 169 | sp<GraphicBuffer> newBuffer = GraphicBuffer::from(buffer); |
| 170 | |
| 171 | std::shared_ptr<renderengine::ExternalTexture> texture; |
| 172 | |
| 173 | for (auto it = mTextureCache.begin(); it != mTextureCache.end(); it++) { |
| 174 | const auto& cachedTexture = *it; |
| 175 | if (cachedTexture->getBuffer()->getId() == newBuffer->getId()) { |
| 176 | texture = cachedTexture; |
| 177 | mTextureCache.erase(it); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (texture) { |
| 183 | mTexture = texture; |
| 184 | } else { |
| 185 | mTexture = std::make_shared< |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 186 | renderengine::impl::ExternalTexture>(GraphicBuffer::from(buffer), |
| 187 | mCompositionEngine.getRenderEngine(), |
| 188 | renderengine::impl::ExternalTexture::Usage:: |
| 189 | WRITEABLE); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 190 | } |
| 191 | mTextureCache.push_back(mTexture); |
| 192 | if (mTextureCache.size() > mMaxTextureCacheSize) { |
| 193 | mTextureCache.erase(mTextureCache.begin()); |
| 194 | } |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 195 | |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 196 | *bufferFence = base::unique_fd(fd); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 197 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 198 | return mTexture; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Alec Mouri | f97df4d | 2023-09-06 02:10:05 +0000 | [diff] [blame] | 201 | void RenderSurface::queueBuffer(base::unique_fd readyFence, float hdrSdrRatio) { |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 202 | auto& state = mDisplay.getState(); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 203 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 204 | if (state.usesClientComposition || state.flipClientTarget) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 205 | // hasFlipClientTargetRequest could return true even if we haven't |
| 206 | // dequeued a buffer before. Try dequeueing one if we don't have a |
| 207 | // buffer ready. |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 208 | if (mTexture == nullptr) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 209 | ALOGI("Attempting to queue a client composited buffer without one " |
| 210 | "previously dequeued for display [%s]. Attempting to dequeue " |
| 211 | "a scratch buffer now", |
| 212 | mDisplay.getName().c_str()); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 213 | // We shouldn't deadlock here, since mTexture == nullptr only |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 214 | // after a successful call to queueBuffer, or if dequeueBuffer has |
| 215 | // never been called. |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 216 | base::unique_fd unused; |
| 217 | dequeueBuffer(&unused); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 220 | if (mTexture == nullptr) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 221 | ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str()); |
| 222 | } else { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 223 | status_t result = mNativeWindow->queueBuffer(mNativeWindow.get(), |
| 224 | mTexture->getBuffer()->getNativeBuffer(), |
| 225 | dup(readyFence)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 226 | if (result != NO_ERROR) { |
| 227 | ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(), |
| 228 | result); |
| 229 | // We risk blocking on dequeueBuffer if the primary display failed |
| 230 | // to queue up its buffer, so crash here. |
| 231 | if (!mDisplay.isVirtual()) { |
| 232 | LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result); |
| 233 | } else { |
| 234 | mNativeWindow->cancelBuffer(mNativeWindow.get(), |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 235 | mTexture->getBuffer()->getNativeBuffer(), |
| 236 | dup(readyFence)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 240 | mTexture = nullptr; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Alec Mouri | f97df4d | 2023-09-06 02:10:05 +0000 | [diff] [blame] | 244 | status_t result = mDisplaySurface->advanceFrame(hdrSdrRatio); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 245 | if (result != NO_ERROR) { |
| 246 | ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void RenderSurface::onPresentDisplayCompleted() { |
| 251 | mDisplaySurface->onFrameCommitted(); |
| 252 | } |
| 253 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 254 | void RenderSurface::dump(std::string& out) const { |
| 255 | using android::base::StringAppendF; |
| 256 | |
| 257 | out.append(" Composition RenderSurface State:"); |
| 258 | |
| 259 | out.append("\n "); |
| 260 | |
| 261 | dumpVal(out, "size", mSize); |
| 262 | StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(), |
| 263 | ANativeWindow_getFormat(mNativeWindow.get())); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 264 | out.append("\n"); |
| 265 | |
| 266 | String8 surfaceDump; |
| 267 | mDisplaySurface->dumpAsString(surfaceDump); |
| 268 | out.append(surfaceDump); |
| 269 | } |
| 270 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 271 | void RenderSurface::setSizeForTest(const ui::Size& size) { |
| 272 | mSize = size; |
| 273 | } |
| 274 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 275 | std::shared_ptr<renderengine::ExternalTexture>& RenderSurface::mutableTextureForTest() { |
| 276 | return mTexture; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Vishnu Nair | a314038 | 2022-02-24 14:07:11 -0800 | [diff] [blame] | 279 | bool RenderSurface::supportsCompositionStrategyPrediction() const { |
| 280 | return mDisplaySurface->supportsCompositionStrategyPrediction(); |
| 281 | } |
| 282 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 283 | } // namespace impl |
| 284 | } // namespace android::compositionengine |