2016-05-12 17:09:30 +00:00
|
|
|
/*
|
2014-06-21 17:54:17 +00:00
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPixelRef.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkPoint3.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/ports/SkTypeface_win.h"
|
|
|
|
#include "include/private/SkColorData.h"
|
|
|
|
#include "include/private/SkFloatingPoint.h"
|
|
|
|
#include "src/core/SkFontMgrPriv.h"
|
|
|
|
#include "src/core/SkFontPriv.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
|
|
|
#include "tools/fonts/TestFontMgr.h"
|
2018-02-15 15:20:04 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
2014-06-21 17:54:17 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
namespace ToolUtils {
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2018-02-07 21:05:29 +00:00
|
|
|
const char* alphatype_name(SkAlphaType at) {
|
|
|
|
switch (at) {
|
2019-04-09 18:45:02 +00:00
|
|
|
case kUnknown_SkAlphaType: return "Unknown";
|
|
|
|
case kOpaque_SkAlphaType: return "Opaque";
|
|
|
|
case kPremul_SkAlphaType: return "Premul";
|
2018-02-07 21:05:29 +00:00
|
|
|
case kUnpremul_SkAlphaType: return "Unpremul";
|
|
|
|
}
|
|
|
|
SkASSERT(false);
|
|
|
|
return "unexpected alphatype";
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:31:26 +00:00
|
|
|
const char* colortype_name(SkColorType ct) {
|
|
|
|
switch (ct) {
|
2019-04-09 18:45:02 +00:00
|
|
|
case kUnknown_SkColorType: return "Unknown";
|
|
|
|
case kAlpha_8_SkColorType: return "Alpha_8";
|
|
|
|
case kRGB_565_SkColorType: return "RGB_565";
|
|
|
|
case kARGB_4444_SkColorType: return "ARGB_4444";
|
|
|
|
case kRGBA_8888_SkColorType: return "RGBA_8888";
|
|
|
|
case kRGB_888x_SkColorType: return "RGB_888x";
|
|
|
|
case kBGRA_8888_SkColorType: return "BGRA_8888";
|
2018-02-07 21:05:29 +00:00
|
|
|
case kRGBA_1010102_SkColorType: return "RGBA_1010102";
|
2019-04-09 18:45:02 +00:00
|
|
|
case kRGB_101010x_SkColorType: return "RGB_101010x";
|
|
|
|
case kGray_8_SkColorType: return "Gray_8";
|
2019-02-28 16:03:27 +00:00
|
|
|
case kRGBA_F16Norm_SkColorType: return "RGBA_F16Norm";
|
2019-04-09 18:45:02 +00:00
|
|
|
case kRGBA_F16_SkColorType: return "RGBA_F16";
|
|
|
|
case kRGBA_F32_SkColorType: return "RGBA_F32";
|
2014-03-17 21:31:26 +00:00
|
|
|
}
|
2018-02-07 21:05:29 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
return "unexpected colortype";
|
2014-03-17 21:31:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:45:02 +00:00
|
|
|
const char* colortype_depth(SkColorType ct) {
|
|
|
|
switch (ct) {
|
|
|
|
case kUnknown_SkColorType: return "Unknown";
|
|
|
|
case kAlpha_8_SkColorType: return "A8";
|
|
|
|
case kRGB_565_SkColorType: return "565";
|
|
|
|
case kARGB_4444_SkColorType: return "4444";
|
|
|
|
case kRGBA_8888_SkColorType: return "8888";
|
|
|
|
case kRGB_888x_SkColorType: return "888";
|
|
|
|
case kBGRA_8888_SkColorType: return "8888";
|
|
|
|
case kRGBA_1010102_SkColorType: return "1010102";
|
|
|
|
case kRGB_101010x_SkColorType: return "101010";
|
|
|
|
case kGray_8_SkColorType: return "G8";
|
|
|
|
case kRGBA_F16Norm_SkColorType: return "F16Norm"; // TODO: "F16"?
|
|
|
|
case kRGBA_F16_SkColorType: return "F16";
|
|
|
|
case kRGBA_F32_SkColorType: return "F32";
|
|
|
|
}
|
|
|
|
SkASSERT(false);
|
|
|
|
return "unexpected colortype";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-15 13:51:08 +00:00
|
|
|
SkColor color_to_565(SkColor color) {
|
2018-08-16 14:17:03 +00:00
|
|
|
// Not a good idea to use this function for greyscale colors...
|
|
|
|
// it will add an obvious purple or green tint.
|
2019-03-20 16:12:10 +00:00
|
|
|
SkASSERT(SkColorGetR(color) != SkColorGetG(color) || SkColorGetR(color) != SkColorGetB(color) ||
|
2018-08-16 14:17:03 +00:00
|
|
|
SkColorGetG(color) != SkColorGetB(color));
|
|
|
|
|
2015-06-15 13:51:08 +00:00
|
|
|
SkPMColor pmColor = SkPreMultiplyColor(color);
|
2019-03-20 16:12:10 +00:00
|
|
|
U16CPU color16 = SkPixel32ToPixel16(pmColor);
|
2015-06-15 13:51:08 +00:00
|
|
|
return SkPixel16ToColor(color16);
|
2015-06-12 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
|
2015-01-26 20:49:00 +00:00
|
|
|
SkBitmap bm;
|
2016-09-28 21:02:10 +00:00
|
|
|
bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
|
2015-01-26 20:49:00 +00:00
|
|
|
bm.eraseColor(c1);
|
|
|
|
bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
|
|
|
|
bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
|
2019-04-08 17:20:23 +00:00
|
|
|
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
|
2015-01-26 20:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
|
|
|
|
SkBitmap bitmap;
|
2016-09-28 21:02:10 +00:00
|
|
|
bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
|
2015-09-03 20:32:33 +00:00
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(&canvas, c1, c2, checkSize);
|
2015-09-03 20:32:33 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
|
|
|
|
SkPaint paint;
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(create_checkerboard_shader(c1, c2, size));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2015-01-26 20:49:00 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkBitmap
|
|
|
|
create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str) {
|
2015-09-03 20:32:33 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(w, h);
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(c);
|
2019-01-03 17:59:14 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), textSize);
|
2015-09-03 20:32:33 +00:00
|
|
|
|
|
|
|
canvas.clear(0x00000000);
|
2019-03-20 16:12:10 +00:00
|
|
|
canvas.drawSimpleText(str,
|
|
|
|
strlen(str),
|
|
|
|
kUTF8_SkTextEncoding,
|
|
|
|
SkIntToScalar(x),
|
|
|
|
SkIntToScalar(y),
|
|
|
|
font,
|
|
|
|
paint);
|
2015-09-03 20:32:33 +00:00
|
|
|
|
2016-12-20 16:09:31 +00:00
|
|
|
// Tag data as sRGB (without doing any color space conversion). Color-space aware configs
|
|
|
|
// will process this correctly but legacy configs will render as if this returned N32.
|
|
|
|
SkBitmap result;
|
|
|
|
result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
|
|
|
|
result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
|
|
|
|
return result;
|
2015-09-03 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
void add_to_text_blob_w_len(SkTextBlobBuilder* builder,
|
|
|
|
const char* text,
|
|
|
|
size_t len,
|
|
|
|
SkTextEncoding encoding,
|
|
|
|
const SkFont& font,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y) {
|
|
|
|
int count = font.countText(text, len, encoding);
|
|
|
|
auto run = builder->allocRun(font, count, x, y);
|
2018-12-22 03:22:31 +00:00
|
|
|
font.textToGlyphs(text, len, encoding, run.glyphs, count);
|
2015-04-14 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
void add_to_text_blob(SkTextBlobBuilder* builder,
|
|
|
|
const char* text,
|
|
|
|
const SkFont& font,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y) {
|
2018-12-22 03:22:31 +00:00
|
|
|
add_to_text_blob_w_len(builder, text, strlen(text), kUTF8_SkTextEncoding, font, x, y);
|
2017-08-15 17:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
void get_text_path(const SkFont& font,
|
|
|
|
const void* text,
|
|
|
|
size_t length,
|
|
|
|
SkTextEncoding encoding,
|
|
|
|
SkPath* dst,
|
|
|
|
const SkPoint pos[]) {
|
|
|
|
SkAutoToGlyphs atg(font, text, length, encoding);
|
|
|
|
const int count = atg.count();
|
2018-12-21 15:58:25 +00:00
|
|
|
SkAutoTArray<SkPoint> computedPos;
|
|
|
|
if (pos == nullptr) {
|
|
|
|
computedPos.reset(count);
|
|
|
|
font.getPos(atg.glyphs(), count, &computedPos[0]);
|
|
|
|
pos = computedPos.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Rec {
|
2019-03-20 16:12:10 +00:00
|
|
|
SkPath* fDst;
|
2018-12-21 15:58:25 +00:00
|
|
|
const SkPoint* fPos;
|
2019-03-20 16:12:10 +00:00
|
|
|
} rec = {dst, pos};
|
|
|
|
font.getPaths(atg.glyphs(),
|
|
|
|
atg.count(),
|
|
|
|
[](const SkPath* src, const SkMatrix& mx, void* ctx) {
|
|
|
|
Rec* rec = (Rec*)ctx;
|
|
|
|
if (src) {
|
|
|
|
SkMatrix tmp(mx);
|
|
|
|
tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
|
|
|
|
rec->fDst->addPath(*src, tmp);
|
|
|
|
}
|
|
|
|
rec->fPos += 1;
|
|
|
|
},
|
|
|
|
&rec);
|
2018-12-21 15:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 21:10:54 +00:00
|
|
|
SkPath make_star(const SkRect& bounds, int numPts, int step) {
|
2019-01-07 05:51:00 +00:00
|
|
|
SkASSERT(numPts != step);
|
2017-11-06 21:10:54 +00:00
|
|
|
SkPath path;
|
|
|
|
path.setFillType(SkPath::kEvenOdd_FillType);
|
2019-03-20 16:12:10 +00:00
|
|
|
path.moveTo(0, -1);
|
2017-11-06 21:10:54 +00:00
|
|
|
for (int i = 1; i < numPts; ++i) {
|
2019-03-20 16:12:10 +00:00
|
|
|
int idx = i * step % numPts;
|
|
|
|
SkScalar theta = idx * 2 * SK_ScalarPI / numPts + SK_ScalarPI / 2;
|
|
|
|
SkScalar x = SkScalarCos(theta);
|
|
|
|
SkScalar y = -SkScalarSin(theta);
|
2017-11-06 21:10:54 +00:00
|
|
|
path.lineTo(x, y);
|
|
|
|
}
|
|
|
|
path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:30:40 +00:00
|
|
|
static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
|
|
|
|
SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
|
2019-03-20 16:12:10 +00:00
|
|
|
unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
|
|
|
|
unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
|
|
|
|
unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
|
2018-07-17 16:30:40 +00:00
|
|
|
*bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
|
2019-03-20 16:12:10 +00:00
|
|
|
const SkPoint center =
|
|
|
|
SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
|
2018-07-17 16:30:40 +00:00
|
|
|
const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
|
|
|
|
|
|
|
|
SkVector3 norm;
|
|
|
|
|
|
|
|
for (int y = dst.fTop; y < dst.fBottom; ++y) {
|
|
|
|
for (int x = dst.fLeft; x < dst.fRight; ++x) {
|
|
|
|
norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
|
|
|
|
norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
|
|
|
|
|
|
|
|
SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
|
|
|
|
if (tmp >= 1.0f) {
|
|
|
|
norm.set(0.0f, 0.0f, 1.0f);
|
|
|
|
} else {
|
|
|
|
norm.fZ = sqrtf(1.0f - tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
norm_to_rgb(bm, x, y, norm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
|
2019-03-20 16:12:10 +00:00
|
|
|
const SkPoint center =
|
|
|
|
SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
|
2018-07-17 16:30:40 +00:00
|
|
|
|
|
|
|
SkIRect inner = dst;
|
2019-03-20 16:12:10 +00:00
|
|
|
inner.inset(dst.width() / 4, dst.height() / 4);
|
2018-07-17 16:30:40 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkPoint3 norm;
|
|
|
|
const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
|
|
|
|
const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
|
|
|
|
const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
|
|
|
|
const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
|
2018-07-17 16:30:40 +00:00
|
|
|
|
|
|
|
for (int y = dst.fTop; y < dst.fBottom; ++y) {
|
|
|
|
for (int x = dst.fLeft; x < dst.fRight; ++x) {
|
|
|
|
if (inner.contains(x, y)) {
|
|
|
|
norm.set(0.0f, 0.0f, 1.0f);
|
|
|
|
} else {
|
|
|
|
SkScalar locX = x + 0.5f - center.fX;
|
|
|
|
SkScalar locY = y + 0.5f - center.fY;
|
|
|
|
|
|
|
|
if (locX >= 0.0f) {
|
|
|
|
if (locY > 0.0f) {
|
2019-03-20 16:12:10 +00:00
|
|
|
norm = locX >= locY ? right : down; // LR corner
|
2018-07-17 16:30:40 +00:00
|
|
|
} else {
|
2019-03-20 16:12:10 +00:00
|
|
|
norm = locX > -locY ? right : up; // UR corner
|
2018-07-17 16:30:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (locY > 0.0f) {
|
2019-03-20 16:12:10 +00:00
|
|
|
norm = -locX > locY ? left : down; // LL corner
|
2018-07-17 16:30:40 +00:00
|
|
|
} else {
|
2019-03-20 16:12:10 +00:00
|
|
|
norm = locX > locY ? up : left; // UL corner
|
2018-07-17 16:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
norm_to_rgb(bm, x, y, norm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
|
2019-03-20 16:12:10 +00:00
|
|
|
const SkPoint center =
|
|
|
|
SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
|
2018-07-17 16:30:40 +00:00
|
|
|
|
|
|
|
static const SkScalar k1OverRoot3 = 0.5773502692f;
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkPoint3 norm;
|
|
|
|
const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
|
|
|
|
const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
|
|
|
|
const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
|
2018-07-17 16:30:40 +00:00
|
|
|
|
|
|
|
for (int y = dst.fTop; y < dst.fBottom; ++y) {
|
|
|
|
for (int x = dst.fLeft; x < dst.fRight; ++x) {
|
|
|
|
SkScalar locX = x + 0.5f - center.fX;
|
|
|
|
SkScalar locY = y + 0.5f - center.fY;
|
|
|
|
|
|
|
|
if (locX >= 0.0f) {
|
|
|
|
if (locY > 0.0f) {
|
2019-03-20 16:12:10 +00:00
|
|
|
norm = locX >= locY ? rightUp : down; // LR corner
|
2018-07-17 16:30:40 +00:00
|
|
|
} else {
|
|
|
|
norm = rightUp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (locY > 0.0f) {
|
2019-03-20 16:12:10 +00:00
|
|
|
norm = -locX > locY ? leftUp : down; // LL corner
|
2018-07-17 16:30:40 +00:00
|
|
|
} else {
|
|
|
|
norm = leftUp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
norm_to_rgb(bm, x, y, norm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:57:21 +00:00
|
|
|
#if !defined(__clang__) && defined(_MSC_VER)
|
2019-03-20 16:12:10 +00:00
|
|
|
// MSVC takes ~2 minutes to compile this function with optimization.
|
|
|
|
// We don't really care to wait that long for this function.
|
|
|
|
#pragma optimize("", off)
|
2016-10-12 21:06:48 +00:00
|
|
|
#endif
|
2015-10-05 14:23:30 +00:00
|
|
|
void make_big_path(SkPath& path) {
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "BigPathBench.inc" // IWYU pragma: keep
|
2015-10-05 14:23:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 15:15:22 +00:00
|
|
|
bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
|
|
|
|
SkPixmap srcPM;
|
|
|
|
if (!src.peekPixels(&srcPM)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkBitmap tmpDst;
|
2017-04-28 15:15:22 +00:00
|
|
|
SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
|
|
|
|
if (!tmpDst.setInfo(dstInfo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-12 19:10:28 +00:00
|
|
|
if (!tmpDst.tryAllocPixels()) {
|
2017-04-28 15:15:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPixmap dstPM;
|
|
|
|
if (!tmpDst.peekPixels(&dstPM)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!srcPM.readPixels(dstPM)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->swap(tmpDst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:49:33 +00:00
|
|
|
void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
|
|
|
|
SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
|
|
|
|
kRGBA_8888_SkColorType == src.colorType());
|
|
|
|
|
|
|
|
SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
|
|
|
|
dst->allocPixels(grayInfo);
|
2019-03-20 16:12:10 +00:00
|
|
|
uint8_t* dst8 = (uint8_t*)dst->getPixels();
|
2017-01-18 16:49:33 +00:00
|
|
|
const uint32_t* src32 = (const uint32_t*)src.getPixels();
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
const int w = src.width();
|
|
|
|
const int h = src.height();
|
2017-01-18 16:49:33 +00:00
|
|
|
const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
if (isBGRA) {
|
|
|
|
// BGRA
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
uint32_t s = src32[x];
|
2019-03-20 16:12:10 +00:00
|
|
|
dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
|
2017-01-18 16:49:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// RGBA
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
uint32_t s = src32[x];
|
2019-03-20 16:12:10 +00:00
|
|
|
dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
|
2017-01-18 16:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
|
|
|
|
dst8 += dst->rowBytes();
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 19:35:46 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
2017-08-08 19:48:54 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
|
|
|
|
if (a.width() != b.width() || a.height() != b.height() || a.colorType() != b.colorType()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-08 19:48:54 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
for (int y = 0; y < a.height(); ++y) {
|
|
|
|
const char* aptr = (const char*)a.addr(0, y);
|
|
|
|
const char* bptr = (const char*)b.addr(0, y);
|
|
|
|
if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
|
|
|
|
return false;
|
2017-08-08 19:48:54 +00:00
|
|
|
}
|
2019-03-20 16:12:10 +00:00
|
|
|
aptr += a.rowBytes();
|
|
|
|
bptr += b.rowBytes();
|
2017-08-08 19:48:54 +00:00
|
|
|
}
|
2019-03-20 16:12:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-08 19:48:54 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
|
|
|
|
SkPixmap pm0, pm1;
|
|
|
|
return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1);
|
|
|
|
}
|
2017-12-04 21:06:03 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
bool equal_pixels(const SkImage* a, const SkImage* b) {
|
|
|
|
// ensure that peekPixels will succeed
|
|
|
|
auto imga = a->makeRasterImage();
|
|
|
|
auto imgb = b->makeRasterImage();
|
2017-12-04 21:06:03 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkPixmap pm0, pm1;
|
|
|
|
return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1);
|
|
|
|
}
|
2018-01-02 20:40:29 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
sk_sp<SkSurface> makeSurface(SkCanvas* canvas,
|
|
|
|
const SkImageInfo& info,
|
|
|
|
const SkSurfaceProps* props) {
|
|
|
|
auto surf = canvas->makeSurface(info, props);
|
|
|
|
if (!surf) {
|
|
|
|
surf = SkSurface::MakeRaster(info, props);
|
2018-09-05 18:41:40 +00:00
|
|
|
}
|
2019-03-20 16:12:10 +00:00
|
|
|
return surf;
|
|
|
|
}
|
2019-03-22 20:30:07 +00:00
|
|
|
|
|
|
|
static DEFINE_bool(nativeFonts, true,
|
|
|
|
"If true, use native font manager and rendering. "
|
|
|
|
"If false, fonts will draw as portably as possible.");
|
|
|
|
#if defined(SK_BUILD_FOR_WIN)
|
|
|
|
static DEFINE_bool(gdi, false,
|
|
|
|
"Use GDI instead of DirectWrite for font rendering.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SetDefaultFontMgr() {
|
|
|
|
if (!FLAGS_nativeFonts) {
|
|
|
|
gSkFontMgr_DefaultFactory = &ToolUtils::MakePortableFontMgr;
|
|
|
|
}
|
|
|
|
#if defined(SK_BUILD_FOR_WIN)
|
|
|
|
if (FLAGS_gdi) {
|
|
|
|
gSkFontMgr_DefaultFactory = &SkFontMgr_New_GDI;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
} // namespace ToolUtils
|