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