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