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