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