jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 1 | Tips & FAQ |
| 2 | ========== |
| 3 | |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 4 | + [Gyp Options](#gypdefines) |
| 5 | + [Bitmap Subsetting](#bitmap-subsetting) |
| 6 | + [Capture a `.skp` file on a web page in Chromium](#skp-capture) |
halcanary | d39d6f3 | 2016-09-12 11:56:28 -0700 | [diff] [blame] | 7 | + [Capture a `.mskp` file on a web page in Chromium](#mskp-capture) |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 8 | + [How to add hardware acceleration in Skia](#hw-acceleration) |
| 9 | + [Does Skia support Font hinting?](#font-hinting) |
| 10 | + [Does Skia shape text (kerning)?](#kerning) |
halcanary | ff65964 | 2016-04-26 04:49:45 -0700 | [diff] [blame] | 11 | + [How do I add drop shadow on text?](#text-shadow) |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 12 | |
| 13 | * * * |
| 14 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 15 | <span id="gypdefines">Gyp Options</span> |
| 16 | ---------------------------------------- |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 17 | |
| 18 | When running `sync-and-gyp`, the `GYP_DEFINES` environment variable can |
| 19 | be used to change Skia’s compile-time settings, using a |
| 20 | space-separated list of key=value pairs. For example, to disable both |
| 21 | the Skia GPU backend and PDF backends, run it as follows: |
| 22 | |
| 23 | <!--?prettify lang=sh?--> |
| 24 | |
| 25 | GYP_DEFINES='skia_gpu=0 skia_pdf=0' python bin/sync-and-gyp |
| 26 | ninja -C out/Debug |
| 27 | |
| 28 | Note: Setting enviroment variables in the Windows CMD.EXE shell [uses a |
| 29 | different syntax](/user/quick/windows#env). |
| 30 | |
| 31 | You can also set environment variables such as `CC`, `CXX`, |
kjlubick | 222b30d | 2015-12-03 09:20:55 -0800 | [diff] [blame] | 32 | `CFLAGS`, `CXXFLAGS`, or `CPPFLAGS` to control how Skia is compiled. |
| 33 | To build with clang, for example: |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 34 | |
| 35 | <!--?prettify lang=sh?--> |
| 36 | |
| 37 | CC='clang' CXX='clang++' python bin/sync-and-gyp |
| 38 | ninja -C out/Debug |
| 39 | |
kjlubick | 222b30d | 2015-12-03 09:20:55 -0800 | [diff] [blame] | 40 | To build with clang and enable a compiler warning for unused parameters in C++ |
| 41 | (but not C or assembly) code: |
| 42 | |
| 43 | <!--?prettify lang=sh?--> |
| 44 | |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 45 | CXXFLAGS='-Wunused-parameter' \ |
| 46 | CC='clang' CXX='clang++' python bin/sync-and-gyp |
kjlubick | 222b30d | 2015-12-03 09:20:55 -0800 | [diff] [blame] | 47 | ninja -C out/Debug |
| 48 | |
| 49 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 50 | The `GYP_GENERATORS` environment variable can be used to set the |
| 51 | build systems that you want to use (as a comma-separated list). |
| 52 | The default is `'ninja,msvs-ninja'` on Windows, `'ninja,xcode'` on |
| 53 | Mac OS X, and just `'ninja'` on Linux. For example, to generate |
| 54 | only Ninja files on Mac: |
| 55 | |
| 56 | <!--?prettify lang=sh?--> |
| 57 | |
| 58 | GYP_GENERATORS='ninja' python bin/sync-and-gyp |
| 59 | ninja -C out/Debug |
| 60 | |
| 61 | Finally, the `SKIA_OUT` environment variable can be used to set |
| 62 | the path for the build directory. The default is `out` inside the |
| 63 | top-level Skia source directory. For example to test Skia with |
| 64 | two different compilers: |
| 65 | |
| 66 | <!--?prettify lang=sh?--> |
| 67 | |
| 68 | CC='clang' CXX='clang++' SKIA_OUT=~/build/skia_clang python bin/sync-and-gyp |
| 69 | CC='gcc' CXX='g++' SKIA_OUT=~/build/skia_gcc python bin/sync-and-gyp |
| 70 | ninja -C ~/build/skia_clang/Debug |
| 71 | ninja -C ~/build/skia_gcc/Debug |
| 72 | |
| 73 | * * * |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 74 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 75 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 76 | |
| 77 | <span id="bitmap-subsetting">Bitmap Subsetting</span> |
| 78 | ----------------------------------------------------- |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 79 | |
| 80 | Taking a subset of a bitmap is effectively free - no pixels are copied or |
| 81 | memory is allocated. This allows Skia to offer an API that typically operates |
| 82 | on entire bitmaps; clients who want to operate on a subset of a bitmap can use |
| 83 | the following pattern, here being used to magnify a portion of an image with |
| 84 | drawBitmapNine(): |
| 85 | |
| 86 | SkBitmap subset; |
| 87 | bitmap.extractSubset(&subset, rect); |
| 88 | canvas->drawBitmapNine(subset, ...); |
| 89 | |
jcgregorio | 1c2a2fe | 2016-04-22 11:25:43 -0700 | [diff] [blame] | 90 | [An example](https://fiddle.skia.org/c/@subset_example) |
| 91 | |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 92 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 93 | * * * |
| 94 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 95 | <span id="skp-capture">Capture a `.skp` file on a web page in Chromium</span> |
| 96 | ----------------------------------------------------------------------------- |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 97 | |
halcanary | 5f0b0ad | 2015-07-08 10:56:01 -0700 | [diff] [blame] | 98 | 1. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking` |
| 99 | 2. Open the JS console (ctrl-shift-J) |
| 100 | 3. Execute: `chrome.gpuBenchmarking.printToSkPicture('/tmp')` |
| 101 | This returns "undefined" on success. |
| 102 | |
pdr | 1e2a702 | 2016-07-06 06:10:25 -0700 | [diff] [blame] | 103 | Open the resulting file in the [Skia Debugger](/dev/tools/debugger), rasterize it with `dm`, |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 104 | or use Skia's `SampleApp` to view it: |
| 105 | |
| 106 | <!--?prettify lang=sh?--> |
halcanary | 5f0b0ad | 2015-07-08 10:56:01 -0700 | [diff] [blame] | 107 | |
halcanary | 5f0b0ad | 2015-07-08 10:56:01 -0700 | [diff] [blame] | 108 | out/Release/dm --src skp --skps /tmp/layer_0.skp -w /tmp \ |
| 109 | --config 8888 gpu pdf --verbose |
| 110 | ls -l /tmp/*/skp/layer_0.skp.* |
| 111 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 112 | out/Release/SampleApp --picture /tmp/layer_0.skp |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 113 | |
| 114 | * * * |
| 115 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 116 | <span id="mskp-capture">Capture a `.mskp` file on a web page in Chromium</span> |
| 117 | ------------------------------------------------------------------------------- |
halcanary | d39d6f3 | 2016-09-12 11:56:28 -0700 | [diff] [blame] | 118 | |
| 119 | Multipage Skia Picture files capture the commands sent to produce PDFs |
| 120 | and printed documents. |
| 121 | |
| 122 | 1. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking` |
| 123 | 2. Open the JS console (ctrl-shift-J) |
| 124 | 3. Execute: `chrome.gpuBenchmarking.printPagesToSkPictures('/tmp/filename.mskp')` |
| 125 | This returns "undefined" on success. |
| 126 | |
| 127 | Open the resulting file in the [Skia Debugger](/dev/tools/debugger) or |
| 128 | process it with `dm`. |
| 129 | |
| 130 | <!--?prettify lang=sh?--> |
| 131 | |
| 132 | experimental/tools/mskp_parser.py /tmp/filename.mskp /tmp/filename.mskp.skp |
| 133 | ls -l /tmp/filename.mskp.skp |
| 134 | # open filename.mskp.skp in the debugger. |
| 135 | |
| 136 | out/Release/dm --src mskp --mskps /tmp/filename.mskp -w /tmp \ |
| 137 | --config pdf --verbose |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 138 | ls -l /tmp/pdf/mskp/filename.mskp.pdf |
halcanary | d39d6f3 | 2016-09-12 11:56:28 -0700 | [diff] [blame] | 139 | |
| 140 | * * * |
| 141 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 142 | <span id="hw-acceleration">How to add hardware acceleration in Skia</span> |
| 143 | -------------------------------------------------------------------------- |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 144 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 145 | There are two ways Skia takes advantage of specific hardware. |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 146 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 147 | 1. Subclass SkCanvas |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 148 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 149 | Since all drawing calls go through SkCanvas, those calls can be |
| 150 | redirected to a different graphics API. SkGLCanvas has been |
| 151 | written to direct its drawing calls to OpenGL. See src/gl/ |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 152 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 153 | 2. Custom bottleneck routines |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 154 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 155 | There are sets of bottleneck routines inside the blits of Skia |
| 156 | that can be replace on a platform in order to take advantage of |
| 157 | specific CPU features. One such example is the NEON SIMD |
| 158 | instructions on ARM v7 devices. See src/opts/ |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 159 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 160 | * * * |
| 161 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 162 | <span id="font-hinting">Does Skia support Font hinting?</span> |
| 163 | -------------------------------------------------------------- |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 164 | |
| 165 | Skia has a built-in font cache, but it does not know how to actual render font |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 166 | files like TrueType into its cache. For that it relies on the platform to |
| 167 | supply an instance of SkScalerContext. This is Skia's abstract interface for |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 168 | communicating with a font scaler engine. In src/ports you can see support |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 169 | files for FreeType, Mac OS X, and Windows GDI font engines. Other font |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 170 | engines can easily be supported in a like manner. |
| 171 | |
| 172 | |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 173 | * * * |
| 174 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 175 | <span id="kerning">Does Skia shape text (kerning)?</span> |
| 176 | --------------------------------------------------------- |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 177 | |
| 178 | No. Skia provides interfaces to draw glyphs, but does not implement a |
halcanary | 7d1c3e6 | 2015-12-14 10:03:31 -0800 | [diff] [blame] | 179 | text shaper. Skia's client's often use |
| 180 | [HarfBuzz](http://www.freedesktop.org/wiki/Software/HarfBuzz/) to |
| 181 | generate the glyphs and their positions, including kerning. |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 182 | |
halcanary | 5441e9f | 2016-05-03 10:18:30 -0700 | [diff] [blame] | 183 | [Here is an example of how to use Skia and HarfBuzz |
| 184 | together](https://github.com/aam/skiaex). In the example, a |
| 185 | `SkTypeface` and a `hb_face_t` are created using the same `mmap()`ed |
| 186 | `.ttf` font file. The HarfBuzz face is used to shape unicode text into |
| 187 | a sequence of glyphs and positions, and the SkTypeface can then be |
| 188 | used to draw those glyphs. |
| 189 | |
halcanary | ff65964 | 2016-04-26 04:49:45 -0700 | [diff] [blame] | 190 | * * * |
| 191 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame^] | 192 | <span id="text-shadow">How do I add drop shadow on text?</span> |
| 193 | --------------------------------------------------------------- |
halcanary | ff65964 | 2016-04-26 04:49:45 -0700 | [diff] [blame] | 194 | |
| 195 | <!--?prettify lang=cc?--> |
| 196 | |
| 197 | void draw(SkCanvas* canvas) { |
| 198 | const char text[] = "Skia"; |
| 199 | const SkScalar radius = 2.0f; |
| 200 | const SkScalar xDrop = 2.0f; |
| 201 | const SkScalar yDrop = 2.0f; |
| 202 | const SkScalar x = 8.0f; |
| 203 | const SkScalar y = 52.0f; |
| 204 | const SkScalar textSize = 48.0f; |
| 205 | const uint8_t blurAlpha = 127; |
| 206 | canvas->drawColor(SK_ColorWHITE); |
| 207 | SkPaint paint; |
| 208 | paint.setAntiAlias(true); |
| 209 | paint.setTextSize(textSize); |
| 210 | SkPaint blur(paint); |
| 211 | blur.setAlpha(blurAlpha); |
| 212 | blur.setMaskFilter(SkBlurMaskFilter::Make( |
| 213 | kNormal_SkBlurStyle, |
| 214 | SkBlurMaskFilter::ConvertRadiusToSigma(radius), 0)); |
| 215 | canvas->drawText(text, strlen(text), x + xDrop, y + yDrop, blur); |
| 216 | canvas->drawText(text, strlen(text), x, y, paint); |
| 217 | } |
| 218 | |
| 219 | <a href='https://fiddle.skia.org/c/@text_shadow'><img src='https://fiddle.skia.org/i/@text_shadow_raster.png'></a> |
| 220 | |
halcanary | 5441e9f | 2016-05-03 10:18:30 -0700 | [diff] [blame] | 221 | * * * |
| 222 | |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 223 | <div style="margin-bottom:99%"></div> |