Debian Codegraph @

gm/showmiplevels.cpp

raw
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/SkImageInfo.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTileMode.h"
22 #include "src/core/SkColorPriv.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 /**
32 * This GM explicitly makes mip map levels solid colors so we can see which are being used.
33 *
34 * Rows 0 and 1 don't use mip maps, so the rocket ship gets smaller (and tiled) from
35 * left to right. The only difference is the filter mode - Row 0 (nearest) looks blockier
36 * than Row 1 (linear).
37 *
38 * Rows 2 and 3 use the nearest mip map mode, which means we expect
39 * Everything other than the first two columns (which are closest to mip level 0 - original image)
40 * will be solid colors (Red, Green, Blue) to show this is happening. The sublte difference in
41 * filtering can still be observed in Column 1.
42 *
43 * Rows 4 and 5 use a linear mipmap mode, which means the we'll see a blend of red and spaceships
44 * for columns 1 and 2, and non-pure colors for the remaining columns as the breakpoints don't
45 * land directly on the mip level changes.
46 *
47 * If the image is being rendered and all rows show the spaceships from left to right, the
48 * hand-crafted mips are being lost somewhere in rendering and that is *wrong*.
49 */
50 class ShowMipLevels : public skiagm::GM {
51 sk_sp<SkImage> fImg;
52
53 SkString getName() const override { return SkString("showmiplevels_explicit"); }
54
55 SkISize getISize() override { return {1130, 970}; }
56
57 void onOnceBeforeDraw() override {
58 fImg = ToolUtils::GetResourceAsImage("images/ship.png");
59 fImg = fImg->makeRasterImage(nullptr); // makeWithMips only works on raster for now
60
61 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
62
63 SkMipmapBuilder builder(fImg->imageInfo());
64 // The number of levels is derived from the size of the image, so we intentionally
65 // pick an image that's big enough to support 3+ levels.
66 SkASSERT(builder.countLevels() >= (int)std::size(colors));
67 for (int i = 0; i < builder.countLevels(); ++i) {
68 auto surf = SkSurfaces::WrapPixels(builder.level(i));
69 surf->getCanvas()->drawColor(colors[i % std::size(colors)]);
70 }
71 fImg = builder.attachTo(fImg);
72 }
73
74 DrawResult onDraw(SkCanvas* canvas, SkString*) override {
75 canvas->drawColor(0xFFDDDDDD);
76
77 canvas->translate(10, 10);
78 for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
79 for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
80 canvas->translate(0, draw_downscaling(canvas, {fm, mm}));
81 }
82 }
83 return DrawResult::kOk;
84 }
85
86 private:
87 SkScalar draw_downscaling(SkCanvas* canvas, SkSamplingOptions sampling) {
88 SkAutoCanvasRestore acr(canvas, true);
89
90 SkPaint paint;
91 SkRect r = {0, 0, 150, 150};
92 for (float scale = 1; scale >= 0.1f; scale *= 0.7f) {
93 SkMatrix matrix = SkMatrix::Scale(scale, scale);
94 paint.setShader(fImg->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
95 sampling, &matrix));
96 canvas->drawRect(r, paint);
97 canvas->translate(r.width() + 10, 0);
98 }
99 return r.height() + 10;
100 }
101 };
102 DEF_GM(return new ShowMipLevels;)