Skia Documentation
repository·main·Indexed 27 days ago
https://github.com/google/skiaSkia is a 2D graphics library. This documentation covers build system configurations for Bazel, GN, and CMake, including RBE Docker image management and external dependency handling. It also provides guidance on CanvasKit (a WebAssembly version of Skia's Canvas API), fuzzing executables for bug identification via OSS-Fuzz, and compiling the minimal_ios_mtl_skia_app for iOS using the Metal backend.
What's inside Skia
- CanvasKit is a WebAssembly build of Skia that allows you to deploy Skia's graphics APIs directly to the web. It provides a playground for testing Canvas and SVG platform APIs and serves as a deployment mechanism for web applications requiring advanced graphics features, such as Skia's Lottie animation support via Skottie.
Overview of Skia graphics capabilities
mainSkia is a graphics library used for drawing text, geometries, and images. It supports advanced graphical features including:
- Geometries & Transformations: 3x3 matrices with perspective.
- Rendering Effects: Antialiasing, transparency, and filters.
- Advanced Shaders: Shaders, xfermodes, maskfilters, and patheffects.
- Text: Subpixel text rendering.
Skia supports multiple device backends for output:
- Raster
- OpenGL
- XPS
- SVG
- Picture (used for recording drawing commands and playing them back into another Canvas)
Overview of Skia Recipe Modules
mainThe
infra/bots/recipe_modulesdirectory contains Skia-specific modules used by recipes (located ininfra/bots/recipes). These modules provide shared utilities for building, running, and managing Skia-related tasks within the infrastructure.Available modules include:
builder_name_schema: Derives expected behavior from task (builder) names.core: A starting point for most recipes; handles setup and sync steps.ct: Shared Cluster Telemetry utilities.flavor: Handles platform-specific details by allowing callers to specify high-level commands.infra: Shared infrastructure-related utilities.run: Utilities for executing commands.swarming: Utilities for managing Swarming tasks.vars: Common global variables used across Skia recipes and modules.
Skia Infrastructure Recipes Overview
mainSkia uses a recipe framework to execute work within Swarming tasks. The core components are:
recipes.py: The script used for running and testing recipes.recipes: The entry points for specific task types (e.g., compiling or running tests).recipe_modules: Shared modules utilized by recipes..recipe_deps: Managed dependencies from other repositories, automatically synced byrecipes.py.
Understand Skia Coordinate Spaces
mainSkia operates using two primary coordinate systems:
- Device Coordinates: Defined by the surface (or device) being rendered to. The origin
(0, 0)is at the upper-left corner, and coordinates extend to(w, h)at the bottom-right. These are effectively measured in pixels. - Local Coordinates: The space used to supply geometry and shaders to the
SkCanvas. By default, local and device coordinates are identical, meaning geometry is typically specified in pixel units.
- Device Coordinates: Defined by the surface (or device) being rendered to. The origin
Understand Skia Color Management logic
mainSkia performs color management by transforming colors from a source color space to a destination color space via a common connection space: XYZ D50.
The process consists of six logical steps:
- Unpremultiply: If the source color is premultiplied (alpha is divided out).
- Linearize: Convert source color using the source color space's transfer function.
- Convert to XYZ D50: Multiply by a 3x3 matrix.
- Convert to Destination Gamut: Multiply by a 3x3 matrix.
- Encode: Use the inverse of the destination color space's transfer function.
- Premultiply: If the destination requires premultiplied alpha.
Note: Steps 2 and 5 (transfer function applications) are the most computationally expensive.
SkSL Language Overview and Differences from GLSL
mainSkSL (Skia Shading Language) is a standardized variant of GLSL used as Skia's internal shading language. The SkSL compiler converts code into GLSL, GLSL ES, SPIR-V, or MSL.
Key syntax and type differences from GLSL:
- Precision: Precision modifiers are not used.
float,int, anduintare always high precision.half,short, andushortare medium precision. - Vector Types: Named
<base type><columns>(e.g.,float2instead ofvec2,bool4instead ofbvec4). - Matrix Types: Named
<base type><columns>x<rows>(e.g.,float2x3instead ofmat2x3). - Capabilities: Access GLSL caps via
sk_Caps.<name>(e.g.,sk_Caps.integerSupport). These can be used inifstatements for compile-time branch elimination. - Built-in Variables:
- Use
sk_FragColorfor output color (do not declare it). - Use
sk_Position(device coordinates) instead ofgl_Position. - Use
sk_PointSizeinstead ofgl_PointSize. - Use
sk_VertexIDinstead ofgl_VertexID. - Use
sk_InstanceIDinstead ofgl_InstanceID. - Use
sk_FragCoordfor fragment coordinates (relative to upper left). - Use
sk_Clockwiseinstead ofgl_FrontFacing.
- Use
- Numbers:
- No
#versionstatement required. - No need to append
.0to make a number a float (e.g.,float2(x, y) * 4is valid). - Type suffixes like
1.0for0xFFuare unsupported.
- No
- Functions & Textures:
- All texture functions are named
sample(e.g.,sample(sampler2D, float3)). - Functions support the
inlinemodifier to force inlining.
- All texture functions are named
- Restrictions: Creating a smaller vector from a larger vector (e.g.,
float2(float3(1))) is disallowed; use swizzles instead.
- Precision: Precision modifiers are not used.
Understand the architecture of GPU Gradients
mainGPU gradients in Skia are composed of three distinct architectural components:
- Color Interpolator (
GrXxxxxGradientColorizer): A one-dimensional component that returns a color for an interpolant valuetin the range[0.0, 1.0]. It handles color stops and how to wrap, tile, or clamp out-of-bound inputs. - Layout (
GrYyyyyGradientLayout): Converts 2D geometry/position into the 1D domain used by the colorizer (e.g., linear or radial). This is the component to implement when designing new gradient shapes. - Top-level Effect: Composes the layout and colorizer. It manages clamping behavior and tile modes.
GrClampedGradientEffect: Handlesclampedanddecaltile modes. Requires border colors to be specified externally.GrTiledGradientEffect: Implementsrepeatandmirrortile modes.
GrGradientShaderprovides static factory functions to createGrFragmentProcessorgraphs that reproduce a specificSkGradientShader.- Color Interpolator (
Understand Skia CPU Backend Architecture
mainThe Skia CPU backend is responsible for rendering graphics on the CPU without utilizing a GPU. The architecture follows a data flow from high-level API calls down to pixel memory writing. The core components include:
- The API Layer: The entry point for drawing commands.
- The Device Layer (
SkBitmapDevice): Manages the target bitmap. - The Orchestrator (
SkDraw): Coordinates the drawing process. - The Rasterizer (
SkScan): Converts geometry into coverage information. - The Pixel Writer (
SkRasterPipelineBlitter): Writes final colors to the destination. - The Workhorse (
SkRasterPipeline): Manages the rendering pipeline. - The Pixel Memory View (
SkPixmap,SkBitmap, andSkPixelRef): Provides access to the underlying pixel data.
Core concepts of the Skia drawing API
mainSkia's drawing operations are organized around the
SkCanvasobject, which acts as the host for drawing calls likedrawRect,drawPath, anddrawText.Every drawing operation typically requires two components:
- The Primitive: The geometric shape or object being drawn (e.g.,
SkRect,SkPath,SkImage). - The Style: An
SkPaintobject that defines color, stroke, font, and effects.
SkCanvasmaintains state related to the drawing destination, such as the current transformation matrix (translation, rotation, skewing, perspective) and clipping regions. In contrast,SkPaintholds the stylistic attributes, such as color and blending modes.canvas->drawRect(rect, paint);- The Primitive: The geometric shape or object being drawn (e.g.,
Understand the Text Shaping concept in Skia
mainIn Skia, text rendering is split into two distinct phases: Shaping and Drawing.
- Shaping: The complex and computationally expensive process of converting a sequence of characters (letters) into a specific set of Glyphs (individual shapes) with precise positions and orders. This handles complexities like ligatures (e.g., 'ffi'), combining marks (e.g., umlauts), and font fallback for different languages.
- Drawing: The act of rendering the resulting positioned glyphs using standard graphics primitives (rects, paths, images) and constructs (transforms, clipping, colors, gradients).
Skia exposes these capabilities via the SkShaper API to allow developers to perform text processing independently of the final rendering context.
Use Unicode comparison utilities
mainThetools/unicode_comparisondirectory provides utilities for comparing Unicode data between SkUnicode (C++) and Go. It consists of a C++ bridge and a set of Go utilities for data downloading, preprocessing, and table generation.