2016-08-12 13:26:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Fuzz.h"
|
|
|
|
#include "SkCanvas.h"
|
2017-02-09 16:31:32 +00:00
|
|
|
#include "SkCommonFlags.h"
|
2016-08-12 13:26:03 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkTLazy.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-11-01 19:01:12 +00:00
|
|
|
#include <vector>
|
2016-08-12 13:26:03 +00:00
|
|
|
|
|
|
|
const int MAX_COUNT = 400;
|
|
|
|
|
2016-11-01 19:01:12 +00:00
|
|
|
void makeMatrix(Fuzz* fuzz, SkMatrix* m) {
|
2016-11-10 21:17:49 +00:00
|
|
|
SkScalar mat[9];
|
|
|
|
fuzz->nextN(mat, 9);
|
|
|
|
m->set9(mat);
|
2016-08-12 13:26:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 19:01:12 +00:00
|
|
|
void initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors,
|
|
|
|
std::vector<SkScalar>* pos, SkShader::TileMode* mode) {
|
2016-11-10 21:17:49 +00:00
|
|
|
int count;
|
|
|
|
fuzz->nextRange(&count, 0, MAX_COUNT);
|
2016-08-12 13:26:03 +00:00
|
|
|
|
2016-11-10 21:17:49 +00:00
|
|
|
// Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
|
|
|
|
// smaller, which leads to more efficient fuzzing.
|
|
|
|
uint8_t m;
|
|
|
|
fuzz->nextRange(&m, 0, 2);
|
|
|
|
*mode = static_cast<SkShader::TileMode>(m);
|
2016-08-12 13:26:03 +00:00
|
|
|
|
2016-10-25 13:11:05 +00:00
|
|
|
colors->clear();
|
|
|
|
pos ->clear();
|
2016-11-01 19:01:12 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2016-11-10 21:17:49 +00:00
|
|
|
SkColor c;
|
|
|
|
SkScalar s;
|
|
|
|
fuzz->next(&c, &s);
|
|
|
|
colors->push_back(c);
|
|
|
|
pos ->push_back(s);
|
2016-10-25 13:11:05 +00:00
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
std::sort(pos->begin(), pos->end());
|
|
|
|
// The order matters. If count == 1, we want pos == 0.
|
|
|
|
(*pos)[count - 1] = 1;
|
|
|
|
(*pos)[0] = 0;
|
2016-08-12 13:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:31:32 +00:00
|
|
|
static void logOptionalMatrix(const char* label, const SkMatrix* m) {
|
|
|
|
if (!m) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF(" %s: [ ", label);
|
2017-02-09 16:31:32 +00:00
|
|
|
for (int i = 0; i < 9; ++i) {
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF("%.9g ", m->get(i));
|
2017-02-09 16:31:32 +00:00
|
|
|
}
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF("]\n");
|
2017-02-09 16:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void logLinearGradient(const SkPoint pts[2],
|
|
|
|
const std::vector<SkColor>& colors,
|
|
|
|
const std::vector<SkScalar> pos,
|
|
|
|
SkShader::TileMode mode,
|
|
|
|
uint32_t flags,
|
|
|
|
const SkMatrix* localMatrix,
|
|
|
|
const SkMatrix* globalMatrix) {
|
|
|
|
if (!FLAGS_verbose) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkDebugf("--- fuzzLinearGradient ---\n");
|
2017-02-09 20:05:15 +00:00
|
|
|
SkDebugf(" pts:\t\t[ (%.9g %.9g) (%.9g %.9g) ]\n",
|
2017-02-09 16:31:32 +00:00
|
|
|
pts[0].x(), pts[0].y(), pts[1].x(), pts[1].y());
|
|
|
|
SkDebugf(" colors:\t[ ");
|
|
|
|
for (auto color : colors) {
|
|
|
|
SkDebugf("0x%x ", color);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkDebugf("]\n pos:\t\t");
|
|
|
|
if (pos.empty()) {
|
|
|
|
SkDebugf("nullptr");
|
|
|
|
} else {
|
|
|
|
SkDebugf("[ ");
|
|
|
|
for (auto p : pos) {
|
|
|
|
SkDebugf("%f ", p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkDebugf("]\n");
|
|
|
|
|
|
|
|
static const char* gModeName[] = {
|
|
|
|
"kClamp_TileMode", "kRepeat_TileMode", "kMirror_TileMode"
|
|
|
|
};
|
|
|
|
SkASSERT(mode < SK_ARRAY_COUNT(gModeName));
|
|
|
|
SkDebugf(" mode:\t\t%s\n", gModeName[mode]);
|
|
|
|
SkDebugf(" flags:\t0x%x\n", flags);
|
|
|
|
logOptionalMatrix("local matrix", localMatrix);
|
|
|
|
logOptionalMatrix("global matrix", globalMatrix);
|
|
|
|
}
|
|
|
|
|
2016-08-12 13:26:03 +00:00
|
|
|
void fuzzLinearGradient(Fuzz* fuzz) {
|
2016-11-10 21:17:49 +00:00
|
|
|
SkPoint pts[2];
|
|
|
|
fuzz->next(&pts[0].fX, &pts[0].fY, &pts[1].fX, &pts[1].fY);
|
|
|
|
bool useLocalMatrix, useGlobalMatrix;
|
|
|
|
fuzz->next(&useLocalMatrix, &useGlobalMatrix);
|
2016-11-01 19:01:12 +00:00
|
|
|
|
|
|
|
std::vector<SkColor> colors;
|
|
|
|
std::vector<SkScalar> pos;
|
|
|
|
SkShader::TileMode mode;
|
|
|
|
initGradientParams(fuzz, &colors, &pos, &mode);
|
|
|
|
|
|
|
|
SkPaint p;
|
2016-11-10 21:17:49 +00:00
|
|
|
uint32_t flags;
|
|
|
|
fuzz->next(&flags);
|
2016-11-01 19:01:12 +00:00
|
|
|
|
|
|
|
SkTLazy<SkMatrix> localMatrix;
|
|
|
|
if (useLocalMatrix) {
|
|
|
|
makeMatrix(fuzz, localMatrix.init());
|
|
|
|
}
|
|
|
|
p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(),
|
|
|
|
colors.size(), mode, flags, localMatrix.getMaybeNull()));
|
|
|
|
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
|
|
|
|
if (useGlobalMatrix) {
|
|
|
|
SkMatrix gm;
|
|
|
|
makeMatrix(fuzz, &gm);
|
2017-02-09 16:31:32 +00:00
|
|
|
logLinearGradient(pts, colors, pos, mode, flags, localMatrix.getMaybeNull(), &gm);
|
2016-11-01 19:01:12 +00:00
|
|
|
SkCanvas* c = surface->getCanvas();
|
|
|
|
c->setMatrix(gm);
|
|
|
|
c->drawPaint(p);
|
|
|
|
} else {
|
2017-02-09 16:31:32 +00:00
|
|
|
logLinearGradient(pts, colors, pos, mode, flags, localMatrix.getMaybeNull(), nullptr);
|
2016-11-01 19:01:12 +00:00
|
|
|
surface->getCanvas()->drawPaint(p);
|
|
|
|
}
|
2016-08-12 13:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuzzRadialGradient(Fuzz* fuzz) {
|
2016-11-10 21:17:49 +00:00
|
|
|
SkPoint center;
|
|
|
|
fuzz->next(¢er.fX, ¢er.fY);
|
|
|
|
SkScalar radius;
|
|
|
|
bool useLocalMatrix, useGlobalMatrix;
|
|
|
|
fuzz->next(&radius, &useLocalMatrix, &useGlobalMatrix);
|
2016-08-12 13:26:03 +00:00
|
|
|
|
|
|
|
|
2016-11-01 19:01:12 +00:00
|
|
|
std::vector<SkColor> colors;
|
|
|
|
std::vector<SkScalar> pos;
|
|
|
|
SkShader::TileMode mode;
|
|
|
|
initGradientParams(fuzz, &colors, &pos, &mode);
|
2016-08-12 13:26:03 +00:00
|
|
|
|
2016-11-01 19:01:12 +00:00
|
|
|
SkPaint p;
|
2016-11-10 21:17:49 +00:00
|
|
|
uint32_t flags;
|
|
|
|
fuzz->next(&flags);
|
2016-08-12 13:26:03 +00:00
|
|
|
|
2016-11-01 19:01:12 +00:00
|
|
|
SkTLazy<SkMatrix> localMatrix;
|
|
|
|
if (useLocalMatrix) {
|
|
|
|
makeMatrix(fuzz, localMatrix.init());
|
|
|
|
}
|
|
|
|
p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(),
|
|
|
|
pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull()));
|
|
|
|
|
|
|
|
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
|
|
|
|
if (useGlobalMatrix) {
|
|
|
|
SkMatrix gm;
|
|
|
|
makeMatrix(fuzz, &gm);
|
|
|
|
SkCanvas* c = surface->getCanvas();
|
|
|
|
c->setMatrix(gm);
|
|
|
|
c->drawPaint(p);
|
|
|
|
} else {
|
|
|
|
surface->getCanvas()->drawPaint(p);
|
|
|
|
}
|
2016-08-12 13:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuzzTwoPointConicalGradient(Fuzz* fuzz) {
|
2016-11-10 21:17:49 +00:00
|
|
|
SkPoint start;
|
|
|
|
fuzz->next(&start.fX, &start.fY);
|
|
|
|
SkPoint end;
|
|
|
|
fuzz->next(&end.fX, &end.fY);
|
|
|
|
SkScalar startRadius, endRadius;
|
|
|
|
bool useLocalMatrix, useGlobalMatrix;
|
|
|
|
fuzz->next(&startRadius, &endRadius, &useLocalMatrix, &useGlobalMatrix);
|
2016-11-01 19:01:12 +00:00
|
|
|
|
|
|
|
std::vector<SkColor> colors;
|
|
|
|
std::vector<SkScalar> pos;
|
|
|
|
SkShader::TileMode mode;
|
|
|
|
initGradientParams(fuzz, &colors, &pos, &mode);
|
|
|
|
|
|
|
|
SkPaint p;
|
2016-11-10 21:17:49 +00:00
|
|
|
uint32_t flags;
|
|
|
|
fuzz->next(&flags);
|
2016-11-01 19:01:12 +00:00
|
|
|
|
|
|
|
SkTLazy<SkMatrix> localMatrix;
|
|
|
|
if (useLocalMatrix) {
|
|
|
|
makeMatrix(fuzz, localMatrix.init());
|
|
|
|
}
|
|
|
|
p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius,
|
|
|
|
end, endRadius, colors.data(), pos.data(), colors.size(), mode,
|
|
|
|
flags, localMatrix.getMaybeNull()));
|
|
|
|
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
|
|
|
|
if (useGlobalMatrix) {
|
|
|
|
SkMatrix gm;
|
|
|
|
makeMatrix(fuzz, &gm);
|
|
|
|
SkCanvas* c = surface->getCanvas();
|
|
|
|
c->setMatrix(gm);
|
|
|
|
c->drawPaint(p);
|
|
|
|
} else {
|
|
|
|
surface->getCanvas()->drawPaint(p);
|
|
|
|
}
|
2016-08-12 13:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuzzSweepGradient(Fuzz* fuzz) {
|
2016-11-10 21:17:49 +00:00
|
|
|
SkScalar cx, cy;
|
|
|
|
bool useLocalMatrix, useGlobalMatrix;
|
|
|
|
fuzz->next(&cx, &cy, &useLocalMatrix, &useGlobalMatrix);
|
2016-11-01 19:01:12 +00:00
|
|
|
|
|
|
|
std::vector<SkColor> colors;
|
|
|
|
std::vector<SkScalar> pos;
|
|
|
|
SkShader::TileMode mode;
|
|
|
|
initGradientParams(fuzz, &colors, &pos, &mode);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
if (useLocalMatrix) {
|
|
|
|
SkMatrix m;
|
|
|
|
makeMatrix(fuzz, &m);
|
2016-11-10 21:17:49 +00:00
|
|
|
uint32_t flags;
|
|
|
|
fuzz->next(&flags);
|
2016-11-01 19:01:12 +00:00
|
|
|
|
|
|
|
p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
|
|
|
|
pos.data(), colors.size(), flags, &m));
|
|
|
|
} else {
|
|
|
|
p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
|
|
|
|
pos.data(), colors.size()));
|
|
|
|
}
|
2016-08-12 13:26:03 +00:00
|
|
|
|
2016-11-01 19:01:12 +00:00
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
|
|
|
|
if (useGlobalMatrix) {
|
|
|
|
SkMatrix gm;
|
|
|
|
makeMatrix(fuzz, &gm);
|
|
|
|
SkCanvas* c = surface->getCanvas();
|
|
|
|
c->setMatrix(gm);
|
|
|
|
c->drawPaint(p);
|
|
|
|
} else {
|
|
|
|
surface->getCanvas()->drawPaint(p);
|
|
|
|
}
|
2016-08-12 13:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_FUZZ(Gradients, fuzz) {
|
2016-11-10 21:17:49 +00:00
|
|
|
uint8_t i;
|
|
|
|
fuzz->next(&i);
|
2016-08-12 13:26:03 +00:00
|
|
|
|
|
|
|
switch(i) {
|
|
|
|
case 0:
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF("LinearGradient\n");
|
2016-08-12 13:26:03 +00:00
|
|
|
fuzzLinearGradient(fuzz);
|
|
|
|
return;
|
|
|
|
case 1:
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF("RadialGradient\n");
|
2016-08-12 13:26:03 +00:00
|
|
|
fuzzRadialGradient(fuzz);
|
|
|
|
return;
|
|
|
|
case 2:
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF("TwoPointConicalGradient\n");
|
2016-08-12 13:26:03 +00:00
|
|
|
fuzzTwoPointConicalGradient(fuzz);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 16:43:39 +00:00
|
|
|
SkDEBUGF("SweepGradient\n");
|
2016-08-12 13:26:03 +00:00
|
|
|
fuzzSweepGradient(fuzz);
|
|
|
|
return;
|
|
|
|
}
|