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