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