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