| 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 |
SkMatrix VertexFiller::viewDifference(const SkMatrix &positionMatrix) const { |
| 48 |
if (SkMatrix inverse; fCreationMatrix.invert(&inverse)) { |
| 49 |
return SkMatrix::Concat(positionMatrix, inverse); |
| 50 |
} |
| 51 |
return SkMatrix::I(); |
| 52 |
} |
| 53 |
|
| 54 |
// Check for integer translate with the same 2x2 matrix. |
| 55 |
// Returns the translation, and true if the change from creation matrix to the position matrix |
| 56 |
// supports using direct glyph masks. |
| 57 |
std::tuple<bool, SkVector> VertexFiller::CanUseDirect( |
| 58 |
const SkMatrix& creationMatrix, const SkMatrix& positionMatrix) { |
| 59 |
// The existing direct glyph info can be used if the creationMatrix, and the |
| 60 |
// positionMatrix have the same 2x2, the translation between them is integer, and no |
| 61 |
// perspective is involved. Calculate the translation in source space to a translation in |
| 62 |
// device space by mapping (0, 0) through both the creationMatrix and the positionMatrix; |
| 63 |
// take the difference. |
| 64 |
SkVector translation = positionMatrix.mapOrigin() - creationMatrix.mapOrigin(); |
| 65 |
return {creationMatrix.getScaleX() == positionMatrix.getScaleX() && |
| 66 |
creationMatrix.getScaleY() == positionMatrix.getScaleY() && |
| 67 |
creationMatrix.getSkewX() == positionMatrix.getSkewX() && |
| 68 |
creationMatrix.getSkewY() == positionMatrix.getSkewY() && |
| 69 |
!positionMatrix.hasPerspective() && !creationMatrix.hasPerspective() && |
| 70 |
SkScalarIsInt(translation.x()) && SkScalarIsInt(translation.y()), |
| 71 |
translation}; |
| 72 |
} |
| 73 |
|
| 74 |
bool VertexFiller::isLCD() const { return fMaskType == MaskFormat::kA565; } |
| 75 |
|
| 76 |
// Return true if the positionMatrix represents an integer translation. Return the device |
| 77 |
// bounding box of all the glyphs. If the bounding box is empty, then something went singular |
| 78 |
// and this operation should be dropped. |
| 79 |
std::tuple<bool, SkRect> VertexFiller::deviceRectAndCheckTransform( |
| 80 |
const SkMatrix &positionMatrix) const { |
| 81 |
if (fCanDrawDirect) { |
| 82 |
const auto [directDrawCompatible, offset] = CanUseDirect(fCreationMatrix, positionMatrix); |
| 83 |
|
| 84 |
if (directDrawCompatible) { |
| 85 |
return {true, fCreationBounds.makeOffset(offset)}; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (SkMatrix inverse; fCreationMatrix.invert(&inverse)) { |
| 90 |
SkMatrix viewDifference = SkMatrix::Concat(positionMatrix, inverse); |
| 91 |
return {false, viewDifference.mapRect(fCreationBounds)}; |
| 92 |
} |
| 93 |
|
| 94 |
// initialPositionMatrix is singular. Do nothing. |
| 95 |
return {false, SkRect::MakeEmpty()}; |
| 96 |
} |
| 97 |
|
| 98 |
} // namespace sktext::gpu |