2014-03-07 03:25:16 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef sk_tool_utils_DEFINED
|
|
|
|
#define sk_tool_utils_DEFINED
|
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
#include "SkColor.h"
|
2015-03-23 12:46:51 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2014-06-21 17:54:17 +00:00
|
|
|
#include "SkImageInfo.h"
|
2015-10-19 19:15:55 +00:00
|
|
|
#include "SkRandom.h"
|
2018-02-07 22:08:21 +00:00
|
|
|
#include "SkRefCnt.h"
|
2016-11-30 22:07:59 +00:00
|
|
|
#include "SkStream.h"
|
2015-10-19 19:15:55 +00:00
|
|
|
#include "SkTDArray.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include "SkTypeface.h"
|
2014-03-07 03:25:16 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
class SkBitmap;
|
|
|
|
class SkCanvas;
|
2017-01-26 14:32:33 +00:00
|
|
|
class SkColorFilter;
|
2017-12-04 21:06:03 +00:00
|
|
|
class SkImage;
|
2014-07-31 12:58:44 +00:00
|
|
|
class SkPaint;
|
2015-10-05 14:23:30 +00:00
|
|
|
class SkPath;
|
2016-08-04 16:03:19 +00:00
|
|
|
class SkRRect;
|
2015-01-26 20:49:00 +00:00
|
|
|
class SkShader;
|
2018-01-02 20:40:29 +00:00
|
|
|
class SkSurface;
|
|
|
|
class SkSurfaceProps;
|
2014-07-31 12:58:44 +00:00
|
|
|
class SkTestFont;
|
2015-04-14 19:17:27 +00:00
|
|
|
class SkTextBlobBuilder;
|
2014-03-18 03:02:11 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
namespace sk_tool_utils {
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2018-02-07 21:05:29 +00:00
|
|
|
const char* alphatype_name(SkAlphaType);
|
2014-03-17 21:31:26 +00:00
|
|
|
const char* colortype_name(SkColorType);
|
2015-07-24 18:08:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map opaque colors from 8888 to 565.
|
|
|
|
*/
|
2015-06-15 13:51:08 +00:00
|
|
|
SkColor color_to_565(SkColor color);
|
2015-07-24 18:08:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a color emoji typeface if available.
|
|
|
|
*/
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> emoji_typeface();
|
2015-07-24 18:08:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the platform supports color emoji, return sample text the emoji can render.
|
|
|
|
*/
|
2015-07-21 19:27:36 +00:00
|
|
|
const char* emoji_sample_text();
|
2015-07-24 18:08:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-13 16:03:16 +00:00
|
|
|
* Returns a string describing the platform font manager, if we're using one, otherwise "".
|
2015-07-24 18:08:01 +00:00
|
|
|
*/
|
2017-11-13 16:03:16 +00:00
|
|
|
const char* platform_font_manager();
|
2014-03-07 03:25:16 +00:00
|
|
|
|
2015-07-24 18:08:01 +00:00
|
|
|
/**
|
|
|
|
* Sets the paint to use a platform-independent text renderer
|
|
|
|
*/
|
2015-08-27 14:41:13 +00:00
|
|
|
void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
|
2016-05-31 18:42:36 +00:00
|
|
|
SkFontStyle style = SkFontStyle());
|
2015-07-24 18:08:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a platform-independent text renderer.
|
|
|
|
*/
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
|
2015-07-24 18:08:01 +00:00
|
|
|
|
2015-06-15 13:48:30 +00:00
|
|
|
/** Call to clean up portable font references. */
|
|
|
|
void release_portable_typefaces();
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-03-07 03:25:16 +00:00
|
|
|
/**
|
2018-02-08 19:10:40 +00:00
|
|
|
* Call writePixels() by using the pixels from bitmap, but with an info that claims
|
2014-03-07 03:25:16 +00:00
|
|
|
* the pixels are colorType + alphaType
|
|
|
|
*/
|
|
|
|
void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
|
2018-02-08 19:10:40 +00:00
|
|
|
void write_pixels(SkSurface*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
|
2014-03-07 03:25:16 +00:00
|
|
|
|
2017-08-08 19:48:54 +00:00
|
|
|
/**
|
|
|
|
* Returns true iff all of the pixels between the two images differ by <= the maxDiff value
|
|
|
|
* per component.
|
|
|
|
*
|
|
|
|
* If the configs differ, return false.
|
|
|
|
*
|
|
|
|
* If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1
|
|
|
|
*/
|
2017-12-04 21:06:03 +00:00
|
|
|
bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0,
|
|
|
|
bool respectColorSpaces = false);
|
|
|
|
bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0,
|
|
|
|
bool respectColorSpaces = false);
|
2017-12-05 20:11:24 +00:00
|
|
|
bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff = 0,
|
2017-12-04 21:06:03 +00:00
|
|
|
bool respectColorSpaces = false);
|
2017-08-08 19:48:54 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
// private to sk_tool_utils
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);
|
2014-07-31 12:58:44 +00:00
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
/** Returns a newly created CheckerboardShader. */
|
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
|
|
|
|
|
|
|
/** Draw a checkerboard pattern in the current canvas, restricted to
|
2015-01-27 16:38:35 +00:00
|
|
|
the current clip, using SkXfermode::kSrc_Mode. */
|
2015-01-26 20:49:00 +00:00
|
|
|
void draw_checkerboard(SkCanvas* canvas,
|
|
|
|
SkColor color1,
|
|
|
|
SkColor color2,
|
2015-09-03 20:32:33 +00:00
|
|
|
int checkSize);
|
|
|
|
|
|
|
|
/** Make it easier to create a bitmap-based checkerboard */
|
|
|
|
SkBitmap create_checkerboard_bitmap(int w, int h,
|
|
|
|
SkColor c1, SkColor c2,
|
|
|
|
int checkSize);
|
2015-01-26 20:49:00 +00:00
|
|
|
|
|
|
|
/** A default checkerboard. */
|
|
|
|
inline void draw_checkerboard(SkCanvas* canvas) {
|
|
|
|
sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
|
|
|
|
int textSize, const char* str);
|
|
|
|
|
2018-01-02 20:40:29 +00:00
|
|
|
// If the canvas does't make a surface (e.g. recording), make a raster surface
|
|
|
|
sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
|
|
|
|
|
2015-04-14 19:17:27 +00:00
|
|
|
// A helper for inserting a drawtext call into a SkTextBlobBuilder
|
2017-08-15 17:28:42 +00:00
|
|
|
void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
|
|
|
|
const SkPaint& origPaint, SkScalar x, SkScalar y);
|
|
|
|
|
|
|
|
void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
|
|
|
|
const SkPaint& origPaint, SkScalar x, SkScalar y);
|
2015-04-14 19:17:27 +00:00
|
|
|
|
2017-11-06 21:10:54 +00:00
|
|
|
// Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
|
|
|
|
//
|
|
|
|
// moveTo(pts[0]);
|
|
|
|
// lineTo(pts[step % numPts]);
|
|
|
|
// ...
|
|
|
|
// lineTo(pts[(step * (N - 1)) % numPts]);
|
|
|
|
//
|
|
|
|
// numPts=5, step=2 will produce a classic five-point star.
|
|
|
|
//
|
|
|
|
// numPts and step must be co-prime.
|
|
|
|
SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
|
|
|
|
|
2015-10-05 14:23:30 +00:00
|
|
|
void make_big_path(SkPath& path);
|
2015-10-19 13:39:17 +00:00
|
|
|
|
|
|
|
// Return a blurred version of 'src'. This doesn't use a separable filter
|
|
|
|
// so it is slow!
|
|
|
|
SkBitmap slow_blur(const SkBitmap& src, float sigma);
|
|
|
|
|
2016-08-04 16:03:19 +00:00
|
|
|
SkRect compute_central_occluder(const SkRRect& rr);
|
2016-08-04 19:35:46 +00:00
|
|
|
SkRect compute_widest_occluder(const SkRRect& rr);
|
|
|
|
SkRect compute_tallest_occluder(const SkRRect& rr);
|
2016-08-04 16:03:19 +00:00
|
|
|
|
2015-10-19 19:15:55 +00:00
|
|
|
// A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
|
2018-02-07 22:08:21 +00:00
|
|
|
class TopoTestNode : public SkRefCnt {
|
2015-10-19 19:15:55 +00:00
|
|
|
public:
|
|
|
|
TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
|
|
|
|
|
|
|
|
void dependsOn(TopoTestNode* src) {
|
|
|
|
*fDependencies.append() = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
int id() const { return fID; }
|
|
|
|
void reset() { fOutputPos = -1; }
|
|
|
|
|
|
|
|
int outputPos() const { return fOutputPos; }
|
|
|
|
|
|
|
|
// check that the topological sort is valid for this node
|
|
|
|
bool check() {
|
|
|
|
if (-1 == fOutputPos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < fDependencies.count(); ++i) {
|
|
|
|
if (-1 == fDependencies[i]->outputPos()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// This node should've been output after all the nodes on which it depends
|
|
|
|
if (fOutputPos < fDependencies[i]->outputPos()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following 7 methods are needed by the topological sort
|
|
|
|
static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
|
|
|
|
static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
|
|
|
|
static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
|
2016-03-29 16:03:52 +00:00
|
|
|
static void Output(TopoTestNode* node, int outputPos) {
|
2015-10-19 19:15:55 +00:00
|
|
|
SkASSERT(-1 != outputPos);
|
2016-03-29 16:03:52 +00:00
|
|
|
node->fOutputPos = outputPos;
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
|
|
|
|
static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
|
2016-03-29 16:03:52 +00:00
|
|
|
static TopoTestNode* Dependency(TopoTestNode* node, int index) {
|
2015-10-19 19:15:55 +00:00
|
|
|
return node->fDependencies[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions for TopoSortBench & TopoSortTest
|
2018-02-07 22:08:21 +00:00
|
|
|
static void AllocNodes(SkTArray<sk_sp<sk_tool_utils::TopoTestNode>>* graph, int num) {
|
|
|
|
graph->reserve(num);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < num; ++i) {
|
2018-02-07 22:08:21 +00:00
|
|
|
graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
static void Print(const SkTArray<TopoTestNode*>& graph) {
|
2015-10-19 19:15:55 +00:00
|
|
|
for (int i = 0; i < graph.count(); ++i) {
|
|
|
|
SkDebugf("%d, ", graph[i]->id());
|
|
|
|
}
|
|
|
|
SkDebugf("\n");
|
|
|
|
}
|
2018-02-07 22:08:21 +00:00
|
|
|
#endif
|
2015-10-19 19:15:55 +00:00
|
|
|
|
|
|
|
// randomize the array
|
2018-02-07 22:08:21 +00:00
|
|
|
static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
|
2015-10-19 19:15:55 +00:00
|
|
|
for (int i = graph->count()-1; i > 0; --i) {
|
|
|
|
int swap = rand->nextU() % (i+1);
|
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[i].swap((*graph)[swap]);
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fID;
|
|
|
|
int fOutputPos;
|
|
|
|
bool fTempMark;
|
|
|
|
|
|
|
|
SkTDArray<TopoTestNode*> fDependencies;
|
|
|
|
};
|
|
|
|
|
2016-11-23 15:55:18 +00:00
|
|
|
template <typename T>
|
|
|
|
inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
|
|
|
|
SkFILEWStream file(path);
|
|
|
|
return file.isValid() && SkEncodeImage(&file, src, f, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) {
|
|
|
|
SkDynamicMemoryWStream buf;
|
|
|
|
return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-28 15:15:22 +00:00
|
|
|
bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
|
2017-01-18 16:49:33 +00:00
|
|
|
void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
|
2014-06-21 17:54:17 +00:00
|
|
|
} // namespace sk_tool_utils
|
|
|
|
|
|
|
|
#endif // sk_tool_utils_DEFINED
|