blob: c0045f84ae8f44eb2df010d96cb9b1658359d75b [file] [log] [blame]
bsalomon@google.com20edf382013-04-01 18:02:55 +00001
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
14namespace skiagm {
15
16/**
17 * Renders overlapping circles with random SkXfermode::Modes against a checkerboard.
18 */
19class MixedXfermodesGM : public GM {
20public:
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
38protected:
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.com20edf382013-04-01 18:02:55 +000048 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.com05a2ee02013-04-02 07:01:34 +000061
bsalomon@google.com20edf382013-04-01 18:02:55 +000062 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.com05a2ee02013-04-02 07:01:34 +000066
bsalomon@google.com20edf382013-04-01 18:02:55 +000067 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.com05a2ee02013-04-02 07:01:34 +000073
bsalomon@google.com20edf382013-04-01 18:02:55 +000074 // 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.com20edf382013-04-01 18:02:55 +000083 }
84
bsalomon@google.com20edf382013-04-01 18:02:55 +000085private:
86 enum {
87 kMinR = 10,
88 kMaxR = 50,
89 kNumCircles = 50,
90 };
91 SkAutoTUnref<SkShader> fBG;
92 typedef GM INHERITED;
93};
94
95//////////////////////////////////////////////////////////////////////////////
96
97static GM* MyFactory(void*) { return new MixedXfermodesGM; }
98static GMRegistry reg(MyFactory);
99
100}