reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColorSpace.h" |
| 12 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkImageInfo.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/core/SkTypes.h" |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 21 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 22 | static sk_sp<SkImage> make_image() { |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 23 | const SkImageInfo info = SkImageInfo::MakeN32Premul(319, 52); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 24 | auto surface(SkSurface::MakeRaster(info)); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 25 | SkCanvas* canvas = surface->getCanvas(); |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 26 | canvas->drawColor(0xFFF8F8F8); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 27 | |
| 28 | SkPaint paint; |
| 29 | paint.setAntiAlias(true); |
| 30 | |
| 31 | paint.setStyle(SkPaint::kStroke_Style); |
| 32 | for (int i = 0; i < 20; ++i) { |
| 33 | canvas->drawCircle(-4, 25, 20, paint); |
| 34 | canvas->translate(25, 0); |
| 35 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 36 | return surface->makeImageSnapshot(); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 37 | } |
| 38 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 39 | DEF_SIMPLE_GM(mipmap, canvas, 400, 200) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 40 | sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data)); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 41 | |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 42 | const SkRect dst = SkRect::MakeWH(177, 15); |
| 43 | |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 44 | SkString str; |
| 45 | str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height()); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 46 | // canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 47 | |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 48 | const SkSamplingOptions samplings[] = { |
| 49 | SkSamplingOptions(SkFilterMode::kNearest), |
| 50 | SkSamplingOptions(SkFilterMode::kLinear), |
| 51 | SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear), |
Mike Reed | f3ac2af | 2021-02-05 12:55:38 -0500 | [diff] [blame] | 52 | SkSamplingOptions(SkCubicResampler::Mitchell()), |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 53 | }; |
| 54 | |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 55 | canvas->translate(20, 20); |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 56 | for (size_t i = 0; i < SK_ARRAY_COUNT(samplings); ++i) { |
| 57 | canvas->drawImageRect(img.get(), dst, samplings[i], nullptr); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 58 | canvas->translate(0, 20); |
| 59 | } |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 60 | canvas->drawImage(img.get(), 20, 20); |
reed | c8e4765 | 2015-02-19 11:39:46 -0800 | [diff] [blame] | 61 | } |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 62 | |
| 63 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | // create a circle image computed raw, so we can wrap it as a linear or srgb image |
| 66 | static sk_sp<SkImage> make(sk_sp<SkColorSpace> cs) { |
| 67 | const int N = 100; |
| 68 | SkImageInfo info = SkImageInfo::Make(N, N, kN32_SkColorType, kPremul_SkAlphaType, cs); |
| 69 | SkBitmap bm; |
| 70 | bm.allocPixels(info); |
| 71 | |
| 72 | for (int y = 0; y < N; ++y) { |
| 73 | for (int x = 0; x < N; ++x) { |
| 74 | *bm.getAddr32(x, y) = (x ^ y) & 1 ? 0xFFFFFFFF : 0xFF000000; |
| 75 | } |
| 76 | } |
| 77 | bm.setImmutable(); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 78 | return bm.asImage(); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void show_mips(SkCanvas* canvas, SkImage* img) { |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 82 | SkSamplingOptions sampling(SkFilterMode::kLinear, |
| 83 | SkMipmapMode::kLinear); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 84 | |
reed | 3cc37d3 | 2016-06-11 04:48:12 -0700 | [diff] [blame] | 85 | // Want to ensure we never draw fractional pixels, so we use an IRect |
| 86 | SkIRect dst = SkIRect::MakeWH(img->width(), img->height()); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 87 | while (dst.width() > 5) { |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 88 | canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 89 | dst.offset(dst.width() + 10, 0); |
reed | 3cc37d3 | 2016-06-11 04:48:12 -0700 | [diff] [blame] | 90 | dst.fRight = dst.fLeft + dst.width()/2; |
| 91 | dst.fBottom = dst.fTop + dst.height()/2; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Ensure that in L32 drawing mode, both images/mips look the same as each other, and |
| 97 | * their mips are darker than the original (since the mips should ignore the gamma in L32). |
| 98 | * |
| 99 | * Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e. |
| 100 | * the mip levels match the original in brightness). |
| 101 | */ |
| 102 | DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) { |
| 103 | sk_sp<SkImage> limg = make(nullptr); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 104 | sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB()); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 105 | |
| 106 | canvas->translate(10, 10); |
| 107 | show_mips(canvas, limg.get()); |
| 108 | canvas->translate(0, limg->height() + 10.0f); |
| 109 | show_mips(canvas, simg.get()); |
| 110 | } |
| 111 | |
brianosman | 79b15f6 | 2016-06-21 13:40:12 -0700 | [diff] [blame] | 112 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | // create a gradient image computed raw, so we can wrap it as a linear or srgb image |
| 115 | static sk_sp<SkImage> make_g8_gradient(sk_sp<SkColorSpace> cs) { |
| 116 | const int N = 100; |
| 117 | SkImageInfo info = SkImageInfo::Make(N, N, kGray_8_SkColorType, kOpaque_SkAlphaType, cs); |
| 118 | SkBitmap bm; |
| 119 | bm.allocPixels(info); |
| 120 | |
| 121 | for (int y = 0; y < N; ++y) { |
| 122 | for (int x = 0; x < N; ++x) { |
| 123 | *bm.getAddr8(x, y) = static_cast<uint8_t>(255.0f * ((x + y) / (2.0f * (N - 1)))); |
| 124 | } |
| 125 | } |
| 126 | bm.setImmutable(); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 127 | return bm.asImage(); |
brianosman | 79b15f6 | 2016-06-21 13:40:12 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void show_mips_only(SkCanvas* canvas, SkImage* img) { |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 131 | SkSamplingOptions sampling(SkFilterMode::kLinear, |
| 132 | SkMipmapMode::kLinear); |
brianosman | 79b15f6 | 2016-06-21 13:40:12 -0700 | [diff] [blame] | 133 | |
| 134 | // Want to ensure we never draw fractional pixels, so we use an IRect |
| 135 | SkIRect dst = SkIRect::MakeWH(img->width() / 2, img->height() / 2); |
| 136 | while (dst.width() > 5) { |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 137 | canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr); |
brianosman | 79b15f6 | 2016-06-21 13:40:12 -0700 | [diff] [blame] | 138 | dst.offset(dst.width() + 10, 0); |
| 139 | dst.fRight = dst.fLeft + dst.width() / 2; |
| 140 | dst.fBottom = dst.fTop + dst.height() / 2; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Ensure that in L32 drawing mode, both images/mips look the same as each other, and |
| 146 | * their mips are darker than the original (since the mips should ignore the gamma in L32). |
| 147 | * |
| 148 | * Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e. |
| 149 | * the mip levels match the original in brightness). |
brianosman | 79b15f6 | 2016-06-21 13:40:12 -0700 | [diff] [blame] | 150 | */ |
| 151 | DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) { |
| 152 | sk_sp<SkImage> limg = make_g8_gradient(nullptr); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 153 | sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB()); |
brianosman | 79b15f6 | 2016-06-21 13:40:12 -0700 | [diff] [blame] | 154 | |
| 155 | canvas->translate(10, 10); |
| 156 | show_mips_only(canvas, limg.get()); |
| 157 | canvas->translate(0, limg->height() + 10.0f); |
| 158 | show_mips_only(canvas, simg.get()); |
| 159 | } |