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 ids = SurfaceComposerClient::getPhysicalDisplayIds(); 39 const auto displayToken = ids.empty() 40 ? nullptr 41 : SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); 42 43 ui::DisplayState displayState; 44 SurfaceComposerClient::getDisplayState(displayToken, &displayState); 45 46 ui::DisplayMode displayMode; 47 SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode); 48 const ui::Size& resolution = displayMode.resolution; 49 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")); 57 consumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight()); 58 59 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); 64 itemConsumer->setFrameAvailableListener(listener); 65 66 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), 67 false /*secure*/); 68 69 SurfaceComposerClient::Transaction t; 70 t.setDisplaySurface(vDisplay, producer); 71 t.setDisplayLayerStack(vDisplay, ui::DEFAULT_LAYER_STACK); 72 t.setDisplayProjection(vDisplay, displayState.orientation, 73 Rect(displayState.layerStackSpaceRect), Rect(resolution)); 74 t.apply(); 75 SurfaceComposerClient::Transaction().apply(true); 76 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 83 BufferItem item; 84 itemConsumer->acquireBuffer(&item, 0, true); 85 constexpr bool kContainsHdr = false; 86 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer, kContainsHdr); 87 itemConsumer->releaseBuffer(item); 88 SurfaceComposerClient::destroyDisplay(vDisplay); 89 return sc; 90 } 91 } 92 93 protected: 94 LayerTransactionTest* mDelegate; 95 RenderPath mRenderPath; 96 std::mutex mMutex; 97 std::condition_variable mCondition; 98 bool mAvailable = false; 99 onFrameAvailable()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: BufferListener(LayerRenderPathTestHarness * owner)108 BufferListener(LayerRenderPathTestHarness* owner) : mOwner(owner) {} 109 LayerRenderPathTestHarness* mOwner; 110 onFrameAvailable(const BufferItem &)111 void onFrameAvailable(const BufferItem& /*item*/) { mOwner->onFrameAvailable(); } 112 }; 113 }; 114 115 class LayerTypeTransactionHarness : public LayerTransactionTest { 116 public: LayerTypeTransactionHarness(uint32_t layerType)117 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {} 118 119 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height, 120 uint32_t flags = 0, SurfaceControl* parent = nullptr, 121 uint32_t* outTransformHint = nullptr, 122 PixelFormat format = PIXEL_FORMAT_RGBA_8888) { 123 // if the flags already have a layer type specified, return an error 124 if (flags & ISurfaceComposerClient::eFXSurfaceMask) { 125 return nullptr; 126 } 127 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent, 128 outTransformHint, format); 129 } 130 fillLayerColor(const sp<SurfaceControl> & layer,const Color & color,uint32_t bufferWidth,uint32_t bufferHeight)131 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, uint32_t bufferWidth, 132 uint32_t bufferHeight) { 133 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color, 134 bufferWidth, bufferHeight)); 135 } 136 fillLayerQuadrant(const sp<SurfaceControl> & layer,uint32_t bufferWidth,uint32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)137 void fillLayerQuadrant(const sp<SurfaceControl>& layer, uint32_t bufferWidth, 138 uint32_t bufferHeight, const Color& topLeft, const Color& topRight, 139 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 146 protected: 147 uint32_t mLayerType; 148 }; 149 } // namespace android 150 #endif 151