blob: b5a66788e624f835df8867b5633be92d84a6ec47 [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);
104}
105
106status_t RenderSurface::beginFrame(bool mustRecompose) {
107 return mDisplaySurface->beginFrame(mustRecompose);
108}
109
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800110status_t RenderSurface::prepareFrame() {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700111 auto& hwc = mCompositionEngine.getHwComposer();
112 const auto id = mDisplay.getId();
113 if (id) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800114 status_t error = hwc.prepare(*id, mDisplay);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700115 if (error != NO_ERROR) {
116 return error;
117 }
118 }
119
120 DisplaySurface::CompositionType compositionType;
121 const bool hasClient = hwc.hasClientComposition(id);
122 const bool hasDevice = hwc.hasDeviceComposition(id);
123 if (hasClient && hasDevice) {
124 compositionType = DisplaySurface::COMPOSITION_MIXED;
125 } else if (hasClient) {
126 compositionType = DisplaySurface::COMPOSITION_GLES;
127 } else if (hasDevice) {
128 compositionType = DisplaySurface::COMPOSITION_HWC;
129 } else {
130 // Nothing to do -- when turning the screen off we get a frame like
131 // this. Call it a HWC frame since we won't be doing any GLES work but
132 // will do a prepare/set cycle.
133 compositionType = DisplaySurface::COMPOSITION_HWC;
134 }
135 return mDisplaySurface->prepareFrame(compositionType);
136}
137
Alec Mouri6338c9d2019-02-07 16:57:51 -0800138sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
139 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700140 int fd = -1;
141 ANativeWindowBuffer* buffer = nullptr;
142
143 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
144
145 if (result != NO_ERROR) {
146 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
147 mDisplay.getName().c_str(), result);
148 // Return fast here as we can't do much more - any rendering we do
149 // now will just be wrong.
150 return mGraphicBuffer;
151 }
152
153 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
154 mGraphicBuffer->getNativeBuffer()->handle);
155 mGraphicBuffer = GraphicBuffer::from(buffer);
156
Alec Mouri6338c9d2019-02-07 16:57:51 -0800157 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700158
159 return mGraphicBuffer;
160}
161
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000162void RenderSurface::queueBuffer(base::unique_fd&& readyFence) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700163 auto& hwc = mCompositionEngine.getHwComposer();
164 const auto id = mDisplay.getId();
165
166 if (hwc.hasClientComposition(id) || hwc.hasFlipClientTargetRequest(id)) {
167 // hasFlipClientTargetRequest could return true even if we haven't
168 // dequeued a buffer before. Try dequeueing one if we don't have a
169 // buffer ready.
170 if (mGraphicBuffer == nullptr) {
171 ALOGI("Attempting to queue a client composited buffer without one "
172 "previously dequeued for display [%s]. Attempting to dequeue "
173 "a scratch buffer now",
174 mDisplay.getName().c_str());
175 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
176 // after a successful call to queueBuffer, or if dequeueBuffer has
177 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800178 base::unique_fd unused;
179 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700180 }
181
182 if (mGraphicBuffer == nullptr) {
183 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
184 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000185 status_t result =
186 mNativeWindow->queueBuffer(mNativeWindow.get(),
187 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700188 if (result != NO_ERROR) {
189 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
190 result);
191 // We risk blocking on dequeueBuffer if the primary display failed
192 // to queue up its buffer, so crash here.
193 if (!mDisplay.isVirtual()) {
194 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
195 } else {
196 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000197 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 }
199 }
200
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201 mGraphicBuffer = nullptr;
202 }
203 }
204
205 status_t result = mDisplaySurface->advanceFrame();
206 if (result != NO_ERROR) {
207 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
208 }
209}
210
211void RenderSurface::onPresentDisplayCompleted() {
212 mDisplaySurface->onFrameCommitted();
213}
214
215void RenderSurface::setViewportAndProjection() {
216 auto& renderEngine = mCompositionEngine.getRenderEngine();
217 Rect sourceCrop = Rect(mSize);
218 renderEngine.setViewportAndProjection(mSize.width, mSize.height, sourceCrop,
219 ui::Transform::ROT_0);
220}
221
Lloyd Pique31cb2942018-10-19 17:23:03 -0700222void RenderSurface::flip() {
223 mPageFlipCount++;
224}
225
226void RenderSurface::dump(std::string& out) const {
227 using android::base::StringAppendF;
228
229 out.append(" Composition RenderSurface State:");
230
231 out.append("\n ");
232
233 dumpVal(out, "size", mSize);
234 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
235 ANativeWindow_getFormat(mNativeWindow.get()));
236 dumpVal(out, "flips", mPageFlipCount);
237 out.append("\n");
238
239 String8 surfaceDump;
240 mDisplaySurface->dumpAsString(surfaceDump);
241 out.append(surfaceDump);
242}
243
244std::uint32_t RenderSurface::getPageFlipCount() const {
245 return mPageFlipCount;
246}
247
248void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
249 mPageFlipCount = count;
250}
251
252void RenderSurface::setSizeForTest(const ui::Size& size) {
253 mSize = size;
254}
255
256sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
257 return mGraphicBuffer;
258}
259
Lloyd Pique31cb2942018-10-19 17:23:03 -0700260} // namespace impl
261} // namespace android::compositionengine