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 "Transform.h"
     21 
     22 #include <stdlib.h>
     23 
     24 #ifndef USE_HWC2
     25 #include <ui/PixelFormat.h>
     26 #endif
     27 #include <ui/Region.h>
     28 
     29 #include <EGL/egl.h>
     30 #include <EGL/eglext.h>
     31 
     32 #ifdef USE_HWC2
     33 #include <binder/IBinder.h>
     34 #include <utils/RefBase.h>
     35 #endif
     36 #include <utils/Mutex.h>
     37 #include <utils/String8.h>
     38 #include <utils/Timers.h>
     39 
     40 #include <hardware/hwcomposer_defs.h>
     41 
     42 #ifdef USE_HWC2
     43 #include <memory>
     44 #endif
     45 
     46 struct ANativeWindow;
     47 
     48 namespace android {
     49 
     50 struct DisplayInfo;
     51 class DisplaySurface;
     52 class Fence;
     53 class IGraphicBufferProducer;
     54 class Layer;
     55 class SurfaceFlinger;
     56 class HWComposer;
     57 
     58 class DisplayDevice : public LightRefBase<DisplayDevice>
     59 {
     60 public:
     61     // region in layer-stack space
     62     mutable Region dirtyRegion;
     63     // region in screen space
     64     mutable Region swapRegion;
     65     // region in screen space
     66     Region undefinedRegion;
     67     bool lastCompositionHadVisibleLayers;
     68 
     69     enum DisplayType {
     70         DISPLAY_ID_INVALID = -1,
     71         DISPLAY_PRIMARY     = HWC_DISPLAY_PRIMARY,
     72         DISPLAY_EXTERNAL    = HWC_DISPLAY_EXTERNAL,
     73         DISPLAY_VIRTUAL     = HWC_DISPLAY_VIRTUAL,
     74         NUM_BUILTIN_DISPLAY_TYPES = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
     75     };
     76 
     77     enum {
     78         PARTIAL_UPDATES = 0x00020000, // video driver feature
     79         SWAP_RECTANGLE  = 0x00080000,
     80     };
     81 
     82     enum {
     83         NO_LAYER_STACK = 0xFFFFFFFF,
     84     };
     85 
     86     // clang-format off
     87     DisplayDevice(
     88             const sp<SurfaceFlinger>& flinger,
     89             DisplayType type,
     90             int32_t hwcId,
     91 #ifndef USE_HWC2
     92             int format,
     93 #endif
     94             bool isSecure,
     95             const wp<IBinder>& displayToken,
     96             const sp<DisplaySurface>& displaySurface,
     97             const sp<IGraphicBufferProducer>& producer,
     98             EGLConfig config,
     99             bool supportWideColor);
    100     // clang-format on
    101 
    102     ~DisplayDevice();
    103 
    104     // whether this is a valid object. An invalid DisplayDevice is returned
    105     // when an non existing id is requested
    106     bool isValid() const;
    107 
    108     // isSecure indicates whether this display can be trusted to display
    109     // secure surfaces.
    110     bool isSecure() const { return mIsSecure; }
    111 
    112     // Flip the front and back buffers if the back buffer is "dirty".  Might
    113     // be instantaneous, might involve copying the frame buffer around.
    114     void flip(const Region& dirty) const;
    115 
    116     int         getWidth() const;
    117     int         getHeight() const;
    118 #ifndef USE_HWC2
    119     PixelFormat getFormat() const;
    120 #endif
    121     uint32_t    getFlags() const;
    122 
    123     EGLSurface  getEGLSurface() const;
    124 
    125     void                    setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
    126     const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
    127     Region                  getDirtyRegion(bool repaintEverything) const;
    128 
    129     void                    setLayerStack(uint32_t stack);
    130     void                    setDisplaySize(const int newWidth, const int newHeight);
    131     void                    setProjection(int orientation, const Rect& viewport, const Rect& frame);
    132 
    133     int                     getOrientation() const { return mOrientation; }
    134     uint32_t                getOrientationTransform() const;
    135     static uint32_t         getPrimaryDisplayOrientationTransform();
    136     const Transform&        getTransform() const { return mGlobalTransform; }
    137     const Rect              getViewport() const { return mViewport; }
    138     const Rect              getFrame() const { return mFrame; }
    139     const Rect&             getScissor() const { return mScissor; }
    140     bool                    needsFiltering() const { return mNeedsFiltering; }
    141 
    142     uint32_t                getLayerStack() const { return mLayerStack; }
    143     int32_t                 getDisplayType() const { return mType; }
    144     int32_t                 getHwcDisplayId() const { return mHwcDisplayId; }
    145     const wp<IBinder>&      getDisplayToken() const { return mDisplayToken; }
    146 
    147     // We pass in mustRecompose so we can keep VirtualDisplaySurface's state
    148     // machine happy without actually queueing a buffer if nothing has changed
    149     status_t beginFrame(bool mustRecompose) const;
    150 #ifdef USE_HWC2
    151     status_t prepareFrame(HWComposer& hwc);
    152     bool getWideColorSupport() const { return mDisplayHasWideColor; }
    153 #else
    154     status_t prepareFrame(const HWComposer& hwc) const;
    155 #endif
    156 
    157     void swapBuffers(HWComposer& hwc) const;
    158 #ifndef USE_HWC2
    159     status_t compositionComplete() const;
    160 #endif
    161 
    162     // called after h/w composer has completed its set() call
    163 #ifdef USE_HWC2
    164     void onSwapBuffersCompleted() const;
    165 #else
    166     void onSwapBuffersCompleted(HWComposer& hwc) const;
    167 #endif
    168 
    169     Rect getBounds() const {
    170         return Rect(mDisplayWidth, mDisplayHeight);
    171     }
    172     inline Rect bounds() const { return getBounds(); }
    173 
    174     void setDisplayName(const String8& displayName);
    175     const String8& getDisplayName() const { return mDisplayName; }
    176 
    177     EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const;
    178     void setViewportAndProjection() const;
    179 
    180     const sp<Fence>& getClientTargetAcquireFence() const;
    181 
    182     /* ------------------------------------------------------------------------
    183      * Display power mode management.
    184      */
    185     int getPowerMode() const;
    186     void setPowerMode(int mode);
    187     bool isDisplayOn() const;
    188 
    189 #ifdef USE_HWC2
    190     android_color_mode_t getActiveColorMode() const;
    191     void setActiveColorMode(android_color_mode_t mode);
    192 #endif
    193 
    194     /* ------------------------------------------------------------------------
    195      * Display active config management.
    196      */
    197     int getActiveConfig() const;
    198     void setActiveConfig(int mode);
    199 
    200     // release HWC resources (if any) for removable displays
    201     void disconnect(HWComposer& hwc);
    202 
    203     /* ------------------------------------------------------------------------
    204      * Debugging
    205      */
    206     uint32_t getPageFlipCount() const;
    207     void dump(String8& result) const;
    208 
    209 private:
    210     /*
    211      *  Constants, set during initialization
    212      */
    213     sp<SurfaceFlinger> mFlinger;
    214     DisplayType mType;
    215     int32_t mHwcDisplayId;
    216     wp<IBinder> mDisplayToken;
    217 
    218     // ANativeWindow this display is rendering into
    219     sp<ANativeWindow> mNativeWindow;
    220     sp<DisplaySurface> mDisplaySurface;
    221 
    222     EGLConfig       mConfig;
    223     EGLDisplay      mDisplay;
    224     EGLSurface      mSurface;
    225     int             mDisplayWidth;
    226     int             mDisplayHeight;
    227 #ifndef USE_HWC2
    228     PixelFormat     mFormat;
    229 #endif
    230     uint32_t        mFlags;
    231     mutable uint32_t mPageFlipCount;
    232     String8         mDisplayName;
    233     bool            mIsSecure;
    234 
    235     /*
    236      * Can only accessed from the main thread, these members
    237      * don't need synchronization.
    238      */
    239 
    240     // list of visible layers on that display
    241     Vector< sp<Layer> > mVisibleLayersSortedByZ;
    242 
    243     /*
    244      * Transaction state
    245      */
    246     static status_t orientationToTransfrom(int orientation,
    247             int w, int h, Transform* tr);
    248 
    249     // The identifier of the active layer stack for this display. Several displays
    250     // can use the same layer stack: A z-ordered group of layers (sometimes called
    251     // "surfaces"). Any given layer can only be on a single layer stack.
    252     uint32_t mLayerStack;
    253 
    254     int mOrientation;
    255     static uint32_t sPrimaryDisplayOrientation;
    256     // user-provided visible area of the layer stack
    257     Rect mViewport;
    258     // user-provided rectangle where mViewport gets mapped to
    259     Rect mFrame;
    260     // pre-computed scissor to apply to the display
    261     Rect mScissor;
    262     Transform mGlobalTransform;
    263     bool mNeedsFiltering;
    264     // Current power mode
    265     int mPowerMode;
    266     // Current active config
    267     int mActiveConfig;
    268 #ifdef USE_HWC2
    269     // current active color mode
    270     android_color_mode_t mActiveColorMode;
    271 
    272     // Need to know if display is wide-color capable or not.
    273     // Initialized by SurfaceFlinger when the DisplayDevice is created.
    274     // Fed to RenderEngine during composition.
    275     bool mDisplayHasWideColor;
    276 #endif
    277 };
    278 
    279 struct DisplayDeviceState {
    280     DisplayDeviceState() = default;
    281     DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure);
    282 
    283     bool isValid() const { return type >= 0; }
    284     bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
    285     bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
    286 
    287     static std::atomic<int32_t> nextDisplayId;
    288     int32_t displayId = nextDisplayId++;
    289     DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID;
    290     sp<IGraphicBufferProducer> surface;
    291     uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
    292     Rect viewport;
    293     Rect frame;
    294     uint8_t orientation = 0;
    295     uint32_t width = 0;
    296     uint32_t height = 0;
    297     String8 displayName;
    298     bool isSecure = false;
    299 };
    300 
    301 }; // namespace android
    302 
    303 #endif // ANDROID_DISPLAY_DEVICE_H
    304