blob: a76228897eb34e93b78ed60ea0c2e9d226d1083f [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
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000028protected:
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000030 return SkString("discard");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000034 return SkISize::Make(100, 100);
35 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000038 GrContext* context = canvas->getGrContext();
39 if (NULL == context) {
40 return;
41 }
42
43 SkISize size = this->getISize();
44 size.fWidth /= 10;
45 size.fHeight /= 10;
46 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
bsalomonafe30052015-01-16 07:32:33 -080047 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000048
49 if (NULL == surface) {
50 return;
51 }
52
53 canvas->clear(SK_ColorBLACK);
54
55 SkRandom rand;
56 for (int x = 0; x < 10; ++x) {
57 for (int y = 0; y < 10; ++y) {
58 surface->getCanvas()->discard();
59 // Make something that isn't too close to the background color, black.
60 SkColor color = rand.nextU() | 0xFF404040;
61 switch (rand.nextULessThan(3)) {
62 case 0:
63 surface->getCanvas()->drawColor(color);
64 break;
65 case 1:
66 surface->getCanvas()->clear(color);
67 break;
68 case 2:
69 SkColorShader shader(color);
70 SkPaint paint;
71 paint.setShader(&shader);
72 surface->getCanvas()->drawPaint(paint);
73 break;
74 }
bsalomon@google.com0b27f2e2014-04-24 21:23:37 +000075 surface->draw(canvas, 10.f*x, 10.f*y, NULL);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000076 }
77 }
78
79 surface->getCanvas()->discard();
80 surface->unref();
81 }
82
83private:
84 typedef GM INHERITED;
85};
86
87//////////////////////////////////////////////////////////////////////////////
88
89DEF_GM( return SkNEW(DiscardGM); )
90
91} // end namespace
92
93#endif