blob: 10512eb9e94728bc7c906150da392a9090554349 [file] [log] [blame]
Lloyd Pique31cb2942018-10-19 17:23:03 -07001/*
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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Alec Mouri6338c9d2019-02-07 16:57:51 -080021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Lloyd Pique31cb2942018-10-19 17:23:03 -070023#include <android-base/stringprintf.h>
24#include <android/native_window.h>
25#include <compositionengine/CompositionEngine.h>
26#include <compositionengine/Display.h>
27#include <compositionengine/DisplaySurface.h>
28#include <compositionengine/RenderSurfaceCreationArgs.h>
29#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080030#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070031#include <compositionengine/impl/RenderSurface.h>
32#include <log/log.h>
33#include <renderengine/RenderEngine.h>
34#include <sync/sync.h>
35#include <system/window.h>
36#include <ui/GraphicBuffer.h>
37#include <ui/Rect.h>
Alec Mouri6338c9d2019-02-07 16:57:51 -080038#include <utils/Trace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070039
40#include "DisplayHardware/HWComposer.h"
41
42namespace android::compositionengine {
43
44RenderSurface::~RenderSurface() = default;
45
46namespace impl {
47
48std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
49 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070050 compositionengine::Display& display,
51 const compositionengine::RenderSurfaceCreationArgs& args) {
52 return std::make_unique<RenderSurface>(compositionEngine, display, args);
Lloyd Pique31cb2942018-10-19 17:23:03 -070053}
54
55RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070056 const RenderSurfaceCreationArgs& args)
Lloyd Pique31cb2942018-10-19 17:23:03 -070057 : mCompositionEngine(compositionEngine),
58 mDisplay(display),
59 mNativeWindow(args.nativeWindow),
60 mDisplaySurface(args.displaySurface),
chaviw8beb4142019-04-11 13:09:05 -070061 mSize(args.displayWidth, args.displayHeight) {
62 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
63}
Lloyd Pique31cb2942018-10-19 17:23:03 -070064
chaviw8beb4142019-04-11 13:09:05 -070065RenderSurface::~RenderSurface() {
66 ANativeWindow* const window = mNativeWindow.get();
67 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
68}
Lloyd Pique31cb2942018-10-19 17:23:03 -070069
70bool RenderSurface::isValid() const {
71 return mSize.isValid();
72}
73
74void RenderSurface::initialize() {
75 ANativeWindow* const window = mNativeWindow.get();
76
77 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
78 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
79 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
80 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
81 status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER);
82 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
83}
84
85const ui::Size& RenderSurface::getSize() const {
86 return mSize;
87}
88
89const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
90 return mDisplaySurface->getClientTargetAcquireFence();
91}
92
93void RenderSurface::setDisplaySize(const ui::Size& size) {
94 mDisplaySurface->resizeBuffers(size.width, size.height);
95 mSize = size;
96}
97
98void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
99 native_window_set_buffers_data_space(mNativeWindow.get(),
100 static_cast<android_dataspace>(dataspace));
101}
102
103void RenderSurface::setProtected(bool useProtected) {
104 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
105 if (useProtected) {
106 usageFlags |= GRALLOC_USAGE_PROTECTED;
107 }
108 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
109 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700110 if (status == NO_ERROR) {
111 mProtected = useProtected;
112 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700113}
114
115status_t RenderSurface::beginFrame(bool mustRecompose) {
116 return mDisplaySurface->beginFrame(mustRecompose);
117}
118
Lloyd Pique66d68602019-02-13 14:23:31 -0800119void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700120 DisplaySurface::CompositionType compositionType;
Lloyd Pique66d68602019-02-13 14:23:31 -0800121 if (usesClientComposition && usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700122 compositionType = DisplaySurface::COMPOSITION_MIXED;
Lloyd Pique66d68602019-02-13 14:23:31 -0800123 } else if (usesClientComposition) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800124 compositionType = DisplaySurface::COMPOSITION_GPU;
Lloyd Pique66d68602019-02-13 14:23:31 -0800125 } else if (usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700126 compositionType = DisplaySurface::COMPOSITION_HWC;
127 } else {
128 // Nothing to do -- when turning the screen off we get a frame like
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800129 // this. Call it a HWC frame since we won't be doing any GPU work but
Lloyd Pique31cb2942018-10-19 17:23:03 -0700130 // will do a prepare/set cycle.
131 compositionType = DisplaySurface::COMPOSITION_HWC;
132 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800133
134 if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) {
135 ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result,
136 strerror(-result));
137 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700138}
139
Alec Mouri6338c9d2019-02-07 16:57:51 -0800140sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
141 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700142 int fd = -1;
143 ANativeWindowBuffer* buffer = nullptr;
144
145 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
146
147 if (result != NO_ERROR) {
148 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
149 mDisplay.getName().c_str(), result);
150 // Return fast here as we can't do much more - any rendering we do
151 // now will just be wrong.
152 return mGraphicBuffer;
153 }
154
155 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
156 mGraphicBuffer->getNativeBuffer()->handle);
157 mGraphicBuffer = GraphicBuffer::from(buffer);
158
Alec Mouri6338c9d2019-02-07 16:57:51 -0800159 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700160
161 return mGraphicBuffer;
162}
163
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700164void RenderSurface::queueBuffer(base::unique_fd readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800165 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700166
Lloyd Pique66d68602019-02-13 14:23:31 -0800167 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700168 // hasFlipClientTargetRequest could return true even if we haven't
169 // dequeued a buffer before. Try dequeueing one if we don't have a
170 // buffer ready.
171 if (mGraphicBuffer == nullptr) {
172 ALOGI("Attempting to queue a client composited buffer without one "
173 "previously dequeued for display [%s]. Attempting to dequeue "
174 "a scratch buffer now",
175 mDisplay.getName().c_str());
176 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
177 // after a successful call to queueBuffer, or if dequeueBuffer has
178 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800179 base::unique_fd unused;
180 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700181 }
182
183 if (mGraphicBuffer == nullptr) {
184 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
185 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000186 status_t result =
187 mNativeWindow->queueBuffer(mNativeWindow.get(),
188 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700189 if (result != NO_ERROR) {
190 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
191 result);
192 // We risk blocking on dequeueBuffer if the primary display failed
193 // to queue up its buffer, so crash here.
194 if (!mDisplay.isVirtual()) {
195 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
196 } else {
197 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000198 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700199 }
200 }
201
Lloyd Pique31cb2942018-10-19 17:23:03 -0700202 mGraphicBuffer = nullptr;
203 }
204 }
205
206 status_t result = mDisplaySurface->advanceFrame();
207 if (result != NO_ERROR) {
208 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
209 }
210}
211
212void RenderSurface::onPresentDisplayCompleted() {
213 mDisplaySurface->onFrameCommitted();
214}
215
Lloyd Pique31cb2942018-10-19 17:23:03 -0700216void RenderSurface::flip() {
217 mPageFlipCount++;
218}
219
220void RenderSurface::dump(std::string& out) const {
221 using android::base::StringAppendF;
222
223 out.append(" Composition RenderSurface State:");
224
225 out.append("\n ");
226
227 dumpVal(out, "size", mSize);
228 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
229 ANativeWindow_getFormat(mNativeWindow.get()));
230 dumpVal(out, "flips", mPageFlipCount);
231 out.append("\n");
232
233 String8 surfaceDump;
234 mDisplaySurface->dumpAsString(surfaceDump);
235 out.append(surfaceDump);
236}
237
238std::uint32_t RenderSurface::getPageFlipCount() const {
239 return mPageFlipCount;
240}
241
242void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
243 mPageFlipCount = count;
244}
245
246void RenderSurface::setSizeForTest(const ui::Size& size) {
247 mSize = size;
248}
249
250sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
251 return mGraphicBuffer;
252}
253
Lloyd Pique31cb2942018-10-19 17:23:03 -0700254} // namespace impl
255} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800256
257// TODO(b/129481165): remove the #pragma below and fix conversion issues
258#pragma clang diagnostic pop // ignored "-Wconversion"