1 /*
2 * Copyright 2014 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 "SkBitmap.h"
9 #include "SkBlendMode.h"
10 #include "SkCanvas.h"
11 #include "SkColorData.h"
12 #include "SkColorPriv.h"
13 #include "SkFontPriv.h"
14 #include "SkFloatingPoint.h"
15 #include "SkImage.h"
16 #include "SkMatrix.h"
17 #include "SkPaint.h"
18 #include "SkPath.h"
19 #include "SkPixelRef.h"
20 #include "SkPixmap.h"
21 #include "SkPoint3.h"
22 #include "SkRRect.h"
23 #include "SkShader.h"
24 #include "SkSurface.h"
25 #include "SkTextBlob.h"
26 #include "sk_tool_utils.h"
27
28 #include <cmath>
29 #include <cstring>
30 #include <memory>
31
32 namespace sk_tool_utils {
33
alphatype_name(SkAlphaType at)34 const char* alphatype_name(SkAlphaType at) {
35 switch (at) {
36 case kUnknown_SkAlphaType: return "Unknown";
37 case kOpaque_SkAlphaType: return "Opaque";
38 case kPremul_SkAlphaType: return "Premul";
39 case kUnpremul_SkAlphaType: return "Unpremul";
40 }
41 SkASSERT(false);
42 return "unexpected alphatype";
43 }
44
colortype_name(SkColorType ct)45 const char* colortype_name(SkColorType ct) {
46 switch (ct) {
47 case kUnknown_SkColorType: return "Unknown";
48 case kAlpha_8_SkColorType: return "Alpha_8";
49 case kRGB_565_SkColorType: return "RGB_565";
50 case kARGB_4444_SkColorType: return "ARGB_4444";
51 case kRGBA_8888_SkColorType: return "RGBA_8888";
52 case kRGB_888x_SkColorType: return "RGB_888x";
53 case kBGRA_8888_SkColorType: return "BGRA_8888";
54 case kRGBA_1010102_SkColorType: return "RGBA_1010102";
55 case kRGB_101010x_SkColorType: return "RGB_101010x";
56 case kGray_8_SkColorType: return "Gray_8";
57 case kRGBA_F16_SkColorType: return "RGBA_F16";
58 case kRGBA_F32_SkColorType: return "RGBA_F32";
59 }
60 SkASSERT(false);
61 return "unexpected colortype";
62 }
63
color_to_565(SkColor color)64 SkColor color_to_565(SkColor color) {
65 // Not a good idea to use this function for greyscale colors...
66 // it will add an obvious purple or green tint.
67 SkASSERT(SkColorGetR(color) != SkColorGetG(color) ||
68 SkColorGetR(color) != SkColorGetB(color) ||
69 SkColorGetG(color) != SkColorGetB(color));
70
71 SkPMColor pmColor = SkPreMultiplyColor(color);
72 U16CPU color16 = SkPixel32ToPixel16(pmColor);
73 return SkPixel16ToColor(color16);
74 }
75
write_pixels(SkCanvas * canvas,const SkBitmap & bitmap,int x,int y,SkColorType colorType,SkAlphaType alphaType)76 void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
77 SkColorType colorType, SkAlphaType alphaType) {
78 SkBitmap tmp(bitmap);
79 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
80
81 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
82 }
83
write_pixels(SkSurface * surface,const SkBitmap & src,int x,int y,SkColorType colorType,SkAlphaType alphaType)84 void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
85 SkColorType colorType, SkAlphaType alphaType) {
86 const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
87 surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
88 }
89
create_checkerboard_shader(SkColor c1,SkColor c2,int size)90 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
91 SkBitmap bm;
92 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
93 bm.eraseColor(c1);
94 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
95 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
96 return SkShader::MakeBitmapShader(
97 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
98 }
99
create_checkerboard_bitmap(int w,int h,SkColor c1,SkColor c2,int checkSize)100 SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
101 SkBitmap bitmap;
102 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
103 SkCanvas canvas(bitmap);
104
105 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
106 return bitmap;
107 }
108
draw_checkerboard(SkCanvas * canvas,SkColor c1,SkColor c2,int size)109 void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
110 SkPaint paint;
111 paint.setShader(create_checkerboard_shader(c1, c2, size));
112 paint.setBlendMode(SkBlendMode::kSrc);
113 canvas->drawPaint(paint);
114 }
115
create_string_bitmap(int w,int h,SkColor c,int x,int y,int textSize,const char * str)116 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
117 int textSize, const char* str) {
118 SkBitmap bitmap;
119 bitmap.allocN32Pixels(w, h);
120 SkCanvas canvas(bitmap);
121
122 SkPaint paint;
123 paint.setColor(c);
124
125 SkFont font(sk_tool_utils::create_portable_typeface(), textSize);
126
127 canvas.clear(0x00000000);
128 canvas.drawSimpleText(str, strlen(str), kUTF8_SkTextEncoding,
129 SkIntToScalar(x), SkIntToScalar(y), font, paint);
130
131 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
132 // will process this correctly but legacy configs will render as if this returned N32.
133 SkBitmap result;
134 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
135 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
136 return result;
137 }
138
add_to_text_blob_w_len(SkTextBlobBuilder * builder,const char * text,size_t len,SkTextEncoding encoding,const SkFont & font,SkScalar x,SkScalar y)139 void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
140 SkTextEncoding encoding, const SkFont& font, SkScalar x, SkScalar y) {
141 int count = font.countText(text, len, encoding);
142 auto run = builder->allocRun(font, count, x, y);
143 font.textToGlyphs(text, len, encoding, run.glyphs, count);
144 }
145
add_to_text_blob(SkTextBlobBuilder * builder,const char * text,const SkFont & font,SkScalar x,SkScalar y)146 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkFont& font,
147 SkScalar x, SkScalar y) {
148 add_to_text_blob_w_len(builder, text, strlen(text), kUTF8_SkTextEncoding, font, x, y);
149 }
150
get_text_path(const SkFont & font,const void * text,size_t length,SkTextEncoding encoding,SkPath * dst,const SkPoint pos[])151 void get_text_path(const SkFont& font, const void* text, size_t length, SkTextEncoding encoding,
152 SkPath* dst, const SkPoint pos[]) {
153 SkAutoToGlyphs atg(font, text, length, encoding);
154 const int count = atg.count();
155 SkAutoTArray<SkPoint> computedPos;
156 if (pos == nullptr) {
157 computedPos.reset(count);
158 font.getPos(atg.glyphs(), count, &computedPos[0]);
159 pos = computedPos.get();
160 }
161
162 struct Rec {
163 SkPath* fDst;
164 const SkPoint* fPos;
165 } rec = { dst, pos };
166 font.getPaths(atg.glyphs(), atg.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
167 Rec* rec = (Rec*)ctx;
168 if (src) {
169 SkMatrix tmp(mx);
170 tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
171 rec->fDst->addPath(*src, tmp);
172 }
173 rec->fPos += 1;
174 }, &rec);
175 }
176
make_star(const SkRect & bounds,int numPts,int step)177 SkPath make_star(const SkRect& bounds, int numPts, int step) {
178 SkASSERT(numPts != step);
179 SkPath path;
180 path.setFillType(SkPath::kEvenOdd_FillType);
181 path.moveTo(0,-1);
182 for (int i = 1; i < numPts; ++i) {
183 int idx = i*step % numPts;
184 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
185 SkScalar x = SkScalarCos(theta);
186 SkScalar y = -SkScalarSin(theta);
187 path.lineTo(x, y);
188 }
189 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
190 return path;
191 }
192
norm_to_rgb(SkBitmap * bm,int x,int y,const SkVector3 & norm)193 static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
194 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
195 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
196 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
197 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
198 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
199 }
200
create_hemi_normal_map(SkBitmap * bm,const SkIRect & dst)201 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
202 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
203 dst.fTop + (dst.height() / 2.0f));
204 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
205
206 SkVector3 norm;
207
208 for (int y = dst.fTop; y < dst.fBottom; ++y) {
209 for (int x = dst.fLeft; x < dst.fRight; ++x) {
210 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
211 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
212
213 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
214 if (tmp >= 1.0f) {
215 norm.set(0.0f, 0.0f, 1.0f);
216 } else {
217 norm.fZ = sqrtf(1.0f - tmp);
218 }
219
220 norm_to_rgb(bm, x, y, norm);
221 }
222 }
223 }
224
create_frustum_normal_map(SkBitmap * bm,const SkIRect & dst)225 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
226 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
227 dst.fTop + (dst.height() / 2.0f));
228
229 SkIRect inner = dst;
230 inner.inset(dst.width()/4, dst.height()/4);
231
232 SkPoint3 norm;
233 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
234 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
235 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
236 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
237
238 for (int y = dst.fTop; y < dst.fBottom; ++y) {
239 for (int x = dst.fLeft; x < dst.fRight; ++x) {
240 if (inner.contains(x, y)) {
241 norm.set(0.0f, 0.0f, 1.0f);
242 } else {
243 SkScalar locX = x + 0.5f - center.fX;
244 SkScalar locY = y + 0.5f - center.fY;
245
246 if (locX >= 0.0f) {
247 if (locY > 0.0f) {
248 norm = locX >= locY ? right : down; // LR corner
249 } else {
250 norm = locX > -locY ? right : up; // UR corner
251 }
252 } else {
253 if (locY > 0.0f) {
254 norm = -locX > locY ? left : down; // LL corner
255 } else {
256 norm = locX > locY ? up : left; // UL corner
257 }
258 }
259 }
260
261 norm_to_rgb(bm, x, y, norm);
262 }
263 }
264 }
265
create_tetra_normal_map(SkBitmap * bm,const SkIRect & dst)266 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
267 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
268 dst.fTop + (dst.height() / 2.0f));
269
270 static const SkScalar k1OverRoot3 = 0.5773502692f;
271
272 SkPoint3 norm;
273 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
274 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
275 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
276
277 for (int y = dst.fTop; y < dst.fBottom; ++y) {
278 for (int x = dst.fLeft; x < dst.fRight; ++x) {
279 SkScalar locX = x + 0.5f - center.fX;
280 SkScalar locY = y + 0.5f - center.fY;
281
282 if (locX >= 0.0f) {
283 if (locY > 0.0f) {
284 norm = locX >= locY ? rightUp : down; // LR corner
285 } else {
286 norm = rightUp;
287 }
288 } else {
289 if (locY > 0.0f) {
290 norm = -locX > locY ? leftUp : down; // LL corner
291 } else {
292 norm = leftUp;
293 }
294 }
295
296 norm_to_rgb(bm, x, y, norm);
297 }
298 }
299 }
300
301 #if !defined(__clang__) && defined(_MSC_VER)
302 // MSVC takes ~2 minutes to compile this function with optimization.
303 // We don't really care to wait that long for this function.
304 #pragma optimize("", off)
305 #endif
make_big_path(SkPath & path)306 void make_big_path(SkPath& path) {
307 #include "BigPathBench.inc" // IWYU pragma: keep
308 }
309
copy_to(SkBitmap * dst,SkColorType dstColorType,const SkBitmap & src)310 bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
311 SkPixmap srcPM;
312 if (!src.peekPixels(&srcPM)) {
313 return false;
314 }
315
316 SkBitmap tmpDst;
317 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
318 if (!tmpDst.setInfo(dstInfo)) {
319 return false;
320 }
321
322 if (!tmpDst.tryAllocPixels()) {
323 return false;
324 }
325
326 SkPixmap dstPM;
327 if (!tmpDst.peekPixels(&dstPM)) {
328 return false;
329 }
330
331 if (!srcPM.readPixels(dstPM)) {
332 return false;
333 }
334
335 dst->swap(tmpDst);
336 return true;
337 }
338
copy_to_g8(SkBitmap * dst,const SkBitmap & src)339 void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
340 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
341 kRGBA_8888_SkColorType == src.colorType());
342
343 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
344 dst->allocPixels(grayInfo);
345 uint8_t* dst8 = (uint8_t*)dst->getPixels();
346 const uint32_t* src32 = (const uint32_t*)src.getPixels();
347
348 const int w = src.width();
349 const int h = src.height();
350 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
351 for (int y = 0; y < h; ++y) {
352 if (isBGRA) {
353 // BGRA
354 for (int x = 0; x < w; ++x) {
355 uint32_t s = src32[x];
356 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
357 }
358 } else {
359 // RGBA
360 for (int x = 0; x < w; ++x) {
361 uint32_t s = src32[x];
362 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
363 }
364 }
365 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
366 dst8 += dst->rowBytes();
367 }
368 }
369
370 //////////////////////////////////////////////////////////////////////////////////////////////
371
equal_pixels(const SkPixmap & a,const SkPixmap & b)372 bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
373 if (a.width() != b.width() ||
374 a.height() != b.height() ||
375 a.colorType() != b.colorType())
376 {
377 return false;
378 }
379
380 for (int y = 0; y < a.height(); ++y) {
381 const char* aptr = (const char*)a.addr(0, y);
382 const char* bptr = (const char*)b.addr(0, y);
383 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
384 return false;
385 }
386 aptr += a.rowBytes();
387 bptr += b.rowBytes();
388 }
389 return true;
390 }
391
equal_pixels(const SkBitmap & bm0,const SkBitmap & bm1)392 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
393 SkPixmap pm0, pm1;
394 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1);
395 }
396
equal_pixels(const SkImage * a,const SkImage * b)397 bool equal_pixels(const SkImage* a, const SkImage* b) {
398 // ensure that peekPixels will succeed
399 auto imga = a->makeRasterImage();
400 auto imgb = b->makeRasterImage();
401
402 SkPixmap pm0, pm1;
403 return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1);
404 }
405
makeSurface(SkCanvas * canvas,const SkImageInfo & info,const SkSurfaceProps * props)406 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
407 const SkSurfaceProps* props) {
408 auto surf = canvas->makeSurface(info, props);
409 if (!surf) {
410 surf = SkSurface::MakeRaster(info, props);
411 }
412 return surf;
413 }
414 } // namespace sk_tool_utils
415