blob: bf6488898df8080f0e2070b9438ca221e355eb23 [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
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04009#include "Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkRegion.h"
15#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040016#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkColorPriv.h"
18#include "SkColorFilter.h"
19#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkTypeface.h"
21
reed8a21c9f2016-03-08 18:50:00 -080022static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 SkPoint pts[2];
24 SkColor colors[2];
25
26 pts[0].set(0, 0);
27 pts[1].set(0, SkIntToScalar(bm.height()));
28 colors[0] = SK_ColorBLACK;
29 colors[1] = SkColorSetARGB(0, 0, 0, 0);
reed8a21c9f2016-03-08 18:50:00 -080030 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000031
reed8a21c9f2016-03-08 18:50:00 -080032 auto shaderB = SkShader::MakeBitmapShader(bm,
reedcfb6bdf2016-03-29 11:32:50 -070033 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
Mike Reed7d954ad2016-10-28 15:42:34 -040035 return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA), SkBlendMode::kDstIn);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036}
37
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040038class ShaderView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039public:
reed8a21c9f2016-03-08 18:50:00 -080040 sk_sp<SkShader> fShader;
41 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000042
rmistry@google.comae933ce2012-08-23 18:19:56 +000043 ShaderView() {
msarettd15750c2016-03-18 15:48:49 -070044 decode_file("/skimages/logo.gif", &fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
46 SkPoint pts[2];
47 SkColor colors[2];
reed@google.com82065d62011-02-07 15:30:46 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 pts[0].set(0, 0);
50 pts[1].set(SkIntToScalar(100), 0);
51 colors[0] = SK_ColorRED;
52 colors[1] = SK_ColorBLUE;
reed8a21c9f2016-03-08 18:50:00 -080053 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 pts[0].set(0, 0);
56 pts[1].set(0, SkIntToScalar(100));
57 colors[0] = SK_ColorBLACK;
58 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
reed8a21c9f2016-03-08 18:50:00 -080059 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000060
reedcfb6bdf2016-03-29 11:32:50 -070061 fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
Mike Reed7d954ad2016-10-28 15:42:34 -040062 SkBlendMode::kDstIn);
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 }
reed@google.com82065d62011-02-07 15:30:46 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040066 bool onQuery(Sample::Event* evt) override {
67 if (Sample::TitleQ(*evt)) {
68 Sample::TitleR(evt, "Shaders");
reed@android.com44a63122009-05-30 02:40:28 +000069 return true;
70 }
71 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 }
reed@google.com82065d62011-02-07 15:30:46 +000073
mtklein36352bf2015-03-25 18:17:31 -070074 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 canvas->drawBitmap(fBitmap, 0, 0);
reed@google.com82065d62011-02-07 15:30:46 +000076
Mike Reed3661bc92017-02-22 13:21:42 -050077 canvas->translate(20, 120);
reed@google.com82065d62011-02-07 15:30:46 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkPaint paint;
80 SkRect r;
81
82 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050083 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 paint.setShader(fShader);
Mike Reed3661bc92017-02-22 13:21:42 -050085 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
87 canvas->translate(SkIntToScalar(110), 0);
88
reed@android.com44a63122009-05-30 02:40:28 +000089 int w = fBitmap.width();
90 int h = fBitmap.height();
91 w = 120;
92 h = 80;
93 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
halcanary96fcdcc2015-08-27 07:41:13 -070095 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 canvas->drawRect(r, paint);
reed8a21c9f2016-03-08 18:50:00 -080097 paint.setShader(make_bitmapfade(fBitmap));
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 canvas->drawRect(r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 }
reed@google.com82065d62011-02-07 15:30:46 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400102 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103};
104
105//////////////////////////////////////////////////////////////////////////////
106
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400107DEF_SAMPLE( return new ShaderView(); )