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" |
| 23 | #include "SkTransparentShader.h" |
| 24 | #include "SkTypeface.h" |
| 25 | |
| 26 | static SkShader* make_bitmapfade(const SkBitmap& bm) |
| 27 | { |
| 28 | SkPoint pts[2]; |
| 29 | SkColor colors[2]; |
| 30 | |
| 31 | pts[0].set(0, 0); |
| 32 | pts[1].set(0, SkIntToScalar(bm.height())); |
| 33 | colors[0] = SK_ColorBLACK; |
| 34 | colors[1] = SkColorSetARGB(0, 0, 0, 0); |
| 35 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
| 36 | |
| 37 | SkShader* shaderB = SkShader::CreateBitmapShader(bm, |
| 38 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 39 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 40 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | |
| 42 | SkShader* shader = new SkComposeShader(shaderB, shaderA, mode); |
| 43 | shaderA->unref(); |
| 44 | shaderB->unref(); |
| 45 | mode->unref(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | return shader; |
| 48 | } |
| 49 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 50 | class ShaderView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | public: |
| 52 | SkShader* fShader; |
| 53 | SkBitmap fBitmap; |
| 54 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 55 | ShaderView() { |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 56 | SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | |
| 58 | SkPoint pts[2]; |
| 59 | SkColor colors[2]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 60 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | pts[0].set(0, 0); |
| 62 | pts[1].set(SkIntToScalar(100), 0); |
| 63 | colors[0] = SK_ColorRED; |
| 64 | colors[1] = SK_ColorBLUE; |
| 65 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
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 | pts[0].set(0, 0); |
| 68 | pts[1].set(0, SkIntToScalar(100)); |
| 69 | colors[0] = SK_ColorBLACK; |
| 70 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
| 71 | SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 72 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 73 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | |
| 75 | fShader = new SkComposeShader(shaderA, shaderB, mode); |
| 76 | shaderA->unref(); |
| 77 | shaderB->unref(); |
| 78 | mode->unref(); |
| 79 | } |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 80 | virtual ~ShaderView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 81 | SkSafeUnref(fShader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | protected: |
| 85 | // overrides from SkEventSink |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 86 | virtual bool onQuery(SkEvent* evt) { |
| 87 | if (SampleCode::TitleQ(*evt)) { |
| 88 | SampleCode::TitleR(evt, "Shaders"); |
| 89 | return true; |
| 90 | } |
| 91 | return this->INHERITED::onQuery(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 93 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 94 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | canvas->drawBitmap(fBitmap, 0, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 96 | |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 97 | canvas->translate(SkIntToScalar(20), SkIntToScalar(120)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | SkPaint paint; |
| 100 | SkRect r; |
| 101 | |
| 102 | paint.setColor(SK_ColorGREEN); |
| 103 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 104 | paint.setShader(fShader); |
| 105 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 106 | |
| 107 | canvas->translate(SkIntToScalar(110), 0); |
| 108 | |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 109 | int w = fBitmap.width(); |
| 110 | int h = fBitmap.height(); |
| 111 | w = 120; |
| 112 | h = 80; |
| 113 | r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | |
| 115 | paint.setShader(NULL); |
| 116 | canvas->drawRect(r, paint); |
| 117 | paint.setShader(make_bitmapfade(fBitmap))->unref(); |
| 118 | canvas->drawRect(r, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | paint.setShader(new SkTransparentShader)->unref(); |
| 121 | canvas->drawRect(r, paint); |
| 122 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 123 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 124 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | this->inval(NULL); |
| 126 | return this->INHERITED::onFindClickHandler(x, y); |
| 127 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 128 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 129 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | return this->INHERITED::onClick(click); |
| 131 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 134 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | ////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | static SkView* MyFactory() { return new ShaderView; } |
| 140 | static SkViewRegister reg(MyFactory); |
| 141 | |