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.
5 #include "base/at_exit.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/compositor/test/context_factories_for_test.h"
30 #include "ui/gfx/canvas.h"
31 #include "ui/gfx/rect.h"
32 #include "ui/gfx/skia_util.h"
33 #ifndef GL_GLEXT_PROTOTYPES
34 #define GL_GLEXT_PROTOTYPES 1
36 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
37 #include "third_party/khronos/GLES2/gl2ext.h"
40 #include "base/message_loop/message_pump_x11.h"
43 using base::TimeTicks;
46 using ui::LayerDelegate;
47 using WebKit::WebGraphicsContext3D;
51 class ColoredLayer : public Layer, public LayerDelegate {
53 explicit ColoredLayer(SkColor color)
54 : Layer(ui::LAYER_TEXTURED),
60 virtual ~ColoredLayer() {}
62 // Overridden from LayerDelegate:
63 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
65 canvas->DrawColor(color_);
69 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
72 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
73 return base::Closure();
76 void set_color(SkColor color) { color_ = color; }
77 void set_draw(bool draw) { draw_ = draw; }
83 DISALLOW_COPY_AND_ASSIGN(ColoredLayer);
86 const int kFrames = 100;
88 // Benchmark base class, hooks up drawing callback and displaying FPS.
89 class BenchCompositorObserver : public ui::CompositorObserver {
91 explicit BenchCompositorObserver(int max_frames)
94 max_frames_(max_frames) {
97 virtual void OnCompositingDidCommit(ui::Compositor* compositor) OVERRIDE {}
99 virtual void OnCompositingStarted(Compositor* compositor,
100 base::TimeTicks start_time) OVERRIDE {}
102 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE {
103 if (start_time_.is_null()) {
104 start_time_ = TimeTicks::Now();
107 if (frames_ % kFrames == 0) {
108 TimeTicks now = TimeTicks::Now();
109 double ms = (now - start_time_).InMillisecondsF() / kFrames;
110 LOG(INFO) << "FPS: " << 1000.f / ms << " (" << ms << " ms)";
114 if (max_frames_ && frames_ == max_frames_) {
115 base::MessageLoop::current()->Quit();
121 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {}
123 virtual void OnCompositingLockStateChanged(
124 Compositor* compositor) OVERRIDE {}
126 virtual void OnUpdateVSyncParameters(ui::Compositor* compositor,
127 base::TimeTicks timebase,
128 base::TimeDelta interval) OVERRIDE {
131 virtual void Draw() {}
133 int frames() const { return frames_; }
136 TimeTicks start_time_;
140 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver);
143 class WebGLTexture : public ui::Texture {
145 WebGLTexture(WebGraphicsContext3D* context, const gfx::Size& size)
146 : ui::Texture(false, size, 1.0f),
148 texture_id_(context_->createTexture()) {
149 context_->bindTexture(GL_TEXTURE_2D, texture_id_);
150 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
151 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
152 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
153 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
154 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
155 size.width(), size.height(), 0,
156 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
159 virtual unsigned int PrepareTexture() OVERRIDE {
163 virtual WebGraphicsContext3D* HostContext3D() OVERRIDE {
168 virtual ~WebGLTexture() {
169 context_->deleteTexture(texture_id_);
172 WebGraphicsContext3D* context_;
173 unsigned texture_id_;
175 DISALLOW_COPY_AND_ASSIGN(WebGLTexture);
178 // A benchmark that adds a texture layer that is updated every frame.
179 class WebGLBench : public BenchCompositorObserver {
181 WebGLBench(Layer* parent, Compositor* compositor, int max_frames)
182 : BenchCompositorObserver(max_frames),
184 webgl_(ui::LAYER_TEXTURED),
185 compositor_(compositor),
189 CommandLine* command_line = CommandLine::ForCurrentProcess();
190 do_draw_ = !command_line->HasSwitch("disable-draw");
192 std::string webgl_size = command_line->GetSwitchValueASCII("webgl-size");
195 if (!webgl_size.empty()) {
196 std::vector<std::string> split_size;
197 base::SplitString(webgl_size, 'x', &split_size);
198 if (split_size.size() == 2) {
199 width = atoi(split_size[0].c_str());
200 height = atoi(split_size[1].c_str());
203 if (!width || !height) {
207 gfx::Rect bounds(width, height);
208 webgl_.SetBounds(bounds);
209 parent_->Add(&webgl_);
212 ui::ContextFactory::GetInstance()->SharedMainThreadContextProvider();
213 WebKit::WebGraphicsContext3D* context = context_provider_->Context3d();
214 context->makeContextCurrent();
215 texture_ = new WebGLTexture(context, bounds.size());
216 fbo_ = context->createFramebuffer();
217 compositor->AddObserver(this);
218 webgl_.SetExternalTexture(texture_.get());
219 context->bindFramebuffer(GL_FRAMEBUFFER, fbo_);
220 context->framebufferTexture2D(
221 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
222 GL_TEXTURE_2D, texture_->PrepareTexture(), 0);
223 context->clearColor(0.f, 1.f, 0.f, 1.f);
224 context->clear(GL_COLOR_BUFFER_BIT);
228 virtual ~WebGLBench() {
229 context_provider_->Context3d()->makeContextCurrent();
230 context_provider_->Context3d()->deleteFramebuffer(fbo_);
231 webgl_.SetShowPaintedContent();
233 compositor_->RemoveObserver(this);
236 virtual void Draw() OVERRIDE {
238 WebKit::WebGraphicsContext3D* context = context_provider_->Context3d();
239 context->makeContextCurrent();
240 context->clearColor((frames() % kFrames)*1.0/kFrames, 1.f, 0.f, 1.f);
241 context->clear(GL_COLOR_BUFFER_BIT);
244 webgl_.SetExternalTexture(texture_.get());
245 webgl_.SchedulePaint(gfx::Rect(webgl_.bounds().size()));
246 compositor_->ScheduleDraw();
252 Compositor* compositor_;
253 scoped_refptr<cc::ContextProvider> context_provider_;
254 scoped_refptr<WebGLTexture> texture_;
256 // The FBO that is used to render to the texture.
259 // Whether or not to draw to the texture every frame.
262 DISALLOW_COPY_AND_ASSIGN(WebGLBench);
265 // A benchmark that paints (in software) all tiles every frame.
266 class SoftwareScrollBench : public BenchCompositorObserver {
268 SoftwareScrollBench(ColoredLayer* layer,
269 Compositor* compositor,
271 : BenchCompositorObserver(max_frames),
273 compositor_(compositor) {
274 compositor->AddObserver(this);
276 !CommandLine::ForCurrentProcess()->HasSwitch("disable-draw"));
279 virtual ~SoftwareScrollBench() {
280 compositor_->RemoveObserver(this);
283 virtual void Draw() OVERRIDE {
285 SkColorSetARGBInline(255*(frames() % kFrames)/kFrames, 255, 0, 255));
286 layer_->SchedulePaint(gfx::Rect(layer_->bounds().size()));
290 ColoredLayer* layer_;
291 Compositor* compositor_;
293 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench);
298 int main(int argc, char** argv) {
299 CommandLine::Init(argc, argv);
301 base::AtExitManager exit_manager;
303 // The ContextFactory must exist before any Compositors are created.
304 bool allow_test_contexts = false;
305 ui::InitializeContextFactoryForTests(allow_test_contexts);
307 ui::RegisterPathProvider();
308 base::i18n::InitializeICU();
309 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
311 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
312 aura::Env::CreateInstance();
313 scoped_ptr<aura::TestScreen> test_screen(
314 aura::TestScreen::CreateFullscreen());
315 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
316 scoped_ptr<aura::RootWindow> root_window(
317 test_screen->CreateRootWindowForPrimaryDisplay());
318 aura::client::SetCaptureClient(
320 new aura::client::DefaultCaptureClient(root_window.get()));
322 scoped_ptr<aura::client::FocusClient> focus_client(
323 new aura::test::TestFocusClient);
324 aura::client::SetFocusClient(root_window.get(), focus_client.get());
327 ColoredLayer background(SK_ColorRED);
328 background.SetBounds(root_window->bounds());
329 root_window->layer()->Add(&background);
331 ColoredLayer window(SK_ColorBLUE);
332 window.SetBounds(gfx::Rect(background.bounds().size()));
333 background.Add(&window);
335 Layer content_layer(ui::LAYER_NOT_DRAWN);
337 CommandLine* command_line = CommandLine::ForCurrentProcess();
338 bool force = command_line->HasSwitch("force-render-surface");
339 content_layer.SetForceRenderSurface(force);
340 gfx::Rect bounds(window.bounds().size());
341 bounds.Inset(0, 30, 0, 0);
342 content_layer.SetBounds(bounds);
343 window.Add(&content_layer);
345 ColoredLayer page_background(SK_ColorWHITE);
346 page_background.SetBounds(gfx::Rect(content_layer.bounds().size()));
347 content_layer.Add(&page_background);
349 int frames = atoi(command_line->GetSwitchValueASCII("frames").c_str());
350 scoped_ptr<BenchCompositorObserver> bench;
352 if (command_line->HasSwitch("bench-software-scroll")) {
353 bench.reset(new SoftwareScrollBench(&page_background,
354 root_window->compositor(),
357 bench.reset(new WebGLBench(&page_background,
358 root_window->compositor(),
363 ui::PrintLayerHierarchy(root_window->layer(), gfx::Point(100, 100));
366 root_window->ShowRootWindow();
367 base::MessageLoopForUI::current()->Run();
368 focus_client.reset();