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