blob: ef5087061536596358e332057eb6038d00a7f20c [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) {
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 Mouria90a5702021-04-16 16:36:21 +0000150std::shared_ptr<renderengine::ExternalTexture> RenderSurface::dequeueBuffer(
151 base::unique_fd* bufferFence) {
Alec Mouri6338c9d2019-02-07 16:57:51 -0800152 ATRACE_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<
186 renderengine::ExternalTexture>(GraphicBuffer::from(buffer),
187 mCompositionEngine.getRenderEngine(),
188 renderengine::ExternalTexture::Usage::WRITEABLE);
189 }
190 mTextureCache.push_back(mTexture);
191 if (mTextureCache.size() > mMaxTextureCacheSize) {
192 mTextureCache.erase(mTextureCache.begin());
193 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700194
Alec Mouri6338c9d2019-02-07 16:57:51 -0800195 *bufferFence = base::unique_fd(fd);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196
Alec Mouria90a5702021-04-16 16:36:21 +0000197 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198}
199
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700200void RenderSurface::queueBuffer(base::unique_fd readyFence) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800201 auto& state = mDisplay.getState();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700202
Lloyd Pique66d68602019-02-13 14:23:31 -0800203 if (state.usesClientComposition || state.flipClientTarget) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700204 // hasFlipClientTargetRequest could return true even if we haven't
205 // dequeued a buffer before. Try dequeueing one if we don't have a
206 // buffer ready.
Alec Mouria90a5702021-04-16 16:36:21 +0000207 if (mTexture == nullptr) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700208 ALOGI("Attempting to queue a client composited buffer without one "
209 "previously dequeued for display [%s]. Attempting to dequeue "
210 "a scratch buffer now",
211 mDisplay.getName().c_str());
Alec Mouria90a5702021-04-16 16:36:21 +0000212 // We shouldn't deadlock here, since mTexture == nullptr only
Lloyd Pique31cb2942018-10-19 17:23:03 -0700213 // after a successful call to queueBuffer, or if dequeueBuffer has
214 // never been called.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800215 base::unique_fd unused;
216 dequeueBuffer(&unused);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700217 }
218
Alec Mouria90a5702021-04-16 16:36:21 +0000219 if (mTexture == nullptr) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700220 ALOGE("No buffer is ready for display [%s]", mDisplay.getName().c_str());
221 } else {
Alec Mouria90a5702021-04-16 16:36:21 +0000222 status_t result = mNativeWindow->queueBuffer(mNativeWindow.get(),
223 mTexture->getBuffer()->getNativeBuffer(),
224 dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700225 if (result != NO_ERROR) {
226 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplay.getName().c_str(),
227 result);
228 // We risk blocking on dequeueBuffer if the primary display failed
229 // to queue up its buffer, so crash here.
230 if (!mDisplay.isVirtual()) {
231 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", result);
232 } else {
233 mNativeWindow->cancelBuffer(mNativeWindow.get(),
Alec Mouria90a5702021-04-16 16:36:21 +0000234 mTexture->getBuffer()->getNativeBuffer(),
235 dup(readyFence));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700236 }
237 }
238
Alec Mouria90a5702021-04-16 16:36:21 +0000239 mTexture = nullptr;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700240 }
241 }
242
243 status_t result = mDisplaySurface->advanceFrame();
244 if (result != NO_ERROR) {
245 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplay.getName().c_str(), result);
246 }
247}
248
249void RenderSurface::onPresentDisplayCompleted() {
250 mDisplaySurface->onFrameCommitted();
251}
252
Lloyd Pique31cb2942018-10-19 17:23:03 -0700253void RenderSurface::flip() {
254 mPageFlipCount++;
255}
256
257void RenderSurface::dump(std::string& out) const {
258 using android::base::StringAppendF;
259
260 out.append(" Composition RenderSurface State:");
261
262 out.append("\n ");
263
264 dumpVal(out, "size", mSize);
265 StringAppendF(&out, "ANativeWindow=%p (format %d) ", mNativeWindow.get(),
266 ANativeWindow_getFormat(mNativeWindow.get()));
267 dumpVal(out, "flips", mPageFlipCount);
268 out.append("\n");
269
270 String8 surfaceDump;
271 mDisplaySurface->dumpAsString(surfaceDump);
272 out.append(surfaceDump);
273}
274
275std::uint32_t RenderSurface::getPageFlipCount() const {
276 return mPageFlipCount;
277}
278
279void RenderSurface::setPageFlipCountForTest(std::uint32_t count) {
280 mPageFlipCount = count;
281}
282
283void RenderSurface::setSizeForTest(const ui::Size& size) {
284 mSize = size;
285}
286
Alec Mouria90a5702021-04-16 16:36:21 +0000287std::shared_ptr<renderengine::ExternalTexture>& RenderSurface::mutableTextureForTest() {
288 return mTexture;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700289}
290
Lloyd Pique31cb2942018-10-19 17:23:03 -0700291} // namespace impl
292} // namespace android::compositionengine