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.
|
|
|
|
*/
|
2009-06-22 02:17:34 +00:00
|
|
|
#include "gm.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
// 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));
|
2012-12-06 21:47:40 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
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;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
paint.setDither(true);
|
|
|
|
paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
|
2014-05-22 12:27:41 +00:00
|
|
|
SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
|
2009-06-22 02:17:34 +00:00
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
|
|
|
|
SkShader::TileMode tmx, SkShader::TileMode tmy) {
|
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
|
|
|
|
paint->setShader(shader)->unref();
|
2013-10-31 17:28:30 +00:00
|
|
|
paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
|
2009-06-22 02:17:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
static const SkColorType gColorTypes[] = {
|
2014-04-11 17:15:40 +00:00
|
|
|
kN32_SkColorType,
|
2014-02-17 21:21:46 +00:00
|
|
|
kRGB_565_SkColorType,
|
2009-06-22 02:17:34 +00:00
|
|
|
};
|
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
class TilingGM : public skiagm::GM {
|
2009-06-22 02:17:34 +00:00
|
|
|
public:
|
2013-07-25 17:52:32 +00:00
|
|
|
TilingGM(bool powerOfTwoSize)
|
2013-08-30 12:41:42 +00:00
|
|
|
: fPowerOfTwoSize(powerOfTwoSize) {
|
2009-06-22 02:17:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
protected:
|
2013-07-25 17:52:32 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
kPOTSize = 32,
|
|
|
|
kNPOTSize = 21,
|
|
|
|
};
|
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
SkString onShortName() {
|
2013-07-25 17:52:32 +00:00
|
|
|
SkString name("tilemodes");
|
|
|
|
if (!fPowerOfTwoSize) {
|
|
|
|
name.append("_npot");
|
|
|
|
}
|
|
|
|
return name;
|
2009-06-22 02:17:34 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
SkISize onISize() { return SkISize::Make(880, 560); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-11-27 15:15:58 +00:00
|
|
|
virtual void onOnceBeforeDraw() SK_OVERRIDE {
|
2013-07-25 17:52:32 +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);
|
2012-11-27 15:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-07-25 17:52:32 +00:00
|
|
|
int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
|
|
|
|
|
|
|
|
SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
|
2009-06-22 02:17:34 +00:00
|
|
|
|
|
|
|
static const char* gConfigNames[] = { "8888", "565", "4444" };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
static const bool gFilters[] = { false, true };
|
|
|
|
static const char* gFilterNames[] = { "point", "bilinear" };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
|
|
|
|
static const char* gModeNames[] = { "C", "R", "M" };
|
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(24);
|
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
|
|
|
|
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);
|
|
|
|
p.setDither(true);
|
|
|
|
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
|
|
|
|
|
|
|
|
p.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
y += SkIntToScalar(16);
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
|
2009-06-22 02:17:34 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
|
|
|
|
x = SkIntToScalar(10);
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
|
|
|
SkPaint paint;
|
2013-08-06 20:47:52 +00:00
|
|
|
#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-08-06 20:47:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-06-22 02:17:34 +00:00
|
|
|
setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
|
|
|
|
paint.setDither(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SkPaint p;
|
|
|
|
SkString str;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
|
|
|
|
canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
y += r.height() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-22 02:17:34 +00:00
|
|
|
private:
|
2013-07-25 17:52:32 +00:00
|
|
|
bool fPowerOfTwoSize;
|
2012-10-18 02:01:59 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2009-06-22 02:17:34 +00:00
|
|
|
};
|
|
|
|
|
2013-07-25 17:52:32 +00:00
|
|
|
static const int gWidth = 32;
|
|
|
|
static const int gHeight = 32;
|
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
|
|
|
|
SkBitmap bm;
|
2014-04-11 17:15:40 +00:00
|
|
|
makebm(&bm, kN32_SkColorType, gWidth, gHeight);
|
2012-10-18 02:01:59 +00:00
|
|
|
return SkShader::CreateBitmapShader(bm, tx, ty);
|
|
|
|
}
|
2009-06-22 02:17:34 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
|
|
|
|
SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
|
|
|
|
SkScalar rad = SkIntToScalar(gWidth)/2;
|
|
|
|
SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
|
|
|
|
|
|
|
|
int index = (int)ty;
|
|
|
|
switch (index % 3) {
|
|
|
|
case 0:
|
|
|
|
return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
|
|
|
|
case 1:
|
|
|
|
return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
|
|
|
|
case 2:
|
|
|
|
return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
|
|
|
|
}
|
2012-12-03 17:35:19 +00:00
|
|
|
|
|
|
|
return NULL;
|
2012-10-18 02:01:59 +00:00
|
|
|
}
|
2009-06-22 02:17:34 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
|
|
|
|
|
|
|
|
class Tiling2GM : public skiagm::GM {
|
|
|
|
ShaderProc fProc;
|
|
|
|
SkString fName;
|
|
|
|
public:
|
|
|
|
Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
|
|
|
|
fName.printf("tilemode_%s", name);
|
|
|
|
}
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
return kSkipTiled_Flag;
|
|
|
|
}
|
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
SkString onShortName() {
|
|
|
|
return fName;
|
|
|
|
}
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
SkISize onISize() { return SkISize::Make(880, 560); }
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
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 };
|
|
|
|
|
|
|
|
static const SkShader::TileMode gModes[] = {
|
|
|
|
SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
|
|
|
|
};
|
|
|
|
static const char* gModeNames[] = {
|
|
|
|
"Clamp", "Repeat", "Mirror"
|
|
|
|
};
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
SkScalar y = SkIntToScalar(24);
|
|
|
|
SkScalar x = SkIntToScalar(66);
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
SkString str(gModeNames[kx]);
|
|
|
|
canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
|
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
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]);
|
|
|
|
canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
|
|
|
|
|
|
|
|
x += SkIntToScalar(50);
|
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
y += r.height() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
2012-10-19 02:01:19 +00:00
|
|
|
|
2012-10-18 02:01:59 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-07-25 17:52:32 +00:00
|
|
|
DEF_GM( return new TilingGM(true); )
|
|
|
|
DEF_GM( return new TilingGM(false); )
|
2013-02-22 17:28:16 +00:00
|
|
|
DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
|
|
|
|
DEF_GM( return new Tiling2GM(make_grad, "gradient"); )
|