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