Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkBlurTypes.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkColorFilter.h" |
| 14 | #include "include/core/SkImage.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 15 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkRefCnt.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkShader.h" |
Kevin Lubick | 8b74188 | 2023-10-06 11:41:38 -0400 | [diff] [blame] | 20 | #include "tools/DecodeUtils.h" |
Brian Osman | 5589c88 | 2022-07-11 11:08:11 -0400 | [diff] [blame] | 21 | #include "tools/Resources.h" |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 22 | |
| 23 | static SkBitmap make_alpha_image(int w, int h) { |
| 24 | SkBitmap bm; |
| 25 | bm.allocPixels(SkImageInfo::MakeA8(w, h)); |
| 26 | bm.eraseARGB(10, 0, 0 , 0); |
| 27 | for (int y = 0; y < bm.height(); ++y) { |
| 28 | for (int x = y; x < bm.width(); ++x) { |
| 29 | *bm.getAddr8(x, y) = 0xFF; |
| 30 | } |
| 31 | } |
| 32 | bm.setImmutable(); |
| 33 | return bm; |
| 34 | } |
| 35 | |
| 36 | static sk_sp<SkColorFilter> make_color_filter() { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 37 | float colorMatrix[20] = { |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 38 | 1, 0, 0, 0, 0, |
| 39 | 0, 1, 0, 0, 0, |
| 40 | 0, 0, 0.5, 0.5, 0, |
| 41 | 0, 0, 0.5, 0.5, 0}; // mix G and A. |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 42 | return SkColorFilters::Matrix(colorMatrix); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) { |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 46 | auto image = make_alpha_image(96, 96).asImage(); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 47 | SkPaint paint; |
| 48 | |
| 49 | paint.setColorFilter(make_color_filter()); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 50 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f)); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 51 | canvas->drawImage(image.get(), 16, 16, SkSamplingOptions(), &paint); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 52 | |
| 53 | paint.setColorFilter(nullptr); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 54 | paint.setShader(SkShaders::Color(SK_ColorCYAN)); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 55 | canvas->drawImage(image.get(), 144, 16, SkSamplingOptions(), &paint); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 56 | |
| 57 | paint.setColorFilter(make_color_filter()); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 58 | canvas->drawImage(image.get(), 16, 144, SkSamplingOptions(), &paint); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 59 | |
| 60 | paint.setMaskFilter(nullptr); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 61 | canvas->drawImage(image.get(), 144, 144, SkSamplingOptions(), &paint); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 62 | } |
Brian Osman | e7809c7 | 2020-07-30 16:49:54 -0400 | [diff] [blame] | 63 | |
| 64 | // Created to demonstrate skbug.com/10556 - GPU backend was failing to apply paint alpha to |
| 65 | // alpha-only image shaders. The two boxes should look the same. |
| 66 | DEF_SIMPLE_GM(alpha_image_alpha_tint, canvas, 152, 80) { |
| 67 | canvas->clear(SK_ColorGRAY); |
| 68 | |
| 69 | SkBitmap bm; |
| 70 | bm.allocPixels(SkImageInfo::MakeA8(64, 64)); |
| 71 | for (int y = 0; y < bm.height(); ++y) { |
| 72 | for (int x = 0; x < bm.width(); ++x) { |
| 73 | *bm.getAddr8(x, y) = y * 4; |
| 74 | } |
| 75 | } |
| 76 | bm.setImmutable(); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 77 | auto image = bm.asImage(); |
Brian Osman | e7809c7 | 2020-07-30 16:49:54 -0400 | [diff] [blame] | 78 | |
| 79 | SkPaint paint; |
| 80 | paint.setColor4f({ 0, 1, 0, 0.5f }); |
| 81 | |
| 82 | canvas->translate(8, 8); |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 83 | canvas->drawImage(image.get(), 0, 0, SkSamplingOptions(), &paint); |
Brian Osman | e7809c7 | 2020-07-30 16:49:54 -0400 | [diff] [blame] | 84 | |
| 85 | canvas->translate(72, 0); |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 86 | paint.setShader(image->makeShader(SkSamplingOptions())); |
Brian Osman | e7809c7 | 2020-07-30 16:49:54 -0400 | [diff] [blame] | 87 | canvas->drawRect({ 0, 0, 64, 64 }, paint); |
| 88 | } |
Brian Osman | 5589c88 | 2022-07-11 11:08:11 -0400 | [diff] [blame] | 89 | |
| 90 | #if defined(SK_SUPPORT_LEGACY_ALPHA_BITMAP_AS_COVERAGE) |
| 91 | // For a long time, the CPU backend treated A8 bitmaps as coverage, rather than alpha. This was |
| 92 | // inconsistent with the GPU backend (skbug.com/9692). When this was fixed, it altered behavior |
| 93 | // for some Android apps (b/231400686). This GM verifies that our Android framework workaround |
| 94 | // produces the old result (mandrill with a round-rect border). |
| 95 | DEF_SIMPLE_GM(alpha_bitmap_is_coverage_ANDROID, canvas, 128, 128) { |
| 96 | SkBitmap maskBitmap; |
| 97 | maskBitmap.allocPixels(SkImageInfo::MakeA8(128, 128)); |
| 98 | { |
| 99 | SkCanvas maskCanvas(maskBitmap); |
| 100 | maskCanvas.clear(SK_ColorWHITE); |
| 101 | |
| 102 | SkPaint maskPaint; |
| 103 | maskPaint.setAntiAlias(true); |
| 104 | maskPaint.setColor(SK_ColorWHITE); |
| 105 | maskPaint.setBlendMode(SkBlendMode::kClear); |
| 106 | maskCanvas.drawRoundRect({0, 0, 128, 128}, 16, 16, maskPaint); |
| 107 | } |
| 108 | |
| 109 | SkBitmap offscreenBitmap; |
| 110 | offscreenBitmap.allocN32Pixels(128, 128); |
| 111 | { |
| 112 | SkCanvas offscreenCanvas(offscreenBitmap); |
Kevin Lubick | 8b74188 | 2023-10-06 11:41:38 -0400 | [diff] [blame] | 113 | offscreenCanvas.drawImage(ToolUtils::GetResourceAsImage("images/mandrill_128.png"), 0, 0); |
Brian Osman | 5589c88 | 2022-07-11 11:08:11 -0400 | [diff] [blame] | 114 | |
| 115 | SkPaint clearPaint; |
| 116 | clearPaint.setAntiAlias(true); |
| 117 | clearPaint.setBlendMode(SkBlendMode::kClear); |
| 118 | // At tip-of-tree (or at any time on the GPU backend), this draw produces full coverage, |
| 119 | // completely erasing the mandrill. With the workaround enabled, the alpha border is treated |
| 120 | // as coverage, so we only apply kClear to those pixels, just erasing the outer border. |
| 121 | offscreenCanvas.drawImage(maskBitmap.asImage(), 0, 0, SkSamplingOptions{}, &clearPaint); |
| 122 | } |
| 123 | |
| 124 | canvas->drawImage(offscreenBitmap.asImage(), 0, 0); |
| 125 | } |
| 126 | #endif |