2013-09-04 19:23:53 +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.
|
|
|
|
*/
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2016-08-04 13:19:33 +00:00
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkMaskFilter.h"
|
2013-09-04 19:23:53 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
// effects
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkBlurDrawLooper.h"
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
|
|
|
|
bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
|
2013-09-04 19:23:53 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
|
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
paint.setDither(true);
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
|
|
|
|
SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
|
2013-09-04 19:23:53 +00:00
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level,
|
2013-09-04 19:23:53 +00:00
|
|
|
SkShader::TileMode tmx, SkShader::TileMode tmy) {
|
2016-03-13 21:13:58 +00:00
|
|
|
paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
|
2015-03-16 17:08:34 +00:00
|
|
|
paint->setFilterQuality(filter_level);
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColorType gColorTypes[] = {
|
2014-04-11 17:15:40 +00:00
|
|
|
kN32_SkColorType,
|
2014-02-17 21:21:46 +00:00
|
|
|
kRGB_565_SkColorType,
|
2013-09-04 19:23:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ScaledTilingGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ScaledTilingGM(bool powerOfTwoSize)
|
2015-07-13 19:08:45 +00:00
|
|
|
: fPowerOfTwoSize(powerOfTwoSize) {
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
enum {
|
|
|
|
kPOTSize = 4,
|
|
|
|
kNPOTSize = 3,
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-09-04 19:23:53 +00:00
|
|
|
SkString name("scaled_tilemodes");
|
|
|
|
if (!fPowerOfTwoSize) {
|
|
|
|
name.append("_npot");
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(880, 760); }
|
2013-09-04 19:23:53 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2013-09-04 19:23:53 +00:00
|
|
|
int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
|
2014-02-17 21:21:46 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
|
|
|
|
makebm(&fTexture[i], gColorTypes[i], size, size);
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-09-04 19:23:53 +00:00
|
|
|
float scale = 32.f/kPOTSize;
|
|
|
|
|
|
|
|
int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
|
|
|
|
|
|
|
|
SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const char* gColorTypeNames[] = { "8888" , "565", "4444" };
|
2013-09-04 19:23:53 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkFilterQuality gFilterQualitys[] =
|
2015-03-16 17:08:34 +00:00
|
|
|
{ kNone_SkFilterQuality,
|
|
|
|
kLow_SkFilterQuality,
|
|
|
|
kMedium_SkFilterQuality,
|
|
|
|
kHigh_SkFilterQuality };
|
2016-09-01 18:24:54 +00:00
|
|
|
const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
|
2013-09-04 19:23:53 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkShader::TileMode gModes[] = {
|
|
|
|
SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
|
|
|
|
const char* gModeNames[] = { "C", "R", "M" };
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(24);
|
|
|
|
SkScalar x = SkIntToScalar(10)/scale;
|
|
|
|
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
|
|
|
SkPaint p;
|
|
|
|
SkString str;
|
|
|
|
p.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&p);
|
2013-09-04 19:23:53 +00:00
|
|
|
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
|
|
|
|
|
|
|
|
p.setTextAlign(SkPaint::kCenter_Align);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(str, scale*(x + r.width()/2), y, p);
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
y = SkIntToScalar(40) / scale;
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
|
2015-03-16 17:08:34 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) {
|
2013-09-04 19:23:53 +00:00
|
|
|
x = SkIntToScalar(10)/scale;
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
|
|
|
SkPaint paint;
|
|
|
|
#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
|
|
|
|
// on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
|
|
|
|
if (!fPowerOfTwoSize) {
|
2014-02-17 21:21:46 +00:00
|
|
|
makebm(&fTexture[i], gColorTypes[i], size, size);
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-03-16 17:08:34 +00:00
|
|
|
setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]);
|
2013-09-04 19:23:53 +00:00
|
|
|
paint.setDither(true);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(scale,scale);
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint p;
|
|
|
|
SkString str;
|
|
|
|
p.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&p);
|
2014-02-17 21:21:46 +00:00
|
|
|
str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(str, scale*x, scale*(y + r.height() * 2 / 3), p);
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
y += r.height() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fPowerOfTwoSize;
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int gWidth = 32;
|
|
|
|
constexpr int gHeight = 32;
|
2013-09-04 19:23:53 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
|
2013-09-04 19:23:53 +00:00
|
|
|
SkBitmap bm;
|
2014-04-11 17:15:40 +00:00
|
|
|
makebm(&bm, kN32_SkColorType, gWidth, gHeight);
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkShader::MakeBitmapShader(bm, tx, ty);
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
|
2013-09-04 19:23:53 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
|
|
|
|
SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
|
|
|
|
SkScalar rad = SkIntToScalar(gWidth)/2;
|
2015-07-13 19:08:45 +00:00
|
|
|
SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
int index = (int)ty;
|
|
|
|
switch (index % 3) {
|
|
|
|
case 0:
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
|
2013-09-04 19:23:53 +00:00
|
|
|
case 1:
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
|
2013-09-04 19:23:53 +00:00
|
|
|
case 2:
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2013-09-04 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
class ScaledTiling2GM : public skiagm::GM {
|
|
|
|
ShaderProc fProc;
|
|
|
|
SkString fName;
|
|
|
|
public:
|
|
|
|
ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
|
|
|
|
fName.printf("scaled_tilemode_%s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-09-04 19:23:53 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2016-04-11 14:46:38 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(650, 610); }
|
2013-09-04 19:23:53 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-09-04 19:23:53 +00:00
|
|
|
canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
|
|
|
|
|
|
|
|
const SkScalar w = SkIntToScalar(gWidth);
|
|
|
|
const SkScalar h = SkIntToScalar(gHeight);
|
|
|
|
SkRect r = { -w, -h, w*2, h*2 };
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkShader::TileMode gModes[] = {
|
2013-09-04 19:23:53 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
|
|
|
|
};
|
2016-09-01 18:24:54 +00:00
|
|
|
const char* gModeNames[] = {
|
2013-09-04 19:23:53 +00:00
|
|
|
"Clamp", "Repeat", "Mirror"
|
|
|
|
};
|
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(24);
|
|
|
|
SkScalar x = SkIntToScalar(66);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&p);
|
2013-09-04 19:23:53 +00:00
|
|
|
p.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
SkString str(gModeNames[kx]);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(str, x + r.width()/2, y, p);
|
2013-09-04 19:23:53 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
y += SkIntToScalar(16) + h;
|
|
|
|
p.setTextAlign(SkPaint::kRight_Align);
|
|
|
|
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
|
|
|
x = SkIntToScalar(16) + w;
|
|
|
|
|
|
|
|
SkString str(gModeNames[ky]);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(str, x, y + h/2, p);
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
x += SkIntToScalar(50);
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(fProc(gModes[kx], gModes[ky]));
|
2013-09-04 19:23:53 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
y += r.height() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return new ScaledTilingGM(true); )
|
|
|
|
DEF_GM( return new ScaledTilingGM(false); )
|
|
|
|
DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
|
|
|
|
DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )
|