Home | History | Annotate | Download | only in tools
      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 #ifndef sk_tool_utils_DEFINED
      9 #define sk_tool_utils_DEFINED
     10 
     11 #include "SkColor.h"
     12 #include "SkImageEncoder.h"
     13 #include "SkImageInfo.h"
     14 #include "SkPixelSerializer.h"
     15 #include "SkRandom.h"
     16 #include "SkTDArray.h"
     17 #include "SkTypeface.h"
     18 
     19 class SkBitmap;
     20 class SkCanvas;
     21 class SkPaint;
     22 class SkPath;
     23 class SkShader;
     24 class SkTestFont;
     25 class SkTextBlobBuilder;
     26 
     27 namespace sk_tool_utils {
     28 
     29     const char* colortype_name(SkColorType);
     30 
     31     /**
     32      * Map opaque colors from 8888 to 565.
     33      */
     34     SkColor color_to_565(SkColor color);
     35 
     36     /**
     37      * Return a color emoji typeface if available.
     38      */
     39     void emoji_typeface(SkAutoTUnref<SkTypeface>* );
     40 
     41     /**
     42      * If the platform supports color emoji, return sample text the emoji can render.
     43      */
     44     const char* emoji_sample_text();
     45 
     46     /**
     47      * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
     48      */
     49     const char* platform_os_emoji();
     50 
     51     /**
     52      * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
     53      */
     54     const char* platform_os_name();
     55 
     56     /**
     57      * Return the platform name without the version number ("Mac", "Win", etc.) if available.
     58      */
     59     SkString major_platform_os_name();
     60 
     61     /**
     62      * Return the platform extra config (e.g. "GDI") if available.
     63      */
     64     const char* platform_extra_config(const char* config);
     65 
     66     /**
     67      * Map serif, san-serif, and monospace to the platform-specific font name.
     68      */
     69     const char* platform_font_name(const char* name);
     70 
     71     /**
     72      * Sets the paint to use a platform-independent text renderer
     73      */
     74     void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
     75                                SkTypeface::Style style = SkTypeface::kNormal);
     76 
     77     /**
     78      * Returns a platform-independent text renderer.
     79      */
     80     SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style);
     81 
     82     /** Call to clean up portable font references. */
     83     void release_portable_typefaces();
     84 
     85     /**
     86      *  Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
     87      *  the pixels are colorType + alphaType
     88      */
     89     void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
     90 
     91     // private to sk_tool_utils
     92     SkTypeface* create_font(const char* name, SkTypeface::Style);
     93 
     94     /** Returns a newly created CheckerboardShader. */
     95     SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size);
     96 
     97     /** Draw a checkerboard pattern in the current canvas, restricted to
     98         the current clip, using SkXfermode::kSrc_Mode. */
     99     void draw_checkerboard(SkCanvas* canvas,
    100                            SkColor color1,
    101                            SkColor color2,
    102                            int checkSize);
    103 
    104     /** Make it easier to create a bitmap-based checkerboard */
    105     SkBitmap create_checkerboard_bitmap(int w, int h,
    106                                         SkColor c1, SkColor c2,
    107                                         int checkSize);
    108 
    109     /** A default checkerboard. */
    110     inline void draw_checkerboard(SkCanvas* canvas) {
    111         sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
    112     }
    113 
    114     SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
    115                                   int textSize, const char* str);
    116 
    117     // A helper for inserting a drawtext call into a SkTextBlobBuilder
    118     void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
    119                           SkScalar x, SkScalar y);
    120 
    121     void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
    122 
    123     void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
    124 
    125     void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
    126 
    127     void make_big_path(SkPath& path);
    128 
    129     // Return a blurred version of 'src'. This doesn't use a separable filter
    130     // so it is slow!
    131     SkBitmap slow_blur(const SkBitmap& src, float sigma);
    132 
    133     // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
    134     class TopoTestNode {
    135     public:
    136         TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
    137 
    138         void dependsOn(TopoTestNode* src) {
    139             *fDependencies.append() = src;
    140         }
    141 
    142         int id() const { return fID; }
    143         void reset() { fOutputPos = -1; }
    144 
    145         int outputPos() const { return fOutputPos; }
    146 
    147         // check that the topological sort is valid for this node
    148         bool check() {
    149             if (-1 == fOutputPos) {
    150                 return false;
    151             }
    152 
    153             for (int i = 0; i < fDependencies.count(); ++i) {
    154                 if (-1 == fDependencies[i]->outputPos()) {
    155                     return false;
    156                 }
    157                 // This node should've been output after all the nodes on which it depends
    158                 if (fOutputPos < fDependencies[i]->outputPos()) {
    159                     return false;
    160                 }
    161             }
    162 
    163             return true;
    164         }
    165 
    166         // The following 7 methods are needed by the topological sort
    167         static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
    168         static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
    169         static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
    170         static void Output(TopoTestNode* node, int outputPos) {
    171             SkASSERT(-1 != outputPos);
    172             node->fOutputPos = outputPos;
    173         }
    174         static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
    175         static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
    176         static TopoTestNode* Dependency(TopoTestNode* node, int index) {
    177             return node->fDependencies[index];
    178         }
    179 
    180         // Helper functions for TopoSortBench & TopoSortTest
    181         static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) {
    182             graph->setReserve(num);
    183 
    184             for (int i = 0; i < num; ++i) {
    185                 *graph->append() = new TopoTestNode(i);
    186             }
    187         }
    188 
    189         static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) {
    190             for (int i = 0; i < graph->count(); ++i) {
    191                 delete (*graph)[i];
    192             }
    193         }
    194 
    195         #ifdef SK_DEBUG
    196         static void Print(const SkTDArray<TopoTestNode*>& graph) {
    197             for (int i = 0; i < graph.count(); ++i) {
    198                 SkDebugf("%d, ", graph[i]->id());
    199             }
    200             SkDebugf("\n");
    201         }
    202         #endif
    203 
    204         // randomize the array
    205         static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
    206             for (int i = graph->count()-1; i > 0; --i) {
    207                 int swap = rand->nextU() % (i+1);
    208 
    209                 TopoTestNode* tmp = (*graph)[i];
    210                 (*graph)[i] = (*graph)[swap];
    211                 (*graph)[swap] = tmp;
    212             }
    213         }
    214 
    215     private:
    216         int  fID;
    217         int  fOutputPos;
    218         bool fTempMark;
    219 
    220         SkTDArray<TopoTestNode*> fDependencies;
    221     };
    222 
    223 }  // namespace sk_tool_utils
    224 
    225 #endif  // sk_tool_utils_DEFINED
    226