Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / ui / ozone / demo / surfaceless_gl_renderer.cc
blob251580336fd018a91bafef0efc21912cadb52ffb
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/demo/surfaceless_gl_renderer.h"
7 #include "base/bind.h"
8 #include "ui/gl/gl_bindings.h"
9 #include "ui/gl/gl_context.h"
10 #include "ui/gl/gl_image.h"
11 #include "ui/gl/gl_image_ozone_native_pixmap.h"
12 #include "ui/gl/gl_surface.h"
13 #include "ui/ozone/public/ozone_platform.h"
14 #include "ui/ozone/public/surface_factory_ozone.h"
16 namespace ui {
18 SurfacelessGlRenderer::BufferWrapper::BufferWrapper() {
21 SurfacelessGlRenderer::BufferWrapper::~BufferWrapper() {
22 if (gl_fb_)
23 glDeleteFramebuffersEXT(1, &gl_fb_);
25 if (gl_tex_) {
26 image_->ReleaseTexImage(GL_TEXTURE_2D);
27 glDeleteTextures(1, &gl_tex_);
28 image_->Destroy(true);
32 bool SurfacelessGlRenderer::BufferWrapper::Initialize(
33 gfx::AcceleratedWidget widget,
34 const gfx::Size& size) {
35 glGenFramebuffersEXT(1, &gl_fb_);
36 glGenTextures(1, &gl_tex_);
38 scoped_refptr<NativePixmap> pixmap =
39 OzonePlatform::GetInstance()
40 ->GetSurfaceFactoryOzone()
41 ->CreateNativePixmap(widget, size, gfx::BufferFormat::BGRX_8888,
42 gfx::BufferUsage::SCANOUT);
43 scoped_refptr<gfx::GLImageOzoneNativePixmap> image(
44 new gfx::GLImageOzoneNativePixmap(size, GL_RGB));
45 if (!image->Initialize(pixmap.get(), gfx::BufferFormat::BGRX_8888)) {
46 LOG(ERROR) << "Failed to create GLImage";
47 return false;
49 image_ = image;
51 glBindFramebufferEXT(GL_FRAMEBUFFER, gl_fb_);
52 glBindTexture(GL_TEXTURE_2D, gl_tex_);
53 image_->BindTexImage(GL_TEXTURE_2D);
55 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
56 gl_tex_, 0);
57 if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
58 LOG(ERROR) << "Failed to create framebuffer "
59 << glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
60 return false;
63 widget_ = widget;
64 size_ = size;
66 return true;
69 void SurfacelessGlRenderer::BufferWrapper::BindFramebuffer() {
70 glBindFramebufferEXT(GL_FRAMEBUFFER, gl_fb_);
73 void SurfacelessGlRenderer::BufferWrapper::SchedulePlane() {
74 image_->ScheduleOverlayPlane(widget_, 0, gfx::OVERLAY_TRANSFORM_NONE,
75 gfx::Rect(size_), gfx::RectF(0, 0, 1, 1));
78 SurfacelessGlRenderer::SurfacelessGlRenderer(gfx::AcceleratedWidget widget,
79 const gfx::Size& size)
80 : GlRenderer(widget, size), weak_ptr_factory_(this) {}
82 SurfacelessGlRenderer::~SurfacelessGlRenderer() {
83 // Need to make current when deleting the framebuffer resources allocated in
84 // the buffers.
85 context_->MakeCurrent(surface_.get());
88 bool SurfacelessGlRenderer::Initialize() {
89 if (!GlRenderer::Initialize())
90 return false;
92 for (size_t i = 0; i < arraysize(buffers_); ++i)
93 if (!buffers_[i].Initialize(widget_, size_))
94 return false;
96 PostRenderFrameTask(gfx::SwapResult::SWAP_ACK);
97 return true;
100 void SurfacelessGlRenderer::RenderFrame() {
101 float fraction = NextFraction();
103 context_->MakeCurrent(surface_.get());
104 buffers_[back_buffer_].BindFramebuffer();
106 glViewport(0, 0, size_.width(), size_.height());
107 glClearColor(1 - fraction, 0.0, fraction, 1.0);
108 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110 buffers_[back_buffer_].SchedulePlane();
111 back_buffer_ ^= 1;
112 if (!surface_->SwapBuffersAsync(base::Bind(&GlRenderer::PostRenderFrameTask,
113 weak_ptr_factory_.GetWeakPtr())))
114 LOG(FATAL) << "Failed to swap buffers";
117 scoped_refptr<gfx::GLSurface> SurfacelessGlRenderer::CreateSurface() {
118 return gfx::GLSurface::CreateSurfacelessViewGLSurface(widget_);
121 } // namespace ui