OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/ozone/demo/gl_renderer.h" |
| 6 |
| 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" |
| 12 |
| 13 namespace ui { |
| 14 |
| 15 GlRenderer::GlRenderer(gfx::AcceleratedWidget widget, const gfx::Size& size) |
| 16 : RendererBase(widget, size), weak_ptr_factory_(this) { |
| 17 } |
| 18 |
| 19 GlRenderer::~GlRenderer() { |
| 20 } |
| 21 |
| 22 bool GlRenderer::Initialize() { |
| 23 surface_ = CreateSurface(); |
| 24 if (!surface_.get()) { |
| 25 LOG(ERROR) << "Failed to create GL surface"; |
| 26 return false; |
| 27 } |
| 28 |
| 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; |
| 34 } |
| 35 |
| 36 if (!surface_->Resize(size_)) { |
| 37 LOG(ERROR) << "Failed to resize GL surface"; |
| 38 return false; |
| 39 } |
| 40 |
| 41 if (!context_->MakeCurrent(surface_.get())) { |
| 42 LOG(ERROR) << "Failed to make GL context current"; |
| 43 return false; |
| 44 } |
| 45 |
| 46 PostRenderFrameTask(gfx::SwapResult::SWAP_ACK); |
| 47 return true; |
| 48 } |
| 49 |
| 50 void GlRenderer::RenderFrame() { |
| 51 float fraction = NextFraction(); |
| 52 |
| 53 context_->MakeCurrent(surface_.get()); |
| 54 |
| 55 glViewport(0, 0, size_.width(), size_.height()); |
| 56 glClearColor(1 - fraction, fraction, 0.0, 1.0); |
| 57 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 58 |
| 59 if (!surface_->SwapBuffersAsync(base::Bind(&GlRenderer::PostRenderFrameTask, |
| 60 weak_ptr_factory_.GetWeakPtr()))) |
| 61 LOG(FATAL) << "Failed to swap buffers"; |
| 62 } |
| 63 |
| 64 void GlRenderer::PostRenderFrameTask(gfx::SwapResult result) { |
| 65 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 66 FROM_HERE, |
| 67 base::Bind(&GlRenderer::RenderFrame, weak_ptr_factory_.GetWeakPtr())); |
| 68 } |
| 69 |
| 70 scoped_refptr<gfx::GLSurface> GlRenderer::CreateSurface() { |
| 71 gfx::SurfaceConfiguration config; |
| 72 return gfx::GLSurface::CreateViewGLSurface(widget_, config); |
| 73 } |
| 74 |
| 75 } // namespace ui |
OLD | NEW |