Layer.h revision 6c925ede620f4080227bb1fe8a41e4b4502348f8
133fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair/* 233fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * Copyright (C) 2007 The Android Open Source Project 333fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * 433fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * Licensed under the Apache License, Version 2.0 (the "License"); 533fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * you may not use this file except in compliance with the License. 633fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * You may obtain a copy of the License at 733fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * 833fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * http://www.apache.org/licenses/LICENSE-2.0 933fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * 1033fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * Unless required by applicable law or agreed to in writing, software 1133fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * distributed under the License is distributed on an "AS IS" BASIS, 1233fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1333fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * See the License for the specific language governing permissions and 1433fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair * limitations under the License. 1533fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair */ 1633fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair 17357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#ifndef ANDROID_LAYER_H 1833fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#define ANDROID_LAYER_H 1933fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair 2033fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include <stdint.h> 2133fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include <sys/types.h> 2233fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair 2333fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include <EGL/egl.h> 2433fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include <EGL/eglext.h> 2533fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair 2633fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include <utils/RefBase.h> 2733fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include <utils/String8.h> 28357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <utils/Timers.h> 29357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair 30357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <ui/FrameStats.h> 31357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <ui/GraphicBuffer.h> 32357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <ui/PixelFormat.h> 33357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <ui/Region.h> 34357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair 35357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <gui/ISurfaceComposerClient.h> 36357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair 37357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include <private/gui/LayerState.h> 38357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair 39357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include "FrameTracker.h" 40357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include "Client.h" 4133fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include "MonitoredProducer.h" 4233fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include "SurfaceFlinger.h" 4333fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include "SurfaceFlingerConsumer.h" 4433fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include "Transform.h" 4533fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair 46357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include "DisplayHardware/HWComposer.h" 4733fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include "DisplayHardware/FloatRect.h" 48357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair#include "RenderEngine/Mesh.h" 4933fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair#include "RenderEngine/Texture.h" 50357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair 5133fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nairnamespace android { 52357988777218502ed68a56a0c8b247ba64b74721Vishnu Nair 5333fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair// --------------------------------------------------------------------------- 5433fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nair 55357988777218502ed68a56a0c8b247ba64b74721Vishnu Nairclass Client; 5633fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nairclass Colorizer; 57357988777218502ed68a56a0c8b247ba64b74721Vishnu Nairclass DisplayDevice; 5833fd3be6088bfacaf659108bad67d24103494aa1Vishnu Nairclass GraphicBuffer; 59357988777218502ed68a56a0c8b247ba64b74721Vishnu Nairclass 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 */ 70class Layer : public SurfaceFlingerConsumer::ContentsChangedListener { 71 static int32_t sSequence; 72 73public: 74 mutable bool contentDirty; 75 // regions below are in window-manager space 76 Region visibleRegion; 77 Region coveredRegion; 78 Region visibleNonTransparentRegion; 79 80 // Layer serial number. This gives layers an explicit ordering, so we 81 // have a stable sort order when their layer stack and Z-order are 82 // the same. 83 int32_t sequence; 84 85 enum { // flags for doTransaction() 86 eDontUpdateGeometryState = 0x00000001, 87 eVisibleRegion = 0x00000002, 88 }; 89 90 struct Geometry { 91 uint32_t w; 92 uint32_t h; 93 Rect crop; 94 inline bool operator ==(const Geometry& rhs) const { 95 return (w == rhs.w && h == rhs.h && crop == rhs.crop); 96 } 97 inline bool operator !=(const Geometry& rhs) const { 98 return !operator ==(rhs); 99 } 100 }; 101 102 struct State { 103 Geometry active; 104 Geometry requested; 105 uint32_t z; 106 uint32_t layerStack; 107 uint8_t alpha; 108 uint8_t flags; 109 uint8_t reserved[2]; 110 int32_t sequence; // changes when visible regions can change 111 Transform transform; 112 // the transparentRegion hint is a bit special, it's latched only 113 // when we receive a buffer -- this is because it's "content" 114 // dependent. 115 Region activeTransparentRegion; 116 Region requestedTransparentRegion; 117 }; 118 119 // ----------------------------------------------------------------------- 120 121 Layer(SurfaceFlinger* flinger, const sp<Client>& client, 122 const String8& name, uint32_t w, uint32_t h, uint32_t flags); 123 124 virtual ~Layer(); 125 126 // the this layer's size and format 127 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); 128 129 // modify current state 130 bool setPosition(float x, float y); 131 bool setLayer(uint32_t z); 132 bool setSize(uint32_t w, uint32_t h); 133 bool setAlpha(uint8_t alpha); 134 bool setMatrix(const layer_state_t::matrix22_t& matrix); 135 bool setTransparentRegionHint(const Region& transparent); 136 bool setFlags(uint8_t flags, uint8_t mask); 137 bool setCrop(const Rect& crop); 138 bool setLayerStack(uint32_t layerStack); 139 140 uint32_t getTransactionFlags(uint32_t flags); 141 uint32_t setTransactionFlags(uint32_t flags); 142 143 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, 144 bool useIdentityTransform) const; 145 Rect computeBounds(const Region& activeTransparentRegion) const; 146 Rect computeBounds() const; 147 148 sp<IBinder> getHandle(); 149 sp<IGraphicBufferProducer> getProducer() const; 150 const String8& getName() const; 151 152 // ----------------------------------------------------------------------- 153 // Virtuals 154 155 virtual const char* getTypeId() const { return "Layer"; } 156 157 /* 158 * isOpaque - true if this surface is opaque 159 * 160 * This takes into account the buffer format (i.e. whether or not the 161 * pixel format includes an alpha channel) and the "opaque" flag set 162 * on the layer. It does not examine the current plane alpha value. 163 */ 164 virtual bool isOpaque(const Layer::State& s) const; 165 166 /* 167 * isSecure - true if this surface is secure, that is if it prevents 168 * screenshots or VNC servers. 169 */ 170 virtual bool isSecure() const { return mSecure; } 171 172 /* 173 * isProtected - true if the layer may contain protected content in the 174 * GRALLOC_USAGE_PROTECTED sense. 175 */ 176 virtual bool isProtected() const; 177 178 /* 179 * isVisible - true if this layer is visible, false otherwise 180 */ 181 virtual bool isVisible() const; 182 183 /* 184 * isFixedSize - true if content has a fixed size 185 */ 186 virtual bool isFixedSize() const; 187 188protected: 189 /* 190 * onDraw - draws the surface. 191 */ 192 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip, 193 bool useIdentityTransform) const; 194 195public: 196 // ----------------------------------------------------------------------- 197 198 void setGeometry(const sp<const DisplayDevice>& hw, 199 HWComposer::HWCLayerInterface& layer); 200 void setPerFrameData(const sp<const DisplayDevice>& hw, 201 HWComposer::HWCLayerInterface& layer); 202 void setAcquireFence(const sp<const DisplayDevice>& hw, 203 HWComposer::HWCLayerInterface& layer); 204 205 Rect getPosition(const sp<const DisplayDevice>& hw); 206 207 /* 208 * called after page-flip 209 */ 210 void onLayerDisplayed(const sp<const DisplayDevice>& hw, 211 HWComposer::HWCLayerInterface* layer); 212 213 /* 214 * called before composition. 215 * returns true if the layer has pending updates. 216 */ 217 bool onPreComposition(); 218 219 /* 220 * called after composition. 221 */ 222 void onPostComposition(); 223 224 /* 225 * draw - performs some global clipping optimizations 226 * and calls onDraw(). 227 */ 228 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const; 229 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const; 230 void draw(const sp<const DisplayDevice>& hw) const; 231 232 /* 233 * doTransaction - process the transaction. This is a good place to figure 234 * out which attributes of the surface have changed. 235 */ 236 uint32_t doTransaction(uint32_t transactionFlags); 237 238 /* 239 * setVisibleRegion - called to set the new visible region. This gives 240 * a chance to update the new visible region or record the fact it changed. 241 */ 242 void setVisibleRegion(const Region& visibleRegion); 243 244 /* 245 * setCoveredRegion - called when the covered region changes. The covered 246 * region corresponds to any area of the surface that is covered 247 * (transparently or not) by another surface. 248 */ 249 void setCoveredRegion(const Region& coveredRegion); 250 251 /* 252 * setVisibleNonTransparentRegion - called when the visible and 253 * non-transparent region changes. 254 */ 255 void setVisibleNonTransparentRegion(const Region& 256 visibleNonTransparentRegion); 257 258 /* 259 * latchBuffer - called each time the screen is redrawn and returns whether 260 * the visible regions need to be recomputed (this is a fairly heavy 261 * operation, so this should be set only if needed). Typically this is used 262 * to figure out if the content or size of a surface has changed. 263 */ 264 Region latchBuffer(bool& recomputeVisibleRegions); 265 266 bool isPotentialCursor() const { return mPotentialCursor;} 267 268 /* 269 * called with the state lock when the surface is removed from the 270 * current list 271 */ 272 void onRemoved(); 273 274 275 // Updates the transform hint in our SurfaceFlingerConsumer to match 276 // the current orientation of the display device. 277 void updateTransformHint(const sp<const DisplayDevice>& hw) const; 278 279 /* 280 * returns the rectangle that crops the content of the layer and scales it 281 * to the layer's size. 282 */ 283 Rect getContentCrop() const; 284 285 /* 286 * Returns if a frame is queued. 287 */ 288 bool hasQueuedFrame() const { return mQueuedFrames > 0 || mSidebandStreamChanged; } 289 290 // ----------------------------------------------------------------------- 291 292 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const; 293 void setFiltering(bool filtering); 294 bool getFiltering() const; 295 296 // only for debugging 297 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; } 298 299 inline const State& getDrawingState() const { return mDrawingState; } 300 inline const State& getCurrentState() const { return mCurrentState; } 301 inline State& getCurrentState() { return mCurrentState; } 302 303 304 /* always call base class first */ 305 void dump(String8& result, Colorizer& colorizer) const; 306 void dumpFrameStats(String8& result) const; 307 void clearFrameStats(); 308 void logFrameStats(); 309 void getFrameStats(FrameStats* outStats) const; 310 311protected: 312 // constant 313 sp<SurfaceFlinger> mFlinger; 314 315 virtual void onFirstRef(); 316 317 /* 318 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer) 319 * is called. 320 */ 321 class LayerCleaner { 322 sp<SurfaceFlinger> mFlinger; 323 wp<Layer> mLayer; 324 protected: 325 ~LayerCleaner(); 326 public: 327 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer); 328 }; 329 330 331private: 332 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener 333 virtual void onFrameAvailable(); 334 virtual void onSidebandStreamChanged(); 335 336 void commitTransaction(); 337 338 // needsLinearFiltering - true if this surface's state requires filtering 339 bool needsFiltering(const sp<const DisplayDevice>& hw) const; 340 341 uint32_t getEffectiveUsage(uint32_t usage) const; 342 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const; 343 bool isCropped() const; 344 static bool getOpacityForFormat(uint32_t format); 345 346 // drawing 347 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip, 348 float r, float g, float b, float alpha) const; 349 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip, 350 bool useIdentityTransform) const; 351 352 // Temporary - Used only for LEGACY camera mode. 353 uint32_t getProducerStickyTransform() const; 354 355 356 // ----------------------------------------------------------------------- 357 358 // constants 359 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer; 360 sp<IGraphicBufferProducer> mProducer; 361 uint32_t mTextureName; // from GLES 362 bool mPremultipliedAlpha; 363 String8 mName; 364 mutable bool mDebug; 365 PixelFormat mFormat; 366 367 // these are protected by an external lock 368 State mCurrentState; 369 State mDrawingState; 370 volatile int32_t mTransactionFlags; 371 372 // thread-safe 373 volatile int32_t mQueuedFrames; 374 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean 375 FrameTracker mFrameTracker; 376 377 // main thread 378 sp<GraphicBuffer> mActiveBuffer; 379 sp<NativeHandle> mSidebandStream; 380 Rect mCurrentCrop; 381 uint32_t mCurrentTransform; 382 uint32_t mCurrentScalingMode; 383 bool mCurrentOpacity; 384 bool mRefreshPending; 385 bool mFrameLatencyNeeded; 386 // Whether filtering is forced on or not 387 bool mFiltering; 388 // Whether filtering is needed b/c of the drawingstate 389 bool mNeedsFiltering; 390 // The mesh used to draw the layer in GLES composition mode 391 mutable Mesh mMesh; 392 // The texture used to draw the layer in GLES composition mode 393 mutable Texture mTexture; 394 395 // page-flip thread (currently main thread) 396 bool mSecure; // no screenshots 397 bool mProtectedByApp; // application requires protected path to external sink 398 399 // protected by mLock 400 mutable Mutex mLock; 401 // Set to true once we've returned this surface's handle 402 mutable bool mHasSurface; 403 const wp<Client> mClientRef; 404 405 // This layer can be a cursor on some displays. 406 bool mPotentialCursor; 407}; 408 409// --------------------------------------------------------------------------- 410 411}; // namespace android 412 413#endif // ANDROID_LAYER_H 414