blob: af3cb9aec1e9dc5e740cafabd0c6fa84c6d454b1 [file] [log] [blame]
Valerie Hau9cfc6d82019-09-23 13:54:07 -07001/*
2 * Copyright (C) 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#ifndef ANDROID_TRANSACTION_TEST_HARNESSES
17#define ANDROID_TRANSACTION_TEST_HARNESSES
18
Melody Hsu793f8362024-01-08 20:00:35 +000019#include <common/FlagManager.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080020#include <ui/DisplayState.h>
Valerie Hau9cfc6d82019-09-23 13:54:07 -070021
Valerie Hau9cfc6d82019-09-23 13:54:07 -070022#include "LayerTransactionTest.h"
Melody Hsu793f8362024-01-08 20:00:35 +000023#include "ui/LayerStack.h"
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080024
Valerie Hau9cfc6d82019-09-23 13:54:07 -070025namespace android {
26
27using android::hardware::graphics::common::V1_1::BufferUsage;
28
29class LayerRenderPathTestHarness {
30public:
31 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
32 : mDelegate(delegate), mRenderPath(renderPath) {}
33
34 std::unique_ptr<ScreenCapture> getScreenCapture() {
35 switch (mRenderPath) {
36 case RenderPath::SCREENSHOT:
37 return mDelegate->screenshot();
38 case RenderPath::VIRTUAL_DISPLAY:
39
Huihong Luo31b5ac22022-08-15 20:38:10 -070040 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
Melody Hsu793f8362024-01-08 20:00:35 +000041 const PhysicalDisplayId displayId = ids.front();
Huihong Luo31b5ac22022-08-15 20:38:10 -070042 const auto displayToken = ids.empty()
43 ? nullptr
Melody Hsu793f8362024-01-08 20:00:35 +000044 : SurfaceComposerClient::getPhysicalDisplayToken(displayId);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080045
46 ui::DisplayState displayState;
47 SurfaceComposerClient::getDisplayState(displayToken, &displayState);
48
Marin Shalamanova7fe3042021-01-29 21:02:08 +010049 ui::DisplayMode displayMode;
50 SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
51 const ui::Size& resolution = displayMode.resolution;
Valerie Hau9cfc6d82019-09-23 13:54:07 -070052
53 sp<IBinder> vDisplay;
54 sp<IGraphicBufferProducer> producer;
55 sp<IGraphicBufferConsumer> consumer;
56 sp<BufferItemConsumer> itemConsumer;
57 BufferQueue::createBufferQueue(&producer, &consumer);
58
59 consumer->setConsumerName(String8("Virtual disp consumer"));
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080060 consumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
Valerie Hau9cfc6d82019-09-23 13:54:07 -070061
Ady Abrahamd11bade2022-08-01 16:18:03 -070062 itemConsumer = sp<BufferItemConsumer>::make(consumer,
63 // Sample usage bits from screenrecord
64 GRALLOC_USAGE_HW_VIDEO_ENCODER |
65 GRALLOC_USAGE_SW_READ_OFTEN);
66 sp<BufferListener> listener = sp<BufferListener>::make(this);
Arthur Hung58144272021-01-16 03:43:53 +000067 itemConsumer->setFrameAvailableListener(listener);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070068
Alan Dingd53801c2024-05-08 16:45:29 -070069 static const std::string kDisplayName("VirtualDisplay");
70 vDisplay = SurfaceComposerClient::createVirtualDisplay(kDisplayName,
71 false /*isSecure*/);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070072
Melody Hsu793f8362024-01-08 20:00:35 +000073 constexpr ui::LayerStack layerStack{
74 848472}; // ASCII for TTH (TransactionTestHarnesses)
75 sp<SurfaceControl> mirrorSc =
76 SurfaceComposerClient::getDefault()->mirrorDisplay(displayId);
77
Valerie Hau9cfc6d82019-09-23 13:54:07 -070078 SurfaceComposerClient::Transaction t;
79 t.setDisplaySurface(vDisplay, producer);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080080 t.setDisplayProjection(vDisplay, displayState.orientation,
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020081 Rect(displayState.layerStackSpaceRect), Rect(resolution));
Melody Hsu793f8362024-01-08 20:00:35 +000082 if (FlagManager::getInstance().ce_fence_promise()) {
83 t.setDisplayLayerStack(vDisplay, layerStack);
84 t.setLayerStack(mirrorSc, layerStack);
85 } else {
86 t.setDisplayLayerStack(vDisplay, ui::DEFAULT_LAYER_STACK);
87 }
Valerie Hau9cfc6d82019-09-23 13:54:07 -070088 t.apply();
89 SurfaceComposerClient::Transaction().apply(true);
Arthur Hung58144272021-01-16 03:43:53 +000090
91 std::unique_lock lock(mMutex);
92 mAvailable = false;
93 // Wait for frame buffer ready.
94 mCondition.wait_for(lock, std::chrono::seconds(2),
95 [this]() NO_THREAD_SAFETY_ANALYSIS { return mAvailable; });
96
Valerie Hau9cfc6d82019-09-23 13:54:07 -070097 BufferItem item;
98 itemConsumer->acquireBuffer(&item, 0, true);
Alec Mouri14d5b862022-04-27 21:20:04 +000099 constexpr bool kContainsHdr = false;
100 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer, kContainsHdr);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700101 itemConsumer->releaseBuffer(item);
Melody Hsu793f8362024-01-08 20:00:35 +0000102
103 // Possible race condition with destroying virtual displays, in which
104 // CompositionEngine::present may attempt to be called on the same
105 // display multiple times. The layerStack is set to invalid here so
106 // that the display is ignored if that scenario occurs.
107 if (FlagManager::getInstance().ce_fence_promise()) {
108 t.setLayerStack(mirrorSc, ui::INVALID_LAYER_STACK);
109 t.apply(true);
110 }
Alan Dingd53801c2024-05-08 16:45:29 -0700111 SurfaceComposerClient::destroyVirtualDisplay(vDisplay);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700112 return sc;
113 }
114 }
115
116protected:
117 LayerTransactionTest* mDelegate;
118 RenderPath mRenderPath;
Arthur Hung58144272021-01-16 03:43:53 +0000119 std::mutex mMutex;
120 std::condition_variable mCondition;
121 bool mAvailable = false;
122
123 void onFrameAvailable() {
124 std::unique_lock lock(mMutex);
125 mAvailable = true;
126 mCondition.notify_all();
127 }
128
129 class BufferListener : public ConsumerBase::FrameAvailableListener {
130 public:
131 BufferListener(LayerRenderPathTestHarness* owner) : mOwner(owner) {}
132 LayerRenderPathTestHarness* mOwner;
133
134 void onFrameAvailable(const BufferItem& /*item*/) { mOwner->onFrameAvailable(); }
135 };
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700136};
137
138class LayerTypeTransactionHarness : public LayerTransactionTest {
139public:
140 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
141
142 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Valerie Hau1acd6962019-10-28 16:35:48 -0700143 uint32_t flags = 0, SurfaceControl* parent = nullptr,
chaviwdebadb82020-03-26 14:57:24 -0700144 uint32_t* outTransformHint = nullptr,
145 PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700146 // if the flags already have a layer type specified, return an error
147 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
148 return nullptr;
149 }
Valerie Hau1acd6962019-10-28 16:35:48 -0700150 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent,
chaviwdebadb82020-03-26 14:57:24 -0700151 outTransformHint, format);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700152 }
153
Marin Shalamanov46084422020-10-13 12:33:42 +0200154 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, uint32_t bufferWidth,
155 uint32_t bufferHeight) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700156 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
157 bufferWidth, bufferHeight));
158 }
159
Marin Shalamanov46084422020-10-13 12:33:42 +0200160 void fillLayerQuadrant(const sp<SurfaceControl>& layer, uint32_t bufferWidth,
161 uint32_t bufferHeight, const Color& topLeft, const Color& topRight,
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700162 const Color& bottomLeft, const Color& bottomRight) {
163 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
164 bufferWidth, bufferHeight,
165 topLeft, topRight,
166 bottomLeft, bottomRight));
167 }
168
169protected:
170 uint32_t mLayerType;
171};
172} // namespace android
173#endif