Layer.h revision 1f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9
1/* 2 * Copyright (C) 2007 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_LAYER_H 18#define ANDROID_LAYER_H 19 20#include <stdint.h> 21#include <sys/types.h> 22 23#include <EGL/egl.h> 24#include <EGL/eglext.h> 25 26#include <utils/RefBase.h> 27#include <utils/String8.h> 28#include <utils/Timers.h> 29 30#include <gfx/FloatRect.h> 31 32#include <ui/FrameStats.h> 33#include <ui/GraphicBuffer.h> 34#include <ui/PixelFormat.h> 35#include <ui/Region.h> 36 37#include <gui/ISurfaceComposerClient.h> 38 39#include <private/gui/LayerState.h> 40 41#include <list> 42 43#include "FrameTracker.h" 44#include "Client.h" 45#include "LayerVector.h" 46#include "MonitoredProducer.h" 47#include "SurfaceFlinger.h" 48#include "SurfaceFlingerConsumer.h" 49#include "Transform.h" 50 51#include "DisplayHardware/HWComposer.h" 52#include "RenderEngine/Mesh.h" 53#include "RenderEngine/Texture.h" 54 55namespace android { 56 57// --------------------------------------------------------------------------- 58 59class Client; 60class Colorizer; 61class DisplayDevice; 62class GraphicBuffer; 63class SurfaceFlinger; 64 65// --------------------------------------------------------------------------- 66 67/* 68 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the 69 * Layer is first referenced. 70 * 71 * This also implements onFrameAvailable(), which notifies SurfaceFlinger 72 * that new data has arrived. 73 */ 74class Layer : public SurfaceFlingerConsumer::ContentsChangedListener { 75 static int32_t sSequence; 76 77public: 78 mutable bool contentDirty; 79 // regions below are in window-manager space 80 Region visibleRegion; 81 Region coveredRegion; 82 Region visibleNonTransparentRegion; 83 Region surfaceDamageRegion; 84 85 // Layer serial number. This gives layers an explicit ordering, so we 86 // have a stable sort order when their layer stack and Z-order are 87 // the same. 88 int32_t sequence; 89 90 enum { // flags for doTransaction() 91 eDontUpdateGeometryState = 0x00000001, 92 eVisibleRegion = 0x00000002, 93 }; 94 95 struct Geometry { 96 uint32_t w; 97 uint32_t h; 98 Transform transform; 99 100 inline bool operator ==(const Geometry& rhs) const { 101 return (w == rhs.w && h == rhs.h) && 102 (transform.tx() == rhs.transform.tx()) && 103 (transform.ty() == rhs.transform.ty()); 104 } 105 inline bool operator !=(const Geometry& rhs) const { 106 return !operator ==(rhs); 107 } 108 }; 109 110 struct State { 111 Geometry active; 112 Geometry requested; 113 int32_t z; 114 uint32_t layerStack; 115#ifdef USE_HWC2 116 float alpha; 117#else 118 uint8_t alpha; 119#endif 120 uint8_t flags; 121 uint8_t mask; 122 uint8_t reserved[2]; 123 int32_t sequence; // changes when visible regions can change 124 bool modified; 125 126 // Crop is expressed in layer space coordinate. 127 Rect crop; 128 Rect requestedCrop; 129 130 // finalCrop is expressed in display space coordinate. 131 Rect finalCrop; 132 133 // If set, defers this state update until the Layer identified by handle 134 // receives a frame with the given frameNumber 135 wp<IBinder> handle; 136 uint64_t frameNumber; 137 138 // the transparentRegion hint is a bit special, it's latched only 139 // when we receive a buffer -- this is because it's "content" 140 // dependent. 141 Region activeTransparentRegion; 142 Region requestedTransparentRegion; 143 android_dataspace dataSpace; 144 }; 145 146 // ----------------------------------------------------------------------- 147 148 Layer(SurfaceFlinger* flinger, const sp<Client>& client, 149 const String8& name, uint32_t w, uint32_t h, uint32_t flags); 150 151 virtual ~Layer(); 152 153 // the this layer's size and format 154 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 155 156 // modify current state 157 bool setPosition(float x, float y, bool immediate); 158 bool setLayer(int32_t z); 159 bool setSize(uint32_t w, uint32_t h); 160#ifdef USE_HWC2 161 bool setAlpha(float alpha); 162#else 163 bool setAlpha(uint8_t alpha); 164#endif 165 bool setMatrix(const layer_state_t::matrix22_t& matrix); 166 bool setTransparentRegionHint(const Region& transparent); 167 bool setFlags(uint8_t flags, uint8_t mask); 168 bool setCrop(const Rect& crop, bool immediate); 169 bool setFinalCrop(const Rect& crop); 170 bool setLayerStack(uint32_t layerStack); 171 bool setDataSpace(android_dataspace dataSpace); 172 uint32_t getLayerStack() const; 173 void deferTransactionUntil(const sp<IBinder>& handle, uint64_t frameNumber); 174 bool setOverrideScalingMode(int32_t overrideScalingMode); 175 176 // If we have received a new buffer this frame, we will pass its surface 177 // damage down to hardware composer. Otherwise, we must send a region with 178 // one empty rect. 179 void useSurfaceDamage(); 180 void useEmptyDamage(); 181 182 uint32_t getTransactionFlags(uint32_t flags); 183 uint32_t setTransactionFlags(uint32_t flags); 184 185 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, 186 bool useIdentityTransform) const; 187 Rect computeBounds(const Region& activeTransparentRegion) const; 188 Rect computeBounds() const; 189 190 int32_t getSequence() const { return sequence; } 191 192 // ----------------------------------------------------------------------- 193 // Virtuals 194 195 virtual const char* getTypeId() const { return "Layer"; } 196 197 /* 198 * isOpaque - true if this surface is opaque 199 * 200 * This takes into account the buffer format (i.e. whether or not the 201 * pixel format includes an alpha channel) and the "opaque" flag set 202 * on the layer. It does not examine the current plane alpha value. 203 */ 204 virtual bool isOpaque(const Layer::State& s) const; 205 206 /* 207 * isSecure - true if this surface is secure, that is if it prevents 208 * screenshots or VNC servers. 209 */ 210 virtual bool isSecure() const; 211 212 /* 213 * isProtected - true if the layer may contain protected content in the 214 * GRALLOC_USAGE_PROTECTED sense. 215 */ 216 virtual bool isProtected() const; 217 218 /* 219 * isVisible - true if this layer is visible, false otherwise 220 */ 221 virtual bool isVisible() const; 222 223 /* 224 * isHiddenByPolicy - true if this layer has been forced invisible. 225 * just because this is false, doesn't mean isVisible() is true. 226 * For example if this layer has no active buffer, it may not be hidden by 227 * policy, but it still can not be visible. 228 */ 229 virtual bool isHiddenByPolicy() const; 230 231 /* 232 * isFixedSize - true if content has a fixed size 233 */ 234 virtual bool isFixedSize() const; 235 236protected: 237 /* 238 * onDraw - draws the surface. 239 */ 240 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip, 241 bool useIdentityTransform) const; 242 243public: 244 // ----------------------------------------------------------------------- 245 246#ifdef USE_HWC2 247 void setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z); 248 void forceClientComposition(int32_t hwcId); 249 void setPerFrameData(const sp<const DisplayDevice>& displayDevice); 250 251 // callIntoHwc exists so we can update our local state and call 252 // acceptDisplayChanges without unnecessarily updating the device's state 253 void setCompositionType(int32_t hwcId, HWC2::Composition type, 254 bool callIntoHwc = true); 255 HWC2::Composition getCompositionType(int32_t hwcId) const; 256 257 void setClearClientTarget(int32_t hwcId, bool clear); 258 bool getClearClientTarget(int32_t hwcId) const; 259 260 void updateCursorPosition(const sp<const DisplayDevice>& hw); 261#else 262 void setGeometry(const sp<const DisplayDevice>& hw, 263 HWComposer::HWCLayerInterface& layer); 264 void setPerFrameData(const sp<const DisplayDevice>& hw, 265 HWComposer::HWCLayerInterface& layer); 266 void setAcquireFence(const sp<const DisplayDevice>& hw, 267 HWComposer::HWCLayerInterface& layer); 268 269 Rect getPosition(const sp<const DisplayDevice>& hw); 270#endif 271 272 /* 273 * called after page-flip 274 */ 275#ifdef USE_HWC2 276 void onLayerDisplayed(const sp<Fence>& releaseFence); 277#else 278 void onLayerDisplayed(const sp<const DisplayDevice>& hw, 279 HWComposer::HWCLayerInterface* layer); 280#endif 281 282 bool shouldPresentNow(const DispSync& dispSync) const; 283 284 /* 285 * called before composition. 286 * returns true if the layer has pending updates. 287 */ 288 bool onPreComposition(nsecs_t refreshStartTime); 289 290 /* 291 * called after composition. 292 * returns true if the layer latched a new buffer this frame. 293 */ 294 bool onPostComposition( 295 const std::shared_ptr<FenceTime>& glDoneFence, 296 const std::shared_ptr<FenceTime>& presentFence, 297 const std::shared_ptr<FenceTime>& retireFence); 298 299#ifdef USE_HWC2 300 // If a buffer was replaced this frame, release the former buffer 301 void releasePendingBuffer(nsecs_t dequeueReadyTime); 302#endif 303 304 /* 305 * draw - performs some global clipping optimizations 306 * and calls onDraw(). 307 */ 308 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const; 309 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const; 310 void draw(const sp<const DisplayDevice>& hw) const; 311 312 /* 313 * doTransaction - process the transaction. This is a good place to figure 314 * out which attributes of the surface have changed. 315 */ 316 uint32_t doTransaction(uint32_t transactionFlags); 317 318 /* 319 * setVisibleRegion - called to set the new visible region. This gives 320 * a chance to update the new visible region or record the fact it changed. 321 */ 322 void setVisibleRegion(const Region& visibleRegion); 323 324 /* 325 * setCoveredRegion - called when the covered region changes. The covered 326 * region corresponds to any area of the surface that is covered 327 * (transparently or not) by another surface. 328 */ 329 void setCoveredRegion(const Region& coveredRegion); 330 331 /* 332 * setVisibleNonTransparentRegion - called when the visible and 333 * non-transparent region changes. 334 */ 335 void setVisibleNonTransparentRegion(const Region& 336 visibleNonTransparentRegion); 337 338 /* 339 * latchBuffer - called each time the screen is redrawn and returns whether 340 * the visible regions need to be recomputed (this is a fairly heavy 341 * operation, so this should be set only if needed). Typically this is used 342 * to figure out if the content or size of a surface has changed. 343 */ 344 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime); 345 346 bool isPotentialCursor() const { return mPotentialCursor;} 347 348 /* 349 * called with the state lock when the surface is removed from the 350 * current list 351 */ 352 void onRemoved(); 353 354 355 // Updates the transform hint in our SurfaceFlingerConsumer to match 356 // the current orientation of the display device. 357 void updateTransformHint(const sp<const DisplayDevice>& hw) const; 358 359 /* 360 * returns the rectangle that crops the content of the layer and scales it 361 * to the layer's size. 362 */ 363 Rect getContentCrop() const; 364 365 /* 366 * Returns if a frame is queued. 367 */ 368 bool hasQueuedFrame() const { return mQueuedFrames > 0 || 369 mSidebandStreamChanged || mAutoRefresh; } 370 371#ifdef USE_HWC2 372 // ----------------------------------------------------------------------- 373 374 bool hasHwcLayer(int32_t hwcId) { 375 if (mHwcLayers.count(hwcId) == 0) { 376 return false; 377 } 378 if (mHwcLayers[hwcId].layer->isAbandoned()) { 379 ALOGI("Erasing abandoned layer %s on %d", mName.string(), hwcId); 380 mHwcLayers.erase(hwcId); 381 return false; 382 } 383 return true; 384 } 385 386 std::shared_ptr<HWC2::Layer> getHwcLayer(int32_t hwcId) { 387 if (mHwcLayers.count(hwcId) == 0) { 388 return nullptr; 389 } 390 return mHwcLayers[hwcId].layer; 391 } 392 393 void setHwcLayer(int32_t hwcId, std::shared_ptr<HWC2::Layer>&& layer) { 394 if (layer) { 395 mHwcLayers[hwcId].layer = layer; 396 } else { 397 mHwcLayers.erase(hwcId); 398 } 399 } 400 401#endif 402 // ----------------------------------------------------------------------- 403 404 void clearWithOpenGL(const sp<const DisplayDevice>& hw) const; 405 void setFiltering(bool filtering); 406 bool getFiltering() const; 407 408 // only for debugging 409 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; } 410 411 inline const State& getDrawingState() const { return mDrawingState; } 412 inline const State& getCurrentState() const { return mCurrentState; } 413 inline State& getCurrentState() { return mCurrentState; } 414 415 416 /* always call base class first */ 417 void dump(String8& result, Colorizer& colorizer) const; 418#ifdef USE_HWC2 419 static void miniDumpHeader(String8& result); 420 void miniDump(String8& result, int32_t hwcId) const; 421#endif 422 void dumpFrameStats(String8& result) const; 423 void dumpFrameEvents(String8& result); 424 void clearFrameStats(); 425 void logFrameStats(); 426 void getFrameStats(FrameStats* outStats) const; 427 428 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush); 429 430 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newEntry, 431 FrameEventHistoryDelta* outDelta); 432 433 bool getTransformToDisplayInverse() const; 434 435 Transform getTransform() const; 436 437 void traverseInReverseZOrder(const std::function<void(Layer*)>& exec); 438 void traverseInZOrder(const std::function<void(Layer*)>& exec); 439 440 void addChild(const sp<Layer>& layer); 441 // Returns index if removed, or negative value otherwise 442 // for symmetry with Vector::remove 443 ssize_t removeChild(const sp<Layer>& layer); 444 sp<Layer> getParent() const { return mParent.promote(); } 445 bool hasParent() const { return getParent() != nullptr; } 446 447 Rect computeScreenBounds(bool reduceTransparentRegion = true) const; 448 bool setChildLayer(const sp<Layer>& childLayer, int32_t z); 449 450 // Copy the current list of children to the drawing state. Called by 451 // SurfaceFlinger to complete a transaction. 452 void commitChildList(); 453 454 int32_t getZ() const; 455protected: 456 // constant 457 sp<SurfaceFlinger> mFlinger; 458 /* 459 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer) 460 * is called. 461 */ 462 class LayerCleaner { 463 sp<SurfaceFlinger> mFlinger; 464 wp<Layer> mLayer; 465 protected: 466 ~LayerCleaner() { 467 // destroy client resources 468 mFlinger->onLayerDestroyed(mLayer); 469 } 470 public: 471 LayerCleaner(const sp<SurfaceFlinger>& flinger, 472 const sp<Layer>& layer) 473 : mFlinger(flinger), mLayer(layer) { 474 } 475 }; 476 477 478 virtual void onFirstRef(); 479 480 481 482private: 483 friend class SurfaceInterceptor; 484 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener 485 virtual void onFrameAvailable(const BufferItem& item) override; 486 virtual void onFrameReplaced(const BufferItem& item) override; 487 virtual void onSidebandStreamChanged() override; 488 489 void commitTransaction(const State& stateToCommit); 490 491 // needsLinearFiltering - true if this surface's state requires filtering 492 bool needsFiltering(const sp<const DisplayDevice>& hw) const; 493 494 uint32_t getEffectiveUsage(uint32_t usage) const; 495 496 gfx::FloatRect computeCrop(const sp<const DisplayDevice>& hw) const; 497 // Compute the initial crop as specified by parent layers and the SurfaceControl 498 // for this layer. Does not include buffer crop from the IGraphicBufferProducer 499 // client, as that should not affect child clipping. Returns in screen space. 500 Rect computeInitialCrop(const sp<const DisplayDevice>& hw) const; 501 bool isCropped() const; 502 static bool getOpacityForFormat(uint32_t format); 503 504 // drawing 505 void clearWithOpenGL(const sp<const DisplayDevice>& hw, 506 float r, float g, float b, float alpha) const; 507 void drawWithOpenGL(const sp<const DisplayDevice>& hw, 508 bool useIdentityTransform) const; 509 510 // Temporary - Used only for LEGACY camera mode. 511 uint32_t getProducerStickyTransform() const; 512 513 // Loads the corresponding system property once per process 514 static bool latchUnsignaledBuffers(); 515 516 void setParent(const sp<Layer>& layer); 517 518 // ----------------------------------------------------------------------- 519 520 class SyncPoint 521 { 522 public: 523 explicit SyncPoint(uint64_t frameNumber) : mFrameNumber(frameNumber), 524 mFrameIsAvailable(false), mTransactionIsApplied(false) {} 525 526 uint64_t getFrameNumber() const { 527 return mFrameNumber; 528 } 529 530 bool frameIsAvailable() const { 531 return mFrameIsAvailable; 532 } 533 534 void setFrameAvailable() { 535 mFrameIsAvailable = true; 536 } 537 538 bool transactionIsApplied() const { 539 return mTransactionIsApplied; 540 } 541 542 void setTransactionApplied() { 543 mTransactionIsApplied = true; 544 } 545 546 private: 547 const uint64_t mFrameNumber; 548 std::atomic<bool> mFrameIsAvailable; 549 std::atomic<bool> mTransactionIsApplied; 550 }; 551 552 // SyncPoints which will be signaled when the correct frame is at the head 553 // of the queue and dropped after the frame has been latched. Protected by 554 // mLocalSyncPointMutex. 555 Mutex mLocalSyncPointMutex; 556 std::list<std::shared_ptr<SyncPoint>> mLocalSyncPoints; 557 558 // SyncPoints which will be signaled and then dropped when the transaction 559 // is applied 560 std::list<std::shared_ptr<SyncPoint>> mRemoteSyncPoints; 561 562 uint64_t getHeadFrameNumber() const; 563 bool headFenceHasSignaled() const; 564 565 // Returns false if the relevant frame has already been latched 566 bool addSyncPoint(const std::shared_ptr<SyncPoint>& point); 567 568 void pushPendingState(); 569 void popPendingState(State* stateToCommit); 570 bool applyPendingStates(State* stateToCommit); 571 572 void clearSyncPoints(); 573 574 // Returns mCurrentScaling mode (originating from the 575 // Client) or mOverrideScalingMode mode (originating from 576 // the Surface Controller) if set. 577 uint32_t getEffectiveScalingMode() const; 578public: 579 /* 580 * The layer handle is just a BBinder object passed to the client 581 * (remote process) -- we don't keep any reference on our side such that 582 * the dtor is called when the remote side let go of its reference. 583 * 584 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for 585 * this layer when the handle is destroyed. 586 */ 587 class Handle : public BBinder, public LayerCleaner { 588 public: 589 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer) 590 : LayerCleaner(flinger, layer), owner(layer) {} 591 592 wp<Layer> owner; 593 }; 594 595 sp<IBinder> getHandle(); 596 sp<IGraphicBufferProducer> getProducer() const; 597 const String8& getName() const; 598 void notifyAvailableFrames(); 599private: 600 601 // ----------------------------------------------------------------------- 602 603 // Check all of the local sync points to ensure that all transactions 604 // which need to have been applied prior to the frame which is about to 605 // be latched have signaled 606 bool allTransactionsSignaled(); 607 608 // constants 609 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer; 610 sp<IGraphicBufferProducer> mProducer; 611 uint32_t mTextureName; // from GLES 612 bool mPremultipliedAlpha; 613 String8 mName; 614 PixelFormat mFormat; 615 616 // these are protected by an external lock 617 State mCurrentState; 618 State mDrawingState; 619 volatile int32_t mTransactionFlags; 620 621 // Accessed from main thread and binder threads 622 Mutex mPendingStateMutex; 623 Vector<State> mPendingStates; 624 625 // thread-safe 626 volatile int32_t mQueuedFrames; 627 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean 628 629 // Timestamp history for UIAutomation. Thread safe. 630 FrameTracker mFrameTracker; 631 632 // Timestamp history for the consumer to query. 633 // Accessed by both consumer and producer on main and binder threads. 634 Mutex mFrameEventHistoryMutex; 635 ConsumerFrameEventHistory mFrameEventHistory; 636 FenceTimeline mAcquireTimeline; 637 FenceTimeline mReleaseTimeline; 638 639 // main thread 640 sp<GraphicBuffer> mActiveBuffer; 641 sp<NativeHandle> mSidebandStream; 642 Rect mCurrentCrop; 643 uint32_t mCurrentTransform; 644 uint32_t mCurrentScalingMode; 645 // We encode unset as -1. 646 int32_t mOverrideScalingMode; 647 bool mCurrentOpacity; 648 bool mBufferLatched = false; // TODO: Use mActiveBuffer? 649 std::atomic<uint64_t> mCurrentFrameNumber; 650 uint64_t mPreviousFrameNumber; // Only accessed on the main thread. 651 bool mRefreshPending; 652 bool mFrameLatencyNeeded; 653 // Whether filtering is forced on or not 654 bool mFiltering; 655 // Whether filtering is needed b/c of the drawingstate 656 bool mNeedsFiltering; 657 // The mesh used to draw the layer in GLES composition mode 658 mutable Mesh mMesh; 659 // The texture used to draw the layer in GLES composition mode 660 mutable Texture mTexture; 661 662#ifdef USE_HWC2 663 // HWC items, accessed from the main thread 664 struct HWCInfo { 665 HWCInfo() 666 : layer(), 667 forceClientComposition(false), 668 compositionType(HWC2::Composition::Invalid), 669 clearClientTarget(false) {} 670 671 std::shared_ptr<HWC2::Layer> layer; 672 bool forceClientComposition; 673 HWC2::Composition compositionType; 674 bool clearClientTarget; 675 Rect displayFrame; 676 gfx::FloatRect sourceCrop; 677 }; 678 std::unordered_map<int32_t, HWCInfo> mHwcLayers; 679#else 680 bool mIsGlesComposition; 681#endif 682 683 // page-flip thread (currently main thread) 684 bool mProtectedByApp; // application requires protected path to external sink 685 686 // protected by mLock 687 mutable Mutex mLock; 688 // Set to true once we've returned this surface's handle 689 mutable bool mHasSurface; 690 const wp<Client> mClientRef; 691 692 // This layer can be a cursor on some displays. 693 bool mPotentialCursor; 694 695 // Local copy of the queued contents of the incoming BufferQueue 696 mutable Mutex mQueueItemLock; 697 Condition mQueueItemCondition; 698 Vector<BufferItem> mQueueItems; 699 std::atomic<uint64_t> mLastFrameNumberReceived; 700 bool mUpdateTexImageFailed; // This is only accessed on the main thread. 701 702 bool mAutoRefresh; 703 bool mFreezePositionUpdates; 704 705 // Child list about to be committed/used for editing. 706 LayerVector mCurrentChildren; 707 // Child list used for rendering. 708 LayerVector mDrawingChildren; 709 710 wp<Layer> mParent; 711}; 712 713// --------------------------------------------------------------------------- 714 715}; // namespace android 716 717#endif // ANDROID_LAYER_H 718