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