• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "src/gpu/AtlasTypes.h"
8 #include "src/text/gpu/VertexFiller.h"
9 #include "src/gpu/graphite/Device.h"
10 #include "src/base/SkZip.h"
11 #include "src/gpu/graphite/DrawWriter.h"
12 #include "src/gpu/graphite/Renderer.h"
13 #include "src/gpu/graphite/RendererProvider.h"
14 #include "src/text/gpu/Glyph.h"
15 #include "src/text/gpu/SubRunAllocator.h"
16 #include "src/text/gpu/SubRunContainer.h"
17 
18 namespace sktext::gpu {
19 
20 struct AtlasPt {
21     uint16_t u;
22     uint16_t v;
23 };
24 
fillInstanceData(skgpu::graphite::DrawWriter * dw,int offset,int count,unsigned short flags,skvx::ushort2 ssboIndex,SkSpan<const Glyph * > glyphs,SkScalar depth) const25 void VertexFiller::fillInstanceData(skgpu::graphite::DrawWriter* dw,
26                                     int offset, int count,
27                                     unsigned short flags,
28                                     skvx::ushort2 ssboIndex,
29                                     SkSpan<const Glyph*> glyphs,
30                                     SkScalar depth) const {
31     auto quadData = [&]() {
32         return SkMakeZip(glyphs.subspan(offset, count),
33                          fLeftTop.subspan(offset, count));
34     };
35 
36     skgpu::graphite::DrawWriter::Instances instances{*dw, {}, {}, 4};
37     instances.reserve(count);
38     // Need to send width, height, uvPos, xyPos, and strikeToSourceScale
39     // pre-transform coords = (s*w*b_x + t_x, s*h*b_y + t_y)
40     // where (b_x, b_y) are the vertexID coords
41     for (auto [glyph, leftTop]: quadData()) {
42         auto[al, at, ar, ab] = glyph->fAtlasLocator.getUVs();
43         instances.append(1) << AtlasPt{uint16_t(ar-al), uint16_t(ab-at)}
44                             << AtlasPt{uint16_t(al & 0x1fff), at}
45                             << leftTop << /*index=*/uint16_t(al >> 13) << flags
46                             << 1.0f
47                             << depth << ssboIndex;
48     }
49 }
50 
51 using Rect = skgpu::graphite::Rect;
52 using Transform = skgpu::graphite::Transform;
53 
boundsAndDeviceMatrix(const Transform & localToDevice,SkPoint drawOrigin) const54 std::tuple<Rect, Transform> VertexFiller::boundsAndDeviceMatrix(const Transform& localToDevice,
55                                                                 SkPoint drawOrigin) const {
56     // The baked-in matrix differs from the current localToDevice by a translation if the
57     // upper 2x2 remains the same, and there's no perspective. Since there's no projection,
58     // Z is irrelevant, so it's okay that fCreationMatrix is an SkMatrix and has
59     // discarded the 3rd row/col, and can ignore those values in localToDevice.
60     const SkM44& positionMatrix = localToDevice.matrix();
61     const bool compatibleMatrix = positionMatrix.rc(0,0) == fCreationMatrix.rc(0, 0) &&
62                                   positionMatrix.rc(0,1) == fCreationMatrix.rc(0, 1) &&
63                                   positionMatrix.rc(1,0) == fCreationMatrix.rc(1, 0) &&
64                                   positionMatrix.rc(1,1) == fCreationMatrix.rc(1, 1) &&
65                                   localToDevice.type() != Transform::Type::kPerspective &&
66                                   !fCreationMatrix.hasPerspective();
67 
68     if (compatibleMatrix) {
69         const SkV4 mappedOrigin = positionMatrix.map(drawOrigin.x(), drawOrigin.y(), 0.f, 1.f);
70         const SkV2 offset = {mappedOrigin.x - fCreationMatrix.getTranslateX(),
71                              mappedOrigin.y - fCreationMatrix.getTranslateY()};
72         if (SkScalarIsInt(offset.x) && SkScalarIsInt(offset.y)) {
73             // The offset is an integer (but make sure), which means the generated mask can be
74             // accessed without changing how texels would be sampled.
75             return {Rect(fCreationBounds),
76                     Transform(SkM44::Translate(SkScalarRoundToInt(offset.x),
77                                                SkScalarRoundToInt(offset.y)))};
78         }
79     }
80 
81     // Otherwise compute the relative transformation from fCreationMatrix to
82     // localToDevice, with the drawOrigin applied. If fCreationMatrix or the
83     // concatenation is not invertible the returned Transform is marked invalid and the draw
84     // will be automatically dropped.
85     const SkMatrix viewDifference = this->viewDifference(
86             localToDevice.preTranslate(drawOrigin.x(), drawOrigin.y()));
87     return {Rect(fCreationBounds), Transform(SkM44(viewDifference))};
88 }
89 
90 }  // namespace sktext::gpu
91