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_H
     18 #define ANDROID_LAYER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <gui/SurfaceTexture.h>
     24 
     25 #include <utils/Timers.h>
     26 
     27 #include <ui/GraphicBuffer.h>
     28 #include <ui/PixelFormat.h>
     29 
     30 #include <gui/ISurfaceComposerClient.h>
     31 
     32 #include <EGL/egl.h>
     33 #include <EGL/eglext.h>
     34 #include <GLES/gl.h>
     35 #include <GLES/glext.h>
     36 
     37 #include "LayerBase.h"
     38 #include "SurfaceTextureLayer.h"
     39 #include "Transform.h"
     40 
     41 namespace android {
     42 
     43 // ---------------------------------------------------------------------------
     44 
     45 class Client;
     46 class GLExtensions;
     47 
     48 // ---------------------------------------------------------------------------
     49 
     50 class Layer : public LayerBaseClient
     51 {
     52 public:
     53             Layer(SurfaceFlinger* flinger, DisplayID display,
     54                     const sp<Client>& client);
     55 
     56     virtual ~Layer();
     57 
     58     virtual const char* getTypeId() const { return "Layer"; }
     59 
     60     // the this layer's size and format
     61     status_t setBuffers(uint32_t w, uint32_t h,
     62             PixelFormat format, uint32_t flags=0);
     63 
     64     bool isFixedSize() const;
     65 
     66     // LayerBase interface
     67     virtual void setGeometry(hwc_layer_t* hwcl);
     68     virtual void setPerFrameData(hwc_layer_t* hwcl);
     69     virtual void onDraw(const Region& clip) const;
     70     virtual uint32_t doTransaction(uint32_t transactionFlags);
     71     virtual void lockPageFlip(bool& recomputeVisibleRegions);
     72     virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
     73     virtual bool isOpaque() const;
     74     virtual bool needsDithering() const     { return mNeedsDithering; }
     75     virtual bool isSecure() const           { return mSecure; }
     76     virtual bool isProtected() const;
     77     virtual void onRemoved();
     78     virtual sp<Layer> getLayer() const { return const_cast<Layer*>(this); }
     79     virtual void setName(const String8& name);
     80     virtual void validateVisibility(const Transform& globalTransform);
     81 
     82     // LayerBaseClient interface
     83     virtual wp<IBinder> getSurfaceTextureBinder() const;
     84 
     85     virtual void onLayerDisplayed();
     86     virtual bool onPreComposition();
     87 
     88     // only for debugging
     89     inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
     90 
     91 protected:
     92     virtual void onFirstRef();
     93     virtual void dump(String8& result, char* scratch, size_t size) const;
     94     virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
     95     virtual void clearStats();
     96 
     97 private:
     98     friend class SurfaceTextureLayer;
     99     void onFrameQueued();
    100     virtual sp<ISurface> createSurface();
    101     uint32_t getEffectiveUsage(uint32_t usage) const;
    102     uint32_t getTransformHint() const;
    103     bool isCropped() const;
    104     Rect computeBufferCrop() const;
    105     static bool getOpacityForFormat(uint32_t format);
    106 
    107     // -----------------------------------------------------------------------
    108 
    109     // constants
    110     sp<SurfaceTexture> mSurfaceTexture;
    111     GLuint mTextureName;
    112 
    113     // thread-safe
    114     volatile int32_t mQueuedFrames;
    115 
    116     // main thread
    117     sp<GraphicBuffer> mActiveBuffer;
    118     Rect mCurrentCrop;
    119     uint32_t mCurrentTransform;
    120     uint32_t mCurrentScalingMode;
    121     bool mCurrentOpacity;
    122     bool mRefreshPending;
    123     bool mFrameLatencyNeeded;
    124     int mFrameLatencyOffset;
    125 
    126     struct Statistics {
    127         Statistics() : timestamp(0), set(0), vsync(0) { }
    128         nsecs_t timestamp;  // buffer timestamp
    129         nsecs_t set;        // buffer displayed timestamp
    130         nsecs_t vsync;      // vsync immediately before set
    131     };
    132 
    133     // protected by mLock
    134     Statistics mFrameStats[128];
    135 
    136     // constants
    137     PixelFormat mFormat;
    138     const GLExtensions& mGLExtensions;
    139     bool mOpaqueLayer;
    140     bool mNeedsDithering;
    141 
    142     // page-flip thread (currently main thread)
    143     bool mSecure;         // no screenshots
    144     bool mProtectedByApp; // application requires protected path to external sink
    145     Region mPostedDirtyRegion;
    146 };
    147 
    148 // ---------------------------------------------------------------------------
    149 
    150 }; // namespace android
    151 
    152 #endif // ANDROID_LAYER_H
    153