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