ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / ozone / platform / dri / gbm_surface_factory.cc
blob9c4f173152244f2a42f4475ad1ad2f6c0d56cc71
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/gbm_surface_factory.h"
7 #include <gbm.h>
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "third_party/khronos/EGL/egl.h"
12 #include "ui/ozone/common/egl_util.h"
13 #include "ui/ozone/platform/dri/dri_window_delegate_impl.h"
14 #include "ui/ozone/platform/dri/dri_window_delegate_manager.h"
15 #include "ui/ozone/platform/dri/drm_device_manager.h"
16 #include "ui/ozone/platform/dri/gbm_buffer.h"
17 #include "ui/ozone/platform/dri/gbm_device.h"
18 #include "ui/ozone/platform/dri/gbm_surface.h"
19 #include "ui/ozone/platform/dri/gbm_surfaceless.h"
20 #include "ui/ozone/platform/dri/hardware_display_controller.h"
21 #include "ui/ozone/public/native_pixmap.h"
22 #include "ui/ozone/public/overlay_candidates_ozone.h"
23 #include "ui/ozone/public/ozone_switches.h"
24 #include "ui/ozone/public/surface_ozone_canvas.h"
25 #include "ui/ozone/public/surface_ozone_egl.h"
27 namespace ui {
28 namespace {
30 class SingleOverlay : public OverlayCandidatesOzone {
31 public:
32 SingleOverlay() {}
33 ~SingleOverlay() override {}
35 void CheckOverlaySupport(OverlaySurfaceCandidateList* candidates) override {
36 if (candidates->size() == 2) {
37 OverlayCandidatesOzone::OverlaySurfaceCandidate* first =
38 &(*candidates)[0];
39 OverlayCandidatesOzone::OverlaySurfaceCandidate* second =
40 &(*candidates)[1];
41 OverlayCandidatesOzone::OverlaySurfaceCandidate* overlay;
42 if (first->plane_z_order == 0) {
43 overlay = second;
44 } else if (second->plane_z_order == 0) {
45 overlay = first;
46 } else {
47 NOTREACHED();
48 return;
50 if (overlay->plane_z_order > 0 &&
51 IsTransformSupported(overlay->transform)) {
52 overlay->overlay_handled = true;
57 private:
58 bool IsTransformSupported(gfx::OverlayTransform transform) {
59 switch (transform) {
60 case gfx::OVERLAY_TRANSFORM_NONE:
61 return true;
62 default:
63 return false;
67 DISALLOW_COPY_AND_ASSIGN(SingleOverlay);
70 } // namespace
72 GbmSurfaceFactory::GbmSurfaceFactory(bool allow_surfaceless)
73 : DriSurfaceFactory(NULL), allow_surfaceless_(allow_surfaceless) {
76 GbmSurfaceFactory::~GbmSurfaceFactory() {}
78 void GbmSurfaceFactory::InitializeGpu(
79 DrmDeviceManager* drm_device_manager,
80 DriWindowDelegateManager* window_manager) {
81 drm_device_manager_ = drm_device_manager;
82 window_manager_ = window_manager;
85 intptr_t GbmSurfaceFactory::GetNativeDisplay() {
86 #if defined(USE_MESA_PLATFORM_NULL)
87 return EGL_DEFAULT_DISPLAY;
88 #else
89 scoped_refptr<GbmDevice> gbm = GetGbmDevice(gfx::kNullAcceleratedWidget);
90 DCHECK(gbm);
91 return reinterpret_cast<intptr_t>(gbm->device());
92 #endif
95 int GbmSurfaceFactory::GetDrmFd() {
96 scoped_refptr<GbmDevice> gbm = GetGbmDevice(gfx::kNullAcceleratedWidget);
97 DCHECK(gbm);
98 return gbm->get_fd();
101 const int32* GbmSurfaceFactory::GetEGLSurfaceProperties(
102 const int32* desired_list) {
103 static const int32 kConfigAttribs[] = {
104 EGL_BUFFER_SIZE, 32,
105 EGL_ALPHA_SIZE, 8,
106 EGL_BLUE_SIZE, 8,
107 EGL_GREEN_SIZE, 8,
108 EGL_RED_SIZE, 8,
109 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
110 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
111 EGL_NONE
114 return kConfigAttribs;
117 bool GbmSurfaceFactory::LoadEGLGLES2Bindings(
118 AddGLLibraryCallback add_gl_library,
119 SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
120 return LoadDefaultEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address);
123 scoped_ptr<SurfaceOzoneCanvas> GbmSurfaceFactory::CreateCanvasForWidget(
124 gfx::AcceleratedWidget widget) {
125 LOG(FATAL) << "Software rendering mode is not supported with GBM platform";
126 return nullptr;
129 scoped_ptr<SurfaceOzoneEGL> GbmSurfaceFactory::CreateEGLSurfaceForWidget(
130 gfx::AcceleratedWidget widget) {
131 scoped_refptr<GbmDevice> gbm = GetGbmDevice(widget);
132 DCHECK(gbm);
134 scoped_ptr<GbmSurface> surface(
135 new GbmSurface(window_manager_->GetWindowDelegate(widget), gbm));
136 if (!surface->Initialize())
137 return nullptr;
139 return surface.Pass();
142 scoped_ptr<SurfaceOzoneEGL>
143 GbmSurfaceFactory::CreateSurfacelessEGLSurfaceForWidget(
144 gfx::AcceleratedWidget widget) {
145 if (!allow_surfaceless_)
146 return nullptr;
148 return make_scoped_ptr(new GbmSurfaceless(
149 window_manager_->GetWindowDelegate(widget), drm_device_manager_));
152 scoped_refptr<ui::NativePixmap> GbmSurfaceFactory::CreateNativePixmap(
153 gfx::AcceleratedWidget widget,
154 gfx::Size size,
155 BufferFormat format,
156 BufferUsage usage) {
157 if (usage == MAP)
158 return nullptr;
160 scoped_refptr<GbmDevice> gbm = GetGbmDevice(widget);
161 DCHECK(gbm);
163 scoped_refptr<GbmBuffer> buffer =
164 GbmBuffer::CreateBuffer(gbm, format, size, true);
165 if (!buffer.get())
166 return nullptr;
168 scoped_refptr<GbmPixmap> pixmap(new GbmPixmap(buffer));
169 if (!pixmap->Initialize())
170 return nullptr;
172 return pixmap;
175 OverlayCandidatesOzone* GbmSurfaceFactory::GetOverlayCandidates(
176 gfx::AcceleratedWidget w) {
177 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
178 switches::kOzoneTestSingleOverlaySupport))
179 return new SingleOverlay();
180 return NULL;
183 bool GbmSurfaceFactory::ScheduleOverlayPlane(
184 gfx::AcceleratedWidget widget,
185 int plane_z_order,
186 gfx::OverlayTransform plane_transform,
187 scoped_refptr<NativePixmap> buffer,
188 const gfx::Rect& display_bounds,
189 const gfx::RectF& crop_rect) {
190 scoped_refptr<GbmPixmap> pixmap = static_cast<GbmPixmap*>(buffer.get());
191 if (!pixmap.get()) {
192 LOG(ERROR) << "ScheduleOverlayPlane passed NULL buffer.";
193 return false;
195 HardwareDisplayController* hdc =
196 window_manager_->GetWindowDelegate(widget)->GetController();
197 if (!hdc)
198 return true;
200 hdc->QueueOverlayPlane(OverlayPlane(pixmap->buffer(),
201 plane_z_order,
202 plane_transform,
203 display_bounds,
204 crop_rect));
205 return true;
208 bool GbmSurfaceFactory::CanShowPrimaryPlaneAsOverlay() {
209 return allow_surfaceless_;
212 bool GbmSurfaceFactory::CanCreateNativePixmap(BufferUsage usage) {
213 switch (usage) {
214 case MAP:
215 return false;
216 case SCANOUT:
217 return true;
219 NOTREACHED();
220 return false;
223 scoped_refptr<GbmDevice> GbmSurfaceFactory::GetGbmDevice(
224 gfx::AcceleratedWidget widget) {
225 return static_cast<GbmDevice*>(
226 drm_device_manager_->GetDrmDevice(widget).get());
229 } // namespace ui