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