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 <ui/DisplayState.h>
20 
21 #include "LayerTransactionTest.h"
22 
23 namespace android {
24 
25 using android::hardware::graphics::common::V1_1::BufferUsage;
26 
27 class LayerRenderPathTestHarness {
28 public:
LayerRenderPathTestHarness(LayerTransactionTest * delegate,RenderPath renderPath)29     LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
30           : mDelegate(delegate), mRenderPath(renderPath) {}
31 
getScreenCapture()32     std::unique_ptr<ScreenCapture> getScreenCapture() {
33         switch (mRenderPath) {
34             case RenderPath::SCREENSHOT:
35                 return mDelegate->screenshot();
36             case RenderPath::VIRTUAL_DISPLAY:
37 
38                 const auto displayToken = SurfaceComposerClient::getInternalDisplayToken();
39 
40                 ui::DisplayState displayState;
41                 SurfaceComposerClient::getDisplayState(displayToken, &displayState);
42 
43                 ui::DisplayMode displayMode;
44                 SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
45                 const ui::Size& resolution = displayMode.resolution;
46 
47                 sp<IBinder> vDisplay;
48                 sp<IGraphicBufferProducer> producer;
49                 sp<IGraphicBufferConsumer> consumer;
50                 sp<BufferItemConsumer> itemConsumer;
51                 BufferQueue::createBufferQueue(&producer, &consumer);
52 
53                 consumer->setConsumerName(String8("Virtual disp consumer"));
54                 consumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
55 
56                 itemConsumer = new BufferItemConsumer(consumer,
57                                                       // Sample usage bits from screenrecord
58                                                       GRALLOC_USAGE_HW_VIDEO_ENCODER |
59                                                               GRALLOC_USAGE_SW_READ_OFTEN);
60                 sp<BufferListener> listener = new BufferListener(this);
61                 itemConsumer->setFrameAvailableListener(listener);
62 
63                 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
64                                                                 false /*secure*/);
65 
66                 SurfaceComposerClient::Transaction t;
67                 t.setDisplaySurface(vDisplay, producer);
68                 t.setDisplayLayerStack(vDisplay, 0);
69                 t.setDisplayProjection(vDisplay, displayState.orientation,
70                                        Rect(displayState.layerStackSpaceRect), Rect(resolution));
71                 t.apply();
72                 SurfaceComposerClient::Transaction().apply(true);
73 
74                 std::unique_lock lock(mMutex);
75                 mAvailable = false;
76                 // Wait for frame buffer ready.
77                 mCondition.wait_for(lock, std::chrono::seconds(2),
78                                     [this]() NO_THREAD_SAFETY_ANALYSIS { return mAvailable; });
79 
80                 BufferItem item;
81                 itemConsumer->acquireBuffer(&item, 0, true);
82                 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
83                 itemConsumer->releaseBuffer(item);
84                 SurfaceComposerClient::destroyDisplay(vDisplay);
85                 return sc;
86         }
87     }
88 
89 protected:
90     LayerTransactionTest* mDelegate;
91     RenderPath mRenderPath;
92     std::mutex mMutex;
93     std::condition_variable mCondition;
94     bool mAvailable = false;
95 
onFrameAvailable()96     void onFrameAvailable() {
97         std::unique_lock lock(mMutex);
98         mAvailable = true;
99         mCondition.notify_all();
100     }
101 
102     class BufferListener : public ConsumerBase::FrameAvailableListener {
103     public:
BufferListener(LayerRenderPathTestHarness * owner)104         BufferListener(LayerRenderPathTestHarness* owner) : mOwner(owner) {}
105         LayerRenderPathTestHarness* mOwner;
106 
onFrameAvailable(const BufferItem &)107         void onFrameAvailable(const BufferItem& /*item*/) { mOwner->onFrameAvailable(); }
108     };
109 };
110 
111 class LayerTypeTransactionHarness : public LayerTransactionTest {
112 public:
LayerTypeTransactionHarness(uint32_t layerType)113     LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
114 
115     sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
116                                    uint32_t flags = 0, SurfaceControl* parent = nullptr,
117                                    uint32_t* outTransformHint = nullptr,
118                                    PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
119         // if the flags already have a layer type specified, return an error
120         if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
121             return nullptr;
122         }
123         return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent,
124                                                  outTransformHint, format);
125     }
126 
fillLayerColor(const sp<SurfaceControl> & layer,const Color & color,uint32_t bufferWidth,uint32_t bufferHeight)127     void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, uint32_t bufferWidth,
128                         uint32_t bufferHeight) {
129         ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
130                                                                      bufferWidth, bufferHeight));
131     }
132 
fillLayerQuadrant(const sp<SurfaceControl> & layer,uint32_t bufferWidth,uint32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)133     void fillLayerQuadrant(const sp<SurfaceControl>& layer, uint32_t bufferWidth,
134                            uint32_t bufferHeight, const Color& topLeft, const Color& topRight,
135                            const Color& bottomLeft, const Color& bottomRight) {
136         ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
137                                                                         bufferWidth, bufferHeight,
138                                                                         topLeft, topRight,
139                                                                         bottomLeft, bottomRight));
140     }
141 
142 protected:
143     uint32_t mLayerType;
144 };
145 } // namespace android
146 #endif
147