| 1 |
/* |
| 2 |
* Copyright 2015 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 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 8 |
|
| 9 |
#if defined(GPU_TEST_UTILS) |
| 10 |
|
| 11 |
#include "include/core/SkAlphaType.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPath.h" |
| 16 |
#include "include/core/SkPathBuilder.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRRect.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/private/base/SkMacros.h" |
| 21 |
#include "include/private/base/SkOnce.h" |
| 22 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 23 |
#include "src/base/SkRandom.h" |
| 24 |
#include "src/core/SkRectPriv.h" |
| 25 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 26 |
#include "src/gpu/ganesh/GrColorSpaceXform.h" |
| 27 |
#include "src/gpu/ganesh/GrFPArgs.h" |
| 28 |
#include "src/gpu/ganesh/GrProcessorUnitTest.h" |
| 29 |
#include "src/gpu/ganesh/GrStyle.h" |
| 30 |
#include "src/utils/SkDashPathPriv.h" |
| 31 |
|
| 32 |
#include <array> |
| 33 |
#include <cstring> |
| 34 |
#include <optional> |
| 35 |
#include <utility> |
| 36 |
|
| 37 |
static const SkMatrix& test_matrix(SkRandom* random, |
| 38 |
bool includeNonPerspective, |
| 39 |
bool includePerspective) { |
| 40 |
static SkMatrix gMatrices[5]; |
| 41 |
static const int kPerspectiveCount = 1; |
| 42 |
static bool gOnce; |
| 43 |
if (!gOnce) { |
| 44 |
gOnce = true; |
| 45 |
gMatrices[0].reset(); |
| 46 |
gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100)); |
| 47 |
gMatrices[2].setRotate(SkIntToScalar(17)); |
| 48 |
gMatrices[3].setRotate(SkIntToScalar(185)); |
| 49 |
gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33)); |
| 50 |
gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf); |
| 51 |
|
| 52 |
// Perspective matrices |
| 53 |
gMatrices[4].setRotate(SkIntToScalar(215)); |
| 54 |
gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f); |
| 55 |
gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f); |
| 56 |
} |
| 57 |
|
| 58 |
uint32_t count = static_cast<uint32_t>(std::size(gMatrices)); |
| 59 |
if (includeNonPerspective && includePerspective) { |
| 60 |
return gMatrices[random->nextULessThan(count)]; |
| 61 |
} else if (!includeNonPerspective) { |
| 62 |
return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)]; |
| 63 |
} else { |
| 64 |
SkASSERT(includeNonPerspective && !includePerspective); |
| 65 |
return gMatrices[random->nextULessThan(count - kPerspectiveCount)]; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
namespace GrTest { |
| 70 |
const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); } |
| 71 |
|
| 72 |
const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) { |
| 73 |
static SkMatrix gMatrices[5]; |
| 74 |
static bool gOnce; |
| 75 |
if (!gOnce) { |
| 76 |
gOnce = true; |
| 77 |
// identity |
| 78 |
gMatrices[0].reset(); |
| 79 |
// translation |
| 80 |
gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100)); |
| 81 |
// scale |
| 82 |
gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17)); |
| 83 |
// scale + translation |
| 84 |
gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17)); |
| 85 |
gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33)); |
| 86 |
// orthogonal basis vectors |
| 87 |
gMatrices[4].reset(); |
| 88 |
gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1)); |
| 89 |
gMatrices[4].setRotate(47); |
| 90 |
|
| 91 |
for (size_t i = 0; i < std::size(gMatrices); i++) { |
| 92 |
SkASSERT(gMatrices[i].preservesRightAngles()); |
| 93 |
} |
| 94 |
} |
| 95 |
return gMatrices[random->nextULessThan(static_cast<uint32_t>(std::size(gMatrices)))]; |
| 96 |
} |
| 97 |
|
| 98 |
const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) { |
| 99 |
static SkMatrix gMatrices[6]; |
| 100 |
static bool gOnce; |
| 101 |
if (!gOnce) { |
| 102 |
gOnce = true; |
| 103 |
// identity |
| 104 |
gMatrices[0].reset(); |
| 105 |
// translation |
| 106 |
gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100)); |
| 107 |
// scale |
| 108 |
gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17)); |
| 109 |
// scale + translation |
| 110 |
gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17)); |
| 111 |
gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33)); |
| 112 |
// reflection |
| 113 |
gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1)); |
| 114 |
// 90 degress rotation |
| 115 |
gMatrices[5].setRotate(90); |
| 116 |
|
| 117 |
for (size_t i = 0; i < std::size(gMatrices); i++) { |
| 118 |
SkASSERT(gMatrices[i].rectStaysRect()); |
| 119 |
} |
| 120 |
} |
| 121 |
return gMatrices[random->nextULessThan(static_cast<uint32_t>(std::size(gMatrices)))]; |
| 122 |
} |
| 123 |
|
| 124 |
const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); } |
| 125 |
const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); } |
| 126 |
|
| 127 |
void TestWrapModes(SkRandom* random, GrSamplerState::WrapMode wrapModes[2]) { |
| 128 |
static const GrSamplerState::WrapMode kWrapModes[] = { |
| 129 |
GrSamplerState::WrapMode::kClamp, |
| 130 |
GrSamplerState::WrapMode::kRepeat, |
| 131 |
GrSamplerState::WrapMode::kMirrorRepeat, |
| 132 |
}; |
| 133 |
wrapModes[0] = kWrapModes[random->nextULessThan(std::size(kWrapModes))]; |
| 134 |
wrapModes[1] = kWrapModes[random->nextULessThan(std::size(kWrapModes))]; |
| 135 |
} |
| 136 |
const SkRect& TestRect(SkRandom* random) { |
| 137 |
static SkRect gRects[7]; |
| 138 |
static bool gOnce; |
| 139 |
if (!gOnce) { |
| 140 |
gOnce = true; |
| 141 |
gRects[0] = SkRect::MakeWH(1.f, 1.f); |
| 142 |
gRects[1] = SkRect::MakeWH(1.0f, 256.0f); |
| 143 |
gRects[2] = SkRect::MakeWH(256.0f, 1.0f); |
| 144 |
gRects[3] = SkRectPriv::MakeLargest(); |
| 145 |
gRects[4] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f); |
| 146 |
gRects[5] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f); |
| 147 |
} |
| 148 |
return gRects[random->nextULessThan(static_cast<uint32_t>(std::size(gRects)))]; |
| 149 |
} |
| 150 |
|
| 151 |
// Just some simple rects for code which expects its input very sanitized |
| 152 |
const SkRect& TestSquare(SkRandom* random) { |
| 153 |
static SkRect gRects[2]; |
| 154 |
static bool gOnce; |
| 155 |
if (!gOnce) { |
| 156 |
gOnce = true; |
| 157 |
gRects[0] = SkRect::MakeWH(128.f, 128.f); |
| 158 |
gRects[1] = SkRect::MakeWH(256.0f, 256.0f); |
| 159 |
} |
| 160 |
return gRects[random->nextULessThan(static_cast<uint32_t>(std::size(gRects)))]; |
| 161 |
} |
| 162 |
|
| 163 |
const SkRRect& TestRRectSimple(SkRandom* random) { |
| 164 |
static SkRRect gRRect[2]; |
| 165 |
static bool gOnce; |
| 166 |
if (!gOnce) { |
| 167 |
gOnce = true; |
| 168 |
SkRect rectangle = SkRect::MakeWH(10.f, 20.f); |
| 169 |
// true round rect with circular corners |
| 170 |
gRRect[0].setRectXY(rectangle, 1.f, 1.f); |
| 171 |
// true round rect with elliptical corners |
| 172 |
gRRect[1].setRectXY(rectangle, 2.0f, 1.0f); |
| 173 |
|
| 174 |
for (size_t i = 0; i < std::size(gRRect); i++) { |
| 175 |
SkASSERT(gRRect[i].isSimple()); |
| 176 |
} |
| 177 |
} |
| 178 |
return gRRect[random->nextULessThan(static_cast<uint32_t>(std::size(gRRect)))]; |
| 179 |
} |
| 180 |
|
| 181 |
const SkPath& TestPath(SkRandom* random) { |
| 182 |
static SkPath gPath[7]; |
| 183 |
static bool gOnce; |
| 184 |
if (!gOnce) { |
| 185 |
gOnce = true; |
| 186 |
// line |
| 187 |
gPath[0] = SkPathBuilder().moveTo(0.f, 0.f) |
| 188 |
.lineTo(10.f, 10.f) |
| 189 |
.detach(); |
| 190 |
// quad |
| 191 |
gPath[1] = SkPathBuilder().moveTo(0.f, 0.f) |
| 192 |
.quadTo(10.f, 10.f, 20.f, 20.f) |
| 193 |
.detach(); |
| 194 |
// conic |
| 195 |
gPath[2] = SkPathBuilder().moveTo(0.f, 0.f) |
| 196 |
.conicTo(10.f, 10.f, 20.f, 20.f, 1.f) |
| 197 |
.detach(); |
| 198 |
// cubic |
| 199 |
gPath[3] = SkPathBuilder().moveTo(0.f, 0.f) |
| 200 |
.cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f) |
| 201 |
.detach(); |
| 202 |
// all three |
| 203 |
gPath[4] = SkPathBuilder().moveTo(0.f, 0.f) |
| 204 |
.lineTo(10.f, 10.f) |
| 205 |
.quadTo(10.f, 10.f, 20.f, 20.f) |
| 206 |
.conicTo(10.f, 10.f, 20.f, 20.f, 1.f) |
| 207 |
.cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f) |
| 208 |
.detach(); |
| 209 |
// convex |
| 210 |
gPath[5] = SkPathBuilder().moveTo(0.0f, 0.0f) |
| 211 |
.lineTo(10.0f, 0.0f) |
| 212 |
.lineTo(10.0f, 10.0f) |
| 213 |
.lineTo(0.0f, 10.0f) |
| 214 |
.close() |
| 215 |
.detach(); |
| 216 |
// concave |
| 217 |
gPath[6] = SkPathBuilder().moveTo(0.0f, 0.0f) |
| 218 |
.lineTo(5.0f, 5.0f) |
| 219 |
.lineTo(10.0f, 0.0f) |
| 220 |
.lineTo(10.0f, 10.0f) |
| 221 |
.lineTo(0.0f, 10.0f) |
| 222 |
.close() |
| 223 |
.detach(); |
| 224 |
} |
| 225 |
|
| 226 |
return gPath[random->nextULessThan(static_cast<uint32_t>(std::size(gPath)))]; |
| 227 |
} |
| 228 |
|
| 229 |
const SkPath& TestPathConvex(SkRandom* random) { |
| 230 |
static SkPath gPath[3]; |
| 231 |
static bool gOnce; |
| 232 |
if (!gOnce) { |
| 233 |
gOnce = true; |
| 234 |
// narrow rect |
| 235 |
gPath[0] = SkPath::Polygon({{{-1.5f, -50.0f}, |
| 236 |
{-1.5f, -50.0f}, |
| 237 |
{ 1.5f, -50.0f}, |
| 238 |
{ 1.5f, 50.0f}, |
| 239 |
{-1.5f, 50.0f}}}, false); |
| 240 |
// degenerate |
| 241 |
gPath[1] = SkPath::Polygon({{{-0.025f, -0.025f}, |
| 242 |
{-0.025f, -0.025f}, |
| 243 |
{ 0.025f, -0.025f}, |
| 244 |
{ 0.025f, 0.025f}, |
| 245 |
{-0.025f, 0.025f}}}, false); |
| 246 |
// clipped triangle |
| 247 |
gPath[2] = SkPath::Polygon({{{-10.0f, -50.0f}, |
| 248 |
{-10.0f, -50.0f}, |
| 249 |
{ 10.0f, -50.0f}, |
| 250 |
{ 50.0f, 31.0f}, |
| 251 |
{ 40.0f, 50.0f}, |
| 252 |
{-40.0f, 50.0f}, |
| 253 |
{-50.0f, 31.0f}}}, false); |
| 254 |
|
| 255 |
for (size_t i = 0; i < std::size(gPath); i++) { |
| 256 |
SkASSERT(gPath[i].isConvex()); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return gPath[random->nextULessThan(static_cast<uint32_t>(std::size(gPath)))]; |
| 261 |
} |
| 262 |
|
| 263 |
static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) { |
| 264 |
bool strokeAndFill = random->nextBool(); |
| 265 |
SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f; |
| 266 |
rec->setStrokeStyle(strokeWidth, strokeAndFill); |
| 267 |
|
| 268 |
SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount)); |
| 269 |
SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount)); |
| 270 |
SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f); |
| 271 |
rec->setStrokeParams(cap, join, miterLimit); |
| 272 |
} |
| 273 |
|
| 274 |
SkStrokeRec TestStrokeRec(SkRandom* random) { |
| 275 |
SkStrokeRec::InitStyle style = |
| 276 |
SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1)); |
| 277 |
SkStrokeRec rec(style); |
| 278 |
randomize_stroke_rec(&rec, random); |
| 279 |
return rec; |
| 280 |
} |
| 281 |
|
| 282 |
void TestStyle(SkRandom* random, GrStyle* style) { |
| 283 |
SkStrokeRec::InitStyle initStyle = |
| 284 |
SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1)); |
| 285 |
SkStrokeRec stroke(initStyle); |
| 286 |
randomize_stroke_rec(&stroke, random); |
| 287 |
sk_sp<SkPathEffect> pe; |
| 288 |
if (random->nextBool()) { |
| 289 |
size_t cnt = random->nextRangeU(1, 50) * 2; |
| 290 |
std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]); |
| 291 |
SkScalar sum = 0; |
| 292 |
for (size_t i = 0; i < cnt; i++) { |
| 293 |
intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01), |
| 294 |
SkDoubleToScalar(10.0)); |
| 295 |
sum += intervals[i]; |
| 296 |
} |
| 297 |
SkScalar phase = random->nextRangeScalar(0, sum); |
| 298 |
pe = TestDashPathEffect::Make({intervals.get(), cnt}, phase); |
| 299 |
} |
| 300 |
*style = GrStyle(stroke, std::move(pe)); |
| 301 |
} |
| 302 |
|
| 303 |
TestDashPathEffect::TestDashPathEffect(SkSpan<const SkScalar> intervals, SkScalar phase) { |
| 304 |
fIntervals.reset(intervals.size()); |
| 305 |
memcpy(fIntervals.get(), intervals.data(), intervals.size_bytes()); |
| 306 |
SkDashPath::CalcDashParameters(phase, fIntervals, &fInitialDashLength, |
| 307 |
&fInitialDashIndex, &fIntervalLength, &fPhase); |
| 308 |
} |
| 309 |
|
| 310 |
bool TestDashPathEffect::onFilterPath(SkPathBuilder* dst, const SkPath& src, SkStrokeRec* rec, |
| 311 |
const SkRect* cullRect, const SkMatrix&) const { |
| 312 |
return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals, |
| 313 |
fInitialDashLength, fInitialDashIndex, fIntervalLength, |
| 314 |
fPhase); |
| 315 |
} |
| 316 |
|
| 317 |
std::optional<SkPathEffectBase::DashInfo> TestDashPathEffect::asADash() const { |
| 318 |
return {{fIntervals, fPhase}}; |
| 319 |
} |
| 320 |
|
| 321 |
sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) { |
| 322 |
static SkColorSpace* gColorSpaces[3]; |
| 323 |
static SkOnce once; |
| 324 |
once([] { |
| 325 |
// No color space (legacy mode) |
| 326 |
gColorSpaces[0] = nullptr; |
| 327 |
// sRGB or color-spin sRGB |
| 328 |
gColorSpaces[1] = SkColorSpace::MakeSRGB().release(); |
| 329 |
gColorSpaces[2] = SkColorSpace::MakeSRGB()->makeColorSpin().release(); |
| 330 |
}); |
| 331 |
return sk_ref_sp( |
| 332 |
gColorSpaces[random->nextULessThan(static_cast<uint32_t>(std::size(gColorSpaces)))]); |
| 333 |
} |
| 334 |
|
| 335 |
sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) { |
| 336 |
// TODO: Add many more kinds of xforms here |
| 337 |
static GrColorSpaceXform* gXforms[3]; |
| 338 |
static SkOnce once; |
| 339 |
once([] { |
| 340 |
sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB(); |
| 341 |
sk_sp<SkColorSpace> spin = SkColorSpace::MakeSRGB()->makeColorSpin(); |
| 342 |
// No gamut change |
| 343 |
gXforms[0] = nullptr; |
| 344 |
gXforms[1] = GrColorSpaceXform::Make( |
| 345 |
srgb.get(), kPremul_SkAlphaType, spin.get(), kPremul_SkAlphaType) |
| 346 |
.release(); |
| 347 |
gXforms[2] = GrColorSpaceXform::Make( |
| 348 |
spin.get(), kPremul_SkAlphaType, srgb.get(), kPremul_SkAlphaType) |
| 349 |
.release(); |
| 350 |
}); |
| 351 |
return sk_ref_sp(gXforms[random->nextULessThan(static_cast<uint32_t>(std::size(gXforms)))]); |
| 352 |
} |
| 353 |
|
| 354 |
TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d) |
| 355 |
: fColorInfoStorage(std::make_unique<GrColorInfo>( |
| 356 |
GrColorType::kRGBA_8888, kPremul_SkAlphaType, TestColorSpace(d->fRandom))) |
| 357 |
, fArgs(d->surfaceDrawContext(), fColorInfoStorage.get(), fSurfaceProps, |
| 358 |
GrFPArgs::Scope::kDefault) {} |
| 359 |
|
| 360 |
TestAsFPArgs::~TestAsFPArgs() {} |
| 361 |
|
| 362 |
GrColor RandomColor(SkRandom* random) { |
| 363 |
// There are only a few cases of random colors which interest us |
| 364 |
enum ColorMode { |
| 365 |
kAllOnes_ColorMode, |
| 366 |
kAllZeros_ColorMode, |
| 367 |
kAlphaOne_ColorMode, |
| 368 |
kRandom_ColorMode, |
| 369 |
kLast_ColorMode = kRandom_ColorMode |
| 370 |
}; |
| 371 |
|
| 372 |
ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1)); |
| 373 |
GrColor color SK_INIT_TO_AVOID_WARNING; |
| 374 |
switch (colorMode) { |
| 375 |
case kAllOnes_ColorMode: |
| 376 |
color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF); |
| 377 |
break; |
| 378 |
case kAllZeros_ColorMode: |
| 379 |
color = GrColorPackRGBA(0, 0, 0, 0); |
| 380 |
break; |
| 381 |
case kAlphaOne_ColorMode: |
| 382 |
color = GrColorPackRGBA(random->nextULessThan(256), |
| 383 |
random->nextULessThan(256), |
| 384 |
random->nextULessThan(256), |
| 385 |
0xFF); |
| 386 |
break; |
| 387 |
case kRandom_ColorMode: { |
| 388 |
uint8_t alpha = random->nextULessThan(256); |
| 389 |
color = GrColorPackRGBA(random->nextRangeU(0, alpha), |
| 390 |
random->nextRangeU(0, alpha), |
| 391 |
random->nextRangeU(0, alpha), |
| 392 |
alpha); |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
return color; |
| 397 |
} |
| 398 |
|
| 399 |
uint8_t RandomCoverage(SkRandom* random) { |
| 400 |
enum CoverageMode { |
| 401 |
kZero_CoverageMode, |
| 402 |
kAllOnes_CoverageMode, |
| 403 |
kRandom_CoverageMode, |
| 404 |
kLast_CoverageMode = kRandom_CoverageMode |
| 405 |
}; |
| 406 |
|
| 407 |
CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1)); |
| 408 |
uint8_t coverage SK_INIT_TO_AVOID_WARNING; |
| 409 |
switch (colorMode) { |
| 410 |
case kZero_CoverageMode: |
| 411 |
coverage = 0; |
| 412 |
break; |
| 413 |
case kAllOnes_CoverageMode: |
| 414 |
coverage = 0xff; |
| 415 |
break; |
| 416 |
case kRandom_CoverageMode: |
| 417 |
coverage = random->nextULessThan(256); |
| 418 |
break; |
| 419 |
} |
| 420 |
return coverage; |
| 421 |
} |
| 422 |
|
| 423 |
} // namespace GrTest |
| 424 |
|
| 425 |
#endif |