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