Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / ui / ozone / demo / gl_renderer.cc
blob015bbde593eb45899e2df52d2985db626cb923ac
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 "base/trace_event/trace_event.h"
10 #include "ui/gl/gl_bindings.h"
11 #include "ui/gl/gl_context.h"
12 #include "ui/gl/gl_surface.h"
14 namespace ui {
16 GlRenderer::GlRenderer(gfx::AcceleratedWidget widget,
17 const scoped_refptr<gfx::GLSurface>& surface,
18 const gfx::Size& size)
19 : RendererBase(widget, size), surface_(surface), weak_ptr_factory_(this) {}
21 GlRenderer::~GlRenderer() {
24 bool GlRenderer::Initialize() {
25 context_ = gfx::GLContext::CreateGLContext(NULL, surface_.get(),
26 gfx::PreferIntegratedGpu);
27 if (!context_.get()) {
28 LOG(ERROR) << "Failed to create GL context";
29 return false;
32 surface_->Resize(size_);
34 if (!context_->MakeCurrent(surface_.get())) {
35 LOG(ERROR) << "Failed to make GL context current";
36 return false;
39 PostRenderFrameTask(gfx::SwapResult::SWAP_ACK);
40 return true;
43 void GlRenderer::RenderFrame() {
44 TRACE_EVENT0("ozone", "GlRenderer::RenderFrame");
46 float fraction = NextFraction();
48 context_->MakeCurrent(surface_.get());
50 glViewport(0, 0, size_.width(), size_.height());
51 glClearColor(1 - fraction, fraction, 0.0, 1.0);
52 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54 if (!surface_->SwapBuffersAsync(base::Bind(&GlRenderer::PostRenderFrameTask,
55 weak_ptr_factory_.GetWeakPtr())))
56 LOG(FATAL) << "Failed to swap buffers";
59 void GlRenderer::PostRenderFrameTask(gfx::SwapResult result) {
60 base::ThreadTaskRunnerHandle::Get()->PostTask(
61 FROM_HERE,
62 base::Bind(&GlRenderer::RenderFrame, weak_ptr_factory_.GetWeakPtr()));
65 } // namespace ui