blob: 40c9deb9cf2b03653941723c3fdfff0979624c6e [file] [log] [blame]
reedc8e47652015-02-19 11:39:46 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColorSpace.h"
12#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#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 Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkTypes.h"
reedc8e47652015-02-19 11:39:46 -080021
reed9ce9d672016-03-17 10:51:11 -070022static sk_sp<SkImage> make_image() {
reedc8e47652015-02-19 11:39:46 -080023 const SkImageInfo info = SkImageInfo::MakeN32Premul(319, 52);
reede8f30622016-03-23 18:59:25 -070024 auto surface(SkSurface::MakeRaster(info));
reedc8e47652015-02-19 11:39:46 -080025 SkCanvas* canvas = surface->getCanvas();
Mike Kleind46dce32018-08-16 10:17:03 -040026 canvas->drawColor(0xFFF8F8F8);
reedc8e47652015-02-19 11:39:46 -080027
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 }
reed9ce9d672016-03-17 10:51:11 -070036 return surface->makeImageSnapshot();
reedc8e47652015-02-19 11:39:46 -080037}
38
halcanary2a243382015-09-09 08:16:41 -070039DEF_SIMPLE_GM(mipmap, canvas, 400, 200) {
reed9ce9d672016-03-17 10:51:11 -070040 sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data));
reedc8e47652015-02-19 11:39:46 -080041
reedc8e47652015-02-19 11:39:46 -080042 const SkRect dst = SkRect::MakeWH(177, 15);
43
reedc8e47652015-02-19 11:39:46 -080044 SkString str;
45 str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
Hal Canarydf2d27e2019-01-08 09:38:02 -050046// canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
reedc8e47652015-02-19 11:39:46 -080047
Mike Reed07c5f522021-01-23 12:23:23 -050048 const SkSamplingOptions samplings[] = {
49 SkSamplingOptions(SkFilterMode::kNearest),
50 SkSamplingOptions(SkFilterMode::kLinear),
51 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
Mike Reedf3ac2af2021-02-05 12:55:38 -050052 SkSamplingOptions(SkCubicResampler::Mitchell()),
Mike Reed07c5f522021-01-23 12:23:23 -050053 };
54
reedc8e47652015-02-19 11:39:46 -080055 canvas->translate(20, 20);
Mike Reed07c5f522021-01-23 12:23:23 -050056 for (size_t i = 0; i < SK_ARRAY_COUNT(samplings); ++i) {
57 canvas->drawImageRect(img.get(), dst, samplings[i], nullptr);
reedc8e47652015-02-19 11:39:46 -080058 canvas->translate(0, 20);
59 }
Mike Reed07c5f522021-01-23 12:23:23 -050060 canvas->drawImage(img.get(), 20, 20);
reedc8e47652015-02-19 11:39:46 -080061}
reed6644d932016-06-10 11:41:47 -070062
63///////////////////////////////////////////////////////////////////////////////////////////////////
64
65// create a circle image computed raw, so we can wrap it as a linear or srgb image
66static 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 Reedac9f0c92020-12-23 10:11:33 -050078 return bm.asImage();
reed6644d932016-06-10 11:41:47 -070079}
80
81static void show_mips(SkCanvas* canvas, SkImage* img) {
Mike Reed07c5f522021-01-23 12:23:23 -050082 SkSamplingOptions sampling(SkFilterMode::kLinear,
83 SkMipmapMode::kLinear);
reed6644d932016-06-10 11:41:47 -070084
reed3cc37d32016-06-11 04:48:12 -070085 // Want to ensure we never draw fractional pixels, so we use an IRect
86 SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
reed6644d932016-06-10 11:41:47 -070087 while (dst.width() > 5) {
Mike Reed07c5f522021-01-23 12:23:23 -050088 canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
reed6644d932016-06-10 11:41:47 -070089 dst.offset(dst.width() + 10, 0);
reed3cc37d32016-06-11 04:48:12 -070090 dst.fRight = dst.fLeft + dst.width()/2;
91 dst.fBottom = dst.fTop + dst.height()/2;
reed6644d932016-06-10 11:41:47 -070092 }
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 */
102DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) {
103 sk_sp<SkImage> limg = make(nullptr);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500104 sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB());
reed6644d932016-06-10 11:41:47 -0700105
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
brianosman79b15f62016-06-21 13:40:12 -0700112///////////////////////////////////////////////////////////////////////////////////////////////////
113
114// create a gradient image computed raw, so we can wrap it as a linear or srgb image
115static 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 Reedac9f0c92020-12-23 10:11:33 -0500127 return bm.asImage();
brianosman79b15f62016-06-21 13:40:12 -0700128}
129
130static void show_mips_only(SkCanvas* canvas, SkImage* img) {
Mike Reed07c5f522021-01-23 12:23:23 -0500131 SkSamplingOptions sampling(SkFilterMode::kLinear,
132 SkMipmapMode::kLinear);
brianosman79b15f62016-06-21 13:40:12 -0700133
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 Reed07c5f522021-01-23 12:23:23 -0500137 canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
brianosman79b15f62016-06-21 13:40:12 -0700138 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).
brianosman79b15f62016-06-21 13:40:12 -0700150 */
151DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
152 sk_sp<SkImage> limg = make_g8_gradient(nullptr);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500153 sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB());
brianosman79b15f62016-06-21 13:40:12 -0700154
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}