ozone: fix HDPMLegacy - do the PF after overlays, also clear old overlays
[chromium-blink-merge.git] / ui / ozone / platform / dri / gbm_surface_factory.cc
blob12dac8725286250212c607f82f85e91002ea5981
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/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"
25 namespace ui {
26 namespace {
28 class SingleOverlay : public OverlayCandidatesOzone {
29 public:
30 SingleOverlay() {}
31 ~SingleOverlay() override {}
33 void CheckOverlaySupport(OverlaySurfaceCandidateList* candidates) override {
34 if (candidates->size() == 2) {
35 OverlayCandidatesOzone::OverlaySurfaceCandidate* first =
36 &(*candidates)[0];
37 OverlayCandidatesOzone::OverlaySurfaceCandidate* second =
38 &(*candidates)[1];
39 OverlayCandidatesOzone::OverlaySurfaceCandidate* overlay;
40 if (first->plane_z_order == 0) {
41 overlay = second;
42 } else if (second->plane_z_order == 0) {
43 overlay = first;
44 } else {
45 NOTREACHED();
46 return;
48 if (overlay->plane_z_order > 0 &&
49 IsTransformSupported(overlay->transform)) {
50 overlay->overlay_handled = true;
55 private:
56 bool IsTransformSupported(gfx::OverlayTransform transform) {
57 switch (transform) {
58 case gfx::OVERLAY_TRANSFORM_NONE:
59 return true;
60 default:
61 return false;
65 DISALLOW_COPY_AND_ASSIGN(SingleOverlay);
68 } // namespace
70 GbmSurfaceFactory::GbmSurfaceFactory(bool allow_surfaceless)
71 : DriSurfaceFactory(NULL, NULL),
72 allow_surfaceless_(allow_surfaceless) {
75 GbmSurfaceFactory::~GbmSurfaceFactory() {}
77 void GbmSurfaceFactory::InitializeGpu(
78 GbmWrapper* gbm,
79 DriWindowDelegateManager* window_manager) {
80 gbm_ = gbm;
81 window_manager_ = window_manager;
84 intptr_t GbmSurfaceFactory::GetNativeDisplay() {
85 #if defined(USE_MESA_PLATFORM_NULL)
86 return EGL_DEFAULT_DISPLAY;
87 #else
88 DCHECK(gbm_);
89 return reinterpret_cast<intptr_t>(gbm_->device());
90 #endif
93 int GbmSurfaceFactory::GetDrmFd() {
94 DCHECK(gbm_);
95 return gbm_->get_fd();
98 const int32* GbmSurfaceFactory::GetEGLSurfaceProperties(
99 const int32* desired_list) {
100 static const int32 kConfigAttribs[] = {
101 EGL_BUFFER_SIZE, 32,
102 EGL_ALPHA_SIZE, 8,
103 EGL_BLUE_SIZE, 8,
104 EGL_GREEN_SIZE, 8,
105 EGL_RED_SIZE, 8,
106 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108 EGL_NONE
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())
125 return nullptr;
127 return surface.Pass();
130 scoped_ptr<SurfaceOzoneEGL>
131 GbmSurfaceFactory::CreateSurfacelessEGLSurfaceForWidget(
132 gfx::AcceleratedWidget widget) {
133 if (!allow_surfaceless_)
134 return nullptr;
136 return scoped_ptr<SurfaceOzoneEGL>(
137 new GbmSurfaceless(window_manager_->GetWindowDelegate(widget)));
140 scoped_refptr<ui::NativePixmap> GbmSurfaceFactory::CreateNativePixmap(
141 gfx::AcceleratedWidget widget,
142 gfx::Size size,
143 BufferFormat format,
144 BufferUsage usage) {
145 if (usage == MAP)
146 return NULL;
148 scoped_refptr<GbmBuffer> buffer =
149 GbmBuffer::CreateBuffer(gbm_, format, size, true);
150 if (!buffer.get())
151 return NULL;
153 scoped_refptr<GbmPixmap> pixmap(new GbmPixmap(buffer));
154 if (!pixmap->Initialize(gbm_))
155 return NULL;
157 return pixmap;
160 OverlayCandidatesOzone* GbmSurfaceFactory::GetOverlayCandidates(
161 gfx::AcceleratedWidget w) {
162 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
163 switches::kOzoneTestSingleOverlaySupport))
164 return new SingleOverlay();
165 return NULL;
168 bool GbmSurfaceFactory::ScheduleOverlayPlane(
169 gfx::AcceleratedWidget widget,
170 int plane_z_order,
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());
176 if (!pixmap.get()) {
177 LOG(ERROR) << "ScheduleOverlayPlane passed NULL buffer.";
178 return false;
180 HardwareDisplayController* hdc =
181 window_manager_->GetWindowDelegate(widget)->GetController();
182 if (!hdc)
183 return true;
185 hdc->QueueOverlayPlane(OverlayPlane(pixmap->buffer(),
186 plane_z_order,
187 plane_transform,
188 display_bounds,
189 crop_rect));
190 return true;
193 bool GbmSurfaceFactory::CanShowPrimaryPlaneAsOverlay() {
194 return allow_surfaceless_;
197 bool GbmSurfaceFactory::CanCreateNativePixmap(BufferUsage usage) {
198 switch (usage) {
199 case MAP:
200 return false;
201 case SCANOUT:
202 return true;
204 NOTREACHED();
205 return false;
208 } // namespace ui