xref: /aosp_15_r20/external/skia/gm/filterindiabox.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2011 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/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypes.h"
18 #include "tools/DecodeUtils.h"
19 #include "tools/Resources.h"
20 #include "tools/ToolUtils.h"
21 
22 namespace {
computeSize(const SkBitmap & bm,const SkMatrix & mat)23 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
24     SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
25                                    SkIntToScalar(bm.height()));
26     mat.mapRect(&bounds);
27     return SkSize::Make(bounds.width(), bounds.height());
28 }
29 
draw_cell(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx,const SkSamplingOptions & sampling)30 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
31                       const SkSamplingOptions& sampling) {
32     SkAutoCanvasRestore acr(canvas, true);
33 
34     canvas->translate(dx, 0);
35     canvas->concat(mat);
36     canvas->drawImage(bm.asImage(), 0, 0, sampling);
37 }
38 
draw_row(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx)39 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
40     draw_cell(canvas, bm, mat, 0 * dx, SkSamplingOptions());
41     draw_cell(canvas, bm, mat, 1 * dx, SkSamplingOptions(SkFilterMode::kLinear));
42     draw_cell(canvas, bm, mat, 2 * dx, SkSamplingOptions(SkFilterMode::kLinear,
43                                                          SkMipmapMode::kLinear));
44     draw_cell(canvas, bm, mat, 3 * dx, SkSamplingOptions(SkCubicResampler::Mitchell()));
45 }
46 
47 class FilterIndiaBoxGM : public skiagm::GM {
48     SkBitmap    fBM;
49     SkMatrix    fMatrix[2];
50 
onOnceBeforeDraw()51     void onOnceBeforeDraw() override {
52         constexpr char kResource[] = "images/box.gif";
53         if (!ToolUtils::GetResourceAsBitmap(kResource, &fBM)) {
54             fBM.allocN32Pixels(1, 1);
55             fBM.eraseARGB(255, 255, 0 , 0); // red == bad
56         }
57         fBM.setImmutable();
58 
59         SkScalar cx = SkScalarHalf(fBM.width());
60         SkScalar cy = SkScalarHalf(fBM.height());
61 
62         float vertScale = 30.0f/55.0f;
63         float horizScale = 150.0f/200.0f;
64 
65         fMatrix[0].setScale(horizScale, vertScale);
66         fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
67     }
68 
getName() const69     SkString getName() const override { return SkString("filterindiabox"); }
70 
getISize()71     SkISize getISize() override { return {680, 130}; }
72 
onDraw(SkCanvas * canvas)73     void onDraw(SkCanvas* canvas) override {
74         canvas->translate(10, 10);
75         for (size_t i = 0; i < std::size(fMatrix); ++i) {
76             SkSize size = computeSize(fBM, fMatrix[i]);
77             size.fWidth += 20;
78             size.fHeight += 20;
79 
80             draw_row(canvas, fBM, fMatrix[i], size.fWidth);
81             canvas->translate(0, size.fHeight);
82         }
83     }
84 };
85 }  // namespace
86 
87 DEF_GM( return new FilterIndiaBoxGM(); )
88