1/*
2 * Copyright (C) 2010 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#ifndef USE_HWC2
18#include "HWComposer_hwc1.h"
19#else
20
21#ifndef ANDROID_SF_HWCOMPOSER_H
22#define ANDROID_SF_HWCOMPOSER_H
23
24#include "HWC2.h"
25
26#include <stdint.h>
27#include <sys/types.h>
28
29#include <ui/Fence.h>
30
31#include <utils/BitSet.h>
32#include <utils/Condition.h>
33#include <utils/Mutex.h>
34#include <utils/StrongPointer.h>
35#include <utils/Thread.h>
36#include <utils/Timers.h>
37#include <utils/Vector.h>
38
39#include <memory>
40#include <set>
41#include <vector>
42
43extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
44                           const struct timespec *request,
45                           struct timespec *remain);
46
47struct framebuffer_device_t;
48
49namespace HWC2 {
50    class Device;
51    class Display;
52}
53
54namespace android {
55// ---------------------------------------------------------------------------
56
57class DisplayDevice;
58class Fence;
59class FloatRect;
60class GraphicBuffer;
61class NativeHandle;
62class Region;
63class String8;
64
65class HWComposer
66{
67public:
68    class EventHandler {
69        friend class HWComposer;
70        virtual void onVSyncReceived(
71            HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0;
72        virtual void onHotplugReceived(HWComposer* composer, int32_t disp, bool connected) = 0;
73        virtual void onInvalidateReceived(HWComposer* composer) = 0;
74    protected:
75        virtual ~EventHandler() {}
76    };
77
78    // useVrComposer is passed to the composer HAL. When true, the composer HAL
79    // will use the vr composer service, otherwise it uses the real hardware
80    // composer.
81    HWComposer(bool useVrComposer);
82
83    ~HWComposer();
84
85    void setEventHandler(EventHandler* handler);
86
87    bool hasCapability(HWC2::Capability capability) const;
88
89    // Attempts to allocate a virtual display. If the virtual display is created
90    // on the HWC device, outId will contain its HWC ID.
91    status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
92            android_pixel_format_t* format, int32_t* outId);
93
94    // Attempts to create a new layer on this display
95    std::shared_ptr<HWC2::Layer> createLayer(int32_t displayId);
96
97    // Asks the HAL what it can do
98    status_t prepare(DisplayDevice& displayDevice);
99
100    status_t setClientTarget(int32_t displayId, uint32_t slot,
101            const sp<Fence>& acquireFence,
102            const sp<GraphicBuffer>& target, android_dataspace_t dataspace);
103
104    // Present layers to the display and read releaseFences.
105    status_t presentAndGetReleaseFences(int32_t displayId);
106
107    // set power mode
108    status_t setPowerMode(int32_t displayId, int mode);
109
110    // set active config
111    status_t setActiveConfig(int32_t displayId, size_t configId);
112
113    // Sets a color transform to be applied to the result of composition
114    status_t setColorTransform(int32_t displayId, const mat4& transform);
115
116    // reset state when an external, non-virtual display is disconnected
117    void disconnectDisplay(int32_t displayId);
118
119    // does this display have layers handled by HWC
120    bool hasDeviceComposition(int32_t displayId) const;
121
122    // does this display have layers handled by GLES
123    bool hasClientComposition(int32_t displayId) const;
124
125    // get the present fence received from the last call to present.
126    sp<Fence> getPresentFence(int32_t displayId) const;
127
128    // Get last release fence for the given layer
129    sp<Fence> getLayerReleaseFence(int32_t displayId,
130            const std::shared_ptr<HWC2::Layer>& layer) const;
131
132    // Set the output buffer and acquire fence for a virtual display.
133    // Returns INVALID_OPERATION if displayId is not a virtual display.
134    status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
135            const sp<GraphicBuffer>& buf);
136
137    // After SurfaceFlinger has retrieved the release fences for all the frames,
138    // it can call this to clear the shared pointers in the release fence map
139    void clearReleaseFences(int32_t displayId);
140
141    // Returns the HDR capabilities of the given display
142    std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId);
143
144    // Events handling ---------------------------------------------------------
145
146    void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
147
148    // Query display parameters.  Pass in a display index (e.g.
149    // HWC_DISPLAY_PRIMARY).
150    nsecs_t getRefreshTimestamp(int32_t displayId) const;
151    bool isConnected(int32_t displayId) const;
152
153    // Non-const because it can update configMap inside of mDisplayData
154    std::vector<std::shared_ptr<const HWC2::Display::Config>>
155            getConfigs(int32_t displayId) const;
156
157    std::shared_ptr<const HWC2::Display::Config>
158            getActiveConfig(int32_t displayId) const;
159
160    std::vector<android_color_mode_t> getColorModes(int32_t displayId) const;
161
162    status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode);
163
164    bool isUsingVrComposer() const;
165
166    // for debugging ----------------------------------------------------------
167    void dump(String8& out) const;
168
169    android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
170private:
171    static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2;
172
173    void loadHwcModule(bool useVrComposer);
174
175    bool isValidDisplay(int32_t displayId) const;
176    static void validateChange(HWC2::Composition from, HWC2::Composition to);
177
178    struct cb_context;
179
180    void invalidate(const std::shared_ptr<HWC2::Display>& display);
181    void vsync(const std::shared_ptr<HWC2::Display>& display,
182            int64_t timestamp);
183    void hotplug(const std::shared_ptr<HWC2::Display>& display,
184            HWC2::Connection connected);
185
186    struct DisplayData {
187        DisplayData();
188        ~DisplayData();
189        void reset();
190
191        bool hasClientComposition;
192        bool hasDeviceComposition;
193        std::shared_ptr<HWC2::Display> hwcDisplay;
194        HWC2::DisplayRequest displayRequests;
195        sp<Fence> lastPresentFence;  // signals when the last set op retires
196        std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>>
197                releaseFences;
198        buffer_handle_t outbufHandle;
199        sp<Fence> outbufAcquireFence;
200        mutable std::unordered_map<int32_t,
201                std::shared_ptr<const HWC2::Display::Config>> configMap;
202
203        // protected by mVsyncLock
204        HWC2::Vsync vsyncEnabled;
205
206        bool validateWasSkipped;
207        HWC2::Error presentError;
208    };
209
210    std::unique_ptr<HWC2::Device>   mHwcDevice;
211    std::vector<DisplayData>        mDisplayData;
212    std::set<size_t>                mFreeDisplaySlots;
213    std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
214    // protect mDisplayData from races between prepare and dump
215    mutable Mutex mDisplayLock;
216
217    cb_context*                     mCBContext;
218    EventHandler*                   mEventHandler;
219    size_t                          mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
220    uint32_t                        mRemainingHwcVirtualDisplays;
221
222    // protected by mLock
223    mutable Mutex mLock;
224    mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync;
225
226    // thread-safe
227    mutable Mutex mVsyncLock;
228};
229
230// ---------------------------------------------------------------------------
231}; // namespace android
232
233#endif // ANDROID_SF_HWCOMPOSER_H
234
235#endif // #ifdef USE_HWC2
236