| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 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 GrAppliedClip_DEFINED |
| 9 |
#define GrAppliedClip_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 14 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 15 |
#include "src/gpu/ganesh/GrScissorState.h" |
| 16 |
#include "src/gpu/ganesh/GrWindowRectsState.h" |
| 17 |
|
| 18 |
#include <cstdint> |
| 19 |
#include <memory> |
| 20 |
#include <utility> |
| 21 |
|
| 22 |
class GrWindowRectangles; |
| 23 |
struct SkISize; |
| 24 |
|
| 25 |
/** |
| 26 |
* Produced by GrHardClip. It provides a set of modifications to the hardware drawing state that |
| 27 |
* implement the clip. |
| 28 |
*/ |
| 29 |
class GrAppliedHardClip { |
| 30 |
public: |
| 31 |
static const GrAppliedHardClip& Disabled() { |
| 32 |
// The size doesn't really matter here since it's returned as const& so an actual scissor |
| 33 |
// will never be set on it, and applied clips are not used to query or bounds test like |
| 34 |
// the GrClip is. |
| 35 |
static const GrAppliedHardClip kDisabled({1 << 29, 1 << 29}); |
| 36 |
return kDisabled; |
| 37 |
} |
| 38 |
|
| 39 |
GrAppliedHardClip(const SkISize& rtDims) : fScissorState(rtDims) {} |
| 40 |
GrAppliedHardClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 41 |
: fScissorState(backingStoreDims) { |
| 42 |
fScissorState.set(SkIRect::MakeSize(logicalRTDims)); |
| 43 |
} |
| 44 |
|
| 45 |
GrAppliedHardClip(GrAppliedHardClip&& that) = default; |
| 46 |
explicit GrAppliedHardClip(const GrAppliedHardClip&) = default; |
| 47 |
|
| 48 |
const GrScissorState& scissorState() const { return fScissorState; } |
| 49 |
const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; } |
| 50 |
uint32_t stencilStackID() const { return fStencilStackID; } |
| 51 |
bool hasStencilClip() const { return SK_InvalidGenID != fStencilStackID; } |
| 52 |
|
| 53 |
/** |
| 54 |
* Intersects the applied clip with the provided rect. Returns false if the draw became empty. |
| 55 |
* 'clippedDrawBounds' will be intersected with 'irect'. This returns false if the clip becomes |
| 56 |
* empty or the draw no longer intersects the clip. In either case the draw can be skipped. |
| 57 |
*/ |
| 58 |
bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) { |
| 59 |
return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect)); |
| 60 |
} |
| 61 |
|
| 62 |
void setScissor(const SkIRect& irect) { |
| 63 |
fScissorState.set(irect); |
| 64 |
} |
| 65 |
|
| 66 |
void addWindowRectangles(const GrWindowRectsState& windowState) { |
| 67 |
SkASSERT(!fWindowRectsState.enabled()); |
| 68 |
fWindowRectsState = windowState; |
| 69 |
} |
| 70 |
|
| 71 |
void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) { |
| 72 |
SkASSERT(!fWindowRectsState.enabled()); |
| 73 |
fWindowRectsState.set(windows, mode); |
| 74 |
} |
| 75 |
|
| 76 |
void addStencilClip(uint32_t stencilStackID) { |
| 77 |
SkASSERT(SK_InvalidGenID == fStencilStackID); |
| 78 |
fStencilStackID = stencilStackID; |
| 79 |
} |
| 80 |
|
| 81 |
bool doesClip() const { |
| 82 |
return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled(); |
| 83 |
} |
| 84 |
|
| 85 |
bool operator==(const GrAppliedHardClip& that) const { |
| 86 |
return fScissorState == that.fScissorState && |
| 87 |
fWindowRectsState == that.fWindowRectsState && |
| 88 |
fStencilStackID == that.fStencilStackID; |
| 89 |
} |
| 90 |
bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); } |
| 91 |
|
| 92 |
private: |
| 93 |
GrScissorState fScissorState; |
| 94 |
GrWindowRectsState fWindowRectsState; |
| 95 |
uint32_t fStencilStackID = SK_InvalidGenID; |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip. |
| 100 |
*/ |
| 101 |
class GrAppliedClip { |
| 102 |
public: |
| 103 |
static GrAppliedClip Disabled() { |
| 104 |
return GrAppliedClip({1 << 29, 1 << 29}); |
| 105 |
} |
| 106 |
|
| 107 |
GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {} |
| 108 |
GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 109 |
: fHardClip(logicalRTDims, backingStoreDims) {} |
| 110 |
|
| 111 |
GrAppliedClip(GrAppliedClip&& that) = default; |
| 112 |
GrAppliedClip(const GrAppliedClip&) = delete; |
| 113 |
|
| 114 |
const GrScissorState& scissorState() const { return fHardClip.scissorState(); } |
| 115 |
const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); } |
| 116 |
uint32_t stencilStackID() const { return fHardClip.stencilStackID(); } |
| 117 |
bool hasStencilClip() const { return fHardClip.hasStencilClip(); } |
| 118 |
int hasCoverageFragmentProcessor() const { return fCoverageFP != nullptr; } |
| 119 |
const GrFragmentProcessor* coverageFragmentProcessor() const { |
| 120 |
SkASSERT(fCoverageFP != nullptr); |
| 121 |
return fCoverageFP.get(); |
| 122 |
} |
| 123 |
std::unique_ptr<GrFragmentProcessor> detachCoverageFragmentProcessor() { |
| 124 |
SkASSERT(fCoverageFP != nullptr); |
| 125 |
return std::move(fCoverageFP); |
| 126 |
} |
| 127 |
|
| 128 |
const GrAppliedHardClip& hardClip() const { return fHardClip; } |
| 129 |
GrAppliedHardClip& hardClip() { return fHardClip; } |
| 130 |
|
| 131 |
void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) { |
| 132 |
if (fCoverageFP == nullptr) { |
| 133 |
fCoverageFP = std::move(fp); |
| 134 |
} else { |
| 135 |
// Compose this coverage FP with the previously-added coverage. |
| 136 |
fCoverageFP = GrFragmentProcessor::Compose(std::move(fp), std::move(fCoverageFP)); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
bool doesClip() const { |
| 141 |
return fHardClip.doesClip() || fCoverageFP != nullptr; |
| 142 |
} |
| 143 |
|
| 144 |
bool operator==(const GrAppliedClip& that) const { |
| 145 |
if (fHardClip != that.fHardClip || |
| 146 |
this->hasCoverageFragmentProcessor() != that.hasCoverageFragmentProcessor()) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
if (fCoverageFP != nullptr && !fCoverageFP->isEqual(*that.fCoverageFP)) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
bool operator!=(const GrAppliedClip& that) const { return !(*this == that); } |
| 155 |
|
| 156 |
void visitProxies(const GrVisitProxyFunc& func) const { |
| 157 |
if (fCoverageFP != nullptr) { |
| 158 |
fCoverageFP->visitProxies(func); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private: |
| 163 |
GrAppliedHardClip fHardClip; |
| 164 |
std::unique_ptr<GrFragmentProcessor> fCoverageFP; |
| 165 |
}; |
| 166 |
|
| 167 |
#endif |