2012-06-07 20:26:47 +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.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2014-06-27 12:49:53 +00:00
|
|
|
#include "SkCanvas.h"
|
2012-06-07 20:26:47 +00:00
|
|
|
#include "SkColorShader.h"
|
|
|
|
#include "SkGradientShader.h"
|
2013-08-29 11:54:56 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkTemplates.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2012-06-07 20:26:47 +00:00
|
|
|
|
2015-01-28 19:44:48 +00:00
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=448299
|
|
|
|
// Giant (inverse) matrix causes overflow when converting/computing using 32.32
|
|
|
|
// Before the fix, we would assert (and then crash).
|
|
|
|
static void test_big_grad(skiatest::Reporter* reporter) {
|
|
|
|
const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
|
|
|
|
const SkPoint pts[] = {{ 15, 14.7112684f }, { 0.709064007f, 12.6108112f }};
|
2015-08-27 14:41:13 +00:00
|
|
|
SkShader* s = SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2015-01-28 19:44:48 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(s)->unref();
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(2000, 1);
|
|
|
|
SkCanvas c(bm);
|
|
|
|
|
|
|
|
const SkScalar affine[] = {
|
|
|
|
1.06608627e-06f, 4.26434525e-07f, 6.2855f, 2.6611f, 273.4393f, 244.0046f
|
|
|
|
};
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setAffine(affine);
|
|
|
|
c.concat(matrix);
|
|
|
|
|
|
|
|
c.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
struct GradRec {
|
|
|
|
int fColorCount;
|
|
|
|
const SkColor* fColors;
|
|
|
|
const SkScalar* fPos;
|
|
|
|
const SkPoint* fPoint; // 2
|
|
|
|
const SkScalar* fRadius; // 2
|
|
|
|
SkShader::TileMode fTileMode;
|
|
|
|
|
|
|
|
void gradCheck(skiatest::Reporter* reporter, SkShader* shader,
|
|
|
|
SkShader::GradientInfo* info,
|
|
|
|
SkShader::GradientType gt) const {
|
|
|
|
SkAutoTMalloc<SkColor> colorStorage(fColorCount);
|
|
|
|
SkAutoTMalloc<SkScalar> posStorage(fColorCount);
|
|
|
|
|
|
|
|
info->fColorCount = fColorCount;
|
|
|
|
info->fColors = colorStorage;
|
|
|
|
info->fColorOffsets = posStorage.get();
|
|
|
|
REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
!memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
!memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
|
|
|
|
REPORTER_ASSERT(reporter, fTileMode == info->fTileMode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-11 18:50:03 +00:00
|
|
|
static void none_gradproc(skiatest::Reporter* reporter, const GradRec&) {
|
2014-07-03 14:26:01 +00:00
|
|
|
SkAutoTUnref<SkShader> s(SkShader::CreateEmptyShader());
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(nullptr));
|
2012-06-07 20:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
|
|
|
|
SkAutoTUnref<SkShader> s(new SkColorShader(rec.fColors[0]));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(nullptr));
|
2012-06-07 20:26:47 +00:00
|
|
|
|
|
|
|
SkShader::GradientInfo info;
|
2015-08-27 14:41:13 +00:00
|
|
|
info.fColors = nullptr;
|
2012-06-07 20:26:47 +00:00
|
|
|
info.fColorCount = 0;
|
|
|
|
s->asAGradient(&info);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == info.fColorCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
|
|
|
|
SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(rec.fPoint,
|
|
|
|
rec.fColors,
|
|
|
|
rec.fPos,
|
|
|
|
rec.fColorCount,
|
|
|
|
rec.fTileMode));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
SkShader::GradientInfo info;
|
|
|
|
rec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
|
|
|
|
SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial(rec.fPoint[0],
|
|
|
|
rec.fRadius[0],
|
|
|
|
rec.fColors,
|
|
|
|
rec.fPos,
|
|
|
|
rec.fColorCount,
|
|
|
|
rec.fTileMode));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
SkShader::GradientInfo info;
|
|
|
|
rec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
|
|
|
|
REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]);
|
|
|
|
REPORTER_ASSERT(reporter, info.fRadius[0] == rec.fRadius[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
|
|
|
|
SkAutoTUnref<SkShader> s(SkGradientShader::CreateSweep(rec.fPoint[0].fX,
|
|
|
|
rec.fPoint[0].fY,
|
|
|
|
rec.fColors,
|
|
|
|
rec.fPos,
|
|
|
|
rec.fColorCount));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
SkShader::GradientInfo info;
|
|
|
|
rec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
|
|
|
|
REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
|
|
|
|
SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointConical(rec.fPoint[0],
|
|
|
|
rec.fRadius[0],
|
|
|
|
rec.fPoint[1],
|
|
|
|
rec.fRadius[1],
|
|
|
|
rec.fColors,
|
|
|
|
rec.fPos,
|
|
|
|
rec.fColorCount,
|
|
|
|
rec.fTileMode));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
SkShader::GradientInfo info;
|
|
|
|
rec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar)));
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:52:06 +00:00
|
|
|
// Ensure that repeated color gradients behave like drawing a single color
|
2013-03-11 18:50:03 +00:00
|
|
|
static void TestConstantGradient(skiatest::Reporter*) {
|
2013-01-30 15:52:06 +00:00
|
|
|
const SkPoint pts[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(10), 0 }
|
|
|
|
};
|
|
|
|
SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
|
|
|
|
const SkScalar pos[] = { 0, SK_Scalar1 };
|
|
|
|
SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(pts,
|
|
|
|
colors,
|
|
|
|
pos,
|
|
|
|
2,
|
|
|
|
SkShader::kClamp_TileMode));
|
|
|
|
SkBitmap outBitmap;
|
2014-02-13 14:41:43 +00:00
|
|
|
outBitmap.allocN32Pixels(10, 1);
|
2013-01-30 15:52:06 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(s.get());
|
2014-02-13 14:41:43 +00:00
|
|
|
SkCanvas canvas(outBitmap);
|
2013-01-30 15:52:06 +00:00
|
|
|
canvas.drawPaint(paint);
|
|
|
|
SkAutoLockPixels alp(outBitmap);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
// The following is commented out because it currently fails
|
|
|
|
// Related bug: https://code.google.com/p/skia/issues/detail?id=1098
|
|
|
|
|
|
|
|
// REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&);
|
|
|
|
|
2013-01-30 15:52:06 +00:00
|
|
|
static void TestGradientShaders(skiatest::Reporter* reporter) {
|
2012-06-07 20:26:47 +00:00
|
|
|
static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
|
|
|
|
static const SkPoint gPts[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(10), SkIntToScalar(20) }
|
|
|
|
};
|
|
|
|
static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
|
|
|
|
|
|
|
|
GradRec rec;
|
|
|
|
rec.fColorCount = SK_ARRAY_COUNT(gColors);
|
|
|
|
rec.fColors = gColors;
|
|
|
|
rec.fPos = gPos;
|
|
|
|
rec.fPoint = gPts;
|
|
|
|
rec.fRadius = gRad;
|
|
|
|
rec.fTileMode = SkShader::kClamp_TileMode;
|
|
|
|
|
|
|
|
static const GradProc gProcs[] = {
|
|
|
|
none_gradproc,
|
|
|
|
color_gradproc,
|
|
|
|
linear_gradproc,
|
|
|
|
radial_gradproc,
|
|
|
|
sweep_gradproc,
|
|
|
|
conical_gradproc,
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-07 20:26:47 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
|
|
|
|
gProcs[i](reporter, rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Gradient, reporter) {
|
2013-01-30 15:52:06 +00:00
|
|
|
TestGradientShaders(reporter);
|
|
|
|
TestConstantGradient(reporter);
|
2015-01-28 19:44:48 +00:00
|
|
|
test_big_grad(reporter);
|
2013-01-30 15:52:06 +00:00
|
|
|
}
|