aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/mixedxfermodes.cpp
blob: c0045f84ae8f44eb2df010d96cb9b1658359d75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

/*
 * Copyright 2013 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 "SkBitmap.h"
#include "SkRandom.h"
#include "SkShader.h"
#include "SkXfermode.h"

namespace skiagm {

/**
 * Renders overlapping circles with random SkXfermode::Modes against a checkerboard.
 */
class MixedXfermodesGM : public GM {
public:
    MixedXfermodesGM() {
        static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF, 0xFFCCCCCC, 0xFFCCCCCC, 0xFFFFFFFF };
        SkBitmap bitmap;
        bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2, 2 * sizeof(uint32_t));
        bitmap.allocPixels();
        bitmap.lockPixels();
        memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData));
        bitmap.unlockPixels();
        fBG.reset(SkShader::CreateBitmapShader(bitmap,
                                               SkShader::kRepeat_TileMode,
                                               SkShader::kRepeat_TileMode));
        SkASSERT(NULL != fBG);
        SkMatrix lm;
        lm.setScale(SkIntToScalar(20), SkIntToScalar(20));
        fBG->setLocalMatrix(lm);
    }

protected:
    virtual SkString onShortName() {
        return SkString("mixed_xfermodes");
    }

    virtual SkISize onISize() {
        return make_isize(790, 640);
    }

    virtual void onDraw(SkCanvas* canvas) {
        SkPaint bgPaint;
        bgPaint.setShader(fBG.get());
        canvas->drawPaint(bgPaint);
        SkISize size = canvas->getDeviceSize();
        SkScalar areaSqrt = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight)));
        SkScalar minR = areaSqrt / 10;
        SkScalar maxR = areaSqrt / 4;
        SkMWCRandom random;
        for (int i = 0; i < kNumCircles; ++i) {
            SkScalar cx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth));
            SkScalar cy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight));
            SkScalar r = random.nextRangeScalar(minR, maxR);
            SkColor color = random.nextU();

            SkXfermode::Mode mode =
                static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1));
            // FIXME: Currently testing kDarken on GPU.
            mode = SkXfermode::kDarken_Mode;

            SkPaint p;
            p.setAntiAlias(true);
            p.setColor(color);
            p.setXfermodeMode(mode);
            canvas->drawCircle(cx, cy, r, p);
        }

        // FIXME: Remove text draw once this GM is finished.
        SkPaint txtPaint;
        txtPaint.setTextSize(areaSqrt / 20);
        txtPaint.setAntiAlias(true);
        static const char kTxt[] = "Work in progres... Do not baseline.";
        canvas->drawText(kTxt, strlen(kTxt),
                         areaSqrt/50,
                         SkIntToScalar(size.fHeight / 2),
                         txtPaint);
    }

private:
    enum {
        kMinR = 10,
        kMaxR = 50,
        kNumCircles = 50,
    };
    SkAutoTUnref<SkShader> fBG;
    typedef GM INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

static GM* MyFactory(void*) { return new MixedXfermodesGM; }
static GMRegistry reg(MyFactory);

}