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 | |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkColorFilter.h" |
| 12 | #include "SkImage.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | static SkBitmap make_alpha_image(int w, int h) { |
| 16 | SkBitmap bm; |
| 17 | bm.allocPixels(SkImageInfo::MakeA8(w, h)); |
| 18 | bm.eraseARGB(10, 0, 0 , 0); |
| 19 | for (int y = 0; y < bm.height(); ++y) { |
| 20 | for (int x = y; x < bm.width(); ++x) { |
| 21 | *bm.getAddr8(x, y) = 0xFF; |
| 22 | } |
| 23 | } |
| 24 | bm.setImmutable(); |
| 25 | return bm; |
| 26 | } |
| 27 | |
| 28 | static sk_sp<SkColorFilter> make_color_filter() { |
| 29 | SkScalar colorMatrix[20] = { |
| 30 | 1, 0, 0, 0, 0, |
| 31 | 0, 1, 0, 0, 0, |
| 32 | 0, 0, 0.5, 0.5, 0, |
| 33 | 0, 0, 0.5, 0.5, 0}; // mix G and A. |
| 34 | return SkColorFilter::MakeMatrixFilterRowMajor255(colorMatrix); |
| 35 | } |
| 36 | |
| 37 | DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) { |
| 38 | auto image = SkImage::MakeFromBitmap(make_alpha_image(96, 96)); |
| 39 | SkPaint paint; |
| 40 | |
| 41 | paint.setColorFilter(make_color_filter()); |
| 42 | paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 10.0f, 0)); |
| 43 | canvas->drawImage(image.get(), 16, 16, &paint); |
| 44 | |
| 45 | paint.setColorFilter(nullptr); |
| 46 | paint.setShader(SkShader::MakeColorShader(SK_ColorCYAN)); |
| 47 | canvas->drawImage(image.get(), 144, 16, &paint); |
| 48 | |
| 49 | paint.setColorFilter(make_color_filter()); |
| 50 | canvas->drawImage(image.get(), 16, 144, &paint); |
| 51 | |
| 52 | paint.setMaskFilter(nullptr); |
| 53 | canvas->drawImage(image.get(), 144, 144, &paint); |
| 54 | } |