1 /* 2 * Copyright 2011 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 GrRenderTarget_DEFINED 9 #define GrRenderTarget_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/gpu/ganesh/GrBackendSurface.h" 13 #include "include/gpu/ganesh/GrTypes.h" 14 #include "include/private/base/SkTArray.h" 15 #include "src/gpu/ganesh/GrAttachment.h" 16 #include "src/gpu/ganesh/GrSurface.h" 17 18 #include <string_view> 19 20 class GrGpu; 21 struct SkISize; 22 struct SkPoint; 23 24 /** 25 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to. 26 * A context's render target is set by setRenderTarget(). Render targets are 27 * created by a createTexture with the kRenderTarget_SurfaceFlag flag. 28 * Additionally, GrContext provides methods for creating GrRenderTargets 29 * that wrap externally created render targets. 30 */ 31 class GrRenderTarget : virtual public GrSurface { 32 public: 33 // Make manual MSAA resolve publicly accessible from GrRenderTarget. 34 using GrSurface::setRequiresManualMSAAResolve; 35 using GrSurface::requiresManualMSAAResolve; 36 alwaysClearStencil()37 virtual bool alwaysClearStencil() const { return false; } 38 39 // GrSurface overrides asRenderTarget()40 GrRenderTarget* asRenderTarget() override { return this; } asRenderTarget()41 const GrRenderTarget* asRenderTarget() const override { return this; } 42 43 /** 44 * Returns the number of samples/pixel in the color buffer (One if non-MSAA). 45 */ numSamples()46 int numSamples() const { return fSampleCnt; } 47 48 virtual GrBackendRenderTarget getBackendRenderTarget() const = 0; 49 getStencilAttachment(bool useMSAASurface)50 GrAttachment* getStencilAttachment(bool useMSAASurface) const { 51 return (useMSAASurface) ? fMSAAStencilAttachment.get() : fStencilAttachment.get(); 52 } 53 getStencilAttachment()54 GrAttachment* getStencilAttachment() const { 55 return getStencilAttachment(this->numSamples() > 1); 56 } 57 58 // Checked when this object is asked to attach a stencil buffer. 59 virtual bool canAttemptStencilAttachment(bool useMSAASurface) const = 0; 60 61 void attachStencilAttachment(sk_sp<GrAttachment> stencil, bool useMSAASurface); 62 63 int numStencilBits(bool useMSAASurface) const; 64 65 /** 66 * Returns a unique key that identifies this render target's sample pattern. (Must be 67 * multisampled.) 68 */ 69 int getSamplePatternKey(); 70 71 /** 72 * Retrieves the per-pixel HW sample locations for this render target, and, as a by-product, the 73 * actual number of samples in use. (This may differ from fSampleCnt.) Sample locations are 74 * returned as 0..1 offsets relative to the top-left corner of the pixel. 75 */ 76 const skia_private::TArray<SkPoint>& getSampleLocations(); 77 78 protected: 79 GrRenderTarget(GrGpu*, 80 const SkISize&, 81 int sampleCount, 82 GrProtected, 83 std::string_view label, 84 sk_sp<GrAttachment> stenicl = nullptr); 85 ~GrRenderTarget() override; 86 87 // override of GrResource 88 void onAbandon() override; 89 void onRelease() override; 90 91 private: 92 // Allows the backends to perform any additional work that is required for attaching a 93 // GrAttachment. When this is called, the GrAttachment has already been put onto 94 // the GrRenderTarget. This function must return false if any failures occur when completing the 95 // stencil attachment. 96 virtual bool completeStencilAttachment(GrAttachment* stencil, bool useMSAASurface) = 0; 97 98 sk_sp<GrAttachment> fStencilAttachment; 99 sk_sp<GrAttachment> fMSAAStencilAttachment; 100 int fSampleCnt; 101 102 using INHERITED = GrSurface; 103 }; 104 105 #endif 106