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 | |
| 17 | #include <android-base/stringprintf.h> |
| 18 | #include <android/native_window.h> |
| 19 | #include <compositionengine/CompositionEngine.h> |
| 20 | #include <compositionengine/Display.h> |
| 21 | #include <compositionengine/DisplaySurface.h> |
| 22 | #include <compositionengine/RenderSurfaceCreationArgs.h> |
| 23 | #include <compositionengine/impl/DumpHelpers.h> |
| 24 | #include <compositionengine/impl/RenderSurface.h> |
| 25 | #include <log/log.h> |
| 26 | #include <renderengine/RenderEngine.h> |
| 27 | #include <sync/sync.h> |
| 28 | #include <system/window.h> |
| 29 | #include <ui/GraphicBuffer.h> |
| 30 | #include <ui/Rect.h> |
| 31 | |
| 32 | #include "DisplayHardware/HWComposer.h" |
| 33 | |
| 34 | namespace android::compositionengine { |
| 35 | |
| 36 | RenderSurface::~RenderSurface() = default; |
| 37 | |
| 38 | namespace impl { |
| 39 | |
| 40 | std::unique_ptr<compositionengine::RenderSurface> createRenderSurface( |
| 41 | const compositionengine::CompositionEngine& compositionEngine, |
| 42 | compositionengine::Display& display, compositionengine::RenderSurfaceCreationArgs&& args) { |
| 43 | return std::make_unique<RenderSurface>(compositionEngine, display, std::move(args)); |
| 44 | } |
| 45 | |
| 46 | RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display, |
| 47 | RenderSurfaceCreationArgs&& args) |
| 48 | : mCompositionEngine(compositionEngine), |
| 49 | mDisplay(display), |
| 50 | mNativeWindow(args.nativeWindow), |
| 51 | mDisplaySurface(args.displaySurface), |
| 52 | mSize(args.displayWidth, args.displayHeight) {} |
| 53 | |
| 54 | RenderSurface::~RenderSurface() = default; |
| 55 | |
| 56 | bool RenderSurface::isValid() const { |
| 57 | return mSize.isValid(); |
| 58 | } |
| 59 | |
| 60 | void RenderSurface::initialize() { |
| 61 | ANativeWindow* const window = mNativeWindow.get(); |
| 62 | |
| 63 | int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL); |
| 64 | ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status); |
| 65 | status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888); |
| 66 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status); |
| 67 | status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER); |
| 68 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status); |
| 69 | } |
| 70 | |
| 71 | const ui::Size& RenderSurface::getSize() const { |
| 72 | return mSize; |
| 73 | } |
| 74 | |
| 75 | const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const { |
| 76 | return mDisplaySurface->getClientTargetAcquireFence(); |
| 77 | } |
| 78 | |
| 79 | void RenderSurface::setDisplaySize(const ui::Size& size) { |
| 80 | mDisplaySurface->resizeBuffers(size.width, size.height); |
| 81 | mSize = size; |
| 82 | } |
| 83 | |
| 84 | void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) { |
| 85 | native_window_set_buffers_data_space(mNativeWindow.get(), |
| 86 | static_cast<android_dataspace>(dataspace)); |
| 87 | } |
| 88 | |
| 89 | void RenderSurface::setProtected(bool useProtected) { |
| 90 | uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER; |
| 91 | if (useProtected) { |
| 92 | usageFlags |= GRALLOC_USAGE_PROTECTED; |
| 93 | } |
| 94 | const int status = native_window_set_usage(mNativeWindow.get(), usageFlags); |
| 95 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status); |
| 96 | } |
| 97 | |
| 98 | status_t RenderSurface::beginFrame(bool mustRecompose) { |
| 99 | return mDisplaySurface->beginFrame(mustRecompose); |
| 100 | } |
| 101 | |
| 102 | status_t RenderSurface::prepareFrame(std::vector<CompositionInfo>& compositionData) { |
| 103 | auto& hwc = mCompositionEngine.getHwComposer(); |
| 104 | const auto id = mDisplay.getId(); |
| 105 | if (id) { |
| 106 | status_t error = hwc.prepare(*id, compositionData); |
| 107 | if (error != NO_ERROR) { |
| 108 | return error; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | DisplaySurface::CompositionType compositionType; |
| 113 | const bool hasClient = hwc.hasClientComposition(id); |
| 114 | const bool hasDevice = hwc.hasDeviceComposition(id); |
| 115 | if (hasClient && hasDevice) { |
| 116 | compositionType = DisplaySurface::COMPOSITION_MIXED; |
| 117 | } else if (hasClient) { |
| 118 | compositionType = DisplaySurface::COMPOSITION_GLES; |
| 119 | } else if (hasDevice) { |
| 120 | compositionType = DisplaySurface::COMPOSITION_HWC; |
| 121 | } else { |
| 122 | // Nothing to do -- when turning the screen off we get a frame like |
| 123 | // this. Call it a HWC frame since we won't be doing any GLES work but |
| 124 | // will do a prepare/set cycle. |
| 125 | compositionType = DisplaySurface::COMPOSITION_HWC; |
| 126 | } |
| 127 | return mDisplaySurface->prepareFrame(compositionType); |
| 128 | } |
| 129 | |
| 130 | sp<GraphicBuffer> RenderSurface::dequeueBuffer() { |
| 131 | int fd = -1; |
| 132 | ANativeWindowBuffer* buffer = nullptr; |
| 133 | |
| 134 | status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd); |
| 135 | |
| 136 | if (result != NO_ERROR) { |
| 137 | ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d", |
| 138 | mDisplay.getName().c_str(), result); |
| 139 | // Return fast here as we can't do much more - any rendering we do |
| 140 | // now will just be wrong. |
| 141 | return mGraphicBuffer; |
| 142 | } |
| 143 | |
| 144 | ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].", |
| 145 | mGraphicBuffer->getNativeBuffer()->handle); |
| 146 | mGraphicBuffer = GraphicBuffer::from(buffer); |
| 147 | |
| 148 | // Block until the buffer is ready |
| 149 | // TODO(alecmouri): it's perhaps more appropriate to block renderengine so |
| 150 | // that the gl driver can block instead. |
| 151 | if (fd >= 0) { |
| 152 | sync_wait(fd, -1); |
| 153 | close(fd); |
| 154 | } |
| 155 | |
| 156 | return mGraphicBuffer; |
| 157 | } |
| 158 | |
| 159 | void RenderSurface::queueBuffer() { |
| 160 | auto& hwc = mCompositionEngine.getHwComposer(); |
| 161 | const auto id = mDisplay.getId(); |
| 162 | |
| 163 | if (hwc.hasClientComposition(id) || hwc.hasFlipClientTargetRequest(id)) { |
| 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. |
| 175 | dequeueBuffer(); |
| 176 | } |
| 177 | |
| 178 | if (mGraphicBuffer == nullptr) { |
| 179 | ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str()); |
| 180 | } else { |
| 181 | status_t result = mNativeWindow->queueBuffer(mNativeWindow.get(), |
| 182 | mGraphicBuffer->getNativeBuffer(), |
| 183 | dup(mBufferReady)); |
| 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(), |
| 193 | mGraphicBuffer->getNativeBuffer(), |
| 194 | dup(mBufferReady)); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | mBufferReady.reset(); |
| 199 | mGraphicBuffer = nullptr; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | status_t result = mDisplaySurface->advanceFrame(); |
| 204 | if (result != NO_ERROR) { |
| 205 | ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void RenderSurface::onPresentDisplayCompleted() { |
| 210 | mDisplaySurface->onFrameCommitted(); |
| 211 | } |
| 212 | |
| 213 | void RenderSurface::setViewportAndProjection() { |
| 214 | auto& renderEngine = mCompositionEngine.getRenderEngine(); |
| 215 | Rect sourceCrop = Rect(mSize); |
| 216 | renderEngine.setViewportAndProjection(mSize.width, mSize.height, sourceCrop, |
| 217 | ui::Transform::ROT_0); |
| 218 | } |
| 219 | |
| 220 | void RenderSurface::finishBuffer() { |
| 221 | auto& renderEngine = mCompositionEngine.getRenderEngine(); |
| 222 | mBufferReady = renderEngine.flush(); |
| 223 | if (mBufferReady.get() < 0) { |
| 224 | renderEngine.finish(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void RenderSurface::flip() { |
| 229 | mPageFlipCount++; |
| 230 | } |
| 231 | |
| 232 | void RenderSurface::dump(std::string& out) const { |
| 233 | using android::base::StringAppendF; |
| 234 | |
| 235 | out.append(" Composition RenderSurface State:"); |
| 236 | |
| 237 | out.append("\n "); |
| 238 | |
| 239 | dumpVal(out, "size", mSize); |
| 240 | StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(), |
| 241 | ANativeWindow_getFormat(mNativeWindow.get())); |
| 242 | dumpVal(out, "flips", mPageFlipCount); |
| 243 | out.append("\n"); |
| 244 | |
| 245 | String8 surfaceDump; |
| 246 | mDisplaySurface->dumpAsString(surfaceDump); |
| 247 | out.append(surfaceDump); |
| 248 | } |
| 249 | |
| 250 | std::uint32_t RenderSurface::getPageFlipCount() const { |
| 251 | return mPageFlipCount; |
| 252 | } |
| 253 | |
| 254 | void RenderSurface::setPageFlipCountForTest(std::uint32_t count) { |
| 255 | mPageFlipCount = count; |
| 256 | } |
| 257 | |
| 258 | void RenderSurface::setSizeForTest(const ui::Size& size) { |
| 259 | mSize = size; |
| 260 | } |
| 261 | |
| 262 | sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() { |
| 263 | return mGraphicBuffer; |
| 264 | } |
| 265 | |
| 266 | base::unique_fd& RenderSurface::mutableBufferReadyForTest() { |
| 267 | return mBufferReady; |
| 268 | } |
| 269 | |
| 270 | } // namespace impl |
| 271 | } // namespace android::compositionengine |