blob: e981172581a2c720aaaf1c91ea1daa3e0f11fe68 [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) {
Lloyd Pique0a456232020-01-16 17:51:13 -080094 mDisplaySurface->resizeBuffers(static_cast<uint32_t>(size.width),
95 static_cast<uint32_t>(size.height));
Lloyd Pique31cb2942018-10-19 17:23:03 -070096 mSize = size;
97}
98
99void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
100 native_window_set_buffers_data_space(mNativeWindow.get(),
101 static_cast<android_dataspace>(dataspace));
102}
103
104void RenderSurface::setProtected(bool useProtected) {
105 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
106 if (useProtected) {
107 usageFlags |= GRALLOC_USAGE_PROTECTED;
108 }
109 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
110 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700111 if (status == NO_ERROR) {
112 mProtected = useProtected;
113 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114}
115
116status_t RenderSurface::beginFrame(bool mustRecompose) {
117 return mDisplaySurface->beginFrame(mustRecompose);
118}
119
Lloyd Pique66d68602019-02-13 14:23:31 -0800120void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700121 DisplaySurface::CompositionType compositionType;
Lloyd Pique66d68602019-02-13 14:23:31 -0800122 if (usesClientComposition && usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700123 compositionType = DisplaySurface::COMPOSITION_MIXED;
Lloyd Pique66d68602019-02-13 14:23:31 -0800124 } else if (usesClientComposition) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800125 compositionType = DisplaySurface::COMPOSITION_GPU;
Lloyd Pique66d68602019-02-13 14:23:31 -0800126 } else if (usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700127 compositionType = DisplaySurface::COMPOSITION_HWC;
128 } else {
129 // Nothing to do -- when turning the screen off we get a frame like
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800130 // this. Call it a HWC frame since we won't be doing any GPU work but
Lloyd Pique31cb2942018-10-19 17:23:03 -0700131 // will do a prepare/set cycle.
132 compositionType = DisplaySurface::COMPOSITION_HWC;
133 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800134
135 if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) {
136 ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result,
137 strerror(-result));
138 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700139}
140
Alec Mouri6338c9d2019-02-07 16:57:51 -0800141sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
142 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700143 int fd = -1;
144 ANativeWindowBuffer* buffer = nullptr;
145
146 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
147
148 if (result != NO_ERROR) {
149 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
150 mDisplay.getName().c_str(), result);
151 // Return fast here as we can't do much more - any rendering we do
152 // now will just be wrong.
153 return mGraphicBuffer;
154 }
155
156 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
157 mGraphicBuffer->getNativeBuffer()->handle);
158 mGraphicBuffer = GraphicBuffer::from(buffer);
159
Alec Mouri6338c9d2019-02-07 16:57:51 -0800160 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700161
162 return mGraphicBuffer;
163}
164
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700165void RenderSurface::queueBuffer(base::unique_fd readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800166 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700167
Lloyd Pique66d68602019-02-13 14:23:31 -0800168 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700169 // hasFlipClientTargetRequest could return true even if we haven't
170 // dequeued a buffer before. Try dequeueing one if we don't have a
171 // buffer ready.
172 if (mGraphicBuffer == nullptr) {
173 ALOGI("Attempting to queue a client composited buffer without one "
174 "previously dequeued for display [%s]. Attempting to dequeue "
175 "a scratch buffer now",
176 mDisplay.getName().c_str());
177 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
178 // after a successful call to queueBuffer, or if dequeueBuffer has
179 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800180 base::unique_fd unused;
181 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700182 }
183
184 if (mGraphicBuffer == nullptr) {
185 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
186 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000187 status_t result =
188 mNativeWindow->queueBuffer(mNativeWindow.get(),
189 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700190 if (result != NO_ERROR) {
191 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
192 result);
193 // We risk blocking on dequeueBuffer if the primary display failed
194 // to queue up its buffer, so crash here.
195 if (!mDisplay.isVirtual()) {
196 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
197 } else {
198 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000199 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200 }
201 }
202
Lloyd Pique31cb2942018-10-19 17:23:03 -0700203 mGraphicBuffer = nullptr;
204 }
205 }
206
207 status_t result = mDisplaySurface->advanceFrame();
208 if (result != NO_ERROR) {
209 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
210 }
211}
212
213void RenderSurface::onPresentDisplayCompleted() {
214 mDisplaySurface->onFrameCommitted();
215}
216
Lloyd Pique31cb2942018-10-19 17:23:03 -0700217void RenderSurface::flip() {
218 mPageFlipCount++;
219}
220
221void 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
239std::uint32_t RenderSurface::getPageFlipCount() const {
240 return mPageFlipCount;
241}
242
243void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
244 mPageFlipCount = count;
245}
246
247void RenderSurface::setSizeForTest(const ui::Size& size) {
248 mSize = size;
249}
250
251sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
252 return mGraphicBuffer;
253}
254
Lloyd Pique31cb2942018-10-19 17:23:03 -0700255} // namespace impl
256} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800257
258// TODO(b/129481165): remove the #pragma below and fix conversion issues
259#pragma clang diagnostic pop // ignored "-Wconversion"