Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 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_H |
| 18 | #define ANDROID_SF_HWCOMPOSER_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 23 | #include <hardware/hwcomposer_defs.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 24 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 25 | #include <utils/Condition.h> |
| 26 | #include <utils/Mutex.h> |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 27 | #include <utils/StrongPointer.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 28 | #include <utils/Thread.h> |
| 29 | #include <utils/Timers.h> |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 30 | #include <utils/Vector.h> |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 32 | extern "C" int clock_nanosleep(clockid_t clock_id, int flags, |
| 33 | const struct timespec *request, |
| 34 | struct timespec *remain); |
| 35 | |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 36 | struct hwc_composer_device_1; |
Jesse Hall | b685c54 | 2012-07-31 14:32:56 -0700 | [diff] [blame] | 37 | struct hwc_display_contents_1; |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 38 | struct hwc_procs; |
| 39 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | // --------------------------------------------------------------------------- |
| 42 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 43 | class GraphicBuffer; |
| 44 | class LayerBase; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 45 | class Region; |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 46 | class String8; |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 47 | class SurfaceFlinger; |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 48 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 49 | class HWComposer |
| 50 | { |
| 51 | public: |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 52 | class EventHandler { |
| 53 | friend class HWComposer; |
| 54 | virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0; |
| 55 | protected: |
| 56 | virtual ~EventHandler() {} |
| 57 | }; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 58 | |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 59 | enum { |
| 60 | MAIN = 0, |
| 61 | HDMI = 1, |
| 62 | MAX_DISPLAYS |
| 63 | }; |
| 64 | |
Mathias Agopian | 888c822 | 2012-08-04 21:10:38 -0700 | [diff] [blame] | 65 | HWComposer(const sp<SurfaceFlinger>& flinger, EventHandler& handler); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 66 | ~HWComposer(); |
| 67 | |
| 68 | status_t initCheck() const; |
| 69 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 70 | // Asks the HAL what it can do |
| 71 | status_t prepare() const; |
| 72 | |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 73 | // disable hwc until next createWorkList |
| 74 | status_t disable(); |
| 75 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 76 | // commits the list |
Jesse Hall | 34a09ba | 2012-07-29 22:35:34 -0700 | [diff] [blame] | 77 | status_t commit(void* fbDisplay, void* fbSurface) const; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 78 | |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 79 | // release hardware resources and blank screen |
Antti Hatala | f5f2712 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 80 | status_t release() const; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 81 | |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 82 | // acquire hardware resources and unblank screen |
| 83 | status_t acquire() const; |
| 84 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 85 | // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED. |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 86 | status_t createWorkList(int32_t id, size_t numLayers); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 87 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 88 | // get number of layers of the given type as updated in prepare(). |
| 89 | // type is HWC_OVERLAY or HWC_FRAMEBUFFER |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 90 | size_t getLayerCount(int32_t id, int type) const; |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 91 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 92 | // needed forward declarations |
| 93 | class LayerListIterator; |
| 94 | |
| 95 | /* |
| 96 | * Interface to hardware composer's layers functionality. |
| 97 | * This abstracts the HAL interface to layers which can evolve in |
| 98 | * incompatible ways from one release to another. |
| 99 | * The idea is that we could extend this interface as we add |
| 100 | * features to h/w composer. |
| 101 | */ |
| 102 | class HWCLayerInterface { |
| 103 | protected: |
| 104 | virtual ~HWCLayerInterface() { } |
| 105 | public: |
| 106 | virtual int32_t getCompositionType() const = 0; |
| 107 | virtual uint32_t getHints() const = 0; |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 108 | virtual int getAndResetReleaseFenceFd() = 0; |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 109 | virtual void setDefaultState() = 0; |
| 110 | virtual void setSkip(bool skip) = 0; |
| 111 | virtual void setBlending(uint32_t blending) = 0; |
| 112 | virtual void setTransform(uint32_t transform) = 0; |
| 113 | virtual void setFrame(const Rect& frame) = 0; |
| 114 | virtual void setCrop(const Rect& crop) = 0; |
| 115 | virtual void setVisibleRegionScreen(const Region& reg) = 0; |
| 116 | virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0; |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 117 | virtual void setAcquireFenceFd(int fenceFd) = 0; |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * Interface used to implement an iterator to a list |
| 122 | * of HWCLayer. |
| 123 | */ |
| 124 | class HWCLayer : public HWCLayerInterface { |
| 125 | friend class LayerListIterator; |
| 126 | // select the layer at the given index |
| 127 | virtual status_t setLayer(size_t index) = 0; |
| 128 | virtual HWCLayer* dup() = 0; |
| 129 | static HWCLayer* copy(HWCLayer *rhs) { |
| 130 | return rhs ? rhs->dup() : NULL; |
| 131 | } |
| 132 | protected: |
| 133 | virtual ~HWCLayer() { } |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * Iterator through a HWCLayer list. |
| 138 | * This behaves more or less like a forward iterator. |
| 139 | */ |
| 140 | class LayerListIterator { |
| 141 | friend struct HWComposer; |
| 142 | HWCLayer* const mLayerList; |
| 143 | size_t mIndex; |
| 144 | |
| 145 | LayerListIterator() : mLayerList(NULL), mIndex(0) { } |
| 146 | |
| 147 | LayerListIterator(HWCLayer* layer, size_t index) |
| 148 | : mLayerList(layer), mIndex(index) { } |
| 149 | |
| 150 | // we don't allow assignment, because we don't need it for now |
| 151 | LayerListIterator& operator = (const LayerListIterator& rhs); |
| 152 | |
| 153 | public: |
| 154 | // copy operators |
| 155 | LayerListIterator(const LayerListIterator& rhs) |
| 156 | : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) { |
| 157 | } |
| 158 | |
| 159 | ~LayerListIterator() { delete mLayerList; } |
| 160 | |
| 161 | // pre-increment |
| 162 | LayerListIterator& operator++() { |
| 163 | mLayerList->setLayer(++mIndex); |
| 164 | return *this; |
| 165 | } |
| 166 | |
| 167 | // dereference |
| 168 | HWCLayerInterface& operator * () { return *mLayerList; } |
| 169 | HWCLayerInterface* operator -> () { return mLayerList; } |
| 170 | |
| 171 | // comparison |
| 172 | bool operator == (const LayerListIterator& rhs) const { |
| 173 | return mIndex == rhs.mIndex; |
| 174 | } |
| 175 | bool operator != (const LayerListIterator& rhs) const { |
| 176 | return !operator==(rhs); |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | // Returns an iterator to the beginning of the layer list |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 181 | LayerListIterator begin(int32_t id); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 182 | |
| 183 | // Returns an iterator to the end of the layer list |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 184 | LayerListIterator end(int32_t id); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 185 | |
| 186 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 187 | // Events handling --------------------------------------------------------- |
| 188 | |
| 189 | enum { |
| 190 | EVENT_VSYNC = HWC_EVENT_VSYNC |
| 191 | }; |
| 192 | |
Mathias Agopian | 03e4072 | 2012-04-26 16:11:59 -0700 | [diff] [blame] | 193 | void eventControl(int event, int enabled); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 194 | |
Mathias Agopian | 888c822 | 2012-08-04 21:10:38 -0700 | [diff] [blame] | 195 | nsecs_t getRefreshPeriod() const; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 196 | nsecs_t getRefreshTimestamp() const; |
| 197 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 198 | // this class is only used to fake the VSync event on systems that don't |
| 199 | // have it. |
| 200 | class VSyncThread : public Thread { |
| 201 | HWComposer& mHwc; |
| 202 | mutable Mutex mLock; |
| 203 | Condition mCondition; |
| 204 | bool mEnabled; |
| 205 | mutable nsecs_t mNextFakeVSync; |
| 206 | nsecs_t mRefreshPeriod; |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 207 | virtual void onFirstRef(); |
| 208 | virtual bool threadLoop(); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 209 | public: |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 210 | VSyncThread(HWComposer& hwc); |
| 211 | void setEnabled(bool enabled); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | friend class VSyncThread; |
| 215 | |
| 216 | // for debugging ---------------------------------------------------------- |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 217 | void dump(String8& out, char* scratch, size_t SIZE, |
| 218 | const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const; |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 219 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 220 | private: |
Jesse Hall | b685c54 | 2012-07-31 14:32:56 -0700 | [diff] [blame] | 221 | |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 222 | LayerListIterator getLayerIterator(int32_t id, size_t index); |
| 223 | size_t getNumLayers(int32_t id) const; |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 224 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 225 | struct cb_context; |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 226 | |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 227 | static void hook_invalidate(struct hwc_procs* procs); |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 228 | static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp); |
| 229 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 230 | inline void invalidate(); |
| 231 | inline void vsync(int dpy, int64_t timestamp); |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 232 | |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 233 | |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 234 | sp<SurfaceFlinger> mFlinger; |
| 235 | hw_module_t const* mModule; |
| 236 | struct hwc_composer_device_1* mHwc; |
Jesse Hall | b685c54 | 2012-07-31 14:32:56 -0700 | [diff] [blame] | 237 | // invariant: mLists[0] != NULL iff mHwc != NULL |
| 238 | // TODO: decide whether mLists[i>0] should be non-NULL when display i is |
| 239 | // not attached/enabled. |
| 240 | struct hwc_display_contents_1* mLists[MAX_DISPLAYS]; |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 241 | |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 242 | size_t mCapacity; |
| 243 | mutable size_t mNumOVLayers; |
| 244 | mutable size_t mNumFBLayers; |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 245 | cb_context* mCBContext; |
| 246 | EventHandler& mEventHandler; |
| 247 | nsecs_t mRefreshPeriod; |
| 248 | size_t mVSyncCount; |
| 249 | sp<VSyncThread> mVSyncThread; |
| 250 | bool mDebugForceFakeVSync; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 251 | |
| 252 | // protected by mLock |
| 253 | mutable Mutex mLock; |
| 254 | mutable nsecs_t mLastHwVSync; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 257 | // --------------------------------------------------------------------------- |
| 258 | }; // namespace android |
| 259 | |
| 260 | #endif // ANDROID_SF_HWCOMPOSER_H |