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_DISPLAY_DEVICE_H
     18 #define ANDROID_DISPLAY_DEVICE_H
     19 
     20 #include <stdlib.h>
     21 
     22 #include <ui/PixelFormat.h>
     23 #include <ui/Region.h>
     24 
     25 #include <EGL/egl.h>
     26 #include <EGL/eglext.h>
     27 
     28 #include <utils/Mutex.h>
     29 #include <utils/String8.h>
     30 #include <utils/Timers.h>
     31 
     32 #include <hardware/hwcomposer_defs.h>
     33 
     34 #include "Transform.h"
     35 
     36 struct ANativeWindow;
     37 
     38 namespace android {
     39 
     40 class DisplayInfo;
     41 class DisplaySurface;
     42 class IGraphicBufferProducer;
     43 class Layer;
     44 class SurfaceFlinger;
     45 class HWComposer;
     46 
     47 class DisplayDevice : public LightRefBase<DisplayDevice>
     48 {
     49 public:
     50     // region in layer-stack space
     51     mutable Region dirtyRegion;
     52     // region in screen space
     53     mutable Region swapRegion;
     54     // region in screen space
     55     Region undefinedRegion;
     56 
     57     enum DisplayType {
     58         DISPLAY_ID_INVALID = -1,
     59         DISPLAY_PRIMARY     = HWC_DISPLAY_PRIMARY,
     60         DISPLAY_EXTERNAL    = HWC_DISPLAY_EXTERNAL,
     61         DISPLAY_VIRTUAL     = HWC_DISPLAY_VIRTUAL,
     62         NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
     63     };
     64 
     65     enum {
     66         PARTIAL_UPDATES = 0x00020000, // video driver feature
     67         SWAP_RECTANGLE  = 0x00080000,
     68     };
     69 
     70     enum {
     71         NO_LAYER_STACK = 0xFFFFFFFF,
     72     };
     73 
     74     DisplayDevice(
     75             const sp<SurfaceFlinger>& flinger,
     76             DisplayType type,
     77             int32_t hwcId,  // negative for non-HWC-composited displays
     78             bool isSecure,
     79             const wp<IBinder>& displayToken,
     80             const sp<DisplaySurface>& displaySurface,
     81             const sp<IGraphicBufferProducer>& producer,
     82             EGLConfig config);
     83 
     84     ~DisplayDevice();
     85 
     86     // whether this is a valid object. An invalid DisplayDevice is returned
     87     // when an non existing id is requested
     88     bool isValid() const;
     89 
     90     // isSecure indicates whether this display can be trusted to display
     91     // secure surfaces.
     92     bool isSecure() const { return mIsSecure; }
     93 
     94     // Flip the front and back buffers if the back buffer is "dirty".  Might
     95     // be instantaneous, might involve copying the frame buffer around.
     96     void flip(const Region& dirty) const;
     97 
     98     int         getWidth() const;
     99     int         getHeight() const;
    100     PixelFormat getFormat() const;
    101     uint32_t    getFlags() const;
    102 
    103     EGLSurface  getEGLSurface() const;
    104 
    105     void                    setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
    106     const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
    107     bool                    getSecureLayerVisible() const;
    108     Region                  getDirtyRegion(bool repaintEverything) const;
    109 
    110     void                    setLayerStack(uint32_t stack);
    111     void                    setProjection(int orientation, const Rect& viewport, const Rect& frame);
    112 
    113     int                     getOrientation() const { return mOrientation; }
    114     uint32_t                getOrientationTransform() const;
    115     const Transform&        getTransform() const { return mGlobalTransform; }
    116     const Rect              getViewport() const { return mViewport; }
    117     const Rect              getFrame() const { return mFrame; }
    118     const Rect&             getScissor() const { return mScissor; }
    119     bool                    needsFiltering() const { return mNeedsFiltering; }
    120 
    121     uint32_t                getLayerStack() const { return mLayerStack; }
    122     int32_t                 getDisplayType() const { return mType; }
    123     int32_t                 getHwcDisplayId() const { return mHwcDisplayId; }
    124     const wp<IBinder>&      getDisplayToken() const { return mDisplayToken; }
    125 
    126     status_t beginFrame() const;
    127     status_t prepareFrame(const HWComposer& hwc) const;
    128 
    129     void swapBuffers(HWComposer& hwc) const;
    130     status_t compositionComplete() const;
    131 
    132     // called after h/w composer has completed its set() call
    133     void onSwapBuffersCompleted(HWComposer& hwc) const;
    134 
    135     Rect getBounds() const {
    136         return Rect(mDisplayWidth, mDisplayHeight);
    137     }
    138     inline Rect bounds() const { return getBounds(); }
    139 
    140     void setDisplayName(const String8& displayName);
    141     const String8& getDisplayName() const { return mDisplayName; }
    142 
    143     EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
    144     void setViewportAndProjection() const;
    145 
    146     /* ------------------------------------------------------------------------
    147      * blank / unblank management
    148      */
    149     void releaseScreen() const;
    150     void acquireScreen() const;
    151     bool isScreenAcquired() const;
    152     bool canDraw() const;
    153 
    154     // release HWC resources (if any) for removable displays
    155     void disconnect(HWComposer& hwc);
    156 
    157     /* ------------------------------------------------------------------------
    158      * Debugging
    159      */
    160     uint32_t getPageFlipCount() const;
    161     void dump(String8& result) const;
    162 
    163 private:
    164     /*
    165      *  Constants, set during initialization
    166      */
    167     sp<SurfaceFlinger> mFlinger;
    168     DisplayType mType;
    169     int32_t mHwcDisplayId;
    170     wp<IBinder> mDisplayToken;
    171 
    172     // ANativeWindow this display is rendering into
    173     sp<ANativeWindow> mNativeWindow;
    174     sp<DisplaySurface> mDisplaySurface;
    175 
    176     EGLDisplay      mDisplay;
    177     EGLSurface      mSurface;
    178     int             mDisplayWidth;
    179     int             mDisplayHeight;
    180     PixelFormat     mFormat;
    181     uint32_t        mFlags;
    182     mutable uint32_t mPageFlipCount;
    183     String8         mDisplayName;
    184     bool            mIsSecure;
    185 
    186     /*
    187      * Can only accessed from the main thread, these members
    188      * don't need synchronization.
    189      */
    190 
    191     // list of visible layers on that display
    192     Vector< sp<Layer> > mVisibleLayersSortedByZ;
    193 
    194     // Whether we have a visible secure layer on this display
    195     bool mSecureLayerVisible;
    196 
    197     // Whether the screen is blanked;
    198     mutable int mScreenAcquired;
    199 
    200 
    201     /*
    202      * Transaction state
    203      */
    204     static status_t orientationToTransfrom(int orientation,
    205             int w, int h, Transform* tr);
    206 
    207     uint32_t mLayerStack;
    208     int mOrientation;
    209     // user-provided visible area of the layer stack
    210     Rect mViewport;
    211     // user-provided rectangle where mViewport gets mapped to
    212     Rect mFrame;
    213     // pre-computed scissor to apply to the display
    214     Rect mScissor;
    215     Transform mGlobalTransform;
    216     bool mNeedsFiltering;
    217 };
    218 
    219 }; // namespace android
    220 
    221 #endif // ANDROID_DISPLAY_DEVICE_H
    222