Index: gm/xfermodes.cpp |
diff --git a/gm/xfermodes.cpp b/gm/xfermodes.cpp |
index bc1bc7ff73bdfb98100b09d755a7847df7f81935..fa4461712d3deb76481fff85a370d34af001779b 100644 |
--- a/gm/xfermodes.cpp |
+++ b/gm/xfermodes.cpp |
@@ -10,7 +10,69 @@ |
#include "SkShader.h" |
#include "SkXfermode.h" |
-namespace skiagm { |
+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, |
+ |
+ kAll_SrcType = 0xFF, //!< All the source types. |
+ kBasic_SrcType = 0x03, //!< Just basic source types. |
+}; |
+ |
+const struct { |
+ SkXfermode::Mode fMode; |
+ 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. |
+} gModes[] = { |
+ { SkXfermode::kClear_Mode, "Clear", kAll_SrcType }, |
+ { SkXfermode::kSrc_Mode, "Src", kAll_SrcType }, |
+ { SkXfermode::kDst_Mode, "Dst", kAll_SrcType }, |
+ { SkXfermode::kSrcOver_Mode, "SrcOver", kAll_SrcType }, |
+ { SkXfermode::kDstOver_Mode, "DstOver", kAll_SrcType }, |
+ { SkXfermode::kSrcIn_Mode, "SrcIn", kAll_SrcType }, |
+ { SkXfermode::kDstIn_Mode, "DstIn", kAll_SrcType }, |
+ { SkXfermode::kSrcOut_Mode, "SrcOut", kAll_SrcType }, |
+ { SkXfermode::kDstOut_Mode, "DstOut", kAll_SrcType }, |
+ { SkXfermode::kSrcATop_Mode, "SrcATop", kAll_SrcType }, |
+ { SkXfermode::kDstATop_Mode, "DstATop", kAll_SrcType }, |
+ |
+ { SkXfermode::kXor_Mode, "Xor", kBasic_SrcType }, |
+ { SkXfermode::kPlus_Mode, "Plus", kBasic_SrcType }, |
+ { SkXfermode::kModulate_Mode, "Modulate", kAll_SrcType }, |
+ { SkXfermode::kScreen_Mode, "Screen", kBasic_SrcType }, |
+ { SkXfermode::kOverlay_Mode, "Overlay", kBasic_SrcType }, |
+ { SkXfermode::kDarken_Mode, "Darken", kBasic_SrcType }, |
+ { SkXfermode::kLighten_Mode, "Lighten", kBasic_SrcType }, |
+ { SkXfermode::kColorDodge_Mode, "ColorDodge", kBasic_SrcType }, |
+ { SkXfermode::kColorBurn_Mode, "ColorBurn", kBasic_SrcType }, |
+ { SkXfermode::kHardLight_Mode, "HardLight", kBasic_SrcType }, |
+ { SkXfermode::kSoftLight_Mode, "SoftLight", kBasic_SrcType }, |
+ { SkXfermode::kDifference_Mode, "Difference", kBasic_SrcType }, |
+ { SkXfermode::kExclusion_Mode, "Exclusion", kBasic_SrcType }, |
+ { SkXfermode::kMultiply_Mode, "Multiply", kAll_SrcType }, |
+ { SkXfermode::kHue_Mode, "Hue", kBasic_SrcType }, |
+ { SkXfermode::kSaturation_Mode, "Saturation", kBasic_SrcType }, |
+ { SkXfermode::kColor_Mode, "Color", kBasic_SrcType }, |
+ { SkXfermode::kLuminosity_Mode, "Luminosity", kBasic_SrcType }, |
+}; |
static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst, |
SkBitmap* transparent) { |
@@ -47,29 +109,7 @@ static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst, |
static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF }; |
-class XfermodesGM : public GM { |
- 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, |
- |
- kAll_SrcType = 0xFF, //!< All the source types. |
- kBasic_SrcType = 0x03, //!< Just basic source types. |
- }; |
- |
+class XfermodesGM : public skiagm::GM { |
SkBitmap fBG; |
SkBitmap fSrcB, fDstB, fTransparent; |
@@ -179,48 +219,6 @@ protected: |
void onDraw(SkCanvas* canvas) override { |
canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
- const struct { |
- SkXfermode::Mode fMode; |
- 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. |
- } gModes[] = { |
- { SkXfermode::kClear_Mode, "Clear", kAll_SrcType }, |
- { SkXfermode::kSrc_Mode, "Src", kAll_SrcType }, |
- { SkXfermode::kDst_Mode, "Dst", kAll_SrcType }, |
- { SkXfermode::kSrcOver_Mode, "SrcOver", kAll_SrcType }, |
- { SkXfermode::kDstOver_Mode, "DstOver", kAll_SrcType }, |
- { SkXfermode::kSrcIn_Mode, "SrcIn", kAll_SrcType }, |
- { SkXfermode::kDstIn_Mode, "DstIn", kAll_SrcType }, |
- { SkXfermode::kSrcOut_Mode, "SrcOut", kAll_SrcType }, |
- { SkXfermode::kDstOut_Mode, "DstOut", kAll_SrcType }, |
- { SkXfermode::kSrcATop_Mode, "SrcATop", kAll_SrcType }, |
- { SkXfermode::kDstATop_Mode, "DstATop", kAll_SrcType }, |
- |
- { SkXfermode::kXor_Mode, "Xor", kBasic_SrcType }, |
- { SkXfermode::kPlus_Mode, "Plus", kBasic_SrcType }, |
- { SkXfermode::kModulate_Mode, "Modulate", kAll_SrcType }, |
- { SkXfermode::kScreen_Mode, "Screen", kBasic_SrcType }, |
- { SkXfermode::kOverlay_Mode, "Overlay", kBasic_SrcType }, |
- { SkXfermode::kDarken_Mode, "Darken", kBasic_SrcType }, |
- { SkXfermode::kLighten_Mode, "Lighten", kBasic_SrcType }, |
- { SkXfermode::kColorDodge_Mode, "ColorDodge", kBasic_SrcType }, |
- { SkXfermode::kColorBurn_Mode, "ColorBurn", kBasic_SrcType }, |
- { SkXfermode::kHardLight_Mode, "HardLight", kBasic_SrcType }, |
- { SkXfermode::kSoftLight_Mode, "SoftLight", kBasic_SrcType }, |
- { SkXfermode::kDifference_Mode, "Difference", kBasic_SrcType }, |
- { SkXfermode::kExclusion_Mode, "Exclusion", kBasic_SrcType }, |
- { SkXfermode::kMultiply_Mode, "Multiply", kAll_SrcType }, |
- { SkXfermode::kHue_Mode, "Hue", kBasic_SrcType }, |
- { SkXfermode::kSaturation_Mode, "Saturation", kBasic_SrcType }, |
- { SkXfermode::kColor_Mode, "Color", kBasic_SrcType }, |
- { SkXfermode::kLuminosity_Mode, "Luminosity", kBasic_SrcType }, |
- }; |
- |
const SkScalar w = SkIntToScalar(W); |
const SkScalar h = SkIntToScalar(H); |
SkMatrix m; |
@@ -291,10 +289,129 @@ protected: |
private: |
typedef GM INHERITED; |
}; |
+DEF_GM( return new XfermodesGM; ) |
+ |
+/////////////////////////////////////////////////////////////////////////////////////////////////// |
+#include "SkNx.h" |
+ |
+static SkPMColor apply_proc(SkPMColor src, SkPMColor dst, SkXfermodeProc4f proc, float src_alpha) { |
+ SkPM4f src4 = SkPM4f::FromPMColor(src); |
+ for (int i = 0; i < 4; ++i) { |
+ src4.fVec[i] *= src_alpha; |
+ } |
+ SkPM4f dst4 = SkPM4f::FromPMColor(dst); |
+ SkPM4f res4 = proc(src4, dst4); |
+ SkPMColor res; |
+ SkNx_cast<uint8_t>(Sk4f::Load(res4.fVec) * Sk4f(255) + Sk4f(0.5f)).store(&res); |
+ return res; |
+} |
+ |
+static bool apply_mode(const SkPixmap& res, const SkPixmap& src, const SkPixmap& dst, |
+ SkXfermode* xfer, float src_alpha) { |
+ SkXfermode::Mode mode; |
+ if (!xfer) { |
+ mode = SkXfermode::kSrcOver_Mode; |
+ } else if (!xfer->asMode(&mode)) { |
+ return false; |
+ } |
+ SkXfermodeProc4f proc = SkXfermode::GetProc4f(mode); |
+ if (!proc) { |
+ return false; |
+ } |
+ |
+ for (int y = 0; y < res.height(); ++y) { |
+ for (int x = 0; x < res.width(); ++x) { |
+ *res.writable_addr32(x, y) = apply_proc(*src.addr32(x, y), *dst.addr32(x, y), |
+ proc, src_alpha); |
+ } |
+ } |
+ return true; |
+} |
-////////////////////////////////////////////////////////////////////////////// |
+void draw_mode(const SkBitmap& srcB, const SkBitmap& dstB, |
+ SkCanvas* canvas, SkXfermode* mode, SkScalar x, SkScalar y, float src_alpha) { |
+ SkBitmap resB; |
+ resB.allocN32Pixels(64, 64); |
-static GM* MyFactory(void*) { return new XfermodesGM; } |
-static GMRegistry reg(MyFactory); |
+ SkPixmap srcPM, dstPM, resPM; |
+ srcB.peekPixels(&srcPM); |
+ dstB.peekPixels(&dstPM); |
+ resB.peekPixels(&resPM); |
+ if (apply_mode(resPM, srcPM, dstPM, mode, src_alpha)) { |
+ canvas->drawBitmap(resB, x, y, nullptr); |
+ } |
+} |
+ |
+DEF_SIMPLE_GM(xfermodes_proc4f, canvas, 1000, 1000) { |
+ SkBitmap bg, srcB, dstB, transparent; |
+ |
+ bg.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kOpaque_SkAlphaType), |
+ gData, 4); |
+ make_bitmaps(64, 64, &dstB, &srcB, &transparent); |
+ |
+ canvas->translate(10, 20); |
+ |
+ const SkScalar w = 64; |
+ const SkScalar h = 64; |
+ SkMatrix m = SkMatrix::MakeScale(6, 6); |
+ SkShader* s = SkShader::CreateBitmapShader(bg, |
+ SkShader::kRepeat_TileMode, |
+ SkShader::kRepeat_TileMode, |
+ &m); |
+ |
+ SkPaint labelP; |
+ labelP.setAntiAlias(true); |
+ sk_tool_utils::set_portable_typeface(&labelP); |
+ labelP.setTextAlign(SkPaint::kCenter_Align); |
+ |
+ const int W = 5; |
+ |
+ const float alphas[] = { 1.0f, 0.5f }; |
+ |
+ for (auto alpha : alphas) { |
+ SkScalar x0 = 0; |
+ SkScalar y0 = 0; |
+ SkScalar x = x0, y = y0; |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
+ SkXfermode* mode = SkXfermode::Create(gModes[i].fMode); |
+ SkAutoUnref aur(mode); |
+ SkRect r; |
+ r.set(x, y, x+w, y+h); |
+ |
+ SkPaint p; |
+ p.setStyle(SkPaint::kFill_Style); |
+ p.setShader(s); |
+ canvas->drawRect(r, p); |
+ |
+ draw_mode(srcB, dstB, canvas, mode, r.fLeft, r.fTop, alpha); |
+ |
+ r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
+ p.setStyle(SkPaint::kStroke_Style); |
+ p.setShader(nullptr); |
+ canvas->drawRect(r, p); |
+ |
+ #if 1 |
+ canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel), |
+ x + w/2, y - labelP.getTextSize()/2, labelP); |
+ #endif |
+ x += w + SkIntToScalar(10); |
+ if ((i % W) == W - 1) { |
+ x = x0; |
+ y += h + SkIntToScalar(30); |
+ } |
+ } |
+ if (y < 320) { |
+ if (x > x0) { |
+ y += h + SkIntToScalar(30); |
+ } |
+ y0 = y; |
+ } else { |
+ x0 += SkIntToScalar(400); |
+ y0 = 0; |
+ } |
+ |
+ canvas->translate(400, 0); |
+ } |
+ s->unref(); |
} |