epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/effects/SkGradientShader.h" |
| 11 | #include "samplecode/DecodeFile.h" |
| 12 | #include "samplecode/Sample.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 13 | #include "tools/Resources.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 15 | namespace { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 16 | static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) { |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 17 | SkPoint pts[2] = { |
| 18 | {0, 0}, |
| 19 | {0, (float)bm.height()}, |
| 20 | }; |
| 21 | SkColor colors[2] = { |
| 22 | SkColorSetARGB(255, 0, 0, 0), |
| 23 | SkColorSetARGB(0, 0, 0, 0), |
| 24 | }; |
| 25 | return SkShaders::Blend(SkBlendMode::kDstIn, |
| 26 | bm.makeShader(), |
| 27 | SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 28 | SkTileMode::kClamp)); |
| 29 | } |
| 30 | |
| 31 | static sk_sp<SkShader> make_blend_shader() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | SkPoint pts[2]; |
| 33 | SkColor colors[2]; |
| 34 | |
| 35 | pts[0].set(0, 0); |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 36 | pts[1].set(SkIntToScalar(100), 0); |
| 37 | colors[0] = SK_ColorRED; |
| 38 | colors[1] = SK_ColorBLUE; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 39 | auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 41 | pts[0].set(0, 0); |
| 42 | pts[1].set(0, SkIntToScalar(100)); |
| 43 | colors[0] = SK_ColorBLACK; |
| 44 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
| 45 | auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 47 | return SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderA), std::move(shaderB)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 50 | struct ShaderView : public Sample { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 51 | sk_sp<SkShader> fShader; |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 52 | sk_sp<SkShader> fShaderFade; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 53 | SkBitmap fBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 55 | void onOnceBeforeDraw() override { |
| 56 | decode_file(GetResourceAsData("images/dog.jpg"), &fBitmap); |
| 57 | fShader = make_blend_shader(); |
| 58 | fShaderFade = make_bitmapfade(fBitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 60 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 61 | SkString name() override { return SkString("Shaders"); } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | canvas->drawBitmap(fBitmap, 0, 0); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 65 | canvas->translate(20, 120); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 66 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | paint.setColor(SK_ColorGREEN); |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 69 | canvas->drawRect(SkRect{0, 0, 100, 100}, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | paint.setShader(fShader); |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 71 | canvas->drawRect(SkRect{0, 0, 100, 100}, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | |
| 73 | canvas->translate(SkIntToScalar(110), 0); |
| 74 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | paint.setShader(nullptr); |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 76 | canvas->drawRect(SkRect{0, 0, 120, 80}, paint); |
| 77 | paint.setShader(fShaderFade); |
| 78 | canvas->drawRect(SkRect{0, 0, 120, 80}, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | }; |
Hal Canary | 352f091 | 2019-07-05 10:42:56 -0400 | [diff] [blame] | 81 | } // namespace |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 82 | DEF_SAMPLE( return new ShaderView(); ) |