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 "ui/gl/gl_bindings.h"
8 #include "ui/gl/gl_context.h"
9 #include "ui/gl/gl_surface.h"
13 GlRenderer::GlRenderer(gfx::AcceleratedWidget widget
, const gfx::Size
& size
)
14 : RendererBase(widget
, size
) {
17 GlRenderer::~GlRenderer() {
20 bool GlRenderer::Initialize() {
21 surface_
= CreateSurface();
22 if (!surface_
.get()) {
23 LOG(ERROR
) << "Failed to create GL surface";
27 context_
= gfx::GLContext::CreateGLContext(NULL
, surface_
.get(),
28 gfx::PreferIntegratedGpu
);
29 if (!context_
.get()) {
30 LOG(ERROR
) << "Failed to create GL context";
34 surface_
->Resize(size_
);
36 if (!context_
->MakeCurrent(surface_
.get())) {
37 LOG(ERROR
) << "Failed to make GL context current";
44 void GlRenderer::RenderFrame() {
45 float fraction
= NextFraction();
47 context_
->MakeCurrent(surface_
.get());
49 glViewport(0, 0, size_
.width(), size_
.height());
50 glClearColor(1 - fraction
, fraction
, 0.0, 1.0);
51 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
53 if (!surface_
->SwapBuffers())
54 LOG(FATAL
) << "Failed to swap buffers";
57 scoped_refptr
<gfx::GLSurface
> GlRenderer::CreateSurface() {
58 return gfx::GLSurface::CreateViewGLSurface(widget_
);