blob: 99cd680047780a692c7a513cac1f8d1646f3bf80 [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"
23#include "SkTransparentShader.h"
24#include "SkTypeface.h"
25
26static SkShader* make_bitmapfade(const SkBitmap& bm)
27{
28 SkPoint pts[2];
29 SkColor colors[2];
30
31 pts[0].set(0, 0);
32 pts[1].set(0, SkIntToScalar(bm.height()));
33 colors[0] = SK_ColorBLACK;
34 colors[1] = SkColorSetARGB(0, 0, 0, 0);
35 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
36
37 SkShader* shaderB = SkShader::CreateBitmapShader(bm,
38 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
39
reed@android.com048522d2009-06-23 12:19:41 +000040 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041
42 SkShader* shader = new SkComposeShader(shaderB, shaderA, mode);
43 shaderA->unref();
44 shaderB->unref();
45 mode->unref();
reed@google.com82065d62011-02-07 15:30:46 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 return shader;
48}
49
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000050class ShaderView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051public:
52 SkShader* fShader;
53 SkBitmap fBitmap;
54
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000055 ShaderView() {
reed@android.com44a63122009-05-30 02:40:28 +000056 SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +000057
58 SkPoint pts[2];
59 SkColor colors[2];
reed@google.com82065d62011-02-07 15:30:46 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 pts[0].set(0, 0);
62 pts[1].set(SkIntToScalar(100), 0);
63 colors[0] = SK_ColorRED;
64 colors[1] = SK_ColorBLUE;
65 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 pts[0].set(0, 0);
68 pts[1].set(0, SkIntToScalar(100));
69 colors[0] = SK_ColorBLACK;
70 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
71 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@google.com82065d62011-02-07 15:30:46 +000072
reed@android.com048522d2009-06-23 12:19:41 +000073 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
75 fShader = new SkComposeShader(shaderA, shaderB, mode);
76 shaderA->unref();
77 shaderB->unref();
78 mode->unref();
79 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000080 virtual ~ShaderView() {
reed@google.com82065d62011-02-07 15:30:46 +000081 SkSafeUnref(fShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 }
reed@google.com82065d62011-02-07 15:30:46 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084protected:
85 // overrides from SkEventSink
reed@android.com44a63122009-05-30 02:40:28 +000086 virtual bool onQuery(SkEvent* evt) {
87 if (SampleCode::TitleQ(*evt)) {
88 SampleCode::TitleR(evt, "Shaders");
89 return true;
90 }
91 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 }
reed@google.com82065d62011-02-07 15:30:46 +000093
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000094 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 canvas->drawBitmap(fBitmap, 0, 0);
reed@google.com82065d62011-02-07 15:30:46 +000096
reed@android.com44a63122009-05-30 02:40:28 +000097 canvas->translate(SkIntToScalar(20), SkIntToScalar(120));
reed@google.com82065d62011-02-07 15:30:46 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 SkPaint paint;
100 SkRect r;
101
102 paint.setColor(SK_ColorGREEN);
103 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
104 paint.setShader(fShader);
105 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
106
107 canvas->translate(SkIntToScalar(110), 0);
108
reed@android.com44a63122009-05-30 02:40:28 +0000109 int w = fBitmap.width();
110 int h = fBitmap.height();
111 w = 120;
112 h = 80;
113 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114
115 paint.setShader(NULL);
116 canvas->drawRect(r, paint);
117 paint.setShader(make_bitmapfade(fBitmap))->unref();
118 canvas->drawRect(r, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 paint.setShader(new SkTransparentShader)->unref();
121 canvas->drawRect(r, paint);
122 }
reed@google.com82065d62011-02-07 15:30:46 +0000123
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000124 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 this->inval(NULL);
126 return this->INHERITED::onFindClickHandler(x, y);
127 }
reed@google.com82065d62011-02-07 15:30:46 +0000128
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000129 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 return this->INHERITED::onClick(click);
131 }
reed@google.com82065d62011-02-07 15:30:46 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000134 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135};
136
137//////////////////////////////////////////////////////////////////////////////
138
139static SkView* MyFactory() { return new ShaderView; }
140static SkViewRegister reg(MyFactory);
141