blob: bdde173ef14d5aadf210e2cd99116e5293fdd212 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRefCnt.h"
14#include "include/core/SkShader.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/utils/SkRandom.h"
20#include "tools/ToolUtils.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000021
Ben Wagner7fde8e12019-05-01 17:28:53 -040022class GrContext;
23class GrRenderTargetContext;
24
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000025namespace skiagm {
26
27/*
28 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
29 * discarding it, drawing to it, and then drawing it to the main canvas.
30 */
Chris Dalton3a778372019-02-07 15:23:36 -070031class DiscardGM : public GpuGM {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000032
33public:
34 DiscardGM() {
35 }
36
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000037protected:
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000039 return SkString("discard");
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000043 return SkISize::Make(100, 100);
44 }
45
Chris Dalton50e24d72019-02-07 16:20:09 -070046 DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
47 SkString* errorMsg) override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000048 SkISize size = this->getISize();
49 size.fWidth /= 10;
50 size.fHeight /= 10;
51 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reede8f30622016-03-23 18:59:25 -070052 auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
halcanary96fcdcc2015-08-27 07:41:13 -070053 if (nullptr == surface) {
Chris Dalton50e24d72019-02-07 16:20:09 -070054 *errorMsg = "Could not create render target.";
55 return DrawResult::kFail;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000056 }
57
58 canvas->clear(SK_ColorBLACK);
59
60 SkRandom rand;
61 for (int x = 0; x < 10; ++x) {
62 for (int y = 0; y < 10; ++y) {
63 surface->getCanvas()->discard();
64 // Make something that isn't too close to the background color, black.
Mike Kleinea3f0142019-03-20 11:12:10 -050065 SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000066 switch (rand.nextULessThan(3)) {
67 case 0:
68 surface->getCanvas()->drawColor(color);
69 break;
70 case 1:
71 surface->getCanvas()->clear(color);
72 break;
73 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000074 SkPaint paint;
Mike Reedc8bea7d2019-04-09 13:55:36 -040075 paint.setShader(SkShaders::Color(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000076 surface->getCanvas()->drawPaint(paint);
77 break;
78 }
halcanary96fcdcc2015-08-27 07:41:13 -070079 surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000080 }
81 }
82
83 surface->getCanvas()->discard();
Chris Dalton50e24d72019-02-07 16:20:09 -070084 return DrawResult::kOk;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000085 }
86
87private:
88 typedef GM INHERITED;
89};
90
91//////////////////////////////////////////////////////////////////////////////
92
halcanary385fe4d2015-08-26 13:07:48 -070093DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000094
95} // end namespace