xref: /aosp_15_r20/external/skia/site/docs/user/coordinates.md (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker---
2*c8dee2aaSAndroid Build Coastguard Workertitle: 'Skia Coordinate Spaces'
3*c8dee2aaSAndroid Build Coastguard WorkerlinkTitle: 'Coordinates'
4*c8dee2aaSAndroid Build Coastguard Worker---
5*c8dee2aaSAndroid Build Coastguard Worker
6*c8dee2aaSAndroid Build Coastguard Worker## Overview
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard WorkerSkia generally refers to two different coordinate spaces: **device** and
9*c8dee2aaSAndroid Build Coastguard Worker**local**. Device coordinates are defined by the surface (or other device) that
10*c8dee2aaSAndroid Build Coastguard Workeryou're rendering to. They range from `(0, 0)` in the upper-left corner of the
11*c8dee2aaSAndroid Build Coastguard Workersurface, to `(w, h)` in the bottom-right corner - they are effectively measured
12*c8dee2aaSAndroid Build Coastguard Workerin pixels.
13*c8dee2aaSAndroid Build Coastguard Worker
14*c8dee2aaSAndroid Build Coastguard Worker---
15*c8dee2aaSAndroid Build Coastguard Worker
16*c8dee2aaSAndroid Build Coastguard Worker## Local Coordinates
17*c8dee2aaSAndroid Build Coastguard Worker
18*c8dee2aaSAndroid Build Coastguard WorkerThe local coordinate space is how all geometry and shaders are supplied to the
19*c8dee2aaSAndroid Build Coastguard Worker`SkCanvas`. By default, the local and device coordinate systems are the same.
20*c8dee2aaSAndroid Build Coastguard WorkerThis means that geometry is typically specified in pixel units. Here, we
21*c8dee2aaSAndroid Build Coastguard Workerposition a rectangle at `(100, 50)`, and specify that it is `50` units wide and
22*c8dee2aaSAndroid Build Coastguard Workertall:
23*c8dee2aaSAndroid Build Coastguard Worker
24*c8dee2aaSAndroid Build Coastguard Worker<fiddle-embed-sk name='96f782b723c5240aab440242f4c7cbfb'></fiddle-embed-sk>
25*c8dee2aaSAndroid Build Coastguard Worker
26*c8dee2aaSAndroid Build Coastguard WorkerLocal coordinates are also used to define and evaluate any `SkShader` on the
27*c8dee2aaSAndroid Build Coastguard Workerpaint. Here, we define a linear gradient shader that goes from green (when
28*c8dee2aaSAndroid Build Coastguard Worker`x == 0`) to blue (when `x == 50`):
29*c8dee2aaSAndroid Build Coastguard Worker
30*c8dee2aaSAndroid Build Coastguard Worker<fiddle-embed-sk name='97cf81a465fdeff01d2298e07a0802a3'></fiddle-embed-sk>
31*c8dee2aaSAndroid Build Coastguard Worker
32*c8dee2aaSAndroid Build Coastguard Worker---
33*c8dee2aaSAndroid Build Coastguard Worker
34*c8dee2aaSAndroid Build Coastguard Worker## Shaders Do Not Move With Geometry
35*c8dee2aaSAndroid Build Coastguard Worker
36*c8dee2aaSAndroid Build Coastguard WorkerNow, let's try to draw the gradient-filled square at `(100, 50)`:
37*c8dee2aaSAndroid Build Coastguard Worker
38*c8dee2aaSAndroid Build Coastguard Worker<fiddle-embed-sk name='3adc73d23d57084f954f52c6b14c8772'></fiddle-embed-sk>
39*c8dee2aaSAndroid Build Coastguard Worker
40*c8dee2aaSAndroid Build Coastguard WorkerWhat happened? Remember, the local coordinate space has not changed. The origin
41*c8dee2aaSAndroid Build Coastguard Workeris still in the upper-left corner of the surface. We have specified that the
42*c8dee2aaSAndroid Build Coastguard Workergeometry should be positioned at `(100, 50)`, but the `SkShader` is still
43*c8dee2aaSAndroid Build Coastguard Workerproducing a gradient as `x` goes from `0` to `50`. We have slid the rectangle
44*c8dee2aaSAndroid Build Coastguard Workeracross the gradient defined by the `SkShader`. Shaders do not move with the
45*c8dee2aaSAndroid Build Coastguard Workergeometry.
46*c8dee2aaSAndroid Build Coastguard Worker
47*c8dee2aaSAndroid Build Coastguard Worker---
48*c8dee2aaSAndroid Build Coastguard Worker
49*c8dee2aaSAndroid Build Coastguard Worker## Transforming Local Coordinate Space
50*c8dee2aaSAndroid Build Coastguard Worker
51*c8dee2aaSAndroid Build Coastguard WorkerTo get the desired effect, we could create a new gradient shader, with the
52*c8dee2aaSAndroid Build Coastguard Workerpositions moved to `100` and `150`. That makes our shaders difficult to reuse.
53*c8dee2aaSAndroid Build Coastguard WorkerInstead, we can use methods on `SkCanvas` to **change the local coordinate
54*c8dee2aaSAndroid Build Coastguard Workerspace**. This causes all local coordinates (geometry and shaders) to be
55*c8dee2aaSAndroid Build Coastguard Workerevaluated in the new space defined by the canvas' transformation matrix:
56*c8dee2aaSAndroid Build Coastguard Worker
57*c8dee2aaSAndroid Build Coastguard Worker<fiddle-embed-sk name='ce89b326b2bbe41587eec738706bf155'></fiddle-embed-sk>
58*c8dee2aaSAndroid Build Coastguard Worker
59*c8dee2aaSAndroid Build Coastguard Worker---
60*c8dee2aaSAndroid Build Coastguard Worker
61*c8dee2aaSAndroid Build Coastguard Worker## <span>Transforming Shader Coordinate Space</span>
62*c8dee2aaSAndroid Build Coastguard Worker
63*c8dee2aaSAndroid Build Coastguard WorkerFinally, it is possible to transform the coordinate space of the `SkShader`,
64*c8dee2aaSAndroid Build Coastguard Workerrelative to the canvas local coordinate space. To do this, you supply a
65*c8dee2aaSAndroid Build Coastguard Worker`localMatrix` parameter when creating the `SkShader`. In this situation, the
66*c8dee2aaSAndroid Build Coastguard Workergeometry is transformed by the `SkCanvas` matrix. The `SkShader` is transformed
67*c8dee2aaSAndroid Build Coastguard Workerby the `SkCanvas` matrix **and** the `localMatrix` for that shader. The other
68*c8dee2aaSAndroid Build Coastguard Workerway to think about this: The `localMatrix` defines a transform that maps the
69*c8dee2aaSAndroid Build Coastguard Workershader's coordinates to the coordinate space of the geometry.
70*c8dee2aaSAndroid Build Coastguard Worker
71*c8dee2aaSAndroid Build Coastguard WorkerTo help illustrate the difference, here's our gradient-filled box. It's first
72*c8dee2aaSAndroid Build Coastguard Workerbeen translated `50` units over and down. Then, we apply a `45` degree rotation
73*c8dee2aaSAndroid Build Coastguard Worker(pivoting on the center of the box) to the canvas. This rotates the geometry of
74*c8dee2aaSAndroid Build Coastguard Workerthe box, and the gradient inside it:
75*c8dee2aaSAndroid Build Coastguard Worker
76*c8dee2aaSAndroid Build Coastguard Worker<fiddle-embed-sk name='d4b52d94342f1b55900d489c7ba8fd21'></fiddle-embed-sk>
77*c8dee2aaSAndroid Build Coastguard Worker
78*c8dee2aaSAndroid Build Coastguard WorkerCompare that to the second example. We still translate `50` units over and down.
79*c8dee2aaSAndroid Build Coastguard WorkerHere, though, we apply the `45` degree rotation _only to the shader_, by
80*c8dee2aaSAndroid Build Coastguard Workerspecifying it as a `localMatrix` to the `SkGradientShader::MakeLinear` function.
81*c8dee2aaSAndroid Build Coastguard WorkerNow, the box remains un-rotated, but the gradient rotates inside the box:
82*c8dee2aaSAndroid Build Coastguard Worker
83*c8dee2aaSAndroid Build Coastguard Worker<fiddle-embed-sk name='886fa46943b67e0d6aa78486dcfbcc2c'></fiddle-embed-sk>
84