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