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