blob: 74aa1a57904551abeaa8bf7cb364ff7b59954608 [file] [log] [blame]
bungeman13b9c952016-05-12 10:09:30 -07001/*
tfarina20108912014-06-21 10:54:17 -07002 * 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
Ben Wagner3560b582018-03-27 18:15:53 +00008#include "SkBitmap.h"
Ben Wagner97182cc2018-02-15 10:20:04 -05009#include "SkBlendMode.h"
Ben Wagner3560b582018-03-27 18:15:53 +000010#include "SkCanvas.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050011#include "SkColorData.h"
12#include "SkColorPriv.h"
Mike Reed38810f32018-12-21 10:58:25 -050013#include "SkFontPriv.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050014#include "SkFloatingPoint.h"
Ben Wagner3560b582018-03-27 18:15:53 +000015#include "SkImage.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050016#include "SkMatrix.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050017#include "SkPaint.h"
18#include "SkPath.h"
19#include "SkPixelRef.h"
20#include "SkPixmap.h"
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040021#include "SkPoint3.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050022#include "SkRRect.h"
Ben Wagner3560b582018-03-27 18:15:53 +000023#include "SkShader.h"
24#include "SkSurface.h"
Ben Wagner3560b582018-03-27 18:15:53 +000025#include "SkTextBlob.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050026#include "sk_tool_utils.h"
27
28#include <cmath>
29#include <cstring>
30#include <memory>
Cary Clark992c7b02014-07-31 08:58:44 -040031
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000032namespace sk_tool_utils {
33
Mike Reedd4746982018-02-07 16:05:29 -050034const 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
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000045const char* colortype_name(SkColorType ct) {
46 switch (ct) {
47 case kUnknown_SkColorType: return "Unknown";
48 case kAlpha_8_SkColorType: return "Alpha_8";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000049 case kRGB_565_SkColorType: return "RGB_565";
Mike Reedd4746982018-02-07 16:05:29 -050050 case kARGB_4444_SkColorType: return "ARGB_4444";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000051 case kRGBA_8888_SkColorType: return "RGBA_8888";
Mike Reedd4746982018-02-07 16:05:29 -050052 case kRGB_888x_SkColorType: return "RGB_888x";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000053 case kBGRA_8888_SkColorType: return "BGRA_8888";
Mike Reedd4746982018-02-07 16:05:29 -050054 case kRGBA_1010102_SkColorType: return "RGBA_1010102";
55 case kRGB_101010x_SkColorType: return "RGB_101010x";
56 case kGray_8_SkColorType: return "Gray_8";
Brian Osmanf750fbc2017-02-08 10:47:28 -050057 case kRGBA_F16_SkColorType: return "RGBA_F16";
Mike Klein37854712018-06-26 11:43:06 -040058 case kRGBA_F32_SkColorType: return "RGBA_F32";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000059 }
Mike Reedd4746982018-02-07 16:05:29 -050060 SkASSERT(false);
61 return "unexpected colortype";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000062}
63
caryclark65cdba62015-06-15 06:51:08 -070064SkColor color_to_565(SkColor color) {
Mike Kleind46dce32018-08-16 10:17:03 -040065 // 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
caryclark65cdba62015-06-15 06:51:08 -070071 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -070072 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -070073 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -070074}
75
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000076void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
77 SkColorType colorType, SkAlphaType alphaType) {
78 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -070079 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000080
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000081 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
82}
83
Mike Reed4c790bd2018-02-08 14:10:40 -050084void 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
reed8a21c9f2016-03-08 18:50:00 -080090sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -080091 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -070092 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -080093 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);
reed8a21c9f2016-03-08 18:50:00 -080096 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -080097 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
98}
99
robertphillips943a4622015-09-03 13:32:33 -0700100SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
101 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700102 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700103 SkCanvas canvas(bitmap);
104
105 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
106 return bitmap;
107}
108
halcanaryb0cce2c2015-01-26 12:49:00 -0800109void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
110 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800111 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700112 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800113 canvas->drawPaint(paint);
114}
115
robertphillips943a4622015-09-03 13:32:33 -0700116SkBitmap 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.setAntiAlias(true);
124 sk_tool_utils::set_portable_typeface(&paint);
125 paint.setColor(c);
126 paint.setTextSize(SkIntToScalar(textSize));
127
128 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400129 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700130
Brian Osman2b25d342016-12-20 11:09:31 -0500131 // 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;
robertphillips943a4622015-09-03 13:32:33 -0700137}
138
Robert Phillips4c72b262017-08-15 13:28:42 -0400139void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
Mike Reed2ed78202018-11-21 15:10:08 -0500140 const SkPaint& paint, SkScalar x, SkScalar y) {
141 SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
joshualitt9e36c1a2015-04-14 12:17:27 -0700142 SkTDArray<uint16_t> glyphs;
143
Mike Reed0f9d33e2018-12-05 10:54:05 -0500144 glyphs.append(font.countText(text, len, paint.getTextEncoding()));
145 font.textToGlyphs(text, len, paint.getTextEncoding(), glyphs.begin(), glyphs.count());
joshualitt9e36c1a2015-04-14 12:17:27 -0700146
Mike Reed2ed78202018-11-21 15:10:08 -0500147 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(font, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700148 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700149 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
150}
151
Robert Phillips4c72b262017-08-15 13:28:42 -0400152void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
153 const SkPaint& origPaint, SkScalar x, SkScalar y) {
154 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
155}
156
Mike Reed38810f32018-12-21 10:58:25 -0500157void get_text_path(const SkFont& font, const void* text, size_t length, SkTextEncoding encoding,
158 SkPath* dst, const SkPoint pos[]) {
159 SkAutoToGlyphs atg(font, text, length, encoding);
160 const int count = atg.count();
161 SkAutoTArray<SkPoint> computedPos;
162 if (pos == nullptr) {
163 computedPos.reset(count);
164 font.getPos(atg.glyphs(), count, &computedPos[0]);
165 pos = computedPos.get();
166 }
167
168 struct Rec {
169 SkPath* fDst;
170 const SkPoint* fPos;
171 } rec = { dst, pos };
172 font.getPaths(atg.glyphs(), atg.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
173 Rec* rec = (Rec*)ctx;
174 if (src) {
175 SkMatrix tmp(mx);
176 tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
177 rec->fDst->addPath(*src, tmp);
178 }
179 rec->fPos += 1;
180 }, &rec);
181}
182
Chris Dalton7c02cc72017-11-06 14:10:54 -0700183SkPath make_star(const SkRect& bounds, int numPts, int step) {
184 SkPath path;
185 path.setFillType(SkPath::kEvenOdd_FillType);
186 path.moveTo(0,-1);
187 for (int i = 1; i < numPts; ++i) {
188 int idx = i*step;
189 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
190 SkScalar x = SkScalarCos(theta);
191 SkScalar y = -SkScalarSin(theta);
192 path.lineTo(x, y);
193 }
194 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
195 return path;
196}
197
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400198static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
199 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
200 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
201 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
202 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
203 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
204}
205
206void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
207 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
208 dst.fTop + (dst.height() / 2.0f));
209 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
210
211 SkVector3 norm;
212
213 for (int y = dst.fTop; y < dst.fBottom; ++y) {
214 for (int x = dst.fLeft; x < dst.fRight; ++x) {
215 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
216 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
217
218 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
219 if (tmp >= 1.0f) {
220 norm.set(0.0f, 0.0f, 1.0f);
221 } else {
222 norm.fZ = sqrtf(1.0f - tmp);
223 }
224
225 norm_to_rgb(bm, x, y, norm);
226 }
227 }
228}
229
230void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
231 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
232 dst.fTop + (dst.height() / 2.0f));
233
234 SkIRect inner = dst;
235 inner.inset(dst.width()/4, dst.height()/4);
236
237 SkPoint3 norm;
238 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
239 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
240 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
241 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
242
243 for (int y = dst.fTop; y < dst.fBottom; ++y) {
244 for (int x = dst.fLeft; x < dst.fRight; ++x) {
245 if (inner.contains(x, y)) {
246 norm.set(0.0f, 0.0f, 1.0f);
247 } else {
248 SkScalar locX = x + 0.5f - center.fX;
249 SkScalar locY = y + 0.5f - center.fY;
250
251 if (locX >= 0.0f) {
252 if (locY > 0.0f) {
253 norm = locX >= locY ? right : down; // LR corner
254 } else {
255 norm = locX > -locY ? right : up; // UR corner
256 }
257 } else {
258 if (locY > 0.0f) {
259 norm = -locX > locY ? left : down; // LL corner
260 } else {
261 norm = locX > locY ? up : left; // UL corner
262 }
263 }
264 }
265
266 norm_to_rgb(bm, x, y, norm);
267 }
268 }
269}
270
271void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
272 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
273 dst.fTop + (dst.height() / 2.0f));
274
275 static const SkScalar k1OverRoot3 = 0.5773502692f;
276
277 SkPoint3 norm;
278 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
279 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
280 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
281
282 for (int y = dst.fTop; y < dst.fBottom; ++y) {
283 for (int x = dst.fLeft; x < dst.fRight; ++x) {
284 SkScalar locX = x + 0.5f - center.fX;
285 SkScalar locY = y + 0.5f - center.fY;
286
287 if (locX >= 0.0f) {
288 if (locY > 0.0f) {
289 norm = locX >= locY ? rightUp : down; // LR corner
290 } else {
291 norm = rightUp;
292 }
293 } else {
294 if (locY > 0.0f) {
295 norm = -locX > locY ? leftUp : down; // LL corner
296 } else {
297 norm = leftUp;
298 }
299 }
300
301 norm_to_rgb(bm, x, y, norm);
302 }
303 }
304}
305
Mike Kleinc722f792017-07-31 11:57:21 -0400306#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400307 // MSVC takes ~2 minutes to compile this function with optimization.
308 // We don't really care to wait that long for this function.
309 #pragma optimize("", off)
310#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700311void make_big_path(SkPath& path) {
Ben Wagner97182cc2018-02-15 10:20:04 -0500312 #include "BigPathBench.inc" // IWYU pragma: keep
joshualitt98d2e2f2015-10-05 07:23:30 -0700313}
314
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400315bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
316 SkPixmap srcPM;
317 if (!src.peekPixels(&srcPM)) {
318 return false;
319 }
320
321 SkBitmap tmpDst;
322 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
323 if (!tmpDst.setInfo(dstInfo)) {
324 return false;
325 }
326
Mike Reed304a07c2017-07-12 15:10:28 -0400327 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400328 return false;
329 }
330
331 SkPixmap dstPM;
332 if (!tmpDst.peekPixels(&dstPM)) {
333 return false;
334 }
335
336 if (!srcPM.readPixels(dstPM)) {
337 return false;
338 }
339
340 dst->swap(tmpDst);
341 return true;
342}
343
Matt Sarett4897fb82017-01-18 11:49:33 -0500344void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
345 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
346 kRGBA_8888_SkColorType == src.colorType());
347
348 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
349 dst->allocPixels(grayInfo);
350 uint8_t* dst8 = (uint8_t*)dst->getPixels();
351 const uint32_t* src32 = (const uint32_t*)src.getPixels();
352
353 const int w = src.width();
354 const int h = src.height();
355 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
356 for (int y = 0; y < h; ++y) {
357 if (isBGRA) {
358 // BGRA
359 for (int x = 0; x < w; ++x) {
360 uint32_t s = src32[x];
361 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
362 }
363 } else {
364 // RGBA
365 for (int x = 0; x < w; ++x) {
366 uint32_t s = src32[x];
367 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
368 }
369 }
370 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
371 dst8 += dst->rowBytes();
372 }
373}
robertphillips401c1962016-08-04 12:35:46 -0700374
Mike Reed5a625e02017-08-08 15:48:54 -0400375 //////////////////////////////////////////////////////////////////////////////////////////////
376
Brian Osman1b151982018-09-20 12:47:39 -0400377 bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
Mike Reed5a625e02017-08-08 15:48:54 -0400378 if (a.width() != b.width() ||
379 a.height() != b.height() ||
Brian Osman1b151982018-09-20 12:47:39 -0400380 a.colorType() != b.colorType())
Mike Reed5a625e02017-08-08 15:48:54 -0400381 {
382 return false;
383 }
384
385 for (int y = 0; y < a.height(); ++y) {
386 const char* aptr = (const char*)a.addr(0, y);
387 const char* bptr = (const char*)b.addr(0, y);
388 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
Brian Osman1b151982018-09-20 12:47:39 -0400389 return false;
Mike Reed5a625e02017-08-08 15:48:54 -0400390 }
391 aptr += a.rowBytes();
392 bptr += b.rowBytes();
393 }
394 return true;
395 }
396
Brian Osman1b151982018-09-20 12:47:39 -0400397 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
Mike Reed5a625e02017-08-08 15:48:54 -0400398 SkPixmap pm0, pm1;
Brian Osman1b151982018-09-20 12:47:39 -0400399 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1);
Mike Reed24846602017-12-04 16:06:03 -0500400 }
401
Brian Osman1b151982018-09-20 12:47:39 -0400402 bool equal_pixels(const SkImage* a, const SkImage* b) {
Mike Reed24846602017-12-04 16:06:03 -0500403 // ensure that peekPixels will succeed
404 auto imga = a->makeRasterImage();
405 auto imgb = b->makeRasterImage();
Mike Reed24846602017-12-04 16:06:03 -0500406
407 SkPixmap pm0, pm1;
Brian Osman1b151982018-09-20 12:47:39 -0400408 return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1);
Mike Reed5a625e02017-08-08 15:48:54 -0400409 }
Mike Reed46596ae2018-01-02 15:40:29 -0500410
Cary Clarka24712e2018-09-05 18:41:40 +0000411 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
412 const SkSurfaceProps* props) {
413 auto surf = canvas->makeSurface(info, props);
414 if (!surf) {
415 surf = SkSurface::MakeRaster(info, props);
416 }
417 return surf;
418 }
tfarina20108912014-06-21 10:54:17 -0700419} // namespace sk_tool_utils