halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 7 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 9 | #include "SkPDFDocument.h" |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 10 | #include "SkStream.h" |
| 11 | #include "Test.h" |
| 12 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 13 | static void run_test(SkWStream* out, SkBlendMode mode, U8CPU alpha) { |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 14 | auto pdfDoc = SkPDF::MakeDocument(out); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 15 | SkCanvas* c = pdfDoc->beginPage(612.0f, 792.0f); |
| 16 | SkPaint black; |
| 17 | SkPaint background; |
| 18 | background.setColor(SK_ColorWHITE); |
| 19 | background.setAlpha(alpha); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 20 | background.setBlendMode(mode); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 21 | c->drawRect(SkRect::MakeWH(612.0f, 792.0f), background); |
| 22 | c->drawRect(SkRect::MakeXYWH(36.0f, 36.0f, 9.0f, 9.0f), black); |
| 23 | c->drawRect(SkRect::MakeXYWH(72.0f, 72.0f, 468.0f, 648.0f), background); |
| 24 | c->drawRect(SkRect::MakeXYWH(108.0f, 108.0f, 9.0f, 9.0f), black); |
| 25 | pdfDoc->close(); |
| 26 | } |
| 27 | |
| 28 | // http://crbug.com/473572 |
| 29 | DEF_TEST(SkPDF_OpaqueSrcModeToSrcOver, r) { |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 30 | REQUIRE_PDF_DOCUMENT(SkPDF_OpaqueSrcModeToSrcOver, r); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 31 | SkDynamicMemoryWStream srcMode; |
| 32 | SkDynamicMemoryWStream srcOverMode; |
| 33 | |
| 34 | U8CPU alpha = SK_AlphaOPAQUE; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 35 | run_test(&srcMode, SkBlendMode::kSrc, alpha); |
| 36 | run_test(&srcOverMode, SkBlendMode::kSrcOver, alpha); |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 37 | REPORTER_ASSERT(r, srcMode.bytesWritten() == srcOverMode.bytesWritten()); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 38 | // The two PDFs should be equal because they have an opaque alpha. |
| 39 | |
| 40 | srcMode.reset(); |
| 41 | srcOverMode.reset(); |
| 42 | |
| 43 | alpha = 0x80; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 44 | run_test(&srcMode, SkBlendMode::kSrc, alpha); |
| 45 | run_test(&srcOverMode, SkBlendMode::kSrcOver, alpha); |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 46 | REPORTER_ASSERT(r, srcMode.bytesWritten() > srcOverMode.bytesWritten()); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 47 | // The two PDFs should not be equal because they have a non-opaque alpha. |
| 48 | } |