Add ICU message format support
[chromium-blink-merge.git] / ui / gl / gl_surface_ozone.cc
blob9f874d4e87bf25f6fc8201b92e620d4f393508f8
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/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/worker_pool.h"
15 #include "ui/gfx/native_widget_types.h"
16 #include "ui/gl/gl_context.h"
17 #include "ui/gl/gl_image.h"
18 #include "ui/gl/gl_image_ozone_native_pixmap.h"
19 #include "ui/gl/gl_implementation.h"
20 #include "ui/gl/gl_surface_egl.h"
21 #include "ui/gl/gl_surface_osmesa.h"
22 #include "ui/gl/gl_surface_stub.h"
23 #include "ui/gl/scoped_binders.h"
24 #include "ui/gl/scoped_make_current.h"
25 #include "ui/ozone/public/native_pixmap.h"
26 #include "ui/ozone/public/ozone_platform.h"
27 #include "ui/ozone/public/surface_factory_ozone.h"
28 #include "ui/ozone/public/surface_ozone_egl.h"
30 namespace gfx {
32 namespace {
34 void WaitForFence(EGLDisplay display, EGLSyncKHR fence) {
35 eglClientWaitSyncKHR(display, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
36 EGL_FOREVER_KHR);
39 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow
40 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL {
41 public:
42 GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
43 AcceleratedWidget widget);
45 // GLSurface:
46 bool Initialize() override;
47 bool Resize(const gfx::Size& size) override;
48 gfx::SwapResult SwapBuffers() override;
49 bool ScheduleOverlayPlane(int z_order,
50 OverlayTransform transform,
51 GLImage* image,
52 const Rect& bounds_rect,
53 const RectF& crop_rect) override;
55 private:
56 using NativeViewGLSurfaceEGL::Initialize;
58 ~GLSurfaceOzoneEGL() override;
60 bool ReinitializeNativeSurface();
62 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
63 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
64 AcceleratedWidget widget_;
66 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL);
69 GLSurfaceOzoneEGL::GLSurfaceOzoneEGL(
70 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
71 AcceleratedWidget widget)
72 : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()),
73 ozone_surface_(ozone_surface.Pass()),
74 widget_(widget) {
77 bool GLSurfaceOzoneEGL::Initialize() {
78 return Initialize(ozone_surface_->CreateVSyncProvider());
81 bool GLSurfaceOzoneEGL::Resize(const gfx::Size& size) {
82 if (!ozone_surface_->ResizeNativeWindow(size)) {
83 if (!ReinitializeNativeSurface() ||
84 !ozone_surface_->ResizeNativeWindow(size))
85 return false;
88 return NativeViewGLSurfaceEGL::Resize(size);
91 gfx::SwapResult GLSurfaceOzoneEGL::SwapBuffers() {
92 gfx::SwapResult result = NativeViewGLSurfaceEGL::SwapBuffers();
93 if (result != gfx::SwapResult::SWAP_ACK)
94 return result;
96 return ozone_surface_->OnSwapBuffers() ? gfx::SwapResult::SWAP_ACK
97 : gfx::SwapResult::SWAP_FAILED;
100 bool GLSurfaceOzoneEGL::ScheduleOverlayPlane(int z_order,
101 OverlayTransform transform,
102 GLImage* image,
103 const Rect& bounds_rect,
104 const RectF& crop_rect) {
105 return image->ScheduleOverlayPlane(widget_, z_order, transform, bounds_rect,
106 crop_rect);
109 GLSurfaceOzoneEGL::~GLSurfaceOzoneEGL() {
110 Destroy(); // EGL surface must be destroyed before SurfaceOzone
113 bool GLSurfaceOzoneEGL::ReinitializeNativeSurface() {
114 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
115 GLContext* current_context = GLContext::GetCurrent();
116 bool was_current = current_context && current_context->IsCurrent(this);
117 if (was_current) {
118 scoped_make_current.reset(new ui::ScopedMakeCurrent(current_context, this));
121 Destroy();
122 ozone_surface_ = ui::OzonePlatform::GetInstance()
123 ->GetSurfaceFactoryOzone()
124 ->CreateEGLSurfaceForWidget(widget_)
125 .Pass();
126 if (!ozone_surface_) {
127 LOG(ERROR) << "Failed to create native surface.";
128 return false;
131 window_ = ozone_surface_->GetNativeWindow();
132 if (!Initialize()) {
133 LOG(ERROR) << "Failed to initialize.";
134 return false;
137 return true;
140 class GL_EXPORT GLSurfaceOzoneSurfaceless : public SurfacelessEGL {
141 public:
142 GLSurfaceOzoneSurfaceless(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
143 AcceleratedWidget widget);
145 // GLSurface:
146 bool Initialize() override;
147 bool Resize(const gfx::Size& size) override;
148 gfx::SwapResult SwapBuffers() override;
149 bool ScheduleOverlayPlane(int z_order,
150 OverlayTransform transform,
151 GLImage* image,
152 const Rect& bounds_rect,
153 const RectF& crop_rect) override;
154 bool IsOffscreen() override;
155 VSyncProvider* GetVSyncProvider() override;
156 bool SupportsPostSubBuffer() override;
157 gfx::SwapResult PostSubBuffer(int x, int y, int width, int height) override;
158 bool SwapBuffersAsync(const SwapCompletionCallback& callback) override;
159 bool PostSubBufferAsync(int x,
160 int y,
161 int width,
162 int height,
163 const SwapCompletionCallback& callback) override;
165 protected:
166 struct Overlay {
167 Overlay(int z_order,
168 OverlayTransform transform,
169 GLImage* image,
170 const Rect& bounds_rect,
171 const RectF& crop_rect);
173 bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget) const;
175 int z_order;
176 OverlayTransform transform;
177 scoped_refptr<GLImage> image;
178 Rect bounds_rect;
179 RectF crop_rect;
182 struct PendingFrame {
183 PendingFrame();
185 bool ScheduleOverlayPlanes(gfx::AcceleratedWidget widget);
187 bool ready;
188 std::vector<Overlay> overlays;
189 SwapCompletionCallback callback;
192 ~GLSurfaceOzoneSurfaceless() override;
194 void SubmitFrame();
196 EGLSyncKHR InsertFence();
197 void FenceRetired(EGLSyncKHR fence, PendingFrame* frame);
199 void SwapCompleted(const SwapCompletionCallback& callback,
200 gfx::SwapResult result);
202 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
203 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
204 AcceleratedWidget widget_;
205 scoped_ptr<VSyncProvider> vsync_provider_;
206 ScopedVector<PendingFrame> unsubmitted_frames_;
207 bool has_implicit_external_sync_;
208 bool last_swap_buffers_result_;
209 bool swap_buffers_pending_;
211 base::WeakPtrFactory<GLSurfaceOzoneSurfaceless> weak_factory_;
213 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless);
216 GLSurfaceOzoneSurfaceless::Overlay::Overlay(int z_order,
217 OverlayTransform transform,
218 GLImage* image,
219 const Rect& bounds_rect,
220 const RectF& crop_rect)
221 : z_order(z_order),
222 transform(transform),
223 image(image),
224 bounds_rect(bounds_rect),
225 crop_rect(crop_rect) {
228 bool GLSurfaceOzoneSurfaceless::Overlay::ScheduleOverlayPlane(
229 gfx::AcceleratedWidget widget) const {
230 return image->ScheduleOverlayPlane(widget, z_order, transform, bounds_rect,
231 crop_rect);
234 GLSurfaceOzoneSurfaceless::PendingFrame::PendingFrame() : ready(false) {
237 bool GLSurfaceOzoneSurfaceless::PendingFrame::ScheduleOverlayPlanes(
238 gfx::AcceleratedWidget widget) {
239 for (const auto& overlay : overlays)
240 if (!overlay.ScheduleOverlayPlane(widget))
241 return false;
242 return true;
245 GLSurfaceOzoneSurfaceless::GLSurfaceOzoneSurfaceless(
246 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
247 AcceleratedWidget widget)
248 : SurfacelessEGL(gfx::Size()),
249 ozone_surface_(ozone_surface.Pass()),
250 widget_(widget),
251 has_implicit_external_sync_(
252 HasEGLExtension("EGL_ARM_implicit_external_sync")),
253 last_swap_buffers_result_(true),
254 swap_buffers_pending_(false),
255 weak_factory_(this) {
256 unsubmitted_frames_.push_back(new PendingFrame());
259 bool GLSurfaceOzoneSurfaceless::Initialize() {
260 if (!SurfacelessEGL::Initialize())
261 return false;
262 vsync_provider_ = ozone_surface_->CreateVSyncProvider();
263 if (!vsync_provider_)
264 return false;
265 return true;
267 bool GLSurfaceOzoneSurfaceless::Resize(const gfx::Size& size) {
268 if (!ozone_surface_->ResizeNativeWindow(size))
269 return false;
271 return SurfacelessEGL::Resize(size);
273 gfx::SwapResult GLSurfaceOzoneSurfaceless::SwapBuffers() {
274 glFlush();
275 // TODO: the following should be replaced by a per surface flush as it gets
276 // implemented in GL drivers.
277 if (has_implicit_external_sync_) {
278 EGLSyncKHR fence = InsertFence();
279 if (!fence)
280 return SwapResult::SWAP_FAILED;
282 EGLDisplay display = GetDisplay();
283 WaitForFence(display, fence);
284 eglDestroySyncKHR(display, fence);
285 } else if (ozone_surface_->IsUniversalDisplayLinkDevice()) {
286 glFinish();
289 unsubmitted_frames_.back()->ScheduleOverlayPlanes(widget_);
290 unsubmitted_frames_.back()->overlays.clear();
292 return ozone_surface_->OnSwapBuffers() ? gfx::SwapResult::SWAP_ACK
293 : gfx::SwapResult::SWAP_FAILED;
295 bool GLSurfaceOzoneSurfaceless::ScheduleOverlayPlane(int z_order,
296 OverlayTransform transform,
297 GLImage* image,
298 const Rect& bounds_rect,
299 const RectF& crop_rect) {
300 unsubmitted_frames_.back()->overlays.push_back(
301 Overlay(z_order, transform, image, bounds_rect, crop_rect));
302 return true;
304 bool GLSurfaceOzoneSurfaceless::IsOffscreen() {
305 return false;
307 VSyncProvider* GLSurfaceOzoneSurfaceless::GetVSyncProvider() {
308 return vsync_provider_.get();
310 bool GLSurfaceOzoneSurfaceless::SupportsPostSubBuffer() {
311 return true;
313 gfx::SwapResult GLSurfaceOzoneSurfaceless::PostSubBuffer(int x,
314 int y,
315 int width,
316 int height) {
317 // The actual sub buffer handling is handled at higher layers.
318 SwapBuffers();
319 return gfx::SwapResult::SWAP_ACK;
321 bool GLSurfaceOzoneSurfaceless::SwapBuffersAsync(
322 const SwapCompletionCallback& callback) {
323 // If last swap failed, don't try to schedule new ones.
324 if (!last_swap_buffers_result_)
325 return false;
327 glFlush();
329 SwapCompletionCallback surface_swap_callback =
330 base::Bind(&GLSurfaceOzoneSurfaceless::SwapCompleted,
331 weak_factory_.GetWeakPtr(), callback);
333 PendingFrame* frame = unsubmitted_frames_.back();
334 frame->callback = surface_swap_callback;
335 unsubmitted_frames_.push_back(new PendingFrame());
337 // TODO: the following should be replaced by a per surface flush as it gets
338 // implemented in GL drivers.
339 if (has_implicit_external_sync_) {
340 EGLSyncKHR fence = InsertFence();
341 if (!fence)
342 return false;
344 base::Closure fence_wait_task =
345 base::Bind(&WaitForFence, GetDisplay(), fence);
347 base::Closure fence_retired_callback =
348 base::Bind(&GLSurfaceOzoneSurfaceless::FenceRetired,
349 weak_factory_.GetWeakPtr(), fence, frame);
351 base::WorkerPool::PostTaskAndReply(FROM_HERE, fence_wait_task,
352 fence_retired_callback, false);
353 return true;
354 } else if (ozone_surface_->IsUniversalDisplayLinkDevice()) {
355 glFinish();
358 frame->ready = true;
359 SubmitFrame();
360 return last_swap_buffers_result_;
362 bool GLSurfaceOzoneSurfaceless::PostSubBufferAsync(
363 int x,
364 int y,
365 int width,
366 int height,
367 const SwapCompletionCallback& callback) {
368 return SwapBuffersAsync(callback);
371 GLSurfaceOzoneSurfaceless::~GLSurfaceOzoneSurfaceless() {
372 Destroy(); // EGL surface must be destroyed before SurfaceOzone
375 void GLSurfaceOzoneSurfaceless::SubmitFrame() {
376 DCHECK(!unsubmitted_frames_.empty());
378 if (unsubmitted_frames_.front()->ready && !swap_buffers_pending_) {
379 scoped_ptr<PendingFrame> frame(unsubmitted_frames_.front());
380 unsubmitted_frames_.weak_erase(unsubmitted_frames_.begin());
381 swap_buffers_pending_ = true;
383 last_swap_buffers_result_ =
384 frame->ScheduleOverlayPlanes(widget_) &&
385 ozone_surface_->OnSwapBuffersAsync(frame->callback);
389 EGLSyncKHR GLSurfaceOzoneSurfaceless::InsertFence() {
390 const EGLint attrib_list[] = {EGL_SYNC_CONDITION_KHR,
391 EGL_SYNC_PRIOR_COMMANDS_IMPLICIT_EXTERNAL_ARM,
392 EGL_NONE};
393 return eglCreateSyncKHR(GetDisplay(), EGL_SYNC_FENCE_KHR, attrib_list);
396 void GLSurfaceOzoneSurfaceless::FenceRetired(EGLSyncKHR fence,
397 PendingFrame* frame) {
398 eglDestroySyncKHR(GetDisplay(), fence);
399 frame->ready = true;
400 SubmitFrame();
403 void GLSurfaceOzoneSurfaceless::SwapCompleted(
404 const SwapCompletionCallback& callback,
405 gfx::SwapResult result) {
406 callback.Run(result);
407 swap_buffers_pending_ = false;
409 SubmitFrame();
412 // This provides surface-like semantics implemented through surfaceless.
413 // A framebuffer is bound automatically.
414 class GL_EXPORT GLSurfaceOzoneSurfacelessSurfaceImpl
415 : public GLSurfaceOzoneSurfaceless {
416 public:
417 GLSurfaceOzoneSurfacelessSurfaceImpl(
418 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
419 AcceleratedWidget widget);
421 // GLSurface:
422 unsigned int GetBackingFrameBufferObject() override;
423 bool OnMakeCurrent(GLContext* context) override;
424 bool Resize(const gfx::Size& size) override;
425 bool SupportsPostSubBuffer() override;
426 gfx::SwapResult SwapBuffers() override;
427 bool SwapBuffersAsync(const SwapCompletionCallback& callback) override;
428 void Destroy() override;
430 private:
431 ~GLSurfaceOzoneSurfacelessSurfaceImpl() override;
433 void BindFramebuffer();
434 bool CreatePixmaps();
436 scoped_refptr<GLContext> context_;
437 GLuint fbo_;
438 GLuint textures_[2];
439 scoped_refptr<GLImage> images_[2];
440 int current_surface_;
441 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfacelessSurfaceImpl);
444 GLSurfaceOzoneSurfacelessSurfaceImpl::GLSurfaceOzoneSurfacelessSurfaceImpl(
445 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
446 AcceleratedWidget widget)
447 : GLSurfaceOzoneSurfaceless(ozone_surface.Pass(), widget),
448 context_(nullptr),
449 fbo_(0),
450 current_surface_(0) {
451 for (auto& texture : textures_)
452 texture = 0;
455 unsigned int
456 GLSurfaceOzoneSurfacelessSurfaceImpl::GetBackingFrameBufferObject() {
457 return fbo_;
460 bool GLSurfaceOzoneSurfacelessSurfaceImpl::OnMakeCurrent(GLContext* context) {
461 DCHECK(!context_ || context == context_);
462 context_ = context;
463 if (!fbo_) {
464 glGenFramebuffersEXT(1, &fbo_);
465 if (!fbo_)
466 return false;
467 glGenTextures(arraysize(textures_), textures_);
468 if (!CreatePixmaps())
469 return false;
471 BindFramebuffer();
472 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_);
473 return SurfacelessEGL::OnMakeCurrent(context);
476 bool GLSurfaceOzoneSurfacelessSurfaceImpl::Resize(const gfx::Size& size) {
477 if (size == GetSize())
478 return true;
479 return GLSurfaceOzoneSurfaceless::Resize(size) && CreatePixmaps();
482 bool GLSurfaceOzoneSurfacelessSurfaceImpl::SupportsPostSubBuffer() {
483 return false;
486 gfx::SwapResult GLSurfaceOzoneSurfacelessSurfaceImpl::SwapBuffers() {
487 if (!images_[current_surface_]->ScheduleOverlayPlane(
488 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE,
489 gfx::Rect(GetSize()), gfx::RectF(1, 1)))
490 return gfx::SwapResult::SWAP_FAILED;
491 gfx::SwapResult result = GLSurfaceOzoneSurfaceless::SwapBuffers();
492 if (result != gfx::SwapResult::SWAP_ACK)
493 return result;
494 current_surface_ ^= 1;
495 BindFramebuffer();
496 return gfx::SwapResult::SWAP_ACK;
499 bool GLSurfaceOzoneSurfacelessSurfaceImpl::SwapBuffersAsync(
500 const SwapCompletionCallback& callback) {
501 if (!images_[current_surface_]->ScheduleOverlayPlane(
502 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE,
503 gfx::Rect(GetSize()), gfx::RectF(1, 1)))
504 return false;
505 if (!GLSurfaceOzoneSurfaceless::SwapBuffersAsync(callback))
506 return false;
507 current_surface_ ^= 1;
508 BindFramebuffer();
509 return true;
512 void GLSurfaceOzoneSurfacelessSurfaceImpl::Destroy() {
513 if (!context_)
514 return;
515 ui::ScopedMakeCurrent context(context_.get(), this);
516 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
517 if (fbo_) {
518 glDeleteTextures(arraysize(textures_), textures_);
519 for (auto& texture : textures_)
520 texture = 0;
521 glDeleteFramebuffersEXT(1, &fbo_);
522 fbo_ = 0;
524 for (auto image : images_) {
525 if (image)
526 image->Destroy(true);
530 GLSurfaceOzoneSurfacelessSurfaceImpl::~GLSurfaceOzoneSurfacelessSurfaceImpl() {
531 DCHECK(!fbo_);
532 for (size_t i = 0; i < arraysize(textures_); i++)
533 DCHECK(!textures_[i]) << "texture " << i << " not released";
536 void GLSurfaceOzoneSurfacelessSurfaceImpl::BindFramebuffer() {
537 ScopedFrameBufferBinder fb(fbo_);
538 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
539 textures_[current_surface_], 0);
542 bool GLSurfaceOzoneSurfacelessSurfaceImpl::CreatePixmaps() {
543 if (!fbo_)
544 return true;
545 for (size_t i = 0; i < arraysize(textures_); i++) {
546 scoped_refptr<ui::NativePixmap> pixmap =
547 ui::OzonePlatform::GetInstance()
548 ->GetSurfaceFactoryOzone()
549 ->CreateNativePixmap(widget_, GetSize(),
550 gfx::BufferFormat::BGRA_8888,
551 gfx::BufferUsage::SCANOUT);
552 if (!pixmap)
553 return false;
554 scoped_refptr<GLImageOzoneNativePixmap> image =
555 new GLImageOzoneNativePixmap(GetSize(), GL_BGRA_EXT);
556 if (!image->Initialize(pixmap.get(), gfx::BufferFormat::BGRA_8888))
557 return false;
558 images_[i] = image;
559 // Bind image to texture.
560 ScopedTextureBinder binder(GL_TEXTURE_2D, textures_[i]);
561 if (!images_[i]->BindTexImage(GL_TEXTURE_2D))
562 return false;
564 return true;
567 } // namespace
569 // static
570 bool GLSurface::InitializeOneOffInternal() {
571 switch (GetGLImplementation()) {
572 case kGLImplementationEGLGLES2:
573 if (!GLSurfaceEGL::InitializeOneOff()) {
574 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
575 return false;
578 return true;
579 case kGLImplementationOSMesaGL:
580 case kGLImplementationMockGL:
581 return true;
582 default:
583 return false;
587 // static
588 scoped_refptr<GLSurface> GLSurface::CreateSurfacelessViewGLSurface(
589 gfx::AcceleratedWidget window) {
590 if (GetGLImplementation() == kGLImplementationEGLGLES2 &&
591 window != kNullAcceleratedWidget &&
592 GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
593 ui::OzonePlatform::GetInstance()
594 ->GetSurfaceFactoryOzone()
595 ->CanShowPrimaryPlaneAsOverlay()) {
596 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
597 ui::OzonePlatform::GetInstance()
598 ->GetSurfaceFactoryOzone()
599 ->CreateSurfacelessEGLSurfaceForWidget(window);
600 if (!surface_ozone)
601 return nullptr;
602 scoped_refptr<GLSurface> surface;
603 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window);
604 if (surface->Initialize())
605 return surface;
608 return nullptr;
611 // static
612 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
613 gfx::AcceleratedWidget window) {
614 if (GetGLImplementation() == kGLImplementationOSMesaGL) {
615 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
616 if (!surface->Initialize())
617 return NULL;
618 return surface;
620 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2);
621 if (window != kNullAcceleratedWidget) {
622 scoped_refptr<GLSurface> surface;
623 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
624 ui::OzonePlatform::GetInstance()
625 ->GetSurfaceFactoryOzone()
626 ->CanShowPrimaryPlaneAsOverlay()) {
627 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
628 ui::OzonePlatform::GetInstance()
629 ->GetSurfaceFactoryOzone()
630 ->CreateSurfacelessEGLSurfaceForWidget(window);
631 if (!surface_ozone)
632 return NULL;
633 surface = new GLSurfaceOzoneSurfacelessSurfaceImpl(surface_ozone.Pass(),
634 window);
635 } else {
636 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
637 ui::OzonePlatform::GetInstance()
638 ->GetSurfaceFactoryOzone()
639 ->CreateEGLSurfaceForWidget(window);
640 if (!surface_ozone)
641 return NULL;
643 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window);
645 if (!surface->Initialize())
646 return NULL;
647 return surface;
648 } else {
649 scoped_refptr<GLSurface> surface = new GLSurfaceStub();
650 if (surface->Initialize())
651 return surface;
653 return NULL;
656 // static
657 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
658 const gfx::Size& size) {
659 switch (GetGLImplementation()) {
660 case kGLImplementationOSMesaGL: {
661 scoped_refptr<GLSurface> surface(
662 new GLSurfaceOSMesa(OSMesaSurfaceFormatBGRA, size));
663 if (!surface->Initialize())
664 return NULL;
666 return surface;
668 case kGLImplementationEGLGLES2: {
669 scoped_refptr<GLSurface> surface;
670 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
671 (size.width() == 0 && size.height() == 0)) {
672 surface = new SurfacelessEGL(size);
673 } else
674 surface = new PbufferGLSurfaceEGL(size);
676 if (!surface->Initialize())
677 return NULL;
678 return surface;
680 case kGLImplementationMockGL:
681 return new GLSurfaceStub;
682 default:
683 NOTREACHED();
684 return NULL;
688 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
689 return ui::OzonePlatform::GetInstance()
690 ->GetSurfaceFactoryOzone()
691 ->GetNativeDisplay();
694 } // namespace gfx