1651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza/*
2651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * Copyright 2015 The Android Open Source Project
3651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza *
4651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * Licensed under the Apache License, Version 2.0 (the "License");
5651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * you may not use this file except in compliance with the License.
6651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * You may obtain a copy of the License at
7651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza *
8651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza *      http://www.apache.org/licenses/LICENSE-2.0
9651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza *
10651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * Unless required by applicable law or agreed to in writing, software
11651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * distributed under the License is distributed on an "AS IS" BASIS,
12651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * See the License for the specific language governing permissions and
14651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza * limitations under the License.
15651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza */
16651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
17651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#ifndef ANDROID_SF_HWC2_H
18651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#define ANDROID_SF_HWC2_H
19651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
20651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#define HWC2_INCLUDE_STRINGIFICATION
21651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#define HWC2_USE_CPP11
22651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <hardware/hwcomposer2.h>
23651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#undef HWC2_INCLUDE_STRINGIFICATION
24651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#undef HWC2_USE_CPP11
25651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
267d7ae7345abfb91be55c4aed6c9be9d09a144e4cDan Stoza#include <ui/HdrCapabilities.h>
275df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza#include <ui/mat4.h>
287d7ae7345abfb91be55c4aed6c9be9d09a144e4cDan Stoza
29651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <utils/Log.h>
30651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <utils/StrongPointer.h>
31651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <utils/Timers.h>
32651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
33651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <functional>
34651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <string>
35651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <unordered_map>
36651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#include <vector>
37651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
38651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozanamespace android {
39651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    class Fence;
40651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    class FloatRect;
41651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    class GraphicBuffer;
42651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    class Rect;
43651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    class Region;
44651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza}
45651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
46651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozanamespace HWC2 {
47651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
48651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaclass Display;
49651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaclass Layer;
50651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
51651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozatypedef std::function<void(std::shared_ptr<Display>, Connection)>
52651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        HotplugCallback;
53651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozatypedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
54651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozatypedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
55651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
56651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaclass Device
57651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza{
58651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozapublic:
59651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Device(hwc2_device_t* device);
60651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    ~Device();
61651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
62651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    friend class HWC2::Display;
63651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    friend class HWC2::Layer;
64651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
65651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Required by HWC2
66651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
67651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::string dump() const;
68651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
69651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    const std::vector<Capability>& getCapabilities() const {
70651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        return mCapabilities;
71651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    };
72651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
73651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    uint32_t getMaxVirtualDisplayCount() const;
74651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Error createVirtualDisplay(uint32_t width, uint32_t height,
755cf424bc129f01ee12c7a4fbea1664276d29f970Dan Stoza            android_pixel_format_t* format,
76651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::shared_ptr<Display>* outDisplay);
77651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
78651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void registerHotplugCallback(HotplugCallback hotplug);
79651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void registerRefreshCallback(RefreshCallback refresh);
80651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void registerVsyncCallback(VsyncCallback vsync);
81651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
82651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // For use by callbacks
83651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
84651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void callHotplug(std::shared_ptr<Display> display, Connection connected);
85651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void callRefresh(std::shared_ptr<Display> display);
86651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
87651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
88651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Other Device methods
89651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
90651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // This will create a Display if one is not found, but it will not be marked
91651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // as connected
92651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
93651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
9409e7a27fabde3da99ba6dfd2fd20be7d1425a9adDan Stoza    bool hasCapability(HWC2::Capability capability) const;
9509e7a27fabde3da99ba6dfd2fd20be7d1425a9adDan Stoza
96651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaprivate:
97651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Initialization methods
98651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
99651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    template <typename PFN>
100651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] bool loadFunctionPointer(
101651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            FunctionDescriptor desc, PFN& outPFN) {
102651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        auto intDesc = static_cast<int32_t>(desc);
103651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
104651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        if (pfn != nullptr) {
105651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            outPFN = reinterpret_cast<PFN>(pfn);
106651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            return true;
107651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        } else {
108651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            ALOGE("Failed to load function %s", to_string(desc).c_str());
109651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            return false;
110651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        }
111651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    }
112651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
113651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    template <typename PFN, typename HOOK>
114651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void registerCallback(Callback callback, HOOK hook) {
115651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        static_assert(std::is_same<PFN, HOOK>::value,
116651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                "Incompatible function pointer");
117651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        auto intCallback = static_cast<int32_t>(callback);
118651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        auto callbackData = static_cast<hwc2_callback_data_t>(this);
119651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
120651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
121651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    }
122651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
123651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void loadCapabilities();
124651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void loadFunctionPointers();
125651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void registerCallbacks();
126651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
127651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // For use by Display
128651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
129651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void destroyVirtualDisplay(hwc2_display_t display);
130651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
131651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Member variables
132651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
133651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    hwc2_device_t* mHwcDevice;
134651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
135651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Device function pointers
136651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
137651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
138651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_DUMP mDump;
139651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
140651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
141651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
142651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Display function pointers
143651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
144651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_CREATE_LAYER mCreateLayer;
145651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_DESTROY_LAYER mDestroyLayer;
146651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
147651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
148076ac670262e448c531e6db7727cfade325866caDan Stoza    HWC2_PFN_GET_COLOR_MODES mGetColorModes;
149651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
150651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
151651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
152651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
153651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
154651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
1557d7ae7345abfb91be55c4aed6c9be9d09a144e4cDan Stoza    HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
156651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
157651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
158651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
159651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
160076ac670262e448c531e6db7727cfade325866caDan Stoza    HWC2_PFN_SET_COLOR_MODE mSetColorMode;
1615df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza    HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
162651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
163651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_POWER_MODE mSetPowerMode;
164651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
165651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
166651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
167651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Layer function pointers
168651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
169651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
170651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
171651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
172651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
173651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
1745df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza    HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
175651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
176651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
177651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
178651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
179651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
180651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
181651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
182651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
183651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::vector<Capability> mCapabilities;
184651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
185651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
186651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    HotplugCallback mHotplug;
187651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::vector<std::pair<std::shared_ptr<Display>, Connection>>
188651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            mPendingHotplugs;
189651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    RefreshCallback mRefresh;
190651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::vector<std::shared_ptr<Display>> mPendingRefreshes;
191651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    VsyncCallback mVsync;
192651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
193651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza};
194651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
195651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaclass Display : public std::enable_shared_from_this<Display>
196651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza{
197651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozapublic:
198651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Display(Device& device, hwc2_display_t id);
199651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    ~Display();
200651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
201651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    friend class HWC2::Device;
202651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    friend class HWC2::Layer;
203651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
204651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    class Config
205651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    {
206651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    public:
207651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        class Builder
208651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        {
209651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        public:
210651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            Builder(Display& display, hwc2_config_t id);
211651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
212651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::shared_ptr<const Config> build() {
213651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                return std::const_pointer_cast<const Config>(
214651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                        std::move(mConfig));
215651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            }
216651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
217651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            Builder& setWidth(int32_t width) {
218651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                mConfig->mWidth = width;
219651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                return *this;
220651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            }
221651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            Builder& setHeight(int32_t height) {
222651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                mConfig->mHeight = height;
223651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                return *this;
224651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            }
225651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            Builder& setVsyncPeriod(int32_t vsyncPeriod) {
226651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                mConfig->mVsyncPeriod = vsyncPeriod;
227651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                return *this;
228651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            }
229651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            Builder& setDpiX(int32_t dpiX) {
230651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                if (dpiX == -1) {
231651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                    mConfig->mDpiX = getDefaultDensity();
232651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                } else {
233651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                    mConfig->mDpiX = dpiX / 1000.0f;
234651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                }
235651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                return *this;
236651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            }
237651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            Builder& setDpiY(int32_t dpiY) {
238651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                if (dpiY == -1) {
239651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                    mConfig->mDpiY = getDefaultDensity();
240651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                } else {
241651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                    mConfig->mDpiY = dpiY / 1000.0f;
242651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                }
243651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                return *this;
244651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            }
245651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
246651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        private:
247651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            float getDefaultDensity();
248651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::shared_ptr<Config> mConfig;
249651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        };
250651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
251651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
252651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        hwc2_config_t getId() const { return mId; }
253651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
254651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        int32_t getWidth() const { return mWidth; }
255651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        int32_t getHeight() const { return mHeight; }
256651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
257651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        float getDpiX() const { return mDpiX; }
258651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        float getDpiY() const { return mDpiY; }
259651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
260651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    private:
261651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        Config(Display& display, hwc2_config_t id);
262651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
263651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        Display& mDisplay;
264651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        hwc2_config_t mId;
265651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
266651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        int32_t mWidth;
267651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        int32_t mHeight;
268651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        nsecs_t mVsyncPeriod;
269651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        float mDpiX;
270651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        float mDpiY;
271651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    };
272651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
273651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Required by HWC2
274651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
275651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error acceptChanges();
276651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error createLayer(
277651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::shared_ptr<Layer>* outLayer);
278651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error getActiveConfig(
279651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::shared_ptr<const Config>* outConfig) const;
280651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error getChangedCompositionTypes(
281651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
282076ac670262e448c531e6db7727cfade325866caDan Stoza    [[clang::warn_unused_result]] Error getColorModes(
283076ac670262e448c531e6db7727cfade325866caDan Stoza            std::vector<int32_t>* outModes) const;
284651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
285651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Doesn't call into the HWC2 device, so no errors are possible
286651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::vector<std::shared_ptr<const Config>> getConfigs() const;
287651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
288651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error getName(std::string* outName) const;
289651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error getRequests(
290651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            DisplayRequest* outDisplayRequests,
291651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
292651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                    outLayerRequests);
293651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
294651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
2957d7ae7345abfb91be55c4aed6c9be9d09a144e4cDan Stoza    [[clang::warn_unused_result]] Error getHdrCapabilities(
2967d7ae7345abfb91be55c4aed6c9be9d09a144e4cDan Stoza            std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
297651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error getReleaseFences(
298651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            std::unordered_map<std::shared_ptr<Layer>,
299651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza                    android::sp<android::Fence>>* outFences) const;
300651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error present(
301651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            android::sp<android::Fence>* outRetireFence);
302651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setActiveConfig(
303651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const std::shared_ptr<const Config>& config);
304651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setClientTarget(
305651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            buffer_handle_t target,
306651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::sp<android::Fence>& acquireFence,
307651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            android_dataspace_t dataspace);
308076ac670262e448c531e6db7727cfade325866caDan Stoza    [[clang::warn_unused_result]] Error setColorMode(int32_t mode);
3095df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza    [[clang::warn_unused_result]] Error setColorTransform(
3105df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza            const android::mat4& matrix, android_color_transform_t hint);
311651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setOutputBuffer(
312651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::sp<android::GraphicBuffer>& buffer,
313651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::sp<android::Fence>& releaseFence);
314651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
315651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
316651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
317651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            uint32_t* outNumRequests);
318651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
319651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Other Display methods
320651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
321651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Device& getDevice() const { return mDevice; }
322651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    hwc2_display_t getId() const { return mId; }
323651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    bool isConnected() const { return mIsConnected; }
324651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
325651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaprivate:
326651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // For use by Device
327651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
328651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Virtual displays are always connected
329651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void setVirtual() {
330651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        mIsVirtual = true;
331651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza        mIsConnected = true;
332651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    }
333651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
334651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void setConnected(bool connected) { mIsConnected = connected; }
335651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
336651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void loadConfig(hwc2_config_t configId);
337651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void loadConfigs();
338651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
339651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // For use by Layer
340651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    void destroyLayer(hwc2_layer_t layerId);
341651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
342651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // This may fail (and return a null pointer) if no layer with this ID exists
343651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // on this display
344651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
345651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
346651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    // Member variables
347651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
348651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Device& mDevice;
349651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    hwc2_display_t mId;
350651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    bool mIsConnected;
351651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    bool mIsVirtual;
352651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
353651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
354651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza};
355651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
356651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaclass Layer
357651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza{
358651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozapublic:
359651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
360651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    ~Layer();
361651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
362651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    bool isAbandoned() const { return mDisplay.expired(); }
363651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    hwc2_layer_t getId() const { return mId; }
364651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
365651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
366651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
367651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::sp<android::Fence>& acquireFence);
368651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setSurfaceDamage(
369651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::Region& damage);
370651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
371651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
372651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
373651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setCompositionType(Composition type);
3745df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza    [[clang::warn_unused_result]] Error setDataspace(
3755df2a86063c6a83813fc1aa3d8938a82f7ff8f14Dan Stoza            android_dataspace_t dataspace);
376651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setDisplayFrame(
377651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::Rect& frame);
378651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
379651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setSidebandStream(
380651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const native_handle_t* stream);
381651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setSourceCrop(
382651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::FloatRect& crop);
383651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setTransform(Transform transform);
384651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setVisibleRegion(
385651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza            const android::Region& region);
386651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
387651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
388651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stozaprivate:
389651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    std::weak_ptr<Display> mDisplay;
390651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    hwc2_display_t mDisplayId;
391651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    Device& mDevice;
392651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza    hwc2_layer_t mId;
393651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza};
394651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
395651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza} // namespace HWC2
396651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza
397651bf314a6a1a77e4cbb66ffb81221200f918290Dan Stoza#endif // ANDROID_SF_HWC2_H
398