blob: 3f841d241bbbdaff8b61b110ad6513b07ef36a5f [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
17#include <android-base/stringprintf.h>
18#include <android/native_window.h>
19#include <compositionengine/CompositionEngine.h>
20#include <compositionengine/Display.h>
21#include <compositionengine/DisplaySurface.h>
22#include <compositionengine/RenderSurfaceCreationArgs.h>
23#include <compositionengine/impl/DumpHelpers.h>
24#include <compositionengine/impl/RenderSurface.h>
25#include <log/log.h>
26#include <renderengine/RenderEngine.h>
27#include <sync/sync.h>
28#include <system/window.h>
29#include <ui/GraphicBuffer.h>
30#include <ui/Rect.h>
31
32#include "DisplayHardware/HWComposer.h"
33
34namespace android::compositionengine {
35
36RenderSurface::~RenderSurface() = default;
37
38namespace impl {
39
40std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
41 const compositionengine::CompositionEngine& compositionEngine,
42 compositionengine::Display& display, compositionengine::RenderSurfaceCreationArgs&& args) {
43 return std::make_unique<RenderSurface>(compositionEngine, display, std::move(args));
44}
45
46RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
47 RenderSurfaceCreationArgs&& args)
48 : mCompositionEngine(compositionEngine),
49 mDisplay(display),
50 mNativeWindow(args.nativeWindow),
51 mDisplaySurface(args.displaySurface),
52 mSize(args.displayWidth, args.displayHeight) {}
53
54RenderSurface::~RenderSurface() = default;
55
56bool RenderSurface::isValid() const {
57 return mSize.isValid();
58}
59
60void RenderSurface::initialize() {
61 ANativeWindow* const window = mNativeWindow.get();
62
63 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
64 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
65 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
66 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
67 status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER);
68 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
69}
70
71const ui::Size& RenderSurface::getSize() const {
72 return mSize;
73}
74
75const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
76 return mDisplaySurface->getClientTargetAcquireFence();
77}
78
79void RenderSurface::setDisplaySize(const ui::Size& size) {
80 mDisplaySurface->resizeBuffers(size.width, size.height);
81 mSize = size;
82}
83
84void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
85 native_window_set_buffers_data_space(mNativeWindow.get(),
86 static_cast<android_dataspace>(dataspace));
87}
88
89void RenderSurface::setProtected(bool useProtected) {
90 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
91 if (useProtected) {
92 usageFlags |= GRALLOC_USAGE_PROTECTED;
93 }
94 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
95 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
96}
97
98status_t RenderSurface::beginFrame(bool mustRecompose) {
99 return mDisplaySurface->beginFrame(mustRecompose);
100}
101
102status_t RenderSurface::prepareFrame(std::vector<CompositionInfo>& compositionData) {
103 auto& hwc = mCompositionEngine.getHwComposer();
104 const auto id = mDisplay.getId();
105 if (id) {
106 status_t error = hwc.prepare(*id, compositionData);
107 if (error != NO_ERROR) {
108 return error;
109 }
110 }
111
112 DisplaySurface::CompositionType compositionType;
113 const bool hasClient = hwc.hasClientComposition(id);
114 const bool hasDevice = hwc.hasDeviceComposition(id);
115 if (hasClient && hasDevice) {
116 compositionType = DisplaySurface::COMPOSITION_MIXED;
117 } else if (hasClient) {
118 compositionType = DisplaySurface::COMPOSITION_GLES;
119 } else if (hasDevice) {
120 compositionType = DisplaySurface::COMPOSITION_HWC;
121 } else {
122 // Nothing to do -- when turning the screen off we get a frame like
123 // this. Call it a HWC frame since we won't be doing any GLES work but
124 // will do a prepare/set cycle.
125 compositionType = DisplaySurface::COMPOSITION_HWC;
126 }
127 return mDisplaySurface->prepareFrame(compositionType);
128}
129
130sp<GraphicBuffer> RenderSurface::dequeueBuffer() {
131 int fd = -1;
132 ANativeWindowBuffer* buffer = nullptr;
133
134 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
135
136 if (result != NO_ERROR) {
137 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
138 mDisplay.getName().c_str(), result);
139 // Return fast here as we can't do much more - any rendering we do
140 // now will just be wrong.
141 return mGraphicBuffer;
142 }
143
144 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
145 mGraphicBuffer->getNativeBuffer()->handle);
146 mGraphicBuffer = GraphicBuffer::from(buffer);
147
148 // Block until the buffer is ready
149 // TODO(alecmouri): it's perhaps more appropriate to block renderengine so
150 // that the gl driver can block instead.
151 if (fd >= 0) {
152 sync_wait(fd, -1);
153 close(fd);
154 }
155
156 return mGraphicBuffer;
157}
158
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000159void RenderSurface::queueBuffer(base::unique_fd&& readyFence) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700160 auto& hwc = mCompositionEngine.getHwComposer();
161 const auto id = mDisplay.getId();
162
163 if (hwc.hasClientComposition(id) || hwc.hasFlipClientTargetRequest(id)) {
164 // 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.
175 dequeueBuffer();
176 }
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
211void RenderSurface::setViewportAndProjection() {
212 auto& renderEngine = mCompositionEngine.getRenderEngine();
213 Rect sourceCrop = Rect(mSize);
214 renderEngine.setViewportAndProjection(mSize.width, mSize.height, sourceCrop,
215 ui::Transform::ROT_0);
216}
217
Lloyd Pique31cb2942018-10-19 17:23:03 -0700218void RenderSurface::flip() {
219 mPageFlipCount++;
220}
221
222void RenderSurface::dump(std::string& out) const {
223 using android::base::StringAppendF;
224
225 out.append(" Composition RenderSurface State:");
226
227 out.append("\n ");
228
229 dumpVal(out, "size", mSize);
230 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
231 ANativeWindow_getFormat(mNativeWindow.get()));
232 dumpVal(out, "flips", mPageFlipCount);
233 out.append("\n");
234
235 String8 surfaceDump;
236 mDisplaySurface->dumpAsString(surfaceDump);
237 out.append(surfaceDump);
238}
239
240std::uint32_t RenderSurface::getPageFlipCount() const {
241 return mPageFlipCount;
242}
243
244void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
245 mPageFlipCount = count;
246}
247
248void RenderSurface::setSizeForTest(const ui::Size& size) {
249 mSize = size;
250}
251
252sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
253 return mGraphicBuffer;
254}
255
Lloyd Pique31cb2942018-10-19 17:23:03 -0700256} // namespace impl
257} // namespace android::compositionengine