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 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 8 | #include "DecodeFile.h" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 9 | #include "Sample.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkRegion.h" |
| 15 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 16 | #include "SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkColorPriv.h" |
| 18 | #include "SkColorFilter.h" |
| 19 | #include "SkTime.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkTypeface.h" |
| 21 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 22 | static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | SkPoint pts[2]; |
| 24 | SkColor colors[2]; |
| 25 | |
| 26 | pts[0].set(0, 0); |
| 27 | pts[1].set(0, SkIntToScalar(bm.height())); |
| 28 | colors[0] = SK_ColorBLACK; |
| 29 | colors[1] = SkColorSetARGB(0, 0, 0, 0); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 30 | auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 32 | auto shaderB = SkShader::MakeBitmapShader(bm, |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 33 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 35 | return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA), SkBlendMode::kDstIn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 38 | class ShaderView : public Sample { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | public: |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 40 | sk_sp<SkShader> fShader; |
| 41 | SkBitmap fBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | ShaderView() { |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 44 | decode_file("/skimages/logo.gif", &fBitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | |
| 46 | SkPoint pts[2]; |
| 47 | SkColor colors[2]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | pts[0].set(0, 0); |
| 50 | pts[1].set(SkIntToScalar(100), 0); |
| 51 | colors[0] = SK_ColorRED; |
| 52 | colors[1] = SK_ColorBLUE; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 53 | auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | pts[0].set(0, 0); |
| 56 | pts[1].set(0, SkIntToScalar(100)); |
| 57 | colors[0] = SK_ColorBLACK; |
| 58 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 59 | auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 60 | |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 61 | fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB), |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 62 | SkBlendMode::kDstIn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 66 | bool onQuery(Sample::Event* evt) override { |
| 67 | if (Sample::TitleQ(*evt)) { |
| 68 | Sample::TitleR(evt, "Shaders"); |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | return this->INHERITED::onQuery(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | canvas->drawBitmap(fBitmap, 0, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 76 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 77 | canvas->translate(20, 120); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | SkPaint paint; |
| 80 | SkRect r; |
| 81 | |
| 82 | paint.setColor(SK_ColorGREEN); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 83 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | paint.setShader(fShader); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 85 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
| 87 | canvas->translate(SkIntToScalar(110), 0); |
| 88 | |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 89 | int w = fBitmap.width(); |
| 90 | int h = fBitmap.height(); |
| 91 | w = 120; |
| 92 | h = 80; |
| 93 | r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | paint.setShader(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | canvas->drawRect(r, paint); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 97 | paint.setShader(make_bitmapfade(fBitmap)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | canvas->drawRect(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 102 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | ////////////////////////////////////////////////////////////////////////////// |
| 106 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 107 | DEF_SAMPLE( return new ShaderView(); ) |