blob: d6028bf4cf910f2256ce3a9501510fa47ca12de0 [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>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000021#include <common/trace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070022#include <compositionengine/CompositionEngine.h>
23#include <compositionengine/Display.h>
24#include <compositionengine/DisplaySurface.h>
25#include <compositionengine/RenderSurfaceCreationArgs.h>
26#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070028#include <compositionengine/impl/RenderSurface.h>
29#include <log/log.h>
Alec Mouria90a5702021-04-16 16:36:21 +000030#include <renderengine/ExternalTexture.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070031#include <renderengine/RenderEngine.h>
Vishnu Nairdbbe3852022-01-12 20:22:11 -080032#include <renderengine/impl/ExternalTexture.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070033#include <system/window.h>
34#include <ui/GraphicBuffer.h>
35#include <ui/Rect.h>
36
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037// TODO(b/129481165): remove the #pragma below and fix conversion issues
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wconversion"
40
Lloyd Pique31cb2942018-10-19 17:23:03 -070041#include "DisplayHardware/HWComposer.h"
42
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080043// TODO(b/129481165): remove the #pragma below and fix conversion issues
44#pragma clang diagnostic pop // ignored "-Wconversion"
45
Lloyd Pique31cb2942018-10-19 17:23:03 -070046namespace android::compositionengine {
47
48RenderSurface::~RenderSurface() = default;
49
50namespace impl {
51
John Reck44418f52020-09-15 18:02:17 -070052constexpr auto DEFAULT_USAGE = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
53
Lloyd Pique31cb2942018-10-19 17:23:03 -070054std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
55 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070056 compositionengine::Display& display,
57 const compositionengine::RenderSurfaceCreationArgs& args) {
58 return std::make_unique<RenderSurface>(compositionEngine, display, args);
Lloyd Pique31cb2942018-10-19 17:23:03 -070059}
60
61RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070062 const RenderSurfaceCreationArgs& args)
Lloyd Pique31cb2942018-10-19 17:23:03 -070063 : mCompositionEngine(compositionEngine),
64 mDisplay(display),
65 mNativeWindow(args.nativeWindow),
66 mDisplaySurface(args.displaySurface),
Alec Mouria90a5702021-04-16 16:36:21 +000067 mSize(args.displayWidth, args.displayHeight),
68 mMaxTextureCacheSize(args.maxTextureCacheSize) {
chaviw8beb4142019-04-11 13:09:05 -070069 LOG_ALWAYS_FATAL_IF(!mNativeWindow);
70}
Lloyd Pique31cb2942018-10-19 17:23:03 -070071
chaviw8beb4142019-04-11 13:09:05 -070072RenderSurface::~RenderSurface() {
73 ANativeWindow* const window = mNativeWindow.get();
74 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
75}
Lloyd Pique31cb2942018-10-19 17:23:03 -070076
77bool RenderSurface::isValid() const {
78 return mSize.isValid();
79}
80
81void RenderSurface::initialize() {
82 ANativeWindow* const window = mNativeWindow.get();
83
84 int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
85 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
86 status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
87 ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status);
John Reck44418f52020-09-15 18:02:17 -070088 status = native_window_set_usage(window, DEFAULT_USAGE);
Lloyd Pique31cb2942018-10-19 17:23:03 -070089 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status);
90}
91
92const ui::Size& RenderSurface::getSize() const {
93 return mSize;
94}
95
96const sp<Fence>& RenderSurface::getClientTargetAcquireFence() const {
97 return mDisplaySurface->getClientTargetAcquireFence();
98}
99
100void RenderSurface::setDisplaySize(const ui::Size& size) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100101 mDisplaySurface->resizeBuffers(size);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700102 mSize = size;
103}
104
105void RenderSurface::setBufferDataspace(ui::Dataspace dataspace) {
106 native_window_set_buffers_data_space(mNativeWindow.get(),
107 static_cast<android_dataspace>(dataspace));
108}
109
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700110void RenderSurface::setBufferPixelFormat(ui::PixelFormat pixelFormat) {
111 native_window_set_buffers_format(mNativeWindow.get(), static_cast<int32_t>(pixelFormat));
112}
113
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114void RenderSurface::setProtected(bool useProtected) {
John Reck44418f52020-09-15 18:02:17 -0700115 uint64_t usageFlags = DEFAULT_USAGE;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700116 if (useProtected) {
117 usageFlags |= GRALLOC_USAGE_PROTECTED;
118 }
119 const int status = native_window_set_usage(mNativeWindow.get(), usageFlags);
120 ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status);
Peiyong Lin52010312019-05-02 14:22:16 -0700121 if (status == NO_ERROR) {
122 mProtected = useProtected;
123 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700124}
125
126status_t RenderSurface::beginFrame(bool mustRecompose) {
127 return mDisplaySurface->beginFrame(mustRecompose);
128}
129
Lloyd Pique66d68602019-02-13 14:23:31 -0800130void RenderSurface::prepareFrame(bool usesClientComposition, bool usesDeviceComposition) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700131 const auto compositionType = [=] {
132 using CompositionType = DisplaySurface::CompositionType;
133
134 if (usesClientComposition && usesDeviceComposition) return CompositionType::Mixed;
135 if (usesClientComposition) return CompositionType::Gpu;
136 if (usesDeviceComposition) return CompositionType::Hwc;
137
Lloyd Pique31cb2942018-10-19 17:23:03 -0700138 // 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.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700141 return CompositionType::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 Mouria90a5702021-04-16 16:36:21 +0000150std::shared_ptr<renderengine::ExternalTexture> RenderSurface::dequeueBuffer(
151 base::unique_fd* bufferFence) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000152 SFTRACE_CALL();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700153 int fd = -1;
154 ANativeWindowBuffer* buffer = nullptr;
155
156 status_t result = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
157
158 if (result != NO_ERROR) {
159 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
160 mDisplay.getName().c_str(), result);
161 // Return fast here as we can't do much more - any rendering we do
162 // now will just be wrong.
Alec Mouria90a5702021-04-16 16:36:21 +0000163 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700164 }
165
Alec Mouria90a5702021-04-16 16:36:21 +0000166 ALOGW_IF(mTexture != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
167 mTexture->getBuffer()->getNativeBuffer()->handle);
168
169 sp<GraphicBuffer> newBuffer = GraphicBuffer::from(buffer);
170
171 std::shared_ptr<renderengine::ExternalTexture> texture;
172
173 for (auto it = mTextureCache.begin(); it != mTextureCache.end(); it++) {
174 const auto& cachedTexture = *it;
175 if (cachedTexture->getBuffer()->getId() == newBuffer->getId()) {
176 texture = cachedTexture;
177 mTextureCache.erase(it);
178 break;
179 }
180 }
181
182 if (texture) {
183 mTexture = texture;
184 } else {
185 mTexture = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800186 renderengine::impl::ExternalTexture>(GraphicBuffer::from(buffer),
187 mCompositionEngine.getRenderEngine(),
188 renderengine::impl::ExternalTexture::Usage::
189 WRITEABLE);
Alec Mouria90a5702021-04-16 16:36:21 +0000190 }
191 mTextureCache.push_back(mTexture);
192 if (mTextureCache.size() > mMaxTextureCacheSize) {
193 mTextureCache.erase(mTextureCache.begin());
194 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700195
Alec Mouri6338c9d2019-02-07 16:57:51 -0800196 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197
Alec Mouria90a5702021-04-16 16:36:21 +0000198 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700199}
200
Alec Mourif97df4d2023-09-06 02:10:05 +0000201void RenderSurface::queueBuffer(base::unique_fd readyFence, float hdrSdrRatio) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800202 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700203
Lloyd Pique66d68602019-02-13 14:23:31 -0800204 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700205 // hasFlipClientTargetRequest could return true even if we haven't
206 // dequeued a buffer before. Try dequeueing one if we don't have a
207 // buffer ready.
Alec Mouria90a5702021-04-16 16:36:21 +0000208 if (mTexture == nullptr) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700209 ALOGI("Attempting to queue a client composited buffer without one "
210 "previously dequeued for display [%s]. Attempting to dequeue "
211 "a scratch buffer now",
212 mDisplay.getName().c_str());
Alec Mouria90a5702021-04-16 16:36:21 +0000213 // We shouldn't deadlock here, since mTexture == nullptr only
Lloyd Pique31cb2942018-10-19 17:23:03 -0700214 // after a successful call to queueBuffer, or if dequeueBuffer has
215 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800216 base::unique_fd unused;
217 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700218 }
219
Alec Mouria90a5702021-04-16 16:36:21 +0000220 if (mTexture == nullptr) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700221 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
222 } else {
Alec Mouria90a5702021-04-16 16:36:21 +0000223 status_t result = mNativeWindow->queueBuffer(mNativeWindow.get(),
224 mTexture->getBuffer()->getNativeBuffer(),
225 dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700226 if (result != NO_ERROR) {
227 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
228 result);
229 // We risk blocking on dequeueBuffer if the primary display failed
230 // to queue up its buffer, so crash here.
231 if (!mDisplay.isVirtual()) {
232 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
233 } else {
234 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mouria90a5702021-04-16 16:36:21 +0000235 mTexture->getBuffer()->getNativeBuffer(),
236 dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700237 }
238 }
239
Alec Mouria90a5702021-04-16 16:36:21 +0000240 mTexture = nullptr;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700241 }
242 }
243
Alec Mourif97df4d2023-09-06 02:10:05 +0000244 status_t result = mDisplaySurface->advanceFrame(hdrSdrRatio);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700245 if (result != NO_ERROR) {
246 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
247 }
248}
249
250void RenderSurface::onPresentDisplayCompleted() {
251 mDisplaySurface->onFrameCommitted();
252}
253
Lloyd Pique31cb2942018-10-19 17:23:03 -0700254void RenderSurface::dump(std::string& out) const {
255 using android::base::StringAppendF;
256
257 out.append(" Composition RenderSurface State:");
258
259 out.append("\n ");
260
261 dumpVal(out, "size", mSize);
262 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
263 ANativeWindow_getFormat(mNativeWindow.get()));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700264 out.append("\n");
265
266 String8 surfaceDump;
267 mDisplaySurface->dumpAsString(surfaceDump);
268 out.append(surfaceDump);
269}
270
Lloyd Pique31cb2942018-10-19 17:23:03 -0700271void RenderSurface::setSizeForTest(const ui::Size& size) {
272 mSize = size;
273}
274
Alec Mouria90a5702021-04-16 16:36:21 +0000275std::shared_ptr<renderengine::ExternalTexture>& RenderSurface::mutableTextureForTest() {
276 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700277}
278
Vishnu Naira3140382022-02-24 14:07:11 -0800279bool RenderSurface::supportsCompositionStrategyPrediction() const {
280 return mDisplaySurface->supportsCompositionStrategyPrediction();
281}
282
Lloyd Pique31cb2942018-10-19 17:23:03 -0700283} // namespace impl
284} // namespace android::compositionengine