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/test/in_process_context_factory.h"
33 #include "ui/gfx/canvas.h"
34 #include "ui/gfx/geometry/rect.h"
35 #include "ui/gfx/skia_util.h"
36 #include "ui/gfx/x/x11_connection.h"
37 #include "ui/gl/gl_surface.h"
39 #ifndef GL_GLEXT_PROTOTYPES
40 #define GL_GLEXT_PROTOTYPES 1
42 #include "third_party/khronos/GLES2/gl2ext.h"
44 using base::TimeTicks
;
47 using ui::LayerDelegate
;
51 class ColoredLayer
: public Layer
, public LayerDelegate
{
53 explicit ColoredLayer(SkColor color
)
54 : Layer(ui::LAYER_TEXTURED
),
60 ~ColoredLayer() override
{}
62 // Overridden from LayerDelegate:
63 void OnPaintLayer(gfx::Canvas
* canvas
) override
{
65 canvas
->DrawColor(color_
);
69 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
{}
71 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
73 base::Closure
PrepareForLayerBoundsChange() override
{
74 return base::Closure();
77 void set_color(SkColor color
) { color_
= color
; }
78 void set_draw(bool draw
) { draw_
= draw
; }
84 DISALLOW_COPY_AND_ASSIGN(ColoredLayer
);
87 const int kFrames
= 100;
89 // Benchmark base class, hooks up drawing callback and displaying FPS.
90 class BenchCompositorObserver
: public ui::CompositorObserver
{
92 explicit BenchCompositorObserver(int max_frames
)
95 max_frames_(max_frames
) {
97 virtual ~BenchCompositorObserver() {}
99 void OnCompositingDidCommit(ui::Compositor
* compositor
) override
{}
101 void OnCompositingStarted(Compositor
* compositor
,
102 base::TimeTicks start_time
) override
{}
104 void OnCompositingEnded(Compositor
* compositor
) override
{
105 if (start_time_
.is_null()) {
106 start_time_
= TimeTicks::Now();
109 if (frames_
% kFrames
== 0) {
110 TimeTicks now
= TimeTicks::Now();
111 double ms
= (now
- start_time_
).InMillisecondsF() / kFrames
;
112 LOG(INFO
) << "FPS: " << 1000.f
/ ms
<< " (" << ms
<< " ms)";
116 if (max_frames_
&& frames_
== max_frames_
) {
117 base::MessageLoop::current()->Quit();
123 void OnCompositingAborted(Compositor
* compositor
) override
{}
125 void OnCompositingLockStateChanged(Compositor
* compositor
) override
{}
127 void OnCompositingShuttingDown(ui::Compositor
* compositor
) override
{}
129 virtual void Draw() {}
131 int frames() const { return frames_
; }
134 TimeTicks start_time_
;
138 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver
);
141 void ReturnMailbox(scoped_refptr
<cc::ContextProvider
> context_provider
,
145 gpu::gles2::GLES2Interface
* gl
= context_provider
->ContextGL();
146 gl
->WaitSyncPointCHROMIUM(sync_point
);
147 gl
->DeleteTextures(1, &texture
);
148 gl
->ShallowFlushCHROMIUM();
151 // A benchmark that adds a texture layer that is updated every frame.
152 class WebGLBench
: public BenchCompositorObserver
{
154 WebGLBench(ui::ContextFactory
* context_factory
,
156 Compositor
* compositor
,
158 : BenchCompositorObserver(max_frames
),
160 webgl_(ui::LAYER_SOLID_COLOR
),
161 compositor_(compositor
),
164 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
165 do_draw_
= !command_line
->HasSwitch("disable-draw");
167 std::string webgl_size
= command_line
->GetSwitchValueASCII("webgl-size");
170 if (!webgl_size
.empty()) {
171 std::vector
<std::string
> split_size
;
172 base::SplitString(webgl_size
, 'x', &split_size
);
173 if (split_size
.size() == 2) {
174 width
= atoi(split_size
[0].c_str());
175 height
= atoi(split_size
[1].c_str());
178 if (!width
|| !height
) {
182 gfx::Rect
bounds(width
, height
);
183 webgl_
.SetBounds(bounds
);
184 parent_
->Add(&webgl_
);
186 context_provider_
= context_factory
->SharedMainThreadContextProvider();
187 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
189 gl
->GenTextures(1, &texture
);
190 gl
->BindTexture(GL_TEXTURE_2D
, texture
);
191 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
192 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
193 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
194 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
195 gl
->TexImage2D(GL_TEXTURE_2D
,
204 gpu::Mailbox mailbox
;
205 gl
->GenMailboxCHROMIUM(mailbox
.name
);
206 gl
->ProduceTextureCHROMIUM(GL_TEXTURE_2D
, mailbox
.name
);
208 gl
->GenFramebuffers(1, &fbo_
);
209 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
210 gl
->FramebufferTexture2D(
211 GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, texture
, 0);
212 gl
->ClearColor(0.f
, 1.f
, 0.f
, 1.f
);
213 gl
->Clear(GL_COLOR_BUFFER_BIT
);
216 GLuint sync_point
= gl
->InsertSyncPointCHROMIUM();
217 webgl_
.SetTextureMailbox(
218 cc::TextureMailbox(mailbox
, GL_TEXTURE_2D
, sync_point
),
219 cc::SingleReleaseCallback::Create(
220 base::Bind(ReturnMailbox
, context_provider_
, texture
)),
222 compositor
->AddObserver(this);
225 ~WebGLBench() override
{
226 webgl_
.SetShowSolidColorContent();
227 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
228 gl
->DeleteFramebuffers(1, &fbo_
);
229 compositor_
->RemoveObserver(this);
232 void Draw() override
{
234 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
235 gl
->ClearColor((frames() % kFrames
)*1.0/kFrames
, 1.f
, 0.f
, 1.f
);
236 gl
->Clear(GL_COLOR_BUFFER_BIT
);
239 webgl_
.SchedulePaint(gfx::Rect(webgl_
.bounds().size()));
240 compositor_
->ScheduleDraw();
246 Compositor
* compositor_
;
247 scoped_refptr
<cc::ContextProvider
> context_provider_
;
249 // The FBO that is used to render to the texture.
252 // Whether or not to draw to the texture every frame.
255 DISALLOW_COPY_AND_ASSIGN(WebGLBench
);
258 // A benchmark that paints (in software) all tiles every frame.
259 class SoftwareScrollBench
: public BenchCompositorObserver
{
261 SoftwareScrollBench(ColoredLayer
* layer
,
262 Compositor
* compositor
,
264 : BenchCompositorObserver(max_frames
),
266 compositor_(compositor
) {
267 compositor
->AddObserver(this);
269 !base::CommandLine::ForCurrentProcess()->HasSwitch("disable-draw"));
272 ~SoftwareScrollBench() override
{ compositor_
->RemoveObserver(this); }
274 void Draw() override
{
276 SkColorSetARGBInline(255*(frames() % kFrames
)/kFrames
, 255, 0, 255));
277 layer_
->SchedulePaint(gfx::Rect(layer_
->bounds().size()));
281 ColoredLayer
* layer_
;
282 Compositor
* compositor_
;
284 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench
);
289 int main(int argc
, char** argv
) {
290 base::CommandLine::Init(argc
, argv
);
292 base::AtExitManager exit_manager
;
295 // This demo uses InProcessContextFactory which uses X on a separate Gpu
297 gfx::InitializeThreadedX11();
300 gfx::GLSurface::InitializeOneOff();
302 // The ContextFactory must exist before any Compositors are created.
303 bool context_factory_for_test
= false;
304 scoped_ptr
<ui::InProcessContextFactory
> context_factory(
305 new ui::InProcessContextFactory(context_factory_for_test
, nullptr));
307 base::i18n::InitializeICU();
309 base::MessageLoopForUI message_loop
;
310 aura::Env::CreateInstance(true);
311 aura::Env::GetInstance()->set_context_factory(context_factory
.get());
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::WindowTreeHost
> host(
316 test_screen
->CreateHostForPrimaryDisplay());
317 aura::client::SetCaptureClient(
319 new aura::client::DefaultCaptureClient(host
->window()));
321 scoped_ptr
<aura::client::FocusClient
> focus_client(
322 new aura::test::TestFocusClient
);
323 aura::client::SetFocusClient(host
->window(), focus_client
.get());
326 ColoredLayer
background(SK_ColorRED
);
327 background
.SetBounds(host
->window()->bounds());
328 host
->window()->layer()->Add(&background
);
330 ColoredLayer
window(SK_ColorBLUE
);
331 window
.SetBounds(gfx::Rect(background
.bounds().size()));
332 background
.Add(&window
);
334 Layer
content_layer(ui::LAYER_NOT_DRAWN
);
336 base::CommandLine
* command_line
= base::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
);
344 ColoredLayer
page_background(SK_ColorWHITE
);
345 page_background
.SetBounds(gfx::Rect(content_layer
.bounds().size()));
346 content_layer
.Add(&page_background
);
348 int frames
= atoi(command_line
->GetSwitchValueASCII("frames").c_str());
349 scoped_ptr
<BenchCompositorObserver
> bench
;
351 if (command_line
->HasSwitch("bench-software-scroll")) {
352 bench
.reset(new SoftwareScrollBench(&page_background
,
356 bench
.reset(new WebGLBench(context_factory
.get(),
363 ui::PrintLayerHierarchy(host
->window()->layer(), gfx::Point(100, 100));
367 base::MessageLoopForUI::current()->Run();
368 focus_client
.reset();