xref: /aosp_15_r20/external/skia/gm/showmiplevels.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 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/SkColor.h"
12 #include "include/core/SkColorPriv.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTileMode.h"
23 #include "src/core/SkMipmap.h"
24 #include "src/core/SkMipmapBuilder.h"
25 #include "tools/DecodeUtils.h"
26 #include "tools/Resources.h"
27 #include "tools/ToolUtils.h"
28 
29 #include <math.h>
30 
31 class ShowMipLevels3 : public skiagm::GM {
32     sk_sp<SkImage> fImg;
33 
getName() const34     SkString getName() const override { return SkString("showmiplevels_explicit"); }
35 
getISize()36     SkISize getISize() override { return {1130, 970}; }
37 
onOnceBeforeDraw()38     void onOnceBeforeDraw() override {
39         fImg = ToolUtils::GetResourceAsImage("images/ship.png");
40         fImg = fImg->makeRasterImage(); // makeWithMips only works on raster for now
41 
42         const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
43 
44         SkMipmapBuilder builder(fImg->imageInfo());
45         for (int i = 0; i < builder.countLevels(); ++i) {
46             auto surf = SkSurfaces::WrapPixels(builder.level(i));
47             surf->getCanvas()->drawColor(colors[i % std::size(colors)]);
48         }
49         fImg = builder.attachTo(fImg);
50     }
51 
onDraw(SkCanvas * canvas,SkString *)52     DrawResult onDraw(SkCanvas* canvas, SkString*) override {
53         canvas->drawColor(0xFFDDDDDD);
54 
55         canvas->translate(10, 10);
56         for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
57             for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
58                 canvas->translate(0, draw_downscaling(canvas, {fm, mm}));
59             }
60         }
61         return DrawResult::kOk;
62     }
63 
64 private:
draw_downscaling(SkCanvas * canvas,SkSamplingOptions sampling)65     SkScalar draw_downscaling(SkCanvas* canvas, SkSamplingOptions sampling) {
66         SkAutoCanvasRestore acr(canvas, true);
67 
68         SkPaint paint;
69         SkRect r = {0, 0, 150, 150};
70         for (float scale = 1; scale >= 0.1f; scale *= 0.7f) {
71             SkMatrix matrix = SkMatrix::Scale(scale, scale);
72             paint.setShader(fImg->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
73                                              sampling, &matrix));
74             canvas->drawRect(r, paint);
75             canvas->translate(r.width() + 10, 0);
76         }
77         return r.height() + 10;
78     }
79 
80     using INHERITED = skiagm::GM;
81 };
82 DEF_GM( return new ShowMipLevels3; )
83