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