HWComposer.h revision dc5b485f74edf2d2f31c62054eb6c180421a3ade
1e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar/* 2e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * Copyright (C) 2010 The Android Open Source Project 3e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * 4e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 5e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * you may not use this file except in compliance with the License. 6e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * You may obtain a copy of the License at 7e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * 8e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 9e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * 10e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * Unless required by applicable law or agreed to in writing, software 11e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 12e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * See the License for the specific language governing permissions and 14e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * limitations under the License. 15e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar */ 16e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 17e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#ifndef ANDROID_SF_HWCOMPOSER_H 18e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#define ANDROID_SF_HWCOMPOSER_H 19e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 20e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#include <stdint.h> 21e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#include <sys/types.h> 22e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 23e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#include <EGL/egl.h> 24e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 25e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#include <hardware/hwcomposer_defs.h> 26e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 27e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#include <utils/StrongPointer.h> 28e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar#include <utils/Vector.h> 29e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 30e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarextern "C" int clock_nanosleep(clockid_t clock_id, int flags, 31e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar const struct timespec *request, 32e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar struct timespec *remain); 33e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 34e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarstruct hwc_composer_device_1; 35e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarstruct hwc_layer_list_1; 36e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarstruct hwc_procs; 37e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 38e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarnamespace android { 39e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar// --------------------------------------------------------------------------- 40e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 41e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarclass String8; 42e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarclass SurfaceFlinger; 43e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarclass LayerBase; 44e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarclass GraphicBuffer; 45e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 46e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarclass HWComposer 47e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar{ 48e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarpublic: 49e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar class EventHandler { 50e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar friend class HWComposer; 51e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0; 52e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar protected: 53e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar virtual ~EventHandler() {} 54e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar }; 55e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar 56e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar HWComposer(const sp<SurfaceFlinger>& flinger, 57e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar EventHandler& handler, nsecs_t refreshPeriod); 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 65 // Asks the HAL what it can do 66 status_t prepare() const; 67 68 // disable hwc until next createWorkList 69 status_t disable(); 70 71 // commits the list 72 status_t commit() const; 73 74 // release hardware resources 75 status_t release() const; 76 77 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED. 78 status_t createWorkList(size_t numLayers); 79 80 // get the layer array created by createWorkList() 81 size_t getNumLayers() const; 82 83 // get number of layers of the given type as updated in prepare(). 84 // type is HWC_OVERLAY or HWC_FRAMEBUFFER 85 size_t getLayerCount(int type) const; 86 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; 103 virtual int getAndResetReleaseFenceFd() = 0; 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 virtual void setAcquireFenceFd(int fenceFd) = 0; 113 }; 114 115 /* 116 * Interface used to implement an iterator to a list 117 * of HWCLayer. 118 */ 119 class HWCLayer : public HWCLayerInterface { 120 friend class LayerListIterator; 121 // select the layer at the given index 122 virtual status_t setLayer(size_t index) = 0; 123 virtual HWCLayer* dup() = 0; 124 static HWCLayer* copy(HWCLayer *rhs) { 125 return rhs ? rhs->dup() : NULL; 126 } 127 protected: 128 virtual ~HWCLayer() { } 129 }; 130 131 /* 132 * Iterator through a HWCLayer list. 133 * This behaves more or less like a forward iterator. 134 */ 135 class LayerListIterator { 136 friend struct HWComposer; 137 HWCLayer* const mLayerList; 138 size_t mIndex; 139 140 LayerListIterator() : mLayerList(NULL), mIndex(0) { } 141 142 LayerListIterator(HWCLayer* layer, size_t index) 143 : mLayerList(layer), mIndex(index) { } 144 145 // we don't allow assignment, because we don't need it for now 146 LayerListIterator& operator = (const LayerListIterator& rhs); 147 148 public: 149 // copy operators 150 LayerListIterator(const LayerListIterator& rhs) 151 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) { 152 } 153 154 ~LayerListIterator() { delete mLayerList; } 155 156 // pre-increment 157 LayerListIterator& operator++() { 158 mLayerList->setLayer(++mIndex); 159 return *this; 160 } 161 162 // dereference 163 HWCLayerInterface& operator * () { return *mLayerList; } 164 HWCLayerInterface* operator -> () { return mLayerList; } 165 166 // comparison 167 bool operator == (const LayerListIterator& rhs) const { 168 return mIndex == rhs.mIndex; 169 } 170 bool operator != (const LayerListIterator& rhs) const { 171 return !operator==(rhs); 172 } 173 }; 174 175 // Returns an iterator to the beginning of the layer list 176 LayerListIterator begin(); 177 178 // Returns an iterator to the end of the layer list 179 LayerListIterator end(); 180 181 182 // Events handling --------------------------------------------------------- 183 184 enum { 185 EVENT_VSYNC = HWC_EVENT_VSYNC 186 }; 187 188 void eventControl(int event, int enabled); 189 190 // this class is only used to fake the VSync event on systems that don't 191 // have it. 192 class VSyncThread : public Thread { 193 HWComposer& mHwc; 194 mutable Mutex mLock; 195 Condition mCondition; 196 bool mEnabled; 197 mutable nsecs_t mNextFakeVSync; 198 nsecs_t mRefreshPeriod; 199 virtual void onFirstRef(); 200 virtual bool threadLoop(); 201 public: 202 VSyncThread(HWComposer& hwc); 203 void setEnabled(bool enabled); 204 }; 205 206 friend class VSyncThread; 207 208 // for debugging ---------------------------------------------------------- 209 void dump(String8& out, char* scratch, size_t SIZE, 210 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const; 211 212private: 213 LayerListIterator getLayerIterator(size_t index); 214 215 struct cb_context; 216 217 static void hook_invalidate(struct hwc_procs* procs); 218 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp); 219 220 inline void invalidate(); 221 inline void vsync(int dpy, int64_t timestamp); 222 223 sp<SurfaceFlinger> mFlinger; 224 hw_module_t const* mModule; 225 struct hwc_composer_device_1* mHwc; 226 struct hwc_layer_list_1* mList; 227 size_t mCapacity; 228 mutable size_t mNumOVLayers; 229 mutable size_t mNumFBLayers; 230 EGLDisplay mDpy; 231 EGLSurface mSur; 232 cb_context* mCBContext; 233 EventHandler& mEventHandler; 234 nsecs_t mRefreshPeriod; 235 size_t mVSyncCount; 236 sp<VSyncThread> mVSyncThread; 237 bool mDebugForceFakeVSync; 238}; 239 240// --------------------------------------------------------------------------- 241}; // namespace android 242 243#endif // ANDROID_SF_HWCOMPOSER_H 244