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.
|
|
|
|
*/
|
2009-06-20 02:38:16 +00:00
|
|
|
#include "gm.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2011-04-26 18:15:03 +00:00
|
|
|
static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst) {
|
2009-06-20 02:38:16 +00:00
|
|
|
src->setConfig(SkBitmap::kARGB_8888_Config, w, h);
|
|
|
|
src->allocPixels();
|
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);
|
|
|
|
p.setColor(0xFFFFCC44);
|
|
|
|
r.set(0, 0, ww*3/4, hh*3/4);
|
|
|
|
c.drawOval(r, p);
|
|
|
|
}
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
dst->setConfig(SkBitmap::kARGB_8888_Config, w, h);
|
|
|
|
dst->allocPixels();
|
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);
|
|
|
|
p.setColor(0xFF66AAFF);
|
|
|
|
r.set(ww/3, hh/3, ww*19/20, hh*19/20);
|
|
|
|
c.drawRect(r, p);
|
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 21:14:21 +00:00
|
|
|
static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
|
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
class XfermodesGM : public GM {
|
|
|
|
SkBitmap fBG;
|
|
|
|
SkBitmap fSrcB, fDstB;
|
|
|
|
|
2011-06-24 19:14:42 +00:00
|
|
|
void draw_mode(SkCanvas* canvas, SkXfermode* mode, int alpha,
|
2010-12-23 16:45:33 +00:00
|
|
|
SkScalar x, SkScalar y) {
|
2009-06-20 02:38:16 +00:00
|
|
|
SkPaint p;
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2011-06-24 19:14:42 +00:00
|
|
|
canvas->drawBitmap(fSrcB, x, y, &p);
|
|
|
|
p.setAlpha(alpha);
|
2009-06-20 02:38:16 +00:00
|
|
|
p.setXfermode(mode);
|
2011-06-24 19:14:42 +00:00
|
|
|
canvas->drawBitmap(fDstB, x, y, &p);
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2013-01-28 21:14:21 +00:00
|
|
|
virtual void onOnceBeforeDraw() SK_OVERRIDE {
|
|
|
|
fBG.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4);
|
|
|
|
fBG.setPixels(gData);
|
|
|
|
fBG.setIsOpaque(true);
|
|
|
|
|
|
|
|
make_bitmaps(W, H, &fSrcB, &fDstB);
|
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:
|
2009-06-23 12:19:41 +00:00
|
|
|
virtual SkString onShortName() {
|
2009-06-21 00:49:18 +00:00
|
|
|
return SkString("xfermodes");
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:15:03 +00:00
|
|
|
virtual SkISize onISize() {
|
2011-06-24 19:14:42 +00:00
|
|
|
return make_isize(790, 640);
|
2009-06-23 12:19:41 +00:00
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
2010-12-23 16:45:33 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
const struct {
|
2010-12-23 16:45:33 +00:00
|
|
|
SkXfermode::Mode fMode;
|
2011-06-24 19:14:42 +00:00
|
|
|
const char* fLabel;
|
2009-06-20 02:38:16 +00:00
|
|
|
} gModes[] = {
|
2011-06-24 19:14:42 +00:00
|
|
|
{ SkXfermode::kClear_Mode, "Clear" },
|
|
|
|
{ SkXfermode::kSrc_Mode, "Src" },
|
|
|
|
{ SkXfermode::kDst_Mode, "Dst" },
|
|
|
|
{ SkXfermode::kSrcOver_Mode, "SrcOver" },
|
|
|
|
{ SkXfermode::kDstOver_Mode, "DstOver" },
|
|
|
|
{ SkXfermode::kSrcIn_Mode, "SrcIn" },
|
|
|
|
{ SkXfermode::kDstIn_Mode, "DstIn" },
|
|
|
|
{ SkXfermode::kSrcOut_Mode, "SrcOut" },
|
|
|
|
{ SkXfermode::kDstOut_Mode, "DstOut" },
|
|
|
|
{ SkXfermode::kSrcATop_Mode, "SrcATop" },
|
|
|
|
{ SkXfermode::kDstATop_Mode, "DstATop" },
|
|
|
|
{ SkXfermode::kXor_Mode, "Xor" },
|
|
|
|
|
|
|
|
{ SkXfermode::kPlus_Mode, "Plus" },
|
2013-01-30 21:36:11 +00:00
|
|
|
{ SkXfermode::kModulate_Mode, "Modulate" },
|
2011-06-24 19:14:42 +00:00
|
|
|
{ SkXfermode::kScreen_Mode, "Screen" },
|
|
|
|
{ SkXfermode::kOverlay_Mode, "Overlay" },
|
|
|
|
{ SkXfermode::kDarken_Mode, "Darken" },
|
|
|
|
{ SkXfermode::kLighten_Mode, "Lighten" },
|
|
|
|
{ SkXfermode::kColorDodge_Mode, "ColorDodge" },
|
|
|
|
{ SkXfermode::kColorBurn_Mode, "ColorBurn" },
|
|
|
|
{ SkXfermode::kHardLight_Mode, "HardLight" },
|
|
|
|
{ SkXfermode::kSoftLight_Mode, "SoftLight" },
|
|
|
|
{ SkXfermode::kDifference_Mode, "Difference" },
|
|
|
|
{ SkXfermode::kExclusion_Mode, "Exclusion" },
|
2013-02-04 20:06:00 +00:00
|
|
|
{ SkXfermode::kMultiply_Mode, "Multiply" },
|
2013-03-05 16:23:59 +00:00
|
|
|
{ SkXfermode::kHue_Mode, "Hue" },
|
|
|
|
{ SkXfermode::kSaturation_Mode, "Saturation" },
|
|
|
|
{ SkXfermode::kColor_Mode, "Color" },
|
|
|
|
{ SkXfermode::kLuminosity_Mode, "Luminosity" },
|
2009-06-20 02:38:16 +00:00
|
|
|
};
|
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
|
|
|
SkShader* s = SkShader::CreateBitmapShader(fBG,
|
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode);
|
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
|
|
|
|
s->setLocalMatrix(m);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
SkPaint labelP;
|
|
|
|
labelP.setAntiAlias(true);
|
|
|
|
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;
|
2011-06-24 19:14:42 +00:00
|
|
|
for (int twice = 0; twice < 2; twice++) {
|
|
|
|
SkScalar x = x0, y = 0;
|
2009-06-20 02:38:16 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
|
2009-06-23 00:09:12 +00:00
|
|
|
SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
|
2010-12-23 16:45:33 +00:00
|
|
|
SkAutoUnref aur(mode);
|
2009-06-20 02:38:16 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(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
|
|
|
|
2010-12-23 16:45:33 +00:00
|
|
|
canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
|
2011-06-24 19:14:42 +00:00
|
|
|
draw_mode(canvas, mode, twice ? 0x88 : 0xFF, 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);
|
|
|
|
p.setShader(NULL);
|
|
|
|
canvas->drawRect(r, p);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2010-12-23 16:45:33 +00:00
|
|
|
#if 1
|
2009-06-20 02:38:16 +00:00
|
|
|
canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-06-24 19:14:42 +00:00
|
|
|
x0 += SkIntToScalar(400);
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
|
|
|
s->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new XfermodesGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|