blob: b4dfba1d0752fda37b276bc59cb9ffb1c1448f6e [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),
55 mSize(args.displayWidth, args.displayHeight) {}
56
57RenderSurface::~RenderSurface() = default;
58
59bool RenderSurface::isValid() const {
60 return mSize.isValid();
61}
62
63void RenderSurface::initialize() {
64 ANativeWindow* const window = mNativeWindow.get();
65
66 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
67 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
68 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
69 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
70 status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER);
71 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
72}
73
74const ui::Size& RenderSurface::getSize() const {
75 return mSize;
76}
77
78const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
79 return mDisplaySurface->getClientTargetAcquireFence();
80}
81
82void RenderSurface::setDisplaySize(const ui::Size& size) {
83 mDisplaySurface->resizeBuffers(size.width, size.height);
84 mSize = size;
85}
86
87void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
88 native_window_set_buffers_data_space(mNativeWindow.get(),
89 static_cast<android_dataspace>(dataspace));
90}
91
92void RenderSurface::setProtected(bool useProtected) {
93 uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER;
94 if (useProtected) {
95 usageFlags |= GRALLOC_USAGE_PROTECTED;
96 }
97 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
98 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
99}
100
101status_t RenderSurface::beginFrame(bool mustRecompose) {
102 return mDisplaySurface->beginFrame(mustRecompose);
103}
104
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800105status_t RenderSurface::prepareFrame() {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700106 auto& hwc = mCompositionEngine.getHwComposer();
107 const auto id = mDisplay.getId();
108 if (id) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800109 status_t error = hwc.prepare(*id, mDisplay);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700110 if (error != NO_ERROR) {
111 return error;
112 }
113 }
114
115 DisplaySurface::CompositionType compositionType;
116 const bool hasClient = hwc.hasClientComposition(id);
117 const bool hasDevice = hwc.hasDeviceComposition(id);
118 if (hasClient && hasDevice) {
119 compositionType = DisplaySurface::COMPOSITION_MIXED;
120 } else if (hasClient) {
121 compositionType = DisplaySurface::COMPOSITION_GLES;
122 } else if (hasDevice) {
123 compositionType = DisplaySurface::COMPOSITION_HWC;
124 } else {
125 // Nothing to do -- when turning the screen off we get a frame like
126 // this. Call it a HWC frame since we won't be doing any GLES work but
127 // will do a prepare/set cycle.
128 compositionType = DisplaySurface::COMPOSITION_HWC;
129 }
130 return mDisplaySurface->prepareFrame(compositionType);
131}
132
Alec Mouri6338c9d2019-02-07 16:57:51 -0800133sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
134 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700135 int fd = -1;
136 ANativeWindowBuffer* buffer = nullptr;
137
138 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
139
140 if (result != NO_ERROR) {
141 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
142 mDisplay.getName().c_str(), result);
143 // Return fast here as we can't do much more - any rendering we do
144 // now will just be wrong.
145 return mGraphicBuffer;
146 }
147
148 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
149 mGraphicBuffer->getNativeBuffer()->handle);
150 mGraphicBuffer = GraphicBuffer::from(buffer);
151
Alec Mouri6338c9d2019-02-07 16:57:51 -0800152 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700153
154 return mGraphicBuffer;
155}
156
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000157void RenderSurface::queueBuffer(base::unique_fd&& readyFence) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700158 auto& hwc = mCompositionEngine.getHwComposer();
159 const auto id = mDisplay.getId();
160
161 if (hwc.hasClientComposition(id) || hwc.hasFlipClientTargetRequest(id)) {
162 // hasFlipClientTargetRequest could return true even if we haven't
163 // dequeued a buffer before. Try dequeueing one if we don't have a
164 // buffer ready.
165 if (mGraphicBuffer == nullptr) {
166 ALOGI("Attempting to queue a client composited buffer without one "
167 "previously dequeued for display [%s]. Attempting to dequeue "
168 "a scratch buffer now",
169 mDisplay.getName().c_str());
170 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
171 // after a successful call to queueBuffer, or if dequeueBuffer has
172 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800173 base::unique_fd unused;
174 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700175 }
176
177 if (mGraphicBuffer == nullptr) {
178 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
179 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000180 status_t result =
181 mNativeWindow->queueBuffer(mNativeWindow.get(),
182 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700183 if (result != NO_ERROR) {
184 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
185 result);
186 // We risk blocking on dequeueBuffer if the primary display failed
187 // to queue up its buffer, so crash here.
188 if (!mDisplay.isVirtual()) {
189 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
190 } else {
191 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000192 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700193 }
194 }
195
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196 mGraphicBuffer = nullptr;
197 }
198 }
199
200 status_t result = mDisplaySurface->advanceFrame();
201 if (result != NO_ERROR) {
202 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
203 }
204}
205
206void RenderSurface::onPresentDisplayCompleted() {
207 mDisplaySurface->onFrameCommitted();
208}
209
210void RenderSurface::setViewportAndProjection() {
211 auto& renderEngine = mCompositionEngine.getRenderEngine();
212 Rect sourceCrop = Rect(mSize);
213 renderEngine.setViewportAndProjection(mSize.width, mSize.height, sourceCrop,
214 ui::Transform::ROT_0);
215}
216
Lloyd Pique31cb2942018-10-19 17:23:03 -0700217void RenderSurface::flip() {
218 mPageFlipCount++;
219}
220
221void RenderSurface::dump(std::string& out) const {
222 using android::base::StringAppendF;
223
224 out.append(" Composition RenderSurface State:");
225
226 out.append("\n ");
227
228 dumpVal(out, "size", mSize);
229 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
230 ANativeWindow_getFormat(mNativeWindow.get()));
231 dumpVal(out, "flips", mPageFlipCount);
232 out.append("\n");
233
234 String8 surfaceDump;
235 mDisplaySurface->dumpAsString(surfaceDump);
236 out.append(surfaceDump);
237}
238
239std::uint32_t RenderSurface::getPageFlipCount() const {
240 return mPageFlipCount;
241}
242
243void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
244 mPageFlipCount = count;
245}
246
247void RenderSurface::setSizeForTest(const ui::Size& size) {
248 mSize = size;
249}
250
251sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
252 return mGraphicBuffer;
253}
254
Lloyd Pique31cb2942018-10-19 17:23:03 -0700255} // namespace impl
256} // namespace android::compositionengine