2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2018-01-23 16:24:08 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
// effects
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkBlurDrawLooper.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
|
|
|
|
bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
|
2012-12-06 21:47:40 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkCanvas canvas(*bm);
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setDither(true);
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
|
2019-04-03 14:27:45 +00:00
|
|
|
SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, SkTileMode tmx, SkTileMode tmy) {
|
2019-04-08 17:20:23 +00:00
|
|
|
paint->setShader(bm.makeShader(tmx, tmy));
|
2015-03-16 17:08:34 +00:00
|
|
|
paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
static const SkColorType gColorTypes[] = {
|
2014-04-11 17:15:40 +00:00
|
|
|
kN32_SkColorType,
|
2014-02-17 02:55:57 +00:00
|
|
|
kRGB_565_SkColorType,
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
static const int gWidth = 32;
|
|
|
|
static const int gHeight = 32;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class TilingView : public Sample {
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> fTextPicture;
|
2016-03-21 20:25:16 +00:00
|
|
|
sk_sp<SkDrawLooper> fLooper;
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
TilingView()
|
2016-03-21 20:25:16 +00:00
|
|
|
: fLooper(SkBlurDrawLooper::Make(0x88000000,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(2))) {
|
2014-02-17 02:55:57 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
|
|
|
|
makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 19:09:42 +00:00
|
|
|
virtual ~TilingView() {
|
2011-09-13 18:15:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
virtual SkString name() { return SkString("Tiling"); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
|
|
|
|
|
|
|
|
static const char* gConfigNames[] = { "8888", "565", "4444" };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static const bool gFilters[] = { false, true };
|
|
|
|
static const char* gFilterNames[] = { "point", "bilinear" };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
static const SkTileMode gModes[] = { SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
|
|
|
|
static const char* gModeNames[] = { "C", "R", "M" };
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(24);
|
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
2015-08-27 14:41:13 +00:00
|
|
|
SkCanvas* textCanvas = nullptr;
|
|
|
|
if (nullptr == fTextPicture) {
|
|
|
|
textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
|
2010-12-20 18:26:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
if (textCanvas) {
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
2010-12-20 18:26:13 +00:00
|
|
|
SkPaint p;
|
|
|
|
SkString str;
|
|
|
|
p.setDither(true);
|
2014-04-15 15:48:36 +00:00
|
|
|
p.setLooper(fLooper);
|
2010-12-20 18:26:13 +00:00
|
|
|
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
|
|
|
|
|
2019-01-03 20:45:53 +00:00
|
|
|
SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
y += SkIntToScalar(16);
|
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
x = SkIntToScalar(10);
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
|
|
|
|
paint.setDither(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 20:34:00 +00:00
|
|
|
if (textCanvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
2014-04-15 15:48:36 +00:00
|
|
|
p.setLooper(fLooper);
|
2019-01-08 19:00:08 +00:00
|
|
|
textCanvas->drawString(
|
|
|
|
SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
|
|
|
|
x, y + r.height() * 2 / 3, SkFont(), p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
y += r.height() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
2010-12-20 18:26:13 +00:00
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
if (textCanvas) {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkASSERT(nullptr == fTextPicture);
|
2016-03-18 14:25:55 +00:00
|
|
|
fTextPicture = recorder.finishRecordingAsPicture();
|
2014-04-15 23:00:57 +00:00
|
|
|
}
|
2014-04-13 19:09:42 +00:00
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(fTextPicture);
|
2016-03-18 14:25:55 +00:00
|
|
|
canvas->drawPicture(fTextPicture.get());
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new TilingView(); )
|