blob: ec94db626068815d7f16181c2c3dbacaf414f5de [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
halcanary75a171c2016-09-26 07:27:04 -070015<span id="gypdefines">Gyp Options</span>
16----------------------------------------
halcanaryb5002392015-11-16 07:37:23 -080017
18When running `sync-and-gyp`, the `GYP_DEFINES` environment variable can
19be used to change Skias compile-time settings, using a
20space-separated list of key=value pairs. For example, to disable both
21the 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
28Note: Setting enviroment variables in the Windows CMD.EXE shell [uses a
29different syntax](/user/quick/windows#env).
30
31You can also set environment variables such as `CC`, `CXX`,
kjlubick222b30d2015-12-03 09:20:55 -080032`CFLAGS`, `CXXFLAGS`, or `CPPFLAGS` to control how Skia is compiled.
33To build with clang, for example:
halcanaryb5002392015-11-16 07:37:23 -080034
35<!--?prettify lang=sh?-->
36
37 CC='clang' CXX='clang++' python bin/sync-and-gyp
38 ninja -C out/Debug
39
kjlubick222b30d2015-12-03 09:20:55 -080040To 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
halcanary217a3332015-12-22 07:08:12 -080045 CXXFLAGS='-Wunused-parameter' \
46 CC='clang' CXX='clang++' python bin/sync-and-gyp
kjlubick222b30d2015-12-03 09:20:55 -080047 ninja -C out/Debug
48
49
halcanaryb5002392015-11-16 07:37:23 -080050The `GYP_GENERATORS` environment variable can be used to set the
51build systems that you want to use (as a comma-separated list).
52The default is `'ninja,msvs-ninja'` on Windows, `'ninja,xcode'` on
53Mac OS X, and just `'ninja'` on Linux. For example, to generate
54only 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
61Finally, the `SKIA_OUT` environment variable can be used to set
62the path for the build directory. The default is `out` inside the
63top-level Skia source directory. For example to test Skia with
64two 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* * *
jcgregorio942262f2015-01-05 11:17:27 -080074
halcanary8d3f7bd2015-07-09 06:58:06 -070075
halcanary75a171c2016-09-26 07:27:04 -070076
77<span id="bitmap-subsetting">Bitmap Subsetting</span>
78-----------------------------------------------------
jcgregorio942262f2015-01-05 11:17:27 -080079
80Taking a subset of a bitmap is effectively free - no pixels are copied or
81memory is allocated. This allows Skia to offer an API that typically operates
82on entire bitmaps; clients who want to operate on a subset of a bitmap can use
83the following pattern, here being used to magnify a portion of an image with
84drawBitmapNine():
85
86 SkBitmap subset;
87 bitmap.extractSubset(&subset, rect);
88 canvas->drawBitmapNine(subset, ...);
89
jcgregorio1c2a2fe2016-04-22 11:25:43 -070090[An example](https://fiddle.skia.org/c/@subset_example)
91
halcanary217a3332015-12-22 07:08:12 -080092
halcanary8d3f7bd2015-07-09 06:58:06 -070093* * *
94
halcanary75a171c2016-09-26 07:27:04 -070095<span id="skp-capture">Capture a `.skp` file on a web page in Chromium</span>
96-----------------------------------------------------------------------------
halcanary8d3f7bd2015-07-09 06:58:06 -070097
halcanary5f0b0ad2015-07-08 10:56:01 -0700981. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking`
992. Open the JS console (ctrl-shift-J)
1003. Execute: `chrome.gpuBenchmarking.printToSkPicture('/tmp')`
101 This returns "undefined" on success.
102
pdr1e2a7022016-07-06 06:10:25 -0700103Open the resulting file in the [Skia Debugger](/dev/tools/debugger), rasterize it with `dm`,
halcanary8d3f7bd2015-07-09 06:58:06 -0700104or use Skia's `SampleApp` to view it:
105
106<!--?prettify lang=sh?-->
halcanary5f0b0ad2015-07-08 10:56:01 -0700107
halcanary5f0b0ad2015-07-08 10:56:01 -0700108 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
halcanary8d3f7bd2015-07-09 06:58:06 -0700112 out/Release/SampleApp --picture /tmp/layer_0.skp
halcanary8d3f7bd2015-07-09 06:58:06 -0700113
114* * *
115
halcanary75a171c2016-09-26 07:27:04 -0700116<span id="mskp-capture">Capture a `.mskp` file on a web page in Chromium</span>
117-------------------------------------------------------------------------------
halcanaryd39d6f32016-09-12 11:56:28 -0700118
119Multipage Skia Picture files capture the commands sent to produce PDFs
120and printed documents.
121
1221. Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking`
1232. Open the JS console (ctrl-shift-J)
1243. Execute: `chrome.gpuBenchmarking.printPagesToSkPictures('/tmp/filename.mskp')`
125 This returns "undefined" on success.
126
127Open the resulting file in the [Skia Debugger](/dev/tools/debugger) or
128process 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
halcanary75a171c2016-09-26 07:27:04 -0700138 ls -l /tmp/pdf/mskp/filename.mskp.pdf
halcanaryd39d6f32016-09-12 11:56:28 -0700139
140* * *
141
halcanary75a171c2016-09-26 07:27:04 -0700142<span id="hw-acceleration">How to add hardware acceleration in Skia</span>
143--------------------------------------------------------------------------
jcgregorio942262f2015-01-05 11:17:27 -0800144
halcanaryb5002392015-11-16 07:37:23 -0800145There are two ways Skia takes advantage of specific hardware.
jcgregorio942262f2015-01-05 11:17:27 -0800146
halcanaryb5002392015-11-16 07:37:23 -08001471. Subclass SkCanvas
jcgregorio942262f2015-01-05 11:17:27 -0800148
halcanaryb5002392015-11-16 07:37:23 -0800149 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/
jcgregorio942262f2015-01-05 11:17:27 -0800152
halcanaryb5002392015-11-16 07:37:23 -08001532. Custom bottleneck routines
jcgregorio942262f2015-01-05 11:17:27 -0800154
halcanaryb5002392015-11-16 07:37:23 -0800155 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/
jcgregorio942262f2015-01-05 11:17:27 -0800159
halcanary8d3f7bd2015-07-09 06:58:06 -0700160* * *
161
halcanary75a171c2016-09-26 07:27:04 -0700162<span id="font-hinting">Does Skia support Font hinting?</span>
163--------------------------------------------------------------
jcgregorio942262f2015-01-05 11:17:27 -0800164
165Skia has a built-in font cache, but it does not know how to actual render font
halcanarya58d6762015-12-14 09:50:15 -0800166files like TrueType into its cache. For that it relies on the platform to
167supply an instance of SkScalerContext. This is Skia's abstract interface for
jcgregorio942262f2015-01-05 11:17:27 -0800168communicating with a font scaler engine. In src/ports you can see support
halcanarya58d6762015-12-14 09:50:15 -0800169files for FreeType, Mac OS X, and Windows GDI font engines. Other font
jcgregorio942262f2015-01-05 11:17:27 -0800170engines can easily be supported in a like manner.
171
172
halcanarya58d6762015-12-14 09:50:15 -0800173* * *
174
halcanary75a171c2016-09-26 07:27:04 -0700175<span id="kerning">Does Skia shape text (kerning)?</span>
176---------------------------------------------------------
halcanarya58d6762015-12-14 09:50:15 -0800177
178No. Skia provides interfaces to draw glyphs, but does not implement a
halcanary7d1c3e62015-12-14 10:03:31 -0800179text shaper. Skia's client's often use
180[HarfBuzz](http://www.freedesktop.org/wiki/Software/HarfBuzz/) to
181generate the glyphs and their positions, including kerning.
halcanarya58d6762015-12-14 09:50:15 -0800182
halcanary5441e9f2016-05-03 10:18:30 -0700183[Here is an example of how to use Skia and HarfBuzz
184together](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
187a sequence of glyphs and positions, and the SkTypeface can then be
188used to draw those glyphs.
189
halcanaryff659642016-04-26 04:49:45 -0700190* * *
191
halcanary75a171c2016-09-26 07:27:04 -0700192<span id="text-shadow">How do I add drop shadow on text?</span>
193---------------------------------------------------------------
halcanaryff659642016-04-26 04:49:45 -0700194
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
halcanary5441e9f2016-05-03 10:18:30 -0700221* * *
222
halcanarya58d6762015-12-14 09:50:15 -0800223<div style="margin-bottom:99%"></div>