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