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