blob: 797a64c7d751ce6dd2422c2a7df1321b347fee82 [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
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080019#include <ui/DisplayState.h>
Valerie Hau9cfc6d82019-09-23 13:54:07 -070020
Valerie Hau9cfc6d82019-09-23 13:54:07 -070021#include "LayerTransactionTest.h"
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080022
Valerie Hau9cfc6d82019-09-23 13:54:07 -070023namespace android {
24
25using android::hardware::graphics::common::V1_1::BufferUsage;
26
27class LayerRenderPathTestHarness {
28public:
29 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
30 : mDelegate(delegate), mRenderPath(renderPath) {}
31
32 std::unique_ptr<ScreenCapture> getScreenCapture() {
33 switch (mRenderPath) {
34 case RenderPath::SCREENSHOT:
35 return mDelegate->screenshot();
36 case RenderPath::VIRTUAL_DISPLAY:
37
Huihong Luo31b5ac22022-08-15 20:38:10 -070038 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
39 const auto displayToken = ids.empty()
40 ? nullptr
41 : SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080042
43 ui::DisplayState displayState;
44 SurfaceComposerClient::getDisplayState(displayToken, &displayState);
45
Marin Shalamanova7fe3042021-01-29 21:02:08 +010046 ui::DisplayMode displayMode;
47 SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
48 const ui::Size& resolution = displayMode.resolution;
Valerie Hau9cfc6d82019-09-23 13:54:07 -070049
50 sp<IBinder> vDisplay;
51 sp<IGraphicBufferProducer> producer;
52 sp<IGraphicBufferConsumer> consumer;
53 sp<BufferItemConsumer> itemConsumer;
54 BufferQueue::createBufferQueue(&producer, &consumer);
55
56 consumer->setConsumerName(String8("Virtual disp consumer"));
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080057 consumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
Valerie Hau9cfc6d82019-09-23 13:54:07 -070058
Ady Abrahamd11bade2022-08-01 16:18:03 -070059 itemConsumer = sp<BufferItemConsumer>::make(consumer,
60 // Sample usage bits from screenrecord
61 GRALLOC_USAGE_HW_VIDEO_ENCODER |
62 GRALLOC_USAGE_SW_READ_OFTEN);
63 sp<BufferListener> listener = sp<BufferListener>::make(this);
Arthur Hung58144272021-01-16 03:43:53 +000064 itemConsumer->setFrameAvailableListener(listener);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070065
66 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
67 false /*secure*/);
68
69 SurfaceComposerClient::Transaction t;
70 t.setDisplaySurface(vDisplay, producer);
Dominik Laskowski29fa1462021-04-27 15:51:50 -070071 t.setDisplayLayerStack(vDisplay, ui::DEFAULT_LAYER_STACK);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080072 t.setDisplayProjection(vDisplay, displayState.orientation,
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020073 Rect(displayState.layerStackSpaceRect), Rect(resolution));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070074 t.apply();
75 SurfaceComposerClient::Transaction().apply(true);
Arthur Hung58144272021-01-16 03:43:53 +000076
77 std::unique_lock lock(mMutex);
78 mAvailable = false;
79 // Wait for frame buffer ready.
80 mCondition.wait_for(lock, std::chrono::seconds(2),
81 [this]() NO_THREAD_SAFETY_ANALYSIS { return mAvailable; });
82
Valerie Hau9cfc6d82019-09-23 13:54:07 -070083 BufferItem item;
84 itemConsumer->acquireBuffer(&item, 0, true);
Alec Mouri14d5b862022-04-27 21:20:04 +000085 constexpr bool kContainsHdr = false;
86 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer, kContainsHdr);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070087 itemConsumer->releaseBuffer(item);
88 SurfaceComposerClient::destroyDisplay(vDisplay);
89 return sc;
90 }
91 }
92
93protected:
94 LayerTransactionTest* mDelegate;
95 RenderPath mRenderPath;
Arthur Hung58144272021-01-16 03:43:53 +000096 std::mutex mMutex;
97 std::condition_variable mCondition;
98 bool mAvailable = false;
99
100 void onFrameAvailable() {
101 std::unique_lock lock(mMutex);
102 mAvailable = true;
103 mCondition.notify_all();
104 }
105
106 class BufferListener : public ConsumerBase::FrameAvailableListener {
107 public:
108 BufferListener(LayerRenderPathTestHarness* owner) : mOwner(owner) {}
109 LayerRenderPathTestHarness* mOwner;
110
111 void onFrameAvailable(const BufferItem& /*item*/) { mOwner->onFrameAvailable(); }
112 };
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700113};
114
115class LayerTypeTransactionHarness : public LayerTransactionTest {
116public:
117 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
118
119 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Valerie Hau1acd6962019-10-28 16:35:48 -0700120 uint32_t flags = 0, SurfaceControl* parent = nullptr,
chaviwdebadb82020-03-26 14:57:24 -0700121 uint32_t* outTransformHint = nullptr,
122 PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700123 // if the flags already have a layer type specified, return an error
124 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
125 return nullptr;
126 }
Valerie Hau1acd6962019-10-28 16:35:48 -0700127 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent,
chaviwdebadb82020-03-26 14:57:24 -0700128 outTransformHint, format);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700129 }
130
Marin Shalamanov46084422020-10-13 12:33:42 +0200131 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, uint32_t bufferWidth,
132 uint32_t bufferHeight) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700133 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
134 bufferWidth, bufferHeight));
135 }
136
Marin Shalamanov46084422020-10-13 12:33:42 +0200137 void fillLayerQuadrant(const sp<SurfaceControl>& layer, uint32_t bufferWidth,
138 uint32_t bufferHeight, const Color& topLeft, const Color& topRight,
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700139 const Color& bottomLeft, const Color& bottomRight) {
140 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
141 bufferWidth, bufferHeight,
142 topLeft, topRight,
143 bottomLeft, bottomRight));
144 }
145
146protected:
147 uint32_t mLayerType;
148};
149} // namespace android
150#endif