blob: fb3fec98d865bf3569d90ce44ab5b841a3c68252 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed8a21c9f2016-03-08 18:50:00 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/effects/SkGradientShader.h"
11#include "samplecode/DecodeFile.h"
12#include "samplecode/Sample.h"
Mike Klein52337de2019-07-25 09:00:52 -050013#include "tools/Resources.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
Hal Canary352f0912019-07-05 10:42:56 -040015namespace {
reed8a21c9f2016-03-08 18:50:00 -080016static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
Hal Canary352f0912019-07-05 10:42:56 -040017 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
31static sk_sp<SkShader> make_blend_shader() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 SkPoint pts[2];
33 SkColor colors[2];
34
35 pts[0].set(0, 0);
Hal Canary352f0912019-07-05 10:42:56 -040036 pts[1].set(SkIntToScalar(100), 0);
37 colors[0] = SK_ColorRED;
38 colors[1] = SK_ColorBLUE;
Mike Reedfae8fce2019-04-03 10:27:45 -040039 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
Hal Canary352f0912019-07-05 10:42:56 -040041 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);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046
Hal Canary352f0912019-07-05 10:42:56 -040047 return SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderA), std::move(shaderB));
reed@android.com8a1c16f2008-12-17 15:59:43 +000048}
49
Hal Canary352f0912019-07-05 10:42:56 -040050struct ShaderView : public Sample {
reed8a21c9f2016-03-08 18:50:00 -080051 sk_sp<SkShader> fShader;
Hal Canary352f0912019-07-05 10:42:56 -040052 sk_sp<SkShader> fShaderFade;
reed8a21c9f2016-03-08 18:50:00 -080053 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000054
Hal Canary352f0912019-07-05 10:42:56 -040055 void onOnceBeforeDraw() override {
56 decode_file(GetResourceAsData("images/dog.jpg"), &fBitmap);
57 fShader = make_blend_shader();
58 fShaderFade = make_bitmapfade(fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 }
reed@google.com82065d62011-02-07 15:30:46 +000060
Hal Canary8a027312019-07-03 10:55:44 -040061 SkString name() override { return SkString("Shaders"); }
reed@google.com82065d62011-02-07 15:30:46 +000062
mtklein36352bf2015-03-25 18:17:31 -070063 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 canvas->drawBitmap(fBitmap, 0, 0);
Mike Reed3661bc92017-02-22 13:21:42 -050065 canvas->translate(20, 120);
reed@google.com82065d62011-02-07 15:30:46 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 paint.setColor(SK_ColorGREEN);
Hal Canary352f0912019-07-05 10:42:56 -040069 canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 paint.setShader(fShader);
Hal Canary352f0912019-07-05 10:42:56 -040071 canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73 canvas->translate(SkIntToScalar(110), 0);
74
halcanary96fcdcc2015-08-27 07:41:13 -070075 paint.setShader(nullptr);
Hal Canary352f0912019-07-05 10:42:56 -040076 canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
77 paint.setShader(fShaderFade);
78 canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080};
Hal Canary352f0912019-07-05 10:42:56 -040081} // namespace
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040082DEF_SAMPLE( return new ShaderView(); )