Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / ozone / dri / hardware_display_controller.h
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.
4
5 #ifndef UI_GFX_OZONE_DRI_HARDWARE_DISPLAY_CONTROLLER_H_
6 #define UI_GFX_OZONE_DRI_HARDWARE_DISPLAY_CONTROLLER_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <xf86drmMode.h>
11 #include <vector>
12
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"
17
18 namespace gfx {
19
20 class DriSurface;
21 class Point;
22
23 // The HDCOz will handle modesettings and scannout operations for hardware
24 // devices.
25 //
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.
32 //
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.
37 //
38 // For example, the following configuration shows 2 different screens being
39 // initialized separately.
40 // -------------      -------------
41 // | Connector |      | Connector |
42 // |   HDMI    |      |    VGA    |
43 // -------------      -------------
44 //       ^                  ^
45 //       |                  |
46 // -------------      -------------
47 // |  Encoder1  |     |  Encoder2 |
48 // -------------      -------------
49 //       ^                  ^
50 //       |                  |
51 // -------------      -------------
52 // |   CRTC1   |      |   CRTC2   |
53 // -------------      -------------
54 //
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
57 // monitors.
58 // -------------      -------------
59 // | Connector |      | Connector |
60 // |   HDMI    |      |    VGA    |
61 // -------------      -------------
62 //       ^                  ^
63 //       |                  |
64 // -------------      -------------
65 // |  Encoder1  |     |  Encoder2 |
66 // -------------      -------------
67 //       ^                  ^
68 //       |                  |
69 //      ----------------------
70 //      |        CRTC1       |
71 //      ----------------------
72 //
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.
77 //
78 // TODO(dnicoara) Need to have a way to detect events (such as monitor
79 // connected or disconnected).
80 class GFX_EXPORT HardwareDisplayController {
81  public:
82   // Controller states. The state transitions will happen from top to bottom.
83   enum State {
84     // When we allocate a HDCO as a stub. At this point there is no connector
85     // and CRTC associated with this device.
86     UNASSOCIATED,
87
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.
91     UNINITIALIZED,
92
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.
96     SURFACE_INITIALIZED,
97
98     // The CRTC now knows about the surface attributes.
99     INITIALIZED,
100
101     // Error state if any of the initialization steps fail.
102     FAILED,
103   };
104
105   HardwareDisplayController();
106
107   ~HardwareDisplayController();
108
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,
114                          uint32_t crtc_id,
115                          uint32_t dpms_property_id,
116                          drmModeModeInfo mode);
117
118   // Associate the HDCO with a surface implementation and initialize it.
119   bool BindSurfaceToController(scoped_ptr<DriSurface> surface);
120
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.
127   //
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.
132   //
133   // Note that this function does not block. Also, this function should not be
134   // called again before the page flip occurrs.
135   //
136   // Returns true if the page flip was successfully registered, false otherwise.
137   bool SchedulePageFlip();
138
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);
148
149   // Set the hardware cursor to show the contents of |surface|.
150   bool SetCursor(DriSurface* surface);
151
152   bool UnsetCursor();
153
154   // Moves the hardware cursor to |location|.
155   bool MoveCursor(const gfx::Point& location);
156
157   State get_state() const { return state_; };
158
159   int get_fd() const { return drm_->get_fd(); };
160
161   const drmModeModeInfo& get_mode() const { return mode_; };
162
163   DriSurface* get_surface() const { return surface_.get(); };
164
165   uint64_t get_time_of_last_flip() const {
166     return time_of_last_flip_;
167   };
168
169  private:
170   // Object containing the connection to the graphics device and wraps the API
171   // calls to control it.
172   DriWrapper* drm_;
173
174   // TODO(dnicoara) Need to allow a CRTC to have multiple connectors.
175   uint32_t connector_id_;
176
177   uint32_t crtc_id_;
178
179   uint32_t dpms_property_id_;
180
181   // TODO(dnicoara) Need to store all the modes.
182   drmModeModeInfo mode_;
183
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_;
187
188   State state_;
189
190   scoped_ptr<DriSurface> surface_;
191
192   uint64_t time_of_last_flip_;
193
194   DISALLOW_COPY_AND_ASSIGN(HardwareDisplayController);
195 };
196
197 }  // namespace gfx
198
199 #endif  // UI_GFX_OZONE_DRI_HARDWARE_DISPLAY_CONTROLLER_H_