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