blob: 7ca74ccdb2c1a8f15b2e8e851495dbad16bf88c4 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SampleCode.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkColorPriv.h"
19#include "SkColorFilter.h"
20#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkTypeface.h"
22
reed8a21c9f2016-03-08 18:50:00 -080023static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 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);
reed8a21c9f2016-03-08 18:50:00 -080031 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
reed8a21c9f2016-03-08 18:50:00 -080033 auto shaderB = SkShader::MakeBitmapShader(bm,
reedcfb6bdf2016-03-29 11:32:50 -070034 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
Mike Reed7d954ad2016-10-28 15:42:34 -040036 return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA), SkBlendMode::kDstIn);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037}
38
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000039class ShaderView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040public:
reed8a21c9f2016-03-08 18:50:00 -080041 sk_sp<SkShader> fShader;
42 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000043
rmistry@google.comae933ce2012-08-23 18:19:56 +000044 ShaderView() {
msarettd15750c2016-03-18 15:48:49 -070045 decode_file("/skimages/logo.gif", &fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046
47 SkPoint pts[2];
48 SkColor colors[2];
reed@google.com82065d62011-02-07 15:30:46 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 pts[0].set(0, 0);
51 pts[1].set(SkIntToScalar(100), 0);
52 colors[0] = SK_ColorRED;
53 colors[1] = SK_ColorBLUE;
reed8a21c9f2016-03-08 18:50:00 -080054 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 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);
reed8a21c9f2016-03-08 18:50:00 -080060 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000061
reedcfb6bdf2016-03-29 11:32:50 -070062 fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
Mike Reed7d954ad2016-10-28 15:42:34 -040063 SkBlendMode::kDstIn);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 }
reed@google.com82065d62011-02-07 15:30:46 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066protected:
67 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070068 bool onQuery(SkEvent* evt) override {
reed@android.com44a63122009-05-30 02:40:28 +000069 if (SampleCode::TitleQ(*evt)) {
70 SampleCode::TitleR(evt, "Shaders");
71 return true;
72 }
73 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 }
reed@google.com82065d62011-02-07 15:30:46 +000075
mtklein36352bf2015-03-25 18:17:31 -070076 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 canvas->drawBitmap(fBitmap, 0, 0);
reed@google.com82065d62011-02-07 15:30:46 +000078
Mike Reed3661bc92017-02-22 13:21:42 -050079 canvas->translate(20, 120);
reed@google.com82065d62011-02-07 15:30:46 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkPaint paint;
82 SkRect r;
83
84 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050085 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 paint.setShader(fShader);
Mike Reed3661bc92017-02-22 13:21:42 -050087 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
89 canvas->translate(SkIntToScalar(110), 0);
90
reed@android.com44a63122009-05-30 02:40:28 +000091 int w = fBitmap.width();
92 int h = fBitmap.height();
93 w = 120;
94 h = 80;
95 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
reed@android.com8a1c16f2008-12-17 15:59:43 +000096
halcanary96fcdcc2015-08-27 07:41:13 -070097 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 canvas->drawRect(r, paint);
reed8a21c9f2016-03-08 18:50:00 -080099 paint.setShader(make_bitmapfade(fBitmap));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 canvas->drawRect(r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 }
reed@google.com82065d62011-02-07 15:30:46 +0000102
reed56263c72015-06-05 11:31:26 -0700103 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000105 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 }
reed@google.com82065d62011-02-07 15:30:46 +0000107
mtklein36352bf2015-03-25 18:17:31 -0700108 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 return this->INHERITED::onClick(click);
110 }
reed@google.com82065d62011-02-07 15:30:46 +0000111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000113 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114};
115
116//////////////////////////////////////////////////////////////////////////////
117
118static SkView* MyFactory() { return new ShaderView; }
119static SkViewRegister reg(MyFactory);