blob: 110158233a6b972ba2d05b125dd08f666f1e7749 [file] [log] [blame] [view]
jcgregorio942262f2015-01-05 11:17:27 -08001Tips & FAQ
2==========
3
halcanary217a3332015-12-22 07:08:12 -08004+ [Gyp Options](#gypdefines)
5+ [Bitmap Subsetting](#bitmap-subsetting)
6+ [Capture a `.skp` file on a web page in Chromium](#skp-capture)
halcanaryd39d6f32016-09-12 11:56:28 -07007+ [Capture a `.mskp` file on a web page in Chromium](#mskp-capture)
halcanary217a3332015-12-22 07:08:12 -08008+ [How to add hardware acceleration in Skia](#hw-acceleration)
9+ [Does Skia support Font hinting?](#font-hinting)
10+ [Does Skia shape text (kerning)?](#kerning)
halcanaryff659642016-04-26 04:49:45 -070011+ [How do I add drop shadow on text?](#text-shadow)
halcanary217a3332015-12-22 07:08:12 -080012
13* * *
14
halcanaryb5002392015-11-16 07:37:23 -080015<span id="gypdefines"></span>
16
17Gyp Options
18-----------
19
20When running `sync-and-gyp`, the `GYP_DEFINES` environment variable can
21be used to change Skias compile-time settings, using a
22space-separated list of key=value pairs. For example, to disable both
23the 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
30Note: Setting enviroment variables in the Windows CMD.EXE shell [uses a
31different syntax](/user/quick/windows#env).
32
33You can also set environment variables such as `CC`, `CXX`,
kjlubick222b30d2015-12-03 09:20:55 -080034`CFLAGS`, `CXXFLAGS`, or `CPPFLAGS` to control how Skia is compiled.
35To build with clang, for example:
halcanaryb5002392015-11-16 07:37:23 -080036
37<!--?prettify lang=sh?-->
38
39 CC='clang' CXX='clang++' python bin/sync-and-gyp
40 ninja -C out/Debug
41
kjlubick222b30d2015-12-03 09:20:55 -080042To 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
halcanary217a3332015-12-22 07:08:12 -080047 CXXFLAGS='-Wunused-parameter' \
48 CC='clang' CXX='clang++' python bin/sync-and-gyp
kjlubick222b30d2015-12-03 09:20:55 -080049 ninja -C out/Debug
50
51
halcanaryb5002392015-11-16 07:37:23 -080052The `GYP_GENERATORS` environment variable can be used to set the
53build systems that you want to use (as a comma-separated list).
54The default is `'ninja,msvs-ninja'` on Windows, `'ninja,xcode'` on
55Mac OS X, and just `'ninja'` on Linux. For example, to generate
56only 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
63Finally, the `SKIA_OUT` environment variable can be used to set
64the path for the build directory. The default is `out` inside the
65top-level Skia source directory. For example to test Skia with
66two 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* * *
jcgregorio942262f2015-01-05 11:17:27 -080076
halcanary8d3f7bd2015-07-09 06:58:06 -070077<span id="bitmap-subsetting"></span>
78
halcanaryb5002392015-11-16 07:37:23 -080079Bitmap Subsetting
80-----------------
jcgregorio942262f2015-01-05 11:17:27 -080081
82Taking a subset of a bitmap is effectively free - no pixels are copied or
83memory is allocated. This allows Skia to offer an API that typically operates
84on entire bitmaps; clients who want to operate on a subset of a bitmap can use
85the following pattern, here being used to magnify a portion of an image with
86drawBitmapNine():
87
88 SkBitmap subset;
89 bitmap.extractSubset(&subset, rect);
90 canvas->drawBitmapNine(subset, ...);
91
jcgregorio1c2a2fe2016-04-22 11:25:43 -070092[An example](https://fiddle.skia.org/c/@subset_example)
93
halcanary217a3332015-12-22 07:08:12 -080094
halcanary8d3f7bd2015-07-09 06:58:06 -070095* * *
96
97<span id="skp-capture"></span>
98
halcanaryb5002392015-11-16 07:37:23 -080099Capture a `.skp` file on a web page in Chromium
100-----------------------------------------------
halcanary8d3f7bd2015-07-09 06:58:06 -0700101
halcanary5f0b0ad2015-07-08 10:56:01 -07001021. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking`
1032. Open the JS console (ctrl-shift-J)
1043. Execute: `chrome.gpuBenchmarking.printToSkPicture('/tmp')`
105 This returns "undefined" on success.
106
pdr1e2a7022016-07-06 06:10:25 -0700107Open the resulting file in the [Skia Debugger](/dev/tools/debugger), rasterize it with `dm`,
halcanary8d3f7bd2015-07-09 06:58:06 -0700108or use Skia's `SampleApp` to view it:
109
110<!--?prettify lang=sh?-->
halcanary5f0b0ad2015-07-08 10:56:01 -0700111
halcanary5f0b0ad2015-07-08 10:56:01 -0700112 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
halcanary8d3f7bd2015-07-09 06:58:06 -0700116 out/Release/SampleApp --picture /tmp/layer_0.skp
halcanary8d3f7bd2015-07-09 06:58:06 -0700117
118* * *
119
halcanaryd39d6f32016-09-12 11:56:28 -0700120<span id="mskp-capture"></span>
121
122Capture a `.mskp` file on a web page in Chromium
123-----------------------------------------------
124
125Multipage Skia Picture files capture the commands sent to produce PDFs
126and printed documents.
127
1281. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking`
1292. Open the JS console (ctrl-shift-J)
1303. Execute: `chrome.gpuBenchmarking.printPagesToSkPictures('/tmp/filename.mskp')`
131 This returns "undefined" on success.
132
133Open the resulting file in the [Skia Debugger](/dev/tools/debugger) or
134process 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
halcanary8d3f7bd2015-07-09 06:58:06 -0700148<span id="hw-acceleration"></span>
jcgregorio942262f2015-01-05 11:17:27 -0800149
halcanaryb5002392015-11-16 07:37:23 -0800150How to add hardware acceleration in Skia
151----------------------------------------
jcgregorio942262f2015-01-05 11:17:27 -0800152
halcanaryb5002392015-11-16 07:37:23 -0800153There are two ways Skia takes advantage of specific hardware.
jcgregorio942262f2015-01-05 11:17:27 -0800154
halcanaryb5002392015-11-16 07:37:23 -08001551. Subclass SkCanvas
jcgregorio942262f2015-01-05 11:17:27 -0800156
halcanaryb5002392015-11-16 07:37:23 -0800157 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/
jcgregorio942262f2015-01-05 11:17:27 -0800160
halcanaryb5002392015-11-16 07:37:23 -08001612. Custom bottleneck routines
jcgregorio942262f2015-01-05 11:17:27 -0800162
halcanaryb5002392015-11-16 07:37:23 -0800163 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/
jcgregorio942262f2015-01-05 11:17:27 -0800167
halcanary8d3f7bd2015-07-09 06:58:06 -0700168* * *
169
170<span id="font-hinting"></span>
171
halcanaryb5002392015-11-16 07:37:23 -0800172Does Skia support Font hinting?
173-------------------------------
jcgregorio942262f2015-01-05 11:17:27 -0800174
175Skia has a built-in font cache, but it does not know how to actual render font
halcanarya58d6762015-12-14 09:50:15 -0800176files like TrueType into its cache. For that it relies on the platform to
177supply an instance of SkScalerContext. This is Skia's abstract interface for
jcgregorio942262f2015-01-05 11:17:27 -0800178communicating with a font scaler engine. In src/ports you can see support
halcanarya58d6762015-12-14 09:50:15 -0800179files for FreeType, Mac OS X, and Windows GDI font engines. Other font
jcgregorio942262f2015-01-05 11:17:27 -0800180engines can easily be supported in a like manner.
181
182
halcanarya58d6762015-12-14 09:50:15 -0800183* * *
184
185<span id="kerning"></span>
186
187Does Skia shape text (kerning)?
188-------------------------------
189
190No. Skia provides interfaces to draw glyphs, but does not implement a
halcanary7d1c3e62015-12-14 10:03:31 -0800191text shaper. Skia's client's often use
192[HarfBuzz](http://www.freedesktop.org/wiki/Software/HarfBuzz/) to
193generate the glyphs and their positions, including kerning.
halcanarya58d6762015-12-14 09:50:15 -0800194
halcanary5441e9f2016-05-03 10:18:30 -0700195[Here is an example of how to use Skia and HarfBuzz
196together](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
199a sequence of glyphs and positions, and the SkTypeface can then be
200used to draw those glyphs.
201
halcanaryff659642016-04-26 04:49:45 -0700202* * *
203
204<span id="text-shadow"></span>
205
206How 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
halcanary5441e9f2016-05-03 10:18:30 -0700235* * *
236
halcanarya58d6762015-12-14 09:50:15 -0800237<div style="margin-bottom:99%"></div>