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