epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkGraphics.h" |
| 13 | #include "SkImageDecoder.h" |
| 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 | da342a8 | 2008-12-23 18:37:08 +0000 | [diff] [blame] | 19 | #include "SkComposeShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkTime.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkTypeface.h" |
| 24 | |
| 25 | static SkShader* make_bitmapfade(const SkBitmap& bm) |
| 26 | { |
| 27 | SkPoint pts[2]; |
| 28 | SkColor colors[2]; |
| 29 | |
| 30 | pts[0].set(0, 0); |
| 31 | pts[1].set(0, SkIntToScalar(bm.height())); |
| 32 | colors[0] = SK_ColorBLACK; |
| 33 | colors[1] = SkColorSetARGB(0, 0, 0, 0); |
| 34 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
| 35 | |
| 36 | SkShader* shaderB = SkShader::CreateBitmapShader(bm, |
| 37 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 38 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 39 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
| 41 | SkShader* shader = new SkComposeShader(shaderB, shaderA, mode); |
| 42 | shaderA->unref(); |
| 43 | shaderB->unref(); |
| 44 | mode->unref(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 45 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | return shader; |
| 47 | } |
| 48 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 49 | class ShaderView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | public: |
| 51 | SkShader* fShader; |
| 52 | SkBitmap fBitmap; |
| 53 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 54 | ShaderView() { |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 55 | SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
| 57 | SkPoint pts[2]; |
| 58 | SkColor colors[2]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 59 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | pts[0].set(0, 0); |
| 61 | pts[1].set(SkIntToScalar(100), 0); |
| 62 | colors[0] = SK_ColorRED; |
| 63 | colors[1] = SK_ColorBLUE; |
| 64 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
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 | pts[0].set(0, 0); |
| 67 | pts[1].set(0, SkIntToScalar(100)); |
| 68 | colors[0] = SK_ColorBLACK; |
| 69 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
| 70 | SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 71 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 72 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | |
| 74 | fShader = new SkComposeShader(shaderA, shaderB, mode); |
| 75 | shaderA->unref(); |
| 76 | shaderB->unref(); |
| 77 | mode->unref(); |
| 78 | } |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 79 | virtual ~ShaderView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 80 | SkSafeUnref(fShader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | } |
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 | protected: |
| 84 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | bool onQuery(SkEvent* evt) override { |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 86 | if (SampleCode::TitleQ(*evt)) { |
| 87 | SampleCode::TitleR(evt, "Shaders"); |
| 88 | return true; |
| 89 | } |
| 90 | return this->INHERITED::onQuery(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 92 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | canvas->drawBitmap(fBitmap, 0, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 95 | |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 96 | canvas->translate(SkIntToScalar(20), SkIntToScalar(120)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | SkPaint paint; |
| 99 | SkRect r; |
| 100 | |
| 101 | paint.setColor(SK_ColorGREEN); |
| 102 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 103 | paint.setShader(fShader); |
| 104 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 105 | |
| 106 | canvas->translate(SkIntToScalar(110), 0); |
| 107 | |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 108 | int w = fBitmap.width(); |
| 109 | int h = fBitmap.height(); |
| 110 | w = 120; |
| 111 | h = 80; |
| 112 | r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | |
| 114 | paint.setShader(NULL); |
| 115 | canvas->drawRect(r, paint); |
| 116 | paint.setShader(make_bitmapfade(fBitmap))->unref(); |
| 117 | canvas->drawRect(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 119 | |
reed | 56263c7 | 2015-06-05 11:31:26 -0700 | [diff] [blame] | 120 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 122 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 124 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 125 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | return this->INHERITED::onClick(click); |
| 127 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 130 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | ////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | static SkView* MyFactory() { return new ShaderView; } |
| 136 | static SkViewRegister reg(MyFactory); |