blob: ff8fe4a4b842c8c9325f7f26161b208fc26969bc [file] [log] [blame]
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +00001/*
2 * Copyright 2014 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
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +00008#include "gm.h"
9#include "SkCanvas.h"
10#include "SkColorShader.h"
11#include "SkPaint.h"
12#include "SkSurface.h"
13
reedf037e0b2014-10-30 11:34:15 -070014#if SK_SUPPORT_GPU
15
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000016namespace skiagm {
17
18/*
19 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
20 * discarding it, drawing to it, and then drawing it to the main canvas.
21 */
22class DiscardGM : public GM {
23
24public:
25 DiscardGM() {
26 }
27
28 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
29
30protected:
31 virtual SkString onShortName() SK_OVERRIDE {
32 return SkString("discard");
33 }
34
35 virtual SkISize onISize() SK_OVERRIDE {
36 return SkISize::Make(100, 100);
37 }
38
39 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
40 GrContext* context = canvas->getGrContext();
41 if (NULL == context) {
42 return;
43 }
44
45 SkISize size = this->getISize();
46 size.fWidth /= 10;
47 size.fHeight /= 10;
48 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
49 SkSurface* surface = SkSurface::NewRenderTarget(context, info);
50
51 if (NULL == surface) {
52 return;
53 }
54
55 canvas->clear(SK_ColorBLACK);
56
57 SkRandom rand;
58 for (int x = 0; x < 10; ++x) {
59 for (int y = 0; y < 10; ++y) {
60 surface->getCanvas()->discard();
61 // Make something that isn't too close to the background color, black.
62 SkColor color = rand.nextU() | 0xFF404040;
63 switch (rand.nextULessThan(3)) {
64 case 0:
65 surface->getCanvas()->drawColor(color);
66 break;
67 case 1:
68 surface->getCanvas()->clear(color);
69 break;
70 case 2:
71 SkColorShader shader(color);
72 SkPaint paint;
73 paint.setShader(&shader);
74 surface->getCanvas()->drawPaint(paint);
75 break;
76 }
bsalomon@google.com0b27f2e2014-04-24 21:23:37 +000077 surface->draw(canvas, 10.f*x, 10.f*y, NULL);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000078 }
79 }
80
81 surface->getCanvas()->discard();
82 surface->unref();
83 }
84
85private:
86 typedef GM INHERITED;
87};
88
89//////////////////////////////////////////////////////////////////////////////
90
91DEF_GM( return SkNEW(DiscardGM); )
92
93} // end namespace
94
95#endif