Home | History | Annotate | Download | only in surfaceflinger
      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_BASE_H
     18 #define ANDROID_LAYER_BASE_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <EGL/egl.h>
     24 #include <EGL/eglext.h>
     25 #include <GLES/gl.h>
     26 
     27 #include <utils/RefBase.h>
     28 
     29 #include <ui/Region.h>
     30 
     31 #include <gui/ISurfaceComposerClient.h>
     32 
     33 #include <private/gui/LayerState.h>
     34 
     35 #include <hardware/hwcomposer.h>
     36 
     37 #include "DisplayHardware/DisplayHardware.h"
     38 #include "Transform.h"
     39 
     40 namespace android {
     41 
     42 // ---------------------------------------------------------------------------
     43 
     44 class Client;
     45 class DisplayHardware;
     46 class GraphicBuffer;
     47 class GraphicPlane;
     48 class Layer;
     49 class LayerBaseClient;
     50 class SurfaceFlinger;
     51 
     52 // ---------------------------------------------------------------------------
     53 
     54 class LayerBase : public RefBase
     55 {
     56     static int32_t sSequence;
     57 
     58 public:
     59             LayerBase(SurfaceFlinger* flinger, DisplayID display);
     60 
     61     DisplayID           dpy;
     62     mutable bool        contentDirty;
     63             Region      visibleRegionScreen;
     64             Region      transparentRegionScreen;
     65             Region      coveredRegionScreen;
     66             int32_t     sequence;
     67 
     68             struct Geometry {
     69                 uint32_t w;
     70                 uint32_t h;
     71                 Rect crop;
     72                 inline bool operator == (const Geometry& rhs) const {
     73                     return (w==rhs.w && h==rhs.h && crop==rhs.crop);
     74                 }
     75                 inline bool operator != (const Geometry& rhs) const {
     76                     return !operator == (rhs);
     77                 }
     78             };
     79 
     80             struct State {
     81                 Geometry        active;
     82                 Geometry        requested;
     83                 uint32_t        z;
     84                 uint8_t         alpha;
     85                 uint8_t         flags;
     86                 uint8_t         reserved[2];
     87                 int32_t         sequence;   // changes when visible regions can change
     88                 Transform       transform;
     89                 Region          transparentRegion;
     90             };
     91 
     92     virtual void setName(const String8& name);
     93             String8 getName() const;
     94 
     95             // modify current state
     96             bool setPosition(float x, float y);
     97             bool setLayer(uint32_t z);
     98             bool setSize(uint32_t w, uint32_t h);
     99             bool setAlpha(uint8_t alpha);
    100             bool setMatrix(const layer_state_t::matrix22_t& matrix);
    101             bool setTransparentRegionHint(const Region& opaque);
    102             bool setFlags(uint8_t flags, uint8_t mask);
    103             bool setCrop(const Rect& crop);
    104 
    105             void commitTransaction();
    106             bool requestTransaction();
    107             void forceVisibilityTransaction();
    108 
    109             uint32_t getTransactionFlags(uint32_t flags);
    110             uint32_t setTransactionFlags(uint32_t flags);
    111 
    112             Rect visibleBounds() const;
    113 
    114     virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
    115     virtual sp<Layer> getLayer() const { return 0; }
    116 
    117     virtual const char* getTypeId() const { return "LayerBase"; }
    118 
    119     virtual void setGeometry(hwc_layer_t* hwcl);
    120     virtual void setPerFrameData(hwc_layer_t* hwcl);
    121 
    122 
    123     /**
    124      * draw - performs some global clipping optimizations
    125      * and calls onDraw().
    126      * Typically this method is not overridden, instead implement onDraw()
    127      * to perform the actual drawing.
    128      */
    129     virtual void draw(const Region& clip) const;
    130     virtual void drawForSreenShot();
    131 
    132     /**
    133      * onDraw - draws the surface.
    134      */
    135     virtual void onDraw(const Region& clip) const = 0;
    136 
    137     /**
    138      * initStates - called just after construction
    139      */
    140     virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
    141 
    142     /**
    143      * doTransaction - process the transaction. This is a good place to figure
    144      * out which attributes of the surface have changed.
    145      */
    146     virtual uint32_t doTransaction(uint32_t transactionFlags);
    147 
    148     /**
    149      * setVisibleRegion - called to set the new visible region. This gives
    150      * a chance to update the new visible region or record the fact it changed.
    151      */
    152     virtual void setVisibleRegion(const Region& visibleRegion);
    153 
    154     /**
    155      * setCoveredRegion - called when the covered region changes. The covered
    156      * region corresponds to any area of the surface that is covered
    157      * (transparently or not) by another surface.
    158      */
    159     virtual void setCoveredRegion(const Region& coveredRegion);
    160 
    161     /**
    162      * validateVisibility - cache a bunch of things
    163      */
    164     virtual void validateVisibility(const Transform& globalTransform);
    165 
    166     /**
    167      * lockPageFlip - called each time the screen is redrawn and returns whether
    168      * the visible regions need to be recomputed (this is a fairly heavy
    169      * operation, so this should be set only if needed). Typically this is used
    170      * to figure out if the content or size of a surface has changed.
    171      */
    172     virtual void lockPageFlip(bool& recomputeVisibleRegions);
    173 
    174     /**
    175      * unlockPageFlip - called each time the screen is redrawn. updates the
    176      * final dirty region wrt the planeTransform.
    177      * At this point, all visible regions, surface position and size, etc... are
    178      * correct.
    179      */
    180     virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
    181 
    182     /**
    183      * isOpaque - true if this surface is opaque
    184      */
    185     virtual bool isOpaque() const  { return true; }
    186 
    187     /**
    188      * needsDithering - true if this surface needs dithering
    189      */
    190     virtual bool needsDithering() const { return false; }
    191 
    192     /**
    193      * needsLinearFiltering - true if this surface's state requires filtering
    194      */
    195     virtual bool needsFiltering() const { return mNeedsFiltering; }
    196 
    197     /**
    198      * isSecure - true if this surface is secure, that is if it prevents
    199      * screenshots or VNC servers.
    200      */
    201     virtual bool isSecure() const       { return false; }
    202 
    203     /**
    204      * isProtected - true if the layer may contain protected content in the
    205      * GRALLOC_USAGE_PROTECTED sense.
    206      */
    207     virtual bool isProtected() const   { return false; }
    208 
    209     /** called with the state lock when the surface is removed from the
    210      *  current list */
    211     virtual void onRemoved() { }
    212 
    213     /** called after page-flip
    214      */
    215     virtual void onLayerDisplayed() { }
    216 
    217     /** called before composition.
    218      * returns true if the layer has pending updates.
    219      */
    220     virtual bool onPreComposition() { return false; }
    221 
    222     /** always call base class first */
    223     virtual void dump(String8& result, char* scratch, size_t size) const;
    224     virtual void shortDump(String8& result, char* scratch, size_t size) const;
    225     virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
    226     virtual void clearStats();
    227 
    228 
    229     enum { // flags for doTransaction()
    230         eDontUpdateGeometryState = 0x00000001,
    231         eVisibleRegion           = 0x00000002,
    232     };
    233 
    234 
    235     inline  const State&    drawingState() const    { return mDrawingState; }
    236     inline  const State&    currentState() const    { return mCurrentState; }
    237     inline  State&          currentState()          { return mCurrentState; }
    238 
    239     int32_t  getOrientation() const { return mOrientation; }
    240     int32_t  getPlaneOrientation() const { return mPlaneOrientation; }
    241 
    242     void clearWithOpenGL(const Region& clip) const;
    243 
    244 protected:
    245     const GraphicPlane& graphicPlane(int dpy) const;
    246           GraphicPlane& graphicPlane(int dpy);
    247 
    248           void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
    249                                GLclampf b, GLclampf alpha) const;
    250           void drawWithOpenGL(const Region& clip) const;
    251 
    252           void setFiltering(bool filtering);
    253           bool getFiltering() const;
    254 
    255                 sp<SurfaceFlinger> mFlinger;
    256                 uint32_t        mFlags;
    257 
    258 private:
    259                 // accessed only in the main thread
    260                 // Whether filtering is forced on or not
    261                 bool            mFiltering;
    262 
    263                 // cached during validateVisibility()
    264                 // Whether filtering is needed b/c of the drawingstate
    265                 bool            mNeedsFiltering;
    266 
    267 protected:
    268                 // cached during validateVisibility()
    269                 int32_t         mOrientation;
    270                 int32_t         mPlaneOrientation;
    271                 Transform       mTransform;
    272                 GLfloat         mVertices[4][2];
    273                 size_t          mNumVertices;
    274                 Rect            mTransformedBounds;
    275 
    276                 // these are protected by an external lock
    277                 State           mCurrentState;
    278                 State           mDrawingState;
    279     volatile    int32_t         mTransactionFlags;
    280 
    281                 // don't change, don't need a lock
    282                 bool            mPremultipliedAlpha;
    283                 String8         mName;
    284     mutable     bool            mDebug;
    285 
    286 
    287 public:
    288     // called from class SurfaceFlinger
    289     virtual ~LayerBase();
    290 
    291 private:
    292     LayerBase(const LayerBase& rhs);
    293 };
    294 
    295 
    296 // ---------------------------------------------------------------------------
    297 
    298 class LayerBaseClient : public LayerBase
    299 {
    300 public:
    301             LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
    302                         const sp<Client>& client);
    303 
    304             virtual ~LayerBaseClient();
    305 
    306             sp<ISurface> getSurface();
    307             wp<IBinder> getSurfaceBinder() const;
    308             virtual wp<IBinder> getSurfaceTextureBinder() const;
    309 
    310     virtual sp<LayerBaseClient> getLayerBaseClient() const {
    311         return const_cast<LayerBaseClient*>(this); }
    312 
    313     virtual const char* getTypeId() const { return "LayerBaseClient"; }
    314 
    315     uint32_t getIdentity() const { return mIdentity; }
    316 
    317 protected:
    318     virtual void dump(String8& result, char* scratch, size_t size) const;
    319     virtual void shortDump(String8& result, char* scratch, size_t size) const;
    320 
    321     class LayerCleaner {
    322         sp<SurfaceFlinger> mFlinger;
    323         wp<LayerBaseClient> mLayer;
    324     protected:
    325         ~LayerCleaner();
    326     public:
    327         LayerCleaner(const sp<SurfaceFlinger>& flinger,
    328                 const sp<LayerBaseClient>& layer);
    329     };
    330 
    331 private:
    332     virtual sp<ISurface> createSurface();
    333 
    334     mutable Mutex mLock;
    335     mutable bool mHasSurface;
    336     wp<IBinder> mClientSurfaceBinder;
    337     const wp<Client> mClientRef;
    338     // only read
    339     const uint32_t mIdentity;
    340     static int32_t sIdentity;
    341 };
    342 
    343 // ---------------------------------------------------------------------------
    344 
    345 }; // namespace android
    346 
    347 #endif // ANDROID_LAYER_BASE_H
    348