xref: /aosp_15_r20/external/skia/gm/badpaint.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkShader.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/private/base/SkTArray.h"
19 
20 using namespace skia_private;
21 
22 /** This GM draws with invalid paints. It should draw nothing other than the background. */
23 class BadPaintGM : public skiagm::GM {
24  public:
BadPaintGM()25     BadPaintGM() {}
26 
27 protected:
getName() const28     SkString getName() const override { return SkString("badpaint"); }
29 
getISize()30     SkISize getISize() override { return SkISize::Make(100, 100); }
31 
onOnceBeforeDraw()32     void onOnceBeforeDraw() override {
33         SkBitmap emptyBmp;
34 
35         SkBitmap blueBmp;
36         blueBmp.allocN32Pixels(10, 10);
37         blueBmp.eraseColor(SK_ColorBLUE);
38 
39         SkMatrix badMatrix;
40         badMatrix.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
41 
42         // Empty bitmap.
43         fPaints.push_back().setColor(SK_ColorGREEN);
44         fPaints.back().setShader(emptyBmp.makeShader(SkSamplingOptions()));
45 
46         // Non-invertible local matrix.
47         fPaints.push_back().setColor(SK_ColorGREEN);
48         fPaints.back().setShader(blueBmp.makeShader(SkSamplingOptions(), badMatrix));
49     }
50 
onDraw(SkCanvas * canvas)51     void onDraw(SkCanvas* canvas) override {
52         SkRect rect = SkRect::MakeXYWH(10, 10, 80, 80);
53         for (int i = 0; i < fPaints.size(); ++i) {
54             canvas->drawRect(rect, fPaints[i]);
55         }
56     }
57 
58 private:
59     TArray<SkPaint> fPaints;
60 
61     using INHERITED = skiagm::GM;
62 };
63 
64 /////////////////////////////////////////////////////////////////////////////////////
65 
66 DEF_GM(return new BadPaintGM;)
67