1/*
2 * Copyright 2017 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
17#pragma once
18
19#define HWC2_USE_CPP11
20#define HWC2_INCLUDE_STRINGIFICATION
21#include <composer-hal/2.1/ComposerClient.h>
22#undef HWC2_USE_CPP11
23#undef HWC2_INCLUDE_STRINGIFICATION
24#include "RenderState.h"
25
26// Needed for display type/ID enums
27#include <hardware/hwcomposer_defs.h>
28
29#include <utils/Condition.h>
30
31#include <chrono>
32
33using namespace android::hardware::graphics::composer::V2_1;
34using namespace android::hardware::graphics::composer::V2_1::hal;
35using namespace android::hardware;
36using namespace std::chrono_literals;
37
38namespace {
39class LayerImpl;
40class Frame;
41class DelayedEventGenerator;
42} // namespace
43
44namespace android {
45class SurfaceComposerClient;
46} // namespace android
47
48namespace sftest {
49
50// NOTE: The ID's need to be exactly these. VR composer and parts of
51// the SurfaceFlinger assume the display IDs to have these values
52// despite the enum being documented as a display type.
53// TODO: Reference to actual documentation
54constexpr Display PRIMARY_DISPLAY = static_cast<Display>(HWC_DISPLAY_PRIMARY);
55constexpr Display EXTERNAL_DISPLAY = static_cast<Display>(HWC_DISPLAY_EXTERNAL);
56
57class FakeComposerClient : public ComposerHal {
58public:
59    FakeComposerClient();
60    virtual ~FakeComposerClient();
61
62    bool hasCapability(hwc2_capability_t capability) override;
63
64    std::string dumpDebugInfo() override;
65    void registerEventCallback(EventCallback* callback) override;
66    void unregisterEventCallback() override;
67
68    uint32_t getMaxVirtualDisplayCount() override;
69    Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
70                               Display* outDisplay) override;
71    Error destroyVirtualDisplay(Display display) override;
72    Error createLayer(Display display, Layer* outLayer) override;
73    Error destroyLayer(Display display, Layer layer) override;
74
75    Error getActiveConfig(Display display, Config* outConfig) override;
76    Error getClientTargetSupport(Display display, uint32_t width, uint32_t height,
77                                 PixelFormat format, Dataspace dataspace) override;
78    Error getColorModes(Display display, hidl_vec<ColorMode>* outModes) override;
79    Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute,
80                              int32_t* outValue) override;
81    Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override;
82    Error getDisplayName(Display display, hidl_string* outName) override;
83    Error getDisplayType(Display display, IComposerClient::DisplayType* outType) override;
84    Error getDozeSupport(Display display, bool* outSupport) override;
85    Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes, float* outMaxLuminance,
86                             float* outMaxAverageLuminance, float* outMinLuminance) override;
87
88    Error setActiveConfig(Display display, Config config) override;
89    Error setColorMode(Display display, ColorMode mode) override;
90    Error setPowerMode(Display display, IComposerClient::PowerMode mode) override;
91    Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
92
93    Error setColorTransform(Display display, const float* matrix, int32_t hint) override;
94    Error setClientTarget(Display display, buffer_handle_t target, int32_t acquireFence,
95                          int32_t dataspace, const std::vector<hwc_rect_t>& damage) override;
96    Error setOutputBuffer(Display display, buffer_handle_t buffer, int32_t releaseFence) override;
97    Error validateDisplay(Display display, std::vector<Layer>* outChangedLayers,
98                          std::vector<IComposerClient::Composition>* outCompositionTypes,
99                          uint32_t* outDisplayRequestMask, std::vector<Layer>* outRequestedLayers,
100                          std::vector<uint32_t>* outRequestMasks) override;
101    Error acceptDisplayChanges(Display display) override;
102    Error presentDisplay(Display display, int32_t* outPresentFence, std::vector<Layer>* outLayers,
103                         std::vector<int32_t>* outReleaseFences) override;
104
105    Error setLayerCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override;
106    Error setLayerBuffer(Display display, Layer layer, buffer_handle_t buffer,
107                         int32_t acquireFence) override;
108    Error setLayerSurfaceDamage(Display display, Layer layer,
109                                const std::vector<hwc_rect_t>& damage) override;
110    Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override;
111    Error setLayerColor(Display display, Layer layer, IComposerClient::Color color) override;
112    Error setLayerCompositionType(Display display, Layer layer, int32_t type) override;
113    Error setLayerDataspace(Display display, Layer layer, int32_t dataspace) override;
114    Error setLayerDisplayFrame(Display display, Layer layer, const hwc_rect_t& frame) override;
115    Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
116    Error setLayerSidebandStream(Display display, Layer layer, buffer_handle_t stream) override;
117    Error setLayerSourceCrop(Display display, Layer layer, const hwc_frect_t& crop) override;
118    Error setLayerTransform(Display display, Layer layer, int32_t transform) override;
119    Error setLayerVisibleRegion(Display display, Layer layer,
120                                const std::vector<hwc_rect_t>& visible) override;
121    Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
122
123    void setClient(ComposerClient* client);
124
125    void requestVSync(uint64_t vsyncTime = 0);
126    // We don't want tests hanging, so always use a timeout. Remember
127    // to always check the number of frames with test ASSERT_!
128    // Wait until next frame is rendered after requesting vsync.
129    void runVSyncAndWait(std::chrono::nanoseconds maxWait = 100ms);
130    void runVSyncAfter(std::chrono::nanoseconds wait);
131
132    int getFrameCount() const;
133    // We don't want tests hanging, so always use a timeout. Remember
134    // to always check the number of frames with test ASSERT_!
135    void waitUntilFrame(int targetFrame, std::chrono::nanoseconds maxWait = 100ms) const;
136    std::vector<RenderState> getFrameRects(int frame) const;
137    std::vector<RenderState> getLatestFrame() const;
138    void clearFrames();
139
140    void onSurfaceFlingerStart();
141    void onSurfaceFlingerStop();
142
143    int getLayerCount() const;
144    Layer getLayer(size_t index) const;
145
146    void hotplugDisplay(Display display, IComposerCallback::Connection state);
147    void refreshDisplay(Display display);
148
149private:
150    LayerImpl& getLayerImpl(Layer handle);
151
152    EventCallback* mEventCallback;
153    Config mCurrentConfig;
154    bool mVsyncEnabled;
155    std::vector<std::unique_ptr<LayerImpl>> mLayers;
156    std::vector<std::unique_ptr<Frame>> mFrames;
157    // Using a pointer to hide the implementation into the CPP file.
158    std::unique_ptr<DelayedEventGenerator> mDelayedEventGenerator;
159    android::sp<android::SurfaceComposerClient> mSurfaceComposer; // For VSync injections
160    mutable android::Mutex mStateMutex;
161    mutable android::Condition mFramesAvailable;
162};
163
164} // namespace sftest
165