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