| 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 |
#ifndef skgpu_graphite_geom_CoverageMaskShape_DEFINED |
| 9 |
#define skgpu_graphite_geom_CoverageMaskShape_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkM44.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/private/base/SkAssert.h" |
| 14 |
#include "src/base/SkVx.h" |
| 15 |
#include "src/gpu/graphite/TextureProxy.h" |
| 16 |
#include "src/gpu/graphite/geom/Rect.h" |
| 17 |
#include "src/gpu/graphite/geom/Shape.h" |
| 18 |
|
| 19 |
#include <utility> |
| 20 |
|
| 21 |
namespace skgpu::graphite { |
| 22 |
|
| 23 |
/** |
| 24 |
* CoverageMaskShape represents a shape for which per-pixel coverage data comes from a |
| 25 |
* texture. This excludes font glyphs that are rendered to a persistent atlas, as those are |
| 26 |
* represented by the SubRunData geometry type. |
| 27 |
* |
| 28 |
* Coverage masks are defined relative to an intermediate coordinate space between the final |
| 29 |
* device space and the original geometry and shading's local space. For atlases and simple cases |
| 30 |
* this intermediate space is pixel-aligned with the final device space, meaning only an integer |
| 31 |
* translation is necessary to align the mask with where the original geometry would have been |
| 32 |
* rendered into the device. In complex cases, the remaining transform may include rotation, skew, |
| 33 |
* or even perspective that has to be applied after some filter effect. |
| 34 |
* |
| 35 |
* Regardless, the DrawParams that records the CoverageMaskShape stores this remaining transform as |
| 36 |
* the "local-to-device" tranform, i.e. "local" refers to the mask's coordinate space. The |
| 37 |
* CoverageMaskShape stores the original local-to-device inverse so that it can reconstruct coords |
| 38 |
* for shading. Like other Geometry types, the bounds() returned by CoverageMaskShape are relative |
| 39 |
* to its local space, so they are identical to its mask size. |
| 40 |
*/ |
| 41 |
class CoverageMaskShape { |
| 42 |
using half2 = skvx::half2; |
| 43 |
using int2 = skvx::int2; |
| 44 |
|
| 45 |
public: |
| 46 |
struct MaskInfo { |
| 47 |
// The texture-relative integer UV coordinates of the top-left corner of this shape's |
| 48 |
// coverage mask bounds. This will include the rounded out transformed device space bounds |
| 49 |
// of the shape plus a 1-pixel border. |
| 50 |
half2 fTextureOrigin; |
| 51 |
|
| 52 |
// The width and height of the bounds of the coverage mask shape in device coordinates. This |
| 53 |
// includes the rounded out transformed device space bounds of the shape + a 1-pixel border |
| 54 |
// added for AA. |
| 55 |
half2 fMaskSize; |
| 56 |
}; |
| 57 |
|
| 58 |
CoverageMaskShape() = default; |
| 59 |
CoverageMaskShape(const Shape& shape, |
| 60 |
sk_sp<TextureProxy> proxy, |
| 61 |
const SkM44& deviceToLocal, |
| 62 |
const MaskInfo& maskInfo) |
| 63 |
: fTextureProxy(std::move(proxy)) |
| 64 |
, fDeviceToLocal(deviceToLocal) |
| 65 |
, fInverted(shape.inverted()) |
| 66 |
, fMaskInfo(maskInfo) { |
| 67 |
SkASSERT(fTextureProxy); |
| 68 |
} |
| 69 |
CoverageMaskShape(const CoverageMaskShape&) = default; |
| 70 |
|
| 71 |
~CoverageMaskShape() = default; |
| 72 |
|
| 73 |
// NOTE: None of the geometry types benefit from move semantics, so we don't bother |
| 74 |
// defining a move assignment operator for CoverageMaskShape. |
| 75 |
CoverageMaskShape& operator=(CoverageMaskShape&&) = delete; |
| 76 |
CoverageMaskShape& operator=(const CoverageMaskShape&) = default; |
| 77 |
|
| 78 |
// Returns the mask-space bounds of the clipped coverage mask shape. For inverse fills this |
| 79 |
// is different from the actual draw bounds stored in the Clip. |
| 80 |
Rect bounds() const { |
| 81 |
return Rect(0.f, 0.f, (float) this->maskSize().x(), (float) this->maskSize().y()); |
| 82 |
} |
| 83 |
|
| 84 |
// The inverse local-to-device matrix. |
| 85 |
const SkM44& deviceToLocal() const { return fDeviceToLocal; } |
| 86 |
|
| 87 |
// The texture-relative integer UV coordinates of the top-left corner of this shape's |
| 88 |
// coverage mask bounds. |
| 89 |
const half2& textureOrigin() const { return fMaskInfo.fTextureOrigin; } |
| 90 |
|
| 91 |
// The width and height of the bounds of the coverage mask shape in device coordinates. |
| 92 |
const half2& maskSize() const { return fMaskInfo.fMaskSize; } |
| 93 |
|
| 94 |
// The texture that the shape will be rendered to. |
| 95 |
const TextureProxy* textureProxy() const { return fTextureProxy.get(); } |
| 96 |
|
| 97 |
// Whether or not the shape will be painted according to an inverse fill rule. |
| 98 |
bool inverted() const { return fInverted; } |
| 99 |
|
| 100 |
private: |
| 101 |
// We store a ref to the texture proxy to keep it alive. |
| 102 |
// TODO: switch to raw pointer once textures/uniforms are extracted in Device::drawGeometry. |
| 103 |
sk_sp<TextureProxy> fTextureProxy; |
| 104 |
SkM44 fDeviceToLocal; |
| 105 |
bool fInverted; |
| 106 |
MaskInfo fMaskInfo; |
| 107 |
}; |
| 108 |
|
| 109 |
} // namespace skgpu::graphite |
| 110 |
|
| 111 |
#endif // skgpu_graphite_geom_CoverageMaskShape_DEFINED |