bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkRandom.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkXfermode.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | /** |
| 17 | * Renders overlapping circles with random SkXfermode::Modes against a checkerboard. |
| 18 | */ |
| 19 | class MixedXfermodesGM : public GM { |
| 20 | public: |
| 21 | MixedXfermodesGM() { |
| 22 | static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF, 0xFFCCCCCC, 0xFFCCCCCC, 0xFFFFFFFF }; |
| 23 | SkBitmap bitmap; |
| 24 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2, 2 * sizeof(uint32_t)); |
| 25 | bitmap.allocPixels(); |
| 26 | bitmap.lockPixels(); |
| 27 | memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData)); |
| 28 | bitmap.unlockPixels(); |
| 29 | fBG.reset(SkShader::CreateBitmapShader(bitmap, |
| 30 | SkShader::kRepeat_TileMode, |
| 31 | SkShader::kRepeat_TileMode)); |
| 32 | SkASSERT(NULL != fBG); |
| 33 | SkMatrix lm; |
| 34 | lm.setScale(SkIntToScalar(20), SkIntToScalar(20)); |
| 35 | fBG->setLocalMatrix(lm); |
| 36 | } |
| 37 | |
| 38 | protected: |
| 39 | virtual SkString onShortName() { |
| 40 | return SkString("mixed_xfermodes"); |
| 41 | } |
| 42 | |
| 43 | virtual SkISize onISize() { |
| 44 | return make_isize(790, 640); |
| 45 | } |
| 46 | |
| 47 | virtual void onDraw(SkCanvas* canvas) { |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 48 | SkPaint bgPaint; |
| 49 | bgPaint.setShader(fBG.get()); |
| 50 | canvas->drawPaint(bgPaint); |
| 51 | SkISize size = canvas->getDeviceSize(); |
| 52 | SkScalar areaSqrt = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))); |
| 53 | SkScalar minR = areaSqrt / 10; |
| 54 | SkScalar maxR = areaSqrt / 4; |
| 55 | SkMWCRandom random; |
| 56 | for (int i = 0; i < kNumCircles; ++i) { |
| 57 | SkScalar cx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth)); |
| 58 | SkScalar cy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight)); |
| 59 | SkScalar r = random.nextRangeScalar(minR, maxR); |
| 60 | SkColor color = random.nextU(); |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 62 | SkXfermode::Mode mode = |
| 63 | static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1)); |
| 64 | // FIXME: Currently testing kDarken on GPU. |
| 65 | mode = SkXfermode::kDarken_Mode; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 67 | SkPaint p; |
| 68 | p.setAntiAlias(true); |
| 69 | p.setColor(color); |
| 70 | p.setXfermodeMode(mode); |
| 71 | canvas->drawCircle(cx, cy, r, p); |
| 72 | } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 74 | // FIXME: Remove text draw once this GM is finished. |
| 75 | SkPaint txtPaint; |
| 76 | txtPaint.setTextSize(areaSqrt / 20); |
| 77 | txtPaint.setAntiAlias(true); |
| 78 | static const char kTxt[] = "Work in progres... Do not baseline."; |
| 79 | canvas->drawText(kTxt, strlen(kTxt), |
| 80 | areaSqrt/50, |
| 81 | SkIntToScalar(size.fHeight / 2), |
| 82 | txtPaint); |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 85 | private: |
| 86 | enum { |
| 87 | kMinR = 10, |
| 88 | kMaxR = 50, |
| 89 | kNumCircles = 50, |
| 90 | }; |
| 91 | SkAutoTUnref<SkShader> fBG; |
| 92 | typedef GM INHERITED; |
| 93 | }; |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | static GM* MyFactory(void*) { return new MixedXfermodesGM; } |
| 98 | static GMRegistry reg(MyFactory); |
| 99 | |
| 100 | } |