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. isSecure()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 getOrientation()133 int getOrientation() const { return mOrientation; } 134 uint32_t getOrientationTransform() const; 135 static uint32_t getPrimaryDisplayOrientationTransform(); getTransform()136 const Transform& getTransform() const { return mGlobalTransform; } getViewport()137 const Rect getViewport() const { return mViewport; } getFrame()138 const Rect getFrame() const { return mFrame; } getScissor()139 const Rect& getScissor() const { return mScissor; } needsFiltering()140 bool needsFiltering() const { return mNeedsFiltering; } 141 getLayerStack()142 uint32_t getLayerStack() const { return mLayerStack; } getDisplayType()143 int32_t getDisplayType() const { return mType; } isPrimary()144 bool isPrimary() const { return mType == DISPLAY_PRIMARY; } getHwcDisplayId()145 int32_t getHwcDisplayId() const { return mHwcDisplayId; } getDisplayToken()146 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; } 147 148 // We pass in mustRecompose so we can keep VirtualDisplaySurface's state 149 // machine happy without actually queueing a buffer if nothing has changed 150 status_t beginFrame(bool mustRecompose) const; 151 #ifdef USE_HWC2 152 status_t prepareFrame(HWComposer& hwc); getWideColorSupport()153 bool getWideColorSupport() const { return mDisplayHasWideColor; } 154 #else 155 status_t prepareFrame(const HWComposer& hwc) const; 156 #endif 157 158 void swapBuffers(HWComposer& hwc) const; 159 #ifndef USE_HWC2 160 status_t compositionComplete() const; 161 #endif 162 163 // called after h/w composer has completed its set() call 164 #ifdef USE_HWC2 165 void onSwapBuffersCompleted() const; 166 #else 167 void onSwapBuffersCompleted(HWComposer& hwc) const; 168 #endif 169 getBounds()170 Rect getBounds() const { 171 return Rect(mDisplayWidth, mDisplayHeight); 172 } bounds()173 inline Rect bounds() const { return getBounds(); } 174 175 void setDisplayName(const String8& displayName); getDisplayName()176 const String8& getDisplayName() const { return mDisplayName; } 177 178 EGLBoolean makeCurrent(EGLDisplay dpy, EGLContext ctx) const; 179 void setViewportAndProjection() const; 180 181 const sp<Fence>& getClientTargetAcquireFence() const; 182 183 /* ------------------------------------------------------------------------ 184 * Display power mode management. 185 */ 186 int getPowerMode() const; 187 void setPowerMode(int mode); 188 bool isDisplayOn() const; 189 190 #ifdef USE_HWC2 191 android_color_mode_t getActiveColorMode() const; 192 void setActiveColorMode(android_color_mode_t mode); 193 void setCompositionDataSpace(android_dataspace dataspace); 194 #endif 195 196 /* ------------------------------------------------------------------------ 197 * Display active config management. 198 */ 199 int getActiveConfig() const; 200 void setActiveConfig(int mode); 201 202 // release HWC resources (if any) for removable displays 203 void disconnect(HWComposer& hwc); 204 205 /* ------------------------------------------------------------------------ 206 * Debugging 207 */ 208 uint32_t getPageFlipCount() const; 209 void dump(String8& result) const; 210 211 private: 212 /* 213 * Constants, set during initialization 214 */ 215 sp<SurfaceFlinger> mFlinger; 216 DisplayType mType; 217 int32_t mHwcDisplayId; 218 wp<IBinder> mDisplayToken; 219 220 // ANativeWindow this display is rendering into 221 sp<ANativeWindow> mNativeWindow; 222 sp<DisplaySurface> mDisplaySurface; 223 224 EGLConfig mConfig; 225 EGLDisplay mDisplay; 226 EGLSurface mSurface; 227 int mDisplayWidth; 228 int mDisplayHeight; 229 #ifndef USE_HWC2 230 PixelFormat mFormat; 231 #endif 232 uint32_t mFlags; 233 mutable uint32_t mPageFlipCount; 234 String8 mDisplayName; 235 bool mIsSecure; 236 237 /* 238 * Can only accessed from the main thread, these members 239 * don't need synchronization. 240 */ 241 242 // list of visible layers on that display 243 Vector< sp<Layer> > mVisibleLayersSortedByZ; 244 245 /* 246 * Transaction state 247 */ 248 static status_t orientationToTransfrom(int orientation, 249 int w, int h, Transform* tr); 250 251 // The identifier of the active layer stack for this display. Several displays 252 // can use the same layer stack: A z-ordered group of layers (sometimes called 253 // "surfaces"). Any given layer can only be on a single layer stack. 254 uint32_t mLayerStack; 255 256 int mOrientation; 257 static uint32_t sPrimaryDisplayOrientation; 258 // user-provided visible area of the layer stack 259 Rect mViewport; 260 // user-provided rectangle where mViewport gets mapped to 261 Rect mFrame; 262 // pre-computed scissor to apply to the display 263 Rect mScissor; 264 Transform mGlobalTransform; 265 bool mNeedsFiltering; 266 // Current power mode 267 int mPowerMode; 268 // Current active config 269 int mActiveConfig; 270 #ifdef USE_HWC2 271 // current active color mode 272 android_color_mode_t mActiveColorMode; 273 274 // Need to know if display is wide-color capable or not. 275 // Initialized by SurfaceFlinger when the DisplayDevice is created. 276 // Fed to RenderEngine during composition. 277 bool mDisplayHasWideColor; 278 #endif 279 }; 280 281 struct DisplayDeviceState { 282 DisplayDeviceState() = default; 283 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure); 284 isValidDisplayDeviceState285 bool isValid() const { return type >= 0; } isMainDisplayDisplayDeviceState286 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } isVirtualDisplayDisplayDeviceState287 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 288 289 static std::atomic<int32_t> nextDisplayId; 290 int32_t displayId = nextDisplayId++; 291 DisplayDevice::DisplayType type = DisplayDevice::DISPLAY_ID_INVALID; 292 sp<IGraphicBufferProducer> surface; 293 uint32_t layerStack = DisplayDevice::NO_LAYER_STACK; 294 Rect viewport; 295 Rect frame; 296 uint8_t orientation = 0; 297 uint32_t width = 0; 298 uint32_t height = 0; 299 String8 displayName; 300 bool isSecure = false; 301 }; 302 303 }; // namespace android 304 305 #endif // ANDROID_DISPLAY_DEVICE_H 306