Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / dri / ozone_platform_gbm.cc
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.
4
5 #include "ui/ozone/platform/dri/ozone_platform_gbm.h"
6
7 #include <dlfcn.h>
8 #include <gbm.h>
9 #include <stdlib.h>
10
11 #include "base/at_exit.h"
12 #include "base/command_line.h"
13 #include "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h"
14 #include "ui/events/ozone/device/device_manager.h"
15 #include "ui/events/ozone/evdev/event_factory_evdev.h"
16 #include "ui/ozone/platform/dri/dri_cursor.h"
17 #include "ui/ozone/platform/dri/dri_gpu_platform_support.h"
18 #include "ui/ozone/platform/dri/dri_gpu_platform_support_host.h"
19 #include "ui/ozone/platform/dri/dri_window.h"
20 #include "ui/ozone/platform/dri/dri_window_delegate_manager.h"
21 #include "ui/ozone/platform/dri/dri_window_manager.h"
22 #include "ui/ozone/platform/dri/dri_wrapper.h"
23 #include "ui/ozone/platform/dri/gbm_buffer.h"
24 #include "ui/ozone/platform/dri/gbm_surface.h"
25 #include "ui/ozone/platform/dri/gbm_surface_factory.h"
26 #include "ui/ozone/platform/dri/native_display_delegate_dri.h"
27 #include "ui/ozone/platform/dri/native_display_delegate_proxy.h"
28 #include "ui/ozone/platform/dri/scanout_buffer.h"
29 #include "ui/ozone/platform/dri/screen_manager.h"
30 #include "ui/ozone/public/cursor_factory_ozone.h"
31 #include "ui/ozone/public/gpu_platform_support.h"
32 #include "ui/ozone/public/gpu_platform_support_host.h"
33 #include "ui/ozone/public/ozone_platform.h"
34 #include "ui/ozone/public/ozone_switches.h"
35
36 namespace ui {
37
38 namespace {
39
40 const char kDefaultGraphicsCardPath[] = "/dev/dri/card0";
41
42 class GbmBufferGenerator : public ScanoutBufferGenerator {
43  public:
44   GbmBufferGenerator(DriWrapper* dri)
45       : dri_(dri),
46         glapi_lib_(dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL)),
47         device_(gbm_create_device(dri_->get_fd())) {
48     if (!device_)
49       LOG(FATAL) << "Unable to initialize gbm for " << kDefaultGraphicsCardPath;
50   }
51   virtual ~GbmBufferGenerator() {
52     gbm_device_destroy(device_);
53     if (glapi_lib_)
54       dlclose(glapi_lib_);
55   }
56
57   gbm_device* device() const { return device_; }
58
59   scoped_refptr<ScanoutBuffer> Create(const gfx::Size& size) override {
60     return GbmBuffer::CreateBuffer(
61         dri_, device_, SurfaceFactoryOzone::RGBA_8888, size, true);
62   }
63
64  protected:
65   DriWrapper* dri_;  // Not owned.
66
67   // HACK: gbm drivers have broken linkage
68   void *glapi_lib_;
69
70   gbm_device* device_;
71
72   DISALLOW_COPY_AND_ASSIGN(GbmBufferGenerator);
73 };
74
75 class OzonePlatformGbm : public OzonePlatform {
76  public:
77   OzonePlatformGbm(bool use_surfaceless) : use_surfaceless_(use_surfaceless) {
78     base::AtExitManager::RegisterTask(
79         base::Bind(&base::DeletePointer<OzonePlatformGbm>, this));
80   }
81   ~OzonePlatformGbm() override {}
82
83   // OzonePlatform:
84   ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override {
85     return surface_factory_ozone_.get();
86   }
87   CursorFactoryOzone* GetCursorFactoryOzone() override {
88     return cursor_factory_ozone_.get();
89   }
90   GpuPlatformSupport* GetGpuPlatformSupport() override {
91     return gpu_platform_support_.get();
92   }
93   GpuPlatformSupportHost* GetGpuPlatformSupportHost() override {
94     return gpu_platform_support_host_.get();
95   }
96   scoped_ptr<PlatformWindow> CreatePlatformWindow(
97       PlatformWindowDelegate* delegate,
98       const gfx::Rect& bounds) override {
99     scoped_ptr<DriWindow> platform_window(
100         new DriWindow(delegate, bounds, gpu_platform_support_host_.get(),
101                       event_factory_ozone_.get(), window_manager_.get()));
102     platform_window->Initialize();
103     return platform_window.Pass();
104   }
105   scoped_ptr<NativeDisplayDelegate> CreateNativeDisplayDelegate() override {
106     return scoped_ptr<NativeDisplayDelegate>(new NativeDisplayDelegateProxy(
107         gpu_platform_support_host_.get(), device_manager_.get()));
108   }
109   void InitializeUI() override {
110     // Needed since the browser process creates the accelerated widgets and that
111     // happens through SFO.
112     surface_factory_ozone_.reset(new GbmSurfaceFactory(use_surfaceless_));
113     device_manager_ = CreateDeviceManager();
114     gpu_platform_support_host_.reset(new DriGpuPlatformSupportHost());
115     cursor_factory_ozone_.reset(new BitmapCursorFactoryOzone);
116     window_manager_.reset(
117         new DriWindowManager(gpu_platform_support_host_.get()));
118     event_factory_ozone_.reset(new EventFactoryEvdev(window_manager_->cursor(),
119                                                      device_manager_.get()));
120   }
121
122   void InitializeGPU() override {
123     dri_.reset(new DriWrapper(kDefaultGraphicsCardPath));
124     dri_->Initialize();
125     buffer_generator_.reset(new GbmBufferGenerator(dri_.get()));
126     screen_manager_.reset(new ScreenManager(dri_.get(),
127                                             buffer_generator_.get()));
128     window_delegate_manager_.reset(new DriWindowDelegateManager());
129     if (!surface_factory_ozone_)
130       surface_factory_ozone_.reset(new GbmSurfaceFactory(use_surfaceless_));
131
132     surface_factory_ozone_->InitializeGpu(
133         dri_.get(), buffer_generator_->device(), screen_manager_.get(),
134         window_delegate_manager_.get());
135     gpu_platform_support_.reset(new DriGpuPlatformSupport(
136         surface_factory_ozone_.get(), window_delegate_manager_.get(),
137         screen_manager_.get(),
138         scoped_ptr<NativeDisplayDelegateDri>(new NativeDisplayDelegateDri(
139             dri_.get(), screen_manager_.get(), NULL))));
140     if (surface_factory_ozone_->InitializeHardware() !=
141         DriSurfaceFactory::INITIALIZED)
142       LOG(FATAL) << "failed to initialize display hardware";
143   }
144
145  private:
146   bool use_surfaceless_;
147   scoped_ptr<DriWrapper> dri_;
148   scoped_ptr<GbmBufferGenerator> buffer_generator_;
149   scoped_ptr<ScreenManager> screen_manager_;
150   scoped_ptr<DeviceManager> device_manager_;
151
152   scoped_ptr<GbmSurfaceFactory> surface_factory_ozone_;
153   scoped_ptr<BitmapCursorFactoryOzone> cursor_factory_ozone_;
154   scoped_ptr<EventFactoryEvdev> event_factory_ozone_;
155
156   scoped_ptr<DriGpuPlatformSupport> gpu_platform_support_;
157   scoped_ptr<DriGpuPlatformSupportHost> gpu_platform_support_host_;
158
159   scoped_ptr<DriWindowDelegateManager> window_delegate_manager_;
160   // Browser side object only.
161   scoped_ptr<DriWindowManager> window_manager_;
162
163   DISALLOW_COPY_AND_ASSIGN(OzonePlatformGbm);
164 };
165
166 }  // namespace
167
168 OzonePlatform* CreateOzonePlatformGbm() {
169   CommandLine* cmd = CommandLine::ForCurrentProcess();
170   return new OzonePlatformGbm(cmd->HasSwitch(switches::kOzoneUseSurfaceless));
171 }
172
173 }  // namespace ui