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