xref: /third_party/skia/site/docs/user/tips.md (revision cb93a386)
1cb93a386Sopenharmony_ci---
2cb93a386Sopenharmony_cititle: 'Tips & FAQ'
3cb93a386Sopenharmony_cilinkTitle: 'Tips & FAQ'
4cb93a386Sopenharmony_ci---
5cb93a386Sopenharmony_ci
6cb93a386Sopenharmony_ci## Capture a `.skp` file on a web page in Chromium
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ciUse the script `experimental/tools/web_to_skp` , _or_ do the following:
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci1.  Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking`
11cb93a386Sopenharmony_ci2.  Open the JS console (Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (MacOS))
12cb93a386Sopenharmony_ci3.  Execute: `chrome.gpuBenchmarking.printToSkPicture('/tmp')` This returns
13cb93a386Sopenharmony_ci    "undefined" on success.
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ciOpen the resulting file in the [Skia Debugger](/docs/dev/tools/debugger),
16cb93a386Sopenharmony_cirasterize it with `dm`, or use Skia's `viewer` to view it:
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci<!--?prettify lang=sh?-->
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ci    out/Release/dm --src skp --skps /tmp/layer_0.skp -w /tmp \
21cb93a386Sopenharmony_ci        --config 8888 gpu pdf --verbose
22cb93a386Sopenharmony_ci    ls -l /tmp/*/skp/layer_0.skp.*
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    out/Release/viewer --skps /tmp --slide layer_0.skp
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci---
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci## Capture a `.mskp` file on a web page in Chromium
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ciMultipage Skia Picture files capture the commands sent to produce PDFs and
31cb93a386Sopenharmony_ciprinted documents.
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ciUse the script `experimental/tools/web_to_mskp` , _or_ do the following:
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci1.  Launch Chrome or Chromium with `--no-sandbox --enable-gpu-benchmarking`
36cb93a386Sopenharmony_ci2.  Open the JS console (Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (MacOS))
37cb93a386Sopenharmony_ci3.  Execute:
38cb93a386Sopenharmony_ci    `chrome.gpuBenchmarking.printPagesToSkPictures('/tmp/filename.mskp')` This
39cb93a386Sopenharmony_ci    returns "undefined" on success.
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ciOpen the resulting file in the [Skia Debugger](/docs/dev/tools/debugger) or
42cb93a386Sopenharmony_ciprocess it with `dm`.
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci<!--?prettify lang=sh?-->
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    experimental/tools/mskp_parser.py /tmp/filename.mskp /tmp/filename.mskp.skp
47cb93a386Sopenharmony_ci    ls -l /tmp/filename.mskp.skp
48cb93a386Sopenharmony_ci    # open filename.mskp.skp in the debugger.
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci    out/Release/dm --src mskp --mskps /tmp/filename.mskp -w /tmp \
51cb93a386Sopenharmony_ci        --config pdf --verbose
52cb93a386Sopenharmony_ci    ls -l /tmp/pdf/mskp/filename.mskp.pdf
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci---
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci## How to add hardware acceleration in Skia
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ciThere are two ways Skia takes advantage of specific hardware.
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci1.  Custom bottleneck routines
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    There are sets of bottleneck routines inside the blits of Skia that can be
63cb93a386Sopenharmony_ci    replace on a platform in order to take advantage of specific CPU features.
64cb93a386Sopenharmony_ci    One such example is the NEON SIMD instructions on ARM v7 devices. See
65cb93a386Sopenharmony_ci    [src/opts/](https://skia.googlesource.com/skia/+/main/src/opts/)
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci---
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci## Does Skia support Font hinting?
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ciSkia has a built-in font cache, but it does not know how to actually render font
72cb93a386Sopenharmony_cifiles like TrueType into its cache. For that it relies on the platform to supply
73cb93a386Sopenharmony_cian instance of `SkScalerContext`. This is Skia's abstract interface for
74cb93a386Sopenharmony_cicommunicating with a font scaler engine. In src/ports you can see support files
75cb93a386Sopenharmony_cifor FreeType, macOS, and Windows GDI font engines. Other font engines can easily
76cb93a386Sopenharmony_cibe supported in a like manner.
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci---
79cb93a386Sopenharmony_ci
80cb93a386Sopenharmony_ci## Does Skia shape text (kerning)?
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ciShaping is the process that translates a span of Unicode text into a span of
83cb93a386Sopenharmony_cipositioned glyphs with the appropriate typefaces.
84cb93a386Sopenharmony_ci
85cb93a386Sopenharmony_ciSkia does not shape text. Skia provides interfaces to draw glyphs, but does not
86cb93a386Sopenharmony_ciimplement a text shaper. Skia's client's often use
87cb93a386Sopenharmony_ci[HarfBuzz](http://www.freedesktop.org/wiki/Software/HarfBuzz/) to generate the
88cb93a386Sopenharmony_ciglyphs and their positions, including kerning.
89cb93a386Sopenharmony_ci
90cb93a386Sopenharmony_ci[Here is an example of how to use Skia and HarfBuzz together](https://github.com/aam/skiaex).
91cb93a386Sopenharmony_ciIn the example, a `SkTypeface` and a `hb_face_t` are created using the same
92cb93a386Sopenharmony_ci`mmap()`ed `.ttf` font file. The HarfBuzz face is used to shape unicode text
93cb93a386Sopenharmony_ciinto a sequence of glyphs and positions, and the `SkTypeface` can then be used
94cb93a386Sopenharmony_cito draw those glyphs.
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ci---
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_ci## How do I add drop shadow on text?
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ci<!--?prettify lang=cc?-->
101cb93a386Sopenharmony_ci
102cb93a386Sopenharmony_ci    void draw(SkCanvas* canvas) {
103cb93a386Sopenharmony_ci        const SkScalar sigma = 1.65f;
104cb93a386Sopenharmony_ci        const SkScalar xDrop = 2.0f;
105cb93a386Sopenharmony_ci        const SkScalar yDrop = 2.0f;
106cb93a386Sopenharmony_ci        const SkScalar x = 8.0f;
107cb93a386Sopenharmony_ci        const SkScalar y = 52.0f;
108cb93a386Sopenharmony_ci        const SkScalar textSize = 48.0f;
109cb93a386Sopenharmony_ci        const uint8_t blurAlpha = 127;
110cb93a386Sopenharmony_ci        auto blob = SkTextBlob::MakeFromString("Skia", SkFont(nullptr, textSize));
111cb93a386Sopenharmony_ci        SkPaint paint;
112cb93a386Sopenharmony_ci        paint.setAntiAlias(true);
113cb93a386Sopenharmony_ci        SkPaint blur(paint);
114cb93a386Sopenharmony_ci        blur.setAlpha(blurAlpha);
115cb93a386Sopenharmony_ci        blur.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, 0));
116cb93a386Sopenharmony_ci        canvas->drawColor(SK_ColorWHITE);
117cb93a386Sopenharmony_ci        canvas->drawTextBlob(blob.get(), x + xDrop, y + yDrop, blur);
118cb93a386Sopenharmony_ci        canvas->drawTextBlob(blob.get(), x,         y,         paint);
119cb93a386Sopenharmony_ci    }
120cb93a386Sopenharmony_ci
121cb93a386Sopenharmony_ci<a href='https://fiddle.skia.org/c/@text_shadow'><img src='https://fiddle.skia.org/i/@text_shadow_raster.png'></a>
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci---
124cb93a386Sopenharmony_ci
125cb93a386Sopenharmony_ci<div style="margin-bottom:99%"></div>
126