2011-12-09 15:48:03 +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
|
|
|
|
2011-12-09 15:48:03 +00:00
|
|
|
#include "SkColorShader.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "Test.h"
|
2011-12-09 15:48:03 +00:00
|
|
|
|
|
|
|
static void test_bitmap(skiatest::Reporter* reporter) {
|
2014-02-13 14:41:43 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
|
|
|
|
|
2011-12-09 15:48:03 +00:00
|
|
|
SkBitmap bmp;
|
2014-02-13 14:41:43 +00:00
|
|
|
bmp.setConfig(info);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-12-09 15:48:03 +00:00
|
|
|
// test 1: bitmap without pixel data
|
2012-08-23 18:14:13 +00:00
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bmp,
|
2011-12-09 15:48:03 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
REPORTER_ASSERT(reporter, shader);
|
|
|
|
REPORTER_ASSERT(reporter, !shader->isOpaque());
|
|
|
|
shader->unref();
|
|
|
|
|
|
|
|
// From this point on, we have pixels
|
2014-02-13 14:41:43 +00:00
|
|
|
bmp.allocPixels(info);
|
2011-12-09 15:48:03 +00:00
|
|
|
|
|
|
|
// test 2: not opaque by default
|
2012-08-23 18:14:13 +00:00
|
|
|
shader = SkShader::CreateBitmapShader(bmp,
|
2011-12-09 15:48:03 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
REPORTER_ASSERT(reporter, shader);
|
|
|
|
REPORTER_ASSERT(reporter, !shader->isOpaque());
|
|
|
|
shader->unref();
|
|
|
|
|
|
|
|
// test 3: explicitly opaque
|
2013-10-21 14:00:07 +00:00
|
|
|
bmp.setAlphaType(kOpaque_SkAlphaType);
|
2012-08-23 18:14:13 +00:00
|
|
|
shader = SkShader::CreateBitmapShader(bmp,
|
2011-12-09 15:48:03 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
REPORTER_ASSERT(reporter, shader);
|
|
|
|
REPORTER_ASSERT(reporter, shader->isOpaque());
|
|
|
|
shader->unref();
|
|
|
|
|
|
|
|
// test 4: explicitly not opaque
|
2013-10-21 14:00:07 +00:00
|
|
|
bmp.setAlphaType(kPremul_SkAlphaType);
|
2012-08-23 18:14:13 +00:00
|
|
|
shader = SkShader::CreateBitmapShader(bmp,
|
2011-12-09 15:48:03 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
REPORTER_ASSERT(reporter, shader);
|
|
|
|
REPORTER_ASSERT(reporter, !shader->isOpaque());
|
|
|
|
shader->unref();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_gradient(skiatest::Reporter* reporter)
|
|
|
|
{
|
|
|
|
SkPoint pts[2];
|
|
|
|
pts[0].iset(0, 0);
|
|
|
|
pts[1].iset(1, 0);
|
|
|
|
SkColor colors[2];
|
|
|
|
SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)};
|
|
|
|
int count = 2;
|
|
|
|
SkShader::TileMode mode = SkShader::kClamp_TileMode;
|
|
|
|
|
|
|
|
// test 1: all opaque
|
|
|
|
colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
|
|
|
|
colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
|
|
|
|
SkShader* grad = SkGradientShader::CreateLinear(pts, colors, pos, count,
|
|
|
|
mode);
|
|
|
|
REPORTER_ASSERT(reporter, grad);
|
|
|
|
REPORTER_ASSERT(reporter, grad->isOpaque());
|
|
|
|
grad->unref();
|
|
|
|
|
|
|
|
// test 2: all 0 alpha
|
|
|
|
colors[0] = SkColorSetARGB(0, 0, 0, 0);
|
|
|
|
colors[1] = SkColorSetARGB(0, 0, 0, 0);
|
|
|
|
grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
|
|
|
|
REPORTER_ASSERT(reporter, grad);
|
|
|
|
REPORTER_ASSERT(reporter, !grad->isOpaque());
|
|
|
|
grad->unref();
|
|
|
|
|
|
|
|
// test 3: one opaque, one transparent
|
|
|
|
colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
|
|
|
|
colors[1] = SkColorSetARGB(0x40, 0, 0, 0);
|
|
|
|
grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
|
|
|
|
REPORTER_ASSERT(reporter, grad);
|
|
|
|
REPORTER_ASSERT(reporter, !grad->isOpaque());
|
|
|
|
grad->unref();
|
|
|
|
|
|
|
|
// test 4: test 3, swapped
|
|
|
|
colors[0] = SkColorSetARGB(0x40, 0, 0, 0);
|
|
|
|
colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
|
|
|
|
grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
|
|
|
|
REPORTER_ASSERT(reporter, grad);
|
|
|
|
REPORTER_ASSERT(reporter, !grad->isOpaque());
|
|
|
|
grad->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_color(skiatest::Reporter* reporter)
|
|
|
|
{
|
|
|
|
SkColorShader colorShader1(SkColorSetARGB(0,0,0,0));
|
|
|
|
REPORTER_ASSERT(reporter, !colorShader1.isOpaque());
|
|
|
|
SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0));
|
|
|
|
REPORTER_ASSERT(reporter, colorShader2.isOpaque());
|
|
|
|
SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0));
|
|
|
|
REPORTER_ASSERT(reporter, !colorShader3.isOpaque());
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(ShaderOpacity, reporter) {
|
2011-12-09 15:48:03 +00:00
|
|
|
test_gradient(reporter);
|
|
|
|
test_color(reporter);
|
|
|
|
test_bitmap(reporter);
|
|
|
|
}
|