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