blob: 1ce6b4c1c2ef0c7bc6bcdc46f865387c34725240 [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,
46 compositionengine::Display& display, compositionengine::RenderSurfaceCreationArgs&& args) {
47 return std::make_unique<RenderSurface>(compositionEngine, display, std::move(args));
48}
49
50RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
51 RenderSurfaceCreationArgs&& args)
52 : mCompositionEngine(compositionEngine),
53 mDisplay(display),
54 mNativeWindow(args.nativeWindow),
55 mDisplaySurface(args.displaySurface),
chaviw8beb4142019-04-11 13:09:05 -070056 mSize(args.displayWidth, args.displayHeight) {
57 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
58}
Lloyd Pique31cb2942018-10-19 17:23:03 -070059
chaviw8beb4142019-04-11 13:09:05 -070060RenderSurface::~RenderSurface() {
61 ANativeWindow* const window = mNativeWindow.get();
62 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
63}
Lloyd Pique31cb2942018-10-19 17:23:03 -070064
65bool RenderSurface::isValid() const {
66 return mSize.isValid();
67}
68
69void RenderSurface::initialize() {
70 ANativeWindow* const window = mNativeWindow.get();
71
72 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
73 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
74 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
75 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
76 status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER);
77 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
78}
79
80const ui::Size& RenderSurface::getSize() const {
81 return mSize;
82}
83
84const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
85 return mDisplaySurface->getClientTargetAcquireFence();
86}
87
88void RenderSurface::setDisplaySize(const ui::Size& size) {
89 mDisplaySurface->resizeBuffers(size.width, size.height);
90 mSize = size;
91}
92
93void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
94 native_window_set_buffers_data_space(mNativeWindow.get(),
95 static_cast<android_dataspace>(dataspace));
96}
97
98void RenderSurface::setProtected(bool useProtected) {
99 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
100 if (useProtected) {
101 usageFlags |= GRALLOC_USAGE_PROTECTED;
102 }
103 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
104 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700105 if (status == NO_ERROR) {
106 mProtected = useProtected;
107 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700108}
109
110status_t RenderSurface::beginFrame(bool mustRecompose) {
111 return mDisplaySurface->beginFrame(mustRecompose);
112}
113
Lloyd Pique66d68602019-02-13 14:23:31 -0800114void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700115 DisplaySurface::CompositionType compositionType;
Lloyd Pique66d68602019-02-13 14:23:31 -0800116 if (usesClientComposition && usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700117 compositionType = DisplaySurface::COMPOSITION_MIXED;
Lloyd Pique66d68602019-02-13 14:23:31 -0800118 } else if (usesClientComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700119 compositionType = DisplaySurface::COMPOSITION_GLES;
Lloyd Pique66d68602019-02-13 14:23:31 -0800120 } else if (usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700121 compositionType = DisplaySurface::COMPOSITION_HWC;
122 } else {
123 // Nothing to do -- when turning the screen off we get a frame like
124 // this. Call it a HWC frame since we won't be doing any GLES work but
125 // will do a prepare/set cycle.
126 compositionType = DisplaySurface::COMPOSITION_HWC;
127 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800128
129 if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) {
130 ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result,
131 strerror(-result));
132 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700133}
134
Alec Mouri6338c9d2019-02-07 16:57:51 -0800135sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
136 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700137 int fd = -1;
138 ANativeWindowBuffer* buffer = nullptr;
139
140 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
141
142 if (result != NO_ERROR) {
143 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
144 mDisplay.getName().c_str(), result);
145 // Return fast here as we can't do much more - any rendering we do
146 // now will just be wrong.
147 return mGraphicBuffer;
148 }
149
150 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
151 mGraphicBuffer->getNativeBuffer()->handle);
152 mGraphicBuffer = GraphicBuffer::from(buffer);
153
Alec Mouri6338c9d2019-02-07 16:57:51 -0800154 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700155
156 return mGraphicBuffer;
157}
158
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000159void RenderSurface::queueBuffer(base::unique_fd&& readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800160 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700161
Lloyd Pique66d68602019-02-13 14:23:31 -0800162 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700163 // hasFlipClientTargetRequest could return true even if we haven't
164 // dequeued a buffer before. Try dequeueing one if we don't have a
165 // buffer ready.
166 if (mGraphicBuffer == nullptr) {
167 ALOGI("Attempting to queue a client composited buffer without one "
168 "previously dequeued for display [%s]. Attempting to dequeue "
169 "a scratch buffer now",
170 mDisplay.getName().c_str());
171 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
172 // after a successful call to queueBuffer, or if dequeueBuffer has
173 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800174 base::unique_fd unused;
175 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700176 }
177
178 if (mGraphicBuffer == nullptr) {
179 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
180 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000181 status_t result =
182 mNativeWindow->queueBuffer(mNativeWindow.get(),
183 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184 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(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000193 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700194 }
195 }
196
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197 mGraphicBuffer = nullptr;
198 }
199 }
200
201 status_t result = mDisplaySurface->advanceFrame();
202 if (result != NO_ERROR) {
203 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
204 }
205}
206
207void RenderSurface::onPresentDisplayCompleted() {
208 mDisplaySurface->onFrameCommitted();
209}
210
Lloyd Pique31cb2942018-10-19 17:23:03 -0700211void RenderSurface::flip() {
212 mPageFlipCount++;
213}
214
215void RenderSurface::dump(std::string& out) const {
216 using android::base::StringAppendF;
217
218 out.append(" Composition RenderSurface State:");
219
220 out.append("\n ");
221
222 dumpVal(out, "size", mSize);
223 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
224 ANativeWindow_getFormat(mNativeWindow.get()));
225 dumpVal(out, "flips", mPageFlipCount);
226 out.append("\n");
227
228 String8 surfaceDump;
229 mDisplaySurface->dumpAsString(surfaceDump);
230 out.append(surfaceDump);
231}
232
233std::uint32_t RenderSurface::getPageFlipCount() const {
234 return mPageFlipCount;
235}
236
237void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
238 mPageFlipCount = count;
239}
240
241void RenderSurface::setSizeForTest(const ui::Size& size) {
242 mSize = size;
243}
244
245sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
246 return mGraphicBuffer;
247}
248
Lloyd Pique31cb2942018-10-19 17:23:03 -0700249} // namespace impl
250} // namespace android::compositionengine