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