| 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 |
|
| 8 |
#include "src/text/gpu/VertexFiller.h" |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/core/SkTypes.h" |
| 15 |
#include "src/core/SkReadBuffer.h" |
| 16 |
#include "src/core/SkWriteBuffer.h" |
| 17 |
#include "src/gpu/AtlasTypes.h" |
| 18 |
#include "src/text/gpu/SubRunAllocator.h" |
| 19 |
#include "src/text/gpu/SubRunContainer.h" |
| 20 |
|
| 21 |
#include <optional> |
| 22 |
|
| 23 |
using MaskFormat = skgpu::MaskFormat; |
| 24 |
|
| 25 |
namespace sktext::gpu { |
| 26 |
|
| 27 |
VertexFiller::VertexFiller(MaskFormat maskFormat, |
| 28 |
const SkMatrix &creationMatrix, |
| 29 |
SkRect creationBounds, |
| 30 |
SkSpan<const SkPoint> leftTop, |
| 31 |
bool canDrawDirect) |
| 32 |
: fMaskType{maskFormat}, fCanDrawDirect{canDrawDirect}, |
| 33 |
fCreationMatrix{creationMatrix}, fCreationBounds{creationBounds}, |
| 34 |
fLeftTop{leftTop} {} |
| 35 |
|
| 36 |
VertexFiller VertexFiller::Make(MaskFormat maskType, |
| 37 |
const SkMatrix &creationMatrix, |
| 38 |
SkRect creationBounds, |
| 39 |
SkSpan<const SkPoint> positions, |
| 40 |
SubRunAllocator *alloc, |
| 41 |
FillerType fillerType) { |
| 42 |
SkSpan<SkPoint> leftTop = alloc->makePODSpan<SkPoint>(positions); |
| 43 |
return VertexFiller{ |
| 44 |
maskType, creationMatrix, creationBounds, leftTop, fillerType == kIsDirect}; |
| 45 |
} |
| 46 |
|
| 47 |
std::optional<VertexFiller> VertexFiller::MakeFromBuffer(SkReadBuffer &buffer, |
| 48 |
SubRunAllocator *alloc) { |
| 49 |
int checkingMaskType = buffer.readInt(); |
| 50 |
if (!buffer.validate( |
| 51 |
0 <= checkingMaskType && checkingMaskType < skgpu::kMaskFormatCount)) { |
| 52 |
return std::nullopt; |
| 53 |
} |
| 54 |
MaskFormat maskType = (MaskFormat) checkingMaskType; |
| 55 |
|
| 56 |
const bool canDrawDirect = buffer.readBool(); |
| 57 |
|
| 58 |
SkMatrix creationMatrix; |
| 59 |
buffer.readMatrix(&creationMatrix); |
| 60 |
|
| 61 |
SkRect creationBounds = buffer.readRect(); |
| 62 |
|
| 63 |
SkSpan<SkPoint> leftTop = MakePointsFromBuffer(buffer, alloc); |
| 64 |
if (leftTop.empty()) { return std::nullopt; } |
| 65 |
|
| 66 |
SkASSERT(buffer.isValid()); |
| 67 |
return VertexFiller{maskType, creationMatrix, creationBounds, leftTop, canDrawDirect}; |
| 68 |
} |
| 69 |
|
| 70 |
void VertexFiller::flatten(SkWriteBuffer &buffer) const { |
| 71 |
buffer.writeInt(static_cast<int>(fMaskType)); |
| 72 |
buffer.writeBool(fCanDrawDirect); |
| 73 |
buffer.writeMatrix(fCreationMatrix); |
| 74 |
buffer.writeRect(fCreationBounds); |
| 75 |
buffer.writePointArray(fLeftTop); |
| 76 |
} |
| 77 |
|
| 78 |
SkMatrix VertexFiller::viewDifference(const SkMatrix &positionMatrix) const { |
| 79 |
if (SkMatrix inverse; fCreationMatrix.invert(&inverse)) { |
| 80 |
return SkMatrix::Concat(positionMatrix, inverse); |
| 81 |
} |
| 82 |
return SkMatrix::I(); |
| 83 |
} |
| 84 |
|
| 85 |
// Check for integer translate with the same 2x2 matrix. |
| 86 |
// Returns the translation, and true if the change from creation matrix to the position matrix |
| 87 |
// supports using direct glyph masks. |
| 88 |
std::tuple<bool, SkVector> VertexFiller::CanUseDirect( |
| 89 |
const SkMatrix& creationMatrix, const SkMatrix& positionMatrix) { |
| 90 |
// The existing direct glyph info can be used if the creationMatrix, and the |
| 91 |
// positionMatrix have the same 2x2, the translation between them is integer, and no |
| 92 |
// perspective is involved. Calculate the translation in source space to a translation in |
| 93 |
// device space by mapping (0, 0) through both the creationMatrix and the positionMatrix; |
| 94 |
// take the difference. |
| 95 |
SkVector translation = positionMatrix.mapOrigin() - creationMatrix.mapOrigin(); |
| 96 |
return {creationMatrix.getScaleX() == positionMatrix.getScaleX() && |
| 97 |
creationMatrix.getScaleY() == positionMatrix.getScaleY() && |
| 98 |
creationMatrix.getSkewX() == positionMatrix.getSkewX() && |
| 99 |
creationMatrix.getSkewY() == positionMatrix.getSkewY() && |
| 100 |
!positionMatrix.hasPerspective() && !creationMatrix.hasPerspective() && |
| 101 |
SkScalarIsInt(translation.x()) && SkScalarIsInt(translation.y()), |
| 102 |
translation}; |
| 103 |
} |
| 104 |
|
| 105 |
bool VertexFiller::isLCD() const { return fMaskType == MaskFormat::kA565; } |
| 106 |
|
| 107 |
// Return true if the positionMatrix represents an integer translation. Return the device |
| 108 |
// bounding box of all the glyphs. If the bounding box is empty, then something went singular |
| 109 |
// and this operation should be dropped. |
| 110 |
std::tuple<bool, SkRect> VertexFiller::deviceRectAndCheckTransform( |
| 111 |
const SkMatrix &positionMatrix) const { |
| 112 |
if (fCanDrawDirect) { |
| 113 |
const auto [directDrawCompatible, offset] = CanUseDirect(fCreationMatrix, positionMatrix); |
| 114 |
|
| 115 |
if (directDrawCompatible) { |
| 116 |
return {true, fCreationBounds.makeOffset(offset)}; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (SkMatrix inverse; fCreationMatrix.invert(&inverse)) { |
| 121 |
SkMatrix viewDifference = SkMatrix::Concat(positionMatrix, inverse); |
| 122 |
return {false, viewDifference.mapRect(fCreationBounds)}; |
| 123 |
} |
| 124 |
|
| 125 |
// initialPositionMatrix is singular. Do nothing. |
| 126 |
return {false, SkRect::MakeEmpty()}; |
| 127 |
} |
| 128 |
|
| 129 |
} // namespace sktext::gpu |