HWComposer.h revision cb55857bbde34a06c19dde3db5064d1717a0173e
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 <hardware/hwcomposer_defs.h> 24 25#include <utils/Condition.h> 26#include <utils/Mutex.h> 27#include <utils/StrongPointer.h> 28#include <utils/Thread.h> 29#include <utils/Timers.h> 30#include <utils/Vector.h> 31#include <utils/BitSet.h> 32 33extern "C" int clock_nanosleep(clockid_t clock_id, int flags, 34 const struct timespec *request, 35 struct timespec *remain); 36 37struct hwc_composer_device_1; 38struct hwc_display_contents_1; 39struct hwc_layer_1; 40struct hwc_procs; 41struct framebuffer_device_t; 42 43namespace android { 44// --------------------------------------------------------------------------- 45 46class GraphicBuffer; 47class Fence; 48class LayerBase; 49class Region; 50class String8; 51class SurfaceFlinger; 52 53class HWComposer 54{ 55public: 56 class EventHandler { 57 friend class HWComposer; 58 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0; 59 virtual void onHotplugReceived(int disp, bool connected) = 0; 60 protected: 61 virtual ~EventHandler() {} 62 }; 63 64 enum { 65 MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1 66 }; 67 68 HWComposer( 69 const sp<SurfaceFlinger>& flinger, 70 EventHandler& handler); 71 72 ~HWComposer(); 73 74 status_t initCheck() const; 75 76 // returns a display ID starting at MAX_DISPLAYS, this ID 77 // is to be used with createWorkList (and all other 78 // methods requiring an ID below). 79 // IDs below MAX_DISPLAY are pre-defined and therefore are always valid. 80 // returns a negative error code if an ID cannot be allocated 81 int32_t allocateDisplayId(); 82 83 // recycles the given ID and frees the associated worklist. 84 // IDs below MAX_DISPLAYS are not recycled 85 status_t freeDisplayId(int32_t id); 86 87 88 // Asks the HAL what it can do 89 status_t prepare(); 90 91 // commits the list 92 status_t commit(); 93 94 // release hardware resources and blank screen 95 status_t release(int disp); 96 97 // acquire hardware resources and unblank screen 98 status_t acquire(int disp); 99 100 // reset state when an external, non-virtual display is disconnected 101 void disconnectDisplay(int disp); 102 103 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED. 104 status_t createWorkList(int32_t id, size_t numLayers); 105 106 bool supportsFramebufferTarget() const; 107 108 // does this display have layers handled by HWC 109 bool hasHwcComposition(int32_t id) const; 110 111 // does this display have layers handled by GLES 112 bool hasGlesComposition(int32_t id) const; 113 114 // get the releaseFence file descriptor for the given display 115 // the release fence is only valid after commit() 116 int getAndResetReleaseFenceFd(int32_t id); 117 118 // needed forward declarations 119 class LayerListIterator; 120 121 // return the visual id to be used to find a suitable EGLConfig for 122 // *ALL* displays. 123 int getVisualID() const; 124 125 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface). 126 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf); 127 int fbCompositionComplete(); 128 void fbDump(String8& result); 129 130 /* 131 * Interface to hardware composer's layers functionality. 132 * This abstracts the HAL interface to layers which can evolve in 133 * incompatible ways from one release to another. 134 * The idea is that we could extend this interface as we add 135 * features to h/w composer. 136 */ 137 class HWCLayerInterface { 138 protected: 139 virtual ~HWCLayerInterface() { } 140 public: 141 virtual int32_t getCompositionType() const = 0; 142 virtual uint32_t getHints() const = 0; 143 virtual int getAndResetReleaseFenceFd() = 0; 144 virtual void setDefaultState() = 0; 145 virtual void setSkip(bool skip) = 0; 146 virtual void setBlending(uint32_t blending) = 0; 147 virtual void setTransform(uint32_t transform) = 0; 148 virtual void setFrame(const Rect& frame) = 0; 149 virtual void setCrop(const Rect& crop) = 0; 150 virtual void setVisibleRegionScreen(const Region& reg) = 0; 151 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0; 152 virtual void setAcquireFenceFd(int fenceFd) = 0; 153 virtual void onDisplayed() = 0; 154 }; 155 156 /* 157 * Interface used to implement an iterator to a list 158 * of HWCLayer. 159 */ 160 class HWCLayer : public HWCLayerInterface { 161 friend class LayerListIterator; 162 // select the layer at the given index 163 virtual status_t setLayer(size_t index) = 0; 164 virtual HWCLayer* dup() = 0; 165 static HWCLayer* copy(HWCLayer *rhs) { 166 return rhs ? rhs->dup() : NULL; 167 } 168 protected: 169 virtual ~HWCLayer() { } 170 }; 171 172 /* 173 * Iterator through a HWCLayer list. 174 * This behaves more or less like a forward iterator. 175 */ 176 class LayerListIterator { 177 friend struct HWComposer; 178 HWCLayer* const mLayerList; 179 size_t mIndex; 180 181 LayerListIterator() : mLayerList(NULL), mIndex(0) { } 182 183 LayerListIterator(HWCLayer* layer, size_t index) 184 : mLayerList(layer), mIndex(index) { } 185 186 // we don't allow assignment, because we don't need it for now 187 LayerListIterator& operator = (const LayerListIterator& rhs); 188 189 public: 190 // copy operators 191 LayerListIterator(const LayerListIterator& rhs) 192 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) { 193 } 194 195 ~LayerListIterator() { delete mLayerList; } 196 197 // pre-increment 198 LayerListIterator& operator++() { 199 mLayerList->setLayer(++mIndex); 200 return *this; 201 } 202 203 // dereference 204 HWCLayerInterface& operator * () { return *mLayerList; } 205 HWCLayerInterface* operator -> () { return mLayerList; } 206 207 // comparison 208 bool operator == (const LayerListIterator& rhs) const { 209 return mIndex == rhs.mIndex; 210 } 211 bool operator != (const LayerListIterator& rhs) const { 212 return !operator==(rhs); 213 } 214 }; 215 216 // Returns an iterator to the beginning of the layer list 217 LayerListIterator begin(int32_t id); 218 219 // Returns an iterator to the end of the layer list 220 LayerListIterator end(int32_t id); 221 222 223 // Events handling --------------------------------------------------------- 224 225 enum { 226 EVENT_VSYNC = HWC_EVENT_VSYNC 227 }; 228 229 void eventControl(int disp, int event, int enabled); 230 231 // Query display parameters. Pass in a display index (e.g. 232 // HWC_DISPLAY_PRIMARY). 233 nsecs_t getRefreshPeriod(int disp) const; 234 nsecs_t getRefreshTimestamp(int disp) const; 235 uint32_t getWidth(int disp) const; 236 uint32_t getHeight(int disp) const; 237 uint32_t getFormat(int disp) const; 238 float getDpiX(int disp) const; 239 float getDpiY(int disp) const; 240 bool isConnected(int disp) const; 241 242 // this class is only used to fake the VSync event on systems that don't 243 // have it. 244 class VSyncThread : public Thread { 245 HWComposer& mHwc; 246 mutable Mutex mLock; 247 Condition mCondition; 248 bool mEnabled; 249 mutable nsecs_t mNextFakeVSync; 250 nsecs_t mRefreshPeriod; 251 virtual void onFirstRef(); 252 virtual bool threadLoop(); 253 public: 254 VSyncThread(HWComposer& hwc); 255 void setEnabled(bool enabled); 256 }; 257 258 friend class VSyncThread; 259 260 // for debugging ---------------------------------------------------------- 261 void dump(String8& out, char* scratch, size_t SIZE) const; 262 263private: 264 void loadHwcModule(); 265 void loadFbHalModule(); 266 267 LayerListIterator getLayerIterator(int32_t id, size_t index); 268 269 struct cb_context; 270 271 static void hook_invalidate(const struct hwc_procs* procs); 272 static void hook_vsync(const struct hwc_procs* procs, int disp, 273 int64_t timestamp); 274 static void hook_hotplug(const struct hwc_procs* procs, int disp, 275 int connected); 276 277 inline void invalidate(); 278 inline void vsync(int disp, int64_t timestamp); 279 inline void hotplug(int disp, int connected); 280 281 status_t queryDisplayProperties(int disp); 282 283 status_t setFramebufferTarget(int32_t id, 284 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf); 285 286 287 struct DisplayData { 288 DisplayData() : xdpi(0), ydpi(0), refresh(0), 289 connected(false), hasFbComp(false), hasOvComp(false), 290 capacity(0), list(NULL), 291 framebufferTarget(NULL), fbTargetHandle(NULL), events(0) { } 292 ~DisplayData() { 293 free(list); 294 } 295 uint32_t width; 296 uint32_t height; 297 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1 298 float xdpi; 299 float ydpi; 300 nsecs_t refresh; 301 bool connected; 302 bool hasFbComp; 303 bool hasOvComp; 304 size_t capacity; 305 hwc_display_contents_1* list; 306 hwc_layer_1* framebufferTarget; 307 buffer_handle_t fbTargetHandle; 308 // protected by mEventControlLock 309 int32_t events; 310 }; 311 312 sp<SurfaceFlinger> mFlinger; 313 framebuffer_device_t* mFbDev; 314 struct hwc_composer_device_1* mHwc; 315 // invariant: mLists[0] != NULL iff mHwc != NULL 316 // mLists[i>0] can be NULL. that display is to be ignored 317 struct hwc_display_contents_1* mLists[MAX_DISPLAYS]; 318 DisplayData mDisplayData[MAX_DISPLAYS]; 319 size_t mNumDisplays; 320 321 cb_context* mCBContext; 322 EventHandler& mEventHandler; 323 size_t mVSyncCount; 324 sp<VSyncThread> mVSyncThread; 325 bool mDebugForceFakeVSync; 326 BitSet32 mAllocatedDisplayIDs; 327 328 // protected by mLock 329 mutable Mutex mLock; 330 mutable nsecs_t mLastHwVSync; 331 332 // thread-safe 333 mutable Mutex mEventControlLock; 334}; 335 336// --------------------------------------------------------------------------- 337}; // namespace android 338 339#endif // ANDROID_SF_HWCOMPOSER_H 340