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  */
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"
15 #include "SkRegion.h"
16 #include "SkShader.h"
17 #include "SkUtils.h"
18 #include "SkXfermode.h"
19 #include "SkComposeShader.h"
20 #include "SkColorPriv.h"
21 #include "SkColorFilter.h"
22 #include "SkTime.h"
23 #include "SkTransparentShader.h"
24 #include "SkTypeface.h"
25 
make_bitmapfade(const SkBitmap & bm)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 
40     SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
41 
42     SkShader* shader = new SkComposeShader(shaderB, shaderA, mode);
43     shaderA->unref();
44     shaderB->unref();
45     mode->unref();
46 
47     return shader;
48 }
49 
50 class ShaderView : public SampleView {
51 public:
52     SkShader*   fShader;
53     SkBitmap    fBitmap;
54 
ShaderView()55     ShaderView() {
56         SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap);
57 
58         SkPoint pts[2];
59         SkColor colors[2];
60 
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);
66 
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);
72 
73         SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
74 
75         fShader = new SkComposeShader(shaderA, shaderB, mode);
76         shaderA->unref();
77         shaderB->unref();
78         mode->unref();
79     }
~ShaderView()80     virtual ~ShaderView() {
81         SkSafeUnref(fShader);
82     }
83 
84 protected:
85     // overrides from SkEventSink
onQuery(SkEvent * evt)86     bool onQuery(SkEvent* evt) override {
87         if (SampleCode::TitleQ(*evt)) {
88             SampleCode::TitleR(evt, "Shaders");
89             return true;
90         }
91         return this->INHERITED::onQuery(evt);
92     }
93 
onDrawContent(SkCanvas * canvas)94     void onDrawContent(SkCanvas* canvas) override {
95         canvas->drawBitmap(fBitmap, 0, 0);
96 
97         canvas->translate(SkIntToScalar(20), SkIntToScalar(120));
98 
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 
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));
114 
115         paint.setShader(NULL);
116         canvas->drawRect(r, paint);
117         paint.setShader(make_bitmapfade(fBitmap))->unref();
118         canvas->drawRect(r, paint);
119 
120         paint.setShader(new SkTransparentShader)->unref();
121         canvas->drawRect(r, paint);
122     }
123 
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)124     virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
125                                               unsigned modi) override {
126         this->inval(NULL);
127         return this->INHERITED::onFindClickHandler(x, y, modi);
128     }
129 
onClick(Click * click)130     bool onClick(Click* click)  override {
131         return this->INHERITED::onClick(click);
132     }
133 
134 private:
135     typedef SampleView INHERITED;
136 };
137 
138 //////////////////////////////////////////////////////////////////////////////
139 
MyFactory()140 static SkView* MyFactory() { return new ShaderView; }
141 static SkViewRegister reg(MyFactory);
142