blob: 3fcd9d155d19b78915f420baf055b9dd68cb8716 [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>
26#include <compositionengine/impl/RenderSurface.h>
27#include <log/log.h>
28#include <renderengine/RenderEngine.h>
29#include <sync/sync.h>
30#include <system/window.h>
31#include <ui/GraphicBuffer.h>
32#include <ui/Rect.h>
Alec Mouri6338c9d2019-02-07 16:57:51 -080033#include <utils/Trace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070034
35#include "DisplayHardware/HWComposer.h"
36
37namespace android::compositionengine {
38
39RenderSurface::~RenderSurface() = default;
40
41namespace impl {
42
43std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
44 const compositionengine::CompositionEngine& compositionEngine,
45 compositionengine::Display& display, compositionengine::RenderSurfaceCreationArgs&& args) {
46 return std::make_unique<RenderSurface>(compositionEngine, display, std::move(args));
47}
48
49RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
50 RenderSurfaceCreationArgs&& args)
51 : mCompositionEngine(compositionEngine),
52 mDisplay(display),
53 mNativeWindow(args.nativeWindow),
54 mDisplaySurface(args.displaySurface),
chaviw8beb4142019-04-11 13:09:05 -070055 mSize(args.displayWidth, args.displayHeight) {
56 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
57}
Lloyd Pique31cb2942018-10-19 17:23:03 -070058
chaviw8beb4142019-04-11 13:09:05 -070059RenderSurface::~RenderSurface() {
60 ANativeWindow* const window = mNativeWindow.get();
61 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
62}
Lloyd Pique31cb2942018-10-19 17:23:03 -070063
64bool RenderSurface::isValid() const {
65 return mSize.isValid();
66}
67
68void RenderSurface::initialize() {
69 ANativeWindow* const window = mNativeWindow.get();
70
71 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
72 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
73 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
74 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
75 status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER);
76 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
77}
78
79const ui::Size& RenderSurface::getSize() const {
80 return mSize;
81}
82
83const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
84 return mDisplaySurface->getClientTargetAcquireFence();
85}
86
87void RenderSurface::setDisplaySize(const ui::Size& size) {
88 mDisplaySurface->resizeBuffers(size.width, size.height);
89 mSize = size;
90}
91
92void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
93 native_window_set_buffers_data_space(mNativeWindow.get(),
94 static_cast<android_dataspace>(dataspace));
95}
96
97void RenderSurface::setProtected(bool useProtected) {
98 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
99 if (useProtected) {
100 usageFlags |= GRALLOC_USAGE_PROTECTED;
101 }
102 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
103 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700104 if (status == NO_ERROR) {
105 mProtected = useProtected;
106 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700107}
108
109status_t RenderSurface::beginFrame(bool mustRecompose) {
110 return mDisplaySurface->beginFrame(mustRecompose);
111}
112
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800113status_t RenderSurface::prepareFrame() {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114 auto& hwc = mCompositionEngine.getHwComposer();
115 const auto id = mDisplay.getId();
116 if (id) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800117 status_t error = hwc.prepare(*id, mDisplay);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700118 if (error != NO_ERROR) {
119 return error;
120 }
121 }
122
123 DisplaySurface::CompositionType compositionType;
124 const bool hasClient = hwc.hasClientComposition(id);
125 const bool hasDevice = hwc.hasDeviceComposition(id);
126 if (hasClient && hasDevice) {
127 compositionType = DisplaySurface::COMPOSITION_MIXED;
128 } else if (hasClient) {
129 compositionType = DisplaySurface::COMPOSITION_GLES;
130 } else if (hasDevice) {
131 compositionType = DisplaySurface::COMPOSITION_HWC;
132 } else {
133 // Nothing to do -- when turning the screen off we get a frame like
134 // this. Call it a HWC frame since we won't be doing any GLES work but
135 // will do a prepare/set cycle.
136 compositionType = DisplaySurface::COMPOSITION_HWC;
137 }
138 return mDisplaySurface->prepareFrame(compositionType);
139}
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
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000165void RenderSurface::queueBuffer(base::unique_fd&& readyFence) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700166 auto& hwc = mCompositionEngine.getHwComposer();
167 const auto id = mDisplay.getId();
168
169 if (hwc.hasClientComposition(id) || hwc.hasFlipClientTargetRequest(id)) {
170 // hasFlipClientTargetRequest could return true even if we haven't
171 // dequeued a buffer before. Try dequeueing one if we don't have a
172 // buffer ready.
173 if (mGraphicBuffer == nullptr) {
174 ALOGI("Attempting to queue a client composited buffer without one "
175 "previously dequeued for display [%s]. Attempting to dequeue "
176 "a scratch buffer now",
177 mDisplay.getName().c_str());
178 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
179 // after a successful call to queueBuffer, or if dequeueBuffer has
180 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800181 base::unique_fd unused;
182 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700183 }
184
185 if (mGraphicBuffer == nullptr) {
186 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
187 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000188 status_t result =
189 mNativeWindow->queueBuffer(mNativeWindow.get(),
190 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191 if (result != NO_ERROR) {
192 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
193 result);
194 // We risk blocking on dequeueBuffer if the primary display failed
195 // to queue up its buffer, so crash here.
196 if (!mDisplay.isVirtual()) {
197 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
198 } else {
199 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000200 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201 }
202 }
203
Lloyd Pique31cb2942018-10-19 17:23:03 -0700204 mGraphicBuffer = nullptr;
205 }
206 }
207
208 status_t result = mDisplaySurface->advanceFrame();
209 if (result != NO_ERROR) {
210 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
211 }
212}
213
214void RenderSurface::onPresentDisplayCompleted() {
215 mDisplaySurface->onFrameCommitted();
216}
217
218void RenderSurface::setViewportAndProjection() {
219 auto& renderEngine = mCompositionEngine.getRenderEngine();
220 Rect sourceCrop = Rect(mSize);
221 renderEngine.setViewportAndProjection(mSize.width, mSize.height, sourceCrop,
222 ui::Transform::ROT_0);
223}
224
Lloyd Pique31cb2942018-10-19 17:23:03 -0700225void RenderSurface::flip() {
226 mPageFlipCount++;
227}
228
229void RenderSurface::dump(std::string& out) const {
230 using android::base::StringAppendF;
231
232 out.append(" Composition RenderSurface State:");
233
234 out.append("\n ");
235
236 dumpVal(out, "size", mSize);
237 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
238 ANativeWindow_getFormat(mNativeWindow.get()));
239 dumpVal(out, "flips", mPageFlipCount);
240 out.append("\n");
241
242 String8 surfaceDump;
243 mDisplaySurface->dumpAsString(surfaceDump);
244 out.append(surfaceDump);
245}
246
247std::uint32_t RenderSurface::getPageFlipCount() const {
248 return mPageFlipCount;
249}
250
251void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
252 mPageFlipCount = count;
253}
254
255void RenderSurface::setSizeForTest(const ui::Size& size) {
256 mSize = size;
257}
258
259sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
260 return mGraphicBuffer;
261}
262
Lloyd Pique31cb2942018-10-19 17:23:03 -0700263} // namespace impl
264} // namespace android::compositionengine