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"
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"
30 class SingleOverlay
: public OverlayCandidatesOzone
{
33 ~SingleOverlay() override
{}
35 void CheckOverlaySupport(OverlaySurfaceCandidateList
* candidates
) override
{
36 if (candidates
->size() == 2) {
37 OverlayCandidatesOzone::OverlaySurfaceCandidate
* first
=
39 OverlayCandidatesOzone::OverlaySurfaceCandidate
* second
=
41 OverlayCandidatesOzone::OverlaySurfaceCandidate
* overlay
;
42 if (first
->plane_z_order
== 0) {
44 } else if (second
->plane_z_order
== 0) {
50 if (overlay
->plane_z_order
> 0 &&
51 IsTransformSupported(overlay
->transform
)) {
52 overlay
->overlay_handled
= true;
58 bool IsTransformSupported(gfx::OverlayTransform transform
) {
60 case gfx::OVERLAY_TRANSFORM_NONE
:
67 DISALLOW_COPY_AND_ASSIGN(SingleOverlay
);
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
;
89 scoped_refptr
<GbmDevice
> gbm
= GetGbmDevice(gfx::kNullAcceleratedWidget
);
91 return reinterpret_cast<intptr_t>(gbm
->device());
95 int GbmSurfaceFactory::GetDrmFd() {
96 scoped_refptr
<GbmDevice
> gbm
= GetGbmDevice(gfx::kNullAcceleratedWidget
);
101 const int32
* GbmSurfaceFactory::GetEGLSurfaceProperties(
102 const int32
* desired_list
) {
103 static const int32 kConfigAttribs
[] = {
109 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
110 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
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";
129 scoped_ptr
<SurfaceOzoneEGL
> GbmSurfaceFactory::CreateEGLSurfaceForWidget(
130 gfx::AcceleratedWidget widget
) {
131 scoped_refptr
<GbmDevice
> gbm
= GetGbmDevice(widget
);
134 scoped_ptr
<GbmSurface
> surface(
135 new GbmSurface(window_manager_
->GetWindowDelegate(widget
), gbm
));
136 if (!surface
->Initialize())
139 return surface
.Pass();
142 scoped_ptr
<SurfaceOzoneEGL
>
143 GbmSurfaceFactory::CreateSurfacelessEGLSurfaceForWidget(
144 gfx::AcceleratedWidget widget
) {
145 if (!allow_surfaceless_
)
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
,
160 scoped_refptr
<GbmDevice
> gbm
= GetGbmDevice(widget
);
163 scoped_refptr
<GbmBuffer
> buffer
=
164 GbmBuffer::CreateBuffer(gbm
, format
, size
, true);
168 scoped_refptr
<GbmPixmap
> pixmap(new GbmPixmap(buffer
));
169 if (!pixmap
->Initialize())
175 OverlayCandidatesOzone
* GbmSurfaceFactory::GetOverlayCandidates(
176 gfx::AcceleratedWidget w
) {
177 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
178 switches::kOzoneTestSingleOverlaySupport
))
179 return new SingleOverlay();
183 bool GbmSurfaceFactory::ScheduleOverlayPlane(
184 gfx::AcceleratedWidget widget
,
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());
192 LOG(ERROR
) << "ScheduleOverlayPlane passed NULL buffer.";
195 HardwareDisplayController
* hdc
=
196 window_manager_
->GetWindowDelegate(widget
)->GetController();
200 hdc
->QueueOverlayPlane(OverlayPlane(pixmap
->buffer(),
208 bool GbmSurfaceFactory::CanShowPrimaryPlaneAsOverlay() {
209 return allow_surfaceless_
;
212 bool GbmSurfaceFactory::CanCreateNativePixmap(BufferUsage usage
) {
223 scoped_refptr
<GbmDevice
> GbmSurfaceFactory::GetGbmDevice(
224 gfx::AcceleratedWidget widget
) {
225 return static_cast<GbmDevice
*>(
226 drm_device_manager_
->GetDrmDevice(widget
).get());