blob: a19d23febf993ebbd67c3b88c9f88dfa1d2387c9 [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>
28#include <log/log.h>
Alec Mouria90a5702021-04-16 16:36:21 +000029#include <renderengine/ExternalTexture.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070030#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),
Alec Mouria90a5702021-04-16 16:36:21 +000066 mSize(args.displayWidth, args.displayHeight),
67 mMaxTextureCacheSize(args.maxTextureCacheSize) {
chaviw8beb4142019-04-11 13:09:05 -070068 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
69}
Lloyd Pique31cb2942018-10-19 17:23:03 -070070
chaviw8beb4142019-04-11 13:09:05 -070071RenderSurface::~RenderSurface() {
72 ANativeWindow* const window = mNativeWindow.get();
73 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
74}
Lloyd Pique31cb2942018-10-19 17:23:03 -070075
76bool RenderSurface::isValid() const {
77 return mSize.isValid();
78}
79
80void RenderSurface::initialize() {
81 ANativeWindow* const window = mNativeWindow.get();
82
83 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
84 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
85 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
86 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
John Reck44418f52020-09-15 18:02:17 -070087 status = native_window_set_usage(window, DEFAULT_USAGE);
Lloyd Pique31cb2942018-10-19 17:23:03 -070088 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
89}
90
91const ui::Size& RenderSurface::getSize() const {
92 return mSize;
93}
94
95const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
96 return mDisplaySurface->getClientTargetAcquireFence();
97}
98
99void RenderSurface::setDisplaySize(const ui::Size& size) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100100 mDisplaySurface->resizeBuffers(size);
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) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700130 const auto compositionType = [=] {
131 using CompositionType = DisplaySurface::CompositionType;
132
133 if (usesClientComposition && usesDeviceComposition) return CompositionType::Mixed;
134 if (usesClientComposition) return CompositionType::Gpu;
135 if (usesDeviceComposition) return CompositionType::Hwc;
136
Lloyd Pique31cb2942018-10-19 17:23:03 -0700137 // Nothing to do -- when turning the screen off we get a frame like
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800138 // this. Call it a HWC frame since we won't be doing any GPU work but
Lloyd Pique31cb2942018-10-19 17:23:03 -0700139 // will do a prepare/set cycle.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700140 return CompositionType::Hwc;
141 }();
Lloyd Pique66d68602019-02-13 14:23:31 -0800142
143 if (status_t result = mDisplaySurface->prepareFrame(compositionType); result != NO_ERROR) {
144 ALOGE("updateCompositionType failed for %s: %d (%s)", mDisplay.getName().c_str(), result,
145 strerror(-result));
146 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700147}
148
Alec Mouria90a5702021-04-16 16:36:21 +0000149std::shared_ptr<renderengine::ExternalTexture> RenderSurface::dequeueBuffer(
150 base::unique_fd* bufferFence) {
Alec Mouri6338c9d2019-02-07 16:57:51 -0800151 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.
Alec Mouria90a5702021-04-16 16:36:21 +0000162 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700163 }
164
Alec Mouria90a5702021-04-16 16:36:21 +0000165 ALOGW_IF(mTexture != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
166 mTexture->getBuffer()->getNativeBuffer()->handle);
167
168 sp<GraphicBuffer> newBuffer = GraphicBuffer::from(buffer);
169
170 std::shared_ptr<renderengine::ExternalTexture> texture;
171
172 for (auto it = mTextureCache.begin(); it != mTextureCache.end(); it++) {
173 const auto& cachedTexture = *it;
174 if (cachedTexture->getBuffer()->getId() == newBuffer->getId()) {
175 texture = cachedTexture;
176 mTextureCache.erase(it);
177 break;
178 }
179 }
180
181 if (texture) {
182 mTexture = texture;
183 } else {
184 mTexture = std::make_shared<
185 renderengine::ExternalTexture>(GraphicBuffer::from(buffer),
186 mCompositionEngine.getRenderEngine(),
187 renderengine::ExternalTexture::Usage::WRITEABLE);
188 }
189 mTextureCache.push_back(mTexture);
190 if (mTextureCache.size() > mMaxTextureCacheSize) {
191 mTextureCache.erase(mTextureCache.begin());
192 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700193
Alec Mouri6338c9d2019-02-07 16:57:51 -0800194 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700195
Alec Mouria90a5702021-04-16 16:36:21 +0000196 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197}
198
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700199void RenderSurface::queueBuffer(base::unique_fd readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800200 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201
Lloyd Pique66d68602019-02-13 14:23:31 -0800202 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700203 // hasFlipClientTargetRequest could return true even if we haven't
204 // dequeued a buffer before. Try dequeueing one if we don't have a
205 // buffer ready.
Alec Mouria90a5702021-04-16 16:36:21 +0000206 if (mTexture == nullptr) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700207 ALOGI("Attempting to queue a client composited buffer without one "
208 "previously dequeued for display [%s]. Attempting to dequeue "
209 "a scratch buffer now",
210 mDisplay.getName().c_str());
Alec Mouria90a5702021-04-16 16:36:21 +0000211 // We shouldn't deadlock here, since mTexture == nullptr only
Lloyd Pique31cb2942018-10-19 17:23:03 -0700212 // after a successful call to queueBuffer, or if dequeueBuffer has
213 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800214 base::unique_fd unused;
215 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700216 }
217
Alec Mouria90a5702021-04-16 16:36:21 +0000218 if (mTexture == nullptr) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700219 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
220 } else {
Alec Mouria90a5702021-04-16 16:36:21 +0000221 status_t result = mNativeWindow->queueBuffer(mNativeWindow.get(),
222 mTexture->getBuffer()->getNativeBuffer(),
223 dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700224 if (result != NO_ERROR) {
225 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
226 result);
227 // We risk blocking on dequeueBuffer if the primary display failed
228 // to queue up its buffer, so crash here.
229 if (!mDisplay.isVirtual()) {
230 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
231 } else {
232 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mouria90a5702021-04-16 16:36:21 +0000233 mTexture->getBuffer()->getNativeBuffer(),
234 dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700235 }
236 }
237
Alec Mouria90a5702021-04-16 16:36:21 +0000238 mTexture = nullptr;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700239 }
240 }
241
242 status_t result = mDisplaySurface->advanceFrame();
243 if (result != NO_ERROR) {
244 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
245 }
246}
247
248void RenderSurface::onPresentDisplayCompleted() {
249 mDisplaySurface->onFrameCommitted();
250}
251
Lloyd Pique31cb2942018-10-19 17:23:03 -0700252void RenderSurface::flip() {
253 mPageFlipCount++;
254}
255
256void RenderSurface::dump(std::string& out) const {
257 using android::base::StringAppendF;
258
259 out.append(" Composition RenderSurface State:");
260
261 out.append("\n ");
262
263 dumpVal(out, "size", mSize);
264 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
265 ANativeWindow_getFormat(mNativeWindow.get()));
266 dumpVal(out, "flips", mPageFlipCount);
267 out.append("\n");
268
269 String8 surfaceDump;
270 mDisplaySurface->dumpAsString(surfaceDump);
271 out.append(surfaceDump);
272}
273
274std::uint32_t RenderSurface::getPageFlipCount() const {
275 return mPageFlipCount;
276}
277
278void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
279 mPageFlipCount = count;
280}
281
282void RenderSurface::setSizeForTest(const ui::Size& size) {
283 mSize = size;
284}
285
Alec Mouria90a5702021-04-16 16:36:21 +0000286std::shared_ptr<renderengine::ExternalTexture>& RenderSurface::mutableTextureForTest() {
287 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700288}
289
Lloyd Pique31cb2942018-10-19 17:23:03 -0700290} // namespace impl
291} // namespace android::compositionengine