OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/gl/init/gl_surface_ozone.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/callback.h" | |
13 #include "base/location.h" | |
14 #include "base/logging.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/scoped_vector.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 #include "base/threading/worker_pool.h" | |
19 #include "ui/gl/egl_util.h" | |
20 #include "ui/gl/gl_context.h" | |
21 #include "ui/gl/gl_image.h" | |
22 #include "ui/gl/gl_implementation.h" | |
23 #include "ui/gl/gl_surface_egl.h" | |
24 #include "ui/gl/gl_surface_osmesa.h" | |
25 #include "ui/gl/gl_surface_overlay.h" | |
26 #include "ui/gl/gl_surface_stub.h" | |
27 #include "ui/gl/scoped_binders.h" | |
28 #include "ui/gl/scoped_make_current.h" | |
29 #include "ui/ozone/gl/gl_image_ozone_native_pixmap.h" | |
30 #include "ui/ozone/public/native_pixmap.h" | |
31 #include "ui/ozone/public/ozone_platform.h" | |
32 #include "ui/ozone/public/surface_factory_ozone.h" | |
33 #include "ui/ozone/public/surface_ozone_egl.h" | |
34 | |
35 namespace gl { | |
36 | |
37 namespace { | |
38 | |
39 // Helper function for base::Bind to create callback to eglChooseConfig. | |
40 bool EglChooseConfig(EGLDisplay display, | |
41 const int32_t* attribs, | |
42 EGLConfig* configs, | |
43 int32_t config_size, | |
44 int32_t* num_configs) { | |
45 return eglChooseConfig(display, attribs, configs, config_size, num_configs); | |
46 } | |
47 | |
48 // Helper function for base::Bind to create callback to eglGetConfigAttrib. | |
49 bool EglGetConfigAttribute(EGLDisplay display, | |
50 EGLConfig config, | |
51 int32_t attribute, | |
52 int32_t* value) { | |
53 return eglGetConfigAttrib(display, config, attribute, value); | |
54 } | |
55 | |
56 // Populates EglConfigCallbacks with appropriate callbacks. | |
57 ui::EglConfigCallbacks GetEglConfigCallbacks(EGLDisplay display) { | |
58 ui::EglConfigCallbacks callbacks; | |
59 callbacks.choose_config = base::Bind(EglChooseConfig, display); | |
60 callbacks.get_config_attribute = base::Bind(EglGetConfigAttribute, display); | |
61 callbacks.get_last_error_string = base::Bind(&ui::GetLastEGLErrorString); | |
62 return callbacks; | |
63 } | |
64 | |
65 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow. | |
66 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { | |
67 public: | |
68 GLSurfaceOzoneEGL(std::unique_ptr<ui::SurfaceOzoneEGL> ozone_surface, | |
69 gfx::AcceleratedWidget widget); | |
70 | |
71 // GLSurface: | |
72 bool Initialize(GLSurface::Format format) override; | |
73 bool Resize(const gfx::Size& size, | |
74 float scale_factor, | |
75 bool has_alpha) override; | |
76 gfx::SwapResult SwapBuffers() override; | |
77 bool ScheduleOverlayPlane(int z_order, | |
78 gfx::OverlayTransform transform, | |
79 GLImage* image, | |
80 const gfx::Rect& bounds_rect, | |
81 const gfx::RectF& crop_rect) override; | |
82 EGLConfig GetConfig() override; | |
83 | |
84 private: | |
85 using NativeViewGLSurfaceEGL::Initialize; | |
86 | |
87 ~GLSurfaceOzoneEGL() override; | |
88 | |
89 bool ReinitializeNativeSurface(); | |
90 | |
91 // The native surface. Deleting this is allowed to free the EGLNativeWindow. | |
92 std::unique_ptr<ui::SurfaceOzoneEGL> ozone_surface_; | |
93 gfx::AcceleratedWidget widget_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL); | |
96 }; | |
97 | |
98 GLSurfaceOzoneEGL::GLSurfaceOzoneEGL( | |
99 std::unique_ptr<ui::SurfaceOzoneEGL> ozone_surface, | |
100 gfx::AcceleratedWidget widget) | |
101 : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()), | |
102 ozone_surface_(std::move(ozone_surface)), | |
103 widget_(widget) {} | |
104 | |
105 bool GLSurfaceOzoneEGL::Initialize(GLSurface::Format format) { | |
106 format_ = format; | |
107 return Initialize(ozone_surface_->CreateVSyncProvider()); | |
108 } | |
109 | |
110 bool GLSurfaceOzoneEGL::Resize(const gfx::Size& size, | |
111 float scale_factor, | |
112 bool has_alpha) { | |
113 if (!ozone_surface_->ResizeNativeWindow(size)) { | |
114 if (!ReinitializeNativeSurface() || | |
115 !ozone_surface_->ResizeNativeWindow(size)) | |
116 return false; | |
117 } | |
118 | |
119 return NativeViewGLSurfaceEGL::Resize(size, scale_factor, has_alpha); | |
120 } | |
121 | |
122 gfx::SwapResult GLSurfaceOzoneEGL::SwapBuffers() { | |
123 gfx::SwapResult result = NativeViewGLSurfaceEGL::SwapBuffers(); | |
124 if (result != gfx::SwapResult::SWAP_ACK) | |
125 return result; | |
126 | |
127 return ozone_surface_->OnSwapBuffers() ? gfx::SwapResult::SWAP_ACK | |
128 : gfx::SwapResult::SWAP_FAILED; | |
129 } | |
130 | |
131 bool GLSurfaceOzoneEGL::ScheduleOverlayPlane(int z_order, | |
132 gfx::OverlayTransform transform, | |
133 GLImage* image, | |
134 const gfx::Rect& bounds_rect, | |
135 const gfx::RectF& crop_rect) { | |
136 return image->ScheduleOverlayPlane(widget_, z_order, transform, bounds_rect, | |
137 crop_rect); | |
138 } | |
139 | |
140 EGLConfig GLSurfaceOzoneEGL::GetConfig() { | |
141 if (!config_) { | |
142 ui::EglConfigCallbacks callbacks = GetEglConfigCallbacks(GetDisplay()); | |
143 config_ = ozone_surface_->GetEGLSurfaceConfig(callbacks); | |
144 } | |
145 if (config_) | |
146 return config_; | |
147 return NativeViewGLSurfaceEGL::GetConfig(); | |
148 } | |
149 | |
150 GLSurfaceOzoneEGL::~GLSurfaceOzoneEGL() { | |
151 Destroy(); // The EGL surface must be destroyed before SurfaceOzone. | |
152 } | |
153 | |
154 bool GLSurfaceOzoneEGL::ReinitializeNativeSurface() { | |
155 std::unique_ptr<ui::ScopedMakeCurrent> scoped_make_current; | |
156 GLContext* current_context = GLContext::GetCurrent(); | |
157 bool was_current = current_context && current_context->IsCurrent(this); | |
158 if (was_current) { | |
159 scoped_make_current.reset(new ui::ScopedMakeCurrent(current_context, this)); | |
160 } | |
161 | |
162 Destroy(); | |
163 ozone_surface_ = ui::OzonePlatform::GetInstance() | |
164 ->GetSurfaceFactoryOzone() | |
165 ->CreateEGLSurfaceForWidget(widget_); | |
166 if (!ozone_surface_) { | |
167 LOG(ERROR) << "Failed to create native surface."; | |
168 return false; | |
169 } | |
170 | |
171 window_ = ozone_surface_->GetNativeWindow(); | |
172 if (!Initialize(format_)) { | |
173 LOG(ERROR) << "Failed to initialize."; | |
174 return false; | |
175 } | |
176 | |
177 return true; | |
178 } | |
179 | |
180 } // namespace | |
181 | |
182 scoped_refptr<GLSurface> CreateViewGLSurfaceOzone( | |
183 gfx::AcceleratedWidget window) { | |
184 std::unique_ptr<ui::SurfaceOzoneEGL> surface_ozone = | |
185 ui::OzonePlatform::GetInstance() | |
186 ->GetSurfaceFactoryOzone() | |
187 ->CreateEGLSurfaceForWidget(window); | |
188 if (!surface_ozone) | |
189 return nullptr; | |
190 return InitializeGLSurface( | |
191 new GLSurfaceOzoneEGL(std::move(surface_ozone), window)); | |
192 } | |
193 | |
194 } // namespace gl | |
OLD | NEW |