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.
9 #include "base/at_exit.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/i18n/icu_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/strings/string_split.h"
16 #include "base/time/time.h"
17 #include "cc/output/context_provider.h"
18 #include "gpu/command_buffer/client/gles2_interface.h"
19 #include "third_party/khronos/GLES2/gl2.h"
20 #include "third_party/skia/include/core/SkXfermode.h"
21 #include "ui/aura/client/default_capture_client.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/test/test_focus_client.h"
24 #include "ui/aura/test/test_screen.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_tree_host.h"
27 #include "ui/base/hit_test.h"
28 #include "ui/compositor/compositor.h"
29 #include "ui/compositor/compositor_observer.h"
30 #include "ui/compositor/debug_utils.h"
31 #include "ui/compositor/layer.h"
32 #include "ui/compositor/paint_recorder.h"
33 #include "ui/compositor/test/in_process_context_factory.h"
34 #include "ui/gfx/canvas.h"
35 #include "ui/gfx/geometry/rect.h"
36 #include "ui/gfx/skia_util.h"
37 #include "ui/gfx/x/x11_connection.h"
38 #include "ui/gl/gl_surface.h"
40 #ifndef GL_GLEXT_PROTOTYPES
41 #define GL_GLEXT_PROTOTYPES 1
43 #include "third_party/khronos/GLES2/gl2ext.h"
45 using base::TimeTicks
;
48 using ui::LayerDelegate
;
52 class ColoredLayer
: public Layer
, public LayerDelegate
{
54 explicit ColoredLayer(SkColor color
)
55 : Layer(ui::LAYER_TEXTURED
),
61 ~ColoredLayer() override
{}
63 // Overridden from LayerDelegate:
64 void OnPaintLayer(const ui::PaintContext
& context
) override
{
66 ui::PaintRecorder
recorder(context
, size());
67 recorder
.canvas()->DrawColor(color_
);
71 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
{}
73 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
75 base::Closure
PrepareForLayerBoundsChange() override
{
76 return base::Closure();
79 void set_color(SkColor color
) { color_
= color
; }
80 void set_draw(bool draw
) { draw_
= draw
; }
86 DISALLOW_COPY_AND_ASSIGN(ColoredLayer
);
89 const int kFrames
= 100;
91 // Benchmark base class, hooks up drawing callback and displaying FPS.
92 class BenchCompositorObserver
: public ui::CompositorObserver
{
94 explicit BenchCompositorObserver(int max_frames
)
97 max_frames_(max_frames
) {
99 virtual ~BenchCompositorObserver() {}
101 void OnCompositingDidCommit(ui::Compositor
* compositor
) override
{}
103 void OnCompositingStarted(Compositor
* compositor
,
104 base::TimeTicks start_time
) override
{}
106 void OnCompositingEnded(Compositor
* compositor
) override
{
107 if (start_time_
.is_null()) {
108 start_time_
= TimeTicks::Now();
111 if (frames_
% kFrames
== 0) {
112 TimeTicks now
= TimeTicks::Now();
113 double ms
= (now
- start_time_
).InMillisecondsF() / kFrames
;
114 LOG(INFO
) << "FPS: " << 1000.f
/ ms
<< " (" << ms
<< " ms)";
118 if (max_frames_
&& frames_
== max_frames_
) {
119 base::MessageLoop::current()->Quit();
125 void OnCompositingAborted(Compositor
* compositor
) override
{}
127 void OnCompositingLockStateChanged(Compositor
* compositor
) override
{}
129 void OnCompositingShuttingDown(ui::Compositor
* compositor
) override
{}
131 virtual void Draw() {}
133 int frames() const { return frames_
; }
136 TimeTicks start_time_
;
140 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver
);
143 void ReturnMailbox(scoped_refptr
<cc::ContextProvider
> context_provider
,
147 gpu::gles2::GLES2Interface
* gl
= context_provider
->ContextGL();
148 gl
->WaitSyncPointCHROMIUM(sync_point
);
149 gl
->DeleteTextures(1, &texture
);
150 gl
->ShallowFlushCHROMIUM();
153 // A benchmark that adds a texture layer that is updated every frame.
154 class WebGLBench
: public BenchCompositorObserver
{
156 WebGLBench(ui::ContextFactory
* context_factory
,
158 Compositor
* compositor
,
160 : BenchCompositorObserver(max_frames
),
162 webgl_(ui::LAYER_SOLID_COLOR
),
163 compositor_(compositor
),
166 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
167 do_draw_
= !command_line
->HasSwitch("disable-draw");
169 std::string webgl_size
= command_line
->GetSwitchValueASCII("webgl-size");
172 if (!webgl_size
.empty()) {
173 std::vector
<std::string
> split_size
= base::SplitString(
174 webgl_size
, "x", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
175 if (split_size
.size() == 2) {
176 width
= atoi(split_size
[0].c_str());
177 height
= atoi(split_size
[1].c_str());
180 if (!width
|| !height
) {
184 gfx::Rect
bounds(width
, height
);
185 webgl_
.SetBounds(bounds
);
186 parent_
->Add(&webgl_
);
188 context_provider_
= context_factory
->SharedMainThreadContextProvider();
189 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
191 gl
->GenTextures(1, &texture
);
192 gl
->BindTexture(GL_TEXTURE_2D
, texture
);
193 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
194 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
195 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
196 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
197 gl
->TexImage2D(GL_TEXTURE_2D
,
206 gpu::Mailbox mailbox
;
207 gl
->GenMailboxCHROMIUM(mailbox
.name
);
208 gl
->ProduceTextureCHROMIUM(GL_TEXTURE_2D
, mailbox
.name
);
210 gl
->GenFramebuffers(1, &fbo_
);
211 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
212 gl
->FramebufferTexture2D(
213 GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, texture
, 0);
214 gl
->ClearColor(0.f
, 1.f
, 0.f
, 1.f
);
215 gl
->Clear(GL_COLOR_BUFFER_BIT
);
218 GLuint sync_point
= gl
->InsertSyncPointCHROMIUM();
219 webgl_
.SetTextureMailbox(
220 cc::TextureMailbox(mailbox
, GL_TEXTURE_2D
, sync_point
),
221 cc::SingleReleaseCallback::Create(
222 base::Bind(ReturnMailbox
, context_provider_
, texture
)),
224 compositor
->AddObserver(this);
227 ~WebGLBench() override
{
228 webgl_
.SetShowSolidColorContent();
229 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
230 gl
->DeleteFramebuffers(1, &fbo_
);
231 compositor_
->RemoveObserver(this);
234 void Draw() override
{
236 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
237 gl
->ClearColor((frames() % kFrames
)*1.0/kFrames
, 1.f
, 0.f
, 1.f
);
238 gl
->Clear(GL_COLOR_BUFFER_BIT
);
241 webgl_
.SchedulePaint(gfx::Rect(webgl_
.bounds().size()));
242 compositor_
->ScheduleDraw();
248 Compositor
* compositor_
;
249 scoped_refptr
<cc::ContextProvider
> context_provider_
;
251 // The FBO that is used to render to the texture.
254 // Whether or not to draw to the texture every frame.
257 DISALLOW_COPY_AND_ASSIGN(WebGLBench
);
260 // A benchmark that paints (in software) all tiles every frame.
261 class SoftwareScrollBench
: public BenchCompositorObserver
{
263 SoftwareScrollBench(ColoredLayer
* layer
,
264 Compositor
* compositor
,
266 : BenchCompositorObserver(max_frames
),
268 compositor_(compositor
) {
269 compositor
->AddObserver(this);
271 !base::CommandLine::ForCurrentProcess()->HasSwitch("disable-draw"));
274 ~SoftwareScrollBench() override
{ compositor_
->RemoveObserver(this); }
276 void Draw() override
{
278 SkColorSetARGBInline(255*(frames() % kFrames
)/kFrames
, 255, 0, 255));
279 layer_
->SchedulePaint(gfx::Rect(layer_
->bounds().size()));
283 ColoredLayer
* layer_
;
284 Compositor
* compositor_
;
286 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench
);
291 int main(int argc
, char** argv
) {
292 base::CommandLine::Init(argc
, argv
);
294 base::AtExitManager exit_manager
;
297 // This demo uses InProcessContextFactory which uses X on a separate Gpu
299 gfx::InitializeThreadedX11();
302 gfx::GLSurface::InitializeOneOff();
304 // The ContextFactory must exist before any Compositors are created.
305 bool context_factory_for_test
= false;
306 scoped_ptr
<ui::InProcessContextFactory
> context_factory(
307 new ui::InProcessContextFactory(context_factory_for_test
, nullptr));
309 base::i18n::InitializeICU();
311 base::MessageLoopForUI message_loop
;
312 aura::Env::CreateInstance(true);
313 aura::Env::GetInstance()->set_context_factory(context_factory
.get());
314 scoped_ptr
<aura::TestScreen
> test_screen(
315 aura::TestScreen::CreateFullscreen());
316 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, test_screen
.get());
317 scoped_ptr
<aura::WindowTreeHost
> host(
318 test_screen
->CreateHostForPrimaryDisplay());
319 aura::client::SetCaptureClient(
321 new aura::client::DefaultCaptureClient(host
->window()));
323 scoped_ptr
<aura::client::FocusClient
> focus_client(
324 new aura::test::TestFocusClient
);
325 aura::client::SetFocusClient(host
->window(), focus_client
.get());
328 ColoredLayer
background(SK_ColorRED
);
329 background
.SetBounds(host
->window()->bounds());
330 host
->window()->layer()->Add(&background
);
332 ColoredLayer
window(SK_ColorBLUE
);
333 window
.SetBounds(gfx::Rect(background
.bounds().size()));
334 background
.Add(&window
);
336 Layer
content_layer(ui::LAYER_NOT_DRAWN
);
338 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
339 bool force
= command_line
->HasSwitch("force-render-surface");
340 content_layer
.SetForceRenderSurface(force
);
341 gfx::Rect
bounds(window
.bounds().size());
342 bounds
.Inset(0, 30, 0, 0);
343 content_layer
.SetBounds(bounds
);
344 window
.Add(&content_layer
);
346 ColoredLayer
page_background(SK_ColorWHITE
);
347 page_background
.SetBounds(gfx::Rect(content_layer
.bounds().size()));
348 content_layer
.Add(&page_background
);
350 int frames
= atoi(command_line
->GetSwitchValueASCII("frames").c_str());
351 scoped_ptr
<BenchCompositorObserver
> bench
;
353 if (command_line
->HasSwitch("bench-software-scroll")) {
354 bench
.reset(new SoftwareScrollBench(&page_background
,
358 bench
.reset(new WebGLBench(context_factory
.get(),
365 ui::PrintLayerHierarchy(host
->window()->layer(), gfx::Point(100, 100));
369 base::MessageLoopForUI::current()->Run();
370 focus_client
.reset();