2015-01-07 21:23:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2015-01-07 21:23:09 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static void make_transparency(SkCanvas* canvas, SkScalar width, SkScalar height) {
|
2015-01-07 21:23:09 +00:00
|
|
|
SkPoint pts[2];
|
|
|
|
pts[0] = SkPoint::Make(0, 0);
|
|
|
|
pts[1] = SkPoint::Make(width, 0);
|
|
|
|
const SkColor kColors[] = {
|
|
|
|
SK_ColorBLACK,
|
|
|
|
SK_ColorGRAY,
|
|
|
|
SK_ColorWHITE,
|
|
|
|
SK_ColorRED,
|
|
|
|
SK_ColorYELLOW,
|
|
|
|
SK_ColorGREEN,
|
|
|
|
SK_ColorCYAN,
|
|
|
|
SK_ColorBLUE,
|
|
|
|
SK_ColorMAGENTA,
|
|
|
|
};
|
|
|
|
const SkScalar kRowHeight = height / SK_ARRAY_COUNT(kColors);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(kColors); ++i) {
|
|
|
|
SkColor shaderColors[2];
|
|
|
|
shaderColors[0] = SK_AlphaTRANSPARENT;
|
|
|
|
shaderColors[1] = kColors[i];
|
|
|
|
SkPaint p;
|
2019-04-03 14:27:45 +00:00
|
|
|
p.setShader(SkGradientShader::MakeLinear(pts, shaderColors, nullptr, 2, SkTileMode::kClamp));
|
2016-03-13 21:13:58 +00:00
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p);
|
2015-01-07 21:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://crrev.com/834303005
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
|
2015-01-07 21:23:09 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(2 * size, 2 * size);
|
|
|
|
bm.eraseColor(c1);
|
|
|
|
bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
|
|
|
|
bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
|
2019-04-08 17:20:23 +00:00
|
|
|
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
|
2015-01-07 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// http://crrev.com/834303005
|
|
|
|
static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
|
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(create_checkerboard_shader(c1, c2, size));
|
2015-01-07 21:23:09 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This GM verifies that a transparent bitmap drawn over a
|
|
|
|
* checkerboard pattern looks correct.
|
|
|
|
*/
|
2015-01-08 20:11:42 +00:00
|
|
|
DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
|
2018-08-16 14:17:03 +00:00
|
|
|
checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
|
2015-01-07 21:23:09 +00:00
|
|
|
{
|
|
|
|
SkAutoCanvasRestore autoCanvasRestore(canvas, true);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(256, 9));
|
2015-01-07 21:23:09 +00:00
|
|
|
make_transparency(surface->getCanvas(), 256.0f, 9.0f);
|
2015-01-08 20:11:42 +00:00
|
|
|
canvas->scale(7.0f, 120.0f);
|
2015-08-27 14:41:13 +00:00
|
|
|
surface->draw(canvas, 0, 0, nullptr);
|
2015-01-07 21:23:09 +00:00
|
|
|
}
|
|
|
|
}
|