blob: 608199da4419b81a2c8a9dc7f806407090d7b9c5 [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"
18#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkTypeface.h"
23
reed8a21c9f2016-03-08 18:50:00 -080024static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 SkPoint pts[2];
26 SkColor colors[2];
27
28 pts[0].set(0, 0);
29 pts[1].set(0, SkIntToScalar(bm.height()));
30 colors[0] = SK_ColorBLACK;
31 colors[1] = SkColorSetARGB(0, 0, 0, 0);
reed8a21c9f2016-03-08 18:50:00 -080032 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
reed8a21c9f2016-03-08 18:50:00 -080034 auto shaderB = SkShader::MakeBitmapShader(bm,
reedcfb6bdf2016-03-29 11:32:50 -070035 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
reedcfb6bdf2016-03-29 11:32:50 -070037 return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA),
38 SkXfermode::Make(SkXfermode::kDstIn_Mode));
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
40
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000041class ShaderView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042public:
reed8a21c9f2016-03-08 18:50:00 -080043 sk_sp<SkShader> fShader;
44 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
rmistry@google.comae933ce2012-08-23 18:19:56 +000046 ShaderView() {
msarettd15750c2016-03-18 15:48:49 -070047 decode_file("/skimages/logo.gif", &fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
49 SkPoint pts[2];
50 SkColor colors[2];
reed@google.com82065d62011-02-07 15:30:46 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 pts[0].set(0, 0);
53 pts[1].set(SkIntToScalar(100), 0);
54 colors[0] = SK_ColorRED;
55 colors[1] = SK_ColorBLUE;
reed8a21c9f2016-03-08 18:50:00 -080056 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 pts[0].set(0, 0);
59 pts[1].set(0, SkIntToScalar(100));
60 colors[0] = SK_ColorBLACK;
61 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
reed8a21c9f2016-03-08 18:50:00 -080062 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000063
reedcfb6bdf2016-03-29 11:32:50 -070064 fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
65 SkXfermode::Make(SkXfermode::kDstIn_Mode));
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 }
reed@google.com82065d62011-02-07 15:30:46 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068protected:
69 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070070 bool onQuery(SkEvent* evt) override {
reed@android.com44a63122009-05-30 02:40:28 +000071 if (SampleCode::TitleQ(*evt)) {
72 SampleCode::TitleR(evt, "Shaders");
73 return true;
74 }
75 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 }
reed@google.com82065d62011-02-07 15:30:46 +000077
mtklein36352bf2015-03-25 18:17:31 -070078 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 canvas->drawBitmap(fBitmap, 0, 0);
reed@google.com82065d62011-02-07 15:30:46 +000080
reed@android.com44a63122009-05-30 02:40:28 +000081 canvas->translate(SkIntToScalar(20), SkIntToScalar(120));
reed@google.com82065d62011-02-07 15:30:46 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 SkPaint paint;
84 SkRect r;
85
86 paint.setColor(SK_ColorGREEN);
87 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
88 paint.setShader(fShader);
89 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
90
91 canvas->translate(SkIntToScalar(110), 0);
92
reed@android.com44a63122009-05-30 02:40:28 +000093 int w = fBitmap.width();
94 int h = fBitmap.height();
95 w = 120;
96 h = 80;
97 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
halcanary96fcdcc2015-08-27 07:41:13 -070099 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 canvas->drawRect(r, paint);
reed8a21c9f2016-03-08 18:50:00 -0800101 paint.setShader(make_bitmapfade(fBitmap));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 canvas->drawRect(r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 }
reed@google.com82065d62011-02-07 15:30:46 +0000104
reed56263c72015-06-05 11:31:26 -0700105 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700106 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000107 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 }
reed@google.com82065d62011-02-07 15:30:46 +0000109
mtklein36352bf2015-03-25 18:17:31 -0700110 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 return this->INHERITED::onClick(click);
112 }
reed@google.com82065d62011-02-07 15:30:46 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000115 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116};
117
118//////////////////////////////////////////////////////////////////////////////
119
120static SkView* MyFactory() { return new ShaderView; }
121static SkViewRegister reg(MyFactory);