2013-04-17 14:19:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class Xfermodes2GM : public GM {
|
|
|
|
public:
|
|
|
|
Xfermodes2GM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-04-17 14:19:44 +00:00
|
|
|
return SkString("xfermodes2");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(455, 475);
|
2013-04-17 14:19:44 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-04-17 14:19:44 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
|
|
|
|
|
|
|
|
const SkScalar w = SkIntToScalar(kSize);
|
|
|
|
const SkScalar h = SkIntToScalar(kSize);
|
|
|
|
|
|
|
|
SkPaint labelP;
|
|
|
|
labelP.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&labelP);
|
2013-04-17 14:19:44 +00:00
|
|
|
labelP.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
|
|
|
|
const int W = 6;
|
|
|
|
|
|
|
|
SkScalar x = 0, y = 0;
|
|
|
|
for (size_t m = 0; m <= SkXfermode::kLastMode; m++) {
|
|
|
|
SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(m);
|
|
|
|
SkXfermode* xm = SkXfermode::Create(mode);
|
|
|
|
SkAutoUnref aur(xm);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
canvas->translate(x, y);
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(false);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setShader(fBG);
|
|
|
|
SkRect r = SkRect::MakeWH(w, h);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->saveLayer(&r, NULL);
|
2013-04-17 14:19:44 +00:00
|
|
|
|
|
|
|
p.setShader(fDst);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
p.setShader(fSrc);
|
|
|
|
p.setXfermode(xm);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setShader(NULL);
|
|
|
|
p.setXfermode(NULL);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
canvas->drawText(SkXfermode::ModeName(mode), strlen(SkXfermode::ModeName(mode)),
|
|
|
|
x + w/2, y - labelP.getTextSize()/2, labelP);
|
|
|
|
#endif
|
|
|
|
x += w + SkIntToScalar(10);
|
|
|
|
if ((m % W) == W - 1) {
|
|
|
|
x = 0;
|
|
|
|
y += h + SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2013-04-19 13:59:52 +00:00
|
|
|
static const uint32_t kCheckData[] = {
|
2015-07-21 14:42:45 +00:00
|
|
|
SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
|
|
|
|
SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
|
|
|
|
SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
|
|
|
|
SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
|
2013-04-17 14:19:44 +00:00
|
|
|
};
|
|
|
|
SkBitmap bg;
|
2014-01-25 16:46:20 +00:00
|
|
|
bg.allocN32Pixels(2, 2, true);
|
2013-04-17 14:19:44 +00:00
|
|
|
memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
|
2013-04-18 07:01:48 +00:00
|
|
|
|
2013-04-17 14:19:44 +00:00
|
|
|
SkMatrix lm;
|
|
|
|
lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
|
2014-04-28 14:55:39 +00:00
|
|
|
fBG.reset(SkShader::CreateBitmapShader(bg,
|
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
&lm));
|
2013-04-18 07:01:48 +00:00
|
|
|
|
2015-03-30 14:52:57 +00:00
|
|
|
SkBitmap srcBmp;
|
|
|
|
srcBmp.allocN32Pixels(kSize, kSize);
|
|
|
|
SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
|
2013-04-18 07:01:48 +00:00
|
|
|
|
2013-04-17 14:19:44 +00:00
|
|
|
for (int y = 0; y < kSize; ++y) {
|
|
|
|
int c = y * (1 << kShift);
|
|
|
|
SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2);
|
|
|
|
for (int x = 0; x < kSize; ++x) {
|
|
|
|
pixels[kSize * y + x] = rowColor;
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 14:52:57 +00:00
|
|
|
fSrc.reset(SkShader::CreateBitmapShader(srcBmp,
|
2013-04-17 14:19:44 +00:00
|
|
|
SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode));
|
2015-03-30 14:52:57 +00:00
|
|
|
SkBitmap dstBmp;
|
|
|
|
dstBmp.allocN32Pixels(kSize, kSize);
|
|
|
|
pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
|
2013-04-18 07:01:48 +00:00
|
|
|
|
2013-04-17 14:19:44 +00:00
|
|
|
for (int x = 0; x < kSize; ++x) {
|
|
|
|
int c = x * (1 << kShift);
|
|
|
|
SkPMColor colColor = SkPackARGB32(c, 0, c, c/2);
|
|
|
|
for (int y = 0; y < kSize; ++y) {
|
|
|
|
pixels[kSize * y + x] = colColor;
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 14:52:57 +00:00
|
|
|
fDst.reset(SkShader::CreateBitmapShader(dstBmp,
|
2013-04-17 14:19:44 +00:00
|
|
|
SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode));
|
|
|
|
}
|
2013-04-18 07:01:48 +00:00
|
|
|
|
2013-04-17 14:19:44 +00:00
|
|
|
enum {
|
|
|
|
kShift = 2,
|
|
|
|
kSize = 256 >> kShift,
|
|
|
|
};
|
2013-04-18 07:01:48 +00:00
|
|
|
|
2013-04-17 14:19:44 +00:00
|
|
|
SkAutoTUnref<SkShader> fBG;
|
|
|
|
SkAutoTUnref<SkShader> fSrc;
|
|
|
|
SkAutoTUnref<SkShader> fDst;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new Xfermodes2GM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|