blob: a9a8cf9c17ab2670a404214a04a1cabfedf6d30b [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.comda342a82008-12-23 18:37:08 +000019#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkTypeface.h"
24
25static SkShader* make_bitmapfade(const SkBitmap& bm)
26{
27 SkPoint pts[2];
28 SkColor colors[2];
29
30 pts[0].set(0, 0);
31 pts[1].set(0, SkIntToScalar(bm.height()));
32 colors[0] = SK_ColorBLACK;
33 colors[1] = SkColorSetARGB(0, 0, 0, 0);
34 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
35
36 SkShader* shaderB = SkShader::CreateBitmapShader(bm,
37 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
38
reed@android.com048522d2009-06-23 12:19:41 +000039 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
41 SkShader* shader = new SkComposeShader(shaderB, shaderA, mode);
42 shaderA->unref();
43 shaderB->unref();
44 mode->unref();
reed@google.com82065d62011-02-07 15:30:46 +000045
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 return shader;
47}
48
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000049class ShaderView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050public:
51 SkShader* fShader;
52 SkBitmap fBitmap;
53
rmistry@google.comae933ce2012-08-23 18:19:56 +000054 ShaderView() {
reed@android.com44a63122009-05-30 02:40:28 +000055 SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000056
57 SkPoint pts[2];
58 SkColor colors[2];
reed@google.com82065d62011-02-07 15:30:46 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 pts[0].set(0, 0);
61 pts[1].set(SkIntToScalar(100), 0);
62 colors[0] = SK_ColorRED;
63 colors[1] = SK_ColorBLUE;
64 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 pts[0].set(0, 0);
67 pts[1].set(0, SkIntToScalar(100));
68 colors[0] = SK_ColorBLACK;
69 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
70 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000071
reed@android.com048522d2009-06-23 12:19:41 +000072 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000073
74 fShader = new SkComposeShader(shaderA, shaderB, mode);
75 shaderA->unref();
76 shaderB->unref();
77 mode->unref();
78 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000079 virtual ~ShaderView() {
reed@google.com82065d62011-02-07 15:30:46 +000080 SkSafeUnref(fShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 }
reed@google.com82065d62011-02-07 15:30:46 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083protected:
84 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070085 bool onQuery(SkEvent* evt) override {
reed@android.com44a63122009-05-30 02:40:28 +000086 if (SampleCode::TitleQ(*evt)) {
87 SampleCode::TitleR(evt, "Shaders");
88 return true;
89 }
90 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 }
reed@google.com82065d62011-02-07 15:30:46 +000092
mtklein36352bf2015-03-25 18:17:31 -070093 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 canvas->drawBitmap(fBitmap, 0, 0);
reed@google.com82065d62011-02-07 15:30:46 +000095
reed@android.com44a63122009-05-30 02:40:28 +000096 canvas->translate(SkIntToScalar(20), SkIntToScalar(120));
reed@google.com82065d62011-02-07 15:30:46 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 SkPaint paint;
99 SkRect r;
100
101 paint.setColor(SK_ColorGREEN);
102 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
103 paint.setShader(fShader);
104 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
105
106 canvas->translate(SkIntToScalar(110), 0);
107
reed@android.com44a63122009-05-30 02:40:28 +0000108 int w = fBitmap.width();
109 int h = fBitmap.height();
110 w = 120;
111 h = 80;
112 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113
114 paint.setShader(NULL);
115 canvas->drawRect(r, paint);
116 paint.setShader(make_bitmapfade(fBitmap))->unref();
117 canvas->drawRect(r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 }
reed@google.com82065d62011-02-07 15:30:46 +0000119
reed56263c72015-06-05 11:31:26 -0700120 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000122 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 }
reed@google.com82065d62011-02-07 15:30:46 +0000124
mtklein36352bf2015-03-25 18:17:31 -0700125 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 return this->INHERITED::onClick(click);
127 }
reed@google.com82065d62011-02-07 15:30:46 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000130 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131};
132
133//////////////////////////////////////////////////////////////////////////////
134
135static SkView* MyFactory() { return new ShaderView; }
136static SkViewRegister reg(MyFactory);