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 "FrameTracker.h" 40 #include "Client.h" 41 #include "MonitoredProducer.h" 42 #include "SurfaceFlinger.h" 43 #include "SurfaceFlingerConsumer.h" 44 #include "Transform.h" 45 46 #include "DisplayHardware/HWComposer.h" 47 #include "DisplayHardware/FloatRect.h" 48 #include "RenderEngine/Mesh.h" 49 #include "RenderEngine/Texture.h" 50 51 namespace android { 52 53 // --------------------------------------------------------------------------- 54 55 class Client; 56 class Colorizer; 57 class DisplayDevice; 58 class GraphicBuffer; 59 class SurfaceFlinger; 60 61 // --------------------------------------------------------------------------- 62 63 /* 64 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the 65 * Layer is first referenced. 66 * 67 * This also implements onFrameAvailable(), which notifies SurfaceFlinger 68 * that new data has arrived. 69 */ 70 class Layer : public SurfaceFlingerConsumer::ContentsChangedListener { 71 static int32_t sSequence; 72 73 public: 74 mutable bool contentDirty; 75 // regions below are in window-manager space 76 Region visibleRegion; 77 Region coveredRegion; 78 Region visibleNonTransparentRegion; 79 Region surfaceDamageRegion; 80 81 // Layer serial number. This gives layers an explicit ordering, so we 82 // have a stable sort order when their layer stack and Z-order are 83 // the same. 84 int32_t sequence; 85 86 enum { // flags for doTransaction() 87 eDontUpdateGeometryState = 0x00000001, 88 eVisibleRegion = 0x00000002, 89 }; 90 91 struct Geometry { 92 uint32_t w; 93 uint32_t h; 94 Rect crop; 95 inline bool operator ==(const Geometry& rhs) const { 96 return (w == rhs.w && h == rhs.h && crop == rhs.crop); 97 } 98 inline bool operator !=(const Geometry& rhs) const { 99 return !operator ==(rhs); 100 } 101 }; 102 103 struct State { 104 Geometry active; 105 Geometry requested; 106 uint32_t z; 107 uint32_t layerStack; 108 uint8_t alpha; 109 uint8_t flags; 110 uint8_t reserved[2]; 111 int32_t sequence; // changes when visible regions can change 112 Transform transform; 113 // the transparentRegion hint is a bit special, it's latched only 114 // when we receive a buffer -- this is because it's "content" 115 // dependent. 116 Region activeTransparentRegion; 117 Region requestedTransparentRegion; 118 }; 119 120 // ----------------------------------------------------------------------- 121 122 Layer(SurfaceFlinger* flinger, const sp<Client>& client, 123 const String8& name, uint32_t w, uint32_t h, uint32_t flags); 124 125 virtual ~Layer(); 126 127 // the this layer's size and format 128 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 129 130 // modify current state 131 bool setPosition(float x, float y); 132 bool setLayer(uint32_t z); 133 bool setSize(uint32_t w, uint32_t h); 134 bool setAlpha(uint8_t alpha); 135 bool setMatrix(const layer_state_t::matrix22_t& matrix); 136 bool setTransparentRegionHint(const Region& transparent); 137 bool setFlags(uint8_t flags, uint8_t mask); 138 bool setCrop(const Rect& crop); 139 bool setLayerStack(uint32_t layerStack); 140 141 // If we have received a new buffer this frame, we will pass its surface 142 // damage down to hardware composer. Otherwise, we must send a region with 143 // one empty rect. 144 void useSurfaceDamage(); 145 void useEmptyDamage(); 146 147 uint32_t getTransactionFlags(uint32_t flags); 148 uint32_t setTransactionFlags(uint32_t flags); 149 150 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, 151 bool useIdentityTransform) const; 152 Rect computeBounds(const Region& activeTransparentRegion) const; 153 Rect computeBounds() const; 154 155 sp<IBinder> getHandle(); 156 sp<IGraphicBufferProducer> getProducer() const; 157 const String8& getName() const; 158 159 // ----------------------------------------------------------------------- 160 // Virtuals 161 getTypeId()162 virtual const char* getTypeId() const { return "Layer"; } 163 164 /* 165 * isOpaque - true if this surface is opaque 166 * 167 * This takes into account the buffer format (i.e. whether or not the 168 * pixel format includes an alpha channel) and the "opaque" flag set 169 * on the layer. It does not examine the current plane alpha value. 170 */ 171 virtual bool isOpaque(const Layer::State& s) const; 172 173 /* 174 * isSecure - true if this surface is secure, that is if it prevents 175 * screenshots or VNC servers. 176 */ 177 virtual bool isSecure() const; 178 179 /* 180 * isProtected - true if the layer may contain protected content in the 181 * GRALLOC_USAGE_PROTECTED sense. 182 */ 183 virtual bool isProtected() const; 184 185 /* 186 * isVisible - true if this layer is visible, false otherwise 187 */ 188 virtual bool isVisible() const; 189 190 /* 191 * isFixedSize - true if content has a fixed size 192 */ 193 virtual bool isFixedSize() const; 194 195 protected: 196 /* 197 * onDraw - draws the surface. 198 */ 199 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip, 200 bool useIdentityTransform) const; 201 202 public: 203 // ----------------------------------------------------------------------- 204 205 void setGeometry(const sp<const DisplayDevice>& hw, 206 HWComposer::HWCLayerInterface& layer); 207 void setPerFrameData(const sp<const DisplayDevice>& hw, 208 HWComposer::HWCLayerInterface& layer); 209 void setAcquireFence(const sp<const DisplayDevice>& hw, 210 HWComposer::HWCLayerInterface& layer); 211 212 Rect getPosition(const sp<const DisplayDevice>& hw); 213 214 /* 215 * called after page-flip 216 */ 217 void onLayerDisplayed(const sp<const DisplayDevice>& hw, 218 HWComposer::HWCLayerInterface* layer); 219 220 bool shouldPresentNow(const DispSync& dispSync) const; 221 222 /* 223 * called before composition. 224 * returns true if the layer has pending updates. 225 */ 226 bool onPreComposition(); 227 228 /* 229 * called after composition. 230 */ 231 void onPostComposition(); 232 233 /* 234 * draw - performs some global clipping optimizations 235 * and calls onDraw(). 236 */ 237 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const; 238 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const; 239 void draw(const sp<const DisplayDevice>& hw) const; 240 241 /* 242 * doTransaction - process the transaction. This is a good place to figure 243 * out which attributes of the surface have changed. 244 */ 245 uint32_t doTransaction(uint32_t transactionFlags); 246 247 /* 248 * setVisibleRegion - called to set the new visible region. This gives 249 * a chance to update the new visible region or record the fact it changed. 250 */ 251 void setVisibleRegion(const Region& visibleRegion); 252 253 /* 254 * setCoveredRegion - called when the covered region changes. The covered 255 * region corresponds to any area of the surface that is covered 256 * (transparently or not) by another surface. 257 */ 258 void setCoveredRegion(const Region& coveredRegion); 259 260 /* 261 * setVisibleNonTransparentRegion - called when the visible and 262 * non-transparent region changes. 263 */ 264 void setVisibleNonTransparentRegion(const Region& 265 visibleNonTransparentRegion); 266 267 /* 268 * latchBuffer - called each time the screen is redrawn and returns whether 269 * the visible regions need to be recomputed (this is a fairly heavy 270 * operation, so this should be set only if needed). Typically this is used 271 * to figure out if the content or size of a surface has changed. 272 */ 273 Region latchBuffer(bool& recomputeVisibleRegions); 274 isPotentialCursor()275 bool isPotentialCursor() const { return mPotentialCursor;} 276 277 /* 278 * called with the state lock when the surface is removed from the 279 * current list 280 */ 281 void onRemoved(); 282 283 284 // Updates the transform hint in our SurfaceFlingerConsumer to match 285 // the current orientation of the display device. 286 void updateTransformHint(const sp<const DisplayDevice>& hw) const; 287 288 /* 289 * returns the rectangle that crops the content of the layer and scales it 290 * to the layer's size. 291 */ 292 Rect getContentCrop() const; 293 294 /* 295 * Returns if a frame is queued. 296 */ hasQueuedFrame()297 bool hasQueuedFrame() const { return mQueuedFrames > 0 || mSidebandStreamChanged; } 298 299 // ----------------------------------------------------------------------- 300 301 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const; 302 void setFiltering(bool filtering); 303 bool getFiltering() const; 304 305 // only for debugging getActiveBuffer()306 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; } 307 getDrawingState()308 inline const State& getDrawingState() const { return mDrawingState; } getCurrentState()309 inline const State& getCurrentState() const { return mCurrentState; } getCurrentState()310 inline State& getCurrentState() { return mCurrentState; } 311 312 313 /* always call base class first */ 314 void dump(String8& result, Colorizer& colorizer) const; 315 void dumpFrameStats(String8& result) const; 316 void clearFrameStats(); 317 void logFrameStats(); 318 void getFrameStats(FrameStats* outStats) const; 319 320 protected: 321 // constant 322 sp<SurfaceFlinger> mFlinger; 323 324 virtual void onFirstRef(); 325 326 /* 327 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer) 328 * is called. 329 */ 330 class LayerCleaner { 331 sp<SurfaceFlinger> mFlinger; 332 wp<Layer> mLayer; 333 protected: 334 ~LayerCleaner(); 335 public: 336 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer); 337 }; 338 339 340 private: 341 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener 342 virtual void onFrameAvailable(const BufferItem& item) override; 343 virtual void onFrameReplaced(const BufferItem& item) override; 344 virtual void onSidebandStreamChanged() override; 345 346 void commitTransaction(); 347 348 // needsLinearFiltering - true if this surface's state requires filtering 349 bool needsFiltering(const sp<const DisplayDevice>& hw) const; 350 351 uint32_t getEffectiveUsage(uint32_t usage) const; 352 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const; 353 bool isCropped() const; 354 static bool getOpacityForFormat(uint32_t format); 355 356 // drawing 357 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip, 358 float r, float g, float b, float alpha) const; 359 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip, 360 bool useIdentityTransform) const; 361 362 // Temporary - Used only for LEGACY camera mode. 363 uint32_t getProducerStickyTransform() const; 364 365 366 // ----------------------------------------------------------------------- 367 368 // constants 369 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer; 370 sp<IGraphicBufferProducer> mProducer; 371 uint32_t mTextureName; // from GLES 372 bool mPremultipliedAlpha; 373 String8 mName; 374 PixelFormat mFormat; 375 376 // these are protected by an external lock 377 State mCurrentState; 378 State mDrawingState; 379 volatile int32_t mTransactionFlags; 380 381 // thread-safe 382 volatile int32_t mQueuedFrames; 383 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean 384 FrameTracker mFrameTracker; 385 386 // main thread 387 sp<GraphicBuffer> mActiveBuffer; 388 sp<NativeHandle> mSidebandStream; 389 Rect mCurrentCrop; 390 uint32_t mCurrentTransform; 391 uint32_t mCurrentScalingMode; 392 bool mCurrentOpacity; 393 bool mRefreshPending; 394 bool mFrameLatencyNeeded; 395 // Whether filtering is forced on or not 396 bool mFiltering; 397 // Whether filtering is needed b/c of the drawingstate 398 bool mNeedsFiltering; 399 // The mesh used to draw the layer in GLES composition mode 400 mutable Mesh mMesh; 401 // The texture used to draw the layer in GLES composition mode 402 mutable Texture mTexture; 403 404 // page-flip thread (currently main thread) 405 bool mProtectedByApp; // application requires protected path to external sink 406 407 // protected by mLock 408 mutable Mutex mLock; 409 // Set to true once we've returned this surface's handle 410 mutable bool mHasSurface; 411 const wp<Client> mClientRef; 412 413 // This layer can be a cursor on some displays. 414 bool mPotentialCursor; 415 416 // Local copy of the queued contents of the incoming BufferQueue 417 mutable Mutex mQueueItemLock; 418 Condition mQueueItemCondition; 419 Vector<BufferItem> mQueueItems; 420 uint64_t mLastFrameNumberReceived; 421 bool mUpdateTexImageFailed; // This is only modified from the main thread 422 }; 423 424 // --------------------------------------------------------------------------- 425 426 }; // namespace android 427 428 #endif // ANDROID_LAYER_H 429