blob: 2c0a6ab749c256e1e3b5bd2c279c8abb6908af10 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#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
24static SkShader* make_bitmapfade(const SkBitmap& bm)
25{
26 SkPoint pts[2];
27 SkColor colors[2];
28
29 pts[0].set(0, 0);
30 pts[1].set(0, SkIntToScalar(bm.height()));
31 colors[0] = SK_ColorBLACK;
32 colors[1] = SkColorSetARGB(0, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070033 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
35 SkShader* shaderB = SkShader::CreateBitmapShader(bm,
36 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
37
reed@android.com048522d2009-06-23 12:19:41 +000038 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
reed8681e602016-02-15 08:27:14 -080040 SkShader* shader = SkShader::CreateComposeShader(shaderB, shaderA, mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 shaderA->unref();
42 shaderB->unref();
43 mode->unref();
reed@google.com82065d62011-02-07 15:30:46 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 return shader;
46}
47
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000048class ShaderView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049public:
50 SkShader* fShader;
51 SkBitmap fBitmap;
52
rmistry@google.comae933ce2012-08-23 18:19:56 +000053 ShaderView() {
reed@android.com44a63122009-05-30 02:40:28 +000054 SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
56 SkPoint pts[2];
57 SkColor colors[2];
reed@google.com82065d62011-02-07 15:30:46 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 pts[0].set(0, 0);
60 pts[1].set(SkIntToScalar(100), 0);
61 colors[0] = SK_ColorRED;
62 colors[1] = SK_ColorBLUE;
halcanary96fcdcc2015-08-27 07:41:13 -070063 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 pts[0].set(0, 0);
66 pts[1].set(0, SkIntToScalar(100));
67 colors[0] = SK_ColorBLACK;
68 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070069 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000070
reed@android.com048522d2009-06-23 12:19:41 +000071 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
reed8681e602016-02-15 08:27:14 -080073 fShader = SkShader::CreateComposeShader(shaderA, shaderB, mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 shaderA->unref();
75 shaderB->unref();
76 mode->unref();
77 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000078 virtual ~ShaderView() {
reed@google.com82065d62011-02-07 15:30:46 +000079 SkSafeUnref(fShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 }
reed@google.com82065d62011-02-07 15:30:46 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082protected:
83 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070084 bool onQuery(SkEvent* evt) override {
reed@android.com44a63122009-05-30 02:40:28 +000085 if (SampleCode::TitleQ(*evt)) {
86 SampleCode::TitleR(evt, "Shaders");
87 return true;
88 }
89 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 }
reed@google.com82065d62011-02-07 15:30:46 +000091
mtklein36352bf2015-03-25 18:17:31 -070092 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 canvas->drawBitmap(fBitmap, 0, 0);
reed@google.com82065d62011-02-07 15:30:46 +000094
reed@android.com44a63122009-05-30 02:40:28 +000095 canvas->translate(SkIntToScalar(20), SkIntToScalar(120));
reed@google.com82065d62011-02-07 15:30:46 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 SkPaint paint;
98 SkRect r;
99
100 paint.setColor(SK_ColorGREEN);
101 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
102 paint.setShader(fShader);
103 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
104
105 canvas->translate(SkIntToScalar(110), 0);
106
reed@android.com44a63122009-05-30 02:40:28 +0000107 int w = fBitmap.width();
108 int h = fBitmap.height();
109 w = 120;
110 h = 80;
111 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112
halcanary96fcdcc2015-08-27 07:41:13 -0700113 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 canvas->drawRect(r, paint);
115 paint.setShader(make_bitmapfade(fBitmap))->unref();
116 canvas->drawRect(r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 }
reed@google.com82065d62011-02-07 15:30:46 +0000118
reed56263c72015-06-05 11:31:26 -0700119 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000121 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 }
reed@google.com82065d62011-02-07 15:30:46 +0000123
mtklein36352bf2015-03-25 18:17:31 -0700124 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 return this->INHERITED::onClick(click);
126 }
reed@google.com82065d62011-02-07 15:30:46 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000129 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130};
131
132//////////////////////////////////////////////////////////////////////////////
133
134static SkView* MyFactory() { return new ShaderView; }
135static SkViewRegister reg(MyFactory);