1 // Copyright 2013 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.
5 #ifndef UI_GFX_OZONE_DRI_HARDWARE_DISPLAY_CONTROLLER_H_
6 #define UI_GFX_OZONE_DRI_HARDWARE_DISPLAY_CONTROLLER_H_
10 #include <xf86drmMode.h>
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "ui/gfx/gfx_export.h"
16 #include "ui/gfx/ozone/dri/dri_wrapper.h"
23 // The HDCOz will handle modesettings and scannout operations for hardware
26 // In the DRM world there are 3 components that need to be paired up to be able
27 // to display an image to the monitor: CRTC (cathode ray tube controller),
28 // encoder and connector. The CRTC determines which framebuffer to read, when
29 // to scanout and where to scanout. Encoders converts the stream from the CRTC
30 // to the appropriate format for the connector. The connector is the physical
31 // connection that monitors connect to.
33 // There is no 1:1:1 pairing for these components. It is possible for an encoder
34 // to be compatible to multiple CRTCs and each connector can be used with
35 // multiple encoders. In addition, it is possible to use one CRTC with multiple
36 // connectors such that we can display the same image on multiple monitors.
38 // For example, the following configuration shows 2 different screens being
39 // initialized separately.
40 // ------------- -------------
41 // | Connector | | Connector |
43 // ------------- -------------
46 // ------------- -------------
47 // | Encoder1 | | Encoder2 |
48 // ------------- -------------
51 // ------------- -------------
52 // | CRTC1 | | CRTC2 |
53 // ------------- -------------
55 // In the following configuration 2 different screens are associated with the
56 // same CRTC, so on scanout the same framebuffer will be displayed on both
58 // ------------- -------------
59 // | Connector | | Connector |
61 // ------------- -------------
64 // ------------- -------------
65 // | Encoder1 | | Encoder2 |
66 // ------------- -------------
69 // ----------------------
71 // ----------------------
73 // Note that it is possible to have more connectors than CRTCs which means that
74 // only a subset of connectors can be active independently, showing different
75 // framebuffers. Though, in this case, it would be possible to have all
76 // connectors active if some use the same CRTC to mirror the display.
78 // TODO(dnicoara) Need to have a way to detect events (such as monitor
79 // connected or disconnected).
80 class GFX_EXPORT HardwareDisplayController {
82 // Controller states. The state transitions will happen from top to bottom.
84 // When we allocate a HDCO as a stub. At this point there is no connector
85 // and CRTC associated with this device.
88 // When |SetControllerInfo| is called and the HDCO has the information of
89 // the hardware it will control. At this point it knows everything it needs
90 // to control the hardware but doesn't have a surface.
93 // A surface is associated with the HDCO. This means that the controller can
94 // potentially display the backing surface to the display. Though the
95 // surface framebuffer still needs to be registered with the CRTC.
98 // The CRTC now knows about the surface attributes.
101 // Error state if any of the initialization steps fail.
105 HardwareDisplayController();
107 ~HardwareDisplayController();
109 // Set the hardware configuration for this HDCO. Once this is set, the HDCO is
110 // responsible for keeping track of the connector and CRTC and cleaning up
111 // when it is destroyed.
112 void SetControllerInfo(DriWrapper* drm,
113 uint32_t connector_id,
115 uint32_t dpms_property_id,
116 drmModeModeInfo mode);
118 // Associate the HDCO with a surface implementation and initialize it.
119 bool BindSurfaceToController(scoped_ptr<DriSurface> surface);
121 // Schedules the |surface_|'s framebuffer to be displayed on the next vsync
122 // event. The event will be posted on the graphics card file descriptor |fd_|
123 // and it can be read and processed by |drmHandleEvent|. That function can
124 // define the callback for the page flip event. A generic data argument will
125 // be presented to the callback. We use that argument to pass in the HDCO
126 // object the event belongs to.
128 // Between this call and the callback, the framebuffer used in this call
129 // should not be modified in any way as it would cause screen tearing if the
130 // hardware performed the flip. Note that the frontbuffer should also not
131 // be modified as it could still be displayed.
133 // Note that this function does not block. Also, this function should not be
134 // called again before the page flip occurrs.
136 // Returns true if the page flip was successfully registered, false otherwise.
137 bool SchedulePageFlip();
139 // Called when the page flip event occurred. The event is provided by the
140 // kernel when a VBlank event finished. This allows the controller to
141 // update internal state and propagate the update to the surface.
142 // The tuple (seconds, useconds) represents the event timestamp. |seconds|
143 // represents the number of seconds while |useconds| represents the
144 // microseconds (< 1 second) in the timestamp.
145 void OnPageFlipEvent(unsigned int frame,
146 unsigned int seconds,
147 unsigned int useconds);
149 // Set the hardware cursor to show the contents of |surface|.
150 bool SetCursor(DriSurface* surface);
154 // Moves the hardware cursor to |location|.
155 bool MoveCursor(const gfx::Point& location);
157 State get_state() const { return state_; };
159 int get_fd() const { return drm_->get_fd(); };
161 const drmModeModeInfo& get_mode() const { return mode_; };
163 DriSurface* get_surface() const { return surface_.get(); };
165 uint64_t get_time_of_last_flip() const {
166 return time_of_last_flip_;
170 // Object containing the connection to the graphics device and wraps the API
171 // calls to control it.
174 // TODO(dnicoara) Need to allow a CRTC to have multiple connectors.
175 uint32_t connector_id_;
179 uint32_t dpms_property_id_;
181 // TODO(dnicoara) Need to store all the modes.
182 drmModeModeInfo mode_;
184 // Saved CRTC state from before we used it. Need it to restore state once we
185 // are finished using this device.
186 drmModeCrtc* saved_crtc_;
190 scoped_ptr<DriSurface> surface_;
192 uint64_t time_of_last_flip_;
194 DISALLOW_COPY_AND_ASSIGN(HardwareDisplayController);
199 #endif // UI_GFX_OZONE_DRI_HARDWARE_DISPLAY_CONTROLLER_H_