Remove the 'gyp_config' concept from MB.
[chromium-blink-merge.git] / ui / ozone / platform / drm / ozone_platform_gbm.cc
blobe321daeff227ddfe6a0127f8e870783e4c43d989
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/at_exit.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h"
15 #include "ui/events/ozone/device/device_manager.h"
16 #include "ui/events/ozone/evdev/event_factory_evdev.h"
17 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
18 #include "ui/ozone/platform/drm/common/drm_util.h"
19 #include "ui/ozone/platform/drm/gpu/drm_device_generator.h"
20 #include "ui/ozone/platform/drm/gpu/drm_device_manager.h"
21 #include "ui/ozone/platform/drm/gpu/drm_gpu_display_manager.h"
22 #include "ui/ozone/platform/drm/gpu/drm_gpu_platform_support.h"
23 #include "ui/ozone/platform/drm/gpu/gbm_buffer.h"
24 #include "ui/ozone/platform/drm/gpu/gbm_device.h"
25 #include "ui/ozone/platform/drm/gpu/gbm_surface.h"
26 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h"
27 #include "ui/ozone/platform/drm/gpu/scanout_buffer.h"
28 #include "ui/ozone/platform/drm/gpu/screen_manager.h"
29 #include "ui/ozone/platform/drm/host/drm_cursor.h"
30 #include "ui/ozone/platform/drm/host/drm_display_host_manager.h"
31 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h"
32 #include "ui/ozone/platform/drm/host/drm_native_display_delegate.h"
33 #include "ui/ozone/platform/drm/host/drm_overlay_manager.h"
34 #include "ui/ozone/platform/drm/host/drm_window_host.h"
35 #include "ui/ozone/platform/drm/host/drm_window_host_manager.h"
36 #include "ui/ozone/public/cursor_factory_ozone.h"
37 #include "ui/ozone/public/gpu_platform_support.h"
38 #include "ui/ozone/public/gpu_platform_support_host.h"
39 #include "ui/ozone/public/ozone_platform.h"
40 #include "ui/ozone/public/ozone_switches.h"
42 #if defined(USE_XKBCOMMON)
43 #include "ui/events/ozone/layout/xkb/xkb_evdev_codes.h"
44 #include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
45 #else
46 #include "ui/events/ozone/layout/stub/stub_keyboard_layout_engine.h"
47 #endif
49 #if defined(USE_VGEM_MAP)
50 #include <fcntl.h>
51 #endif
53 namespace ui {
55 namespace {
57 class GlApiLoader {
58 public:
59 GlApiLoader()
60 : glapi_lib_(dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL)) {}
62 ~GlApiLoader() {
63 if (glapi_lib_)
64 dlclose(glapi_lib_);
67 private:
68 // HACK: gbm drivers have broken linkage. The Mesa DRI driver references
69 // symbols in the libglapi library however it does not explicitly link against
70 // it. That caused linkage errors when running an application that does not
71 // explicitly link against libglapi.
72 void* glapi_lib_;
74 DISALLOW_COPY_AND_ASSIGN(GlApiLoader);
77 class GbmBufferGenerator : public ScanoutBufferGenerator {
78 public:
79 GbmBufferGenerator() {}
80 ~GbmBufferGenerator() override {}
82 // ScanoutBufferGenerator:
83 scoped_refptr<ScanoutBuffer> Create(const scoped_refptr<DrmDevice>& drm,
84 const gfx::Size& size) override {
85 scoped_refptr<GbmDevice> gbm(static_cast<GbmDevice*>(drm.get()));
86 return GbmBuffer::CreateBuffer(gbm, gfx::BufferFormat::BGRA_8888, size,
87 gfx::BufferUsage::SCANOUT);
90 protected:
91 DISALLOW_COPY_AND_ASSIGN(GbmBufferGenerator);
94 class GbmDeviceGenerator : public DrmDeviceGenerator {
95 public:
96 GbmDeviceGenerator(bool use_atomic) : use_atomic_(use_atomic) {}
97 ~GbmDeviceGenerator() override {}
99 // DrmDeviceGenerator:
100 scoped_refptr<DrmDevice> CreateDevice(const base::FilePath& path,
101 base::File file) override {
102 scoped_refptr<DrmDevice> drm = new GbmDevice(path, file.Pass());
103 if (drm->Initialize(use_atomic_))
104 return drm;
106 return nullptr;
109 private:
110 bool use_atomic_;
112 DISALLOW_COPY_AND_ASSIGN(GbmDeviceGenerator);
115 class OzonePlatformGbm : public OzonePlatform {
116 public:
117 OzonePlatformGbm(bool use_surfaceless) : use_surfaceless_(use_surfaceless) {}
118 ~OzonePlatformGbm() override {}
120 // OzonePlatform:
121 ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override {
122 return surface_factory_ozone_.get();
124 OverlayManagerOzone* GetOverlayManager() override {
125 return overlay_manager_.get();
127 CursorFactoryOzone* GetCursorFactoryOzone() override {
128 return cursor_factory_ozone_.get();
130 InputController* GetInputController() override {
131 return event_factory_ozone_->input_controller();
133 GpuPlatformSupport* GetGpuPlatformSupport() override {
134 return gpu_platform_support_.get();
136 GpuPlatformSupportHost* GetGpuPlatformSupportHost() override {
137 return gpu_platform_support_host_.get();
139 scoped_ptr<SystemInputInjector> CreateSystemInputInjector() override {
140 return event_factory_ozone_->CreateSystemInputInjector();
142 scoped_ptr<PlatformWindow> CreatePlatformWindow(
143 PlatformWindowDelegate* delegate,
144 const gfx::Rect& bounds) override {
145 scoped_ptr<DrmWindowHost> platform_window(
146 new DrmWindowHost(delegate, bounds, gpu_platform_support_host_.get(),
147 event_factory_ozone_.get(), cursor_.get(),
148 window_manager_.get(), display_manager_.get()));
149 platform_window->Initialize();
150 return platform_window.Pass();
152 scoped_ptr<NativeDisplayDelegate> CreateNativeDisplayDelegate() override {
153 return make_scoped_ptr(
154 new DrmNativeDisplayDelegate(display_manager_.get()));
156 base::ScopedFD OpenClientNativePixmapDevice() const override {
157 #if defined(USE_VGEM_MAP)
158 static const char kVgemPath[] = "/dev/dri/renderD129";
159 base::ScopedFD vgem_fd(open(kVgemPath, O_RDWR | O_CLOEXEC));
160 DCHECK(vgem_fd.is_valid()) << "Failed to open: " << kVgemPath;
161 return vgem_fd;
162 #endif
163 return base::ScopedFD();
165 void InitializeUI() override {
166 device_manager_ = CreateDeviceManager();
167 window_manager_.reset(new DrmWindowHostManager());
168 cursor_.reset(new DrmCursor(window_manager_.get()));
169 #if defined(USE_XKBCOMMON)
170 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(make_scoped_ptr(
171 new XkbKeyboardLayoutEngine(xkb_evdev_code_converter_)));
172 #else
173 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(
174 make_scoped_ptr(new StubKeyboardLayoutEngine()));
175 #endif
176 event_factory_ozone_.reset(new EventFactoryEvdev(
177 cursor_.get(), device_manager_.get(),
178 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()));
179 gpu_platform_support_host_.reset(
180 new DrmGpuPlatformSupportHost(cursor_.get()));
181 display_manager_.reset(new DrmDisplayHostManager(
182 gpu_platform_support_host_.get(), device_manager_.get(),
183 event_factory_ozone_->input_controller()));
184 cursor_factory_ozone_.reset(new BitmapCursorFactoryOzone);
185 overlay_manager_.reset(new DrmOverlayManager(
186 use_surfaceless_, gpu_platform_support_host_.get()));
189 void InitializeGPU() override {
190 bool use_atomic = false;
191 #if defined(USE_DRM_ATOMIC)
192 use_atomic = true;
193 #endif
194 gl_api_loader_.reset(new GlApiLoader());
195 drm_device_manager_.reset(new DrmDeviceManager(
196 scoped_ptr<DrmDeviceGenerator>(new GbmDeviceGenerator(use_atomic))));
197 buffer_generator_.reset(new GbmBufferGenerator());
198 screen_manager_.reset(new ScreenManager(buffer_generator_.get()));
199 surface_factory_ozone_.reset(new GbmSurfaceFactory(use_surfaceless_));
200 surface_factory_ozone_->InitializeGpu(drm_device_manager_.get(),
201 screen_manager_.get());
202 scoped_ptr<DrmGpuDisplayManager> display_manager(new DrmGpuDisplayManager(
203 screen_manager_.get(), drm_device_manager_.get()));
204 gpu_platform_support_.reset(new DrmGpuPlatformSupport(
205 drm_device_manager_.get(), screen_manager_.get(),
206 buffer_generator_.get(), display_manager.Pass()));
209 private:
210 // Objects in both processes.
211 bool use_surfaceless_;
213 // Objects in the GPU process.
214 scoped_ptr<GbmSurfaceFactory> surface_factory_ozone_;
215 scoped_ptr<GlApiLoader> gl_api_loader_;
216 scoped_ptr<DrmDeviceManager> drm_device_manager_;
217 scoped_ptr<GbmBufferGenerator> buffer_generator_;
218 scoped_ptr<ScreenManager> screen_manager_;
219 scoped_ptr<DrmGpuPlatformSupport> gpu_platform_support_;
221 // Objects in the Browser process.
222 scoped_ptr<DeviceManager> device_manager_;
223 scoped_ptr<BitmapCursorFactoryOzone> cursor_factory_ozone_;
224 scoped_ptr<DrmWindowHostManager> window_manager_;
225 scoped_ptr<DrmCursor> cursor_;
226 scoped_ptr<EventFactoryEvdev> event_factory_ozone_;
227 scoped_ptr<DrmGpuPlatformSupportHost> gpu_platform_support_host_;
228 scoped_ptr<DrmDisplayHostManager> display_manager_;
229 scoped_ptr<DrmOverlayManager> overlay_manager_;
231 #if defined(USE_XKBCOMMON)
232 XkbEvdevCodes xkb_evdev_code_converter_;
233 #endif
235 DISALLOW_COPY_AND_ASSIGN(OzonePlatformGbm);
238 } // namespace
240 OzonePlatform* CreateOzonePlatformGbm() {
241 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
242 #if defined(USE_MESA_PLATFORM_NULL)
243 // Only works with surfaceless.
244 cmd->AppendSwitch(switches::kOzoneUseSurfaceless);
245 #endif
246 return new OzonePlatformGbm(cmd->HasSwitch(switches::kOzoneUseSurfaceless));
249 } // namespace ui