Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / ozone / platform / drm / ozone_platform_gbm.cc
blob0a6e08cf5dd99776c60d077d60f09904c429c4dc
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 #include "ui/ozone/platform/drm/ozone_platform_gbm.h"
7 #include <dlfcn.h>
8 #include <gbm.h>
9 #include <stdlib.h>
11 #include "base/bind.h"
12 #include "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h"
13 #include "ui/events/ozone/device/device_manager.h"
14 #include "ui/events/ozone/evdev/event_factory_evdev.h"
15 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
16 #include "ui/ozone/platform/drm/common/drm_util.h"
17 #include "ui/ozone/platform/drm/gpu/drm_device_generator.h"
18 #include "ui/ozone/platform/drm/gpu/drm_device_manager.h"
19 #include "ui/ozone/platform/drm/gpu/drm_gpu_display_manager.h"
20 #include "ui/ozone/platform/drm/gpu/drm_gpu_platform_support.h"
21 #include "ui/ozone/platform/drm/gpu/gbm_buffer.h"
22 #include "ui/ozone/platform/drm/gpu/gbm_device.h"
23 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h"
24 #include "ui/ozone/platform/drm/gpu/scanout_buffer.h"
25 #include "ui/ozone/platform/drm/gpu/screen_manager.h"
26 #include "ui/ozone/platform/drm/host/drm_cursor.h"
27 #include "ui/ozone/platform/drm/host/drm_display_host_manager.h"
28 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h"
29 #include "ui/ozone/platform/drm/host/drm_native_display_delegate.h"
30 #include "ui/ozone/platform/drm/host/drm_overlay_manager.h"
31 #include "ui/ozone/platform/drm/host/drm_window_host.h"
32 #include "ui/ozone/platform/drm/host/drm_window_host_manager.h"
33 #include "ui/ozone/public/cursor_factory_ozone.h"
34 #include "ui/ozone/public/gpu_platform_support.h"
35 #include "ui/ozone/public/gpu_platform_support_host.h"
36 #include "ui/ozone/public/ozone_platform.h"
37 #include "ui/ozone/public/ozone_switches.h"
39 #if defined(USE_XKBCOMMON)
40 #include "ui/events/ozone/layout/xkb/xkb_evdev_codes.h"
41 #include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
42 #else
43 #include "ui/events/ozone/layout/stub/stub_keyboard_layout_engine.h"
44 #endif
46 #if defined(USE_VGEM_MAP)
47 #include <fcntl.h>
48 #endif
50 namespace ui {
52 namespace {
54 class GlApiLoader {
55 public:
56 GlApiLoader()
57 : glapi_lib_(dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL)) {}
59 ~GlApiLoader() {
60 if (glapi_lib_)
61 dlclose(glapi_lib_);
64 private:
65 // HACK: gbm drivers have broken linkage. The Mesa DRI driver references
66 // symbols in the libglapi library however it does not explicitly link against
67 // it. That caused linkage errors when running an application that does not
68 // explicitly link against libglapi.
69 void* glapi_lib_;
71 DISALLOW_COPY_AND_ASSIGN(GlApiLoader);
74 class GbmBufferGenerator : public ScanoutBufferGenerator {
75 public:
76 GbmBufferGenerator() {}
77 ~GbmBufferGenerator() override {}
79 // ScanoutBufferGenerator:
80 scoped_refptr<ScanoutBuffer> Create(const scoped_refptr<DrmDevice>& drm,
81 gfx::BufferFormat format,
82 const gfx::Size& size) override {
83 scoped_refptr<GbmDevice> gbm(static_cast<GbmDevice*>(drm.get()));
84 return GbmBuffer::CreateBuffer(gbm, format, size,
85 gfx::BufferUsage::SCANOUT);
88 protected:
89 DISALLOW_COPY_AND_ASSIGN(GbmBufferGenerator);
92 class GbmDeviceGenerator : public DrmDeviceGenerator {
93 public:
94 GbmDeviceGenerator(bool use_atomic) : use_atomic_(use_atomic) {}
95 ~GbmDeviceGenerator() override {}
97 // DrmDeviceGenerator:
98 scoped_refptr<DrmDevice> CreateDevice(const base::FilePath& path,
99 base::File file) override {
100 scoped_refptr<DrmDevice> drm = new GbmDevice(path, file.Pass());
101 if (drm->Initialize(use_atomic_))
102 return drm;
104 return nullptr;
107 private:
108 bool use_atomic_;
110 DISALLOW_COPY_AND_ASSIGN(GbmDeviceGenerator);
113 class OzonePlatformGbm : public OzonePlatform {
114 public:
115 OzonePlatformGbm() {}
116 ~OzonePlatformGbm() override {}
118 // OzonePlatform:
119 ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override {
120 return surface_factory_ozone_.get();
122 OverlayManagerOzone* GetOverlayManager() override {
123 return overlay_manager_.get();
125 CursorFactoryOzone* GetCursorFactoryOzone() override {
126 return cursor_factory_ozone_.get();
128 InputController* GetInputController() override {
129 return event_factory_ozone_->input_controller();
131 GpuPlatformSupport* GetGpuPlatformSupport() override {
132 return gpu_platform_support_.get();
134 GpuPlatformSupportHost* GetGpuPlatformSupportHost() override {
135 return gpu_platform_support_host_.get();
137 scoped_ptr<SystemInputInjector> CreateSystemInputInjector() override {
138 return event_factory_ozone_->CreateSystemInputInjector();
140 scoped_ptr<PlatformWindow> CreatePlatformWindow(
141 PlatformWindowDelegate* delegate,
142 const gfx::Rect& bounds) override {
143 scoped_ptr<DrmWindowHost> platform_window(
144 new DrmWindowHost(delegate, bounds, gpu_platform_support_host_.get(),
145 event_factory_ozone_.get(), cursor_.get(),
146 window_manager_.get(), display_manager_.get()));
147 platform_window->Initialize();
148 return platform_window.Pass();
150 scoped_ptr<NativeDisplayDelegate> CreateNativeDisplayDelegate() override {
151 return make_scoped_ptr(
152 new DrmNativeDisplayDelegate(display_manager_.get()));
154 base::ScopedFD OpenClientNativePixmapDevice() const override {
155 #if defined(USE_VGEM_MAP)
156 static const char kVgemPath[] = "/dev/dri/renderD129";
157 base::ScopedFD vgem_fd(open(kVgemPath, O_RDWR | O_CLOEXEC));
158 DCHECK(vgem_fd.is_valid()) << "Failed to open: " << kVgemPath;
159 return vgem_fd;
160 #endif
161 return base::ScopedFD();
163 void InitializeUI() override {
164 device_manager_ = CreateDeviceManager();
165 window_manager_.reset(new DrmWindowHostManager());
166 cursor_.reset(new DrmCursor(window_manager_.get()));
167 #if defined(USE_XKBCOMMON)
168 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(make_scoped_ptr(
169 new XkbKeyboardLayoutEngine(xkb_evdev_code_converter_)));
170 #else
171 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(
172 make_scoped_ptr(new StubKeyboardLayoutEngine()));
173 #endif
174 event_factory_ozone_.reset(new EventFactoryEvdev(
175 cursor_.get(), device_manager_.get(),
176 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()));
177 gpu_platform_support_host_.reset(
178 new DrmGpuPlatformSupportHost(cursor_.get()));
179 display_manager_.reset(new DrmDisplayHostManager(
180 gpu_platform_support_host_.get(), device_manager_.get(),
181 event_factory_ozone_->input_controller()));
182 cursor_factory_ozone_.reset(new BitmapCursorFactoryOzone);
183 overlay_manager_.reset(
184 new DrmOverlayManager(gpu_platform_support_host_.get()));
187 void InitializeGPU() override {
188 bool use_atomic = false;
189 #if defined(USE_DRM_ATOMIC)
190 use_atomic = true;
191 #endif
192 gl_api_loader_.reset(new GlApiLoader());
193 drm_device_manager_.reset(new DrmDeviceManager(
194 scoped_ptr<DrmDeviceGenerator>(new GbmDeviceGenerator(use_atomic))));
195 buffer_generator_.reset(new GbmBufferGenerator());
196 screen_manager_.reset(new ScreenManager(buffer_generator_.get()));
197 surface_factory_ozone_.reset(new GbmSurfaceFactory());
198 surface_factory_ozone_->InitializeGpu(drm_device_manager_.get(),
199 screen_manager_.get());
200 scoped_ptr<DrmGpuDisplayManager> display_manager(new DrmGpuDisplayManager(
201 screen_manager_.get(), drm_device_manager_.get()));
202 gpu_platform_support_.reset(new DrmGpuPlatformSupport(
203 drm_device_manager_.get(), screen_manager_.get(),
204 buffer_generator_.get(), display_manager.Pass()));
207 private:
208 // Objects in the GPU process.
209 scoped_ptr<GbmSurfaceFactory> surface_factory_ozone_;
210 scoped_ptr<GlApiLoader> gl_api_loader_;
211 scoped_ptr<DrmDeviceManager> drm_device_manager_;
212 scoped_ptr<GbmBufferGenerator> buffer_generator_;
213 scoped_ptr<ScreenManager> screen_manager_;
214 scoped_ptr<DrmGpuPlatformSupport> gpu_platform_support_;
216 // Objects in the Browser process.
217 scoped_ptr<DeviceManager> device_manager_;
218 scoped_ptr<BitmapCursorFactoryOzone> cursor_factory_ozone_;
219 scoped_ptr<DrmWindowHostManager> window_manager_;
220 scoped_ptr<DrmCursor> cursor_;
221 scoped_ptr<EventFactoryEvdev> event_factory_ozone_;
222 scoped_ptr<DrmGpuPlatformSupportHost> gpu_platform_support_host_;
223 scoped_ptr<DrmDisplayHostManager> display_manager_;
224 scoped_ptr<DrmOverlayManager> overlay_manager_;
226 #if defined(USE_XKBCOMMON)
227 XkbEvdevCodes xkb_evdev_code_converter_;
228 #endif
230 DISALLOW_COPY_AND_ASSIGN(OzonePlatformGbm);
233 } // namespace
235 OzonePlatform* CreateOzonePlatformGbm() {
236 return new OzonePlatformGbm;
239 } // namespace ui