| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/skia_benchmarking_extension.h" | 5 #include "content/renderer/skia_benchmarking_extension.h" |
| 6 | 6 |
| 7 #include "base/values.h" |
| 8 #include "skia/ext/benchmarking_canvas.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
| 9 #include "third_party/skia/include/core/SkGraphics.h" | 11 #include "third_party/skia/include/core/SkGraphics.h" |
| 10 #include "third_party/skia/src/utils/debugger/SkDebugCanvas.h" | |
| 11 #include "third_party/skia/src/utils/debugger/SkDrawCommand.h" | |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 testing::AssertionResult HasInfoField(SkDebugCanvas& canvas, int index, | 15 testing::AssertionResult HasArg(const base::ListValue* args, |
| 16 const char* field) { | 16 const char name[]) { |
| 17 const base::DictionaryValue* arg; |
| 17 | 18 |
| 18 const SkTDArray<SkString*>* info = canvas.getCommandInfo(index); | 19 for (size_t i = 0; i < args->GetSize(); ++i) { |
| 19 if (info == NULL) | 20 if (!args->GetDictionary(i, &arg) || arg->size() != 1) |
| 20 return testing::AssertionFailure() << " command info not found for index " | 21 return testing::AssertionFailure() << " malformed argument for index " |
| 21 << index; | 22 << i; |
| 22 | 23 |
| 23 for (int i = 0; i < info->count(); ++i) { | 24 if (arg->HasKey(name)) |
| 24 const SkString* info_str = (*info)[i]; | 25 return testing::AssertionSuccess() << " argument '" << name |
| 25 if (info_str == NULL) | 26 << "' found at index " << i; |
| 26 return testing::AssertionFailure() << " NULL info string for index " | |
| 27 << index; | |
| 28 | |
| 29 // FIXME: loose info paramter test. | |
| 30 if (strstr(info_str->c_str(), field) != NULL) | |
| 31 return testing::AssertionSuccess() << field << " found"; | |
| 32 } | 27 } |
| 33 | 28 |
| 34 return testing::AssertionFailure() << field << " not found"; | 29 return testing::AssertionFailure() << "argument not found: '" << name << "'"; |
| 35 } | 30 } |
| 36 | 31 |
| 37 } | 32 } |
| 38 | 33 |
| 39 namespace content { | 34 namespace content { |
| 40 | 35 |
| 41 TEST(SkiaBenchmarkingExtensionTest, SkDebugCanvas) { | 36 TEST(SkiaBenchmarkingExtensionTest, BenchmarkingCanvas) { |
| 42 SkGraphics::Init(); | 37 SkGraphics::Init(); |
| 43 | 38 |
| 44 // Prepare canvas and resources. | 39 // Prepare canvas and resources. |
| 45 SkDebugCanvas canvas(100, 100); | 40 SkCanvas canvas(100, 100); |
| 41 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
| 42 |
| 46 SkPaint red_paint; | 43 SkPaint red_paint; |
| 47 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); | 44 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0)); |
| 48 SkRect fullRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); | 45 SkRect fullRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 49 SkRect fillRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), | 46 SkRect fillRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), |
| 50 SkIntToScalar(50), SkIntToScalar(50)); | 47 SkIntToScalar(50), SkIntToScalar(50)); |
| 51 | 48 |
| 52 SkMatrix trans; | 49 SkMatrix trans; |
| 53 trans.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); | 50 trans.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); |
| 54 | 51 |
| 55 // Draw a trivial scene. | 52 // Draw a trivial scene. |
| 56 canvas.save(); | 53 benchmarking_canvas.save(); |
| 57 canvas.clipRect(fullRect, SkRegion::kIntersect_Op, false); | 54 benchmarking_canvas.clipRect(fullRect, SkRegion::kIntersect_Op, false); |
| 58 canvas.setMatrix(trans); | 55 benchmarking_canvas.setMatrix(trans); |
| 59 canvas.drawRect(fillRect, red_paint); | 56 benchmarking_canvas.drawRect(fillRect, red_paint); |
| 60 canvas.restore(); | 57 benchmarking_canvas.restore(); |
| 61 | 58 |
| 62 // Verify the recorded commands. | 59 // Verify the recorded commands. |
| 63 SkDrawCommand::OpType cmd; | 60 const base::ListValue& ops = benchmarking_canvas.Commands(); |
| 64 int idx = 0; | 61 ASSERT_EQ(ops.GetSize(), static_cast<size_t>(5)); |
| 65 ASSERT_EQ(canvas.getSize(), 5); | |
| 66 | 62 |
| 67 ASSERT_TRUE(canvas.getDrawCommandAt(idx) != NULL); | 63 size_t index = 0; |
| 68 cmd = canvas.getDrawCommandAt(idx)->getType(); | 64 const base::DictionaryValue* op; |
| 69 EXPECT_EQ(cmd, SkDrawCommand::kSave_OpType); | 65 const base::ListValue* op_args; |
| 70 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Save"); | 66 std::string op_name; |
| 71 | 67 |
| 72 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL); | 68 ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 73 cmd = canvas.getDrawCommandAt(idx)->getType(); | 69 EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 74 EXPECT_EQ(cmd, SkDrawCommand::kClipRect_OpType); | 70 EXPECT_EQ(op_name, "Save"); |
| 75 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "ClipRect"); | 71 ASSERT_TRUE(op->GetList("info", &op_args)); |
| 76 EXPECT_TRUE(HasInfoField(canvas, idx, "SkRect")); | 72 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0)); |
| 77 EXPECT_TRUE(HasInfoField(canvas, idx, "Op")); | |
| 78 EXPECT_TRUE(HasInfoField(canvas, idx, "doAA")); | |
| 79 | 73 |
| 80 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL); | 74 ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 81 cmd = canvas.getDrawCommandAt(idx)->getType(); | 75 EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 82 EXPECT_EQ(cmd, SkDrawCommand::kSetMatrix_OpType); | 76 EXPECT_EQ(op_name, "ClipRect"); |
| 83 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "SetMatrix"); | 77 ASSERT_TRUE(op->GetList("info", &op_args)); |
| 78 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(3)); |
| 79 EXPECT_TRUE(HasArg(op_args, "rect")); |
| 80 EXPECT_TRUE(HasArg(op_args, "op")); |
| 81 EXPECT_TRUE(HasArg(op_args, "anti-alias")); |
| 84 | 82 |
| 85 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL); | 83 ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 86 cmd = canvas.getDrawCommandAt(idx)->getType(); | 84 EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 87 EXPECT_EQ(cmd, SkDrawCommand::kDrawRect_OpType); | 85 EXPECT_EQ(op_name, "SetMatrix"); |
| 88 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "DrawRect"); | 86 ASSERT_TRUE(op->GetList("info", &op_args)); |
| 89 EXPECT_TRUE(HasInfoField(canvas, idx, "SkRect")); | 87 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(1)); |
| 88 EXPECT_TRUE(HasArg(op_args, "matrix")); |
| 90 | 89 |
| 91 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL); | 90 ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 92 cmd = canvas.getDrawCommandAt(idx)->getType(); | 91 EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 93 EXPECT_EQ(cmd, SkDrawCommand::kRestore_OpType); | 92 EXPECT_EQ(op_name, "DrawRect"); |
| 94 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Restore"); | 93 ASSERT_TRUE(op->GetList("info", &op_args)); |
| 94 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(2)); |
| 95 EXPECT_TRUE(HasArg(op_args, "rect")); |
| 96 EXPECT_TRUE(HasArg(op_args, "paint")); |
| 97 |
| 98 ASSERT_TRUE(ops.GetDictionary(index++, &op)); |
| 99 EXPECT_TRUE(op->GetString("cmd_string", &op_name)); |
| 100 EXPECT_EQ(op_name, "Restore"); |
| 101 ASSERT_TRUE(op->GetList("info", &op_args)); |
| 102 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0)); |
| 95 } | 103 } |
| 96 | 104 |
| 97 } // namespace content | 105 } // namespace content |
| OLD | NEW |