| OLD | NEW |
| (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 #if defined(USE_X11) | |
| 6 #include <X11/Xlib.h> | |
| 7 #endif | |
| 8 | |
| 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/macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/strings/string_split.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "cc/output/context_provider.h" | |
| 20 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 21 #include "third_party/khronos/GLES2/gl2.h" | |
| 22 #include "third_party/skia/include/core/SkXfermode.h" | |
| 23 #include "ui/aura/client/default_capture_client.h" | |
| 24 #include "ui/aura/env.h" | |
| 25 #include "ui/aura/test/test_focus_client.h" | |
| 26 #include "ui/aura/test/test_screen.h" | |
| 27 #include "ui/aura/window.h" | |
| 28 #include "ui/aura/window_tree_host.h" | |
| 29 #include "ui/base/hit_test.h" | |
| 30 #include "ui/compositor/compositor.h" | |
| 31 #include "ui/compositor/compositor_observer.h" | |
| 32 #include "ui/compositor/debug_utils.h" | |
| 33 #include "ui/compositor/layer.h" | |
| 34 #include "ui/compositor/paint_recorder.h" | |
| 35 #include "ui/compositor/test/in_process_context_factory.h" | |
| 36 #include "ui/gfx/canvas.h" | |
| 37 #include "ui/gfx/geometry/rect.h" | |
| 38 #include "ui/gfx/skia_util.h" | |
| 39 #include "ui/gfx/x/x11_connection.h" | |
| 40 #include "ui/gl/gl_surface.h" | |
| 41 | |
| 42 #ifndef GL_GLEXT_PROTOTYPES | |
| 43 #define GL_GLEXT_PROTOTYPES 1 | |
| 44 #endif | |
| 45 #include "third_party/khronos/GLES2/gl2ext.h" | |
| 46 | |
| 47 using base::TimeTicks; | |
| 48 using ui::Compositor; | |
| 49 using ui::Layer; | |
| 50 using ui::LayerDelegate; | |
| 51 | |
| 52 namespace { | |
| 53 | |
| 54 class ColoredLayer : public Layer, public LayerDelegate { | |
| 55 public: | |
| 56 explicit ColoredLayer(SkColor color) | |
| 57 : Layer(ui::LAYER_TEXTURED), | |
| 58 color_(color), | |
| 59 draw_(true) { | |
| 60 set_delegate(this); | |
| 61 } | |
| 62 | |
| 63 ~ColoredLayer() override {} | |
| 64 | |
| 65 // Overridden from LayerDelegate: | |
| 66 void OnPaintLayer(const ui::PaintContext& context) override { | |
| 67 if (draw_) { | |
| 68 ui::PaintRecorder recorder(context, size()); | |
| 69 recorder.canvas()->DrawColor(color_); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} | |
| 74 | |
| 75 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} | |
| 76 | |
| 77 base::Closure PrepareForLayerBoundsChange() override { | |
| 78 return base::Closure(); | |
| 79 } | |
| 80 | |
| 81 void set_color(SkColor color) { color_ = color; } | |
| 82 void set_draw(bool draw) { draw_ = draw; } | |
| 83 | |
| 84 private: | |
| 85 SkColor color_; | |
| 86 bool draw_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(ColoredLayer); | |
| 89 }; | |
| 90 | |
| 91 const int kFrames = 100; | |
| 92 | |
| 93 // Benchmark base class, hooks up drawing callback and displaying FPS. | |
| 94 class BenchCompositorObserver : public ui::CompositorObserver { | |
| 95 public: | |
| 96 explicit BenchCompositorObserver(int max_frames) | |
| 97 : start_time_(), | |
| 98 frames_(0), | |
| 99 max_frames_(max_frames) { | |
| 100 } | |
| 101 virtual ~BenchCompositorObserver() {} | |
| 102 | |
| 103 void OnCompositingDidCommit(ui::Compositor* compositor) override {} | |
| 104 | |
| 105 void OnCompositingStarted(Compositor* compositor, | |
| 106 base::TimeTicks start_time) override {} | |
| 107 | |
| 108 void OnCompositingEnded(Compositor* compositor) override { | |
| 109 if (start_time_.is_null()) { | |
| 110 start_time_ = TimeTicks::Now(); | |
| 111 } else { | |
| 112 ++frames_; | |
| 113 if (frames_ % kFrames == 0) { | |
| 114 TimeTicks now = TimeTicks::Now(); | |
| 115 double ms = (now - start_time_).InMillisecondsF() / kFrames; | |
| 116 LOG(INFO) << "FPS: " << 1000.f / ms << " (" << ms << " ms)"; | |
| 117 start_time_ = now; | |
| 118 } | |
| 119 } | |
| 120 if (max_frames_ && frames_ == max_frames_) { | |
| 121 base::MessageLoop::current()->QuitWhenIdle(); | |
| 122 } else { | |
| 123 Draw(); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void OnCompositingAborted(Compositor* compositor) override {} | |
| 128 | |
| 129 void OnCompositingLockStateChanged(Compositor* compositor) override {} | |
| 130 | |
| 131 void OnCompositingShuttingDown(ui::Compositor* compositor) override {} | |
| 132 | |
| 133 virtual void Draw() {} | |
| 134 | |
| 135 int frames() const { return frames_; } | |
| 136 | |
| 137 private: | |
| 138 TimeTicks start_time_; | |
| 139 int frames_; | |
| 140 int max_frames_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(BenchCompositorObserver); | |
| 143 }; | |
| 144 | |
| 145 void ReturnMailbox(scoped_refptr<cc::ContextProvider> context_provider, | |
| 146 GLuint texture, | |
| 147 const gpu::SyncToken& sync_token, | |
| 148 bool is_lost) { | |
| 149 gpu::gles2::GLES2Interface* gl = context_provider->ContextGL(); | |
| 150 gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); | |
| 151 gl->DeleteTextures(1, &texture); | |
| 152 gl->ShallowFlushCHROMIUM(); | |
| 153 } | |
| 154 | |
| 155 gfx::Size GetFullscreenSize() { | |
| 156 #if defined(OS_WIN) | |
| 157 return gfx::Size(GetSystemMetrics(SM_CXSCREEN), | |
| 158 GetSystemMetrics(SM_CYSCREEN)); | |
| 159 #else | |
| 160 NOTIMPLEMENTED(); | |
| 161 return gfx::Size(1024, 768); | |
| 162 #endif | |
| 163 } | |
| 164 | |
| 165 // A benchmark that adds a texture layer that is updated every frame. | |
| 166 class WebGLBench : public BenchCompositorObserver { | |
| 167 public: | |
| 168 WebGLBench(ui::ContextFactory* context_factory, | |
| 169 Layer* parent, | |
| 170 Compositor* compositor, | |
| 171 int max_frames) | |
| 172 : BenchCompositorObserver(max_frames), | |
| 173 parent_(parent), | |
| 174 webgl_(ui::LAYER_SOLID_COLOR), | |
| 175 compositor_(compositor), | |
| 176 fbo_(0), | |
| 177 do_draw_(true) { | |
| 178 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 179 do_draw_ = !command_line->HasSwitch("disable-draw"); | |
| 180 | |
| 181 std::string webgl_size = command_line->GetSwitchValueASCII("webgl-size"); | |
| 182 int width = 0; | |
| 183 int height = 0; | |
| 184 if (!webgl_size.empty()) { | |
| 185 std::vector<std::string> split_size = base::SplitString( | |
| 186 webgl_size, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 187 if (split_size.size() == 2) { | |
| 188 width = atoi(split_size[0].c_str()); | |
| 189 height = atoi(split_size[1].c_str()); | |
| 190 } | |
| 191 } | |
| 192 if (!width || !height) { | |
| 193 width = 800; | |
| 194 height = 600; | |
| 195 } | |
| 196 gfx::Rect bounds(width, height); | |
| 197 webgl_.SetBounds(bounds); | |
| 198 parent_->Add(&webgl_); | |
| 199 | |
| 200 context_provider_ = context_factory->SharedMainThreadContextProvider(); | |
| 201 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
| 202 GLuint texture = 0; | |
| 203 gl->GenTextures(1, &texture); | |
| 204 gl->BindTexture(GL_TEXTURE_2D, texture); | |
| 205 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 206 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 207 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 208 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 209 gl->TexImage2D(GL_TEXTURE_2D, | |
| 210 0, | |
| 211 GL_RGBA, | |
| 212 width, | |
| 213 height, | |
| 214 0, | |
| 215 GL_RGBA, | |
| 216 GL_UNSIGNED_BYTE, | |
| 217 NULL); | |
| 218 gpu::Mailbox mailbox; | |
| 219 gl->GenMailboxCHROMIUM(mailbox.name); | |
| 220 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | |
| 221 | |
| 222 gl->GenFramebuffers(1, &fbo_); | |
| 223 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); | |
| 224 gl->FramebufferTexture2D( | |
| 225 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); | |
| 226 gl->ClearColor(0.f, 1.f, 0.f, 1.f); | |
| 227 gl->Clear(GL_COLOR_BUFFER_BIT); | |
| 228 | |
| 229 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); | |
| 230 gl->Flush(); | |
| 231 | |
| 232 gpu::SyncToken sync_token; | |
| 233 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | |
| 234 webgl_.SetTextureMailbox( | |
| 235 cc::TextureMailbox(mailbox, sync_token, GL_TEXTURE_2D), | |
| 236 cc::SingleReleaseCallback::Create( | |
| 237 base::Bind(ReturnMailbox, context_provider_, texture)), | |
| 238 bounds.size()); | |
| 239 compositor->AddObserver(this); | |
| 240 } | |
| 241 | |
| 242 ~WebGLBench() override { | |
| 243 webgl_.SetShowSolidColorContent(); | |
| 244 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
| 245 gl->DeleteFramebuffers(1, &fbo_); | |
| 246 compositor_->RemoveObserver(this); | |
| 247 } | |
| 248 | |
| 249 void Draw() override { | |
| 250 if (do_draw_) { | |
| 251 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
| 252 gl->ClearColor((frames() % kFrames)*1.0/kFrames, 1.f, 0.f, 1.f); | |
| 253 gl->Clear(GL_COLOR_BUFFER_BIT); | |
| 254 gl->Flush(); | |
| 255 } | |
| 256 webgl_.SchedulePaint(gfx::Rect(webgl_.bounds().size())); | |
| 257 compositor_->ScheduleDraw(); | |
| 258 } | |
| 259 | |
| 260 private: | |
| 261 Layer* parent_; | |
| 262 Layer webgl_; | |
| 263 Compositor* compositor_; | |
| 264 scoped_refptr<cc::ContextProvider> context_provider_; | |
| 265 | |
| 266 // The FBO that is used to render to the texture. | |
| 267 unsigned int fbo_; | |
| 268 | |
| 269 // Whether or not to draw to the texture every frame. | |
| 270 bool do_draw_; | |
| 271 | |
| 272 DISALLOW_COPY_AND_ASSIGN(WebGLBench); | |
| 273 }; | |
| 274 | |
| 275 // A benchmark that paints (in software) all tiles every frame. | |
| 276 class SoftwareScrollBench : public BenchCompositorObserver { | |
| 277 public: | |
| 278 SoftwareScrollBench(ColoredLayer* layer, | |
| 279 Compositor* compositor, | |
| 280 int max_frames) | |
| 281 : BenchCompositorObserver(max_frames), | |
| 282 layer_(layer), | |
| 283 compositor_(compositor) { | |
| 284 compositor->AddObserver(this); | |
| 285 layer_->set_draw( | |
| 286 !base::CommandLine::ForCurrentProcess()->HasSwitch("disable-draw")); | |
| 287 } | |
| 288 | |
| 289 ~SoftwareScrollBench() override { compositor_->RemoveObserver(this); } | |
| 290 | |
| 291 void Draw() override { | |
| 292 layer_->set_color( | |
| 293 SkColorSetARGBInline(255*(frames() % kFrames)/kFrames, 255, 0, 255)); | |
| 294 layer_->SchedulePaint(gfx::Rect(layer_->bounds().size())); | |
| 295 } | |
| 296 | |
| 297 private: | |
| 298 ColoredLayer* layer_; | |
| 299 Compositor* compositor_; | |
| 300 | |
| 301 DISALLOW_COPY_AND_ASSIGN(SoftwareScrollBench); | |
| 302 }; | |
| 303 | |
| 304 } // namespace | |
| 305 | |
| 306 int main(int argc, char** argv) { | |
| 307 base::CommandLine::Init(argc, argv); | |
| 308 | |
| 309 base::AtExitManager exit_manager; | |
| 310 | |
| 311 #if defined(USE_X11) | |
| 312 // This demo uses InProcessContextFactory which uses X on a separate Gpu | |
| 313 // thread. | |
| 314 gfx::InitializeThreadedX11(); | |
| 315 #endif | |
| 316 | |
| 317 gfx::GLSurface::InitializeOneOff(); | |
| 318 | |
| 319 // The ContextFactory must exist before any Compositors are created. | |
| 320 bool context_factory_for_test = false; | |
| 321 scoped_ptr<ui::InProcessContextFactory> context_factory( | |
| 322 new ui::InProcessContextFactory(context_factory_for_test, nullptr)); | |
| 323 | |
| 324 base::i18n::InitializeICU(); | |
| 325 | |
| 326 base::MessageLoopForUI message_loop; | |
| 327 aura::Env::CreateInstance(true); | |
| 328 aura::Env::GetInstance()->set_context_factory(context_factory.get()); | |
| 329 scoped_ptr<aura::TestScreen> test_screen( | |
| 330 aura::TestScreen::Create(GetFullscreenSize())); | |
| 331 gfx::Screen::SetScreenInstance(test_screen.get()); | |
| 332 scoped_ptr<aura::WindowTreeHost> host( | |
| 333 test_screen->CreateHostForPrimaryDisplay()); | |
| 334 aura::client::SetCaptureClient( | |
| 335 host->window(), | |
| 336 new aura::client::DefaultCaptureClient(host->window())); | |
| 337 | |
| 338 scoped_ptr<aura::client::FocusClient> focus_client( | |
| 339 new aura::test::TestFocusClient); | |
| 340 aura::client::SetFocusClient(host->window(), focus_client.get()); | |
| 341 | |
| 342 // add layers | |
| 343 ColoredLayer background(SK_ColorRED); | |
| 344 background.SetBounds(host->window()->bounds()); | |
| 345 host->window()->layer()->Add(&background); | |
| 346 | |
| 347 ColoredLayer window(SK_ColorBLUE); | |
| 348 window.SetBounds(gfx::Rect(background.bounds().size())); | |
| 349 background.Add(&window); | |
| 350 | |
| 351 Layer content_layer(ui::LAYER_NOT_DRAWN); | |
| 352 | |
| 353 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 354 bool force = command_line->HasSwitch("force-render-surface"); | |
| 355 content_layer.SetForceRenderSurface(force); | |
| 356 gfx::Rect bounds(window.bounds().size()); | |
| 357 bounds.Inset(0, 30, 0, 0); | |
| 358 content_layer.SetBounds(bounds); | |
| 359 window.Add(&content_layer); | |
| 360 | |
| 361 ColoredLayer page_background(SK_ColorWHITE); | |
| 362 page_background.SetBounds(gfx::Rect(content_layer.bounds().size())); | |
| 363 content_layer.Add(&page_background); | |
| 364 | |
| 365 int frames = atoi(command_line->GetSwitchValueASCII("frames").c_str()); | |
| 366 scoped_ptr<BenchCompositorObserver> bench; | |
| 367 | |
| 368 if (command_line->HasSwitch("bench-software-scroll")) { | |
| 369 bench.reset(new SoftwareScrollBench(&page_background, | |
| 370 host->compositor(), | |
| 371 frames)); | |
| 372 } else { | |
| 373 bench.reset(new WebGLBench(context_factory.get(), | |
| 374 &page_background, | |
| 375 host->compositor(), | |
| 376 frames)); | |
| 377 } | |
| 378 | |
| 379 #ifndef NDEBUG | |
| 380 ui::PrintLayerHierarchy(host->window()->layer(), gfx::Point(100, 100)); | |
| 381 #endif | |
| 382 | |
| 383 host->Show(); | |
| 384 base::MessageLoopForUI::current()->Run(); | |
| 385 focus_client.reset(); | |
| 386 host.reset(); | |
| 387 | |
| 388 return 0; | |
| 389 } | |
| OLD | NEW |