ozone: fix HDPMLegacy - do the PF after overlays, also clear old overlays
[chromium-blink-merge.git] / ui / ozone / platform / dri / ozone_platform_gbm.cc
blobbfba0e04851c35aed77d8292a661b4df80060e5f
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/dri/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/dri/display_manager.h"
19 #include "ui/ozone/platform/dri/dri_cursor.h"
20 #include "ui/ozone/platform/dri/dri_gpu_platform_support.h"
21 #include "ui/ozone/platform/dri/dri_gpu_platform_support_host.h"
22 #include "ui/ozone/platform/dri/dri_window.h"
23 #include "ui/ozone/platform/dri/dri_window_delegate_manager.h"
24 #include "ui/ozone/platform/dri/dri_window_manager.h"
25 #include "ui/ozone/platform/dri/gbm_buffer.h"
26 #include "ui/ozone/platform/dri/gbm_surface.h"
27 #include "ui/ozone/platform/dri/gbm_surface_factory.h"
28 #include "ui/ozone/platform/dri/gbm_wrapper.h"
29 #include "ui/ozone/platform/dri/native_display_delegate_dri.h"
30 #include "ui/ozone/platform/dri/native_display_delegate_proxy.h"
31 #include "ui/ozone/platform/dri/scanout_buffer.h"
32 #include "ui/ozone/platform/dri/screen_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 namespace ui {
48 namespace {
50 const char kDefaultGraphicsCardPath[] = "/dev/dri/card0";
52 class GlApiLoader {
53 public:
54 GlApiLoader()
55 : glapi_lib_(dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL)) {}
57 ~GlApiLoader() {
58 if (glapi_lib_)
59 dlclose(glapi_lib_);
62 private:
63 // HACK: gbm drivers have broken linkage. The Mesa DRI driver references
64 // symbols in the libglapi library however it does not explicitly link against
65 // it. That caused linkage errors when running an application that does not
66 // explicitly link against libglapi.
67 void* glapi_lib_;
69 DISALLOW_COPY_AND_ASSIGN(GlApiLoader);
72 class GbmBufferGenerator : public ScanoutBufferGenerator {
73 public:
74 GbmBufferGenerator() {}
75 ~GbmBufferGenerator() override {}
77 scoped_refptr<ScanoutBuffer> Create(DriWrapper* drm,
78 const gfx::Size& size) override {
79 return GbmBuffer::CreateBuffer(static_cast<GbmWrapper*>(drm),
80 SurfaceFactoryOzone::RGBA_8888, size, true);
83 protected:
84 DISALLOW_COPY_AND_ASSIGN(GbmBufferGenerator);
87 class OzonePlatformGbm : public OzonePlatform {
88 public:
89 OzonePlatformGbm(bool use_surfaceless) : use_surfaceless_(use_surfaceless) {
91 ~OzonePlatformGbm() override {}
93 // OzonePlatform:
94 ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override {
95 return surface_factory_ozone_.get();
97 CursorFactoryOzone* GetCursorFactoryOzone() override {
98 return cursor_factory_ozone_.get();
100 InputController* GetInputController() override {
101 return event_factory_ozone_->input_controller();
103 GpuPlatformSupport* GetGpuPlatformSupport() override {
104 return gpu_platform_support_.get();
106 GpuPlatformSupportHost* GetGpuPlatformSupportHost() override {
107 return gpu_platform_support_host_.get();
109 scoped_ptr<SystemInputInjector> CreateSystemInputInjector() override {
110 return event_factory_ozone_->CreateSystemInputInjector();
112 scoped_ptr<PlatformWindow> CreatePlatformWindow(
113 PlatformWindowDelegate* delegate,
114 const gfx::Rect& bounds) override {
115 scoped_ptr<DriWindow> platform_window(
116 new DriWindow(delegate, bounds, gpu_platform_support_host_.get(),
117 event_factory_ozone_.get(), cursor_.get(),
118 window_manager_.get(), display_manager_.get()));
119 platform_window->Initialize();
120 return platform_window.Pass();
122 scoped_ptr<NativeDisplayDelegate> CreateNativeDisplayDelegate() override {
123 return scoped_ptr<NativeDisplayDelegate>(new NativeDisplayDelegateProxy(
124 gpu_platform_support_host_.get(), device_manager_.get(),
125 display_manager_.get()));
127 void InitializeUI() override {
128 display_manager_.reset(new DisplayManager());
129 // Needed since the browser process creates the accelerated widgets and that
130 // happens through SFO.
131 if (!surface_factory_ozone_)
132 surface_factory_ozone_.reset(new GbmSurfaceFactory(use_surfaceless_));
133 device_manager_ = CreateDeviceManager();
134 gpu_platform_support_host_.reset(new DriGpuPlatformSupportHost());
135 window_manager_.reset(new DriWindowManager());
136 cursor_factory_ozone_.reset(new BitmapCursorFactoryOzone);
137 cursor_.reset(
138 new DriCursor(window_manager_.get(), gpu_platform_support_host_.get()));
139 cursor_->Init();
140 #if defined(USE_XKBCOMMON)
141 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(make_scoped_ptr(
142 new XkbKeyboardLayoutEngine(xkb_evdev_code_converter_)));
143 #else
144 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(
145 make_scoped_ptr(new StubKeyboardLayoutEngine()));
146 #endif
147 event_factory_ozone_.reset(new EventFactoryEvdev(
148 cursor_.get(), device_manager_.get(),
149 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()));
152 void InitializeGPU() override {
153 gl_api_loader_.reset(new GlApiLoader());
154 // Async page flips are supported only on surfaceless mode.
155 gbm_.reset(new GbmWrapper(kDefaultGraphicsCardPath));
156 gbm_->Initialize();
157 buffer_generator_.reset(new GbmBufferGenerator());
158 screen_manager_.reset(
159 new ScreenManager(gbm_.get(), buffer_generator_.get()));
160 window_delegate_manager_.reset(new DriWindowDelegateManager());
161 if (!surface_factory_ozone_)
162 surface_factory_ozone_.reset(new GbmSurfaceFactory(use_surfaceless_));
164 surface_factory_ozone_->InitializeGpu(gbm_.get(),
165 window_delegate_manager_.get());
166 scoped_ptr<NativeDisplayDelegateDri> ndd(
167 new NativeDisplayDelegateDri(gbm_.get(), screen_manager_.get()));
168 ndd->Initialize();
169 gpu_platform_support_.reset(
170 new DriGpuPlatformSupport(gbm_.get(), window_delegate_manager_.get(),
171 screen_manager_.get(), ndd.Pass()));
174 private:
175 bool use_surfaceless_;
176 scoped_ptr<GlApiLoader> gl_api_loader_;
177 scoped_ptr<GbmWrapper> gbm_;
178 scoped_ptr<GbmBufferGenerator> buffer_generator_;
179 scoped_ptr<ScreenManager> screen_manager_;
180 scoped_ptr<DeviceManager> device_manager_;
182 scoped_ptr<GbmSurfaceFactory> surface_factory_ozone_;
183 scoped_ptr<BitmapCursorFactoryOzone> cursor_factory_ozone_;
184 scoped_ptr<EventFactoryEvdev> event_factory_ozone_;
186 scoped_ptr<DriGpuPlatformSupport> gpu_platform_support_;
187 scoped_ptr<DriGpuPlatformSupportHost> gpu_platform_support_host_;
189 scoped_ptr<DriWindowDelegateManager> window_delegate_manager_;
190 // Browser side object only.
191 scoped_ptr<DriCursor> cursor_;
192 scoped_ptr<DriWindowManager> window_manager_;
193 scoped_ptr<DisplayManager> display_manager_;
195 #if defined(USE_XKBCOMMON)
196 XkbEvdevCodes xkb_evdev_code_converter_;
197 #endif
199 DISALLOW_COPY_AND_ASSIGN(OzonePlatformGbm);
202 } // namespace
204 OzonePlatform* CreateOzonePlatformGbm() {
205 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
206 #if defined(USE_MESA_PLATFORM_NULL)
207 // Only works with surfaceless.
208 cmd->AppendSwitch(switches::kOzoneUseSurfaceless);
209 #endif
210 return new OzonePlatformGbm(cmd->HasSwitch(switches::kOzoneUseSurfaceless));
213 } // namespace ui