bench_main.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 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 "base/at_exit.h"
6#include "base/bind.h"
7#include "base/command_line.h"
8#include "base/i18n/icu_util.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/message_loop/message_loop.h"
11#include "base/strings/string_split.h"
12#include "base/time/time.h"
13#include "cc/output/context_provider.h"
14#include "gpu/command_buffer/client/gles2_interface.h"
15#include "third_party/khronos/GLES2/gl2.h"
16#include "third_party/skia/include/core/SkXfermode.h"
17#include "ui/aura/client/default_capture_client.h"
18#include "ui/aura/env.h"
19#include "ui/aura/root_window.h"
20#include "ui/aura/test/test_focus_client.h"
21#include "ui/aura/test/test_screen.h"
22#include "ui/aura/window.h"
23#include "ui/base/hit_test.h"
24#include "ui/compositor/compositor.h"
25#include "ui/compositor/compositor_observer.h"
26#include "ui/compositor/debug_utils.h"
27#include "ui/compositor/layer.h"
28#include "ui/compositor/test/context_factories_for_test.h"
29#include "ui/gfx/canvas.h"
30#include "ui/gfx/rect.h"
31#include "ui/gfx/skia_util.h"
32#include "ui/gl/gl_surface.h"
33
34#ifndef GL_GLEXT_PROTOTYPES
35#define GL_GLEXT_PROTOTYPES 1
36#endif
37#include "third_party/khronos/GLES2/gl2ext.h"
38
39#if defined(USE_X11)
40#include "base/message_loop/message_pump_x11.h"
41#endif
42
43using base::TimeTicks;
44using ui::Compositor;
45using ui::Layer;
46using ui::LayerDelegate;
47
48namespace {
49
50class ColoredLayer : public Layer, public LayerDelegate {
51 public:
52  explicit ColoredLayer(SkColor color)
53      : Layer(ui::LAYER_TEXTURED),
54        color_(color),
55        draw_(true) {
56    set_delegate(this);
57  }
58
59  virtual ~ColoredLayer() {}
60
61  // Overridden from LayerDelegate:
62  virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
63    if (draw_) {
64      canvas->DrawColor(color_);
65    }
66  }
67
68  virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
69  }
70
71  virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
72    return base::Closure();
73  }
74
75  void set_color(SkColor color) { color_ = color; }
76  void set_draw(bool draw) { draw_ = draw; }
77
78 private:
79  SkColor color_;
80  bool draw_;
81
82  DISALLOW_COPY_AND_ASSIGN(ColoredLayer);
83};
84
85const int kFrames = 100;
86
87// Benchmark base class, hooks up drawing callback and displaying FPS.
88class BenchCompositorObserver : public ui::CompositorObserver {
89 public:
90  explicit BenchCompositorObserver(int max_frames)
91      : start_time_(),
92        frames_(0),
93        max_frames_(max_frames) {
94  }
95
96  virtual void OnCompositingDidCommit(ui::Compositor* compositor) OVERRIDE {}
97
98  virtual void OnCompositingStarted(Compositor* compositor,
99                                    base::TimeTicks start_time) OVERRIDE {}
100
101  virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE {
102    if (start_time_.is_null()) {
103      start_time_ = TimeTicks::Now();
104    } else {
105      ++frames_;
106      if (frames_ % kFrames == 0) {
107        TimeTicks now = TimeTicks::Now();
108        double ms = (now - start_time_).InMillisecondsF() / kFrames;
109        LOG(INFO) << "FPS: " << 1000.f / ms << " (" << ms << " ms)";
110        start_time_ = now;
111      }
112    }
113    if (max_frames_ && frames_ == max_frames_) {
114      base::MessageLoop::current()->Quit();
115    } else {
116      Draw();
117    }
118  }
119
120  virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {}
121
122  virtual void OnCompositingLockStateChanged(
123      Compositor* compositor) OVERRIDE {}
124
125  virtual void Draw() {}
126
127  int frames() const { return frames_; }
128
129 private:
130  TimeTicks start_time_;
131  int frames_;
132  int max_frames_;
133
134  DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver);
135};
136
137class WebGLTexture : public ui::Texture {
138 public:
139  WebGLTexture(gpu::gles2::GLES2Interface* gl, const gfx::Size& size)
140      : ui::Texture(false, size, 1.0f),
141        gl_(gl),
142        texture_id_(0u) {
143    gl->GenTextures(1, &texture_id_);
144    gl->BindTexture(GL_TEXTURE_2D, texture_id_);
145    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
148    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
149    gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width(), size.height(),
150                   0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
151  }
152
153  virtual unsigned int PrepareTexture() OVERRIDE {
154    return texture_id_;
155  }
156
157 private:
158  virtual ~WebGLTexture() {
159    gl_->DeleteTextures(1, &texture_id_);
160  }
161
162  gpu::gles2::GLES2Interface* gl_;
163  GLuint texture_id_;
164
165  DISALLOW_COPY_AND_ASSIGN(WebGLTexture);
166};
167
168// A benchmark that adds a texture layer that is updated every frame.
169class WebGLBench : public BenchCompositorObserver {
170 public:
171  WebGLBench(Layer* parent, Compositor* compositor, int max_frames)
172      : BenchCompositorObserver(max_frames),
173        parent_(parent),
174        webgl_(ui::LAYER_TEXTURED),
175        compositor_(compositor),
176        texture_(),
177        fbo_(0),
178        do_draw_(true) {
179    CommandLine* command_line = CommandLine::ForCurrentProcess();
180    do_draw_ = !command_line->HasSwitch("disable-draw");
181
182    std::string webgl_size = command_line->GetSwitchValueASCII("webgl-size");
183    int width = 0;
184    int height = 0;
185    if (!webgl_size.empty()) {
186      std::vector<std::string> split_size;
187      base::SplitString(webgl_size, 'x', &split_size);
188      if (split_size.size() == 2) {
189        width = atoi(split_size[0].c_str());
190        height = atoi(split_size[1].c_str());
191      }
192    }
193    if (!width || !height) {
194      width = 800;
195      height = 600;
196    }
197    gfx::Rect bounds(width, height);
198    webgl_.SetBounds(bounds);
199    parent_->Add(&webgl_);
200
201    context_provider_ =
202        ui::ContextFactory::GetInstance()->SharedMainThreadContextProvider();
203    gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL();
204    texture_ = new WebGLTexture(gl, bounds.size());
205    gl->GenFramebuffers(1, &fbo_);
206    compositor->AddObserver(this);
207    webgl_.SetExternalTexture(texture_.get());
208    gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
209    gl->FramebufferTexture2D(
210        GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
211        GL_TEXTURE_2D, texture_->PrepareTexture(), 0);
212    gl->ClearColor(0.f, 1.f, 0.f, 1.f);
213    gl->Clear(GL_COLOR_BUFFER_BIT);
214    gl->Flush();
215  }
216
217  virtual ~WebGLBench() {
218    context_provider_->ContextGL()->DeleteFramebuffers(1, &fbo_);
219    webgl_.SetShowPaintedContent();
220    texture_ = NULL;
221    compositor_->RemoveObserver(this);
222  }
223
224  virtual void Draw() OVERRIDE {
225    if (do_draw_) {
226      gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL();
227      gl->ClearColor((frames() % kFrames)*1.0/kFrames, 1.f, 0.f, 1.f);
228      gl->Clear(GL_COLOR_BUFFER_BIT);
229      gl->Flush();
230    }
231    webgl_.SetExternalTexture(texture_.get());
232    webgl_.SchedulePaint(gfx::Rect(webgl_.bounds().size()));
233    compositor_->ScheduleDraw();
234  }
235
236 private:
237  Layer* parent_;
238  Layer webgl_;
239  Compositor* compositor_;
240  scoped_refptr<cc::ContextProvider> context_provider_;
241  scoped_refptr<WebGLTexture> texture_;
242
243  // The FBO that is used to render to the texture.
244  unsigned int fbo_;
245
246  // Whether or not to draw to the texture every frame.
247  bool do_draw_;
248
249  DISALLOW_COPY_AND_ASSIGN(WebGLBench);
250};
251
252// A benchmark that paints (in software) all tiles every frame.
253class SoftwareScrollBench : public BenchCompositorObserver {
254 public:
255  SoftwareScrollBench(ColoredLayer* layer,
256                      Compositor* compositor,
257                      int max_frames)
258      : BenchCompositorObserver(max_frames),
259        layer_(layer),
260        compositor_(compositor) {
261    compositor->AddObserver(this);
262    layer_->set_draw(
263        !CommandLine::ForCurrentProcess()->HasSwitch("disable-draw"));
264  }
265
266  virtual ~SoftwareScrollBench() {
267    compositor_->RemoveObserver(this);
268  }
269
270  virtual void Draw() OVERRIDE {
271    layer_->set_color(
272        SkColorSetARGBInline(255*(frames() % kFrames)/kFrames, 255, 0, 255));
273    layer_->SchedulePaint(gfx::Rect(layer_->bounds().size()));
274  }
275
276 private:
277  ColoredLayer* layer_;
278  Compositor* compositor_;
279
280  DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench);
281};
282
283}  // namespace
284
285int main(int argc, char** argv) {
286  CommandLine::Init(argc, argv);
287
288  base::AtExitManager exit_manager;
289
290  gfx::GLSurface::InitializeOneOff();
291
292  // The ContextFactory must exist before any Compositors are created.
293  bool allow_test_contexts = false;
294  ui::InitializeContextFactoryForTests(allow_test_contexts);
295
296  base::i18n::InitializeICU();
297
298  base::MessageLoopForUI message_loop;
299  aura::Env::CreateInstance();
300  scoped_ptr<aura::TestScreen> test_screen(
301      aura::TestScreen::CreateFullscreen());
302  gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
303  scoped_ptr<aura::RootWindow> root_window(
304      test_screen->CreateRootWindowForPrimaryDisplay());
305  aura::client::SetCaptureClient(
306      root_window->window(),
307      new aura::client::DefaultCaptureClient(root_window->window()));
308
309  scoped_ptr<aura::client::FocusClient> focus_client(
310      new aura::test::TestFocusClient);
311  aura::client::SetFocusClient(root_window->window(), focus_client.get());
312
313  // add layers
314  ColoredLayer background(SK_ColorRED);
315  background.SetBounds(root_window->window()->bounds());
316  root_window->window()->layer()->Add(&background);
317
318  ColoredLayer window(SK_ColorBLUE);
319  window.SetBounds(gfx::Rect(background.bounds().size()));
320  background.Add(&window);
321
322  Layer content_layer(ui::LAYER_NOT_DRAWN);
323
324  CommandLine* command_line = CommandLine::ForCurrentProcess();
325  bool force = command_line->HasSwitch("force-render-surface");
326  content_layer.SetForceRenderSurface(force);
327  gfx::Rect bounds(window.bounds().size());
328  bounds.Inset(0, 30, 0, 0);
329  content_layer.SetBounds(bounds);
330  window.Add(&content_layer);
331
332  ColoredLayer page_background(SK_ColorWHITE);
333  page_background.SetBounds(gfx::Rect(content_layer.bounds().size()));
334  content_layer.Add(&page_background);
335
336  int frames = atoi(command_line->GetSwitchValueASCII("frames").c_str());
337  scoped_ptr<BenchCompositorObserver> bench;
338
339  if (command_line->HasSwitch("bench-software-scroll")) {
340    bench.reset(new SoftwareScrollBench(&page_background,
341                                        root_window->host()->compositor(),
342                                        frames));
343  } else {
344    bench.reset(new WebGLBench(&page_background,
345                               root_window->host()->compositor(),
346                               frames));
347  }
348
349#ifndef NDEBUG
350  ui::PrintLayerHierarchy(root_window->window()->layer(), gfx::Point(100, 100));
351#endif
352
353  root_window->host()->Show();
354  base::MessageLoopForUI::current()->Run();
355  focus_client.reset();
356  root_window.reset();
357
358  return 0;
359}
360