Layer.h revision 4125a4ffaf374ca9c0773f256998557d3325343e
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 "SurfaceFlinger.h" 41#include "SurfaceFlingerConsumer.h" 42#include "SurfaceTextureLayer.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::FrameAvailableListener { 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) const; 139 Rect computeBounds() const; 140 141 sp<IBinder> getHandle(); 142 sp<IGraphicBufferProducer> getBufferQueue() const; 143 const String8& getName() const; 144 145 // ----------------------------------------------------------------------- 146 // Virtuals 147 148 virtual const char* getTypeId() const { return "Layer"; } 149 150 /* 151 * isOpaque - true if this surface is opaque 152 * 153 * This takes into account the buffer format (i.e. whether or not the 154 * pixel format includes an alpha channel) and the "opaque" flag set 155 * on the layer. It does not examine the current plane alpha value. 156 */ 157 virtual bool isOpaque(const Layer::State& s) const; 158 159 /* 160 * isSecure - true if this surface is secure, that is if it prevents 161 * screenshots or VNC servers. 162 */ 163 virtual bool isSecure() const { return mSecure; } 164 165 /* 166 * isProtected - true if the layer may contain protected content in the 167 * GRALLOC_USAGE_PROTECTED sense. 168 */ 169 virtual bool isProtected() const; 170 171 /* 172 * isVisible - true if this layer is visible, false otherwise 173 */ 174 virtual bool isVisible() const; 175 176 /* 177 * isFixedSize - true if content has a fixed size 178 */ 179 virtual bool isFixedSize() const; 180 181protected: 182 /* 183 * onDraw - draws the surface. 184 */ 185 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const; 186 187public: 188 // ----------------------------------------------------------------------- 189 190 void setGeometry(const sp<const DisplayDevice>& hw, 191 HWComposer::HWCLayerInterface& layer); 192 void setPerFrameData(const sp<const DisplayDevice>& hw, 193 HWComposer::HWCLayerInterface& layer); 194 void setAcquireFence(const sp<const DisplayDevice>& hw, 195 HWComposer::HWCLayerInterface& layer); 196 197 /* 198 * called after page-flip 199 */ 200 void onLayerDisplayed(const sp<const DisplayDevice>& hw, 201 HWComposer::HWCLayerInterface* layer); 202 203 /* 204 * called before composition. 205 * returns true if the layer has pending updates. 206 */ 207 bool onPreComposition(); 208 209 /* 210 * called after composition. 211 */ 212 void onPostComposition(); 213 214 /* 215 * draw - performs some global clipping optimizations 216 * and calls onDraw(). 217 */ 218 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const; 219 void draw(const sp<const DisplayDevice>& hw); 220 221 /* 222 * doTransaction - process the transaction. This is a good place to figure 223 * out which attributes of the surface have changed. 224 */ 225 uint32_t doTransaction(uint32_t transactionFlags); 226 227 /* 228 * setVisibleRegion - called to set the new visible region. This gives 229 * a chance to update the new visible region or record the fact it changed. 230 */ 231 void setVisibleRegion(const Region& visibleRegion); 232 233 /* 234 * setCoveredRegion - called when the covered region changes. The covered 235 * region corresponds to any area of the surface that is covered 236 * (transparently or not) by another surface. 237 */ 238 void setCoveredRegion(const Region& coveredRegion); 239 240 /* 241 * setVisibleNonTransparentRegion - called when the visible and 242 * non-transparent region changes. 243 */ 244 void setVisibleNonTransparentRegion(const Region& 245 visibleNonTransparentRegion); 246 247 /* 248 * latchBuffer - called each time the screen is redrawn and returns whether 249 * the visible regions need to be recomputed (this is a fairly heavy 250 * operation, so this should be set only if needed). Typically this is used 251 * to figure out if the content or size of a surface has changed. 252 */ 253 Region latchBuffer(bool& recomputeVisibleRegions); 254 255 /* 256 * called with the state lock when the surface is removed from the 257 * current list 258 */ 259 void onRemoved(); 260 261 262 // Updates the transform hint in our SurfaceFlingerConsumer to match 263 // the current orientation of the display device. 264 void updateTransformHint(const sp<const DisplayDevice>& hw) const; 265 266 /* 267 * returns the rectangle that crops the content of the layer and scales it 268 * to the layer's size. 269 */ 270 Rect getContentCrop() const; 271 272 // ----------------------------------------------------------------------- 273 274 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const; 275 void setFiltering(bool filtering); 276 bool getFiltering() const; 277 278 // only for debugging 279 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; } 280 281 inline const State& getDrawingState() const { return mDrawingState; } 282 inline const State& getCurrentState() const { return mCurrentState; } 283 inline State& getCurrentState() { return mCurrentState; } 284 285 286 /* always call base class first */ 287 void dump(String8& result, Colorizer& colorizer) const; 288 void dumpStats(String8& result) const; 289 void clearStats(); 290 void logFrameStats(); 291 292protected: 293 // constant 294 sp<SurfaceFlinger> mFlinger; 295 296 virtual void onFirstRef(); 297 298 /* 299 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer) 300 * is called. 301 */ 302 class LayerCleaner { 303 sp<SurfaceFlinger> mFlinger; 304 wp<Layer> mLayer; 305 protected: 306 ~LayerCleaner(); 307 public: 308 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer); 309 }; 310 311 312private: 313 // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener 314 virtual void onFrameAvailable(); 315 316 void commitTransaction(); 317 318 // needsLinearFiltering - true if this surface's state requires filtering 319 bool needsFiltering(const sp<const DisplayDevice>& hw) const; 320 321 uint32_t getEffectiveUsage(uint32_t usage) const; 322 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const; 323 bool isCropped() const; 324 static bool getOpacityForFormat(uint32_t format); 325 326 // drawing 327 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip, 328 float r, float g, float b, float alpha) const; 329 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const; 330 331 332 // ----------------------------------------------------------------------- 333 334 // constants 335 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer; 336 sp<BufferQueue> mBufferQueue; 337 uint32_t mTextureName; 338 bool mPremultipliedAlpha; 339 String8 mName; 340 mutable bool mDebug; 341 PixelFormat mFormat; 342 343 // these are protected by an external lock 344 State mCurrentState; 345 State mDrawingState; 346 volatile int32_t mTransactionFlags; 347 348 // thread-safe 349 volatile int32_t mQueuedFrames; 350 FrameTracker mFrameTracker; 351 352 // main thread 353 sp<GraphicBuffer> mActiveBuffer; 354 Rect mCurrentCrop; 355 uint32_t mCurrentTransform; 356 uint32_t mCurrentScalingMode; 357 bool mCurrentOpacity; 358 bool mRefreshPending; 359 bool mFrameLatencyNeeded; 360 // Whether filtering is forced on or not 361 bool mFiltering; 362 // Whether filtering is needed b/c of the drawingstate 363 bool mNeedsFiltering; 364 // The mesh used to draw the layer in GLES composition mode 365 mutable Mesh mMesh; 366 // The mesh used to draw the layer in GLES composition mode 367 mutable Texture mTexture; 368 369 // page-flip thread (currently main thread) 370 bool mSecure; // no screenshots 371 bool mProtectedByApp; // application requires protected path to external sink 372 373 // protected by mLock 374 mutable Mutex mLock; 375 // Set to true once we've returned this surface's handle 376 mutable bool mHasSurface; 377 const wp<Client> mClientRef; 378}; 379 380// --------------------------------------------------------------------------- 381 382}; // namespace android 383 384#endif // ANDROID_LAYER_H 385