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"
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";
32 surface_
->Resize(size_
);
34 if (!context_
->MakeCurrent(surface_
.get())) {
35 LOG(ERROR
) << "Failed to make GL context current";
39 PostRenderFrameTask(gfx::SwapResult::SWAP_ACK
);
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(
62 base::Bind(&GlRenderer::RenderFrame
, weak_ptr_factory_
.GetWeakPtr()));