blob: ea028f83f9fbca043861211823597ba04eaae386 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkTypes.h"
reedc8e47652015-02-19 11:39:46 -080020
reed9ce9d672016-03-17 10:51:11 -070021static sk_sp<SkImage> make_image() {
reedc8e47652015-02-19 11:39:46 -080022 const SkImageInfo info = SkImageInfo::MakeN32Premul(319, 52);
Kevin Lubick5c93acf2023-05-09 12:11:43 -040023 auto surface(SkSurfaces::Raster(info));
reedc8e47652015-02-19 11:39:46 -080024 SkCanvas* canvas = surface->getCanvas();
Mike Kleind46dce32018-08-16 10:17:03 -040025 canvas->drawColor(0xFFF8F8F8);
reedc8e47652015-02-19 11:39:46 -080026
27 SkPaint paint;
28 paint.setAntiAlias(true);
29
30 paint.setStyle(SkPaint::kStroke_Style);
31 for (int i = 0; i < 20; ++i) {
32 canvas->drawCircle(-4, 25, 20, paint);
33 canvas->translate(25, 0);
34 }
reed9ce9d672016-03-17 10:51:11 -070035 return surface->makeImageSnapshot();
reedc8e47652015-02-19 11:39:46 -080036}
37
halcanary2a243382015-09-09 08:16:41 -070038DEF_SIMPLE_GM(mipmap, canvas, 400, 200) {
reed9ce9d672016-03-17 10:51:11 -070039 sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data));
reedc8e47652015-02-19 11:39:46 -080040
reedc8e47652015-02-19 11:39:46 -080041 const SkRect dst = SkRect::MakeWH(177, 15);
42
reedc8e47652015-02-19 11:39:46 -080043 SkString str;
44 str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
Hal Canarydf2d27e2019-01-08 09:38:02 -050045// canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
reedc8e47652015-02-19 11:39:46 -080046
Mike Reed07c5f522021-01-23 12:23:23 -050047 const SkSamplingOptions samplings[] = {
48 SkSamplingOptions(SkFilterMode::kNearest),
49 SkSamplingOptions(SkFilterMode::kLinear),
50 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
Mike Reedf3ac2af2021-02-05 12:55:38 -050051 SkSamplingOptions(SkCubicResampler::Mitchell()),
Mike Reed07c5f522021-01-23 12:23:23 -050052 };
53
reedc8e47652015-02-19 11:39:46 -080054 canvas->translate(20, 20);
Herb Derbyc37b3862022-06-21 09:49:17 -040055 for (size_t i = 0; i < std::size(samplings); ++i) {
Mike Reed07c5f522021-01-23 12:23:23 -050056 canvas->drawImageRect(img.get(), dst, samplings[i], nullptr);
reedc8e47652015-02-19 11:39:46 -080057 canvas->translate(0, 20);
58 }
Mike Reed07c5f522021-01-23 12:23:23 -050059 canvas->drawImage(img.get(), 20, 20);
reedc8e47652015-02-19 11:39:46 -080060}
reed6644d932016-06-10 11:41:47 -070061
62///////////////////////////////////////////////////////////////////////////////////////////////////
63
64// create a circle image computed raw, so we can wrap it as a linear or srgb image
65static sk_sp<SkImage> make(sk_sp<SkColorSpace> cs) {
66 const int N = 100;
67 SkImageInfo info = SkImageInfo::Make(N, N, kN32_SkColorType, kPremul_SkAlphaType, cs);
68 SkBitmap bm;
69 bm.allocPixels(info);
70
71 for (int y = 0; y < N; ++y) {
72 for (int x = 0; x < N; ++x) {
73 *bm.getAddr32(x, y) = (x ^ y) & 1 ? 0xFFFFFFFF : 0xFF000000;
74 }
75 }
76 bm.setImmutable();
Mike Reedac9f0c92020-12-23 10:11:33 -050077 return bm.asImage();
reed6644d932016-06-10 11:41:47 -070078}
79
80static void show_mips(SkCanvas* canvas, SkImage* img) {
Mike Reed07c5f522021-01-23 12:23:23 -050081 SkSamplingOptions sampling(SkFilterMode::kLinear,
82 SkMipmapMode::kLinear);
reed6644d932016-06-10 11:41:47 -070083
reed3cc37d32016-06-11 04:48:12 -070084 // Want to ensure we never draw fractional pixels, so we use an IRect
85 SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
reed6644d932016-06-10 11:41:47 -070086 while (dst.width() > 5) {
Mike Reed07c5f522021-01-23 12:23:23 -050087 canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
reed6644d932016-06-10 11:41:47 -070088 dst.offset(dst.width() + 10, 0);
reed3cc37d32016-06-11 04:48:12 -070089 dst.fRight = dst.fLeft + dst.width()/2;
90 dst.fBottom = dst.fTop + dst.height()/2;
reed6644d932016-06-10 11:41:47 -070091 }
92}
93
94/*
95 * Ensure that in L32 drawing mode, both images/mips look the same as each other, and
96 * their mips are darker than the original (since the mips should ignore the gamma in L32).
97 *
98 * Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
99 * the mip levels match the original in brightness).
100 */
101DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) {
102 sk_sp<SkImage> limg = make(nullptr);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500103 sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB());
reed6644d932016-06-10 11:41:47 -0700104
105 canvas->translate(10, 10);
106 show_mips(canvas, limg.get());
107 canvas->translate(0, limg->height() + 10.0f);
108 show_mips(canvas, simg.get());
109}
110
brianosman79b15f62016-06-21 13:40:12 -0700111///////////////////////////////////////////////////////////////////////////////////////////////////
112
113// create a gradient image computed raw, so we can wrap it as a linear or srgb image
114static sk_sp<SkImage> make_g8_gradient(sk_sp<SkColorSpace> cs) {
115 const int N = 100;
116 SkImageInfo info = SkImageInfo::Make(N, N, kGray_8_SkColorType, kOpaque_SkAlphaType, cs);
117 SkBitmap bm;
118 bm.allocPixels(info);
119
120 for (int y = 0; y < N; ++y) {
121 for (int x = 0; x < N; ++x) {
122 *bm.getAddr8(x, y) = static_cast<uint8_t>(255.0f * ((x + y) / (2.0f * (N - 1))));
123 }
124 }
125 bm.setImmutable();
Mike Reedac9f0c92020-12-23 10:11:33 -0500126 return bm.asImage();
brianosman79b15f62016-06-21 13:40:12 -0700127}
128
129static void show_mips_only(SkCanvas* canvas, SkImage* img) {
Mike Reed07c5f522021-01-23 12:23:23 -0500130 SkSamplingOptions sampling(SkFilterMode::kLinear,
131 SkMipmapMode::kLinear);
brianosman79b15f62016-06-21 13:40:12 -0700132
133 // Want to ensure we never draw fractional pixels, so we use an IRect
134 SkIRect dst = SkIRect::MakeWH(img->width() / 2, img->height() / 2);
135 while (dst.width() > 5) {
Mike Reed07c5f522021-01-23 12:23:23 -0500136 canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
brianosman79b15f62016-06-21 13:40:12 -0700137 dst.offset(dst.width() + 10, 0);
138 dst.fRight = dst.fLeft + dst.width() / 2;
139 dst.fBottom = dst.fTop + dst.height() / 2;
140 }
141}
142
143/*
144 * Ensure that in L32 drawing mode, both images/mips look the same as each other, and
145 * their mips are darker than the original (since the mips should ignore the gamma in L32).
146 *
147 * Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
148 * the mip levels match the original in brightness).
brianosman79b15f62016-06-21 13:40:12 -0700149 */
150DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
151 sk_sp<SkImage> limg = make_g8_gradient(nullptr);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500152 sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB());
brianosman79b15f62016-06-21 13:40:12 -0700153
154 canvas->translate(10, 10);
155 show_mips_only(canvas, limg.get());
156 canvas->translate(0, limg->height() + 10.0f);
157 show_mips_only(canvas, simg.get());
158}