| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkShader.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkTileMode.h" |
| 22 |
#include "include/core/SkTypes.h" |
| 23 |
#include "src/core/SkColorPriv.h" |
| 24 |
|
| 25 |
constexpr SkBlendMode gModes[] = { |
| 26 |
SkBlendMode::kClear, |
| 27 |
SkBlendMode::kSrc, |
| 28 |
SkBlendMode::kDst, |
| 29 |
SkBlendMode::kSrcOver, |
| 30 |
SkBlendMode::kDstOver, |
| 31 |
SkBlendMode::kSrcIn, |
| 32 |
SkBlendMode::kDstIn, |
| 33 |
SkBlendMode::kSrcOut, |
| 34 |
SkBlendMode::kDstOut, |
| 35 |
SkBlendMode::kSrcATop, |
| 36 |
SkBlendMode::kDstATop, |
| 37 |
SkBlendMode::kXor, |
| 38 |
}; |
| 39 |
|
| 40 |
const int gWidth = 64; |
| 41 |
const int gHeight = 64; |
| 42 |
const SkScalar W = SkIntToScalar(gWidth); |
| 43 |
const SkScalar H = SkIntToScalar(gHeight); |
| 44 |
|
| 45 |
static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) { |
| 46 |
|
| 47 |
SkPaint paint; |
| 48 |
paint.setAntiAlias(true); |
| 49 |
|
| 50 |
SkRect r = SkRect::MakeWH(W, H); |
| 51 |
r.inset(W/10, H/10); |
| 52 |
|
| 53 |
paint.setColor(SK_ColorBLUE); |
| 54 |
paint.setAlpha(a0); |
| 55 |
canvas->drawOval(r, paint); |
| 56 |
|
| 57 |
paint.setColor(SK_ColorRED); |
| 58 |
paint.setAlpha(a1); |
| 59 |
paint.setBlendMode(mode); |
| 60 |
for (int angle = 0; angle < 24; ++angle) { |
| 61 |
SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth; |
| 62 |
SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight; |
| 63 |
paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24); |
| 64 |
canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint); |
| 65 |
} |
| 66 |
|
| 67 |
return H; |
| 68 |
} |
| 69 |
|
| 70 |
static sk_sp<SkShader> make_bg_shader() { |
| 71 |
SkBitmap bm; |
| 72 |
bm.allocN32Pixels(2, 2); |
| 73 |
*bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
| 74 |
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE); |
| 75 |
|
| 76 |
SkMatrix m; |
| 77 |
m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
| 78 |
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), m); |
| 79 |
} |
| 80 |
|
| 81 |
namespace skiagm { |
| 82 |
|
| 83 |
class HairModesGM : public GM { |
| 84 |
SkPaint fBGPaint; |
| 85 |
|
| 86 |
protected: |
| 87 |
SkString getName() const override { return SkString("hairmodes"); } |
| 88 |
|
| 89 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 90 |
|
| 91 |
void onOnceBeforeDraw() override { |
| 92 |
fBGPaint.setShader(make_bg_shader()); |
| 93 |
} |
| 94 |
|
| 95 |
void onDraw(SkCanvas* canvas) override { |
| 96 |
const SkRect bounds = SkRect::MakeWH(W, H); |
| 97 |
constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
| 98 |
|
| 99 |
canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
| 100 |
|
| 101 |
for (int alpha = 0; alpha < 4; ++alpha) { |
| 102 |
canvas->save(); |
| 103 |
canvas->save(); |
| 104 |
for (size_t i = 0; i < std::size(gModes); ++i) { |
| 105 |
if (6 == i) { |
| 106 |
canvas->restore(); |
| 107 |
canvas->translate(W * 5, 0); |
| 108 |
canvas->save(); |
| 109 |
} |
| 110 |
|
| 111 |
canvas->drawRect(bounds, fBGPaint); |
| 112 |
canvas->saveLayer(&bounds, nullptr); |
| 113 |
SkScalar dy = drawCell(canvas, gModes[i], |
| 114 |
gAlphaValue[alpha & 1], |
| 115 |
gAlphaValue[alpha & 2]); |
| 116 |
canvas->restore(); |
| 117 |
|
| 118 |
canvas->translate(0, dy * 5 / 4); |
| 119 |
} |
| 120 |
canvas->restore(); |
| 121 |
canvas->restore(); |
| 122 |
canvas->translate(W * 5 / 4, 0); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
private: |
| 127 |
using INHERITED = GM; |
| 128 |
}; |
| 129 |
|
| 130 |
////////////////////////////////////////////////////////////////////////////// |
| 131 |
|
| 132 |
DEF_GM( return new HairModesGM; ) |
| 133 |
} // namespace skiagm |