blob: 2faf36d348460d1878c9043c62b77f5cf4e74dc6 [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040019#include "include/gpu/GrDirectContext.h"
20#include "include/gpu/GrRecordingContext.h"
Kevin Lubick5c93acf2023-05-09 12:11:43 -040021#include "include/gpu/ganesh/SkSurfaceGanesh.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050022#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/ToolUtils.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000024
Kevin Lubick5c93acf2023-05-09 12:11:43 -040025#if defined(SK_GRAPHITE)
26#include "include/gpu/graphite/Surface.h"
27#endif
28
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000029namespace skiagm {
30
31/*
32 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
33 * discarding it, drawing to it, and then drawing it to the main canvas.
34 */
Robert Phillipsedcd4312021-06-03 10:14:16 -040035class DiscardGM : public GM {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000036
37public:
Robert Phillips44333c52020-06-30 13:28:00 -040038 DiscardGM() {}
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000039
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000040protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000041 SkString getName() const override { return SkString("discard"); }
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000042
Leandro Lovisolo8f023882023-08-15 21:13:52 +000043 SkISize getISize() override { return SkISize::Make(100, 100); }
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000044
Robert Phillipsedcd4312021-06-03 10:14:16 -040045 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillips95c250c2020-06-29 15:36:12 -040046
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000047 SkISize size = this->getISize();
48 size.fWidth /= 10;
49 size.fHeight /= 10;
50 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
Robert Phillips88d8aba2023-02-22 11:04:06 -050051 sk_sp<SkSurface> surface;
52
53 auto dContext = GrAsDirectContext(canvas->recordingContext());
54 if (dContext && !dContext->abandoned()) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040055 surface = SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kNo, info);
Robert Phillips88d8aba2023-02-22 11:04:06 -050056 }
57
Kevin Lubick0f7b44e2023-02-28 09:13:11 -050058#if defined(SK_GRAPHITE)
Robert Phillips88d8aba2023-02-22 11:04:06 -050059 auto recorder = canvas->recorder();
60 if (recorder) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040061 surface = SkSurfaces::RenderTarget(recorder, info);
Robert Phillips88d8aba2023-02-22 11:04:06 -050062 }
63#endif
64
65 if (!surface) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040066 surface = SkSurfaces::Raster(info);
Robert Phillips88d8aba2023-02-22 11:04:06 -050067 }
68 if (!surface) {
69 *errorMsg = "Could not create surface.";
Chris Dalton50e24d72019-02-07 16:20:09 -070070 return DrawResult::kFail;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000071 }
72
73 canvas->clear(SK_ColorBLACK);
74
75 SkRandom rand;
76 for (int x = 0; x < 10; ++x) {
77 for (int y = 0; y < 10; ++y) {
78 surface->getCanvas()->discard();
79 // Make something that isn't too close to the background color, black.
Mike Kleinea3f0142019-03-20 11:12:10 -050080 SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000081 switch (rand.nextULessThan(3)) {
82 case 0:
83 surface->getCanvas()->drawColor(color);
84 break;
85 case 1:
86 surface->getCanvas()->clear(color);
87 break;
88 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000089 SkPaint paint;
Mike Reedc8bea7d2019-04-09 13:55:36 -040090 paint.setShader(SkShaders::Color(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000091 surface->getCanvas()->drawPaint(paint);
92 break;
93 }
Mike Reedb746b1f2021-01-06 08:43:51 -050094 surface->draw(canvas, 10.f*x, 10.f*y);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000095 }
96 }
97
98 surface->getCanvas()->discard();
Chris Dalton50e24d72019-02-07 16:20:09 -070099 return DrawResult::kOk;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +0000100 }
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +0000101};
102
103//////////////////////////////////////////////////////////////////////////////
104
halcanary385fe4d2015-08-26 13:07:48 -0700105DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +0000106
John Stilesa6841be2020-08-06 14:11:56 -0400107} // namespace skiagm