• 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 "include/core/SkCanvas.h"
9 #include "include/core/SkShader.h"
10 #include "include/effects/SkGradientShader.h"
11 #include "samplecode/DecodeFile.h"
12 #include "samplecode/Sample.h"
13 #include "tools/Resources.h"
14 
15 namespace {
make_bitmapfade(const SkBitmap & bm)16 static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
17     SkPoint pts[2] = {
18         {0, 0},
19         {0, (float)bm.height()},
20     };
21     SkColor colors[2] = {
22         SkColorSetARGB(255, 0, 0, 0),
23         SkColorSetARGB(0,   0, 0, 0),
24     };
25     return SkShaders::Blend(SkBlendMode::kDstIn,
26                             bm.makeShader(),
27                             SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
28                                                          SkTileMode::kClamp));
29 }
30 
make_blend_shader()31 static sk_sp<SkShader> make_blend_shader() {
32     SkPoint pts[2];
33     SkColor colors[2];
34 
35     pts[0].set(0, 0);
36     pts[1].set(SkIntToScalar(100), 0);
37     colors[0] = SK_ColorRED;
38     colors[1] = SK_ColorBLUE;
39     auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
40 
41     pts[0].set(0, 0);
42     pts[1].set(0, SkIntToScalar(100));
43     colors[0] = SK_ColorBLACK;
44     colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
45     auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
46 
47     return SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderA), std::move(shaderB));
48 }
49 
50 struct ShaderView : public Sample {
51     sk_sp<SkShader> fShader;
52     sk_sp<SkShader> fShaderFade;
53     SkBitmap        fBitmap;
54 
onOnceBeforeDraw__anon75dc86790111::ShaderView55     void onOnceBeforeDraw() override {
56         decode_file(GetResourceAsData("images/dog.jpg"), &fBitmap);
57         fShader = make_blend_shader();
58         fShaderFade = make_bitmapfade(fBitmap);
59     }
60 
name__anon75dc86790111::ShaderView61     SkString name() override { return SkString("Shaders"); }
62 
onDrawContent__anon75dc86790111::ShaderView63     void onDrawContent(SkCanvas* canvas) override {
64         canvas->drawBitmap(fBitmap, 0, 0);
65         canvas->translate(20, 120);
66 
67         SkPaint paint;
68         paint.setColor(SK_ColorGREEN);
69         canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
70         paint.setShader(fShader);
71         canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
72 
73         canvas->translate(SkIntToScalar(110), 0);
74 
75         paint.setShader(nullptr);
76         canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
77         paint.setShader(fShaderFade);
78         canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
79     }
80 };
81 }  // namespace
82 DEF_SAMPLE( return new ShaderView(); )
83