1 /* 2 * Copyright 2017 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 StencilClip_DEFINED 9 #define StencilClip_DEFINED 10 11 #include "src/gpu/GrAppliedClip.h" 12 #include "src/gpu/GrFixedClip.h" 13 14 namespace skgpu::v1 { 15 16 /** 17 * Implements GrHardClip with the currently-existing stencil buffer contents and GrFixedClip. 18 */ 19 class StencilClip final : public GrHardClip { 20 public: 21 explicit StencilClip(const SkISize& rtDims, uint32_t stencilStackID = SK_InvalidGenID) fFixedClip(rtDims)22 : fFixedClip(rtDims) 23 , fStencilStackID(stencilStackID) {} 24 25 StencilClip(const SkISize& rtDims, 26 const SkIRect& scissorRect, 27 uint32_t stencilStackID = SK_InvalidGenID) fFixedClip(rtDims,scissorRect)28 : fFixedClip(rtDims, scissorRect) 29 , fStencilStackID(stencilStackID) {} 30 fixedClip()31 const GrFixedClip& fixedClip() const { return fFixedClip; } fixedClip()32 GrFixedClip& fixedClip() { return fFixedClip; } 33 stencilStackID()34 uint32_t stencilStackID() const { return fStencilStackID; } hasStencilClip()35 bool hasStencilClip() const { return SK_InvalidGenID != fStencilStackID; } setStencilClip(uint32_t stencilStackID)36 void setStencilClip(uint32_t stencilStackID) { fStencilStackID = stencilStackID; } 37 getConservativeBounds()38 SkIRect getConservativeBounds() const final { 39 return fFixedClip.getConservativeBounds(); 40 } 41 apply(GrAppliedHardClip * out,SkIRect * bounds)42 Effect apply(GrAppliedHardClip* out, SkIRect* bounds) const final { 43 Effect effect = fFixedClip.apply(out, bounds); 44 if (effect == Effect::kClippedOut) { 45 // Stencil won't bring back coverage 46 return Effect::kClippedOut; 47 } 48 if (this->hasStencilClip()) { 49 out->addStencilClip(fStencilStackID); 50 effect = Effect::kClipped; 51 } 52 return effect; 53 } 54 preApply(const SkRect & drawBounds,GrAA aa)55 PreClipResult preApply(const SkRect& drawBounds, GrAA aa) const final { 56 if (this->hasStencilClip()) { 57 return this->INHERITED::preApply(drawBounds, aa); 58 } else { 59 return fFixedClip.preApply(drawBounds, aa); 60 } 61 } 62 63 private: 64 GrFixedClip fFixedClip; 65 uint32_t fStencilStackID; 66 67 using INHERITED = GrClip; 68 }; 69 70 } // namespace skgpu::v1 71 72 #endif // StencilClip_DEFINED 73