blob: b47f7fd8a9b6d2baf048e8806371c7827f0be603 [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>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080028
Lloyd Pique31cb2942018-10-19 17:23:03 -070029#include <log/log.h>
30#include <renderengine/RenderEngine.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070031#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
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080036// TODO(b/129481165): remove the #pragma below and fix conversion issues
37#pragma clang diagnostic push
38#pragma clang diagnostic ignored "-Wconversion"
39
Lloyd Pique31cb2942018-10-19 17:23:03 -070040#include "DisplayHardware/HWComposer.h"
41
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080042// TODO(b/129481165): remove the #pragma below and fix conversion issues
43#pragma clang diagnostic pop // ignored "-Wconversion"
44
Lloyd Pique31cb2942018-10-19 17:23:03 -070045namespace android::compositionengine {
46
47RenderSurface::~RenderSurface() = default;
48
49namespace impl {
50
John Reck44418f52020-09-15 18:02:17 -070051constexpr auto DEFAULT_USAGE = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
52
Lloyd Pique31cb2942018-10-19 17:23:03 -070053std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
54 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070055 compositionengine::Display& display,
56 const compositionengine::RenderSurfaceCreationArgs& args) {
57 return std::make_unique<RenderSurface>(compositionEngine, display, args);
Lloyd Pique31cb2942018-10-19 17:23:03 -070058}
59
60RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070061 const RenderSurfaceCreationArgs& args)
Lloyd Pique31cb2942018-10-19 17:23:03 -070062 : mCompositionEngine(compositionEngine),
63 mDisplay(display),
64 mNativeWindow(args.nativeWindow),
65 mDisplaySurface(args.displaySurface),
chaviw8beb4142019-04-11 13:09:05 -070066 mSize(args.displayWidth, args.displayHeight) {
67 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
68}
Lloyd Pique31cb2942018-10-19 17:23:03 -070069
chaviw8beb4142019-04-11 13:09:05 -070070RenderSurface::~RenderSurface() {
71 ANativeWindow* const window = mNativeWindow.get();
72 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
73}
Lloyd Pique31cb2942018-10-19 17:23:03 -070074
75bool RenderSurface::isValid() const {
76 return mSize.isValid();
77}
78
79void RenderSurface::initialize() {
80 ANativeWindow* const window = mNativeWindow.get();
81
82 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
83 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
84 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
85 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
John Reck44418f52020-09-15 18:02:17 -070086 status = native_window_set_usage(window, DEFAULT_USAGE);
Lloyd Pique31cb2942018-10-19 17:23:03 -070087 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
88}
89
90const ui::Size& RenderSurface::getSize() const {
91 return mSize;
92}
93
94const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
95 return mDisplaySurface->getClientTargetAcquireFence();
96}
97
98void RenderSurface::setDisplaySize(const ui::Size& size) {
Lloyd Pique0a456232020-01-16 17:51:13 -080099 mDisplaySurface->resizeBuffers(static_cast<uint32_t>(size.width),
100 static_cast<uint32_t>(size.height));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700101 mSize = size;
102}
103
104void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
105 native_window_set_buffers_data_space(mNativeWindow.get(),
106 static_cast<android_dataspace>(dataspace));
107}
108
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700109void RenderSurface::setBufferPixelFormat(ui::PixelFormat pixelFormat) {
110 native_window_set_buffers_format(mNativeWindow.get(), static_cast<int32_t>(pixelFormat));
111}
112
Lloyd Pique31cb2942018-10-19 17:23:03 -0700113void RenderSurface::setProtected(bool useProtected) {
John Reck44418f52020-09-15 18:02:17 -0700114 uint64_t usageFlags = DEFAULT_USAGE;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700115 if (useProtected) {
116 usageFlags |= GRALLOC_USAGE_PROTECTED;
117 }
118 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
119 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700120 if (status == NO_ERROR) {
121 mProtected = useProtected;
122 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700123}
124
125status_t RenderSurface::beginFrame(bool mustRecompose) {
126 return mDisplaySurface->beginFrame(mustRecompose);
127}
128
Lloyd Pique66d68602019-02-13 14:23:31 -0800129void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700130 DisplaySurface::CompositionType compositionType;
Lloyd Pique66d68602019-02-13 14:23:31 -0800131 if (usesClientComposition && usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700132 compositionType = DisplaySurface::COMPOSITION_MIXED;
Lloyd Pique66d68602019-02-13 14:23:31 -0800133 } else if (usesClientComposition) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800134 compositionType = DisplaySurface::COMPOSITION_GPU;
Lloyd Pique66d68602019-02-13 14:23:31 -0800135 } else if (usesDeviceComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700136 compositionType = DisplaySurface::COMPOSITION_HWC;
137 } else {
138 // Nothing to do -- when turning the screen off we get a frame like
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800139 // this. Call it a HWC frame since we won't be doing any GPU work but
Lloyd Pique31cb2942018-10-19 17:23:03 -0700140 // will do a prepare/set cycle.
141 compositionType = DisplaySurface::COMPOSITION_HWC;
142 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800143
144 if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) {
145 ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result,
146 strerror(-result));
147 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700148}
149
Alec Mouri6338c9d2019-02-07 16:57:51 -0800150sp<GraphicBuffer> RenderSurface::dequeueBuffer(base::unique_fd* bufferFence) {
151 ATRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700152 int fd = -1;
153 ANativeWindowBuffer* buffer = nullptr;
154
155 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
156
157 if (result != NO_ERROR) {
158 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
159 mDisplay.getName().c_str(), result);
160 // Return fast here as we can't do much more - any rendering we do
161 // now will just be wrong.
162 return mGraphicBuffer;
163 }
164
165 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
166 mGraphicBuffer->getNativeBuffer()->handle);
167 mGraphicBuffer = GraphicBuffer::from(buffer);
168
Alec Mouri6338c9d2019-02-07 16:57:51 -0800169 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700170
171 return mGraphicBuffer;
172}
173
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700174void RenderSurface::queueBuffer(base::unique_fd readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800175 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700176
Lloyd Pique66d68602019-02-13 14:23:31 -0800177 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700178 // hasFlipClientTargetRequest could return true even if we haven't
179 // dequeued a buffer before. Try dequeueing one if we don't have a
180 // buffer ready.
181 if (mGraphicBuffer == nullptr) {
182 ALOGI("Attempting to queue a client composited buffer without one "
183 "previously dequeued for display [%s]. Attempting to dequeue "
184 "a scratch buffer now",
185 mDisplay.getName().c_str());
186 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
187 // after a successful call to queueBuffer, or if dequeueBuffer has
188 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800189 base::unique_fd unused;
190 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191 }
192
193 if (mGraphicBuffer == nullptr) {
194 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
195 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000196 status_t result =
197 mNativeWindow->queueBuffer(mNativeWindow.get(),
198 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700199 if (result != NO_ERROR) {
200 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
201 result);
202 // We risk blocking on dequeueBuffer if the primary display failed
203 // to queue up its buffer, so crash here.
204 if (!mDisplay.isVirtual()) {
205 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
206 } else {
207 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000208 mGraphicBuffer->getNativeBuffer(), dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700209 }
210 }
211
Lloyd Pique31cb2942018-10-19 17:23:03 -0700212 mGraphicBuffer = nullptr;
213 }
214 }
215
216 status_t result = mDisplaySurface->advanceFrame();
217 if (result != NO_ERROR) {
218 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
219 }
220}
221
222void RenderSurface::onPresentDisplayCompleted() {
223 mDisplaySurface->onFrameCommitted();
224}
225
Lloyd Pique31cb2942018-10-19 17:23:03 -0700226void RenderSurface::flip() {
227 mPageFlipCount++;
228}
229
230void RenderSurface::dump(std::string& out) const {
231 using android::base::StringAppendF;
232
233 out.append(" Composition RenderSurface State:");
234
235 out.append("\n ");
236
237 dumpVal(out, "size", mSize);
238 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
239 ANativeWindow_getFormat(mNativeWindow.get()));
240 dumpVal(out, "flips", mPageFlipCount);
241 out.append("\n");
242
243 String8 surfaceDump;
244 mDisplaySurface->dumpAsString(surfaceDump);
245 out.append(surfaceDump);
246}
247
248std::uint32_t RenderSurface::getPageFlipCount() const {
249 return mPageFlipCount;
250}
251
252void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
253 mPageFlipCount = count;
254}
255
256void RenderSurface::setSizeForTest(const ui::Size& size) {
257 mSize = size;
258}
259
260sp<GraphicBuffer>& RenderSurface::mutableGraphicBufferForTest() {
261 return mGraphicBuffer;
262}
263
Lloyd Pique31cb2942018-10-19 17:23:03 -0700264} // namespace impl
265} // namespace android::compositionengine