| 1 |
/* |
| 2 |
* Copyright 2024 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/gpu/graphite/RasterPathUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkStrokeRec.h" |
| 11 |
#include "include/private/base/SkFixed.h" |
| 12 |
#include "src/base/SkFloatBits.h" |
| 13 |
#include "src/core/SkBlitter_A8.h" |
| 14 |
#include "src/gpu/graphite/geom/Shape.h" |
| 15 |
#include "src/gpu/graphite/geom/Transform.h" |
| 16 |
|
| 17 |
namespace skgpu::graphite { |
| 18 |
|
| 19 |
std::tuple<SkBitmap, RasterMaskHelper> RasterMaskHelper::Allocate(SkISize size, |
| 20 |
SkIVector translation, |
| 21 |
int padding, |
| 22 |
SkAlpha initialAlpha) { |
| 23 |
SkASSERT(padding >= 0); |
| 24 |
SkASSERT(!size.isEmpty()); |
| 25 |
|
| 26 |
SkISize paddedSize{size.width() + 2 * padding, size.height() + 2 * padding}; |
| 27 |
SkBitmap bitmap; |
| 28 |
bitmap.allocPixels(SkImageInfo::MakeA8(paddedSize)); |
| 29 |
memset(bitmap.getAddr(0, 0), initialAlpha, bitmap.computeByteSize()); |
| 30 |
|
| 31 |
const SkPixmap outerPM = bitmap.pixmap(); |
| 32 |
SkPixmap innerPM; |
| 33 |
SkAssertResult(outerPM.extractSubset(&innerPM, SkIRect::MakePtSize({padding, padding}, size))); |
| 34 |
|
| 35 |
return std::make_tuple(std::move(bitmap), RasterMaskHelper{innerPM, translation}); |
| 36 |
} |
| 37 |
|
| 38 |
RasterMaskHelper::RasterMaskHelper(SkPixmap pixmap, SkIVector translation) |
| 39 |
: fPixels{pixmap} |
| 40 |
, fRasterClip{SkIRect::MakeSize(pixmap.dimensions())} { |
| 41 |
SkASSERT(fPixels.addr()); |
| 42 |
SkASSERT(!fPixels.info().dimensions().isEmpty()); |
| 43 |
SkASSERT(pixmap.colorType() == kAlpha_8_SkColorType); |
| 44 |
|
| 45 |
fTranslate.fX = translation.fX; |
| 46 |
fTranslate.fY = translation.fY; |
| 47 |
} |
| 48 |
|
| 49 |
skcpu::Draw make_draw(const SkPixmap& pm, const SkRasterClip& rc, const SkMatrix& m) { |
| 50 |
skcpu::Draw draw; |
| 51 |
draw.fDst = pm; |
| 52 |
draw.fBlitterChooser = SkA8Blitter_Choose; |
| 53 |
draw.fCTM = &m; |
| 54 |
draw.fRC = &rc; |
| 55 |
return draw; |
| 56 |
} |
| 57 |
|
| 58 |
void RasterMaskHelper::drawShape(const Shape& shape, |
| 59 |
const Transform& localToDevice, |
| 60 |
const SkStrokeRec& strokeRec) { |
| 61 |
SkPaint paint; |
| 62 |
paint.setBlendMode(SkBlendMode::kSrc); // "Replace" mode |
| 63 |
paint.setAntiAlias(true); |
| 64 |
// SkPaint's color is unpremul so this will produce alpha in every channel. |
| 65 |
paint.setColor(SK_ColorWHITE); |
| 66 |
strokeRec.applyToPaint(&paint); |
| 67 |
|
| 68 |
SkMatrix translatedMatrix = SkMatrix(localToDevice); |
| 69 |
translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 70 |
|
| 71 |
// TODO: use drawRect, drawRRect, drawArc |
| 72 |
SkPath path = shape.asPath(); |
| 73 |
if (path.isInverseFillType()) { |
| 74 |
// The shader will handle the inverse fill in this case |
| 75 |
path.toggleInverseFillType(); |
| 76 |
} |
| 77 |
make_draw(fPixels, fRasterClip, translatedMatrix).drawPathCoverage(path, paint); |
| 78 |
} |
| 79 |
|
| 80 |
void RasterMaskHelper::drawClip(const Shape& shape, const Transform& localToDevice, uint8_t alpha) { |
| 81 |
SkPaint paint; |
| 82 |
paint.setBlendMode(SkBlendMode::kSrc); // "Replace" mode |
| 83 |
paint.setAntiAlias(true); |
| 84 |
// SkPaint's color is unpremul so this will produce alpha in every channel. |
| 85 |
paint.setColor(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF)); |
| 86 |
|
| 87 |
SkMatrix translatedMatrix = SkMatrix(localToDevice); |
| 88 |
translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 89 |
|
| 90 |
// TODO: use drawRect, drawRRect, drawArc |
| 91 |
SkPath path = shape.asPath(); |
| 92 |
skcpu::Draw draw = make_draw(fPixels, fRasterClip, translatedMatrix); |
| 93 |
// Because we could be combining multiple paths into one entry we don't touch |
| 94 |
// the inverse fill in this case. |
| 95 |
if (0xFF == alpha) { |
| 96 |
SkASSERT(0xFF == paint.getAlpha()); |
| 97 |
draw.drawPathCoverage(path, paint); |
| 98 |
} else { |
| 99 |
draw.drawPath(path, paint, nullptr); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
uint32_t add_transform_key(skgpu::UniqueKey::Builder* builder, |
| 104 |
int startIndex, |
| 105 |
const Transform& transform) { |
| 106 |
// We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 107 |
SkMatrix mat = transform.matrix().asM33(); |
| 108 |
SkScalar sx = mat.get(SkMatrix::kMScaleX); |
| 109 |
SkScalar sy = mat.get(SkMatrix::kMScaleY); |
| 110 |
SkScalar kx = mat.get(SkMatrix::kMSkewX); |
| 111 |
SkScalar ky = mat.get(SkMatrix::kMSkewY); |
| 112 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 113 |
// Fractional translate does not affect caching on Android. This is done for better cache |
| 114 |
// hit ratio and speed and is matching HWUI behavior, which didn't consider the matrix |
| 115 |
// at all when caching paths. |
| 116 |
SkFixed fracX = 0; |
| 117 |
SkFixed fracY = 0; |
| 118 |
#else |
| 119 |
SkScalar tx = mat.get(SkMatrix::kMTransX); |
| 120 |
SkScalar ty = mat.get(SkMatrix::kMTransY); |
| 121 |
// Allow 8 bits each in x and y of subpixel positioning. |
| 122 |
SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 123 |
SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
| 124 |
#endif |
| 125 |
(*builder)[startIndex + 0] = SkFloat2Bits(sx); |
| 126 |
(*builder)[startIndex + 1] = SkFloat2Bits(sy); |
| 127 |
(*builder)[startIndex + 2] = SkFloat2Bits(kx); |
| 128 |
(*builder)[startIndex + 3] = SkFloat2Bits(ky); |
| 129 |
// FracX and fracY are &ed with 0x0000ff00, so need to shift one down to fill 16 bits. |
| 130 |
uint32_t fracBits = fracX | (fracY >> 8); |
| 131 |
|
| 132 |
return fracBits; |
| 133 |
} |
| 134 |
|
| 135 |
skgpu::UniqueKey GeneratePathMaskKey(const Shape& shape, |
| 136 |
const Transform& transform, |
| 137 |
const SkStrokeRec& strokeRec, |
| 138 |
skvx::half2 maskOrigin, |
| 139 |
skvx::half2 maskSize) { |
| 140 |
skgpu::UniqueKey maskKey; |
| 141 |
{ |
| 142 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 143 |
uint16_t styleKeySize = 7; |
| 144 |
if (!strokeRec.isHairlineStyle() && !strokeRec.isFillStyle()) { |
| 145 |
// Add space for width and miter if needed |
| 146 |
styleKeySize += 2; |
| 147 |
} |
| 148 |
skgpu::UniqueKey::Builder builder(&maskKey, kDomain, styleKeySize + shape.keySize(), |
| 149 |
"Raster Path Mask"); |
| 150 |
builder[0] = maskOrigin.x() | (maskOrigin.y() << 16); |
| 151 |
builder[1] = maskSize.x() | (maskSize.y() << 16); |
| 152 |
|
| 153 |
// Add transform key and get packed fractional translation bits |
| 154 |
uint32_t fracBits = add_transform_key(&builder, 2, transform); |
| 155 |
// Distinguish between path styles. For anything but fill, we also need to include |
| 156 |
// the cap. (SW grows hairlines by 0.5 pixel with round and square caps). For stroke |
| 157 |
// or fill-and-stroke we need to include the join, width, and miter. |
| 158 |
static_assert(SkStrokeRec::kStyleCount <= (1 << 2)); |
| 159 |
static_assert(SkPaint::kCapCount <= (1 << 2)); |
| 160 |
static_assert(SkPaint::kJoinCount <= (1 << 2)); |
| 161 |
uint32_t styleBits = strokeRec.getStyle(); |
| 162 |
if (!strokeRec.isFillStyle()) { |
| 163 |
styleBits |= (strokeRec.getCap() << 2); |
| 164 |
} |
| 165 |
if (!strokeRec.isHairlineStyle() && !strokeRec.isFillStyle()) { |
| 166 |
styleBits |= (strokeRec.getJoin() << 4); |
| 167 |
builder[6] = SkFloat2Bits(strokeRec.getWidth()); |
| 168 |
builder[7] = SkFloat2Bits(strokeRec.getMiter()); |
| 169 |
} |
| 170 |
builder[styleKeySize-1] = fracBits | (styleBits << 16); |
| 171 |
shape.writeKey(&builder[styleKeySize], /*includeInverted=*/false); |
| 172 |
} |
| 173 |
return maskKey; |
| 174 |
} |
| 175 |
|
| 176 |
skgpu::UniqueKey GenerateClipMaskKey(uint32_t stackRecordID, |
| 177 |
const ClipStack::ElementList* elementsForMask, |
| 178 |
SkIRect maskDeviceBounds, |
| 179 |
bool includeBounds, |
| 180 |
SkIRect* keyBounds, |
| 181 |
bool* usesPathKey) { |
| 182 |
static constexpr int kMaxShapeCountForKey = 2; |
| 183 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 184 |
|
| 185 |
skgpu::UniqueKey maskKey; |
| 186 |
// if the element list is too large we just use the stackRecordID |
| 187 |
if (elementsForMask->size() <= kMaxShapeCountForKey) { |
| 188 |
static constexpr int kXformKeySize = 5; |
| 189 |
uint16_t keySize = includeBounds ? 2 : 0; |
| 190 |
// Iterate through to get key size; given kMaxShapeCountForKey and Shape's own key size |
| 191 |
// limitations, this should always fit safely within a 16-bit number |
| 192 |
for (int i = 0; i < elementsForMask->size(); ++i) { |
| 193 |
keySize += kXformKeySize + (*elementsForMask)[i]->fShape.keySize(); |
| 194 |
} |
| 195 |
|
| 196 |
skgpu::UniqueKey::Builder builder(&maskKey, kDomain, keySize, "Clip Path Mask"); |
| 197 |
int elementKeyIndex = 0; |
| 198 |
Rect unclippedBounds = Rect::InfiniteInverted(); |
| 199 |
for (int i = 0; i < elementsForMask->size(); ++i) { |
| 200 |
const ClipStack::Element* element = (*elementsForMask)[i]; |
| 201 |
|
| 202 |
// Add transform key and get packed fractional translation bits |
| 203 |
uint32_t fracBits = add_transform_key(&builder, |
| 204 |
elementKeyIndex, |
| 205 |
element->fLocalToDevice); |
| 206 |
uint32_t opBits = static_cast<uint32_t>(element->fOp); |
| 207 |
builder[elementKeyIndex + 4] = fracBits | (opBits << 16); |
| 208 |
|
| 209 |
const Shape& shape = element->fShape; |
| 210 |
shape.writeKey(&builder[elementKeyIndex + kXformKeySize], |
| 211 |
/*includeInverted=*/true); |
| 212 |
|
| 213 |
elementKeyIndex += kXformKeySize + shape.keySize(); |
| 214 |
|
| 215 |
Rect transformedBounds = element->fLocalToDevice.mapRect(element->fShape.bounds()); |
| 216 |
unclippedBounds.join(transformedBounds); |
| 217 |
} |
| 218 |
|
| 219 |
// The keyBounds are the maskDeviceBounds relative to the full transformed mask. We use |
| 220 |
// this to ensure we capture the situation where the maskDeviceBounds are equal in two |
| 221 |
// cases but actually enclose different regions of the full mask due to an integer |
| 222 |
// translation (which is not captured in the key) in the element transforms. |
| 223 |
*keyBounds = maskDeviceBounds.makeOffset(-unclippedBounds.left(), |
| 224 |
-unclippedBounds.top()); |
| 225 |
|
| 226 |
if (includeBounds) { |
| 227 |
SkASSERT(SkTFitsIn<int16_t>(keyBounds->left())); |
| 228 |
SkASSERT(SkTFitsIn<int16_t>(keyBounds->top())); |
| 229 |
SkASSERT(SkTFitsIn<int16_t>(keyBounds->right())); |
| 230 |
SkASSERT(SkTFitsIn<int16_t>(keyBounds->bottom())); |
| 231 |
|
| 232 |
builder[elementKeyIndex] = keyBounds->left() | (keyBounds->top() << 16); |
| 233 |
builder[elementKeyIndex+1] = keyBounds->right() | (keyBounds->bottom() << 16); |
| 234 |
} |
| 235 |
|
| 236 |
*usesPathKey = true; |
| 237 |
return maskKey; |
| 238 |
} |
| 239 |
|
| 240 |
// Either we have too many elements or at least one shape can't create a key |
| 241 |
skgpu::UniqueKey::Builder builder(&maskKey, kDomain, 1, "Clip SaveRecord Mask"); |
| 242 |
builder[0] = stackRecordID; |
| 243 |
|
| 244 |
*usesPathKey = false; |
| 245 |
// It doesn't matter what the keyBounds are in this case -- |
| 246 |
// the stackRecordID is enough to distinguish between clips. |
| 247 |
*keyBounds = {}; |
| 248 |
return maskKey; |
| 249 |
} |
| 250 |
|
| 251 |
} // namespace skgpu::graphite |