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();
|
|
|
|
src->eraseColor(0);
|
|
|
|
|
|
|
|
SkCanvas c(*src);
|
|
|
|
SkPaint p;
|
|
|
|
SkRect r;
|
|
|
|
SkScalar ww = SkIntToScalar(w);
|
|
|
|
SkScalar hh = SkIntToScalar(h);
|
|
|
|
|
|
|
|
p.setAntiAlias(true);
|
2011-04-26 18:15:03 +00:00
|
|
|
p.setColor(0xFFFFCC44);
|
2009-06-20 02:38:16 +00:00
|
|
|
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();
|
|
|
|
dst->eraseColor(0);
|
|
|
|
c.setBitmapDevice(*dst);
|
|
|
|
|
|
|
|
p.setColor(0xFF66AAFF);
|
|
|
|
r.set(ww/3, hh/3, ww*19/20, hh*19/20);
|
|
|
|
c.drawRect(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
|
|
|
|
|
|
|
|
class XfermodesGM : public GM {
|
|
|
|
SkBitmap fBG;
|
|
|
|
SkBitmap fSrcB, fDstB;
|
|
|
|
|
2011-06-24 17:05:17 +00:00
|
|
|
/* The sourceType argument indicates what to draw for the source part. Skia
|
|
|
|
* uses the implied shape of the drawing command and these modes
|
|
|
|
* demonstrate that.
|
|
|
|
*
|
|
|
|
* 0x01: A WxH image with a rectangle in the lower right.
|
|
|
|
* 0x02: Same as 0x01, but with an alpha of 34.5%.
|
|
|
|
* 0x04: Same as 0x02, but scaled down by half.
|
|
|
|
* 0x08: The same rectangle as 0x01, but drawn directly instead in an image.
|
|
|
|
* 0x10: Two rectangles, first on the right half, second on the bottom half.
|
|
|
|
* 0x20: Same as 0x10, but on a layer.
|
|
|
|
*/
|
|
|
|
void draw_mode(SkCanvas* canvas, SkXfermode* mode, int sourceType,
|
2010-12-23 16:45:33 +00:00
|
|
|
SkScalar x, SkScalar y) {
|
2009-06-20 02:38:16 +00:00
|
|
|
SkPaint p;
|
2011-06-24 17:05:17 +00:00
|
|
|
SkMatrix m;
|
|
|
|
bool restoreNeeded = false;
|
|
|
|
m.setTranslate(x, y);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2011-06-24 17:05:17 +00:00
|
|
|
canvas->drawBitmapMatrix(fSrcB, m, &p);
|
2009-06-20 02:38:16 +00:00
|
|
|
p.setXfermode(mode);
|
2011-06-24 17:05:17 +00:00
|
|
|
switch (sourceType) {
|
|
|
|
case 0x20: {
|
|
|
|
SkRect bounds = SkRect::MakeXYWH(x, y, W, H);
|
|
|
|
canvas->saveLayer(&bounds, &p);
|
|
|
|
restoreNeeded = true;
|
|
|
|
p.setXfermodeMode(SkXfermode::kSrcOver_Mode);
|
|
|
|
// Fall through.
|
|
|
|
}
|
|
|
|
case 0x10: {
|
|
|
|
SkScalar halfW = SkIntToScalar(W) / 2;
|
|
|
|
SkScalar halfH = SkIntToScalar(H) / 2;
|
|
|
|
p.setColor(0xFF66AAFF);
|
|
|
|
SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW, H);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
p.setColor(0xFFAA66FF);
|
|
|
|
r = SkRect::MakeXYWH(x, y + halfH, W, halfH);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0x8: {
|
|
|
|
SkScalar w = SkIntToScalar(W);
|
|
|
|
SkScalar h = SkIntToScalar(H);
|
|
|
|
SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
|
|
|
|
w * 37 / 60, h * 37 / 60);
|
|
|
|
p.setColor(0xFF66AAFF);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0x4:
|
|
|
|
m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
|
|
|
|
// Fall through.
|
|
|
|
case 0x2:
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
// Fall through.
|
|
|
|
case 0x1:
|
|
|
|
canvas->drawBitmapMatrix(fDstB, m, &p);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restoreNeeded) {
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
}
|
2011-04-26 18:15:03 +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;
|
2011-04-26 18:15:03 +00:00
|
|
|
XfermodesGM() {
|
|
|
|
// Do all this work in a temporary so we get a deep copy,
|
|
|
|
// especially of gBG.
|
|
|
|
SkBitmap scratchBitmap;
|
|
|
|
scratchBitmap.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4);
|
|
|
|
scratchBitmap.setPixels(gBG);
|
|
|
|
scratchBitmap.setIsOpaque(true);
|
|
|
|
scratchBitmap.copyTo(&fBG, SkBitmap::kARGB_4444_Config);
|
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
make_bitmaps(W, H, &fSrcB, &fDstB);
|
|
|
|
}
|
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 17:05:17 +00:00
|
|
|
return make_isize(1590, 640);
|
2009-06-23 12:19:41 +00:00
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
|
|
|
|
void drawBG(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
}
|
2011-04-26 18:15:03 +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
|
|
|
this->drawBG(canvas);
|
2011-04-26 18:15:03 +00:00
|
|
|
|
2011-06-24 17:05:17 +00:00
|
|
|
const int kMaxSourceType = 0x20;
|
2009-06-20 02:38:16 +00:00
|
|
|
const struct {
|
2010-12-23 16:45:33 +00:00
|
|
|
SkXfermode::Mode fMode;
|
2011-06-24 17:05:17 +00:00
|
|
|
const char* fLabel;
|
|
|
|
int fSourceTypeMask; // The source types to use this
|
|
|
|
// 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.
|
2009-06-20 02:38:16 +00:00
|
|
|
} gModes[] = {
|
2011-06-24 17:05:17 +00:00
|
|
|
{ SkXfermode::kClear_Mode, "Clear", 0x3F },
|
|
|
|
{ SkXfermode::kSrc_Mode, "Src", 0x3F },
|
|
|
|
{ SkXfermode::kDst_Mode, "Dst", 0x3F },
|
|
|
|
{ SkXfermode::kSrcOver_Mode, "SrcOver", 0x3F },
|
|
|
|
{ SkXfermode::kDstOver_Mode, "DstOver", 0x3F },
|
|
|
|
{ SkXfermode::kSrcIn_Mode, "SrcIn", 0x3F },
|
|
|
|
{ SkXfermode::kDstIn_Mode, "DstIn", 0x3F },
|
|
|
|
{ SkXfermode::kSrcOut_Mode, "SrcOut", 0x3F },
|
|
|
|
{ SkXfermode::kDstOut_Mode, "DstOut", 0x3F },
|
|
|
|
{ SkXfermode::kSrcATop_Mode, "SrcATop", 0x3F },
|
|
|
|
{ SkXfermode::kDstATop_Mode, "DstATop", 0x3F },
|
|
|
|
|
|
|
|
{ SkXfermode::kXor_Mode, "Xor", 0x03 },
|
|
|
|
{ SkXfermode::kPlus_Mode, "Plus", 0x03 },
|
|
|
|
{ SkXfermode::kMultiply_Mode, "Multiply", 0x3F },
|
|
|
|
{ SkXfermode::kScreen_Mode, "Screen", 0x03 },
|
|
|
|
{ SkXfermode::kOverlay_Mode, "Overlay", 0x03 },
|
|
|
|
{ SkXfermode::kDarken_Mode, "Darken", 0x03 },
|
|
|
|
{ SkXfermode::kLighten_Mode, "Lighten", 0x03 },
|
|
|
|
{ SkXfermode::kColorDodge_Mode, "ColorDodge", 0x03 },
|
|
|
|
{ SkXfermode::kColorBurn_Mode, "ColorBurn", 0x03 },
|
|
|
|
{ SkXfermode::kHardLight_Mode, "HardLight", 0x03 },
|
|
|
|
{ SkXfermode::kSoftLight_Mode, "SoftLight", 0x03 },
|
|
|
|
{ SkXfermode::kDifference_Mode, "Difference", 0x03 },
|
|
|
|
{ SkXfermode::kExclusion_Mode, "Exclusion", 0x03 },
|
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 17:05:17 +00:00
|
|
|
SkScalar y0 = 0;
|
|
|
|
for (int sourceType = 1; sourceType <= kMaxSourceType; sourceType *=2) {
|
|
|
|
SkScalar x = x0, y = y0;
|
2009-06-20 02:38:16 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
|
2011-06-24 17:05:17 +00:00
|
|
|
if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
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 17:05:17 +00:00
|
|
|
draw_mode(canvas, mode, sourceType, 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 17:05:17 +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
|
|
|
}
|
|
|
|
s->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new XfermodesGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|