Skia Documentation

repository·main·Indexed 27 days ago

https://github.com/google/skia

Skia 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.

Tokens
75.3K
Snippets
163
Records
483
Agent score
94%

What's inside Skia

  1. Overview of CanvasKit - Skia + WebAssembly

    main
    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.
  2. Overview of Skia graphics capabilities

    main

    Skia 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
    • PDF
    • XPS
    • SVG
    • Picture (used for recording drawing commands and playing them back into another Canvas)
  3. Overview of Skia Recipe Modules

    main

    The infra/bots/recipe_modules directory contains Skia-specific modules used by recipes (located in infra/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.
  4. Skia Infrastructure Recipes Overview

    main

    Skia 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 by recipes.py.
  5. Understand Skia Coordinate Spaces

    main

    Skia operates using two primary coordinate systems:

    1. 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.
    2. 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.
  6. Understand Skia Color Management logic

    main

    Skia 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:

    1. Unpremultiply: If the source color is premultiplied (alpha is divided out).
    2. Linearize: Convert source color using the source color space's transfer function.
    3. Convert to XYZ D50: Multiply by a 3x3 matrix.
    4. Convert to Destination Gamut: Multiply by a 3x3 matrix.
    5. Encode: Use the inverse of the destination color space's transfer function.
    6. Premultiply: If the destination requires premultiplied alpha.

    Note: Steps 2 and 5 (transfer function applications) are the most computationally expensive.

  7. SkSL Language Overview and Differences from GLSL

    main

    SkSL (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, and uint are always high precision. half, short, and ushort are medium precision.
    • Vector Types: Named <base type><columns> (e.g., float2 instead of vec2, bool4 instead of bvec4).
    • Matrix Types: Named <base type><columns>x<rows> (e.g., float2x3 instead of mat2x3).
    • Capabilities: Access GLSL caps via sk_Caps.<name> (e.g., sk_Caps.integerSupport). These can be used in if statements for compile-time branch elimination.
    • Built-in Variables:
      • Use sk_FragColor for output color (do not declare it).
      • Use sk_Position (device coordinates) instead of gl_Position.
      • Use sk_PointSize instead of gl_PointSize.
      • Use sk_VertexID instead of gl_VertexID.
      • Use sk_InstanceID instead of gl_InstanceID.
      • Use sk_FragCoord for fragment coordinates (relative to upper left).
      • Use sk_Clockwise instead of gl_FrontFacing.
    • Numbers:
      • No #version statement required.
      • No need to append .0 to make a number a float (e.g., float2(x, y) * 4 is valid).
      • Type suffixes like 1.0f or 0xFFu are unsupported.
    • Functions & Textures:
      • All texture functions are named sample (e.g., sample(sampler2D, float3)).
      • Functions support the inline modifier to force inlining.
    • Restrictions: Creating a smaller vector from a larger vector (e.g., float2(float3(1))) is disallowed; use swizzles instead.
  8. Understand the architecture of GPU Gradients

    main

    GPU gradients in Skia are composed of three distinct architectural components:

    1. Color Interpolator (GrXxxxxGradientColorizer): A one-dimensional component that returns a color for an interpolant value t in the range [0.0, 1.0]. It handles color stops and how to wrap, tile, or clamp out-of-bound inputs.
    2. 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.
    3. Top-level Effect: Composes the layout and colorizer. It manages clamping behavior and tile modes.
      • GrClampedGradientEffect: Handles clamped and decal tile modes. Requires border colors to be specified externally.
      • GrTiledGradientEffect: Implements repeat and mirror tile modes.

    GrGradientShader provides static factory functions to create GrFragmentProcessor graphs that reproduce a specific SkGradientShader.

  9. Understand Skia CPU Backend Architecture

    main

    The 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:

    1. The API Layer: The entry point for drawing commands.
    2. The Device Layer (SkBitmapDevice): Manages the target bitmap.
    3. The Orchestrator (SkDraw): Coordinates the drawing process.
    4. The Rasterizer (SkScan): Converts geometry into coverage information.
    5. The Pixel Writer (SkRasterPipelineBlitter): Writes final colors to the destination.
    6. The Workhorse (SkRasterPipeline): Manages the rendering pipeline.
    7. The Pixel Memory View (SkPixmap, SkBitmap, and SkPixelRef): Provides access to the underlying pixel data.
  10. Core concepts of the Skia drawing API

    main

    Skia's drawing operations are organized around the SkCanvas object, which acts as the host for drawing calls like drawRect, drawPath, and drawText.

    Every drawing operation typically requires two components:

    1. The Primitive: The geometric shape or object being drawn (e.g., SkRect, SkPath, SkImage).
    2. The Style: An SkPaint object that defines color, stroke, font, and effects.

    SkCanvas maintains state related to the drawing destination, such as the current transformation matrix (translation, rotation, skewing, perspective) and clipping regions. In contrast, SkPaint holds the stylistic attributes, such as color and blending modes.

    canvas->drawRect(rect, paint);
  11. Understand the Text Shaping concept in Skia

    main

    In Skia, text rendering is split into two distinct phases: Shaping and Drawing.

    1. 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.
    2. 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.