blob: 8983d53a251847325254441c86492436d555c088 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
reed@google.comdd335ae2012-12-13 19:24:05 +00002 * 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.comaa5b6732011-07-29 15:13:20 +00006 */
7
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00008#ifndef GrRenderTarget_DEFINED
9#define GrRenderTarget_DEFINED
10
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000011#include "GrSurface.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000012#include "SkRect.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000013
egdaniel8dc7c3a2015-04-16 11:22:42 -070014class GrStencilAttachment;
bsalomon6bc1b5f2015-02-23 09:06:38 -080015class GrRenderTargetPriv;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000016
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
bsalomon37dd3312014-11-03 08:47:23 -080020 * created by a createTexture with the kRenderTarget_SurfaceFlag flag.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021 * Additionally, GrContext provides methods for creating GrRenderTargets
22 * that wrap externally created render targets.
23 */
bsalomon37dd3312014-11-03 08:47:23 -080024class GrRenderTarget : virtual public GrSurface {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000025public:
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000026 // GrSurface overrides
mtklein36352bf2015-03-25 18:17:31 -070027 GrRenderTarget* asRenderTarget() override { return this; }
28 const GrRenderTarget* asRenderTarget() const override { return this; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000029
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000030 // GrRenderTarget
31 /**
vbuzinovdded6962015-06-12 08:59:45 -070032 * 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.com5bfc2172011-07-29 20:29:05 +000045
46 /**
vbuzinovdded6962015-06-12 08:59:45 -070047 * @return true if the surface is multisampled in all buffers,
48 * false otherwise
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000049 */
vbuzinovdded6962015-06-12 08:59:45 -070050 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.comaa5b6732011-07-29 15:13:20 +000091
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.com16e3dde2012-10-25 18:43:28 +000096 * when the render target has been modified outside of Gr. This has no
97 * effect on wrapped backend render targets.
98 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000099 * @param rect a rect bounding the area needing resolve. NULL indicates
100 * the whole RT needs resolving.
101 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000102 void flagAsNeedingResolve(const SkIRect* rect = NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000103
104 /**
105 * Call to override the region that needs to be resolved.
106 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000107 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000108
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.orgfd03d4a2013-07-17 21:39:42 +0000123 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000124
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000125 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000126 * Provide a performance hint that the render target's contents are allowed
127 * to become undefined.
128 */
129 void discard();
130
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000131 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000132 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000133 // resolve destination.
134 enum ResolveType {
135 kCanResolve_ResolveType,
136 kAutoResolves_ResolveType,
137 kCantResolve_ResolveType,
138 };
139 virtual ResolveType getResolveType() const = 0;
140
joshualitt81793412015-07-08 12:54:04 -0700141 /**
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
bsalomon6bc1b5f2015-02-23 09:06:38 -0800147 // Provides access to functions that aren't part of the public API.
148 GrRenderTargetPriv renderTargetPriv();
149 const GrRenderTargetPriv renderTargetPriv() const;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000150
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000151protected:
vbuzinovdded6962015-06-12 08:59:45 -0700152 GrRenderTarget(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc,
153 SampleConfig sampleConfig)
bsalomon5236cf42015-01-14 10:42:08 -0800154 : INHERITED(gpu, lifeCycle, desc)
vbuzinovdded6962015-06-12 08:59:45 -0700155 , fStencilAttachment(NULL)
156 , fSampleConfig(sampleConfig) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000157 fResolveRect.setLargestInverted();
158 }
159
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000160 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700161 void onAbandon() override;
162 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000163
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000164private:
bsalomon6bc1b5f2015-02-23 09:06:38 -0800165 // Checked when this object is asked to attach a stencil buffer.
166 virtual bool canAttemptStencilAttachment() const = 0;
167
168 friend class GrRenderTargetPriv;
169
egdaniel8dc7c3a2015-04-16 11:22:42 -0700170 GrStencilAttachment* fStencilAttachment;
vbuzinovdded6962015-06-12 08:59:45 -0700171 SampleConfig fSampleConfig;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000172
egdaniel8dc7c3a2015-04-16 11:22:42 -0700173 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000174
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000175 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000176};
177
vbuzinovdded6962015-06-12 08:59:45 -0700178
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000179#endif