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.
|
|
|
|
*/
|
2014-01-13 14:53:55 +00:00
|
|
|
|
2011-05-20 14:13:56 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkBlurMask.h"
|
2011-05-20 14:13:56 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
|
|
|
|
static void setNamedTypeface(SkPaint* paint, const char name[]) {
|
2016-05-31 18:42:36 +00:00
|
|
|
paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
|
2011-05-20 14:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
|
|
|
|
|
|
|
|
class XfermodesBlurView : public SampleView {
|
|
|
|
SkBitmap fBG;
|
|
|
|
SkBitmap fSrcB, fDstB;
|
|
|
|
|
2016-03-29 18:32:50 +00:00
|
|
|
void draw_mode(SkCanvas* canvas, sk_sp<SkXfermode> mode, int alpha,
|
2011-05-20 14:13:56 +00:00
|
|
|
SkScalar x, SkScalar y) {
|
|
|
|
SkPaint p;
|
2016-04-04 17:02:58 +00:00
|
|
|
p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(5),
|
|
|
|
SkBlurMaskFilter::kNone_BlurFlag));
|
2011-05-20 14:13:56 +00:00
|
|
|
|
|
|
|
SkScalar ww = SkIntToScalar(W);
|
|
|
|
SkScalar hh = SkIntToScalar(H);
|
|
|
|
|
|
|
|
// draw a circle covering the upper
|
|
|
|
// left three quarters of the canvas
|
|
|
|
p.setColor(0xFFCC44FF);
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, ww*3/4, hh*3/4);
|
|
|
|
r.offset(x, y);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
|
2016-03-29 18:32:50 +00:00
|
|
|
p.setXfermode(std::move(mode));
|
2011-05-20 14:13:56 +00:00
|
|
|
|
|
|
|
// draw a square overlapping the circle
|
|
|
|
// in the lower right of the canvas
|
|
|
|
p.setColor(0x00AA6633 | alpha << 24);
|
|
|
|
r.set(ww/3, hh/3, ww*19/20, hh*19/20);
|
|
|
|
r.offset(x, y);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
const static int W = 64;
|
|
|
|
const static int H = 64;
|
2011-07-07 14:38:03 +00:00
|
|
|
XfermodesBlurView() {
|
2014-05-29 15:57:20 +00:00
|
|
|
fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType),
|
|
|
|
gBG, 4);
|
2011-05-20 14:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "XfermodesBlur");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
|
|
|
|
|
2011-08-04 13:50:17 +00:00
|
|
|
if (false) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(50);
|
2016-05-31 18:42:36 +00:00
|
|
|
paint.setTypeface(SkTypeface::MakeFromName("Arial Unicode MS", SkFontStyle()));
|
2011-08-04 13:50:17 +00:00
|
|
|
char buffer[10];
|
|
|
|
size_t len = SkUTF8_FromUnichar(0x8500, buffer);
|
|
|
|
canvas->drawText(buffer, len, 40, 40, paint);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-04 18:11:01 +00:00
|
|
|
if (false) {
|
2011-08-04 13:50:17 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-04 13:50:17 +00:00
|
|
|
SkRect r0 = { 0, 0, 10.5f, 20 };
|
|
|
|
SkRect r1 = { 10.5f, 10, 20, 30 };
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r0, paint);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawRect(r1, paint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-20 14:13:56 +00:00
|
|
|
const struct {
|
|
|
|
SkXfermode::Mode fMode;
|
|
|
|
const char* fLabel;
|
|
|
|
} gModes[] = {
|
|
|
|
{ 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-05-20 14:13:56 +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" },*/
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkScalar w = SkIntToScalar(W);
|
|
|
|
const SkScalar h = SkIntToScalar(H);
|
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
|
2016-03-09 02:50:00 +00:00
|
|
|
auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode, &m);
|
2011-05-20 14:13:56 +00:00
|
|
|
|
|
|
|
SkPaint labelP;
|
|
|
|
labelP.setAntiAlias(true);
|
|
|
|
labelP.setLCDRenderText(true);
|
|
|
|
labelP.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
setNamedTypeface(&labelP, "Menlo Regular");
|
|
|
|
|
|
|
|
const int W = 5;
|
|
|
|
|
|
|
|
SkScalar x0 = 0;
|
|
|
|
for (int twice = 0; twice < 2; twice++) {
|
|
|
|
SkScalar x = x0, y = 0;
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
|
|
|
|
SkRect r;
|
|
|
|
r.set(x, y, x+w, y+h);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setShader(s);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2016-03-29 18:32:50 +00:00
|
|
|
draw_mode(canvas, SkXfermode::Make(gModes[i].fMode),
|
|
|
|
twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
|
2011-05-20 14:13:56 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
2015-08-27 14:41:13 +00:00
|
|
|
p.setShader(nullptr);
|
2011-05-20 14:13:56 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
|
|
|
canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
|
|
|
|
x + w/2, y - labelP.getTextSize()/2, labelP);
|
|
|
|
x += w + SkIntToScalar(10);
|
|
|
|
if ((i % W) == W - 1) {
|
|
|
|
x = x0;
|
|
|
|
y += h + SkIntToScalar(30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x0 += SkIntToScalar(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new XfermodesBlurView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|