blob: 1705d8ec7a76b8220757b36ea932c4ca75ad9b3b [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:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000035 SkString getName() const override { return SkString("tiledscaledbitmap"); }
humper535e3b22014-10-27 10:32:06 -070036
Leandro Lovisolo8f023882023-08-15 21:13:52 +000037 SkISize getISize() override { return SkISize::Make(1016, 616); }
humper535e3b22014-10-27 10:32:06 -070038
tfarina752e7eb2014-12-20 06:53:43 -080039 static SkBitmap make_bm(int width, int height) {
humper535e3b22014-10-27 10:32:06 -070040 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);
tfarina752e7eb2014-12-20 06:53:43 -080047 return bm;
humper535e3b22014-10-27 10:32:06 -070048 }
49
mtklein36352bf2015-03-25 18:17:31 -070050 void onOnceBeforeDraw() override {
humper535e3b22014-10-27 10:32:06 -070051 fBitmap = make_bm(360, 288);
52 }
53
mtklein36352bf2015-03-25 18:17:31 -070054 void onDraw(SkCanvas* canvas) override {
humper535e3b22014-10-27 10:32:06 -070055 SkPaint paint;
56
57 paint.setAntiAlias(true);
humper535e3b22014-10-27 10:32:06 -070058
59 SkMatrix mat;
60 mat.setScale(121.f/360.f, 93.f/288.f);
61 mat.postTranslate(-72, -72);
62
Mike Reed057fcbe2020-12-12 14:31:25 -050063 paint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
Mike Reedf3ac2af2021-02-05 12:55:38 -050064 SkSamplingOptions(SkCubicResampler::Mitchell()), mat));
Mike Reed3661bc92017-02-22 13:21:42 -050065 canvas->drawRect({ 8, 8, 1008, 608 }, paint);
humper535e3b22014-10-27 10:32:06 -070066 }
67
68private:
69 SkBitmap fBitmap;
70
John Stiles7571f9e2020-09-02 22:42:33 -040071 using INHERITED = GM;
humper535e3b22014-10-27 10:32:06 -070072};
73
74//////////////////////////////////////////////////////////////////////////////
75
halcanary385fe4d2015-08-26 13:07:48 -070076DEF_GM(return new TiledScaledBitmapGM;)
John Stilesa6841be2020-08-06 14:11:56 -040077} // namespace skiagm