xref: /aosp_15_r20/external/skia/gm/tiledscaledbitmap.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/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/SkShader.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTileMode.h"
18 
19  /***
20   *
21   * This GM reproduces Skia bug 2904, in which a tiled bitmap shader was failing to draw correctly
22   * when fractional image scaling was ignored by the high quality bitmap scaler.
23   *
24   ***/
25 
26 namespace skiagm {
27 
28 class TiledScaledBitmapGM : public GM {
29 public:
30 
TiledScaledBitmapGM()31     TiledScaledBitmapGM() {
32     }
33 
34 protected:
getName() const35     SkString getName() const override { return SkString("tiledscaledbitmap"); }
36 
getISize()37     SkISize getISize() override { return SkISize::Make(1016, 616); }
38 
make_bm(int width,int height)39     static SkBitmap make_bm(int width, int height) {
40         SkBitmap bm;
41         bm.allocN32Pixels(width, height);
42         bm.eraseColor(SK_ColorTRANSPARENT);
43         SkCanvas canvas(bm);
44         SkPaint paint;
45         paint.setAntiAlias(true);
46         canvas.drawCircle(width/2.f, height/2.f, width/4.f, paint);
47         return bm;
48     }
49 
onOnceBeforeDraw()50     void onOnceBeforeDraw() override {
51         fBitmap = make_bm(360, 288);
52     }
53 
onDraw(SkCanvas * canvas)54     void onDraw(SkCanvas* canvas) override {
55         SkPaint paint;
56 
57         paint.setAntiAlias(true);
58 
59         SkMatrix mat;
60         mat.setScale(121.f/360.f, 93.f/288.f);
61         mat.postTranslate(-72, -72);
62 
63         paint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
64                                            SkSamplingOptions(SkCubicResampler::Mitchell()), mat));
65         canvas->drawRect({ 8, 8, 1008, 608 }, paint);
66     }
67 
68 private:
69     SkBitmap fBitmap;
70 
71     using INHERITED = GM;
72 };
73 
74 //////////////////////////////////////////////////////////////////////////////
75 
76 DEF_GM(return new TiledScaledBitmapGM;)
77 }  // namespace skiagm
78