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/gbm_buffer.h"
16 #include "ui/ozone/platform/dri/gbm_surface.h"
17 #include "ui/ozone/platform/dri/gbm_surfaceless.h"
18 #include "ui/ozone/platform/dri/gbm_wrapper.h"
19 #include "ui/ozone/platform/dri/hardware_display_controller.h"
20 #include "ui/ozone/public/native_pixmap.h"
21 #include "ui/ozone/public/overlay_candidates_ozone.h"
22 #include "ui/ozone/public/ozone_switches.h"
23 #include "ui/ozone/public/surface_ozone_egl.h"
28 class SingleOverlay
: public OverlayCandidatesOzone
{
31 ~SingleOverlay() override
{}
33 void CheckOverlaySupport(OverlaySurfaceCandidateList
* candidates
) override
{
34 if (candidates
->size() == 2) {
35 OverlayCandidatesOzone::OverlaySurfaceCandidate
* first
=
37 OverlayCandidatesOzone::OverlaySurfaceCandidate
* second
=
39 OverlayCandidatesOzone::OverlaySurfaceCandidate
* overlay
;
40 if (first
->plane_z_order
== 0) {
42 } else if (second
->plane_z_order
== 0) {
48 if (overlay
->plane_z_order
> 0 &&
49 IsTransformSupported(overlay
->transform
)) {
50 overlay
->overlay_handled
= true;
56 bool IsTransformSupported(gfx::OverlayTransform transform
) {
58 case gfx::OVERLAY_TRANSFORM_NONE
:
65 DISALLOW_COPY_AND_ASSIGN(SingleOverlay
);
70 GbmSurfaceFactory::GbmSurfaceFactory(bool allow_surfaceless
)
71 : DriSurfaceFactory(NULL
, NULL
),
72 allow_surfaceless_(allow_surfaceless
) {
75 GbmSurfaceFactory::~GbmSurfaceFactory() {}
77 void GbmSurfaceFactory::InitializeGpu(
79 DriWindowDelegateManager
* window_manager
) {
81 window_manager_
= window_manager
;
84 intptr_t GbmSurfaceFactory::GetNativeDisplay() {
85 #if defined(USE_MESA_PLATFORM_NULL)
86 return EGL_DEFAULT_DISPLAY
;
89 return reinterpret_cast<intptr_t>(gbm_
->device());
93 int GbmSurfaceFactory::GetDrmFd() {
95 return gbm_
->get_fd();
98 const int32
* GbmSurfaceFactory::GetEGLSurfaceProperties(
99 const int32
* desired_list
) {
100 static const int32 kConfigAttribs
[] = {
106 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
107 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
111 return kConfigAttribs
;
114 bool GbmSurfaceFactory::LoadEGLGLES2Bindings(
115 AddGLLibraryCallback add_gl_library
,
116 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {
117 return LoadDefaultEGLGLES2Bindings(add_gl_library
, set_gl_get_proc_address
);
120 scoped_ptr
<SurfaceOzoneEGL
> GbmSurfaceFactory::CreateEGLSurfaceForWidget(
121 gfx::AcceleratedWidget widget
) {
122 scoped_ptr
<GbmSurface
> surface(
123 new GbmSurface(window_manager_
->GetWindowDelegate(widget
), gbm_
));
124 if (!surface
->Initialize())
127 return surface
.Pass();
130 scoped_ptr
<SurfaceOzoneEGL
>
131 GbmSurfaceFactory::CreateSurfacelessEGLSurfaceForWidget(
132 gfx::AcceleratedWidget widget
) {
133 if (!allow_surfaceless_
)
136 return scoped_ptr
<SurfaceOzoneEGL
>(
137 new GbmSurfaceless(window_manager_
->GetWindowDelegate(widget
)));
140 scoped_refptr
<ui::NativePixmap
> GbmSurfaceFactory::CreateNativePixmap(
141 gfx::AcceleratedWidget widget
,
148 scoped_refptr
<GbmBuffer
> buffer
=
149 GbmBuffer::CreateBuffer(gbm_
, format
, size
, true);
153 scoped_refptr
<GbmPixmap
> pixmap(new GbmPixmap(buffer
));
154 if (!pixmap
->Initialize(gbm_
))
160 OverlayCandidatesOzone
* GbmSurfaceFactory::GetOverlayCandidates(
161 gfx::AcceleratedWidget w
) {
162 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
163 switches::kOzoneTestSingleOverlaySupport
))
164 return new SingleOverlay();
168 bool GbmSurfaceFactory::ScheduleOverlayPlane(
169 gfx::AcceleratedWidget widget
,
171 gfx::OverlayTransform plane_transform
,
172 scoped_refptr
<NativePixmap
> buffer
,
173 const gfx::Rect
& display_bounds
,
174 const gfx::RectF
& crop_rect
) {
175 scoped_refptr
<GbmPixmap
> pixmap
= static_cast<GbmPixmap
*>(buffer
.get());
177 LOG(ERROR
) << "ScheduleOverlayPlane passed NULL buffer.";
180 HardwareDisplayController
* hdc
=
181 window_manager_
->GetWindowDelegate(widget
)->GetController();
185 hdc
->QueueOverlayPlane(OverlayPlane(pixmap
->buffer(),
193 bool GbmSurfaceFactory::CanShowPrimaryPlaneAsOverlay() {
194 return allow_surfaceless_
;
197 bool GbmSurfaceFactory::CanCreateNativePixmap(BufferUsage usage
) {