humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "Resources.h" |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkStream.h" |
| 16 | |
| 17 | |
| 18 | /*** |
| 19 | * |
| 20 | * This GM reproduces Skia bug 2904, in which a tiled bitmap shader was failing to draw correctly |
| 21 | * when fractional image scaling was ignored by the high quality bitmap scaler. |
| 22 | * |
| 23 | ***/ |
| 24 | |
| 25 | namespace skiagm { |
| 26 | |
| 27 | class TiledScaledBitmapGM : public GM { |
| 28 | public: |
| 29 | |
| 30 | TiledScaledBitmapGM() { |
| 31 | } |
| 32 | |
| 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 35 | return SkString("tiledscaledbitmap"); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 39 | return SkISize::Make(1016, 616); |
| 40 | } |
| 41 | |
tfarina | 752e7eb | 2014-12-20 06:53:43 -0800 | [diff] [blame] | 42 | static SkBitmap make_bm(int width, int height) { |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 43 | SkBitmap bm; |
| 44 | bm.allocN32Pixels(width, height); |
| 45 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 46 | SkCanvas canvas(bm); |
| 47 | SkPaint paint; |
| 48 | paint.setAntiAlias(true); |
| 49 | canvas.drawCircle(width/2.f, height/2.f, width/4.f, paint); |
tfarina | 752e7eb | 2014-12-20 06:53:43 -0800 | [diff] [blame] | 50 | return bm; |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | void onOnceBeforeDraw() override { |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 54 | fBitmap = make_bm(360, 288); |
| 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | void onDraw(SkCanvas* canvas) override { |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 58 | SkPaint paint; |
| 59 | |
| 60 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 61 | paint.setFilterQuality(kHigh_SkFilterQuality); |
humper | 535e3b2 | 2014-10-27 10:32:06 -0700 | [diff] [blame] | 62 | |
| 63 | SkMatrix mat; |
| 64 | mat.setScale(121.f/360.f, 93.f/288.f); |
| 65 | mat.postTranslate(-72, -72); |
| 66 | |
| 67 | SkShader *shader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &mat); |
| 68 | paint.setShader(shader); |
| 69 | |
| 70 | SkSafeUnref(shader); |
| 71 | canvas->drawRectCoords(8,8,1008, 608, paint); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | SkBitmap fBitmap; |
| 76 | |
| 77 | typedef GM INHERITED; |
| 78 | }; |
| 79 | |
| 80 | ////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | DEF_GM(return SkNEW(TiledScaledBitmapGM);) |
| 83 | |
| 84 | } |