bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | /* |
reed@google.com | dd335ae | 2012-12-13 19:24:05 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 8 | #ifndef GrRenderTarget_DEFINED |
| 9 | #define GrRenderTarget_DEFINED |
| 10 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 11 | #include "GrSurface.h" |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 12 | #include "SkRect.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 13 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 14 | class GrDrawTarget; |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 15 | class GrStencilAttachment; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 16 | class GrRenderTargetPriv; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * GrRenderTarget represents a 2D buffer of pixels that can be rendered to. |
| 20 | * A context's render target is set by setRenderTarget(). Render targets are |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 21 | * created by a createTexture with the kRenderTarget_SurfaceFlag flag. |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 22 | * Additionally, GrContext provides methods for creating GrRenderTargets |
| 23 | * that wrap externally created render targets. |
| 24 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 25 | class GrRenderTarget : virtual public GrSurface { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 26 | public: |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 27 | // GrSurface overrides |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | GrRenderTarget* asRenderTarget() override { return this; } |
| 29 | const GrRenderTarget* asRenderTarget() const override { return this; } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 30 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 31 | // GrRenderTarget |
| 32 | /** |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 33 | * On some hardware it is possible for a render target to have multisampling |
| 34 | * only in certain buffers. |
| 35 | * Enforce only two legal sample configs. |
| 36 | * kUnified_SampleConfig signifies multisampling in both color and stencil |
| 37 | * buffers and is available across all hardware. |
| 38 | * kStencil_SampleConfig means multisampling is present in stencil buffer |
| 39 | * only; this config requires hardware support of |
| 40 | * NV_framebuffer_mixed_samples. |
| 41 | */ |
| 42 | enum SampleConfig { |
| 43 | kUnified_SampleConfig = 0, |
| 44 | kStencil_SampleConfig = 1 |
| 45 | }; |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 48 | * @return true if the surface is multisampled in all buffers, |
| 49 | * false otherwise |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 50 | */ |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 51 | bool isUnifiedMultisampled() const { |
| 52 | if (fSampleConfig != kUnified_SampleConfig) { |
| 53 | return false; |
| 54 | } |
| 55 | return 0 != fDesc.fSampleCnt; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return true if the surface is multisampled in the stencil buffer, |
| 60 | * false otherwise |
| 61 | */ |
| 62 | bool isStencilBufferMultisampled() const { |
| 63 | return 0 != fDesc.fSampleCnt; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return the number of color samples-per-pixel, or zero if non-MSAA or |
| 68 | * multisampled in the stencil buffer only. |
| 69 | */ |
| 70 | int numColorSamples() const { |
| 71 | if (fSampleConfig == kUnified_SampleConfig) { |
| 72 | return fDesc.fSampleCnt; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return the number of stencil samples-per-pixel, or zero if non-MSAA. |
| 79 | */ |
| 80 | int numStencilSamples() const { |
| 81 | return fDesc.fSampleCnt; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return true if the surface is mixed sampled, false otherwise. |
| 86 | */ |
| 87 | bool hasMixedSamples() const { |
| 88 | SkASSERT(kStencil_SampleConfig != fSampleConfig || |
| 89 | this->isStencilBufferMultisampled()); |
| 90 | return kStencil_SampleConfig == fSampleConfig; |
| 91 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Call to indicate the multisample contents were modified such that the |
| 95 | * render target needs to be resolved before it can be used as texture. Gr |
| 96 | * tracks this for its own drawing and thus this only needs to be called |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 97 | * when the render target has been modified outside of Gr. This has no |
| 98 | * effect on wrapped backend render targets. |
| 99 | * |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 100 | * @param rect a rect bounding the area needing resolve. NULL indicates |
| 101 | * the whole RT needs resolving. |
| 102 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 103 | void flagAsNeedingResolve(const SkIRect* rect = NULL); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Call to override the region that needs to be resolved. |
| 107 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 108 | void overrideResolveRect(const SkIRect rect); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Call to indicate that GrRenderTarget was externally resolved. This may |
| 112 | * allow Gr to skip a redundant resolve step. |
| 113 | */ |
| 114 | void flagAsResolved() { fResolveRect.setLargestInverted(); } |
| 115 | |
| 116 | /** |
| 117 | * @return true if the GrRenderTarget requires MSAA resolving |
| 118 | */ |
| 119 | bool needsResolve() const { return !fResolveRect.isEmpty(); } |
| 120 | |
| 121 | /** |
| 122 | * Returns a rect bounding the region needing resolving. |
| 123 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 124 | const SkIRect& getResolveRect() const { return fResolveRect; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 125 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 126 | /** |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 127 | * Provide a performance hint that the render target's contents are allowed |
| 128 | * to become undefined. |
| 129 | */ |
| 130 | void discard(); |
| 131 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 132 | // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | // 0 in GL), or be unresolvable because the client didn't give us the |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 134 | // resolve destination. |
| 135 | enum ResolveType { |
| 136 | kCanResolve_ResolveType, |
| 137 | kAutoResolves_ResolveType, |
| 138 | kCantResolve_ResolveType, |
| 139 | }; |
| 140 | virtual ResolveType getResolveType() const = 0; |
| 141 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Return the native ID or handle to the rendertarget, depending on the |
| 144 | * platform. e.g. on OpenGL, return the FBO ID. |
| 145 | */ |
| 146 | virtual GrBackendObject getRenderTargetHandle() const = 0; |
| 147 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 148 | // Checked when this object is asked to attach a stencil buffer. |
| 149 | virtual bool canAttemptStencilAttachment() const = 0; |
| 150 | |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 151 | // Provides access to functions that aren't part of the public API. |
| 152 | GrRenderTargetPriv renderTargetPriv(); |
| 153 | const GrRenderTargetPriv renderTargetPriv() const; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 154 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 155 | void setLastDrawTarget(GrDrawTarget* dt); |
| 156 | GrDrawTarget* getLastDrawTarget() { return fLastDrawTarget; } |
| 157 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 158 | protected: |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 159 | GrRenderTarget(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc, |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 160 | SampleConfig sampleConfig, GrStencilAttachment* stencil = nullptr) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 161 | : INHERITED(gpu, lifeCycle, desc) |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 162 | , fStencilAttachment(stencil) |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 163 | , fSampleConfig(sampleConfig) |
| 164 | , fLastDrawTarget(nullptr) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 165 | fResolveRect.setLargestInverted(); |
| 166 | } |
| 167 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 168 | ~GrRenderTarget() override; |
| 169 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 170 | // override of GrResource |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 171 | void onAbandon() override; |
| 172 | void onRelease() override; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 174 | private: |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 175 | // Allows the backends to perform any additional work that is required for attaching a |
| 176 | // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto |
| 177 | // the GrRenderTarget. This function must return false if any failures occur when completing the |
| 178 | // stencil attachment. |
| 179 | virtual bool completeStencilAttachment() = 0; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 180 | |
| 181 | friend class GrRenderTargetPriv; |
| 182 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 183 | GrStencilAttachment* fStencilAttachment; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 184 | SampleConfig fSampleConfig; |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 185 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 186 | SkIRect fResolveRect; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 187 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 188 | // The last drawTarget that wrote to or is currently going to write to this renderTarget |
| 189 | // The drawTarget can be closed (e.g., no draw context is currently bound |
| 190 | // to this renderTarget). |
| 191 | // This back-pointer is required so that we can add a dependancy between |
| 192 | // the drawTarget used to create the current contents of this renderTarget |
| 193 | // and the drawTarget of a destination renderTarget to which this one is being drawn. |
| 194 | GrDrawTarget* fLastDrawTarget; |
| 195 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 196 | typedef GrSurface INHERITED; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 199 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 200 | #endif |