• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 
8 #include "DecodeFile.h"
9 #include "SampleCode.h"
10 #include "SkView.h"
11 #include "SkCanvas.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
14 #include "SkPath.h"
15 #include "SkRegion.h"
16 #include "SkShader.h"
17 #include "SkUtils.h"
18 #include "SkColorPriv.h"
19 #include "SkColorFilter.h"
20 #include "SkTime.h"
21 #include "SkTypeface.h"
22 
make_bitmapfade(const SkBitmap & bm)23 static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
24     SkPoint pts[2];
25     SkColor colors[2];
26 
27     pts[0].set(0, 0);
28     pts[1].set(0, SkIntToScalar(bm.height()));
29     colors[0] = SK_ColorBLACK;
30     colors[1] = SkColorSetARGB(0, 0, 0, 0);
31     auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
32 
33     auto shaderB = SkShader::MakeBitmapShader(bm,
34                                               SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
35 
36     return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA), SkBlendMode::kDstIn);
37 }
38 
39 class ShaderView : public SampleView {
40 public:
41     sk_sp<SkShader> fShader;
42     SkBitmap        fBitmap;
43 
ShaderView()44     ShaderView() {
45         decode_file("/skimages/logo.gif", &fBitmap);
46 
47         SkPoint pts[2];
48         SkColor colors[2];
49 
50         pts[0].set(0, 0);
51         pts[1].set(SkIntToScalar(100), 0);
52         colors[0] = SK_ColorRED;
53         colors[1] = SK_ColorBLUE;
54         auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
55 
56         pts[0].set(0, 0);
57         pts[1].set(0, SkIntToScalar(100));
58         colors[0] = SK_ColorBLACK;
59         colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
60         auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
61 
62         fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
63                                               SkBlendMode::kDstIn);
64     }
65 
66 protected:
67     // overrides from SkEventSink
onQuery(SkEvent * evt)68     bool onQuery(SkEvent* evt) override {
69         if (SampleCode::TitleQ(*evt)) {
70             SampleCode::TitleR(evt, "Shaders");
71             return true;
72         }
73         return this->INHERITED::onQuery(evt);
74     }
75 
onDrawContent(SkCanvas * canvas)76     void onDrawContent(SkCanvas* canvas) override {
77         canvas->drawBitmap(fBitmap, 0, 0);
78 
79         canvas->translate(20, 120);
80 
81         SkPaint paint;
82         SkRect  r;
83 
84         paint.setColor(SK_ColorGREEN);
85         canvas->drawRect(SkRect::MakeWH(100, 100), paint);
86         paint.setShader(fShader);
87         canvas->drawRect(SkRect::MakeWH(100, 100), paint);
88 
89         canvas->translate(SkIntToScalar(110), 0);
90 
91         int w = fBitmap.width();
92         int h = fBitmap.height();
93         w = 120;
94         h = 80;
95         r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
96 
97         paint.setShader(nullptr);
98         canvas->drawRect(r, paint);
99         paint.setShader(make_bitmapfade(fBitmap));
100         canvas->drawRect(r, paint);
101     }
102 
103 private:
104     typedef SampleView INHERITED;
105 };
106 
107 //////////////////////////////////////////////////////////////////////////////
108 
MyFactory()109 static SkView* MyFactory() { return new ShaderView; }
110 static SkViewRegister reg(MyFactory);
111