Fix OOP <webview> resize and autosize.
[chromium-blink-merge.git] / ui / ozone / demo / gl_renderer.cc
blobbd6470eed9c0d3f62e7af3abe25ee936568594ff
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/gl_renderer.h"
7 #include "base/location.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "ui/gl/gl_bindings.h"
10 #include "ui/gl/gl_context.h"
11 #include "ui/gl/gl_surface.h"
13 namespace ui {
15 GlRenderer::GlRenderer(gfx::AcceleratedWidget widget, const gfx::Size& size)
16 : RendererBase(widget, size), weak_ptr_factory_(this) {
19 GlRenderer::~GlRenderer() {
22 bool GlRenderer::Initialize() {
23 surface_ = CreateSurface();
24 if (!surface_.get()) {
25 LOG(ERROR) << "Failed to create GL surface";
26 return false;
29 context_ = gfx::GLContext::CreateGLContext(NULL, surface_.get(),
30 gfx::PreferIntegratedGpu);
31 if (!context_.get()) {
32 LOG(ERROR) << "Failed to create GL context";
33 return false;
36 surface_->Resize(size_);
38 if (!context_->MakeCurrent(surface_.get())) {
39 LOG(ERROR) << "Failed to make GL context current";
40 return false;
43 PostRenderFrameTask(gfx::SwapResult::SWAP_ACK);
44 return true;
47 void GlRenderer::RenderFrame() {
48 float fraction = NextFraction();
50 context_->MakeCurrent(surface_.get());
52 glViewport(0, 0, size_.width(), size_.height());
53 glClearColor(1 - fraction, fraction, 0.0, 1.0);
54 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
56 if (!surface_->SwapBuffersAsync(base::Bind(&GlRenderer::PostRenderFrameTask,
57 weak_ptr_factory_.GetWeakPtr())))
58 LOG(FATAL) << "Failed to swap buffers";
61 void GlRenderer::PostRenderFrameTask(gfx::SwapResult result) {
62 base::ThreadTaskRunnerHandle::Get()->PostTask(
63 FROM_HERE,
64 base::Bind(&GlRenderer::RenderFrame, weak_ptr_factory_.GetWeakPtr()));
67 scoped_refptr<gfx::GLSurface> GlRenderer::CreateSurface() {
68 return gfx::GLSurface::CreateViewGLSurface(widget_);
71 } // namespace ui