| 1 |
/* |
| 2 |
* Copyright 2016 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/SkBlendMode.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkSurface.h" |
| 22 |
#include "include/private/base/SkMalloc.h" |
| 23 |
#include "tools/ToolUtils.h" |
| 24 |
|
| 25 |
#if defined(SK_GANESH) |
| 26 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop, |
| 30 |
int padRight, int padBottom) { |
| 31 |
SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom); |
| 32 |
return ToolUtils::makeSurface(root, info); |
| 33 |
} |
| 34 |
|
| 35 |
static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop, |
| 36 |
int padRight, int padBottom) { |
| 37 |
const int kCap = 28; |
| 38 |
const int kMid = 8; |
| 39 |
const int kSize = 2*kCap + 3*kMid; |
| 40 |
|
| 41 |
auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom)); |
| 42 |
SkCanvas* canvas = surface->getCanvas(); |
| 43 |
canvas->translate((float) padLeft, (float) padTop); |
| 44 |
|
| 45 |
SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 46 |
const SkScalar strokeWidth = SkIntToScalar(6); |
| 47 |
const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2; |
| 48 |
|
| 49 |
xDivs[0] = kCap + padLeft; |
| 50 |
yDivs[0] = kCap + padTop; |
| 51 |
xDivs[1] = kCap + kMid + padLeft; |
| 52 |
yDivs[1] = kCap + kMid + padTop; |
| 53 |
xDivs[2] = kCap + 2 * kMid + padLeft; |
| 54 |
yDivs[2] = kCap + 2 * kMid + padTop; |
| 55 |
xDivs[3] = kCap + 3 * kMid + padLeft; |
| 56 |
yDivs[3] = kCap + 3 * kMid + padTop; |
| 57 |
|
| 58 |
SkPaint paint; |
| 59 |
paint.setAntiAlias(true); |
| 60 |
|
| 61 |
paint.setColor(0xFFFFFF00); |
| 62 |
canvas->drawRoundRect(r, radius, radius, paint); |
| 63 |
|
| 64 |
r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize)); |
| 65 |
paint.setColor(0x8800FF00); |
| 66 |
canvas->drawRect(r, paint); |
| 67 |
r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize)); |
| 68 |
paint.setColor(0x880000FF); |
| 69 |
canvas->drawRect(r, paint); |
| 70 |
r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize)); |
| 71 |
paint.setColor(0x88FF00FF); |
| 72 |
canvas->drawRect(r, paint); |
| 73 |
|
| 74 |
r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid)); |
| 75 |
paint.setColor(0x8800FF00); |
| 76 |
canvas->drawRect(r, paint); |
| 77 |
r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid)); |
| 78 |
paint.setColor(0x880000FF); |
| 79 |
canvas->drawRect(r, paint); |
| 80 |
r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid)); |
| 81 |
paint.setColor(0x88FF00FF); |
| 82 |
canvas->drawRect(r, paint); |
| 83 |
|
| 84 |
return surface->makeImageSnapshot(); |
| 85 |
} |
| 86 |
|
| 87 |
static void image_to_bitmap(GrDirectContext* dContext, const SkImage* image, SkBitmap* bm) { |
| 88 |
SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height()); |
| 89 |
bm->allocPixels(info); |
| 90 |
image->readPixels(dContext, info, bm->getPixels(), bm->rowBytes(), 0, 0); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more |
| 95 |
* than nine patches. |
| 96 |
*/ |
| 97 |
class LatticeGM : public skiagm::GM { |
| 98 |
public: |
| 99 |
LatticeGM() {} |
| 100 |
|
| 101 |
protected: |
| 102 |
SkString getName() const override { return SkString("lattice"); } |
| 103 |
|
| 104 |
SkISize getISize() override { return SkISize::Make(800, 800); } |
| 105 |
|
| 106 |
void onDrawHelper(GrDirectContext* dContext, SkCanvas* canvas, int padLeft, int padTop, |
| 107 |
int padRight, int padBottom) { |
| 108 |
canvas->save(); |
| 109 |
|
| 110 |
int xDivs[5]; |
| 111 |
int yDivs[5]; |
| 112 |
xDivs[0] = padLeft; |
| 113 |
yDivs[0] = padTop; |
| 114 |
|
| 115 |
SkBitmap bitmap; |
| 116 |
sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop, |
| 117 |
padRight, padBottom); |
| 118 |
image_to_bitmap(dContext, image.get(), &bitmap); |
| 119 |
|
| 120 |
const SkSize size[] = { |
| 121 |
{ 50, 50, }, // shrink in both axes |
| 122 |
{ 50, 200, }, // shrink in X |
| 123 |
{ 200, 50, }, // shrink in Y |
| 124 |
{ 200, 200, }, |
| 125 |
}; |
| 126 |
|
| 127 |
canvas->drawImage(image, 10, 10); |
| 128 |
|
| 129 |
SkScalar x = SkIntToScalar(100); |
| 130 |
SkScalar y = SkIntToScalar(100); |
| 131 |
|
| 132 |
SkCanvas::Lattice lattice; |
| 133 |
lattice.fXCount = 4; |
| 134 |
lattice.fXDivs = xDivs + 1; |
| 135 |
lattice.fYCount = 4; |
| 136 |
lattice.fYDivs = yDivs + 1; |
| 137 |
lattice.fRectTypes = nullptr; |
| 138 |
lattice.fColors = nullptr; |
| 139 |
|
| 140 |
SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop, |
| 141 |
image->width() - padRight, image->height() - padBottom); |
| 142 |
lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ? |
| 143 |
nullptr : &bounds; |
| 144 |
|
| 145 |
for (int iy = 0; iy < 2; ++iy) { |
| 146 |
for (int ix = 0; ix < 2; ++ix) { |
| 147 |
int i = ix * 2 + iy; |
| 148 |
SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60, |
| 149 |
size[i].width(), size[i].height()); |
| 150 |
canvas->drawImageLattice(image.get(), lattice, r); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Provide hints about 3 solid color rects. These colors match |
| 155 |
// what was already in the bitmap. |
| 156 |
int fixedColorX[3] = {2, 4, 1}; |
| 157 |
int fixedColorY[3] = {1, 1, 2}; |
| 158 |
SkColor fixedColor[3] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK}; |
| 159 |
const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, |
| 160 |
kUnpremul_SkAlphaType); |
| 161 |
for (int rectNum = 0; rectNum < 3; rectNum++) { |
| 162 |
int srcX = xDivs[fixedColorX[rectNum]-1]; |
| 163 |
int srcY = yDivs[fixedColorY[rectNum]-1]; |
| 164 |
image->readPixels(dContext, info, &fixedColor[rectNum], 4, srcX, srcY); |
| 165 |
} |
| 166 |
|
| 167 |
// Include the degenerate first div. While normally the first patch is "scalable", |
| 168 |
// this will mean that the first non-degenerate patch is "fixed". |
| 169 |
lattice.fXCount = 5; |
| 170 |
lattice.fXDivs = xDivs; |
| 171 |
lattice.fYCount = 5; |
| 172 |
lattice.fYDivs = yDivs; |
| 173 |
|
| 174 |
// Let's skip a few rects. |
| 175 |
SkCanvas::Lattice::RectType flags[36]; |
| 176 |
sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::RectType)); |
| 177 |
flags[4] = SkCanvas::Lattice::kTransparent; |
| 178 |
flags[9] = SkCanvas::Lattice::kTransparent; |
| 179 |
flags[12] = SkCanvas::Lattice::kTransparent; |
| 180 |
flags[19] = SkCanvas::Lattice::kTransparent; |
| 181 |
for (int rectNum = 0; rectNum < 3; rectNum++) { |
| 182 |
flags[fixedColorY[rectNum]*6 + fixedColorX[rectNum]] |
| 183 |
= SkCanvas::Lattice::kFixedColor; |
| 184 |
} |
| 185 |
lattice.fRectTypes = flags; |
| 186 |
|
| 187 |
SkColor colors[36]; |
| 188 |
sk_bzero(colors, 36 * sizeof(SkColor)); |
| 189 |
for (int rectNum = 0; rectNum < 3; rectNum++) { |
| 190 |
colors[fixedColorY[rectNum]*6 + fixedColorX[rectNum]] |
| 191 |
= fixedColor[rectNum]; |
| 192 |
} |
| 193 |
|
| 194 |
lattice.fColors = colors; |
| 195 |
|
| 196 |
canvas->translate(400, 0); |
| 197 |
for (int iy = 0; iy < 2; ++iy) { |
| 198 |
for (int ix = 0; ix < 2; ++ix) { |
| 199 |
int i = ix * 2 + iy; |
| 200 |
SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60, |
| 201 |
size[i].width(), size[i].height()); |
| 202 |
canvas->drawImageLattice(image.get(), lattice, r); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
canvas->restore(); |
| 207 |
} |
| 208 |
|
| 209 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 210 |
#if defined(SK_GANESH) |
| 211 |
auto rContext = canvas->recordingContext(); |
| 212 |
auto dContext = GrAsDirectContext(rContext); |
| 213 |
if (rContext && !dContext) { |
| 214 |
*errorMsg = "not supported in ddl"; |
| 215 |
return DrawResult::kSkip; |
| 216 |
} |
| 217 |
#else |
| 218 |
constexpr GrDirectContext* dContext = nullptr; |
| 219 |
#endif |
| 220 |
this->onDrawHelper(dContext, canvas, 0, 0, 0, 0); |
| 221 |
canvas->translate(0.0f, 400.0f); |
| 222 |
this->onDrawHelper(dContext, canvas, 3, 7, 4, 11); |
| 223 |
return DrawResult::kOk; |
| 224 |
} |
| 225 |
|
| 226 |
private: |
| 227 |
using INHERITED = skiagm::GM; |
| 228 |
}; |
| 229 |
DEF_GM( return new LatticeGM; ) |
| 230 |
|
| 231 |
|
| 232 |
// LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles. |
| 233 |
class LatticeGM2 : public skiagm::GM { |
| 234 |
public: |
| 235 |
LatticeGM2() {} |
| 236 |
SkString getName() const override { return SkString("lattice2"); } |
| 237 |
|
| 238 |
SkISize getISize() override { return SkISize::Make(800, 800); } |
| 239 |
|
| 240 |
sk_sp<SkImage> makeImage(SkCanvas* root, int padLeft, int padTop, int padRight, int padBottom) { |
| 241 |
const int kSize = 80; |
| 242 |
auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom)); |
| 243 |
SkCanvas* canvas = surface->getCanvas(); |
| 244 |
SkPaint paint; |
| 245 |
paint.setAntiAlias(false); |
| 246 |
SkRect r; |
| 247 |
|
| 248 |
//first line |
| 249 |
r.setXYWH(0, 0, 4, 1); //4x1 green rect |
| 250 |
paint.setColor(0xFF00FF00); |
| 251 |
canvas->drawRect(r, paint); |
| 252 |
|
| 253 |
r.setXYWH(4, 0, 1, 1); //1x1 blue pixel -> draws as rectangle |
| 254 |
paint.setColor(0xFF0000FF); |
| 255 |
canvas->drawRect(r, paint); |
| 256 |
|
| 257 |
r.setXYWH(5, 0, kSize-5, 1); //the rest of the line is red |
| 258 |
paint.setColor(0xFFFF0000); |
| 259 |
canvas->drawRect(r, paint); |
| 260 |
|
| 261 |
|
| 262 |
//second line -> draws as fixed color rectangles |
| 263 |
r.setXYWH(0, 1, 4, 1); //4x1 red rect |
| 264 |
paint.setColor(0xFFFF0000); |
| 265 |
canvas->drawRect(r, paint); |
| 266 |
|
| 267 |
r.setXYWH(4, 1, 1, 1); //1x1 blue pixel with alpha |
| 268 |
paint.setColor(0x880000FF); |
| 269 |
canvas->drawRect(r, paint); |
| 270 |
|
| 271 |
r.setXYWH(5, 1, kSize-5, 1); //the rest of the line is green |
| 272 |
paint.setColor(0xFF00FF00); |
| 273 |
canvas->drawRect(r, paint); |
| 274 |
|
| 275 |
|
| 276 |
//third line - does not draw, because it is transparent |
| 277 |
r.setXYWH(0, 2, 4, kSize-2); //4x78 green rect |
| 278 |
paint.setColor(0xFF00FF00); |
| 279 |
canvas->drawRect(r, paint); |
| 280 |
|
| 281 |
r.setXYWH(4, 2, 1, kSize-2); //1x78 red pixel with alpha |
| 282 |
paint.setColor(0x88FF0000); |
| 283 |
canvas->drawRect(r, paint); |
| 284 |
|
| 285 |
r.setXYWH(5, 2, kSize-5, kSize-2); //the rest of the image is blue |
| 286 |
paint.setColor(0xFF0000FF); |
| 287 |
canvas->drawRect(r, paint); |
| 288 |
|
| 289 |
return surface->makeImageSnapshot(); |
| 290 |
} |
| 291 |
|
| 292 |
void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom, |
| 293 |
SkPaint& paint) { |
| 294 |
int xDivs[2] = {4, 5}; |
| 295 |
int yDivs[2] = {1, 2}; |
| 296 |
|
| 297 |
canvas->save(); |
| 298 |
|
| 299 |
sk_sp<SkImage> image = makeImage(canvas, padLeft, padTop, padRight, padBottom); |
| 300 |
|
| 301 |
canvas->drawImage(image, 10, 10); |
| 302 |
|
| 303 |
SkCanvas::Lattice lattice; |
| 304 |
lattice.fXCount = 2; |
| 305 |
lattice.fXDivs = xDivs; |
| 306 |
lattice.fYCount = 2; |
| 307 |
lattice.fYDivs = yDivs; |
| 308 |
lattice.fBounds = nullptr; |
| 309 |
|
| 310 |
SkCanvas::Lattice::RectType flags[9]; |
| 311 |
sk_bzero(flags, 9 * sizeof(SkCanvas::Lattice::RectType)); |
| 312 |
flags[3] = SkCanvas::Lattice::kFixedColor; |
| 313 |
flags[4] = SkCanvas::Lattice::kFixedColor; |
| 314 |
flags[5] = SkCanvas::Lattice::kFixedColor; |
| 315 |
|
| 316 |
flags[6] = SkCanvas::Lattice::kTransparent; |
| 317 |
flags[7] = SkCanvas::Lattice::kTransparent; |
| 318 |
flags[8] = SkCanvas::Lattice::kTransparent; |
| 319 |
lattice.fRectTypes = flags; |
| 320 |
|
| 321 |
SkColor colors[9] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK, |
| 322 |
0xFFFF0000, 0x880000FF, 0xFF00FF00, |
| 323 |
SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK}; |
| 324 |
lattice.fColors = colors; |
| 325 |
paint.setColor(0xFFFFFFFF); |
| 326 |
canvas->drawImageLattice(image.get(), lattice, |
| 327 |
SkRect::MakeXYWH(100, 100, 200, 200), |
| 328 |
SkFilterMode::kNearest, &paint); |
| 329 |
|
| 330 |
//draw the same content with alpha |
| 331 |
canvas->translate(400, 0); |
| 332 |
paint.setColor(0x80000FFF); |
| 333 |
canvas->drawImageLattice(image.get(), lattice, |
| 334 |
SkRect::MakeXYWH(100, 100, 200, 200), |
| 335 |
SkFilterMode::kNearest, &paint); |
| 336 |
|
| 337 |
canvas->restore(); |
| 338 |
} |
| 339 |
|
| 340 |
void onDraw(SkCanvas* canvas) override { |
| 341 |
|
| 342 |
//draw a rectangle in the background with transparent pixels |
| 343 |
SkPaint paint; |
| 344 |
paint.setColor(0x7F123456); |
| 345 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 346 |
canvas->drawRect( SkRect::MakeXYWH(300, 0, 300, 800), paint); |
| 347 |
|
| 348 |
//draw image lattice with kSrcOver blending |
| 349 |
paint.setBlendMode(SkBlendMode::kSrcOver); |
| 350 |
this->onDrawHelper(canvas, 0, 0, 0, 0, paint); |
| 351 |
|
| 352 |
//draw image lattice with kSrcATop blending |
| 353 |
canvas->translate(0.0f, 400.0f); |
| 354 |
paint.setBlendMode(SkBlendMode::kSrcATop); |
| 355 |
this->onDrawHelper(canvas, 0, 0, 0, 0, paint); |
| 356 |
} |
| 357 |
|
| 358 |
private: |
| 359 |
using INHERITED = skiagm::GM; |
| 360 |
}; |
| 361 |
DEF_GM( return new LatticeGM2; ) |
| 362 |
|
| 363 |
// Code paths that incorporate the paint color when drawing the lattice (using an alpha image) |
| 364 |
DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) { |
| 365 |
auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100)); |
| 366 |
surface->getCanvas()->clear(0); |
| 367 |
surface->getCanvas()->drawCircle(50, 50, 50, SkPaint()); |
| 368 |
auto image = surface->makeImageSnapshot(); |
| 369 |
|
| 370 |
int divs[] = { 20, 40, 60, 80 }; |
| 371 |
|
| 372 |
SkCanvas::Lattice lattice; |
| 373 |
lattice.fXCount = 4; |
| 374 |
lattice.fXDivs = divs; |
| 375 |
lattice.fYCount = 4; |
| 376 |
lattice.fYDivs = divs; |
| 377 |
lattice.fRectTypes = nullptr; |
| 378 |
lattice.fColors = nullptr; |
| 379 |
lattice.fBounds = nullptr; |
| 380 |
|
| 381 |
SkPaint paint; |
| 382 |
paint.setColor(SK_ColorMAGENTA); |
| 383 |
canvas->drawImageLattice(image.get(), lattice, SkRect::MakeWH(120, 120), |
| 384 |
SkFilterMode::kNearest, &paint); |
| 385 |
} |
| 386 |
|
| 387 |
static sk_sp<SkImage> make_symmetry_test_image() { |
| 388 |
auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(8, 8)); |
| 389 |
auto canvas = surface->getCanvas(); |
| 390 |
canvas->drawColor(SK_ColorBLUE); |
| 391 |
SkPaint p; |
| 392 |
p.setColor(SK_ColorGREEN); |
| 393 |
canvas->drawRect(SkRect::MakeXYWH(2, 2, 4, 4), p); |
| 394 |
return surface->makeImageSnapshot(); |
| 395 |
} |
| 396 |
|
| 397 |
// b/349428795 : A nine-patch should be able to have zero-sized regions on either end. Before |
| 398 |
// fixing the bug, it only worked on the left/top, leading to non-symmetric results in this GM. |
| 399 |
// Correct rendering is for each row to be symmetric. |
| 400 |
DEF_SIMPLE_GM(ninepatch_edge_case_349428795, canvas, 500, 150) { |
| 401 |
auto nine = make_symmetry_test_image(); |
| 402 |
|
| 403 |
for (int i = -1; i < 6; i++) { |
| 404 |
canvas->drawImageNine(nine.get(), |
| 405 |
SkIRect::MakeXYWH(i, 2, 4, 4), |
| 406 |
SkRect::MakeXYWH(i * 70 + 80, 10, 64, 64), |
| 407 |
SkFilterMode::kLinear); |
| 408 |
canvas->drawImageNine(nine.get(), |
| 409 |
SkIRect::MakeXYWH(2, i, 4, 4), |
| 410 |
SkRect::MakeXYWH(i * 70 + 80, 80, 64, 64), |
| 411 |
SkFilterMode::kLinear); |
| 412 |
} |
| 413 |
} |