blob: 8cec42fd09cd434ac7b3cba9580a2d77bdf80d9f [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000010#include "SkCanvas.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000011#include "SkPaint.h"
bsalomon4ee6bd82015-05-27 13:23:23 -070012#include "SkRandom.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000013#include "SkSurface.h"
14
15namespace skiagm {
16
17/*
18 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
19 * discarding it, drawing to it, and then drawing it to the main canvas.
20 */
Chris Dalton3a778372019-02-07 15:23:36 -070021class DiscardGM : public GpuGM {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000022
23public:
24 DiscardGM() {
25 }
26
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000027protected:
mtklein36352bf2015-03-25 18:17:31 -070028 SkString onShortName() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000029 return SkString("discard");
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000033 return SkISize::Make(100, 100);
34 }
35
Chris Dalton50e24d72019-02-07 16:20:09 -070036 DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
37 SkString* errorMsg) override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000038 SkISize size = this->getISize();
39 size.fWidth /= 10;
40 size.fHeight /= 10;
41 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reede8f30622016-03-23 18:59:25 -070042 auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
halcanary96fcdcc2015-08-27 07:41:13 -070043 if (nullptr == surface) {
Chris Dalton50e24d72019-02-07 16:20:09 -070044 *errorMsg = "Could not create render target.";
45 return DrawResult::kFail;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000046 }
47
48 canvas->clear(SK_ColorBLACK);
49
50 SkRandom rand;
51 for (int x = 0; x < 10; ++x) {
52 for (int y = 0; y < 10; ++y) {
53 surface->getCanvas()->discard();
54 // Make something that isn't too close to the background color, black.
caryclark12596012015-07-29 05:27:47 -070055 SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000056 switch (rand.nextULessThan(3)) {
57 case 0:
58 surface->getCanvas()->drawColor(color);
59 break;
60 case 1:
61 surface->getCanvas()->clear(color);
62 break;
63 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000064 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070065 paint.setShader(SkShader::MakeColorShader(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000066 surface->getCanvas()->drawPaint(paint);
67 break;
68 }
halcanary96fcdcc2015-08-27 07:41:13 -070069 surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000070 }
71 }
72
73 surface->getCanvas()->discard();
Chris Dalton50e24d72019-02-07 16:20:09 -070074 return DrawResult::kOk;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000075 }
76
77private:
78 typedef GM INHERITED;
79};
80
81//////////////////////////////////////////////////////////////////////////////
82
halcanary385fe4d2015-08-26 13:07:48 -070083DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000084
85} // end namespace