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