| 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 |
#ifndef skgpu_graphite_RasterPathUtils_DEFINED |
| 9 |
#define skgpu_graphite_RasterPathUtils_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/private/base/SkNoncopyable.h" |
| 13 |
#include "src/base/SkVx.h" |
| 14 |
#include "src/core/SkDraw.h" |
| 15 |
#include "src/core/SkRasterClip.h" |
| 16 |
#include "src/gpu/ResourceKey.h" |
| 17 |
#include "src/gpu/graphite/ClipStack.h" |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
class Shape; |
| 22 |
class Transform; |
| 23 |
|
| 24 |
/** |
| 25 |
* The RasterMaskHelper helps generate masks using the software rendering |
| 26 |
* path. It is intended to be used as: |
| 27 |
* |
| 28 |
* RasterMaskHelper helper(pixmapstorage); |
| 29 |
* helper.init(...); |
| 30 |
* helper.drawShape(...); |
| 31 |
* |
| 32 |
* The result of this process will be the mask rendered in the Pixmap, |
| 33 |
* at the upper left hand corner of the bounds. |
| 34 |
* |
| 35 |
* This can be used for clip masks as well, by doing: |
| 36 |
* helper.drawClip(...); |
| 37 |
* |
| 38 |
* Rasterized clip masks will include the inversion in the mask; rasterized path |
| 39 |
* masks assume that the CoverageMask shader will handle the inversion. |
| 40 |
*/ |
| 41 |
|
| 42 |
class RasterMaskHelper : SkNoncopyable { |
| 43 |
public: |
| 44 |
// Let RasterMaskHelper allocate the backing store (A8). The caller can optionally |
| 45 |
// provide a padding and a translation applied to each draw. The padding is added to |
| 46 |
// the size on all four sides but excluded from the area rendered to. That is, (0,0) |
| 47 |
// will refer to the top-left pixel *inside* the padding. Since each draw method |
| 48 |
// takes a Transform, the translation is just a convenience to save repeated |
| 49 |
// concatenations when the caller already has prebuilt Transforms. The entire |
| 50 |
// allocation (including the padding) is initialized to initialAlpha. |
| 51 |
static std::tuple<SkBitmap, RasterMaskHelper> Allocate(SkISize size, |
| 52 |
SkIVector translation = {0, 0}, |
| 53 |
int padding = 0, |
| 54 |
SkAlpha initialAlpha = 0); |
| 55 |
// The caller provides the storage, which must be A8. |
| 56 |
explicit RasterMaskHelper(SkPixmap pixmap, SkIVector translation = {0, 0}); |
| 57 |
|
| 58 |
// Draw a single shape into the bitmap (as a path) at location resultBounds. |
| 59 |
void drawShape(const Shape& shape, |
| 60 |
const Transform& localToDevice, |
| 61 |
const SkStrokeRec& strokeRec); |
| 62 |
|
| 63 |
// Draw a single shape into the bitmap (as a path) at location resultBounds. |
| 64 |
// Variant used for clipping. |
| 65 |
void drawClip(const Shape& shape, const Transform& localToDevice, uint8_t alpha); |
| 66 |
|
| 67 |
private: |
| 68 |
SkPixmap fPixels; |
| 69 |
SkRasterClip fRasterClip; |
| 70 |
SkVector fTranslate; |
| 71 |
}; |
| 72 |
|
| 73 |
skgpu::UniqueKey GeneratePathMaskKey(const Shape& shape, |
| 74 |
const Transform& transform, |
| 75 |
const SkStrokeRec& strokeRec, |
| 76 |
skvx::half2 maskOrigin, |
| 77 |
skvx::half2 maskSize); |
| 78 |
|
| 79 |
skgpu::UniqueKey GenerateClipMaskKey(uint32_t stackRecordID, |
| 80 |
const ClipStack::ElementList* elementsForMask, |
| 81 |
SkIRect maskDeviceBounds, |
| 82 |
bool includeBounds, |
| 83 |
SkIRect* keyBounds, |
| 84 |
bool* usesPathKey); |
| 85 |
|
| 86 |
} // namespace skgpu::graphite |
| 87 |
|
| 88 |
#endif // skgpu_graphite_RasterPathUtils_DEFINED |