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" |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 20 | |
| 21 | static SkBitmap make_alpha_image(int w, int h) { |
| 22 | SkBitmap bm; |
| 23 | bm.allocPixels(SkImageInfo::MakeA8(w, h)); |
| 24 | bm.eraseARGB(10, 0, 0 , 0); |
| 25 | for (int y = 0; y < bm.height(); ++y) { |
| 26 | for (int x = y; x < bm.width(); ++x) { |
| 27 | *bm.getAddr8(x, y) = 0xFF; |
| 28 | } |
| 29 | } |
| 30 | bm.setImmutable(); |
| 31 | return bm; |
| 32 | } |
| 33 | |
| 34 | static sk_sp<SkColorFilter> make_color_filter() { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 35 | float colorMatrix[20] = { |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 36 | 1, 0, 0, 0, 0, |
| 37 | 0, 1, 0, 0, 0, |
| 38 | 0, 0, 0.5, 0.5, 0, |
| 39 | 0, 0, 0.5, 0.5, 0}; // mix G and A. |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 40 | return SkColorFilters::Matrix(colorMatrix); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) { |
| 44 | auto image = SkImage::MakeFromBitmap(make_alpha_image(96, 96)); |
| 45 | SkPaint paint; |
| 46 | |
| 47 | paint.setColorFilter(make_color_filter()); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 48 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f)); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 49 | canvas->drawImage(image.get(), 16, 16, &paint); |
| 50 | |
| 51 | paint.setColorFilter(nullptr); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 52 | paint.setShader(SkShaders::Color(SK_ColorCYAN)); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 53 | canvas->drawImage(image.get(), 144, 16, &paint); |
| 54 | |
| 55 | paint.setColorFilter(make_color_filter()); |
| 56 | canvas->drawImage(image.get(), 16, 144, &paint); |
| 57 | |
| 58 | paint.setMaskFilter(nullptr); |
| 59 | canvas->drawImage(image.get(), 144, 144, &paint); |
| 60 | } |
Brian Osman | e7809c7 | 2020-07-30 16:49:54 -0400 | [diff] [blame] | 61 | |
| 62 | // Created to demonstrate skbug.com/10556 - GPU backend was failing to apply paint alpha to |
| 63 | // alpha-only image shaders. The two boxes should look the same. |
| 64 | DEF_SIMPLE_GM(alpha_image_alpha_tint, canvas, 152, 80) { |
| 65 | canvas->clear(SK_ColorGRAY); |
| 66 | |
| 67 | SkBitmap bm; |
| 68 | bm.allocPixels(SkImageInfo::MakeA8(64, 64)); |
| 69 | for (int y = 0; y < bm.height(); ++y) { |
| 70 | for (int x = 0; x < bm.width(); ++x) { |
| 71 | *bm.getAddr8(x, y) = y * 4; |
| 72 | } |
| 73 | } |
| 74 | bm.setImmutable(); |
| 75 | auto image = SkImage::MakeFromBitmap(bm); |
| 76 | |
| 77 | SkPaint paint; |
| 78 | paint.setColor4f({ 0, 1, 0, 0.5f }); |
| 79 | |
| 80 | canvas->translate(8, 8); |
| 81 | canvas->drawImage(image.get(), 0, 0, &paint); |
| 82 | |
| 83 | canvas->translate(72, 0); |
| 84 | paint.setShader(image->makeShader()); |
| 85 | canvas->drawRect({ 0, 0, 64, 64 }, paint); |
| 86 | } |