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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkFilterQuality.h"
13 #include "include/core/SkImage.h"
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"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTypes.h"
21 
make_image()22 static sk_sp<SkImage> make_image() {
23     const SkImageInfo info = SkImageInfo::MakeN32Premul(319, 52);
24     auto surface(SkSurface::MakeRaster(info));
25     SkCanvas* canvas = surface->getCanvas();
26     canvas->drawColor(0xFFF8F8F8);
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     }
36     return surface->makeImageSnapshot();
37 }
38 
39 DEF_SIMPLE_GM(mipmap, canvas, 400, 200) {
40     sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data));
41 
42     SkPaint paint;
43     const SkRect dst = SkRect::MakeWH(177, 15);
44 
45     SkString str;
46     str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
47 //    canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
48 
49     canvas->translate(20, 20);
50     for (int i = 0; i < 4; ++i) {
51         paint.setFilterQuality(SkFilterQuality(i));
52         canvas->drawImageRect(img.get(), dst, &paint);
53         canvas->translate(0, 20);
54     }
55     canvas->drawImage(img.get(), 20, 20, nullptr);
56 }
57 
58 ///////////////////////////////////////////////////////////////////////////////////////////////////
59 
60 // create a circle image computed raw, so we can wrap it as a linear or srgb image
make(sk_sp<SkColorSpace> cs)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 
show_mips(SkCanvas * canvas,SkImage * img)76 static void show_mips(SkCanvas* canvas, SkImage* img) {
77     SkPaint paint;
78     paint.setFilterQuality(kMedium_SkFilterQuality);
79 
80     // Want to ensure we never draw fractional pixels, so we use an IRect
81     SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
82     while (dst.width() > 5) {
83         canvas->drawImageRect(img, SkRect::Make(dst), &paint);
84         dst.offset(dst.width() + 10, 0);
85         dst.fRight = dst.fLeft + dst.width()/2;
86         dst.fBottom = dst.fTop + dst.height()/2;
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);
99     sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB());
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 
107 ///////////////////////////////////////////////////////////////////////////////////////////////////
108 
109 // create a gradient image computed raw, so we can wrap it as a linear or srgb image
make_g8_gradient(sk_sp<SkColorSpace> cs)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 
show_mips_only(SkCanvas * canvas,SkImage * img)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).
145  */
146 DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
147     sk_sp<SkImage> limg = make_g8_gradient(nullptr);
148     sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB());
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 }
155