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 | + [Bitmap Subsetting](#bitmap-subsetting) |
| 5 | + [Capture a `.skp` file on a web page in Chromium](#skp-capture) |
halcanary | d39d6f3 | 2016-09-12 11:56:28 -0700 | [diff] [blame] | 6 | + [Capture a `.mskp` file on a web page in Chromium](#mskp-capture) |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 7 | + [How to add hardware acceleration in Skia](#hw-acceleration) |
| 8 | + [Does Skia support Font hinting?](#font-hinting) |
| 9 | + [Does Skia shape text (kerning)?](#kerning) |
halcanary | ff65964 | 2016-04-26 04:49:45 -0700 | [diff] [blame] | 10 | + [How do I add drop shadow on text?](#text-shadow) |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 11 | |
| 12 | * * * |
| 13 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 14 | <span id="bitmap-subsetting">Bitmap Subsetting</span> |
| 15 | ----------------------------------------------------- |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 16 | |
| 17 | Taking a subset of a bitmap is effectively free - no pixels are copied or |
| 18 | memory is allocated. This allows Skia to offer an API that typically operates |
| 19 | on entire bitmaps; clients who want to operate on a subset of a bitmap can use |
| 20 | the following pattern, here being used to magnify a portion of an image with |
| 21 | drawBitmapNine(): |
| 22 | |
| 23 | SkBitmap subset; |
| 24 | bitmap.extractSubset(&subset, rect); |
| 25 | canvas->drawBitmapNine(subset, ...); |
| 26 | |
jcgregorio | 1c2a2fe | 2016-04-22 11:25:43 -0700 | [diff] [blame] | 27 | [An example](https://fiddle.skia.org/c/@subset_example) |
| 28 | |
halcanary | 217a333 | 2015-12-22 07:08:12 -0800 | [diff] [blame] | 29 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 30 | * * * |
| 31 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 32 | <span id="skp-capture">Capture a `.skp` file on a web page in Chromium</span> |
| 33 | ----------------------------------------------------------------------------- |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 34 | |
halcanary | 5f0b0ad | 2015-07-08 10:56:01 -0700 | [diff] [blame] | 35 | 1. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking` |
| 36 | 2. Open the JS console (ctrl-shift-J) |
| 37 | 3. Execute: `chrome.gpuBenchmarking.printToSkPicture('/tmp')` |
| 38 | This returns "undefined" on success. |
| 39 | |
pdr | 1e2a702 | 2016-07-06 06:10:25 -0700 | [diff] [blame] | 40 | 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] | 41 | or use Skia's `SampleApp` to view it: |
| 42 | |
| 43 | <!--?prettify lang=sh?--> |
halcanary | 5f0b0ad | 2015-07-08 10:56:01 -0700 | [diff] [blame] | 44 | |
halcanary | 5f0b0ad | 2015-07-08 10:56:01 -0700 | [diff] [blame] | 45 | out/Release/dm --src skp --skps /tmp/layer_0.skp -w /tmp \ |
| 46 | --config 8888 gpu pdf --verbose |
| 47 | ls -l /tmp/*/skp/layer_0.skp.* |
| 48 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 49 | out/Release/SampleApp --picture /tmp/layer_0.skp |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 50 | |
| 51 | * * * |
| 52 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 53 | <span id="mskp-capture">Capture a `.mskp` file on a web page in Chromium</span> |
| 54 | ------------------------------------------------------------------------------- |
halcanary | d39d6f3 | 2016-09-12 11:56:28 -0700 | [diff] [blame] | 55 | |
| 56 | Multipage Skia Picture files capture the commands sent to produce PDFs |
| 57 | and printed documents. |
| 58 | |
| 59 | 1. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking` |
| 60 | 2. Open the JS console (ctrl-shift-J) |
| 61 | 3. Execute: `chrome.gpuBenchmarking.printPagesToSkPictures('/tmp/filename.mskp')` |
| 62 | This returns "undefined" on success. |
| 63 | |
| 64 | Open the resulting file in the [Skia Debugger](/dev/tools/debugger) or |
| 65 | process it with `dm`. |
| 66 | |
| 67 | <!--?prettify lang=sh?--> |
| 68 | |
| 69 | experimental/tools/mskp_parser.py /tmp/filename.mskp /tmp/filename.mskp.skp |
| 70 | ls -l /tmp/filename.mskp.skp |
| 71 | # open filename.mskp.skp in the debugger. |
| 72 | |
| 73 | out/Release/dm --src mskp --mskps /tmp/filename.mskp -w /tmp \ |
| 74 | --config pdf --verbose |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 75 | ls -l /tmp/pdf/mskp/filename.mskp.pdf |
halcanary | d39d6f3 | 2016-09-12 11:56:28 -0700 | [diff] [blame] | 76 | |
| 77 | * * * |
| 78 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 79 | <span id="hw-acceleration">How to add hardware acceleration in Skia</span> |
| 80 | -------------------------------------------------------------------------- |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 81 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 82 | There are two ways Skia takes advantage of specific hardware. |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 83 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 84 | 1. Subclass SkCanvas |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 85 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 86 | Since all drawing calls go through SkCanvas, those calls can be |
| 87 | redirected to a different graphics API. SkGLCanvas has been |
| 88 | written to direct its drawing calls to OpenGL. See src/gl/ |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 89 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 90 | 2. Custom bottleneck routines |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 91 | |
halcanary | b500239 | 2015-11-16 07:37:23 -0800 | [diff] [blame] | 92 | There are sets of bottleneck routines inside the blits of Skia |
| 93 | that can be replace on a platform in order to take advantage of |
| 94 | specific CPU features. One such example is the NEON SIMD |
| 95 | instructions on ARM v7 devices. See src/opts/ |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 96 | |
halcanary | 8d3f7bd | 2015-07-09 06:58:06 -0700 | [diff] [blame] | 97 | * * * |
| 98 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 99 | <span id="font-hinting">Does Skia support Font hinting?</span> |
| 100 | -------------------------------------------------------------- |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 101 | |
| 102 | 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] | 103 | files like TrueType into its cache. For that it relies on the platform to |
| 104 | supply an instance of SkScalerContext. This is Skia's abstract interface for |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 105 | communicating with a font scaler engine. In src/ports you can see support |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 106 | files for FreeType, Mac OS X, and Windows GDI font engines. Other font |
jcgregorio | 942262f | 2015-01-05 11:17:27 -0800 | [diff] [blame] | 107 | engines can easily be supported in a like manner. |
| 108 | |
| 109 | |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 110 | * * * |
| 111 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 112 | <span id="kerning">Does Skia shape text (kerning)?</span> |
| 113 | --------------------------------------------------------- |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 114 | |
| 115 | No. Skia provides interfaces to draw glyphs, but does not implement a |
halcanary | 7d1c3e6 | 2015-12-14 10:03:31 -0800 | [diff] [blame] | 116 | text shaper. Skia's client's often use |
| 117 | [HarfBuzz](http://www.freedesktop.org/wiki/Software/HarfBuzz/) to |
| 118 | generate the glyphs and their positions, including kerning. |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 119 | |
halcanary | 5441e9f | 2016-05-03 10:18:30 -0700 | [diff] [blame] | 120 | [Here is an example of how to use Skia and HarfBuzz |
| 121 | together](https://github.com/aam/skiaex). In the example, a |
| 122 | `SkTypeface` and a `hb_face_t` are created using the same `mmap()`ed |
| 123 | `.ttf` font file. The HarfBuzz face is used to shape unicode text into |
| 124 | a sequence of glyphs and positions, and the SkTypeface can then be |
| 125 | used to draw those glyphs. |
| 126 | |
halcanary | ff65964 | 2016-04-26 04:49:45 -0700 | [diff] [blame] | 127 | * * * |
| 128 | |
halcanary | 75a171c | 2016-09-26 07:27:04 -0700 | [diff] [blame] | 129 | <span id="text-shadow">How do I add drop shadow on text?</span> |
| 130 | --------------------------------------------------------------- |
halcanary | ff65964 | 2016-04-26 04:49:45 -0700 | [diff] [blame] | 131 | |
| 132 | <!--?prettify lang=cc?--> |
| 133 | |
| 134 | void draw(SkCanvas* canvas) { |
| 135 | const char text[] = "Skia"; |
| 136 | const SkScalar radius = 2.0f; |
| 137 | const SkScalar xDrop = 2.0f; |
| 138 | const SkScalar yDrop = 2.0f; |
| 139 | const SkScalar x = 8.0f; |
| 140 | const SkScalar y = 52.0f; |
| 141 | const SkScalar textSize = 48.0f; |
| 142 | const uint8_t blurAlpha = 127; |
| 143 | canvas->drawColor(SK_ColorWHITE); |
| 144 | SkPaint paint; |
| 145 | paint.setAntiAlias(true); |
| 146 | paint.setTextSize(textSize); |
| 147 | SkPaint blur(paint); |
| 148 | blur.setAlpha(blurAlpha); |
| 149 | blur.setMaskFilter(SkBlurMaskFilter::Make( |
| 150 | kNormal_SkBlurStyle, |
| 151 | SkBlurMaskFilter::ConvertRadiusToSigma(radius), 0)); |
| 152 | canvas->drawText(text, strlen(text), x + xDrop, y + yDrop, blur); |
| 153 | canvas->drawText(text, strlen(text), x, y, paint); |
| 154 | } |
| 155 | |
| 156 | <a href='https://fiddle.skia.org/c/@text_shadow'><img src='https://fiddle.skia.org/i/@text_shadow_raster.png'></a> |
| 157 | |
halcanary | 5441e9f | 2016-05-03 10:18:30 -0700 | [diff] [blame] | 158 | * * * |
| 159 | |
halcanary | a58d676 | 2015-12-14 09:50:15 -0800 | [diff] [blame] | 160 | <div style="margin-bottom:99%"></div> |