1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_OZONE_PLATFORM_DRM_GPU_SCREEN_MANAGER_H_
6 #define UI_OZONE_PLATFORM_DRM_GPU_SCREEN_MANAGER_H_
7 
8 #include <stdint.h>
9 #include <memory>
10 #include <unordered_map>
11 
12 #include "base/macros.h"
13 #include "base/observer_list.h"
14 #include "ui/gfx/native_widget_types.h"
15 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
16 
17 typedef struct _drmModeModeInfo drmModeModeInfo;
18 
19 namespace gfx {
20 class Point;
21 class Rect;
22 }  // namespace gfx
23 
24 namespace ui {
25 
26 class DrmDevice;
27 class DrmWindow;
28 
29 // Responsible for keeping track of active displays and configuring them.
30 class ScreenManager {
31  public:
32   ScreenManager();
33   virtual ~ScreenManager();
34 
35   // Register a display controller. This must be called before trying to
36   // configure it.
37   void AddDisplayController(const scoped_refptr<DrmDevice>& drm,
38                             uint32_t crtc,
39                             uint32_t connector);
40 
41   // Remove a display controller from the list of active controllers. The
42   // controller is removed since it was disconnected.
43   void RemoveDisplayController(const scoped_refptr<DrmDevice>& drm,
44                                uint32_t crtc);
45 
46   // Configure a display controller. The display controller is identified by
47   // (|crtc|, |connector|) and the controller is modeset using |mode|.
48   bool ConfigureDisplayController(const scoped_refptr<DrmDevice>& drm,
49                                   uint32_t crtc,
50                                   uint32_t connector,
51                                   const gfx::Point& origin,
52                                   const drmModeModeInfo& mode);
53 
54   // Disable the display controller identified by |crtc|. Note, the controller
55   // may still be connected, so this does not remove the controller.
56   bool DisableDisplayController(const scoped_refptr<DrmDevice>& drm,
57                                 uint32_t crtc);
58 
59   // Returns a reference to the display controller configured to display within
60   // |bounds|. If the caller caches the controller it must also register as an
61   // observer to be notified when the controller goes out of scope.
62   HardwareDisplayController* GetDisplayController(const gfx::Rect& bounds);
63 
64   // Adds a window for |widget|. Note: |widget| should not be associated with a
65   // window when calling this function.
66   void AddWindow(gfx::AcceleratedWidget widget,
67                  std::unique_ptr<DrmWindow> window);
68 
69   // Removes the window for |widget|. Note: |widget| must have a window
70   // associated with it when calling this function.
71   std::unique_ptr<DrmWindow> RemoveWindow(gfx::AcceleratedWidget widget);
72 
73   // Returns the window associated with |widget|. Note: This function should be
74   // called only if a valid window has been associated with |widget|.
75   DrmWindow* GetWindow(gfx::AcceleratedWidget widget);
76 
77   // Updates the mapping between display controllers and windows such that a
78   // controller will be associated with at most one window.
79   void UpdateControllerToWindowMapping();
80 
81  private:
82   using HardwareDisplayControllers =
83       std::vector<std::unique_ptr<HardwareDisplayController>>;
84   using WidgetToWindowMap =
85       std::unordered_map<gfx::AcceleratedWidget, std::unique_ptr<DrmWindow>>;
86 
87   // Returns an iterator into |controllers_| for the controller identified by
88   // (|crtc|, |connector|).
89   HardwareDisplayControllers::iterator FindDisplayController(
90       const scoped_refptr<DrmDevice>& drm,
91       uint32_t crtc);
92 
93   bool ActualConfigureDisplayController(const scoped_refptr<DrmDevice>& drm,
94                                         uint32_t crtc,
95                                         uint32_t connector,
96                                         const gfx::Point& origin,
97                                         const drmModeModeInfo& mode);
98 
99   // Returns an iterator into |controllers_| for the controller located at
100   // |origin|.
101   HardwareDisplayControllers::iterator FindActiveDisplayControllerByLocation(
102       const gfx::Rect& bounds);
103 
104   // Returns an iterator into |controllers_| for the controller located at
105   // |origin| with matching DRM device.
106   HardwareDisplayControllers::iterator FindActiveDisplayControllerByLocation(
107       const scoped_refptr<DrmDevice>& drm,
108       const gfx::Rect& bounds);
109 
110   // Tries to set the controller identified by (|crtc|, |connector|) to mirror
111   // those in |mirror|. |original| is an iterator to the HDC where the
112   // controller is currently present.
113   bool HandleMirrorMode(HardwareDisplayControllers::iterator original,
114                         HardwareDisplayControllers::iterator mirror,
115                         const scoped_refptr<DrmDevice>& drm,
116                         uint32_t crtc,
117                         uint32_t connector,
118                         const drmModeModeInfo& mode);
119 
120   DrmOverlayPlane GetModesetBuffer(HardwareDisplayController* controller,
121                                    const gfx::Rect& bounds);
122 
123   bool EnableController(HardwareDisplayController* controller);
124 
125   // Modeset the |controller| using |origin| and |mode|. If there is a window at
126   // the controller location, then we'll re-use the current buffer.
127   bool ModesetController(HardwareDisplayController* controller,
128                          const gfx::Point& origin,
129                          const drmModeModeInfo& mode);
130 
131   DrmWindow* FindWindowAt(const gfx::Rect& bounds) const;
132 
133   // List of display controllers (active and disabled).
134   HardwareDisplayControllers controllers_;
135 
136   WidgetToWindowMap window_map_;
137 
138   DISALLOW_COPY_AND_ASSIGN(ScreenManager);
139 };
140 
141 }  // namespace ui
142 
143 #endif  // UI_OZONE_PLATFORM_DRM_GPU_SCREEN_MANAGER_H_
144