Roll src/third_party/skia 7b05ff1:dab1f60
[chromium-blink-merge.git] / cc / quads / draw_quad_perftest.cc
blob08ce6d07b296ce96f89abf61a4157197eec16e05
1 // Copyright 2015 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 <vector>
7 #include "base/bind.h"
8 #include "base/time/time.h"
9 #include "cc/debug/lap_timer.h"
10 #include "cc/quads/draw_quad.h"
11 #include "cc/quads/render_pass.h"
12 #include "cc/quads/texture_draw_quad.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/perf/perf_test.h"
16 namespace cc {
17 namespace {
19 static const int kTimeLimitMillis = 2000;
20 static const int kWarmupRuns = 5;
21 static const int kTimeCheckInterval = 10;
23 SharedQuadState* CreateSharedQuadState(RenderPass* render_pass) {
24 gfx::Transform quad_transform = gfx::Transform(1.0, 0.0, 0.5, 1.0, 0.5, 0.0);
25 gfx::Size content_bounds(26, 28);
26 gfx::Rect visible_layer_rect(10, 12, 14, 16);
27 gfx::Rect clip_rect(19, 21, 23, 25);
28 bool is_clipped = false;
29 float opacity = 1.f;
30 int sorting_context_id = 65536;
31 SkXfermode::Mode blend_mode = SkXfermode::kSrcOver_Mode;
33 SharedQuadState* state = render_pass->CreateAndAppendSharedQuadState();
34 state->SetAll(quad_transform, content_bounds, visible_layer_rect, clip_rect,
35 is_clipped, opacity, blend_mode, sorting_context_id);
36 return state;
39 class DrawQuadPerfTest : public testing::Test {
40 public:
41 DrawQuadPerfTest()
42 : timer_(kWarmupRuns,
43 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
44 kTimeCheckInterval) {}
46 void CreateRenderPass() {
47 render_pass_ = RenderPass::Create();
48 SharedQuadState* new_shared_state(
49 CreateSharedQuadState(render_pass_.get()));
50 shared_state_ = render_pass_->CreateAndAppendSharedQuadState();
51 shared_state_->CopyFrom(new_shared_state);
54 void CleanUpRenderPass() {
55 render_pass_.reset();
56 shared_state_ = nullptr;
59 void GenerateTextureDrawQuads(int count, std::vector<DrawQuad*>* quads) {
60 for (int i = 0; i < count; ++i) {
61 TextureDrawQuad* quad =
62 render_pass_->CreateAndAppendDrawQuad<TextureDrawQuad>();
63 gfx::Rect rect(0, 0, 100, 100);
64 ResourceId resource_id = 1;
65 bool premultiplied_alpha = true;
66 gfx::PointF uv_top_left(0, 0);
67 gfx::PointF uv_bottom_right(1, 1);
68 SkColor background_color = SK_ColorRED;
69 float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
70 bool y_flipped = false;
71 bool nearest_neighbor = true;
73 quad->SetNew(shared_state_, rect, rect, rect, resource_id,
74 premultiplied_alpha, uv_top_left, uv_bottom_right,
75 background_color, vertex_opacity, y_flipped,
76 nearest_neighbor);
77 quads->push_back(quad);
81 void RunIterateResourceTest(const std::string& test_name, int quad_count) {
82 CreateRenderPass();
83 std::vector<DrawQuad*> quads;
84 GenerateTextureDrawQuads(quad_count, &quads);
86 timer_.Reset();
87 do {
88 for (auto* quad : quads) {
89 for (ResourceId& resource_id : quad->resources)
90 ++resource_id;
92 timer_.NextLap();
93 } while (!timer_.HasTimeLimitExpired());
95 perf_test::PrintResult("draw_quad_iterate_resources", "", test_name,
96 timer_.LapsPerSecond(), "runs/s", true);
97 CleanUpRenderPass();
100 private:
101 scoped_ptr<RenderPass> render_pass_;
102 SharedQuadState* shared_state_;
103 LapTimer timer_;
106 TEST_F(DrawQuadPerfTest, IterateResources) {
107 RunIterateResourceTest("10_quads", 10);
108 RunIterateResourceTest("100_quads", 100);
109 RunIterateResourceTest("500_quads", 500);
112 } // namespace
113 } // namespace cc