Home | History | Annotate | Download | only in DisplayHardware
      1 /*
      2  * Copyright 2015 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_HWC2_H
     18 #define ANDROID_SF_HWC2_H
     19 
     20 #define HWC2_INCLUDE_STRINGIFICATION
     21 #define HWC2_USE_CPP11
     22 #include <hardware/hwcomposer2.h>
     23 #undef HWC2_INCLUDE_STRINGIFICATION
     24 #undef HWC2_USE_CPP11
     25 
     26 #include <ui/HdrCapabilities.h>
     27 #include <ui/mat4.h>
     28 
     29 #include <utils/Log.h>
     30 #include <utils/StrongPointer.h>
     31 #include <utils/Timers.h>
     32 
     33 #include <functional>
     34 #include <string>
     35 #include <unordered_map>
     36 #include <vector>
     37 
     38 namespace android {
     39     class Fence;
     40     class FloatRect;
     41     class GraphicBuffer;
     42     class Rect;
     43     class Region;
     44 }
     45 
     46 namespace HWC2 {
     47 
     48 class Display;
     49 class Layer;
     50 
     51 typedef std::function<void(std::shared_ptr<Display>, Connection)>
     52         HotplugCallback;
     53 typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
     54 typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
     55 
     56 class Device
     57 {
     58 public:
     59     Device(hwc2_device_t* device);
     60     ~Device();
     61 
     62     friend class HWC2::Display;
     63     friend class HWC2::Layer;
     64 
     65     // Required by HWC2
     66 
     67     std::string dump() const;
     68 
     69     const std::vector<Capability>& getCapabilities() const {
     70         return mCapabilities;
     71     };
     72 
     73     uint32_t getMaxVirtualDisplayCount() const;
     74     Error createVirtualDisplay(uint32_t width, uint32_t height,
     75             android_pixel_format_t* format,
     76             std::shared_ptr<Display>* outDisplay);
     77 
     78     void registerHotplugCallback(HotplugCallback hotplug);
     79     void registerRefreshCallback(RefreshCallback refresh);
     80     void registerVsyncCallback(VsyncCallback vsync);
     81 
     82     // For use by callbacks
     83 
     84     void callHotplug(std::shared_ptr<Display> display, Connection connected);
     85     void callRefresh(std::shared_ptr<Display> display);
     86     void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
     87 
     88     // Other Device methods
     89 
     90     // This will create a Display if one is not found, but it will not be marked
     91     // as connected
     92     std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
     93 
     94     bool hasCapability(HWC2::Capability capability) const;
     95 
     96 private:
     97     // Initialization methods
     98 
     99     template <typename PFN>
    100     [[clang::warn_unused_result]] bool loadFunctionPointer(
    101             FunctionDescriptor desc, PFN& outPFN) {
    102         auto intDesc = static_cast<int32_t>(desc);
    103         auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
    104         if (pfn != nullptr) {
    105             outPFN = reinterpret_cast<PFN>(pfn);
    106             return true;
    107         } else {
    108             ALOGE("Failed to load function %s", to_string(desc).c_str());
    109             return false;
    110         }
    111     }
    112 
    113     template <typename PFN, typename HOOK>
    114     void registerCallback(Callback callback, HOOK hook) {
    115         static_assert(std::is_same<PFN, HOOK>::value,
    116                 "Incompatible function pointer");
    117         auto intCallback = static_cast<int32_t>(callback);
    118         auto callbackData = static_cast<hwc2_callback_data_t>(this);
    119         auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
    120         mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
    121     }
    122 
    123     void loadCapabilities();
    124     void loadFunctionPointers();
    125     void registerCallbacks();
    126 
    127     // For use by Display
    128 
    129     void destroyVirtualDisplay(hwc2_display_t display);
    130 
    131     // Member variables
    132 
    133     hwc2_device_t* mHwcDevice;
    134 
    135     // Device function pointers
    136     HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
    137     HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
    138     HWC2_PFN_DUMP mDump;
    139     HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
    140     HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
    141 
    142     // Display function pointers
    143     HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
    144     HWC2_PFN_CREATE_LAYER mCreateLayer;
    145     HWC2_PFN_DESTROY_LAYER mDestroyLayer;
    146     HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
    147     HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
    148     HWC2_PFN_GET_COLOR_MODES mGetColorModes;
    149     HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
    150     HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
    151     HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
    152     HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
    153     HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
    154     HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
    155     HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
    156     HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
    157     HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
    158     HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
    159     HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
    160     HWC2_PFN_SET_COLOR_MODE mSetColorMode;
    161     HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
    162     HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
    163     HWC2_PFN_SET_POWER_MODE mSetPowerMode;
    164     HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
    165     HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
    166 
    167     // Layer function pointers
    168     HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
    169     HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
    170     HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
    171     HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
    172     HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
    173     HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
    174     HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
    175     HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
    176     HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
    177     HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
    178     HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
    179     HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
    180     HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
    181     HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
    182 
    183     std::vector<Capability> mCapabilities;
    184     std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
    185 
    186     HotplugCallback mHotplug;
    187     std::vector<std::pair<std::shared_ptr<Display>, Connection>>
    188             mPendingHotplugs;
    189     RefreshCallback mRefresh;
    190     std::vector<std::shared_ptr<Display>> mPendingRefreshes;
    191     VsyncCallback mVsync;
    192     std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
    193 };
    194 
    195 class Display : public std::enable_shared_from_this<Display>
    196 {
    197 public:
    198     Display(Device& device, hwc2_display_t id);
    199     ~Display();
    200 
    201     friend class HWC2::Device;
    202     friend class HWC2::Layer;
    203 
    204     class Config
    205     {
    206     public:
    207         class Builder
    208         {
    209         public:
    210             Builder(Display& display, hwc2_config_t id);
    211 
    212             std::shared_ptr<const Config> build() {
    213                 return std::const_pointer_cast<const Config>(
    214                         std::move(mConfig));
    215             }
    216 
    217             Builder& setWidth(int32_t width) {
    218                 mConfig->mWidth = width;
    219                 return *this;
    220             }
    221             Builder& setHeight(int32_t height) {
    222                 mConfig->mHeight = height;
    223                 return *this;
    224             }
    225             Builder& setVsyncPeriod(int32_t vsyncPeriod) {
    226                 mConfig->mVsyncPeriod = vsyncPeriod;
    227                 return *this;
    228             }
    229             Builder& setDpiX(int32_t dpiX) {
    230                 if (dpiX == -1) {
    231                     mConfig->mDpiX = getDefaultDensity();
    232                 } else {
    233                     mConfig->mDpiX = dpiX / 1000.0f;
    234                 }
    235                 return *this;
    236             }
    237             Builder& setDpiY(int32_t dpiY) {
    238                 if (dpiY == -1) {
    239                     mConfig->mDpiY = getDefaultDensity();
    240                 } else {
    241                     mConfig->mDpiY = dpiY / 1000.0f;
    242                 }
    243                 return *this;
    244             }
    245 
    246         private:
    247             float getDefaultDensity();
    248             std::shared_ptr<Config> mConfig;
    249         };
    250 
    251         hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
    252         hwc2_config_t getId() const { return mId; }
    253 
    254         int32_t getWidth() const { return mWidth; }
    255         int32_t getHeight() const { return mHeight; }
    256         nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
    257         float getDpiX() const { return mDpiX; }
    258         float getDpiY() const { return mDpiY; }
    259 
    260     private:
    261         Config(Display& display, hwc2_config_t id);
    262 
    263         Display& mDisplay;
    264         hwc2_config_t mId;
    265 
    266         int32_t mWidth;
    267         int32_t mHeight;
    268         nsecs_t mVsyncPeriod;
    269         float mDpiX;
    270         float mDpiY;
    271     };
    272 
    273     // Required by HWC2
    274 
    275     [[clang::warn_unused_result]] Error acceptChanges();
    276     [[clang::warn_unused_result]] Error createLayer(
    277             std::shared_ptr<Layer>* outLayer);
    278     [[clang::warn_unused_result]] Error getActiveConfig(
    279             std::shared_ptr<const Config>* outConfig) const;
    280     [[clang::warn_unused_result]] Error getChangedCompositionTypes(
    281             std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
    282     [[clang::warn_unused_result]] Error getColorModes(
    283             std::vector<int32_t>* outModes) const;
    284 
    285     // Doesn't call into the HWC2 device, so no errors are possible
    286     std::vector<std::shared_ptr<const Config>> getConfigs() const;
    287 
    288     [[clang::warn_unused_result]] Error getName(std::string* outName) const;
    289     [[clang::warn_unused_result]] Error getRequests(
    290             DisplayRequest* outDisplayRequests,
    291             std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
    292                     outLayerRequests);
    293     [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
    294     [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
    295     [[clang::warn_unused_result]] Error getHdrCapabilities(
    296             std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
    297     [[clang::warn_unused_result]] Error getReleaseFences(
    298             std::unordered_map<std::shared_ptr<Layer>,
    299                     android::sp<android::Fence>>* outFences) const;
    300     [[clang::warn_unused_result]] Error present(
    301             android::sp<android::Fence>* outRetireFence);
    302     [[clang::warn_unused_result]] Error setActiveConfig(
    303             const std::shared_ptr<const Config>& config);
    304     [[clang::warn_unused_result]] Error setClientTarget(
    305             buffer_handle_t target,
    306             const android::sp<android::Fence>& acquireFence,
    307             android_dataspace_t dataspace);
    308     [[clang::warn_unused_result]] Error setColorMode(int32_t mode);
    309     [[clang::warn_unused_result]] Error setColorTransform(
    310             const android::mat4& matrix, android_color_transform_t hint);
    311     [[clang::warn_unused_result]] Error setOutputBuffer(
    312             const android::sp<android::GraphicBuffer>& buffer,
    313             const android::sp<android::Fence>& releaseFence);
    314     [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
    315     [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
    316     [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
    317             uint32_t* outNumRequests);
    318 
    319     // Other Display methods
    320 
    321     Device& getDevice() const { return mDevice; }
    322     hwc2_display_t getId() const { return mId; }
    323     bool isConnected() const { return mIsConnected; }
    324 
    325 private:
    326     // For use by Device
    327 
    328     // Virtual displays are always connected
    329     void setVirtual() {
    330         mIsVirtual = true;
    331         mIsConnected = true;
    332     }
    333 
    334     void setConnected(bool connected) { mIsConnected = connected; }
    335     int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
    336     void loadConfig(hwc2_config_t configId);
    337     void loadConfigs();
    338 
    339     // For use by Layer
    340     void destroyLayer(hwc2_layer_t layerId);
    341 
    342     // This may fail (and return a null pointer) if no layer with this ID exists
    343     // on this display
    344     std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
    345 
    346     // Member variables
    347 
    348     Device& mDevice;
    349     hwc2_display_t mId;
    350     bool mIsConnected;
    351     bool mIsVirtual;
    352     std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
    353     std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
    354 };
    355 
    356 class Layer
    357 {
    358 public:
    359     Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
    360     ~Layer();
    361 
    362     bool isAbandoned() const { return mDisplay.expired(); }
    363     hwc2_layer_t getId() const { return mId; }
    364 
    365     [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
    366     [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
    367             const android::sp<android::Fence>& acquireFence);
    368     [[clang::warn_unused_result]] Error setSurfaceDamage(
    369             const android::Region& damage);
    370 
    371     [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
    372     [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
    373     [[clang::warn_unused_result]] Error setCompositionType(Composition type);
    374     [[clang::warn_unused_result]] Error setDataspace(
    375             android_dataspace_t dataspace);
    376     [[clang::warn_unused_result]] Error setDisplayFrame(
    377             const android::Rect& frame);
    378     [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
    379     [[clang::warn_unused_result]] Error setSidebandStream(
    380             const native_handle_t* stream);
    381     [[clang::warn_unused_result]] Error setSourceCrop(
    382             const android::FloatRect& crop);
    383     [[clang::warn_unused_result]] Error setTransform(Transform transform);
    384     [[clang::warn_unused_result]] Error setVisibleRegion(
    385             const android::Region& region);
    386     [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
    387 
    388 private:
    389     std::weak_ptr<Display> mDisplay;
    390     hwc2_display_t mDisplayId;
    391     Device& mDevice;
    392     hwc2_layer_t mId;
    393 };
    394 
    395 } // namespace HWC2
    396 
    397 #endif // ANDROID_SF_HWC2_H
    398