Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / gl / gl_surface_ozone.cc
blobb2b96414d459415ac9568fe02e6b4690595c9ee1
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/gl/gl_surface.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "ui/gfx/native_widget_types.h"
10 #include "ui/gl/gl_context.h"
11 #include "ui/gl/gl_image.h"
12 #include "ui/gl/gl_implementation.h"
13 #include "ui/gl/gl_surface_egl.h"
14 #include "ui/gl/gl_surface_osmesa.h"
15 #include "ui/gl/gl_surface_stub.h"
16 #include "ui/gl/scoped_make_current.h"
17 #include "ui/ozone/public/surface_factory_ozone.h"
18 #include "ui/ozone/public/surface_ozone_egl.h"
20 namespace gfx {
22 namespace {
24 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow
25 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL {
26 public:
27 GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
28 AcceleratedWidget widget)
29 : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()),
30 ozone_surface_(ozone_surface.Pass()),
31 widget_(widget) {}
33 virtual bool Initialize() OVERRIDE {
34 return Initialize(ozone_surface_->CreateVSyncProvider());
36 virtual bool Resize(const gfx::Size& size) OVERRIDE {
37 if (!ozone_surface_->ResizeNativeWindow(size)) {
38 if (!ReinitializeNativeSurface() ||
39 !ozone_surface_->ResizeNativeWindow(size))
40 return false;
43 return NativeViewGLSurfaceEGL::Resize(size);
45 virtual bool SwapBuffers() OVERRIDE {
46 if (!NativeViewGLSurfaceEGL::SwapBuffers())
47 return false;
49 return ozone_surface_->OnSwapBuffers();
51 virtual bool ScheduleOverlayPlane(int z_order,
52 OverlayTransform transform,
53 GLImage* image,
54 const Rect& bounds_rect,
55 const RectF& crop_rect) OVERRIDE {
56 return image->ScheduleOverlayPlane(
57 widget_, z_order, transform, bounds_rect, crop_rect);
60 private:
61 using NativeViewGLSurfaceEGL::Initialize;
63 virtual ~GLSurfaceOzoneEGL() {
64 Destroy(); // EGL surface must be destroyed before SurfaceOzone
67 bool ReinitializeNativeSurface() {
68 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
69 GLContext* current_context = GLContext::GetCurrent();
70 bool was_current =
71 current_context && current_context->IsCurrent(this);
72 if (was_current) {
73 scoped_make_current.reset(
74 new ui::ScopedMakeCurrent(current_context, this));
77 Destroy();
78 ozone_surface_ =
79 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(
80 widget_).Pass();
81 if (!ozone_surface_) {
82 LOG(ERROR) << "Failed to create native surface.";
83 return false;
86 window_ = ozone_surface_->GetNativeWindow();
87 if (!Initialize()) {
88 LOG(ERROR) << "Failed to initialize.";
89 return false;
92 return true;
95 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
96 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
97 AcceleratedWidget widget_;
99 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL);
102 class GL_EXPORT GLSurfaceOzoneSurfaceless : public SurfacelessEGL {
103 public:
104 GLSurfaceOzoneSurfaceless(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
105 AcceleratedWidget widget)
106 : SurfacelessEGL(gfx::Size()),
107 ozone_surface_(ozone_surface.Pass()),
108 widget_(widget) {}
110 virtual bool Initialize() OVERRIDE {
111 if (!SurfacelessEGL::Initialize())
112 return false;
113 vsync_provider_ = ozone_surface_->CreateVSyncProvider();
114 if (!vsync_provider_)
115 return false;
116 return true;
118 virtual bool Resize(const gfx::Size& size) OVERRIDE {
119 if (!ozone_surface_->ResizeNativeWindow(size))
120 return false;
122 return SurfacelessEGL::Resize(size);
124 virtual bool SwapBuffers() OVERRIDE {
125 return ozone_surface_->OnSwapBuffers();
127 virtual bool ScheduleOverlayPlane(int z_order,
128 OverlayTransform transform,
129 GLImage* image,
130 const Rect& bounds_rect,
131 const RectF& crop_rect) OVERRIDE {
132 return image->ScheduleOverlayPlane(
133 widget_, z_order, transform, bounds_rect, crop_rect);
135 virtual bool IsOffscreen() OVERRIDE { return false; }
136 virtual VSyncProvider* GetVSyncProvider() OVERRIDE {
137 return vsync_provider_.get();
140 private:
141 virtual ~GLSurfaceOzoneSurfaceless() {
142 Destroy(); // EGL surface must be destroyed before SurfaceOzone
145 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
146 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
147 AcceleratedWidget widget_;
148 scoped_ptr<VSyncProvider> vsync_provider_;
150 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless);
153 } // namespace
155 // static
156 bool GLSurface::InitializeOneOffInternal() {
157 switch (GetGLImplementation()) {
158 case kGLImplementationEGLGLES2:
159 if (!GLSurfaceEGL::InitializeOneOff()) {
160 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
161 return false;
164 return true;
165 case kGLImplementationOSMesaGL:
166 case kGLImplementationMockGL:
167 return true;
168 default:
169 return false;
173 // static
174 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
175 gfx::AcceleratedWidget window) {
176 if (GetGLImplementation() == kGLImplementationOSMesaGL) {
177 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
178 if (!surface->Initialize())
179 return NULL;
180 return surface;
182 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2);
183 if (window != kNullAcceleratedWidget) {
184 scoped_refptr<GLSurface> surface;
185 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
186 ui::SurfaceFactoryOzone::GetInstance()
187 ->CanShowPrimaryPlaneAsOverlay()) {
188 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
189 ui::SurfaceFactoryOzone::GetInstance()
190 ->CreateSurfacelessEGLSurfaceForWidget(window);
191 if (!surface_ozone)
192 return NULL;
193 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window);
194 } else {
195 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
196 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(
197 window);
198 if (!surface_ozone)
199 return NULL;
201 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window);
203 if (!surface->Initialize())
204 return NULL;
205 return surface;
206 } else {
207 scoped_refptr<GLSurface> surface = new GLSurfaceStub();
208 if (surface->Initialize())
209 return surface;
211 return NULL;
214 // static
215 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
216 const gfx::Size& size) {
217 switch (GetGLImplementation()) {
218 case kGLImplementationOSMesaGL: {
219 scoped_refptr<GLSurface> surface(
220 new GLSurfaceOSMesa(OSMesaSurfaceFormatBGRA, size));
221 if (!surface->Initialize())
222 return NULL;
224 return surface;
226 case kGLImplementationEGLGLES2: {
227 scoped_refptr<GLSurface> surface;
228 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
229 (size.width() == 0 && size.height() == 0)) {
230 surface = new SurfacelessEGL(size);
231 } else
232 surface = new PbufferGLSurfaceEGL(size);
234 if (!surface->Initialize())
235 return NULL;
236 return surface;
238 default:
239 NOTREACHED();
240 return NULL;
244 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
245 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
248 } // namespace gfx