• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/utils/SkRandom.h"
20 #include "tools/ToolUtils.h"
21 
22 class GrContext;
23 class GrRenderTargetContext;
24 
25 namespace 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  */
31 class DiscardGM : public GpuGM {
32 
33 public:
DiscardGM()34     DiscardGM() {
35     }
36 
37 protected:
onShortName()38     SkString onShortName() override {
39         return SkString("discard");
40     }
41 
onISize()42     SkISize onISize() override {
43         return SkISize::Make(100, 100);
44     }
45 
onDraw(GrContext * context,GrRenderTargetContext *,SkCanvas * canvas,SkString * errorMsg)46     DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
47                       SkString* errorMsg) override {
48         SkISize size = this->getISize();
49         size.fWidth /= 10;
50         size.fHeight /= 10;
51         SkImageInfo info = SkImageInfo::MakeN32Premul(size);
52         auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
53         if (nullptr == surface) {
54             *errorMsg = "Could not create render target.";
55             return DrawResult::kFail;
56         }
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.
65               SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040);
66               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:
74                       SkPaint paint;
75                       paint.setShader(SkShaders::Color(color));
76                       surface->getCanvas()->drawPaint(paint);
77                       break;
78               }
79               surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
80             }
81         }
82 
83         surface->getCanvas()->discard();
84         return DrawResult::kOk;
85     }
86 
87 private:
88     typedef GM INHERITED;
89 };
90 
91 //////////////////////////////////////////////////////////////////////////////
92 
93 DEF_GM(return new DiscardGM;)
94 
95 } // end namespace
96