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