2011-09-19 19:01:38 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
|
2017-03-15 16:19:37 +00:00
|
|
|
constexpr SkBlendMode gModes[] = {
|
|
|
|
SkBlendMode::kClear,
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kDstOut,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstATop,
|
|
|
|
SkBlendMode::kXor,
|
2011-09-19 19:01:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int gWidth = 64;
|
|
|
|
const int gHeight = 64;
|
|
|
|
const SkScalar W = SkIntToScalar(gWidth);
|
|
|
|
const SkScalar H = SkIntToScalar(gHeight);
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
|
2011-09-19 19:01:38 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(W, H);
|
|
|
|
r.inset(W/10, H/10);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setAlpha(a0);
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setAlpha(a1);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(mode);
|
2011-09-19 19:01:38 +00:00
|
|
|
for (int angle = 0; angle < 24; ++angle) {
|
|
|
|
SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
|
|
|
|
SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
|
|
|
|
paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
|
|
|
|
canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
return H;
|
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_bg_shader() {
|
2011-09-19 19:01:38 +00:00
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(2, 2);
|
2011-09-19 19:01:38 +00:00
|
|
|
*bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
|
2015-07-29 12:27:47 +00:00
|
|
|
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE);
|
2011-09-19 19:01:38 +00:00
|
|
|
|
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
|
2019-04-08 17:20:23 +00:00
|
|
|
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
|
2011-09-19 19:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
class HairModesGM : public GM {
|
|
|
|
SkPaint fBGPaint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2011-09-19 19:01:38 +00:00
|
|
|
return SkString("hairmodes");
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
virtual SkISize onISize() override { return SkISize::Make(640, 480); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2016-03-13 21:13:58 +00:00
|
|
|
fBGPaint.setShader(make_bg_shader());
|
2012-11-27 15:15:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2011-09-19 19:01:38 +00:00
|
|
|
const SkRect bounds = SkRect::MakeWH(W, H);
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
for (int alpha = 0; alpha < 4; ++alpha) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->save();
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
|
|
|
|
if (6 == i) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * 5, 0);
|
|
|
|
canvas->save();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
canvas->drawRect(bounds, fBGPaint);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&bounds, nullptr);
|
2017-03-15 16:19:37 +00:00
|
|
|
SkScalar dy = drawCell(canvas, gModes[i],
|
2011-09-19 19:01:38 +00:00
|
|
|
gAlphaValue[alpha & 1],
|
|
|
|
gAlphaValue[alpha & 2]);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
canvas->translate(0, dy * 5 / 4);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * 5 / 4, 0);
|
|
|
|
}
|
|
|
|
}
|
2011-09-19 19:08:33 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-09-19 19:01:38 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new HairModesGM; )
|
2011-09-19 19:01:38 +00:00
|
|
|
}
|