summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordnicoara <dnicoara@chromium.org>2014-12-16 10:17:54 -0800
committerCommit bot <commit-bot@chromium.org>2014-12-16 18:19:15 +0000
commitc0411a00aebc99400e293fd9beca14d1acb6d4af (patch)
treea7abe1e62993fcefccf77eb0431738051261041a
parent985ea2968468efc082159c65d0e0c04004af406b (diff)
downloadchromium_src-c0411a00aebc99400e293fd9beca14d1acb6d4af.zip
chromium_src-c0411a00aebc99400e293fd9beca14d1acb6d4af.tar.gz
chromium_src-c0411a00aebc99400e293fd9beca14d1acb6d4af.tar.bz2
[Ozone-Demo] Extract rendering parts into separate renderer classes
Makes it easier to abstract the part that renders to buffers and add new ones. BUG=none NOTRY=true Review URL: https://codereview.chromium.org/805843003 Cr-Commit-Position: refs/heads/master@{#308613}
-rw-r--r--ui/ozone/demo/BUILD.gn7
-rw-r--r--ui/ozone/demo/gl_renderer.cc55
-rw-r--r--ui/ozone/demo/gl_renderer.h37
-rw-r--r--ui/ozone/demo/ozone_demo.cc141
-rw-r--r--ui/ozone/demo/ozone_demos.gyp7
-rw-r--r--ui/ozone/demo/renderer.h21
-rw-r--r--ui/ozone/demo/renderer_base.cc29
-rw-r--r--ui/ozone/demo/renderer_base.h30
-rw-r--r--ui/ozone/demo/software_renderer.cc47
-rw-r--r--ui/ozone/demo/software_renderer.h33
10 files changed, 294 insertions, 113 deletions
diff --git a/ui/ozone/demo/BUILD.gn b/ui/ozone/demo/BUILD.gn
index c58431b..dace7ac 100644
--- a/ui/ozone/demo/BUILD.gn
+++ b/ui/ozone/demo/BUILD.gn
@@ -10,7 +10,14 @@ group("demo") {
executable("ozone_demo") {
sources = [
+ "gl_renderer.cc",
+ "gl_renderer.h",
"ozone_demo.cc",
+ "renderer.h",
+ "renderer_base.cc",
+ "renderer_base.h",
+ "software_renderer.cc",
+ "software_renderer.h",
]
deps = [
diff --git a/ui/ozone/demo/gl_renderer.cc b/ui/ozone/demo/gl_renderer.cc
new file mode 100644
index 0000000..f814149
--- /dev/null
+++ b/ui/ozone/demo/gl_renderer.cc
@@ -0,0 +1,55 @@
+// 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_ = gfx::GLSurface::CreateViewGLSurface(widget_);
+ 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();
+
+ 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";
+}
+
+} // namespace ui
diff --git a/ui/ozone/demo/gl_renderer.h b/ui/ozone/demo/gl_renderer.h
new file mode 100644
index 0000000..2271086
--- /dev/null
+++ b/ui/ozone/demo/gl_renderer.h
@@ -0,0 +1,37 @@
+// 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.
+
+#ifndef UI_OZONE_DEMO_GL_RENDERER_H_
+#define UI_OZONE_DEMO_GL_RENDERER_H_
+
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "ui/ozone/demo/renderer_base.h"
+
+namespace gfx {
+class GLContext;
+class GLSurface;
+} // namespace gfx
+
+namespace ui {
+
+class GlRenderer : public RendererBase {
+ public:
+ GlRenderer(gfx::AcceleratedWidget widget, const gfx::Size& size);
+ ~GlRenderer() override;
+
+ // Renderer:
+ bool Initialize() override;
+ void RenderFrame() override;
+
+ private:
+ scoped_refptr<gfx::GLSurface> surface_;
+ scoped_refptr<gfx::GLContext> context_;
+
+ DISALLOW_COPY_AND_ASSIGN(GlRenderer);
+};
+
+} // namespace ui
+
+#endif // UI_OZONE_DEMO_GL_RENDERER_H_
diff --git a/ui/ozone/demo/ozone_demo.cc b/ui/ozone/demo/ozone_demo.cc
index f2bb1bb1d..291b026 100644
--- a/ui/ozone/demo/ozone_demo.cc
+++ b/ui/ozone/demo/ozone_demo.cc
@@ -7,16 +7,12 @@
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/timer/timer.h"
-#include "third_party/skia/include/core/SkCanvas.h"
-#include "third_party/skia/include/core/SkColor.h"
-#include "third_party/skia/include/core/SkSurface.h"
+#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
-#include "ui/gl/gl_bindings.h"
-#include "ui/gl/gl_context.h"
#include "ui/gl/gl_surface.h"
+#include "ui/ozone/demo/gl_renderer.h"
+#include "ui/ozone/demo/software_renderer.h"
#include "ui/ozone/public/ozone_platform.h"
-#include "ui/ozone/public/surface_factory_ozone.h"
-#include "ui/ozone/public/surface_ozone_canvas.h"
#include "ui/ozone/public/ui_thread_gpu.h"
#include "ui/platform_window/platform_window.h"
#include "ui/platform_window/platform_window_delegate.h"
@@ -30,11 +26,20 @@ const int kAnimationSteps = 240;
const char kDisableGpu[] = "disable-gpu";
+const char kWindowSize[] = "window-size";
+
class DemoWindow : public ui::PlatformWindowDelegate {
public:
- DemoWindow() : widget_(gfx::kNullAcceleratedWidget), iteration_(0) {
+ DemoWindow() : widget_(gfx::kNullAcceleratedWidget) {
+ int width = kTestWindowWidth;
+ int height = kTestWindowHeight;
+ sscanf(CommandLine::ForCurrentProcess()
+ ->GetSwitchValueASCII(kWindowSize)
+ .c_str(),
+ "%dx%d", &width, &height);
+
platform_window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
- this, gfx::Rect(kTestWindowWidth, kTestWindowHeight));
+ this, gfx::Rect(width, height));
}
~DemoWindow() override {}
@@ -49,11 +54,17 @@ class DemoWindow : public ui::PlatformWindowDelegate {
void Start() {
if (!CommandLine::ForCurrentProcess()->HasSwitch(kDisableGpu) &&
- gfx::GLSurface::InitializeOneOff() && StartInProcessGpu() &&
- InitializeGLSurface()) {
- StartAnimationGL();
- } else if (InitializeSoftwareSurface()) {
- StartAnimationSoftware();
+ gfx::GLSurface::InitializeOneOff() && StartInProcessGpu()) {
+ renderer_.reset(new ui::GlRenderer(GetAcceleratedWidget(), GetSize()));
+ } else {
+ renderer_.reset(
+ new ui::SoftwareRenderer(GetAcceleratedWidget(), GetSize()));
+ }
+
+ if (renderer_->Initialize()) {
+ timer_.Start(FROM_HERE,
+ base::TimeDelta::FromMicroseconds(kFrameDelayMilliseconds),
+ renderer_.get(), &ui::Renderer::RenderFrame);
} else {
LOG(ERROR) << "Failed to create drawing surface";
Quit();
@@ -81,108 +92,15 @@ class DemoWindow : public ui::PlatformWindowDelegate {
void OnActivationChanged(bool active) override {}
private:
- bool InitializeGLSurface() {
- surface_ = gfx::GLSurface::CreateViewGLSurface(GetAcceleratedWidget());
- 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";
- surface_ = NULL;
- return false;
- }
-
- surface_->Resize(GetSize());
-
- if (!context_->MakeCurrent(surface_.get())) {
- LOG(ERROR) << "Failed to make GL context current";
- surface_ = NULL;
- context_ = NULL;
- return false;
- }
-
- return true;
- }
-
- bool InitializeSoftwareSurface() {
- software_surface_ =
- ui::SurfaceFactoryOzone::GetInstance()->CreateCanvasForWidget(
- GetAcceleratedWidget());
- if (!software_surface_) {
- LOG(ERROR) << "Failed to create software surface";
- return false;
- }
-
- software_surface_->ResizeCanvas(GetSize());
- return true;
- }
-
- void StartAnimationGL() {
- timer_.Start(FROM_HERE,
- base::TimeDelta::FromMicroseconds(kFrameDelayMilliseconds),
- this,
- &DemoWindow::RenderFrameGL);
- }
-
- void StartAnimationSoftware() {
- timer_.Start(FROM_HERE,
- base::TimeDelta::FromMicroseconds(kFrameDelayMilliseconds),
- this,
- &DemoWindow::RenderFrameSoftware);
- }
void StopAnimation() { timer_.Stop(); }
- float NextFraction() {
- float fraction = (sinf(iteration_ * 2 * M_PI / kAnimationSteps) + 1) / 2;
-
- iteration_++;
- iteration_ %= kAnimationSteps;
-
- return fraction;
- }
-
- void RenderFrameGL() {
- float fraction = NextFraction();
- gfx::Size window_size = GetSize();
-
- glViewport(0, 0, window_size.width(), window_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";
- }
-
- void RenderFrameSoftware() {
- float fraction = NextFraction();
- gfx::Size window_size = GetSize();
-
- skia::RefPtr<SkSurface> surface = software_surface_->GetSurface();
-
- SkColor color =
- SkColorSetARGB(0xff, 0, 0xff * fraction, 0xff * (1 - fraction));
-
- surface->getCanvas()->clear(color);
-
- software_surface_->PresentCanvas(gfx::Rect(window_size));
- }
-
bool StartInProcessGpu() { return ui_thread_gpu_.Initialize(); }
- // Timer for animation.
- base::RepeatingTimer<DemoWindow> timer_;
-
- // Bits for GL rendering.
- scoped_refptr<gfx::GLSurface> surface_;
- scoped_refptr<gfx::GLContext> context_;
+ scoped_ptr<ui::Renderer> renderer_;
- // Bits for software rendeirng.
- scoped_ptr<ui::SurfaceOzoneCanvas> software_surface_;
+ // Timer for animation.
+ base::RepeatingTimer<ui::Renderer> timer_;
// Window-related state.
scoped_ptr<ui::PlatformWindow> platform_window_;
@@ -191,9 +109,6 @@ class DemoWindow : public ui::PlatformWindowDelegate {
// Helper for applications that do GL on main thread.
ui::UiThreadGpu ui_thread_gpu_;
- // Animation state.
- int iteration_;
-
DISALLOW_COPY_AND_ASSIGN(DemoWindow);
};
diff --git a/ui/ozone/demo/ozone_demos.gyp b/ui/ozone/demo/ozone_demos.gyp
index edd3e32..eb8b8d2 100644
--- a/ui/ozone/demo/ozone_demos.gyp
+++ b/ui/ozone/demo/ozone_demos.gyp
@@ -19,7 +19,14 @@
'../../../ui/ozone/ozone.gyp:ozone_base',
],
'sources': [
+ 'gl_renderer.cc',
+ 'gl_renderer.h',
'ozone_demo.cc',
+ 'renderer.h',
+ 'renderer_base.cc',
+ 'renderer_base.h',
+ 'software_renderer.cc',
+ 'software_renderer.h',
],
},
],
diff --git a/ui/ozone/demo/renderer.h b/ui/ozone/demo/renderer.h
new file mode 100644
index 0000000..2e411e1
--- /dev/null
+++ b/ui/ozone/demo/renderer.h
@@ -0,0 +1,21 @@
+// 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.
+
+#ifndef UI_OZONE_DEMO_RENDERER_H_
+#define UI_OZONE_DEMO_RENDERER_H_
+
+namespace ui {
+
+class Renderer {
+ public:
+ virtual ~Renderer() {}
+
+ virtual bool Initialize() = 0;
+
+ virtual void RenderFrame() = 0;
+};
+
+} // namespace ui
+
+#endif // UI_OZONE_DEMO_RENDERER_H_
diff --git a/ui/ozone/demo/renderer_base.cc b/ui/ozone/demo/renderer_base.cc
new file mode 100644
index 0000000..95519de
--- /dev/null
+++ b/ui/ozone/demo/renderer_base.cc
@@ -0,0 +1,29 @@
+// 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/renderer_base.h"
+
+namespace ui {
+
+namespace {
+const int kAnimationSteps = 240;
+} // namespace
+
+RendererBase::RendererBase(gfx::AcceleratedWidget widget, const gfx::Size& size)
+ : widget_(widget), size_(size), iteration_(0) {
+}
+
+RendererBase::~RendererBase() {
+}
+
+float RendererBase::NextFraction() {
+ float fraction = (sinf(iteration_ * 2 * M_PI / kAnimationSteps) + 1) / 2;
+
+ iteration_++;
+ iteration_ %= kAnimationSteps;
+
+ return fraction;
+}
+
+} // namespace ui
diff --git a/ui/ozone/demo/renderer_base.h b/ui/ozone/demo/renderer_base.h
new file mode 100644
index 0000000..38700cf
--- /dev/null
+++ b/ui/ozone/demo/renderer_base.h
@@ -0,0 +1,30 @@
+// 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.
+
+#ifndef UI_OZONE_DEMO_RENDERER_BASE_H_
+#define UI_OZONE_DEMO_RENDERER_BASE_H_
+
+#include "ui/gfx/geometry/size.h"
+#include "ui/gfx/native_widget_types.h"
+#include "ui/ozone/demo/renderer.h"
+
+namespace ui {
+
+class RendererBase : public Renderer {
+ public:
+ RendererBase(gfx::AcceleratedWidget widget, const gfx::Size& size);
+ ~RendererBase() override;
+
+ protected:
+ float NextFraction();
+
+ gfx::AcceleratedWidget widget_;
+ gfx::Size size_;
+
+ int iteration_;
+};
+
+} // namespace ui
+
+#endif // UI_OZONE_DEMO_RENDERER_BASE_H_
diff --git a/ui/ozone/demo/software_renderer.cc b/ui/ozone/demo/software_renderer.cc
new file mode 100644
index 0000000..3cf621d
--- /dev/null
+++ b/ui/ozone/demo/software_renderer.cc
@@ -0,0 +1,47 @@
+// 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/software_renderer.h"
+
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkSurface.h"
+#include "ui/ozone/public/surface_factory_ozone.h"
+#include "ui/ozone/public/surface_ozone_canvas.h"
+
+namespace ui {
+
+SoftwareRenderer::SoftwareRenderer(gfx::AcceleratedWidget widget,
+ const gfx::Size& size)
+ : RendererBase(widget, size) {
+}
+
+SoftwareRenderer::~SoftwareRenderer() {
+}
+
+bool SoftwareRenderer::Initialize() {
+ software_surface_ =
+ ui::SurfaceFactoryOzone::GetInstance()->CreateCanvasForWidget(widget_);
+ if (!software_surface_) {
+ LOG(ERROR) << "Failed to create software surface";
+ return false;
+ }
+
+ software_surface_->ResizeCanvas(size_);
+ return true;
+}
+
+void SoftwareRenderer::RenderFrame() {
+ float fraction = NextFraction();
+
+ skia::RefPtr<SkSurface> surface = software_surface_->GetSurface();
+
+ SkColor color =
+ SkColorSetARGB(0xff, 0, 0xff * fraction, 0xff * (1 - fraction));
+
+ surface->getCanvas()->clear(color);
+
+ software_surface_->PresentCanvas(gfx::Rect(size_));
+}
+
+} // namespace ui
diff --git a/ui/ozone/demo/software_renderer.h b/ui/ozone/demo/software_renderer.h
new file mode 100644
index 0000000..710bc11
--- /dev/null
+++ b/ui/ozone/demo/software_renderer.h
@@ -0,0 +1,33 @@
+// 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.
+
+#ifndef UI_OZONE_DEMO_SOFTWARE_RENDERER_H_
+#define UI_OZONE_DEMO_SOFTWARE_RENDERER_H_
+
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "ui/ozone/demo/renderer_base.h"
+
+namespace ui {
+
+class SurfaceOzoneCanvas;
+
+class SoftwareRenderer : public RendererBase {
+ public:
+ SoftwareRenderer(gfx::AcceleratedWidget widget, const gfx::Size& size);
+ ~SoftwareRenderer() override;
+
+ // Renderer:
+ bool Initialize() override;
+ void RenderFrame() override;
+
+ private:
+ scoped_ptr<SurfaceOzoneCanvas> software_surface_;
+
+ DISALLOW_COPY_AND_ASSIGN(SoftwareRenderer);
+};
+
+} // namespace ui
+
+#endif // UI_OZONE_DEMO_SOFTWARE_RENDERER_H_