Debian Codegraph @

gm/discard.cpp

raw
1 /*
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
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkSurface.h"
18 #include "include/core/SkTypes.h"
19 #include "src/base/SkRandom.h"
20 #include "tools/ToolUtils.h"
21
22 #if defined(SK_GANESH)
23 #include "include/gpu/ganesh/GrDirectContext.h"
24 #include "include/gpu/ganesh/GrRecordingContext.h"
25 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
26 #endif
27
28 #if defined(SK_GRAPHITE)
29 #include "include/gpu/graphite/Surface.h"
30 #endif
31
32 namespace skiagm {
33
34 /*
35 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
36 * discarding it, drawing to it, and then drawing it to the main canvas.
37 */
38 class DiscardGM : public GM {
39
40 public:
41 DiscardGM() {}
42
43 protected:
44 SkString getName() const override { return SkString("discard"); }
45
46 SkISize getISize() override { return SkISize::Make(100, 100); }
47
48 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
49
50 SkISize size = this->getISize();
51 size.fWidth /= 10;
52 size.fHeight /= 10;
53 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
54 sk_sp<SkSurface> surface;
55
56 #if defined(SK_GANESH)
57 if (auto dContext = GrAsDirectContext(canvas->recordingContext());
58 dContext && !dContext->abandoned()) {
59 surface = SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kNo, info);
60 }
61 #endif
62
63 #if defined(SK_GRAPHITE)
64 if (auto recorder = canvas->recorder()) {
65 surface = SkSurfaces::RenderTarget(recorder, info);
66 }
67 #endif
68
69 if (!surface) {
70 surface = SkSurfaces::Raster(info);
71 }
72 if (!surface) {
73 *errorMsg = "Could not create surface.";
74 return DrawResult::kFail;
75 }
76
77 canvas->clear(SK_ColorBLACK);
78
79 SkRandom rand;
80 for (int x = 0; x < 10; ++x) {
81 for (int y = 0; y < 10; ++y) {
82 surface->getCanvas()->discard();
83 // Make something that isn't too close to the background color, black.
84 SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040);
85 switch (rand.nextULessThan(3)) {
86 case 0:
87 surface->getCanvas()->drawColor(color);
88 break;
89 case 1:
90 surface->getCanvas()->clear(color);
91 break;
92 case 2:
93 SkPaint paint;
94 paint.setShader(SkShaders::Color(color));
95 surface->getCanvas()->drawPaint(paint);
96 break;
97 }
98 surface->draw(canvas, 10.f*x, 10.f*y);
99 }
100 }
101
102 surface->getCanvas()->discard();
103 return DrawResult::kOk;
104 }
105 };
106
107 //////////////////////////////////////////////////////////////////////////////
108
109 DEF_GM(return new DiscardGM;)
110
111 } // namespace skiagm