| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "cc/paint/paint_op_buffer.h" | 5 #include "cc/paint/paint_op_buffer.h" |
| 6 #include "cc/test/test_skcanvas.h" | 6 #include "cc/test/test_skcanvas.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 249 |
| 250 SaveCountingCanvas canvas; | 250 SaveCountingCanvas canvas; |
| 251 buffer.playback(&canvas); | 251 buffer.playback(&canvas); |
| 252 | 252 |
| 253 EXPECT_EQ(1, canvas.save_count_); | 253 EXPECT_EQ(1, canvas.save_count_); |
| 254 EXPECT_EQ(1, canvas.restore_count_); | 254 EXPECT_EQ(1, canvas.restore_count_); |
| 255 EXPECT_EQ(rect, canvas.draw_rect_); | 255 EXPECT_EQ(rect, canvas.draw_rect_); |
| 256 EXPECT_EQ(draw_flags.getAlpha(), canvas.paint_.getAlpha()); | 256 EXPECT_EQ(draw_flags.getAlpha(), canvas.paint_.getAlpha()); |
| 257 } | 257 } |
| 258 | 258 |
| 259 // Verify that the save draw restore code works with a single op |
| 260 // that's not a draw op, and the optimization does not kick in. |
| 261 TEST(PaintOpBufferTest, SaveDrawRestore_SingleOpNotADrawOp) { |
| 262 PaintOpBuffer buffer; |
| 263 |
| 264 uint8_t alpha = 100; |
| 265 buffer.push<SaveLayerAlphaOp>(nullptr, alpha); |
| 266 |
| 267 buffer.push<NoopOp>(); |
| 268 buffer.push<RestoreOp>(); |
| 269 |
| 270 SaveCountingCanvas canvas; |
| 271 buffer.playback(&canvas); |
| 272 |
| 273 EXPECT_EQ(1, canvas.save_count_); |
| 274 EXPECT_EQ(1, canvas.restore_count_); |
| 275 } |
| 276 |
| 277 // Test that the save/draw/restore optimization applies if the single op |
| 278 // is a DrawRecord that itself has a single draw op. |
| 279 TEST(PaintOpBufferTest, SaveDrawRestore_SingleOpRecordWithSingleOp) { |
| 280 sk_sp<PaintRecord> record = sk_make_sp<PaintRecord>(); |
| 281 |
| 282 PaintFlags draw_flags; |
| 283 draw_flags.setColor(SK_ColorMAGENTA); |
| 284 draw_flags.setAlpha(50); |
| 285 EXPECT_TRUE(draw_flags.SupportsFoldingAlpha()); |
| 286 SkRect rect = SkRect::MakeXYWH(1, 2, 3, 4); |
| 287 record->push<DrawRectOp>(rect, draw_flags); |
| 288 EXPECT_EQ(record->approximateOpCount(), 1); |
| 289 |
| 290 PaintOpBuffer buffer; |
| 291 |
| 292 uint8_t alpha = 100; |
| 293 buffer.push<SaveLayerAlphaOp>(nullptr, alpha); |
| 294 buffer.push<DrawRecordOp>(std::move(record)); |
| 295 buffer.push<RestoreOp>(); |
| 296 |
| 297 SaveCountingCanvas canvas; |
| 298 buffer.playback(&canvas); |
| 299 |
| 300 EXPECT_EQ(0, canvas.save_count_); |
| 301 EXPECT_EQ(0, canvas.restore_count_); |
| 302 EXPECT_EQ(rect, canvas.draw_rect_); |
| 303 |
| 304 float expected_alpha = alpha * 50 / 255.f; |
| 305 EXPECT_LE(std::abs(expected_alpha - canvas.paint_.getAlpha()), 1.f); |
| 306 } |
| 307 |
| 308 // The same as the above SingleOpRecord test, but the single op is not |
| 309 // a draw op. So, there's no way to fold in the save layer optimization. |
| 310 // Verify that the optimization doesn't apply and that this doesn't crash. |
| 311 // See: http://crbug.com/712093. |
| 312 TEST(PaintOpBufferTest, SaveDrawRestore_SingleOpRecordWithSingleNonDrawOp) { |
| 313 sk_sp<PaintRecord> record = sk_make_sp<PaintRecord>(); |
| 314 record->push<NoopOp>(); |
| 315 EXPECT_EQ(record->approximateOpCount(), 1); |
| 316 EXPECT_FALSE(record->GetFirstOp()->IsDrawOp()); |
| 317 |
| 318 PaintOpBuffer buffer; |
| 319 |
| 320 uint8_t alpha = 100; |
| 321 buffer.push<SaveLayerAlphaOp>(nullptr, alpha); |
| 322 buffer.push<DrawRecordOp>(std::move(record)); |
| 323 buffer.push<RestoreOp>(); |
| 324 |
| 325 SaveCountingCanvas canvas; |
| 326 buffer.playback(&canvas); |
| 327 |
| 328 EXPECT_EQ(1, canvas.save_count_); |
| 329 EXPECT_EQ(1, canvas.restore_count_); |
| 330 } |
| 331 |
| 259 } // namespace cc | 332 } // namespace cc |
| OLD | NEW |