blob: 62f055f61ec5c100df71e570a23340d402f3fb29 [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 "SkPaint.h"
12#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000013#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
17#include "SkColorPriv.h"
18#include "SkColorFilter.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000019#include "SkPicture.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkTypeface.h"
21
22// effects
23#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000024#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkBlurDrawLooper.h"
26
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000027static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
28 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000029 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkCanvas canvas(*bm);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000032 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
34 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
35 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 paint.setDither(true);
reed8a21c9f2016-03-08 18:50:00 -080038 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
39 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 canvas.drawPaint(paint);
41}
42
43static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
44 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed8a21c9f2016-03-08 18:50:00 -080045 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070046 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000049static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000050 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000051 kRGB_565_SkColorType,
reed@android.com8a1c16f2008-12-17 15:59:43 +000052};
53static const int gWidth = 32;
54static const int gHeight = 32;
55
reed@google.comf2183392011-04-22 14:10:48 +000056class TilingView : public SampleView {
reedca2622b2016-03-18 07:25:55 -070057 sk_sp<SkPicture> fTextPicture;
reed5e1ddb12015-12-21 08:52:45 -080058 SkAutoTUnref<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000060 TilingView()
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000061 : fLooper(SkBlurDrawLooper::Create(0x88000000,
62 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
63 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000064 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
65 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 }
67 }
68
robertphillips@google.com84b18c72014-04-13 19:09:42 +000069 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000070 }
71
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000072 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074protected:
75 // overrides from SkEventSink
76 virtual bool onQuery(SkEvent* evt) {
77 if (SampleCode::TitleQ(*evt)) {
78 SampleCode::TitleR(evt, "Tiling");
79 return true;
80 }
81 return this->INHERITED::onQuery(evt);
82 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reed@google.comf2183392011-04-22 14:10:48 +000084 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
86
87 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 static const bool gFilters[] = { false, true };
90 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
93 static const char* gModeNames[] = { "C", "R", "M" };
94
95 SkScalar y = SkIntToScalar(24);
96 SkScalar x = SkIntToScalar(10);
97
robertphillips@google.com84b18c72014-04-13 19:09:42 +000098 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070099 SkCanvas* textCanvas = nullptr;
100 if (nullptr == fTextPicture) {
101 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +0000102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
bsalomon49f085d2014-09-05 13:34:00 -0700104 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000105 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
106 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000107 SkPaint p;
108 SkString str;
109 p.setAntiAlias(true);
110 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000111 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000112 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
113
114 p.setTextAlign(SkPaint::kCenter_Align);
115 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.comf2b98d62010-12-20 18:26:13 +0000117 x += r.width() * 4 / 3;
118 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 }
120 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 y += SkIntToScalar(16);
123
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000124 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000125 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000127 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
128 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 SkPaint paint;
130 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
131 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 canvas->save();
134 canvas->translate(x, y);
135 canvas->drawRect(r, paint);
136 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 x += r.width() * 4 / 3;
139 }
140 }
bsalomon49f085d2014-09-05 13:34:00 -0700141 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 SkPaint p;
143 SkString str;
144 p.setAntiAlias(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000145 p.setLooper(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000147 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 }
149
150 y += r.height() * 4 / 3;
151 }
152 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000153
bsalomon49f085d2014-09-05 13:34:00 -0700154 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700156 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000157 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000158
bsalomon49f085d2014-09-05 13:34:00 -0700159 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700160 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163private:
reed@google.comf2183392011-04-22 14:10:48 +0000164 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169static SkView* MyFactory() { return new TilingView; }
170static SkViewRegister reg(MyFactory);