blob: ca880b73c83df1d4ee19642c1912dab81c0b565c [file] [log] [blame]
humper535e3b22014-10-27 10:32:06 -07001/*
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 */
reed1a9b9642016-03-13 14:13:58 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPaint.h"
14#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTileMode.h"
humper535e3b22014-10-27 10:32:06 -070018
humper535e3b22014-10-27 10:32:06 -070019 /***
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
26namespace skiagm {
27
28class TiledScaledBitmapGM : public GM {
29public:
30
31 TiledScaledBitmapGM() {
32 }
33
34protected:
mtklein36352bf2015-03-25 18:17:31 -070035 SkString onShortName() override {
humper535e3b22014-10-27 10:32:06 -070036 return SkString("tiledscaledbitmap");
37 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 SkISize onISize() override {
humper535e3b22014-10-27 10:32:06 -070040 return SkISize::Make(1016, 616);
41 }
42
tfarina752e7eb2014-12-20 06:53:43 -080043 static SkBitmap make_bm(int width, int height) {
humper535e3b22014-10-27 10:32:06 -070044 SkBitmap bm;
45 bm.allocN32Pixels(width, height);
46 bm.eraseColor(SK_ColorTRANSPARENT);
47 SkCanvas canvas(bm);
48 SkPaint paint;
49 paint.setAntiAlias(true);
50 canvas.drawCircle(width/2.f, height/2.f, width/4.f, paint);
tfarina752e7eb2014-12-20 06:53:43 -080051 return bm;
humper535e3b22014-10-27 10:32:06 -070052 }
53
mtklein36352bf2015-03-25 18:17:31 -070054 void onOnceBeforeDraw() override {
humper535e3b22014-10-27 10:32:06 -070055 fBitmap = make_bm(360, 288);
56 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 void onDraw(SkCanvas* canvas) override {
humper535e3b22014-10-27 10:32:06 -070059 SkPaint paint;
60
61 paint.setAntiAlias(true);
humper535e3b22014-10-27 10:32:06 -070062
63 SkMatrix mat;
64 mat.setScale(121.f/360.f, 93.f/288.f);
65 mat.postTranslate(-72, -72);
66
Mike Reed057fcbe2020-12-12 14:31:25 -050067 paint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
Mike Reedf3ac2af2021-02-05 12:55:38 -050068 SkSamplingOptions(SkCubicResampler::Mitchell()), mat));
Mike Reed3661bc92017-02-22 13:21:42 -050069 canvas->drawRect({ 8, 8, 1008, 608 }, paint);
humper535e3b22014-10-27 10:32:06 -070070 }
71
72private:
73 SkBitmap fBitmap;
74
John Stiles7571f9e2020-09-02 22:42:33 -040075 using INHERITED = GM;
humper535e3b22014-10-27 10:32:06 -070076};
77
78//////////////////////////////////////////////////////////////////////////////
79
halcanary385fe4d2015-08-26 13:07:48 -070080DEF_GM(return new TiledScaledBitmapGM;)
John Stilesa6841be2020-08-06 14:11:56 -040081} // namespace skiagm