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