| 1 |
/* |
| 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 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkPoint.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkShader.h" |
| 21 |
#include "include/core/SkSize.h" |
| 22 |
#include "include/core/SkString.h" |
| 23 |
#include "include/core/SkTileMode.h" |
| 24 |
#include "include/core/SkTypeface.h" |
| 25 |
#include "include/core/SkTypes.h" |
| 26 |
#include "include/effects/SkGradient.h" |
| 27 |
#include "include/utils/SkTextUtils.h" |
| 28 |
#include "tools/DecodeUtils.h" |
| 29 |
#include "tools/GpuToolUtils.h" |
| 30 |
#include "tools/Resources.h" |
| 31 |
#include "tools/ToolUtils.h" |
| 32 |
#include "tools/fonts/FontToolUtils.h" |
| 33 |
|
| 34 |
#include <functional> |
| 35 |
|
| 36 |
static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
| 37 |
bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
| 38 |
bm->eraseColor(SK_ColorTRANSPARENT); |
| 39 |
|
| 40 |
SkCanvas canvas(*bm); |
| 41 |
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; |
| 42 |
SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen, SkColors::kBlue }; |
| 43 |
SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 44 |
SkPaint paint; |
| 45 |
|
| 46 |
paint.setDither(true); |
| 47 |
paint.setShader(SkShaders::LinearGradient(pts, {{colors, pos, SkTileMode::kClamp}, {}})); |
| 48 |
canvas.drawPaint(paint); |
| 49 |
} |
| 50 |
|
| 51 |
static void setup(SkCanvas* canvas, SkPaint* paint, const SkBitmap& bm, SkFilterMode fm, |
| 52 |
SkTileMode tmx, SkTileMode tmy) { |
| 53 |
sk_sp<SkImage> img = SkImages::RasterFromBitmap(bm); |
| 54 |
img = ToolUtils::MakeTextureImage(canvas, std::move(img)); |
| 55 |
if (img) { |
| 56 |
// img can be null if the GPU context has been abandoned. |
| 57 |
paint->setShader(img->makeShader(tmx, tmy, SkSamplingOptions(fm))); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
constexpr SkColorType gColorTypes[] = { |
| 62 |
kN32_SkColorType, |
| 63 |
kRGB_565_SkColorType, |
| 64 |
}; |
| 65 |
|
| 66 |
class TilingGM : public skiagm::GM { |
| 67 |
public: |
| 68 |
TilingGM(bool powerOfTwoSize) |
| 69 |
: fPowerOfTwoSize(powerOfTwoSize) { |
| 70 |
} |
| 71 |
|
| 72 |
SkBitmap fTexture[std::size(gColorTypes)]; |
| 73 |
|
| 74 |
protected: |
| 75 |
|
| 76 |
enum { |
| 77 |
kPOTSize = 32, |
| 78 |
kNPOTSize = 21, |
| 79 |
}; |
| 80 |
|
| 81 |
SkString getName() const override { |
| 82 |
SkString name("tilemodes"); |
| 83 |
if (!fPowerOfTwoSize) { |
| 84 |
name.append("_npot"); |
| 85 |
} |
| 86 |
return name; |
| 87 |
} |
| 88 |
|
| 89 |
SkISize getISize() override { return SkISize::Make(880, 560); } |
| 90 |
|
| 91 |
void onOnceBeforeDraw() override { |
| 92 |
int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
| 93 |
for (size_t i = 0; i < std::size(gColorTypes); i++) { |
| 94 |
makebm(&fTexture[i], gColorTypes[i], size, size); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void onDraw(SkCanvas* canvas) override { |
| 99 |
SkPaint textPaint; |
| 100 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 101 |
|
| 102 |
int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
| 103 |
|
| 104 |
SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; |
| 105 |
|
| 106 |
const char* gConfigNames[] = { "8888", "565" }; |
| 107 |
|
| 108 |
constexpr SkFilterMode gFilters[] = { SkFilterMode::kNearest, SkFilterMode::kLinear }; |
| 109 |
static const char* gFilterNames[] = { "point", "bilinear" }; |
| 110 |
|
| 111 |
constexpr SkTileMode gModes[] = { |
| 112 |
SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror }; |
| 113 |
static const char* gModeNames[] = { "C", "R", "M" }; |
| 114 |
|
| 115 |
SkScalar y = SkIntToScalar(24); |
| 116 |
SkScalar x = SkIntToScalar(10); |
| 117 |
|
| 118 |
for (size_t kx = 0; kx < std::size(gModes); kx++) { |
| 119 |
for (size_t ky = 0; ky < std::size(gModes); ky++) { |
| 120 |
SkPaint p; |
| 121 |
p.setDither(true); |
| 122 |
SkString str; |
| 123 |
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 124 |
|
| 125 |
SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, p, |
| 126 |
SkTextUtils::kCenter_Align); |
| 127 |
|
| 128 |
x += r.width() * 4 / 3; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
y += SkIntToScalar(16); |
| 133 |
|
| 134 |
for (size_t i = 0; i < std::size(gColorTypes); i++) { |
| 135 |
for (size_t j = 0; j < std::size(gFilters); j++) { |
| 136 |
x = SkIntToScalar(10); |
| 137 |
for (size_t kx = 0; kx < std::size(gModes); kx++) { |
| 138 |
for (size_t ky = 0; ky < std::size(gModes); ky++) { |
| 139 |
SkPaint paint; |
| 140 |
#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue |
| 141 |
// on SGX where resizing NPOT textures to POT textures exhibits a driver bug. |
| 142 |
if (!fPowerOfTwoSize) { |
| 143 |
makebm(&fTexture[i], gColorTypes[i], size, size); |
| 144 |
} |
| 145 |
#endif |
| 146 |
setup(canvas, &paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 147 |
paint.setDither(true); |
| 148 |
|
| 149 |
canvas->save(); |
| 150 |
canvas->translate(x, y); |
| 151 |
canvas->drawRect(r, paint); |
| 152 |
canvas->restore(); |
| 153 |
|
| 154 |
x += r.width() * 4 / 3; |
| 155 |
} |
| 156 |
} |
| 157 |
canvas->drawString(SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]), |
| 158 |
x, y + r.height() * 2 / 3, font, textPaint); |
| 159 |
|
| 160 |
y += r.height() * 4 / 3; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
private: |
| 166 |
bool fPowerOfTwoSize; |
| 167 |
using INHERITED = skiagm::GM; |
| 168 |
}; |
| 169 |
DEF_GM( return new TilingGM(true); ) |
| 170 |
DEF_GM( return new TilingGM(false); ) |
| 171 |
|
| 172 |
constexpr int gWidth = 32; |
| 173 |
constexpr int gHeight = 32; |
| 174 |
|
| 175 |
static sk_sp<SkShader> make_bm(SkTileMode tx, SkTileMode ty) { |
| 176 |
SkBitmap bm; |
| 177 |
makebm(&bm, kN32_SkColorType, gWidth, gHeight); |
| 178 |
return bm.makeShader(tx, ty, SkSamplingOptions()); |
| 179 |
} |
| 180 |
|
| 181 |
static sk_sp<SkShader> make_grad(SkTileMode tx, SkTileMode ty) { |
| 182 |
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 183 |
SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 184 |
SkScalar rad = SkIntToScalar(gWidth)/2; |
| 185 |
SkColor4f colors[] = {{1,0,0,1}, SkColor4f::FromColor(ToolUtils::color_to_565(0xFF0044FF))}; |
| 186 |
const SkGradient grad = {{colors, {}, tx}, {}}; |
| 187 |
|
| 188 |
int index = (int)ty; |
| 189 |
switch (index % 3) { |
| 190 |
case 0: |
| 191 |
return SkShaders::LinearGradient(pts, grad); |
| 192 |
case 1: |
| 193 |
return SkShaders::RadialGradient(center, rad, grad); |
| 194 |
case 2: |
| 195 |
return SkShaders::SweepGradient(center, 135, 225, grad); |
| 196 |
} |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
typedef sk_sp<SkShader> (*ShaderProc)(SkTileMode, SkTileMode); |
| 201 |
|
| 202 |
class Tiling2GM : public skiagm::GM { |
| 203 |
ShaderProc fProc; |
| 204 |
const char* fName; |
| 205 |
|
| 206 |
public: |
| 207 |
Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {} |
| 208 |
|
| 209 |
private: |
| 210 |
SkString getName() const override { return SkString(fName); } |
| 211 |
|
| 212 |
SkISize getISize() override { return SkISize::Make(650, 610); } |
| 213 |
|
| 214 |
void onDraw(SkCanvas* canvas) override { |
| 215 |
canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); |
| 216 |
|
| 217 |
const SkScalar w = SkIntToScalar(gWidth); |
| 218 |
const SkScalar h = SkIntToScalar(gHeight); |
| 219 |
SkRect r = { -w, -h, w*2, h*2 }; |
| 220 |
|
| 221 |
constexpr SkTileMode gModes[] = { |
| 222 |
SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror |
| 223 |
}; |
| 224 |
const char* gModeNames[] = { |
| 225 |
"Clamp", "Repeat", "Mirror" |
| 226 |
}; |
| 227 |
|
| 228 |
SkScalar y = SkIntToScalar(24); |
| 229 |
SkScalar x = SkIntToScalar(66); |
| 230 |
|
| 231 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 232 |
|
| 233 |
for (size_t kx = 0; kx < std::size(gModes); kx++) { |
| 234 |
SkString str(gModeNames[kx]); |
| 235 |
SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(), |
| 236 |
SkTextUtils::kCenter_Align); |
| 237 |
x += r.width() * 4 / 3; |
| 238 |
} |
| 239 |
|
| 240 |
y += SkIntToScalar(16) + h; |
| 241 |
|
| 242 |
for (size_t ky = 0; ky < std::size(gModes); ky++) { |
| 243 |
x = SkIntToScalar(16) + w; |
| 244 |
|
| 245 |
SkString str(gModeNames[ky]); |
| 246 |
SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), |
| 247 |
SkTextUtils::kRight_Align); |
| 248 |
|
| 249 |
x += SkIntToScalar(50); |
| 250 |
for (size_t kx = 0; kx < std::size(gModes); kx++) { |
| 251 |
SkPaint paint; |
| 252 |
paint.setShader(fProc(gModes[kx], gModes[ky])); |
| 253 |
|
| 254 |
canvas->save(); |
| 255 |
canvas->translate(x, y); |
| 256 |
canvas->drawRect(r, paint); |
| 257 |
canvas->restore(); |
| 258 |
|
| 259 |
x += r.width() * 4 / 3; |
| 260 |
} |
| 261 |
y += r.height() * 4 / 3; |
| 262 |
} |
| 263 |
} |
| 264 |
}; |
| 265 |
|
| 266 |
DEF_GM( return new Tiling2GM(make_bm, "tilemode_bitmap"); ) |
| 267 |
DEF_GM( return new Tiling2GM(make_grad, "tilemode_gradient"); ) |
| 268 |
|
| 269 |
//////////////////// |
| 270 |
|
| 271 |
DEF_SIMPLE_GM(tilemode_decal, canvas, 720, 1100) { |
| 272 |
auto img = ToolUtils::GetResourceAsImage("images/mandrill_128.png"); |
| 273 |
SkPaint bgpaint; |
| 274 |
bgpaint.setColor(SK_ColorYELLOW); |
| 275 |
|
| 276 |
SkRect r = { -20, -20, img->width() + 20.0f, img->height() + 20.0f }; |
| 277 |
canvas->translate(45, 45); |
| 278 |
|
| 279 |
std::function<void(SkPaint*, SkTileMode, SkTileMode)> shader_procs[] = { |
| 280 |
[img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
| 281 |
// Test no filtering with decal mode |
| 282 |
paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkFilterMode::kNearest))); |
| 283 |
}, |
| 284 |
[img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
| 285 |
// Test bilerp approximation for decal mode (or clamp to border HW) |
| 286 |
paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkFilterMode::kLinear))); |
| 287 |
}, |
| 288 |
[img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
| 289 |
// Test bicubic filter with decal mode |
| 290 |
paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkCubicResampler::Mitchell()))); |
| 291 |
}, |
| 292 |
[img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
| 293 |
SkColor4f colors[] = { SkColors::kRed, SkColors::kBlue }; |
| 294 |
const SkPoint pts[] = {{ 0, 0 }, {img->width()*1.0f, img->height()*1.0f }}; |
| 295 |
paint->setShader(SkShaders::LinearGradient(pts, {{colors, {}, tx}, {}})); |
| 296 |
}, |
| 297 |
[img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
| 298 |
SkColor4f colors[] = { SkColors::kRed, SkColors::kBlue }; |
| 299 |
paint->setShader(SkShaders::RadialGradient({ img->width()*0.5f, img->width()*0.5f }, |
| 300 |
img->width()*0.5f, {{colors, {}, tx}, {}})); |
| 301 |
}, |
| 302 |
}; |
| 303 |
|
| 304 |
const struct XY { |
| 305 |
SkTileMode fX; |
| 306 |
SkTileMode fY; |
| 307 |
} pairs[] = { |
| 308 |
{ SkTileMode::kClamp, SkTileMode::kClamp }, |
| 309 |
{ SkTileMode::kClamp, SkTileMode::kDecal }, |
| 310 |
{ SkTileMode::kDecal, SkTileMode::kClamp }, |
| 311 |
{ SkTileMode::kDecal, SkTileMode::kDecal }, |
| 312 |
}; |
| 313 |
for (const auto& p : pairs) { |
| 314 |
SkPaint paint; |
| 315 |
canvas->save(); |
| 316 |
for (const auto& proc : shader_procs) { |
| 317 |
canvas->save(); |
| 318 |
// Apply a slight rotation to highlight the differences between filtered and unfiltered |
| 319 |
// decal edges |
| 320 |
canvas->rotate(4); |
| 321 |
canvas->drawRect(r, bgpaint); |
| 322 |
proc(&paint, p.fX, p.fY); |
| 323 |
canvas->drawRect(r, paint); |
| 324 |
canvas->restore(); |
| 325 |
canvas->translate(0, r.height() + 20); |
| 326 |
} |
| 327 |
canvas->restore(); |
| 328 |
canvas->translate(r.width() + 10, 0); |
| 329 |
} |
| 330 |
} |