• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 
10 #include "Resources.h"
11 #include "SkBitmapScaler.h"
12 #include "SkGradientShader.h"
13 #include "SkTypeface.h"
14 #include "SkImageDecoder.h"
15 #include "SkStream.h"
16 #include "SkPaint.h"
17 #include "SkMipMap.h"
18 #include "Resources.h"
19 #include "sk_tool_utils.h"
20 
21 #define SHOW_MIP_COLOR  0xFF000000
22 
make_bitmap(int w,int h)23 static SkBitmap make_bitmap(int w, int h) {
24     SkBitmap bm;
25     bm.allocN32Pixels(w, h);
26     SkCanvas canvas(bm);
27     canvas.clear(0xFFFFFFFF);
28     SkPaint paint;
29     paint.setStyle(SkPaint::kStroke_Style);
30     paint.setStrokeWidth(w / 16.0f);
31     paint.setColor(SHOW_MIP_COLOR);
32     canvas.drawCircle(w/2.0f, h/2.0f, w/3.0f, paint);
33     return bm;
34 }
35 
make_bitmap2(int w,int h)36 static SkBitmap make_bitmap2(int w, int h) {
37     SkBitmap bm;
38     bm.allocN32Pixels(w, h);
39     SkCanvas canvas(bm);
40     canvas.clear(0xFFFFFFFF);
41     SkPaint paint;
42     paint.setColor(SHOW_MIP_COLOR);
43     paint.setStyle(SkPaint::kStroke_Style);
44 
45     SkScalar inset = 2;
46     SkRect r = SkRect::MakeIWH(w, h).makeInset(0.5f, 0.5f);
47     while (r.width() > 4) {
48         canvas.drawRect(r, paint);
49         r.inset(inset, inset);
50         inset += 1;
51     }
52     return bm;
53 }
54 
55 #include "SkNx.h"
make_bitmap3(int w,int h)56 static SkBitmap make_bitmap3(int w, int h) {
57     SkBitmap bm;
58     bm.allocN32Pixels(w, h);
59     SkCanvas canvas(bm);
60     canvas.clear(0xFFFFFFFF);
61     SkPaint paint;
62     paint.setStyle(SkPaint::kStroke_Style);
63     paint.setStrokeWidth(2.1f);
64     paint.setColor(SHOW_MIP_COLOR);
65 
66     SkScalar s = SkIntToScalar(w);
67     Sk4f p(s, -s, -s, s);
68     Sk4f d(5);
69     while (p[1] < s) {
70         canvas.drawLine(p[0],p[1], p[2], p[3], paint);
71         p = p + d;
72     }
73     return bm;
74 }
75 
76 class ShowMipLevels : public skiagm::GM {
77     const int fN;
78     SkBitmap  fBM[4];
79 
80 public:
gamma(unsigned n)81     static unsigned gamma(unsigned n) {
82         float x = n / 255.0f;
83 #if 0
84         x = sqrtf(x);
85 #else
86         if (x > 0.0031308f) {
87             x = 1.055f * (powf(x, (1.0f / 2.4f))) - 0.055f;
88         } else {
89             x = 12.92f * x;
90         }
91 #endif
92         return (int)(x * 255);
93     }
94 
apply_gamma(const SkBitmap & bm)95     static void apply_gamma(const SkBitmap& bm) {
96         return; // below is our experiment for sRGB correction
97         bm.lockPixels();
98         for (int y = 0; y < bm.height(); ++y) {
99             for (int x = 0; x < bm.width(); ++x) {
100                 SkPMColor c = *bm.getAddr32(x, y);
101                 unsigned r = gamma(SkGetPackedR32(c));
102                 unsigned g = gamma(SkGetPackedG32(c));
103                 unsigned b = gamma(SkGetPackedB32(c));
104                 *bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
105             }
106         }
107     }
108 
ShowMipLevels(int N)109     ShowMipLevels(int N) : fN(N) {
110         fBM[0] = sk_tool_utils::create_checkerboard_bitmap(N, N, SK_ColorBLACK, SK_ColorWHITE, 2);
111         fBM[1] = make_bitmap(N, N);
112         fBM[2] = make_bitmap2(N, N);
113         fBM[3] = make_bitmap3(N, N);
114     }
115 
116 protected:
117 
onShortName()118     SkString onShortName() override {
119         SkString str;
120         str.printf("showmiplevels_%d", fN);
121         return str;
122     }
123 
onISize()124     SkISize onISize() override {
125         return { 824, 862 };
126     }
127 
DrawAndFrame(SkCanvas * canvas,const SkBitmap & orig,SkScalar x,SkScalar y)128     static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) {
129         SkBitmap bm;
130         orig.copyTo(&bm);
131         apply_gamma(bm);
132 
133         canvas->drawBitmap(bm, x, y, nullptr);
134         SkPaint paint;
135         paint.setStyle(SkPaint::kStroke_Style);
136         paint.setColor(0xFFFFCCCC);
137         canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
138     }
139 
drawLevels(SkCanvas * canvas,const SkBitmap & baseBM,F func)140     template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) {
141         SkScalar x = 4;
142         SkScalar y = 4;
143 
144         SkPixmap prevPM;
145         baseBM.lockPixels();
146         baseBM.peekPixels(&prevPM);
147 
148         SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
149 
150         int index = 0;
151         SkMipMap::Level level;
152         SkScalar scale = 0.5f;
153         while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
154             SkBitmap bm = func(prevPM, level.fPixmap);
155             DrawAndFrame(canvas, bm, x, y);
156 
157             if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
158                 break;
159             }
160             if (index & 1) {
161                 x += level.fPixmap.width() + 4;
162             } else {
163                 y += level.fPixmap.height() + 4;
164             }
165             scale /= 2;
166             prevPM = level.fPixmap;
167             index += 1;
168         }
169     }
170 
drawSet(SkCanvas * canvas,const SkBitmap & orig)171     void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
172         SkAutoCanvasRestore acr(canvas, true);
173 
174         drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) {
175             SkBitmap bm;
176             bm.installPixels(curr);
177             return bm;
178         });
179 
180         const SkBitmapScaler::ResizeMethod methods[] = {
181             SkBitmapScaler::RESIZE_BOX,
182             SkBitmapScaler::RESIZE_TRIANGLE,
183             SkBitmapScaler::RESIZE_LANCZOS3,
184             SkBitmapScaler::RESIZE_HAMMING,
185             SkBitmapScaler::RESIZE_MITCHELL,
186         };
187 
188         SkPixmap basePM;
189         orig.lockPixels();
190         orig.peekPixels(&basePM);
191         for (auto method : methods) {
192             canvas->translate(orig.width()/2 + 8.0f, 0);
193             drawLevels(canvas, orig, [basePM, method](const SkPixmap& prev, const SkPixmap& curr) {
194                 SkBitmap bm;
195                 SkBitmapScaler::Resize(&bm, prev, method, curr.width(), curr.height());
196                 return bm;
197             });
198         }
199     }
200 
onDraw(SkCanvas * canvas)201     void onDraw(SkCanvas* canvas) override {
202         canvas->translate(4, 4);
203         for (const auto& bm : fBM) {
204             this->drawSet(canvas, bm);
205             canvas->translate(0, bm.height() * 0.85f);
206         }
207     }
208 
209 private:
210     typedef skiagm::GM INHERITED;
211 };
212 DEF_GM( return new ShowMipLevels(255); )
213 DEF_GM( return new ShowMipLevels(256); )
214 
215 ///////////////////////////////////////////////////////////////////////////////////////////////////
216 
217 /**
218  *  Show mip levels that were built, for all supported colortypes
219  */
220 class ShowMipLevels2 : public skiagm::GM {
221     const int fW, fH;
222     SkBitmap  fBM[4];
223 
224 public:
ShowMipLevels2(int w,int h)225     ShowMipLevels2(int w, int h) : fW(w), fH(h) {
226         fBM[0] = sk_tool_utils::create_checkerboard_bitmap(w, h, SHOW_MIP_COLOR, SK_ColorWHITE, 2);
227         fBM[1] = make_bitmap(w, h);
228         fBM[2] = make_bitmap2(w, h);
229         fBM[3] = make_bitmap3(w, h);
230     }
231 
232 protected:
233 
onShortName()234     SkString onShortName() override {
235         SkString str;
236         str.printf("showmiplevels2_%dx%d", fW, fH);
237         return str;
238     }
239 
onISize()240     SkISize onISize() override {
241         return { 824, 862 };
242     }
243 
DrawAndFrame(SkCanvas * canvas,const SkBitmap & bm,SkScalar x,SkScalar y)244     static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) {
245         canvas->drawBitmap(bm, x, y, nullptr);
246         SkPaint paint;
247         paint.setStyle(SkPaint::kStroke_Style);
248         paint.setColor(0xFFFFCCCC);
249         canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
250     }
251 
drawLevels(SkCanvas * canvas,const SkBitmap & baseBM)252     void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) {
253         SkScalar x = 4;
254         SkScalar y = 4;
255 
256         SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
257 
258         int index = 0;
259         SkMipMap::Level level;
260         SkScalar scale = 0.5f;
261         while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
262             SkBitmap bm;
263             bm.installPixels(level.fPixmap);
264             DrawAndFrame(canvas, bm, x, y);
265 
266             if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
267                 break;
268             }
269             if (index & 1) {
270                 x += level.fPixmap.width() + 4;
271             } else {
272                 y += level.fPixmap.height() + 4;
273             }
274             scale /= 2;
275             index += 1;
276         }
277     }
278 
drawSet(SkCanvas * canvas,const SkBitmap & orig)279     void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
280         const SkColorType ctypes[] = {
281             kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType
282         };
283 
284         SkAutoCanvasRestore acr(canvas, true);
285 
286         for (auto ctype : ctypes) {
287             SkBitmap bm;
288             orig.copyTo(&bm, ctype);
289             drawLevels(canvas, bm);
290             canvas->translate(orig.width()/2 + 8.0f, 0);
291         }
292     }
293 
onDraw(SkCanvas * canvas)294     void onDraw(SkCanvas* canvas) override {
295         canvas->translate(4, 4);
296         for (const auto& bm : fBM) {
297             this->drawSet(canvas, bm);
298             // round so we always produce an integral translate, so the GOLD tool won't show
299             // unimportant diffs if this is drawn on a GPU with different rounding rules
300             // since we draw the bitmaps using nearest-neighbor
301             canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
302         }
303     }
304 
305 private:
306     typedef skiagm::GM INHERITED;
307 };
308 DEF_GM( return new ShowMipLevels2(255, 255); )
309 DEF_GM( return new ShowMipLevels2(256, 255); )
310 DEF_GM( return new ShowMipLevels2(255, 256); )
311 DEF_GM( return new ShowMipLevels2(256, 256); )
312