blob: 949feb4041cf00d57f8217dc0e1b6bde97bff47d [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
Alec Mouri6338c9d2019-02-07 16:57:51 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Lloyd Pique31cb2942018-10-19 17:23:03 -070019#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 Pique66d68602019-02-13 14:23:31 -080026#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070027#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 Mouri6338c9d2019-02-07 16:57:51 -080034#include <utils/Trace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070035
36#include "DisplayHardware/HWComposer.h"
37
38namespace android::compositionengine {
39
40RenderSurface::~RenderSurface() = default;
41
42namespace impl {
43
44std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
45 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070046 compositionengine::Display& display,
47 const compositionengine::RenderSurfaceCreationArgs& args) {
48 return std::make_unique<RenderSurface>(compositionEngine, display, args);
Lloyd Pique31cb2942018-10-19 17:23:03 -070049}
50
51RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070052 const RenderSurfaceCreationArgs& args)
Lloyd Pique31cb2942018-10-19 17:23:03 -070053 : mCompositionEngine(compositionEngine),
54 mDisplay(display),
55 mNativeWindow(args.nativeWindow),
56 mDisplaySurface(args.displaySurface),
chaviw8beb4142019-04-11 13:09:05 -070057 mSize(args.displayWidth, args.displayHeight) {
58 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
59}
Lloyd Pique31cb2942018-10-19 17:23:03 -070060
chaviw8beb4142019-04-11 13:09:05 -070061RenderSurface::~RenderSurface() {
62 ANativeWindow* const window = mNativeWindow.get();
63 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
64}
Lloyd Pique31cb2942018-10-19 17:23:03 -070065
66bool RenderSurface::isValid() const {
67 return mSize.isValid();
68}
69
70void RenderSurface::initialize() {
71 ANativeWindow* const window = mNativeWindow.get();
72
73 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
74 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
75 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
76 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
77 status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER);
78 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
79}
80
81const ui::Size& RenderSurface::getSize() const {
82 return mSize;
83}
84
85const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
86 return mDisplaySurface->getClientTargetAcquireFence();
87}
88
89void RenderSurface::setDisplaySize(const ui::Size& size) {
90 mDisplaySurface->resizeBuffers(size.width, size.height);
91 mSize = size;
92}
93
94void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
95 native_window_set_buffers_data_space(mNativeWindow.get(),
96 static_cast<android_dataspace>(dataspace));
97}
98
99void RenderSurface::setProtected(bool useProtected) {
100 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
101 if (useProtected) {
102 usageFlags |= GRALLOC_USAGE_PROTECTED;
103 }
104 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
105 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700106 if (status == NO_ERROR) {
107 mProtected = useProtected;
108 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700109}
110
111status_t RenderSurface::beginFrame(bool mustRecompose) {
112 return mDisplaySurface->beginFrame(mustRecompose);
113}
114
Lloyd Pique66d68602019-02-13 14:23:31 -0800115void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700116 DisplaySurface::CompositionType compositionType;
Lloyd Pique66d68602019-02-13 14:23:31 -0800117 if (usesClientComposition && usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700118 compositionType = DisplaySurface::COMPOSITION_MIXED;
Lloyd Pique66d68602019-02-13 14:23:31 -0800119 } else if (usesClientComposition) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800120 compositionType = DisplaySurface::COMPOSITION_GPU;
Lloyd Pique66d68602019-02-13 14:23:31 -0800121 } else if (usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700122 compositionType = DisplaySurface::COMPOSITION_HWC;
123 } else {
124 // Nothing to do -- when turning the screen off we get a frame like
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800125 // this. Call it a HWC frame since we won't be doing any GPU work but
Lloyd Pique31cb2942018-10-19 17:23:03 -0700126 // will do a prepare/set cycle.
127 compositionType = DisplaySurface::COMPOSITION_HWC;
128 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800129
130 if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) {
131 ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result,
132 strerror(-result));
133 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700134}
135
Alec Mouri6338c9d2019-02-07 16:57:51 -0800136sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
137 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700138 int fd = -1;
139 ANativeWindowBuffer* buffer = nullptr;
140
141 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
142
143 if (result != NO_ERROR) {
144 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
145 mDisplay.getName().c_str(), result);
146 // Return fast here as we can't do much more - any rendering we do
147 // now will just be wrong.
148 return mGraphicBuffer;
149 }
150
151 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
152 mGraphicBuffer->getNativeBuffer()->handle);
153 mGraphicBuffer = GraphicBuffer::from(buffer);
154
Alec Mouri6338c9d2019-02-07 16:57:51 -0800155 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700156
157 return mGraphicBuffer;
158}
159
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700160void RenderSurface::queueBuffer(base::unique_fd readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800161 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700162
Lloyd Pique66d68602019-02-13 14:23:31 -0800163 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700164 // 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.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800175 base::unique_fd unused;
176 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700177 }
178
179 if (mGraphicBuffer == nullptr) {
180 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
181 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000182 status_t result =
183 mNativeWindow->queueBuffer(mNativeWindow.get(),
184 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700185 if (result != NO_ERROR) {
186 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
187 result);
188 // We risk blocking on dequeueBuffer if the primary display failed
189 // to queue up its buffer, so crash here.
190 if (!mDisplay.isVirtual()) {
191 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
192 } else {
193 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000194 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700195 }
196 }
197
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 mGraphicBuffer = nullptr;
199 }
200 }
201
202 status_t result = mDisplaySurface->advanceFrame();
203 if (result != NO_ERROR) {
204 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
205 }
206}
207
208void RenderSurface::onPresentDisplayCompleted() {
209 mDisplaySurface->onFrameCommitted();
210}
211
Lloyd Pique31cb2942018-10-19 17:23:03 -0700212void RenderSurface::flip() {
213 mPageFlipCount++;
214}
215
216void RenderSurface::dump(std::string& out) const {
217 using android::base::StringAppendF;
218
219 out.append(" Composition RenderSurface State:");
220
221 out.append("\n ");
222
223 dumpVal(out, "size", mSize);
224 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
225 ANativeWindow_getFormat(mNativeWindow.get()));
226 dumpVal(out, "flips", mPageFlipCount);
227 out.append("\n");
228
229 String8 surfaceDump;
230 mDisplaySurface->dumpAsString(surfaceDump);
231 out.append(surfaceDump);
232}
233
234std::uint32_t RenderSurface::getPageFlipCount() const {
235 return mPageFlipCount;
236}
237
238void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
239 mPageFlipCount = count;
240}
241
242void RenderSurface::setSizeForTest(const ui::Size& size) {
243 mSize = size;
244}
245
246sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
247 return mGraphicBuffer;
248}
249
Lloyd Pique31cb2942018-10-19 17:23:03 -0700250} // namespace impl
251} // namespace android::compositionengine