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 ANDROID_SF_HWCOMPOSER_HWC1_H
     18 #define ANDROID_SF_HWCOMPOSER_HWC1_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <hardware/hwcomposer_defs.h>
     24 
     25 #include <system/graphics.h>
     26 
     27 #include <ui/Fence.h>
     28 
     29 #include <utils/BitSet.h>
     30 #include <utils/Condition.h>
     31 #include <utils/Mutex.h>
     32 #include <utils/StrongPointer.h>
     33 #include <utils/Thread.h>
     34 #include <utils/Timers.h>
     35 #include <utils/Vector.h>
     36 
     37 extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
     38                            const struct timespec *request,
     39                            struct timespec *remain);
     40 
     41 struct hwc_composer_device_1;
     42 struct hwc_display_contents_1;
     43 struct hwc_layer_1;
     44 struct hwc_procs;
     45 struct framebuffer_device_t;
     46 
     47 namespace android {
     48 // ---------------------------------------------------------------------------
     49 
     50 class Fence;
     51 class FloatRect;
     52 class GraphicBuffer;
     53 class NativeHandle;
     54 class Region;
     55 class String8;
     56 class SurfaceFlinger;
     57 
     58 class HWComposer
     59 {
     60 public:
     61     class EventHandler {
     62         friend class HWComposer;
     63         virtual void onVSyncReceived(
     64             HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0;
     65         virtual void onHotplugReceived(HWComposer* composer, int disp, bool connected) = 0;
     66         virtual void onInvalidateReceived(HWComposer* composer) = 0;
     67     protected:
     68         virtual ~EventHandler() {}
     69     };
     70 
     71     enum {
     72         NUM_BUILTIN_DISPLAYS = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
     73         MAX_HWC_DISPLAYS = HWC_NUM_DISPLAY_TYPES,
     74         VIRTUAL_DISPLAY_ID_BASE = HWC_DISPLAY_VIRTUAL,
     75     };
     76 
     77     HWComposer(
     78             const sp<SurfaceFlinger>& flinger,
     79             EventHandler& handler);
     80 
     81     ~HWComposer();
     82 
     83     status_t initCheck() const;
     84 
     85     // Returns a display ID starting at VIRTUAL_DISPLAY_ID_BASE, this ID is to
     86     // be used with createWorkList (and all other methods requiring an ID
     87     // below).
     88     // IDs below NUM_BUILTIN_DISPLAYS are pre-defined and therefore are
     89     // always valid.
     90     // Returns -1 if an ID cannot be allocated
     91     int32_t allocateDisplayId();
     92 
     93     // Recycles the given virtual display ID and frees the associated worklist.
     94     // IDs below NUM_BUILTIN_DISPLAYS are not recycled.
     95     status_t freeDisplayId(int32_t id);
     96 
     97 
     98     // Asks the HAL what it can do
     99     status_t prepare();
    100 
    101     // commits the list
    102     status_t commit();
    103 
    104     // set power mode
    105     status_t setPowerMode(int disp, int mode);
    106 
    107     // set active config
    108     status_t setActiveConfig(int disp, int mode);
    109 
    110     // reset state when an external, non-virtual display is disconnected
    111     void disconnectDisplay(int disp);
    112 
    113     // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
    114     status_t createWorkList(int32_t id, size_t numLayers);
    115 
    116     bool supportsFramebufferTarget() const;
    117 
    118     // does this display have layers handled by HWC
    119     bool hasHwcComposition(int32_t id) const;
    120 
    121     // does this display have layers handled by GLES
    122     bool hasGlesComposition(int32_t id) const;
    123 
    124     // get the releaseFence file descriptor for a display's framebuffer layer.
    125     // the release fence is only valid after commit()
    126     sp<Fence> getAndResetReleaseFence(int32_t id);
    127 
    128     // needed forward declarations
    129     class LayerListIterator;
    130 
    131     // return the visual id to be used to find a suitable EGLConfig for
    132     // *ALL* displays.
    133     int getVisualID() const;
    134 
    135     // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
    136     int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
    137     int fbCompositionComplete();
    138     void fbDump(String8& result);
    139 
    140     // Set the output buffer and acquire fence for a virtual display.
    141     // Returns INVALID_OPERATION if id is not a virtual display.
    142     status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
    143             const sp<GraphicBuffer>& buf);
    144 
    145     // Get the retire fence for the last committed frame. This fence will
    146     // signal when the h/w composer is completely finished with the frame.
    147     // For physical displays, it is no longer being displayed. For virtual
    148     // displays, writes to the output buffer are complete.
    149     sp<Fence> getLastRetireFence(int32_t id) const;
    150 
    151     status_t setCursorPositionAsync(int32_t id, const Rect &pos);
    152 
    153     /*
    154      * Interface to hardware composer's layers functionality.
    155      * This abstracts the HAL interface to layers which can evolve in
    156      * incompatible ways from one release to another.
    157      * The idea is that we could extend this interface as we add
    158      * features to h/w composer.
    159      */
    160     class HWCLayerInterface {
    161     protected:
    162         virtual ~HWCLayerInterface() { }
    163     public:
    164         virtual int32_t getCompositionType() const = 0;
    165         virtual uint32_t getHints() const = 0;
    166         virtual sp<Fence> getAndResetReleaseFence() = 0;
    167         virtual void setDefaultState() = 0;
    168         virtual void setSkip(bool skip) = 0;
    169         virtual void setIsCursorLayerHint(bool isCursor = true) = 0;
    170         virtual void setBlending(uint32_t blending) = 0;
    171         virtual void setTransform(uint32_t transform) = 0;
    172         virtual void setFrame(const Rect& frame) = 0;
    173         virtual void setCrop(const FloatRect& crop) = 0;
    174         virtual void setVisibleRegionScreen(const Region& reg) = 0;
    175         virtual void setSurfaceDamage(const Region& reg) = 0;
    176         virtual void setSidebandStream(const sp<NativeHandle>& stream) = 0;
    177         virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
    178         virtual void setAcquireFenceFd(int fenceFd) = 0;
    179         virtual void setPlaneAlpha(uint8_t alpha) = 0;
    180         virtual void onDisplayed() = 0;
    181     };
    182 
    183     /*
    184      * Interface used to implement an iterator to a list
    185      * of HWCLayer.
    186      */
    187     class HWCLayer : public HWCLayerInterface {
    188         friend class LayerListIterator;
    189         // select the layer at the given index
    190         virtual status_t setLayer(size_t index) = 0;
    191         virtual HWCLayer* dup() = 0;
    192         static HWCLayer* copy(HWCLayer *rhs) {
    193             return rhs ? rhs->dup() : NULL;
    194         }
    195     protected:
    196         virtual ~HWCLayer() { }
    197     };
    198 
    199     /*
    200      * Iterator through a HWCLayer list.
    201      * This behaves more or less like a forward iterator.
    202      */
    203     class LayerListIterator {
    204         friend class HWComposer;
    205         HWCLayer* const mLayerList;
    206         size_t mIndex;
    207 
    208         LayerListIterator() : mLayerList(NULL), mIndex(0) { }
    209 
    210         LayerListIterator(HWCLayer* layer, size_t index)
    211             : mLayerList(layer), mIndex(index) { }
    212 
    213         // we don't allow assignment, because we don't need it for now
    214         LayerListIterator& operator = (const LayerListIterator& rhs);
    215 
    216     public:
    217         // copy operators
    218         LayerListIterator(const LayerListIterator& rhs)
    219             : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
    220         }
    221 
    222         ~LayerListIterator() { delete mLayerList; }
    223 
    224         // pre-increment
    225         LayerListIterator& operator++() {
    226             mLayerList->setLayer(++mIndex);
    227             return *this;
    228         }
    229 
    230         // dereference
    231         HWCLayerInterface& operator * () { return *mLayerList; }
    232         HWCLayerInterface* operator -> () { return mLayerList; }
    233 
    234         // comparison
    235         bool operator == (const LayerListIterator& rhs) const {
    236             return mIndex == rhs.mIndex;
    237         }
    238         bool operator != (const LayerListIterator& rhs) const {
    239             return !operator==(rhs);
    240         }
    241     };
    242 
    243     // Returns an iterator to the beginning of the layer list
    244     LayerListIterator begin(int32_t id);
    245 
    246     // Returns an iterator to the end of the layer list
    247     LayerListIterator end(int32_t id);
    248 
    249 
    250     // Events handling ---------------------------------------------------------
    251 
    252     enum {
    253         EVENT_VSYNC = HWC_EVENT_VSYNC
    254     };
    255 
    256     void eventControl(int disp, int event, int enabled);
    257 
    258     struct DisplayConfig {
    259         uint32_t width;
    260         uint32_t height;
    261         float xdpi;
    262         float ydpi;
    263         nsecs_t refresh;
    264         android_color_mode_t colorMode;
    265         bool operator==(const DisplayConfig& rhs) const {
    266             return width == rhs.width &&
    267                     height == rhs.height &&
    268                     xdpi == rhs.xdpi &&
    269                     ydpi == rhs.ydpi &&
    270                     refresh == rhs.refresh &&
    271                     colorMode == rhs.colorMode;
    272         }
    273     };
    274 
    275     // Query display parameters.  Pass in a display index (e.g.
    276     // HWC_DISPLAY_PRIMARY).
    277     nsecs_t getRefreshTimestamp(int disp) const;
    278     sp<Fence> getDisplayFence(int disp) const;
    279     uint32_t getFormat(int disp) const;
    280     bool isConnected(int disp) const;
    281 
    282     // These return the values for the current config of a given display index.
    283     // To get the values for all configs, use getConfigs below.
    284     uint32_t getWidth(int disp) const;
    285     uint32_t getHeight(int disp) const;
    286     float getDpiX(int disp) const;
    287     float getDpiY(int disp) const;
    288     nsecs_t getRefreshPeriod(int disp) const;
    289     android_color_mode_t getColorMode(int disp) const;
    290 
    291     const Vector<DisplayConfig>& getConfigs(int disp) const;
    292     size_t getCurrentConfig(int disp) const;
    293 
    294     status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
    295             uint32_t format);
    296 
    297     // this class is only used to fake the VSync event on systems that don't
    298     // have it.
    299     class VSyncThread : public Thread {
    300         HWComposer& mHwc;
    301         mutable Mutex mLock;
    302         Condition mCondition;
    303         bool mEnabled;
    304         mutable nsecs_t mNextFakeVSync;
    305         nsecs_t mRefreshPeriod;
    306         virtual void onFirstRef();
    307         virtual bool threadLoop();
    308     public:
    309         VSyncThread(HWComposer& hwc);
    310         void setEnabled(bool enabled);
    311     };
    312 
    313     friend class VSyncThread;
    314 
    315     // for debugging ----------------------------------------------------------
    316     void dump(String8& out) const;
    317 
    318 private:
    319     void loadHwcModule();
    320     int loadFbHalModule();
    321 
    322     LayerListIterator getLayerIterator(int32_t id, size_t index);
    323 
    324     struct cb_context;
    325 
    326     static void hook_invalidate(const struct hwc_procs* procs);
    327     static void hook_vsync(const struct hwc_procs* procs, int disp,
    328             int64_t timestamp);
    329     static void hook_hotplug(const struct hwc_procs* procs, int disp,
    330             int connected);
    331 
    332     inline void invalidate();
    333     inline void vsync(int disp, int64_t timestamp);
    334     inline void hotplug(int disp, int connected);
    335 
    336     status_t queryDisplayProperties(int disp);
    337 
    338     status_t setFramebufferTarget(int32_t id,
    339             const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
    340 
    341     struct DisplayData {
    342         DisplayData();
    343         ~DisplayData();
    344         Vector<DisplayConfig> configs;
    345         size_t currentConfig;
    346         uint32_t format;    // pixel format from FB hal, for pre-hwc-1.1
    347         bool connected;
    348         bool hasFbComp;
    349         bool hasOvComp;
    350         size_t capacity;
    351         hwc_display_contents_1* list;
    352         hwc_layer_1* framebufferTarget;
    353         buffer_handle_t fbTargetHandle;
    354         sp<Fence> lastRetireFence;  // signals when the last set op retires
    355         sp<Fence> lastDisplayFence; // signals when the last set op takes
    356                                     // effect on screen
    357         buffer_handle_t outbufHandle;
    358         sp<Fence> outbufAcquireFence;
    359 
    360         // protected by mEventControlLock
    361         int32_t events;
    362 
    363         // We need to hold "copies" of these for memory management purposes. The
    364         // actual hwc_layer_1_t holds pointers to the memory within. Vector<>
    365         // internally doesn't copy the memory unless one of the copies is
    366         // modified.
    367         Vector<Region> visibleRegions;
    368         Vector<Region> surfaceDamageRegions;
    369     };
    370 
    371     sp<SurfaceFlinger>              mFlinger;
    372     framebuffer_device_t*           mFbDev;
    373     struct hwc_composer_device_1*   mHwc;
    374     // invariant: mLists[0] != NULL iff mHwc != NULL
    375     // mLists[i>0] can be NULL. that display is to be ignored
    376     struct hwc_display_contents_1*  mLists[MAX_HWC_DISPLAYS];
    377     DisplayData                     mDisplayData[MAX_HWC_DISPLAYS];
    378     // protect mDisplayData from races between prepare and dump
    379     mutable Mutex mDisplayLock;
    380     size_t                          mNumDisplays;
    381 
    382     cb_context*                     mCBContext;
    383     EventHandler&                   mEventHandler;
    384     size_t                          mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
    385     sp<VSyncThread>                 mVSyncThread;
    386     bool                            mDebugForceFakeVSync;
    387     BitSet32                        mAllocatedDisplayIDs;
    388 
    389     // protected by mLock
    390     mutable Mutex mLock;
    391     mutable nsecs_t mLastHwVSync[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
    392 
    393     // thread-safe
    394     mutable Mutex mEventControlLock;
    395 };
    396 
    397 // ---------------------------------------------------------------------------
    398 }; // namespace android
    399 
    400 #endif // ANDROID_SF_HWCOMPOSER_H
    401