blob: cb8061ec9f95b172b5a083f2d2f4f3e9af4e290b [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 */
Mike Reed75ae4212018-01-23 11:24:08 -05007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
Mike Reed75ae4212018-01-23 11:24:08 -050010#include "SkBitmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkPaint.h"
13#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000014#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
18#include "SkColorPriv.h"
19#include "SkColorFilter.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000020#include "SkPicture.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkTypeface.h"
22
23// effects
24#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000025#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkBlurDrawLooper.h"
27
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000028static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
29 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000030 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 SkCanvas canvas(*bm);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000033 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
35 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
36 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 paint.setDither(true);
reed8a21c9f2016-03-08 18:50:00 -080039 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
40 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 canvas.drawPaint(paint);
42}
43
44static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
45 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed8a21c9f2016-03-08 18:50:00 -080046 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070047 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048}
49
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000050static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000051 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000052 kRGB_565_SkColorType,
reed@android.com8a1c16f2008-12-17 15:59:43 +000053};
54static const int gWidth = 32;
55static const int gHeight = 32;
56
reed@google.comf2183392011-04-22 14:10:48 +000057class TilingView : public SampleView {
reedca2622b2016-03-18 07:25:55 -070058 sk_sp<SkPicture> fTextPicture;
reed7b380d02016-03-21 13:25:16 -070059 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000061 TilingView()
reed7b380d02016-03-21 13:25:16 -070062 : fLooper(SkBlurDrawLooper::Make(0x88000000,
63 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
64 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000065 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
66 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
68 }
69
robertphillips@google.com84b18c72014-04-13 19:09:42 +000070 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000071 }
72
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000073 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075protected:
76 // overrides from SkEventSink
77 virtual bool onQuery(SkEvent* evt) {
78 if (SampleCode::TitleQ(*evt)) {
79 SampleCode::TitleR(evt, "Tiling");
80 return true;
81 }
82 return this->INHERITED::onQuery(evt);
83 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@google.comf2183392011-04-22 14:10:48 +000085 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
87
88 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 static const bool gFilters[] = { false, true };
91 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
94 static const char* gModeNames[] = { "C", "R", "M" };
95
96 SkScalar y = SkIntToScalar(24);
97 SkScalar x = SkIntToScalar(10);
98
robertphillips@google.com84b18c72014-04-13 19:09:42 +000099 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -0700100 SkCanvas* textCanvas = nullptr;
101 if (nullptr == fTextPicture) {
102 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +0000103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104
bsalomon49f085d2014-09-05 13:34:00 -0700105 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000106 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
107 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000108 SkPaint p;
109 SkString str;
110 p.setAntiAlias(true);
111 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000112 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000113 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
114
115 p.setTextAlign(SkPaint::kCenter_Align);
Cary Clark2a475ea2017-04-28 15:35:12 -0400116 textCanvas->drawString(str, x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
reed@android.comf2b98d62010-12-20 18:26:13 +0000118 x += r.width() * 4 / 3;
119 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 }
121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 y += SkIntToScalar(16);
124
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000125 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000126 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000128 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
129 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkPaint paint;
131 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
132 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 canvas->save();
135 canvas->translate(x, y);
136 canvas->drawRect(r, paint);
137 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 x += r.width() * 4 / 3;
140 }
141 }
bsalomon49f085d2014-09-05 13:34:00 -0700142 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 SkPaint p;
144 SkString str;
145 p.setAntiAlias(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000146 p.setLooper(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400148 textCanvas->drawString(str, x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 }
150
151 y += r.height() * 4 / 3;
152 }
153 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000154
bsalomon49f085d2014-09-05 13:34:00 -0700155 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700156 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700157 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000158 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000159
bsalomon49f085d2014-09-05 13:34:00 -0700160 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700161 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164private:
reed@google.comf2183392011-04-22 14:10:48 +0000165 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166};
167
168//////////////////////////////////////////////////////////////////////////////
169
170static SkView* MyFactory() { return new TilingView; }
171static SkViewRegister reg(MyFactory);