aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/discard.cpp
blob: ff8fe4a4b842c8c9325f7f26161b208fc26969bc (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
/*
 * Copyright 2014 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 "SkColorShader.h"
#include "SkPaint.h"
#include "SkSurface.h"

#if SK_SUPPORT_GPU

namespace skiagm {

/*
 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
 * discarding it, drawing to it, and then drawing it to the main canvas.
 */
class DiscardGM : public GM {

public:
    DiscardGM() {
    }

    virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }

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

    virtual SkISize onISize() SK_OVERRIDE {
        return SkISize::Make(100, 100);
    }

    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        GrContext* context = canvas->getGrContext();
        if (NULL == context) {
            return;
        }

        SkISize size = this->getISize();
        size.fWidth /= 10;
        size.fHeight /= 10;
        SkImageInfo info = SkImageInfo::MakeN32Premul(size);
        SkSurface* surface = SkSurface::NewRenderTarget(context, info);

        if (NULL == surface) {
            return;
        }

        canvas->clear(SK_ColorBLACK);

        SkRandom rand;
        for (int x = 0; x < 10; ++x) {
            for (int y = 0; y < 10; ++y) {
              surface->getCanvas()->discard();
              // Make something that isn't too close to the background color, black.
              SkColor color = rand.nextU() | 0xFF404040;
              switch (rand.nextULessThan(3)) {
                  case 0:
                      surface->getCanvas()->drawColor(color);
                      break;
                  case 1:
                      surface->getCanvas()->clear(color);
                      break;
                  case 2:
                      SkColorShader shader(color);
                      SkPaint paint;
                      paint.setShader(&shader);
                      surface->getCanvas()->drawPaint(paint);
                      break;
              }
              surface->draw(canvas, 10.f*x, 10.f*y, NULL);
            }
        }

        surface->getCanvas()->discard();
        surface->unref();
    }

private:
    typedef GM INHERITED;
};

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

DEF_GM( return SkNEW(DiscardGM); )

} // end namespace

#endif