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