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