• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <com_android_graphics_libgui_flags.h>
20 #include <ui/DisplayState.h>
21 
22 #include "LayerTransactionTest.h"
23 #include "ui/LayerStack.h"
24 
25 namespace android {
26 
27 using android::hardware::graphics::common::V1_1::BufferUsage;
28 
29 class LayerRenderPathTestHarness {
30 public:
LayerRenderPathTestHarness(LayerTransactionTest * delegate,RenderPath renderPath)31     LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
32           : mDelegate(delegate), mRenderPath(renderPath) {}
33 
getScreenCapture()34     std::unique_ptr<ScreenCapture> getScreenCapture() {
35         switch (mRenderPath) {
36             case RenderPath::SCREENSHOT:
37                 return mDelegate->screenshot();
38             case RenderPath::VIRTUAL_DISPLAY:
39 
40                 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
41                 const PhysicalDisplayId displayId = ids.front();
42                 const auto displayToken = ids.empty()
43                         ? nullptr
44                         : SurfaceComposerClient::getPhysicalDisplayToken(displayId);
45 
46                 ui::DisplayState displayState;
47                 SurfaceComposerClient::getDisplayState(displayToken, &displayState);
48 
49                 ui::DisplayMode displayMode;
50                 SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
51                 ui::Size resolution = displayMode.resolution;
52                 if (displayState.orientation == ui::Rotation::Rotation90 ||
53                     displayState.orientation == ui::Rotation::Rotation270) {
54                     std::swap(resolution.width, resolution.height);
55                 }
56 
57                 sp<IBinder> vDisplay;
58 
59 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
60                 sp<BufferItemConsumer> itemConsumer = sp<BufferItemConsumer>::make(
61                         // Sample usage bits from screenrecord
62                         GRALLOC_USAGE_HW_VIDEO_ENCODER | GRALLOC_USAGE_SW_READ_OFTEN);
63                 sp<BufferListener> listener = sp<BufferListener>::make(this);
64                 itemConsumer->setFrameAvailableListener(listener);
65                 itemConsumer->setName(String8("Virtual disp consumer (TransactionTest)"));
66                 itemConsumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
67 #else
68                 sp<IGraphicBufferProducer> producer;
69                 sp<IGraphicBufferConsumer> consumer;
70                 sp<BufferItemConsumer> itemConsumer;
71                 BufferQueue::createBufferQueue(&producer, &consumer);
72 
73                 consumer->setConsumerName(String8("Virtual disp consumer (TransactionTest)"));
74                 consumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
75 
76                 itemConsumer = sp<BufferItemConsumer>::make(consumer,
77                                                             // Sample usage bits from screenrecord
78                                                             GRALLOC_USAGE_HW_VIDEO_ENCODER |
79                                                                     GRALLOC_USAGE_SW_READ_OFTEN);
80                 sp<BufferListener> listener = sp<BufferListener>::make(this);
81                 itemConsumer->setFrameAvailableListener(listener);
82 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
83 
84                 static const std::string kDisplayName("VirtualDisplay");
85                 vDisplay = SurfaceComposerClient::createVirtualDisplay(kDisplayName,
86                                                                        false /*isSecure*/);
87 
88                 constexpr ui::LayerStack layerStack{
89                         848472}; // ASCII for TTH (TransactionTestHarnesses)
90                 sp<SurfaceControl> mirrorSc =
91                         SurfaceComposerClient::getDefault()->mirrorDisplay(displayId);
92 
93                 SurfaceComposerClient::Transaction t;
94 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
95                 t.setDisplaySurface(vDisplay,
96                                     itemConsumer->getSurface()->getIGraphicBufferProducer());
97 #else
98                 t.setDisplaySurface(vDisplay, producer);
99 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
100                 t.setDisplayProjection(vDisplay, ui::Rotation::Rotation0, Rect(resolution),
101                                        Rect(resolution));
102                 t.setDisplayLayerStack(vDisplay, layerStack);
103                 t.setLayerStack(mirrorSc, layerStack);
104                 t.apply();
105                 SurfaceComposerClient::Transaction().apply(true);
106 
107                 std::unique_lock lock(mMutex);
108                 mAvailable = false;
109                 // Wait for frame buffer ready.
110                 mCondition.wait_for(lock, std::chrono::seconds(2),
111                                     [this]() NO_THREAD_SAFETY_ANALYSIS { return mAvailable; });
112 
113                 BufferItem item;
114                 itemConsumer->acquireBuffer(&item, 0, true);
115                 constexpr bool kContainsHdr = false;
116                 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer, kContainsHdr);
117                 itemConsumer->releaseBuffer(item);
118 
119                 // Possible race condition with destroying virtual displays, in which
120                 // CompositionEngine::present may attempt to be called on the same
121                 // display multiple times. The layerStack is set to invalid here so
122                 // that the display is ignored if that scenario occurs.
123                 t.setLayerStack(mirrorSc, ui::INVALID_LAYER_STACK);
124                 t.apply(true);
125                 SurfaceComposerClient::destroyVirtualDisplay(vDisplay);
126                 return sc;
127         }
128     }
129 
130 protected:
131     LayerTransactionTest* mDelegate;
132     RenderPath mRenderPath;
133     std::mutex mMutex;
134     std::condition_variable mCondition;
135     bool mAvailable = false;
136 
onFrameAvailable()137     void onFrameAvailable() {
138         std::unique_lock lock(mMutex);
139         mAvailable = true;
140         mCondition.notify_all();
141     }
142 
143     class BufferListener : public ConsumerBase::FrameAvailableListener {
144     public:
BufferListener(LayerRenderPathTestHarness * owner)145         BufferListener(LayerRenderPathTestHarness* owner) : mOwner(owner) {}
146         LayerRenderPathTestHarness* mOwner;
147 
onFrameAvailable(const BufferItem &)148         void onFrameAvailable(const BufferItem& /*item*/) { mOwner->onFrameAvailable(); }
149     };
150 };
151 
152 class LayerTypeTransactionHarness : public LayerTransactionTest {
153 public:
LayerTypeTransactionHarness(uint32_t layerType)154     LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
155 
156     sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
157                                    uint32_t flags = 0, SurfaceControl* parent = nullptr,
158                                    uint32_t* outTransformHint = nullptr,
159                                    PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
160         // if the flags already have a layer type specified, return an error
161         if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
162             return nullptr;
163         }
164         return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent,
165                                                  outTransformHint, format);
166     }
167 
fillLayerColor(const sp<SurfaceControl> & layer,const Color & color,uint32_t bufferWidth,uint32_t bufferHeight)168     void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, uint32_t bufferWidth,
169                         uint32_t bufferHeight) {
170         ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
171                                                                      bufferWidth, bufferHeight));
172     }
173 
fillLayerQuadrant(const sp<SurfaceControl> & layer,uint32_t bufferWidth,uint32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)174     void fillLayerQuadrant(const sp<SurfaceControl>& layer, uint32_t bufferWidth,
175                            uint32_t bufferHeight, const Color& topLeft, const Color& topRight,
176                            const Color& bottomLeft, const Color& bottomRight) {
177         ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
178                                                                         bufferWidth, bufferHeight,
179                                                                         topLeft, topRight,
180                                                                         bottomLeft, bottomRight));
181     }
182 
183 protected:
184     uint32_t mLayerType;
185 };
186 } // namespace android
187 #endif
188