blob: ea86883496a34f2ce6c2d9e7c3c8a0e695e72943 [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
42 SkPaint paint;
43 const SkRect dst = SkRect::MakeWH(177, 15);
44
reedc8e47652015-02-19 11:39:46 -080045 SkString str;
46 str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
Hal Canarydf2d27e2019-01-08 09:38:02 -050047// canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
reedc8e47652015-02-19 11:39:46 -080048
49 canvas->translate(20, 20);
50 for (int i = 0; i < 4; ++i) {
reed93a12152015-03-16 10:08:34 -070051 paint.setFilterQuality(SkFilterQuality(i));
reed9ce9d672016-03-17 10:51:11 -070052 canvas->drawImageRect(img.get(), dst, &paint);
reedc8e47652015-02-19 11:39:46 -080053 canvas->translate(0, 20);
54 }
reed9ce9d672016-03-17 10:51:11 -070055 canvas->drawImage(img.get(), 20, 20, nullptr);
reedc8e47652015-02-19 11:39:46 -080056}
reed6644d932016-06-10 11:41:47 -070057
58///////////////////////////////////////////////////////////////////////////////////////////////////
59
60// create a circle image computed raw, so we can wrap it as a linear or srgb image
61static 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
76static void show_mips(SkCanvas* canvas, SkImage* img) {
77 SkPaint paint;
78 paint.setFilterQuality(kMedium_SkFilterQuality);
79
reed3cc37d32016-06-11 04:48:12 -070080 // Want to ensure we never draw fractional pixels, so we use an IRect
81 SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
reed6644d932016-06-10 11:41:47 -070082 while (dst.width() > 5) {
reed3cc37d32016-06-11 04:48:12 -070083 canvas->drawImageRect(img, SkRect::Make(dst), &paint);
reed6644d932016-06-10 11:41:47 -070084 dst.offset(dst.width() + 10, 0);
reed3cc37d32016-06-11 04:48:12 -070085 dst.fRight = dst.fLeft + dst.width()/2;
86 dst.fBottom = dst.fTop + dst.height()/2;
reed6644d932016-06-10 11:41:47 -070087 }
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 */
97DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) {
98 sk_sp<SkImage> limg = make(nullptr);
Matt Sarett77a7a1b2017-02-07 13:56:11 -050099 sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB());
reed6644d932016-06-10 11:41:47 -0700100
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
brianosman79b15f62016-06-21 13:40:12 -0700107///////////////////////////////////////////////////////////////////////////////////////////////////
108
109// create a gradient image computed raw, so we can wrap it as a linear or srgb image
110static 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
125static 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).
brianosman79b15f62016-06-21 13:40:12 -0700145 */
146DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
147 sk_sp<SkImage> limg = make_g8_gradient(nullptr);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500148 sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB());
brianosman79b15f62016-06-21 13:40:12 -0700149
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}