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