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