blob: 91a67ae133e57e2d440c1c4ec890a0fdee67c86e [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.com4d73ac22012-06-13 18:54:08 +000026 SK_DECLARE_INST_COUNT(GrRenderTarget)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000027
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000028 // GrSurface overrides
mtklein36352bf2015-03-25 18:17:31 -070029 GrRenderTarget* asRenderTarget() override { return this; }
30 const GrRenderTarget* asRenderTarget() const override { return this; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000031
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000032 // GrRenderTarget
33 /**
vbuzinovdded6962015-06-12 08:59:45 -070034 * On some hardware it is possible for a render target to have multisampling
35 * only in certain buffers.
36 * Enforce only two legal sample configs.
37 * kUnified_SampleConfig signifies multisampling in both color and stencil
38 * buffers and is available across all hardware.
39 * kStencil_SampleConfig means multisampling is present in stencil buffer
40 * only; this config requires hardware support of
41 * NV_framebuffer_mixed_samples.
42 */
43 enum SampleConfig {
44 kUnified_SampleConfig = 0,
45 kStencil_SampleConfig = 1
46 };
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000047
48 /**
vbuzinovdded6962015-06-12 08:59:45 -070049 * @return true if the surface is multisampled in all buffers,
50 * false otherwise
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000051 */
vbuzinovdded6962015-06-12 08:59:45 -070052 bool isUnifiedMultisampled() const {
53 if (fSampleConfig != kUnified_SampleConfig) {
54 return false;
55 }
56 return 0 != fDesc.fSampleCnt;
57 }
58
59 /**
60 * @return true if the surface is multisampled in the stencil buffer,
61 * false otherwise
62 */
63 bool isStencilBufferMultisampled() const {
64 return 0 != fDesc.fSampleCnt;
65 }
66
67 /**
68 * @return the number of color samples-per-pixel, or zero if non-MSAA or
69 * multisampled in the stencil buffer only.
70 */
71 int numColorSamples() const {
72 if (fSampleConfig == kUnified_SampleConfig) {
73 return fDesc.fSampleCnt;
74 }
75 return 0;
76 }
77
78 /**
79 * @return the number of stencil samples-per-pixel, or zero if non-MSAA.
80 */
81 int numStencilSamples() const {
82 return fDesc.fSampleCnt;
83 }
84
85 /**
86 * @return true if the surface is mixed sampled, false otherwise.
87 */
88 bool hasMixedSamples() const {
89 SkASSERT(kStencil_SampleConfig != fSampleConfig ||
90 this->isStencilBufferMultisampled());
91 return kStencil_SampleConfig == fSampleConfig;
92 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000093
94 /**
95 * Call to indicate the multisample contents were modified such that the
96 * render target needs to be resolved before it can be used as texture. Gr
97 * tracks this for its own drawing and thus this only needs to be called
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000098 * when the render target has been modified outside of Gr. This has no
99 * effect on wrapped backend render targets.
100 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000101 * @param rect a rect bounding the area needing resolve. NULL indicates
102 * the whole RT needs resolving.
103 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000104 void flagAsNeedingResolve(const SkIRect* rect = NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000105
106 /**
107 * Call to override the region that needs to be resolved.
108 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000109 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000110
111 /**
112 * Call to indicate that GrRenderTarget was externally resolved. This may
113 * allow Gr to skip a redundant resolve step.
114 */
115 void flagAsResolved() { fResolveRect.setLargestInverted(); }
116
117 /**
118 * @return true if the GrRenderTarget requires MSAA resolving
119 */
120 bool needsResolve() const { return !fResolveRect.isEmpty(); }
121
122 /**
123 * Returns a rect bounding the region needing resolving.
124 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000125 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000126
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000127 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000128 * Provide a performance hint that the render target's contents are allowed
129 * to become undefined.
130 */
131 void discard();
132
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000133 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000134 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000135 // resolve destination.
136 enum ResolveType {
137 kCanResolve_ResolveType,
138 kAutoResolves_ResolveType,
139 kCantResolve_ResolveType,
140 };
141 virtual ResolveType getResolveType() const = 0;
142
bsalomon6bc1b5f2015-02-23 09:06:38 -0800143 // Provides access to functions that aren't part of the public API.
144 GrRenderTargetPriv renderTargetPriv();
145 const GrRenderTargetPriv renderTargetPriv() const;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000146
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000147protected:
vbuzinovdded6962015-06-12 08:59:45 -0700148 GrRenderTarget(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc,
149 SampleConfig sampleConfig)
bsalomon5236cf42015-01-14 10:42:08 -0800150 : INHERITED(gpu, lifeCycle, desc)
vbuzinovdded6962015-06-12 08:59:45 -0700151 , fStencilAttachment(NULL)
152 , fSampleConfig(sampleConfig) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000153 fResolveRect.setLargestInverted();
154 }
155
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000156 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700157 void onAbandon() override;
158 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000159
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000160private:
bsalomon6bc1b5f2015-02-23 09:06:38 -0800161 // Checked when this object is asked to attach a stencil buffer.
162 virtual bool canAttemptStencilAttachment() const = 0;
163
164 friend class GrRenderTargetPriv;
165
egdaniel8dc7c3a2015-04-16 11:22:42 -0700166 GrStencilAttachment* fStencilAttachment;
vbuzinovdded6962015-06-12 08:59:45 -0700167 SampleConfig fSampleConfig;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000168
egdaniel8dc7c3a2015-04-16 11:22:42 -0700169 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000170
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000171 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172};
173
vbuzinovdded6962015-06-12 08:59:45 -0700174
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000175#endif