Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1242)

Side by Side Diff: ui/aura/bench/bench_main.cc

Issue 10444019: Aura: add a bench utility (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix review comments Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/aura/aura.gyp ('k') | ui/aura/root_window.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/skia/include/core/SkXfermode.h"
14 #include "ui/aura/env.h"
15 #include "ui/aura/event.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/aura/single_monitor_manager.h"
18 #include "ui/aura/window.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/base/ui_base_paths.h"
22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/compositor_observer.h"
24 #include "ui/compositor/debug_utils.h"
25 #include "ui/compositor/layer.h"
26 #include "ui/compositor/test/compositor_test_support.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/rect.h"
29 #include "ui/gfx/skia_util.h"
30 #include "third_party/khronos/GLES2/gl2.h"
31 #ifndef GL_GLEXT_PROTOTYPES
32 #define GL_GLEXT_PROTOTYPES 1
33 #endif
34 #include "third_party/khronos/GLES2/gl2ext.h"
35 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsC ontext3D.h"
36
37 #if defined(USE_X11)
38 #include "base/message_pump_x.h"
39 #endif
40
41 using base::TimeTicks;
42 using ui::Compositor;
43 using ui::Layer;
44 using ui::LayerDelegate;
45 using WebKit::WebGraphicsContext3D;
46
47 namespace {
48
49 class ColoredLayer : public Layer, public LayerDelegate {
50 public:
51 explicit ColoredLayer(SkColor color)
52 : Layer(ui::LAYER_TEXTURED),
53 color_(color),
54 draw_(true) {
55 set_delegate(this);
56 }
57
58 virtual ~ColoredLayer() {}
59
60 // Overridden from LayerDelegate:
61 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
62 if (draw_) {
63 canvas->DrawColor(color_);
64 }
65 }
66
67 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
68 }
69
70 void set_color(SkColor color) { color_ = color; }
71 void set_draw(bool draw) { draw_ = draw; }
72
73 private:
74 SkColor color_;
75 bool draw_;
76
77 DISALLOW_COPY_AND_ASSIGN(ColoredLayer);
78 };
79
80 const int kFrames = 100;
81
82 // Benchmark base class, hooks up drawing callback and displaying FPS.
83 class BenchCompositorObserver : public ui::CompositorObserver {
84 public:
85 explicit BenchCompositorObserver(int max_frames)
86 : start_time_(),
87 frames_(0),
88 max_frames_(max_frames) {
89 }
90 virtual void OnCompositingStarted(Compositor* compositor) OVERRIDE {}
91
92 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE {
93 if (start_time_.is_null()) {
94 start_time_ = TimeTicks::Now();
95 } else {
96 ++frames_;
97 if (frames_ % kFrames == 0) {
98 TimeTicks now = TimeTicks::Now();
99 double ms = (now - start_time_).InMillisecondsF() / kFrames;
100 LOG(INFO) << "FPS: " << 1000.f / ms << " (" << ms << " ms)";
101 start_time_ = now;
102 }
103 }
104 if (max_frames_ && frames_ == max_frames_) {
105 MessageLoop::current()->Quit();
106 } else {
107 Draw();
108 }
109 }
110
111 virtual void Draw() {}
112
113 int frames() const { return frames_; }
114
115 private:
116 TimeTicks start_time_;
117 int frames_;
118 int max_frames_;
119
120 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver);
121 };
122
123 class WebGLTexture : public ui::Texture {
124 public:
125 WebGLTexture(WebGraphicsContext3D* context, const gfx::Size& size)
126 : ui::Texture(false, size),
127 context_(context) {
128 set_texture_id(context_->createTexture());
129 context_->bindTexture(GL_TEXTURE_2D, texture_id());
130 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
131 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
132 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
133 context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
134 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
135 size.width(), size.height(), 0,
136 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
137 }
138
139 virtual ~WebGLTexture() {
140 context_->deleteTexture(texture_id());
141 }
142
143 private:
144 WebGraphicsContext3D* context_;
145
146 DISALLOW_COPY_AND_ASSIGN(WebGLTexture);
147 };
148
149 // A benchmark that adds a texture layer that is updated every frame.
150 class WebGLBench : public BenchCompositorObserver {
151 public:
152 WebGLBench(Layer* parent, Compositor* compositor, int max_frames)
153 : BenchCompositorObserver(max_frames),
154 parent_(parent),
155 webgl_(ui::LAYER_TEXTURED),
156 compositor_(compositor),
157 context_(),
158 texture_(),
159 fbo_(0),
160 do_draw_(true) {
161 CommandLine* command_line = CommandLine::ForCurrentProcess();
162 do_draw_ = !command_line->HasSwitch("disable-draw");
163
164 std::string webgl_size = command_line->GetSwitchValueASCII("webgl-size");
165 int width = 0;
166 int height = 0;
167 if (!webgl_size.empty()) {
168 std::vector<std::string> split_size;
169 base::SplitString(webgl_size, 'x', &split_size);
170 if (split_size.size() == 2) {
171 width = atoi(split_size[0].c_str());
172 height = atoi(split_size[1].c_str());
173 }
174 }
175 if (!width || !height) {
176 width = 800;
177 height = 600;
178 }
179 gfx::Rect bounds(width, height);
180 webgl_.SetBounds(bounds);
181 parent_->Add(&webgl_);
182
183 context_.reset(
184 ui::ContextFactory::GetInstance()->CreateOffscreenContext(compositor));
185 context_->makeContextCurrent();
186 texture_ = new WebGLTexture(context_.get(), bounds.size());
187 fbo_ = context_->createFramebuffer();
188 compositor->AddObserver(this);
189 webgl_.SetExternalTexture(texture_);
190 context_->bindFramebuffer(GL_FRAMEBUFFER, fbo_);
191 context_->framebufferTexture2D(
192 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
193 GL_TEXTURE_2D, texture_->texture_id(), 0);
194 context_->clearColor(0.f, 1.f, 0.f, 1.f);
195 context_->clear(GL_COLOR_BUFFER_BIT);
196 context_->flush();
197 }
198
199 virtual ~WebGLBench() {
200 context_->makeContextCurrent();
201 context_->deleteFramebuffer(fbo_);
202 webgl_.SetExternalTexture(NULL);
203 texture_ = NULL;
204 compositor_->RemoveObserver(this);
205 }
206
207 virtual void Draw() OVERRIDE {
208 if (do_draw_) {
209 context_->makeContextCurrent();
210 context_->clearColor((frames() % kFrames)*1.0/kFrames, 1.f, 0.f, 1.f);
211 context_->clear(GL_COLOR_BUFFER_BIT);
212 context_->flush();
213 }
214 webgl_.SetExternalTexture(texture_);
215 webgl_.SchedulePaint(gfx::Rect(webgl_.bounds().size()));
216 compositor_->ScheduleDraw();
217 }
218
219 private:
220 Layer* parent_;
221 Layer webgl_;
222 Compositor* compositor_;
223 scoped_ptr<WebGraphicsContext3D> context_;
224 scoped_refptr<WebGLTexture> texture_;
225
226 // The FBO that is used to render to the texture.
227 unsigned int fbo_;
228
229 // Whether or not to draw to the texture every frame.
230 bool do_draw_;
231
232 DISALLOW_COPY_AND_ASSIGN(WebGLBench);
233 };
234
235 // A benchmark that paints (in software) all tiles every frame.
236 class SoftwareScrollBench : public BenchCompositorObserver {
237 public:
238 SoftwareScrollBench(ColoredLayer* layer,
239 Compositor* compositor,
240 int max_frames)
241 : BenchCompositorObserver(max_frames),
242 layer_(layer),
243 compositor_(compositor) {
244 compositor->AddObserver(this);
245 layer_->set_draw(
246 !CommandLine::ForCurrentProcess()->HasSwitch("disable-draw"));
247 }
248
249 virtual ~SoftwareScrollBench() {
250 compositor_->RemoveObserver(this);
251 }
252
253 virtual void Draw() OVERRIDE {
254 layer_->set_color(
255 SkColorSetARGBInline(255*(frames() % kFrames)/kFrames, 255, 0, 255));
256 layer_->SchedulePaint(gfx::Rect(layer_->bounds().size()));
257 }
258
259 private:
260 ColoredLayer* layer_;
261 Compositor* compositor_;
262
263 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench);
264 };
265
266 } // anonymous namespace
267
268 int main(int argc, char** argv) {
269 CommandLine::Init(argc, argv);
270
271 base::AtExitManager exit_manager;
272
273 ui::RegisterPathProvider();
274 icu_util::Initialize();
275 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
276
277 MessageLoop message_loop(MessageLoop::TYPE_UI);
278 ui::CompositorTestSupport::Initialize();
279 aura::SingleMonitorManager* manager = new aura::SingleMonitorManager;
280 manager->set_use_fullscreen_host_window(true);
281 aura::Env::GetInstance()->SetMonitorManager(manager);
282 scoped_ptr<aura::RootWindow> root_window(
283 aura::MonitorManager::CreateRootWindowForPrimaryMonitor());
284
285 // add layers
286 ColoredLayer background(SK_ColorRED);
287 background.SetBounds(root_window->bounds());
288 root_window->layer()->Add(&background);
289
290 ColoredLayer window(SK_ColorBLUE);
291 window.SetBounds(gfx::Rect(background.bounds().size()));
292 background.Add(&window);
293
294 Layer content_layer(ui::LAYER_NOT_DRAWN);
295
296 CommandLine* command_line = CommandLine::ForCurrentProcess();
297 bool force = command_line->HasSwitch("force-render-surface");
298 content_layer.SetForceRenderSurface(force);
299 gfx::Rect bounds(window.bounds().size());
300 bounds.Inset(0, 30, 0, 0);
301 content_layer.SetBounds(bounds);
302 window.Add(&content_layer);
303
304 ColoredLayer page_background(SK_ColorWHITE);
305 page_background.SetBounds(gfx::Rect(content_layer.bounds().size()));
306 content_layer.Add(&page_background);
307
308 int frames = atoi(command_line->GetSwitchValueASCII("frames").c_str());
309 scoped_ptr<BenchCompositorObserver> bench;
310
311 if (command_line->HasSwitch("bench-software-scroll")) {
312 bench.reset(new SoftwareScrollBench(&page_background,
313 root_window->compositor(),
314 frames));
315 } else {
316 bench.reset(new WebGLBench(&page_background,
317 root_window->compositor(),
318 frames));
319 }
320
321 #ifndef NDEBUG
322 ui::PrintLayerHierarchy(root_window->layer(), gfx::Point(100, 100));
323 #endif
324
325 root_window->ShowRootWindow();
326 MessageLoopForUI::current()->Run();
327 root_window.reset();
328
329 ui::CompositorTestSupport::Terminate();
330
331 return 0;
332 }
OLDNEW
« no previous file with comments | « ui/aura/aura.gyp ('k') | ui/aura/root_window.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698