ToolUtils.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifndef ToolUtils_DEFINED
  8. #define ToolUtils_DEFINED
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkData.h"
  11. #include "include/core/SkEncodedImageFormat.h"
  12. #include "include/core/SkFont.h"
  13. #include "include/core/SkFontStyle.h"
  14. #include "include/core/SkFontTypes.h"
  15. #include "include/core/SkImageEncoder.h"
  16. #include "include/core/SkImageInfo.h"
  17. #include "include/core/SkPixmap.h"
  18. #include "include/core/SkRect.h"
  19. #include "include/core/SkRefCnt.h"
  20. #include "include/core/SkScalar.h"
  21. #include "include/core/SkStream.h"
  22. #include "include/core/SkSurface.h"
  23. #include "include/core/SkTypeface.h"
  24. #include "include/core/SkTypes.h"
  25. #include "include/private/SkTArray.h"
  26. #include "include/private/SkTDArray.h"
  27. #include "include/utils/SkRandom.h"
  28. class SkBitmap;
  29. class SkCanvas;
  30. class SkFontStyle;
  31. class SkImage;
  32. class SkPath;
  33. class SkPixmap;
  34. class SkRRect;
  35. class SkShader;
  36. class SkSurface;
  37. class SkSurfaceProps;
  38. class SkTextBlobBuilder;
  39. class SkTypeface;
  40. namespace ToolUtils {
  41. const char* alphatype_name (SkAlphaType);
  42. const char* colortype_name (SkColorType);
  43. const char* colortype_depth(SkColorType); // like colortype_name, but channel order agnostic
  44. /**
  45. * Map opaque colors from 8888 to 565.
  46. */
  47. SkColor color_to_565(SkColor color);
  48. /* Return a color emoji typeface with planets to scale if available. */
  49. sk_sp<SkTypeface> planet_typeface();
  50. /** Return a color emoji typeface if available. */
  51. sk_sp<SkTypeface> emoji_typeface();
  52. /** Sample text for the emoji_typeface font. */
  53. const char* emoji_sample_text();
  54. /**
  55. * Returns a platform-independent text renderer.
  56. */
  57. sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
  58. static inline sk_sp<SkTypeface> create_portable_typeface() {
  59. return create_portable_typeface(nullptr, SkFontStyle());
  60. }
  61. /**
  62. * Turn on portable (--nonativeFonts) or GDI font rendering (--gdi).
  63. */
  64. void SetDefaultFontMgr();
  65. void get_text_path(const SkFont&,
  66. const void* text,
  67. size_t length,
  68. SkTextEncoding,
  69. SkPath*,
  70. const SkPoint* positions = nullptr);
  71. /**
  72. * Returns true iff all of the pixels between the two images are identical.
  73. *
  74. * If the configs differ, return false.
  75. */
  76. bool equal_pixels(const SkPixmap&, const SkPixmap&);
  77. bool equal_pixels(const SkBitmap&, const SkBitmap&);
  78. bool equal_pixels(const SkImage* a, const SkImage* b);
  79. /** Returns a newly created CheckerboardShader. */
  80. sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
  81. /** Draw a checkerboard pattern in the current canvas, restricted to
  82. the current clip, using SkXfermode::kSrc_Mode. */
  83. void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize);
  84. /** Make it easier to create a bitmap-based checkerboard */
  85. SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize);
  86. /** A default checkerboard. */
  87. inline void draw_checkerboard(SkCanvas* canvas) {
  88. ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
  89. }
  90. SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
  91. // If the canvas does't make a surface (e.g. recording), make a raster surface
  92. sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
  93. // A helper for inserting a drawtext call into a SkTextBlobBuilder
  94. void add_to_text_blob_w_len(SkTextBlobBuilder*,
  95. const char* text,
  96. size_t len,
  97. SkTextEncoding,
  98. const SkFont&,
  99. SkScalar x,
  100. SkScalar y);
  101. void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y);
  102. // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
  103. //
  104. // moveTo(pts[0]);
  105. // lineTo(pts[step % numPts]);
  106. // ...
  107. // lineTo(pts[(step * (N - 1)) % numPts]);
  108. //
  109. // numPts=5, step=2 will produce a classic five-point star.
  110. //
  111. // numPts and step must be co-prime.
  112. SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
  113. void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
  114. void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
  115. void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
  116. void make_big_path(SkPath& path);
  117. // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
  118. class TopoTestNode : public SkRefCnt {
  119. public:
  120. TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) {}
  121. void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
  122. int id() const { return fID; }
  123. void reset() { fOutputPos = -1; }
  124. int outputPos() const { return fOutputPos; }
  125. // check that the topological sort is valid for this node
  126. bool check() {
  127. if (-1 == fOutputPos) {
  128. return false;
  129. }
  130. for (int i = 0; i < fDependencies.count(); ++i) {
  131. if (-1 == fDependencies[i]->outputPos()) {
  132. return false;
  133. }
  134. // This node should've been output after all the nodes on which it depends
  135. if (fOutputPos < fDependencies[i]->outputPos()) {
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. // The following 7 methods are needed by the topological sort
  142. static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
  143. static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
  144. static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
  145. static void Output(TopoTestNode* node, int outputPos) {
  146. SkASSERT(-1 != outputPos);
  147. node->fOutputPos = outputPos;
  148. }
  149. static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
  150. static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
  151. static TopoTestNode* Dependency(TopoTestNode* node, int index) {
  152. return node->fDependencies[index];
  153. }
  154. // Helper functions for TopoSortBench & TopoSortTest
  155. static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) {
  156. graph->reserve(num);
  157. for (int i = 0; i < num; ++i) {
  158. graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
  159. }
  160. }
  161. #ifdef SK_DEBUG
  162. static void Print(const SkTArray<TopoTestNode*>& graph) {
  163. for (int i = 0; i < graph.count(); ++i) {
  164. SkDebugf("%d, ", graph[i]->id());
  165. }
  166. SkDebugf("\n");
  167. }
  168. #endif
  169. // randomize the array
  170. static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
  171. for (int i = graph->count() - 1; i > 0; --i) {
  172. int swap = rand->nextU() % (i + 1);
  173. (*graph)[i].swap((*graph)[swap]);
  174. }
  175. }
  176. private:
  177. int fID;
  178. int fOutputPos;
  179. bool fTempMark;
  180. SkTDArray<TopoTestNode*> fDependencies;
  181. };
  182. template <typename T>
  183. inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
  184. SkFILEWStream file(path);
  185. return file.isValid() && SkEncodeImage(&file, src, f, q);
  186. }
  187. bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
  188. void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
  189. class PixelIter {
  190. public:
  191. PixelIter();
  192. PixelIter(SkSurface* surf) {
  193. SkPixmap pm;
  194. if (!surf->peekPixels(&pm)) {
  195. pm.reset();
  196. }
  197. this->reset(pm);
  198. }
  199. void reset(const SkPixmap& pm) {
  200. fPM = pm;
  201. fLoc = {-1, 0};
  202. }
  203. void* next(SkIPoint* loc = nullptr) {
  204. if (!fPM.addr()) {
  205. return nullptr;
  206. }
  207. fLoc.fX += 1;
  208. if (fLoc.fX >= fPM.width()) {
  209. fLoc.fX = 0;
  210. if (++fLoc.fY >= fPM.height()) {
  211. this->setDone();
  212. return nullptr;
  213. }
  214. }
  215. if (loc) {
  216. *loc = fLoc;
  217. }
  218. return fPM.writable_addr(fLoc.fX, fLoc.fY);
  219. }
  220. void setDone() { fPM.reset(); }
  221. private:
  222. SkPixmap fPM;
  223. SkIPoint fLoc;
  224. };
  225. } // namespace ToolUtils
  226. #endif // ToolUtils_DEFINED