reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/private/SkNx.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 22 | #include "src/core/SkMipmap.h" |
Mike Reed | 6affa2c | 2020-11-30 13:09:24 -0500 | [diff] [blame] | 23 | #include "src/core/SkMipmapBuilder.h" |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame] | 24 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/ToolUtils.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 26 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include <math.h> |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 28 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 29 | class ShowMipLevels3 : public skiagm::GM { |
| 30 | sk_sp<SkImage> fImg; |
| 31 | |
| 32 | SkString onShortName() override { return SkString("showmiplevels_explicit"); } |
| 33 | |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 34 | SkISize onISize() override { return {1130, 970}; } |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 35 | |
| 36 | void onOnceBeforeDraw() override { |
| 37 | fImg = GetResourceAsImage("images/ship.png"); |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 38 | fImg = fImg->makeRasterImage(); // makeWithMips only works on raster for now |
| 39 | |
| 40 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 41 | |
| 42 | SkMipmapBuilder builder(fImg->imageInfo()); |
| 43 | for (int i = 0; i < builder.countLevels(); ++i) { |
| 44 | auto surf = SkSurface::MakeRasterDirect(builder.level(i)); |
| 45 | surf->getCanvas()->drawColor(colors[i % SK_ARRAY_COUNT(colors)]); |
| 46 | } |
Mike Reed | b4aa639 | 2020-09-17 13:58:26 -0400 | [diff] [blame] | 47 | fImg = builder.attachTo(fImg.get()); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Mike Reed | fd6f709 | 2020-07-16 16:29:16 -0400 | [diff] [blame] | 50 | DrawResult onDraw(SkCanvas* canvas, SkString*) override { |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 51 | if (canvas->recordingContext()) { |
Mike Reed | fd6f709 | 2020-07-16 16:29:16 -0400 | [diff] [blame] | 52 | // mips not supported yet |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 53 | return DrawResult::kSkip; |
| 54 | } |
| 55 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 56 | canvas->drawColor(0xFFDDDDDD); |
| 57 | |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 58 | canvas->translate(10, 10); |
Mike Reed | fd6f709 | 2020-07-16 16:29:16 -0400 | [diff] [blame] | 59 | for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) { |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 60 | for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) { |
| 61 | canvas->translate(0, draw_downscaling(canvas, {fm, mm})); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 62 | } |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 63 | } |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 64 | return DrawResult::kOk; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 68 | SkScalar draw_downscaling(SkCanvas* canvas, SkSamplingOptions sampling) { |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 69 | SkAutoCanvasRestore acr(canvas, true); |
| 70 | |
| 71 | SkPaint paint; |
| 72 | SkRect r = {0, 0, 150, 150}; |
| 73 | for (float scale = 1; scale >= 0.1f; scale *= 0.7f) { |
| 74 | SkMatrix matrix = SkMatrix::Scale(scale, scale); |
| 75 | paint.setShader(fImg->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 76 | sampling, &matrix)); |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 77 | canvas->drawRect(r, paint); |
| 78 | canvas->translate(r.width() + 10, 0); |
| 79 | } |
| 80 | return r.height() + 10; |
| 81 | } |
| 82 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 83 | using INHERITED = skiagm::GM; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 84 | }; |
| 85 | DEF_GM( return new ShowMipLevels3; ) |