bench_main.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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 "third_party/khronos/GLES2/gl2.h" 15#include "third_party/skia/include/core/SkXfermode.h" 16#include "ui/aura/client/default_capture_client.h" 17#include "ui/aura/env.h" 18#include "ui/aura/root_window.h" 19#include "ui/aura/test/test_focus_client.h" 20#include "ui/aura/test/test_screen.h" 21#include "ui/aura/window.h" 22#include "ui/base/hit_test.h" 23#include "ui/base/resource/resource_bundle.h" 24#include "ui/base/ui_base_paths.h" 25#include "ui/compositor/compositor.h" 26#include "ui/compositor/compositor_observer.h" 27#include "ui/compositor/debug_utils.h" 28#include "ui/compositor/layer.h" 29#include "ui/gfx/canvas.h" 30#include "ui/gfx/rect.h" 31#include "ui/gfx/skia_util.h" 32#ifndef GL_GLEXT_PROTOTYPES 33#define GL_GLEXT_PROTOTYPES 1 34#endif 35#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" 36#include "third_party/khronos/GLES2/gl2ext.h" 37 38#if defined(USE_X11) 39#include "base/message_loop/message_pump_x11.h" 40#endif 41 42using base::TimeTicks; 43using ui::Compositor; 44using ui::Layer; 45using ui::LayerDelegate; 46using WebKit::WebGraphicsContext3D; 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 OnUpdateVSyncParameters(ui::Compositor* compositor, 126 base::TimeTicks timebase, 127 base::TimeDelta interval) OVERRIDE { 128 } 129 130 virtual void Draw() {} 131 132 int frames() const { return frames_; } 133 134 private: 135 TimeTicks start_time_; 136 int frames_; 137 int max_frames_; 138 139 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver); 140}; 141 142class WebGLTexture : public ui::Texture { 143 public: 144 WebGLTexture(WebGraphicsContext3D* context, const gfx::Size& size) 145 : ui::Texture(false, size, 1.0f), 146 context_(context), 147 texture_id_(context_->createTexture()) { 148 context_->bindTexture(GL_TEXTURE_2D, texture_id_); 149 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 150 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 151 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 152 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 153 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 154 size.width(), size.height(), 0, 155 GL_RGBA, GL_UNSIGNED_BYTE, NULL); 156 } 157 158 virtual unsigned int PrepareTexture() OVERRIDE { 159 return texture_id_; 160 } 161 162 virtual WebGraphicsContext3D* HostContext3D() OVERRIDE { 163 return context_; 164 } 165 166 private: 167 virtual ~WebGLTexture() { 168 context_->deleteTexture(texture_id_); 169 } 170 171 WebGraphicsContext3D* context_; 172 unsigned texture_id_; 173 174 DISALLOW_COPY_AND_ASSIGN(WebGLTexture); 175}; 176 177// A benchmark that adds a texture layer that is updated every frame. 178class WebGLBench : public BenchCompositorObserver { 179 public: 180 WebGLBench(Layer* parent, Compositor* compositor, int max_frames) 181 : BenchCompositorObserver(max_frames), 182 parent_(parent), 183 webgl_(ui::LAYER_TEXTURED), 184 compositor_(compositor), 185 texture_(), 186 fbo_(0), 187 do_draw_(true) { 188 CommandLine* command_line = CommandLine::ForCurrentProcess(); 189 do_draw_ = !command_line->HasSwitch("disable-draw"); 190 191 std::string webgl_size = command_line->GetSwitchValueASCII("webgl-size"); 192 int width = 0; 193 int height = 0; 194 if (!webgl_size.empty()) { 195 std::vector<std::string> split_size; 196 base::SplitString(webgl_size, 'x', &split_size); 197 if (split_size.size() == 2) { 198 width = atoi(split_size[0].c_str()); 199 height = atoi(split_size[1].c_str()); 200 } 201 } 202 if (!width || !height) { 203 width = 800; 204 height = 600; 205 } 206 gfx::Rect bounds(width, height); 207 webgl_.SetBounds(bounds); 208 parent_->Add(&webgl_); 209 210 context_provider_ = 211 ui::ContextFactory::GetInstance()->SharedMainThreadContextProvider(); 212 WebKit::WebGraphicsContext3D* context = context_provider_->Context3d(); 213 context->makeContextCurrent(); 214 texture_ = new WebGLTexture(context, bounds.size()); 215 fbo_ = context->createFramebuffer(); 216 compositor->AddObserver(this); 217 webgl_.SetExternalTexture(texture_.get()); 218 context->bindFramebuffer(GL_FRAMEBUFFER, fbo_); 219 context->framebufferTexture2D( 220 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 221 GL_TEXTURE_2D, texture_->PrepareTexture(), 0); 222 context->clearColor(0.f, 1.f, 0.f, 1.f); 223 context->clear(GL_COLOR_BUFFER_BIT); 224 context->flush(); 225 } 226 227 virtual ~WebGLBench() { 228 context_provider_->Context3d()->makeContextCurrent(); 229 context_provider_->Context3d()->deleteFramebuffer(fbo_); 230 webgl_.SetShowPaintedContent(); 231 texture_ = NULL; 232 compositor_->RemoveObserver(this); 233 } 234 235 virtual void Draw() OVERRIDE { 236 if (do_draw_) { 237 WebKit::WebGraphicsContext3D* context = context_provider_->Context3d(); 238 context->makeContextCurrent(); 239 context->clearColor((frames() % kFrames)*1.0/kFrames, 1.f, 0.f, 1.f); 240 context->clear(GL_COLOR_BUFFER_BIT); 241 context->flush(); 242 } 243 webgl_.SetExternalTexture(texture_.get()); 244 webgl_.SchedulePaint(gfx::Rect(webgl_.bounds().size())); 245 compositor_->ScheduleDraw(); 246 } 247 248 private: 249 Layer* parent_; 250 Layer webgl_; 251 Compositor* compositor_; 252 scoped_refptr<cc::ContextProvider> context_provider_; 253 scoped_refptr<WebGLTexture> texture_; 254 255 // The FBO that is used to render to the texture. 256 unsigned int fbo_; 257 258 // Whether or not to draw to the texture every frame. 259 bool do_draw_; 260 261 DISALLOW_COPY_AND_ASSIGN(WebGLBench); 262}; 263 264// A benchmark that paints (in software) all tiles every frame. 265class SoftwareScrollBench : public BenchCompositorObserver { 266 public: 267 SoftwareScrollBench(ColoredLayer* layer, 268 Compositor* compositor, 269 int max_frames) 270 : BenchCompositorObserver(max_frames), 271 layer_(layer), 272 compositor_(compositor) { 273 compositor->AddObserver(this); 274 layer_->set_draw( 275 !CommandLine::ForCurrentProcess()->HasSwitch("disable-draw")); 276 } 277 278 virtual ~SoftwareScrollBench() { 279 compositor_->RemoveObserver(this); 280 } 281 282 virtual void Draw() OVERRIDE { 283 layer_->set_color( 284 SkColorSetARGBInline(255*(frames() % kFrames)/kFrames, 255, 0, 255)); 285 layer_->SchedulePaint(gfx::Rect(layer_->bounds().size())); 286 } 287 288 private: 289 ColoredLayer* layer_; 290 Compositor* compositor_; 291 292 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench); 293}; 294 295} // namespace 296 297int main(int argc, char** argv) { 298 CommandLine::Init(argc, argv); 299 300 base::AtExitManager exit_manager; 301 302 // The ContextFactory must exist before any Compositors are created. 303 bool allow_test_contexts = false; 304 ui::Compositor::InitializeContextFactoryForTests(allow_test_contexts); 305 306 ui::RegisterPathProvider(); 307 base::i18n::InitializeICU(); 308 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); 309 310 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); 311 aura::Env::CreateInstance(); 312 scoped_ptr<aura::TestScreen> test_screen( 313 aura::TestScreen::CreateFullscreen()); 314 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get()); 315 scoped_ptr<aura::RootWindow> root_window( 316 test_screen->CreateRootWindowForPrimaryDisplay()); 317 aura::client::SetCaptureClient( 318 root_window.get(), 319 new aura::client::DefaultCaptureClient(root_window.get())); 320 321 scoped_ptr<aura::client::FocusClient> focus_client( 322 new aura::test::TestFocusClient); 323 aura::client::SetFocusClient(root_window.get(), focus_client.get()); 324 325 // add layers 326 ColoredLayer background(SK_ColorRED); 327 background.SetBounds(root_window->bounds()); 328 root_window->layer()->Add(&background); 329 330 ColoredLayer window(SK_ColorBLUE); 331 window.SetBounds(gfx::Rect(background.bounds().size())); 332 background.Add(&window); 333 334 Layer content_layer(ui::LAYER_NOT_DRAWN); 335 336 CommandLine* command_line = CommandLine::ForCurrentProcess(); 337 bool force = command_line->HasSwitch("force-render-surface"); 338 content_layer.SetForceRenderSurface(force); 339 gfx::Rect bounds(window.bounds().size()); 340 bounds.Inset(0, 30, 0, 0); 341 content_layer.SetBounds(bounds); 342 window.Add(&content_layer); 343 344 ColoredLayer page_background(SK_ColorWHITE); 345 page_background.SetBounds(gfx::Rect(content_layer.bounds().size())); 346 content_layer.Add(&page_background); 347 348 int frames = atoi(command_line->GetSwitchValueASCII("frames").c_str()); 349 scoped_ptr<BenchCompositorObserver> bench; 350 351 if (command_line->HasSwitch("bench-software-scroll")) { 352 bench.reset(new SoftwareScrollBench(&page_background, 353 root_window->compositor(), 354 frames)); 355 } else { 356 bench.reset(new WebGLBench(&page_background, 357 root_window->compositor(), 358 frames)); 359 } 360 361#ifndef NDEBUG 362 ui::PrintLayerHierarchy(root_window->layer(), gfx::Point(100, 100)); 363#endif 364 365 root_window->ShowRootWindow(); 366 base::MessageLoopForUI::current()->Run(); 367 focus_client.reset(); 368 root_window.reset(); 369 370 return 0; 371} 372