[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 1 | // Copyright (c) 2013 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 "content/renderer/skia_benchmarking_extension.h" |
| 6 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 9 | #include "base/values.h" |
| 10 | #include "skia/ext/benchmarking_canvas.h" |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "third_party/skia/include/core/SkCanvas.h" |
| 13 | #include "third_party/skia/include/core/SkGraphics.h" |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 17 | testing::AssertionResult HasArg(const base::ListValue* args, |
| 18 | const char name[]) { |
| 19 | const base::DictionaryValue* arg; |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 20 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 21 | for (size_t i = 0; i < args->GetSize(); ++i) { |
| 22 | if (!args->GetDictionary(i, &arg) || arg->size() != 1) |
| 23 | return testing::AssertionFailure() << " malformed argument for index " |
| 24 | << i; |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 25 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 26 | if (arg->HasKey(name)) |
| 27 | return testing::AssertionSuccess() << " argument '" << name |
| 28 | << "' found at index " << i; |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 29 | } |
| 30 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 31 | return testing::AssertionFailure() << "argument not found: '" << name << "'"; |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | } |
| 35 | |
| 36 | namespace content { |
| 37 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 38 | TEST(SkiaBenchmarkingExtensionTest, BenchmarkingCanvas) { |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 39 | SkGraphics::Init(); |
| 40 | |
| 41 | // Prepare canvas and resources. |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 42 | SkCanvas canvas(100, 100); |
| 43 | skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
| 44 | |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 45 | SkPaint red_paint; |
| 46 | red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); |
| 47 | SkRect fullRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 48 | SkRect fillRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), |
| 49 | SkIntToScalar(50), SkIntToScalar(50)); |
| 50 | |
robertphillips | e83e75c | 2015-02-12 19:40:51 | [diff] [blame] | 51 | SkMatrix trans; |
| 52 | trans.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); |
| 53 | |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 54 | // Draw a trivial scene. |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 55 | benchmarking_canvas.save(); |
| 56 | benchmarking_canvas.clipRect(fullRect, SkRegion::kIntersect_Op, false); |
| 57 | benchmarking_canvas.setMatrix(trans); |
| 58 | benchmarking_canvas.drawRect(fillRect, red_paint); |
| 59 | benchmarking_canvas.restore(); |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 60 | |
| 61 | // Verify the recorded commands. |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 62 | const base::ListValue& ops = benchmarking_canvas.Commands(); |
| 63 | ASSERT_EQ(ops.GetSize(), static_cast<size_t>(5)); |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 64 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 65 | size_t index = 0; |
| 66 | const base::DictionaryValue* op; |
| 67 | const base::ListValue* op_args; |
| 68 | std::string op_name; |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 69 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 70 | ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 71 | EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 72 | EXPECT_EQ(op_name, "Save"); |
| 73 | ASSERT_TRUE(op->GetList("info", &op_args)); |
| 74 | EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0)); |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 75 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 76 | ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 77 | EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 78 | EXPECT_EQ(op_name, "ClipRect"); |
| 79 | ASSERT_TRUE(op->GetList("info", &op_args)); |
| 80 | EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(3)); |
| 81 | EXPECT_TRUE(HasArg(op_args, "rect")); |
| 82 | EXPECT_TRUE(HasArg(op_args, "op")); |
| 83 | EXPECT_TRUE(HasArg(op_args, "anti-alias")); |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 84 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 85 | ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 86 | EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 87 | EXPECT_EQ(op_name, "SetMatrix"); |
| 88 | ASSERT_TRUE(op->GetList("info", &op_args)); |
| 89 | EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(1)); |
| 90 | EXPECT_TRUE(HasArg(op_args, "matrix")); |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 91 | |
fmalita | f5190e0 | 2015-02-26 15:48:42 | [diff] [blame] | 92 | ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 93 | EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 94 | EXPECT_EQ(op_name, "DrawRect"); |
| 95 | ASSERT_TRUE(op->GetList("info", &op_args)); |
| 96 | EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(2)); |
| 97 | EXPECT_TRUE(HasArg(op_args, "rect")); |
| 98 | EXPECT_TRUE(HasArg(op_args, "paint")); |
| 99 | |
| 100 | ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 101 | EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 102 | EXPECT_EQ(op_name, "Restore"); |
| 103 | ASSERT_TRUE(op->GetList("info", &op_args)); |
| 104 | EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0)); |
[email protected] | cb9118b3 | 2013-06-26 14:15:07 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace content |