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