blob: 136458170ea75f01f7a933bdde466bf56dcee6d9 [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"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000010#include "SkPaint.h"
bsalomon4ee6bd82015-05-27 13:23:23 -070011#include "SkRandom.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000012#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();
halcanary96fcdcc2015-08-27 07:41:13 -070039 if (nullptr == context) {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000040 return;
41 }
42
43 SkISize size = this->getISize();
44 size.fWidth /= 10;
45 size.fHeight /= 10;
46 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reede8f30622016-03-23 18:59:25 -070047 auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
halcanary96fcdcc2015-08-27 07:41:13 -070048 if (nullptr == surface) {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000049 return;
50 }
51
52 canvas->clear(SK_ColorBLACK);
53
54 SkRandom rand;
55 for (int x = 0; x < 10; ++x) {
56 for (int y = 0; y < 10; ++y) {
57 surface->getCanvas()->discard();
58 // Make something that isn't too close to the background color, black.
caryclark12596012015-07-29 05:27:47 -070059 SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000060 switch (rand.nextULessThan(3)) {
61 case 0:
62 surface->getCanvas()->drawColor(color);
63 break;
64 case 1:
65 surface->getCanvas()->clear(color);
66 break;
67 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000068 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070069 paint.setShader(SkShader::MakeColorShader(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000070 surface->getCanvas()->drawPaint(paint);
71 break;
72 }
halcanary96fcdcc2015-08-27 07:41:13 -070073 surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000074 }
75 }
76
77 surface->getCanvas()->discard();
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000078 }
79
80private:
81 typedef GM INHERITED;
82};
83
84//////////////////////////////////////////////////////////////////////////////
85
halcanary385fe4d2015-08-26 13:07:48 -070086DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000087
88} // end namespace
89
90#endif