bench_main.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.h" 11#include "base/string_split.h" 12#include "base/time.h" 13#include "third_party/khronos/GLES2/gl2.h" 14#include "third_party/skia/include/core/SkXfermode.h" 15#include "ui/aura/client/default_capture_client.h" 16#include "ui/aura/env.h" 17#include "ui/aura/focus_manager.h" 18#include "ui/aura/root_window.h" 19#include "ui/aura/single_display_manager.h" 20#include "ui/aura/window.h" 21#include "ui/base/hit_test.h" 22#include "ui/base/resource/resource_bundle.h" 23#include "ui/base/ui_base_paths.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/compositor_test_support.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/khronos/GLES2/gl2ext.h" 36#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsContext3D.h" 37 38#if defined(USE_X11) 39#include "base/message_pump_aurax11.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) OVERRIDE {} 99 100 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE { 101 if (start_time_.is_null()) { 102 start_time_ = TimeTicks::Now(); 103 } else { 104 ++frames_; 105 if (frames_ % kFrames == 0) { 106 TimeTicks now = TimeTicks::Now(); 107 double ms = (now - start_time_).InMillisecondsF() / kFrames; 108 LOG(INFO) << "FPS: " << 1000.f / ms << " (" << ms << " ms)"; 109 start_time_ = now; 110 } 111 } 112 if (max_frames_ && frames_ == max_frames_) { 113 MessageLoop::current()->Quit(); 114 } else { 115 Draw(); 116 } 117 } 118 119 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {} 120 121 virtual void OnCompositingLockStateChanged( 122 Compositor* compositor) OVERRIDE {} 123 124 virtual void Draw() {} 125 126 int frames() const { return frames_; } 127 128 private: 129 TimeTicks start_time_; 130 int frames_; 131 int max_frames_; 132 133 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver); 134}; 135 136class WebGLTexture : public ui::Texture { 137 public: 138 WebGLTexture(WebGraphicsContext3D* context, const gfx::Size& size) 139 : ui::Texture(false, size, 1.0f), 140 context_(context), 141 texture_id_(context_->createTexture()) { 142 context_->bindTexture(GL_TEXTURE_2D, texture_id_); 143 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 144 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 145 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 146 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 147 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 148 size.width(), size.height(), 0, 149 GL_RGBA, GL_UNSIGNED_BYTE, NULL); 150 } 151 152 virtual unsigned int PrepareTexture() OVERRIDE { 153 return texture_id_; 154 } 155 156 virtual WebGraphicsContext3D* HostContext3D() OVERRIDE { 157 return context_; 158 } 159 160 private: 161 virtual ~WebGLTexture() { 162 context_->deleteTexture(texture_id_); 163 } 164 165 WebGraphicsContext3D* context_; 166 unsigned texture_id_; 167 168 DISALLOW_COPY_AND_ASSIGN(WebGLTexture); 169}; 170 171// A benchmark that adds a texture layer that is updated every frame. 172class WebGLBench : public BenchCompositorObserver { 173 public: 174 WebGLBench(Layer* parent, Compositor* compositor, int max_frames) 175 : BenchCompositorObserver(max_frames), 176 parent_(parent), 177 webgl_(ui::LAYER_TEXTURED), 178 compositor_(compositor), 179 context_(), 180 texture_(), 181 fbo_(0), 182 do_draw_(true) { 183 CommandLine* command_line = CommandLine::ForCurrentProcess(); 184 do_draw_ = !command_line->HasSwitch("disable-draw"); 185 186 std::string webgl_size = command_line->GetSwitchValueASCII("webgl-size"); 187 int width = 0; 188 int height = 0; 189 if (!webgl_size.empty()) { 190 std::vector<std::string> split_size; 191 base::SplitString(webgl_size, 'x', &split_size); 192 if (split_size.size() == 2) { 193 width = atoi(split_size[0].c_str()); 194 height = atoi(split_size[1].c_str()); 195 } 196 } 197 if (!width || !height) { 198 width = 800; 199 height = 600; 200 } 201 gfx::Rect bounds(width, height); 202 webgl_.SetBounds(bounds); 203 parent_->Add(&webgl_); 204 205 context_.reset(ui::ContextFactory::GetInstance()->CreateOffscreenContext()); 206 context_->makeContextCurrent(); 207 texture_ = new WebGLTexture(context_.get(), bounds.size()); 208 fbo_ = context_->createFramebuffer(); 209 compositor->AddObserver(this); 210 webgl_.SetExternalTexture(texture_); 211 context_->bindFramebuffer(GL_FRAMEBUFFER, fbo_); 212 context_->framebufferTexture2D( 213 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 214 GL_TEXTURE_2D, texture_->PrepareTexture(), 0); 215 context_->clearColor(0.f, 1.f, 0.f, 1.f); 216 context_->clear(GL_COLOR_BUFFER_BIT); 217 context_->flush(); 218 } 219 220 virtual ~WebGLBench() { 221 context_->makeContextCurrent(); 222 context_->deleteFramebuffer(fbo_); 223 webgl_.SetExternalTexture(NULL); 224 texture_ = NULL; 225 compositor_->RemoveObserver(this); 226 } 227 228 virtual void Draw() OVERRIDE { 229 if (do_draw_) { 230 context_->makeContextCurrent(); 231 context_->clearColor((frames() % kFrames)*1.0/kFrames, 1.f, 0.f, 1.f); 232 context_->clear(GL_COLOR_BUFFER_BIT); 233 context_->flush(); 234 } 235 webgl_.SetExternalTexture(texture_); 236 webgl_.SchedulePaint(gfx::Rect(webgl_.bounds().size())); 237 compositor_->ScheduleDraw(); 238 } 239 240 private: 241 Layer* parent_; 242 Layer webgl_; 243 Compositor* compositor_; 244 scoped_ptr<WebGraphicsContext3D> context_; 245 scoped_refptr<WebGLTexture> texture_; 246 247 // The FBO that is used to render to the texture. 248 unsigned int fbo_; 249 250 // Whether or not to draw to the texture every frame. 251 bool do_draw_; 252 253 DISALLOW_COPY_AND_ASSIGN(WebGLBench); 254}; 255 256// A benchmark that paints (in software) all tiles every frame. 257class SoftwareScrollBench : public BenchCompositorObserver { 258 public: 259 SoftwareScrollBench(ColoredLayer* layer, 260 Compositor* compositor, 261 int max_frames) 262 : BenchCompositorObserver(max_frames), 263 layer_(layer), 264 compositor_(compositor) { 265 compositor->AddObserver(this); 266 layer_->set_draw( 267 !CommandLine::ForCurrentProcess()->HasSwitch("disable-draw")); 268 } 269 270 virtual ~SoftwareScrollBench() { 271 compositor_->RemoveObserver(this); 272 } 273 274 virtual void Draw() OVERRIDE { 275 layer_->set_color( 276 SkColorSetARGBInline(255*(frames() % kFrames)/kFrames, 255, 0, 255)); 277 layer_->SchedulePaint(gfx::Rect(layer_->bounds().size())); 278 } 279 280 private: 281 ColoredLayer* layer_; 282 Compositor* compositor_; 283 284 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench); 285}; 286 287} // namespace 288 289int main(int argc, char** argv) { 290 CommandLine::Init(argc, argv); 291 292 base::AtExitManager exit_manager; 293 294 ui::RegisterPathProvider(); 295 icu_util::Initialize(); 296 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); 297 298 MessageLoop message_loop(MessageLoop::TYPE_UI); 299 ui::CompositorTestSupport::Initialize(); 300 aura::SingleDisplayManager* manager = new aura::SingleDisplayManager; 301 manager->set_use_fullscreen_host_window(true); 302 aura::Env::GetInstance()->SetDisplayManager(manager); 303 scoped_ptr<aura::RootWindow> root_window( 304 aura::DisplayManager::CreateRootWindowForPrimaryDisplay()); 305 aura::client::SetCaptureClient( 306 root_window.get(), 307 new aura::client::DefaultCaptureClient(root_window.get())); 308 309 scoped_ptr<aura::FocusManager> focus_manager(new aura::FocusManager); 310 root_window->set_focus_manager(focus_manager.get()); 311 312 // add layers 313 ColoredLayer background(SK_ColorRED); 314 background.SetBounds(root_window->bounds()); 315 root_window->layer()->Add(&background); 316 317 ColoredLayer window(SK_ColorBLUE); 318 window.SetBounds(gfx::Rect(background.bounds().size())); 319 background.Add(&window); 320 321 Layer content_layer(ui::LAYER_NOT_DRAWN); 322 323 CommandLine* command_line = CommandLine::ForCurrentProcess(); 324 bool force = command_line->HasSwitch("force-render-surface"); 325 content_layer.SetForceRenderSurface(force); 326 gfx::Rect bounds(window.bounds().size()); 327 bounds.Inset(0, 30, 0, 0); 328 content_layer.SetBounds(bounds); 329 window.Add(&content_layer); 330 331 ColoredLayer page_background(SK_ColorWHITE); 332 page_background.SetBounds(gfx::Rect(content_layer.bounds().size())); 333 content_layer.Add(&page_background); 334 335 int frames = atoi(command_line->GetSwitchValueASCII("frames").c_str()); 336 scoped_ptr<BenchCompositorObserver> bench; 337 338 if (command_line->HasSwitch("bench-software-scroll")) { 339 bench.reset(new SoftwareScrollBench(&page_background, 340 root_window->compositor(), 341 frames)); 342 } else { 343 bench.reset(new WebGLBench(&page_background, 344 root_window->compositor(), 345 frames)); 346 } 347 348#ifndef NDEBUG 349 ui::PrintLayerHierarchy(root_window->layer(), gfx::Point(100, 100)); 350#endif 351 352 root_window->ShowRootWindow(); 353 MessageLoopForUI::current()->Run(); 354 focus_manager.reset(); 355 root_window.reset(); 356 357 ui::CompositorTestSupport::Terminate(); 358 359 return 0; 360} 361