Mostly just formatting fixes with a few grammatical changes. Two real notable changes: - Removed references to SkGLCanvas from Tips & FAQ and replaced with references to `SkDevice` and `SkSurface`. - Deleted deprecated "Quick Start Guides" folder Docs-Preview: https://skia.org/?cl=92361 Bug: skia: Change-Id: Ief790b1c2bae8fe0e39aa8d66c79f80560d18c9e Reviewed-on: https://skia-review.googlesource.com/92361 Reviewed-by: Heather Miller <hcm@google.com> Reviewed-by: Joe Gregorio <jcgregorio@google.com> Commit-Queue: Joe Gregorio <jcgregorio@google.com>
5.5 KiB
Tips & FAQ
- Bitmap Subsetting
- Capture a
.skp
file on a web page in Chromium - Capture a
.mskp
file on a web page in Chromium - How to add hardware acceleration in Skia
- Does Skia support Font hinting?
- Does Skia shape text (kerning)?
- How do I add drop shadow on text?
Bitmap Subsetting
Taking a subset of a bitmap is effectively free - no pixels are copied or memory is allocated. This allows Skia to offer an API that typically operates on entire bitmaps; clients who want to operate on a subset of a bitmap can use the following pattern, here being used to magnify a portion of an image with drawBitmapNine():
SkBitmap subset;
bitmap.extractSubset(&subset, rect);
canvas->drawBitmapNine(subset, ...);
Capture a .skp
file on a web page in Chromium
- Launch Chrome or Chromium with
--no-sandbox --enable-gpu-benchmarking
- Open the JS console (ctrl-shift-J)
- Execute:
chrome.gpuBenchmarking.printToSkPicture('/tmp')
This returns "undefined" on success.
Open the resulting file in the Skia Debugger, rasterize it with dm
,
or use Skia's SampleApp
to view it:
out/Release/dm --src skp --skps /tmp/layer_0.skp -w /tmp \
--config 8888 gpu pdf --verbose
ls -l /tmp/*/skp/layer_0.skp.*
out/Release/SampleApp --picture /tmp/layer_0.skp
Capture a .mskp
file on a web page in Chromium
Multipage Skia Picture files capture the commands sent to produce PDFs and printed documents.
- Launch Chrome or Chromium with
--no-sandbox --enable-gpu-benchmarking
- Open the JS console (ctrl-shift-J)
- Execute:
chrome.gpuBenchmarking.printPagesToSkPictures('/tmp/filename.mskp')
This returns "undefined" on success.
Open the resulting file in the Skia Debugger or
process it with dm
.
experimental/tools/mskp_parser.py /tmp/filename.mskp /tmp/filename.mskp.skp
ls -l /tmp/filename.mskp.skp
# open filename.mskp.skp in the debugger.
out/Release/dm --src mskp --mskps /tmp/filename.mskp -w /tmp \
--config pdf --verbose
ls -l /tmp/pdf/mskp/filename.mskp.pdf
How to add hardware acceleration in Skia
There are two ways Skia takes advantage of specific hardware.
-
Custom bottleneck routines
There are sets of bottleneck routines inside the blits of Skia that can be replace on a platform in order to take advantage of specific CPU features. One such example is the NEON SIMD instructions on ARM v7 devices. See src/opts/
Does Skia support Font hinting?
Skia has a built-in font cache, but it does not know how to actually render font
files like TrueType into its cache. For that it relies on the platform to
supply an instance of SkScalerContext
. This is Skia's abstract interface for
communicating with a font scaler engine. In src/ports you can see support
files for FreeType, macOS, and Windows GDI font engines. Other font
engines can easily be supported in a like manner.
Does Skia shape text (kerning)?
No. Skia provides interfaces to draw glyphs, but does not implement a text shaper. Skia's client's often use HarfBuzz to generate the glyphs and their positions, including kerning.
Here is an example of how to use Skia and HarfBuzz
together. In the example, a
SkTypeface
and a hb_face_t
are created using the same mmap()
ed
.ttf
font file. The HarfBuzz face is used to shape unicode text into
a sequence of glyphs and positions, and the SkTypeface
can then be
used to draw those glyphs.
How do I add drop shadow on text?
void draw(SkCanvas* canvas) {
const char text[] = "Skia";
const SkScalar radius = 2.0f;
const SkScalar xDrop = 2.0f;
const SkScalar yDrop = 2.0f;
const SkScalar x = 8.0f;
const SkScalar y = 52.0f;
const SkScalar textSize = 48.0f;
const uint8_t blurAlpha = 127;
canvas->drawColor(SK_ColorWHITE);
SkPaint paint;
paint.setAntiAlias(true);
paint.setTextSize(textSize);
SkPaint blur(paint);
blur.setAlpha(blurAlpha);
blur.setMaskFilter(SkBlurMaskFilter::Make(
kNormal_SkBlurStyle,
SkBlurMaskFilter::ConvertRadiusToSigma(radius), 0));
canvas->drawText(text, strlen(text), x + xDrop, y + yDrop, blur);
canvas->drawText(text, strlen(text), x, y, paint);
}