summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/demo/gl_renderer.cc
blob: 191d343cf2ca3261314a0cdba75bae4879ee0ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/demo/gl_renderer.h"

#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_surface.h"

namespace ui {

GlRenderer::GlRenderer(gfx::AcceleratedWidget widget, const gfx::Size& size)
    : RendererBase(widget, size) {
}

GlRenderer::~GlRenderer() {
}

bool GlRenderer::Initialize() {
  surface_ = CreateSurface();
  if (!surface_.get()) {
    LOG(ERROR) << "Failed to create GL surface";
    return false;
  }

  context_ = gfx::GLContext::CreateGLContext(NULL, surface_.get(),
                                             gfx::PreferIntegratedGpu);
  if (!context_.get()) {
    LOG(ERROR) << "Failed to create GL context";
    return false;
  }

  surface_->Resize(size_);

  if (!context_->MakeCurrent(surface_.get())) {
    LOG(ERROR) << "Failed to make GL context current";
    return false;
  }

  return true;
}

void GlRenderer::RenderFrame() {
  float fraction = NextFraction();

  context_->MakeCurrent(surface_.get());

  glViewport(0, 0, size_.width(), size_.height());
  glClearColor(1 - fraction, fraction, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  if (!surface_->SwapBuffers())
    LOG(FATAL) << "Failed to swap buffers";
}

scoped_refptr<gfx::GLSurface> GlRenderer::CreateSurface() {
  return gfx::GLSurface::CreateViewGLSurface(widget_);
}

}  // namespace ui