2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2016-02-18 20:39:14 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2009-06-20 02:38:16 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
|
2016-02-09 19:59:24 +00:00
|
|
|
enum SrcType {
|
|
|
|
//! A WxH image with a rectangle in the lower right.
|
|
|
|
kRectangleImage_SrcType = 0x01,
|
|
|
|
//! kRectangleImage_SrcType with an alpha of 34.5%.
|
|
|
|
kRectangleImageWithAlpha_SrcType = 0x02,
|
|
|
|
//! kRectnagleImageWithAlpha_SrcType scaled down by half.
|
|
|
|
kSmallRectangleImageWithAlpha_SrcType = 0x04,
|
|
|
|
//! kRectangleImage_SrcType drawn directly instead in an image.
|
|
|
|
kRectangle_SrcType = 0x08,
|
|
|
|
//! Two rectangles, first on the right half, second on the bottom half.
|
|
|
|
kQuarterClear_SrcType = 0x10,
|
|
|
|
//! kQuarterClear_SrcType in a layer.
|
|
|
|
kQuarterClearInLayer_SrcType = 0x20,
|
|
|
|
//! A W/2xH/2 transparent image.
|
|
|
|
kSmallTransparentImage_SrcType = 0x40,
|
|
|
|
//! kRectangleImage_SrcType drawn directly with a mask.
|
|
|
|
kRectangleWithMask_SrcType = 0x80,
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-02-09 19:59:24 +00:00
|
|
|
kAll_SrcType = 0xFF, //!< All the source types.
|
|
|
|
kBasic_SrcType = 0x03, //!< Just basic source types.
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct {
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode fMode;
|
|
|
|
int fSourceTypeMask; // The source types to use this
|
2016-02-09 19:59:24 +00:00
|
|
|
// mode with. See draw_mode for
|
|
|
|
// an explanation of each type.
|
|
|
|
// PDF has to play some tricks
|
|
|
|
// to support the base modes,
|
|
|
|
// test those more extensively.
|
|
|
|
} gModes[] = {
|
2017-03-15 16:19:37 +00:00
|
|
|
{ SkBlendMode::kClear, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kSrc, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kDst, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kSrcOver, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kDstOver, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kSrcIn, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kDstIn, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kSrcOut, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kDstOut, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kSrcATop, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kDstATop, kAll_SrcType },
|
|
|
|
|
|
|
|
{ SkBlendMode::kXor, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kPlus, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kModulate, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kScreen, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kOverlay, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kDarken, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kLighten, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kColorDodge, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kColorBurn, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kHardLight, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kSoftLight, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kDifference, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kExclusion, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kMultiply, kAll_SrcType },
|
|
|
|
{ SkBlendMode::kHue, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kSaturation, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kColor, kBasic_SrcType },
|
|
|
|
{ SkBlendMode::kLuminosity, kBasic_SrcType },
|
2016-02-09 19:59:24 +00:00
|
|
|
};
|
2009-06-20 02:38:16 +00:00
|
|
|
|
2013-12-03 21:08:46 +00:00
|
|
|
static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
|
|
|
|
SkBitmap* transparent) {
|
2014-01-25 16:46:20 +00:00
|
|
|
src->allocN32Pixels(w, h);
|
2012-12-06 21:47:40 +00:00
|
|
|
src->eraseColor(SK_ColorTRANSPARENT);
|
2009-06-20 02:38:16 +00:00
|
|
|
|
|
|
|
SkPaint p;
|
2012-09-26 02:45:10 +00:00
|
|
|
p.setAntiAlias(true);
|
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
SkRect r;
|
|
|
|
SkScalar ww = SkIntToScalar(w);
|
|
|
|
SkScalar hh = SkIntToScalar(h);
|
|
|
|
|
2012-09-26 02:45:10 +00:00
|
|
|
{
|
|
|
|
SkCanvas c(*src);
|
2015-07-21 14:42:45 +00:00
|
|
|
p.setColor(sk_tool_utils::color_to_565(0xFFFFCC44));
|
2012-09-26 02:45:10 +00:00
|
|
|
r.set(0, 0, ww*3/4, hh*3/4);
|
|
|
|
c.drawOval(r, p);
|
|
|
|
}
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
dst->allocN32Pixels(w, h);
|
2012-12-06 21:47:40 +00:00
|
|
|
dst->eraseColor(SK_ColorTRANSPARENT);
|
2009-06-20 02:38:16 +00:00
|
|
|
|
2012-09-26 02:45:10 +00:00
|
|
|
{
|
|
|
|
SkCanvas c(*dst);
|
2015-07-21 14:42:45 +00:00
|
|
|
p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
|
2012-09-26 02:45:10 +00:00
|
|
|
r.set(ww/3, hh/3, ww*19/20, hh*19/20);
|
|
|
|
c.drawRect(r, p);
|
|
|
|
}
|
2013-12-03 21:08:46 +00:00
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
transparent->allocN32Pixels(w, h);
|
2013-12-03 21:08:46 +00:00
|
|
|
transparent->eraseColor(SK_ColorTRANSPARENT);
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 21:14:21 +00:00
|
|
|
static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
|
|
|
|
|
2016-02-09 19:59:24 +00:00
|
|
|
class XfermodesGM : public skiagm::GM {
|
2009-06-20 02:38:16 +00:00
|
|
|
SkBitmap fBG;
|
2013-12-03 21:08:46 +00:00
|
|
|
SkBitmap fSrcB, fDstB, fTransparent;
|
2009-06-20 02:38:16 +00:00
|
|
|
|
2013-10-30 20:48:05 +00:00
|
|
|
/* The srcType argument indicates what to draw for the source part. Skia
|
|
|
|
* uses the implied shape of the drawing command and these modes
|
|
|
|
* demonstrate that.
|
|
|
|
*/
|
2016-10-06 00:33:02 +00:00
|
|
|
void draw_mode(SkCanvas* canvas, SkBlendMode mode, SrcType srcType, SkScalar x, SkScalar y) {
|
2009-06-20 02:38:16 +00:00
|
|
|
SkPaint p;
|
2013-10-30 20:48:05 +00:00
|
|
|
SkMatrix m;
|
|
|
|
bool restoreNeeded = false;
|
|
|
|
m.setTranslate(x, y);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2014-12-10 17:02:16 +00:00
|
|
|
canvas->drawBitmap(fSrcB, x, y, &p);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(mode);
|
2013-10-30 20:48:05 +00:00
|
|
|
switch (srcType) {
|
2014-12-10 17:02:16 +00:00
|
|
|
case kSmallTransparentImage_SrcType: {
|
2013-12-03 21:08:46 +00:00
|
|
|
m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
|
2014-12-10 17:02:16 +00:00
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->concat(m);
|
|
|
|
canvas->drawBitmap(fTransparent, 0, 0, &p);
|
2013-12-03 21:08:46 +00:00
|
|
|
break;
|
2014-12-10 17:02:16 +00:00
|
|
|
}
|
2013-10-30 20:48:05 +00:00
|
|
|
case kQuarterClearInLayer_SrcType: {
|
2013-10-30 21:29:47 +00:00
|
|
|
SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
|
|
|
|
SkIntToScalar(H));
|
2013-10-30 20:48:05 +00:00
|
|
|
canvas->saveLayer(&bounds, &p);
|
|
|
|
restoreNeeded = true;
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrcOver);
|
2013-10-30 20:48:05 +00:00
|
|
|
// Fall through.
|
|
|
|
}
|
|
|
|
case kQuarterClear_SrcType: {
|
|
|
|
SkScalar halfW = SkIntToScalar(W) / 2;
|
|
|
|
SkScalar halfH = SkIntToScalar(H) / 2;
|
2015-07-21 14:42:45 +00:00
|
|
|
p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
|
2013-10-30 21:29:47 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
|
|
|
|
SkIntToScalar(H));
|
2013-10-30 20:48:05 +00:00
|
|
|
canvas->drawRect(r, p);
|
2015-07-21 14:42:45 +00:00
|
|
|
p.setColor(sk_tool_utils::color_to_565(0xFFAA66FF));
|
2013-10-30 21:29:47 +00:00
|
|
|
r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
|
2013-10-30 20:48:05 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-17 23:38:28 +00:00
|
|
|
case kRectangleWithMask_SrcType: {
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2013-12-17 23:38:28 +00:00
|
|
|
restoreNeeded = true;
|
|
|
|
SkScalar w = SkIntToScalar(W);
|
|
|
|
SkScalar h = SkIntToScalar(H);
|
|
|
|
SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
|
|
|
|
canvas->clipRect(r);
|
|
|
|
// Fall through.
|
|
|
|
}
|
2013-10-30 20:48:05 +00:00
|
|
|
case kRectangle_SrcType: {
|
|
|
|
SkScalar w = SkIntToScalar(W);
|
|
|
|
SkScalar h = SkIntToScalar(H);
|
|
|
|
SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
|
|
|
|
w * 37 / 60, h * 37 / 60);
|
2015-07-21 14:42:45 +00:00
|
|
|
p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
|
2013-10-30 20:48:05 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSmallRectangleImageWithAlpha_SrcType:
|
|
|
|
m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
|
|
|
|
// Fall through.
|
|
|
|
case kRectangleImageWithAlpha_SrcType:
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
// Fall through.
|
2014-12-10 17:02:16 +00:00
|
|
|
case kRectangleImage_SrcType: {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->concat(m);
|
|
|
|
canvas->drawBitmap(fDstB, 0, 0, &p);
|
2013-10-30 20:48:05 +00:00
|
|
|
break;
|
2014-12-10 17:02:16 +00:00
|
|
|
}
|
2013-10-30 20:48:05 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restoreNeeded) {
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-02-17 21:21:46 +00:00
|
|
|
fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
|
|
|
|
kOpaque_SkAlphaType),
|
|
|
|
gData, 4);
|
2013-01-28 21:14:21 +00:00
|
|
|
|
2013-12-03 21:08:46 +00:00
|
|
|
make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
|
2011-12-20 13:41:09 +00:00
|
|
|
}
|
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
public:
|
2010-12-23 16:45:33 +00:00
|
|
|
const static int W = 64;
|
|
|
|
const static int H = 64;
|
2013-01-28 21:14:21 +00:00
|
|
|
XfermodesGM() {}
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2009-06-21 00:49:18 +00:00
|
|
|
return SkString("xfermodes");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2016-09-26 22:51:24 +00:00
|
|
|
return SkISize::Make(1990, 570);
|
2009-06-23 12:19:41 +00:00
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2010-12-23 16:45:33 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2010-12-23 16:45:33 +00:00
|
|
|
const SkScalar w = SkIntToScalar(W);
|
|
|
|
const SkScalar h = SkIntToScalar(H);
|
2009-06-20 02:38:16 +00:00
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
|
2016-03-13 21:13:58 +00:00
|
|
|
auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode, &m);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
SkPaint labelP;
|
|
|
|
labelP.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&labelP);
|
2009-06-20 02:38:16 +00:00
|
|
|
labelP.setTextAlign(SkPaint::kCenter_Align);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-23 00:09:12 +00:00
|
|
|
const int W = 5;
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
SkScalar x0 = 0;
|
2013-10-30 20:48:05 +00:00
|
|
|
SkScalar y0 = 0;
|
|
|
|
for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
|
|
|
|
SkScalar x = x0, y = y0;
|
2009-06-20 02:38:16 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
|
2013-10-30 20:48:05 +00:00
|
|
|
if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-26 22:51:24 +00:00
|
|
|
SkRect r{ x, y, x+w, y+h };
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2010-12-23 16:45:33 +00:00
|
|
|
SkPaint p;
|
2009-06-20 02:38:16 +00:00
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setShader(s);
|
|
|
|
canvas->drawRect(r, p);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2016-09-26 22:51:24 +00:00
|
|
|
draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
|
2013-10-30 20:48:05 +00:00
|
|
|
r.fLeft, r.fTop);
|
2010-12-23 16:45:33 +00:00
|
|
|
canvas->restore();
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2010-12-23 16:45:33 +00:00
|
|
|
r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
2015-08-27 14:41:13 +00:00
|
|
|
p.setShader(nullptr);
|
2010-12-23 16:45:33 +00:00
|
|
|
canvas->drawRect(r, p);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2010-12-23 16:45:33 +00:00
|
|
|
#if 1
|
2017-03-15 16:19:37 +00:00
|
|
|
const char* label = SkBlendMode_Name(gModes[i].fMode);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(label,
|
2009-06-20 02:38:16 +00:00
|
|
|
x + w/2, y - labelP.getTextSize()/2, labelP);
|
2010-12-23 16:45:33 +00:00
|
|
|
#endif
|
2009-06-20 02:38:16 +00:00
|
|
|
x += w + SkIntToScalar(10);
|
2009-06-23 00:09:12 +00:00
|
|
|
if ((i % W) == W - 1) {
|
2009-06-20 02:38:16 +00:00
|
|
|
x = x0;
|
|
|
|
y += h + SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 20:48:05 +00:00
|
|
|
if (y < 320) {
|
|
|
|
if (x > x0) {
|
|
|
|
y += h + SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
y0 = y;
|
|
|
|
} else {
|
|
|
|
x0 += SkIntToScalar(400);
|
|
|
|
y0 = 0;
|
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
2016-02-09 19:59:24 +00:00
|
|
|
DEF_GM( return new XfermodesGM; )
|