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.
5 #ifndef UI_OZONE_PLATFORM_DRM_GPU_DRM_DEVICE_H_
6 #define UI_OZONE_PLATFORM_DRM_GPU_DRM_DEVICE_H_
12 #include "base/callback.h"
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_vector.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/rect_f.h"
20 #include "ui/gfx/overlay_transform.h"
21 #include "ui/ozone/ozone_export.h"
22 #include "ui/ozone/platform/drm/common/scoped_drm_types.h"
23 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager.h"
25 typedef struct _drmEventContext drmEventContext
;
26 typedef struct _drmModeModeInfo drmModeModeInfo
;
31 class SingleThreadTaskRunner
;
36 class HardwareDisplayPlaneManager
;
37 struct GammaRampRGBEntry
;
39 // Wraps DRM calls into a nice interface. Used to provide different
40 // implementations of the DRM calls. For the actual implementation the DRM API
41 // would be called. In unit tests this interface would be stubbed.
42 class OZONE_EXPORT DrmDevice
: public base::RefCountedThreadSafe
<DrmDevice
> {
44 typedef base::Callback
<void(unsigned int /* frame */,
45 unsigned int /* seconds */,
46 unsigned int /* useconds */)> PageFlipCallback
;
48 DrmDevice(const base::FilePath
& device_path
, base::File file
);
51 virtual bool Initialize(bool use_atomic
);
53 // |task_runner| will be used to asynchronously page flip.
54 virtual void InitializeTaskRunner(
55 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
);
57 // Get the CRTC state. This is generally used to save state before using the
58 // CRTC. When the user finishes using the CRTC, the user should restore the
59 // CRTC to it's initial state. Use |SetCrtc| to restore the state.
60 virtual ScopedDrmCrtcPtr
GetCrtc(uint32_t crtc_id
);
62 // Used to configure CRTC with ID |crtc_id| to use the connector in
63 // |connectors|. The CRTC will be configured with mode |mode| and will display
64 // the framebuffer with ID |framebuffer|. Before being able to display the
65 // framebuffer, it should be registered with the CRTC using |AddFramebuffer|.
66 virtual bool SetCrtc(uint32_t crtc_id
,
68 std::vector
<uint32_t> connectors
,
69 drmModeModeInfo
* mode
);
71 // Used to set a specific configuration to the CRTC. Normally this function
72 // would be called with a CRTC saved state (from |GetCrtc|) to restore it to
73 // its original configuration.
74 virtual bool SetCrtc(drmModeCrtc
* crtc
, std::vector
<uint32_t> connectors
);
76 virtual bool DisableCrtc(uint32_t crtc_id
);
78 // Returns the connector properties for |connector_id|.
79 virtual ScopedDrmConnectorPtr
GetConnector(uint32_t connector_id
);
81 // Register a buffer with the CRTC. On successful registration, the CRTC will
82 // assign a framebuffer ID to |framebuffer|.
83 virtual bool AddFramebuffer(uint32_t width
,
89 uint32_t* framebuffer
);
91 // Deregister the given |framebuffer|.
92 virtual bool RemoveFramebuffer(uint32_t framebuffer
);
94 // Get the DRM details associated with |framebuffer|.
95 virtual ScopedDrmFramebufferPtr
GetFramebuffer(uint32_t framebuffer
);
97 // Schedules a pageflip for CRTC |crtc_id|. This function will return
98 // immediately. Upon completion of the pageflip event, the CRTC will be
99 // displaying the buffer with ID |framebuffer| and will have a DRM event
101 virtual bool PageFlip(uint32_t crtc_id
,
102 uint32_t framebuffer
,
104 const PageFlipCallback
& callback
);
106 // Schedule an overlay to be show during the page flip for CRTC |crtc_id|.
107 // |source| location from |framebuffer| will be shown on overlay
108 // |overlay_plane|, in the bounds specified by |location| on the screen.
109 virtual bool PageFlipOverlay(uint32_t crtc_id
,
110 uint32_t framebuffer
,
111 const gfx::Rect
& location
,
112 const gfx::Rect
& source
,
115 // Returns the property with name |name| associated with |connector|. Returns
116 // NULL if property not found. If the returned value is valid, it must be
117 // released using FreeProperty().
118 virtual ScopedDrmPropertyPtr
GetProperty(drmModeConnector
* connector
,
121 // Sets the value of property with ID |property_id| to |value|. The property
122 // is applied to the connector with ID |connector_id|.
123 virtual bool SetProperty(uint32_t connector_id
,
124 uint32_t property_id
,
127 // Can be used to query device/driver |capability|. Sets the value of
128 // |capability to |value|. Returns true in case of a succesful query.
129 virtual bool GetCapability(uint64_t capability
, uint64_t* value
);
131 // Return a binary blob associated with |connector|. The binary blob is
132 // associated with the property with name |name|. Return NULL if the property
133 // could not be found or if the property does not have a binary blob. If valid
134 // the returned object must be freed using FreePropertyBlob().
135 virtual ScopedDrmPropertyBlobPtr
GetPropertyBlob(drmModeConnector
* connector
,
138 // Set the cursor to be displayed in CRTC |crtc_id|. (width, height) is the
139 // cursor size pointed by |handle|.
140 virtual bool SetCursor(uint32_t crtc_id
,
142 const gfx::Size
& size
);
144 // Move the cursor on CRTC |crtc_id| to (x, y);
145 virtual bool MoveCursor(uint32_t crtc_id
, const gfx::Point
& point
);
147 virtual bool CreateDumbBuffer(const SkImageInfo
& info
,
151 virtual bool DestroyDumbBuffer(uint32_t handle
);
153 virtual bool MapDumbBuffer(uint32_t handle
, size_t size
, void** pixels
);
155 virtual bool UnmapDumbBuffer(void* pixels
, size_t size
);
157 virtual bool CloseBufferHandle(uint32_t handle
);
159 virtual bool CommitProperties(drmModePropertySet
* properties
,
163 const PageFlipCallback
& callback
);
165 // Set the gamma ramp for |crtc_id| to reflect the ramps in |lut|.
166 virtual bool SetGammaRamp(uint32_t crtc_id
,
167 const std::vector
<GammaRampRGBEntry
>& lut
);
169 virtual bool SetCapability(uint64_t capability
, uint64_t value
);
171 // Drm master related
172 virtual bool SetMaster();
173 virtual bool DropMaster();
175 int get_fd() const { return file_
.GetPlatformFile(); }
177 base::FilePath
device_path() const { return device_path_
; }
179 HardwareDisplayPlaneManager
* plane_manager() { return plane_manager_
.get(); }
182 friend class base::RefCountedThreadSafe
<DrmDevice
>;
184 virtual ~DrmDevice();
186 scoped_ptr
<HardwareDisplayPlaneManager
> plane_manager_
;
190 class PageFlipManager
;
192 // Path to DRM device.
193 const base::FilePath device_path_
;
198 // Helper thread to perform IO listener operations.
199 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
201 // Watcher for |fd_| listening for page flip events.
202 scoped_refptr
<IOWatcher
> watcher_
;
204 scoped_refptr
<PageFlipManager
> page_flip_manager_
;
206 DISALLOW_COPY_AND_ASSIGN(DrmDevice
);
211 #endif // UI_OZONE_PLATFORM_DRM_GPU_DRM_DEVICE_H_