blob: a5616b989599a27d6d3de4dff06d1bc1bc001269 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
2 Copyright 2011 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrRenderTarget_DEFINED
19#define GrRenderTarget_DEFINED
20
21#include "GrClip.h"
22#include "GrRect.h"
23#include "GrResource.h"
24
25class GrTexture;
26
27/**
28 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
29 * A context's render target is set by setRenderTarget(). Render targets are
30 * created by a createTexture with the kRenderTarget_TextureFlag flag.
31 * Additionally, GrContext provides methods for creating GrRenderTargets
32 * that wrap externally created render targets.
33 */
34class GrRenderTarget : public GrResource {
35
36public:
37 /**
38 * @return the width of the rendertarget
39 */
40 int width() const { return fWidth; }
41 /**
42 * @return the height of the rendertarget
43 */
44 int height() const { return fHeight; }
45
46 /**
47 * @return the pixel config. Can be kUnknown_GrPixelConfig
48 * if client asked us to render to a target that has a pixel
49 * config that isn't equivalent with one of our configs.
50 */
51 int config() const { return fConfig; }
52
53 /**
54 * @return the number of stencil bits in the rendertarget
55 */
56 int stencilBits() const { return fStencilBits; }
57
58 /**
59 * @return the texture associated with the rendertarget, may be NULL.
60 */
61 GrTexture* asTexture() {return fTexture;}
62
63 /**
64 * If this RT is multisampled, this is the multisample buffer
65 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
66 */
67 virtual intptr_t getRenderTargetHandle() const = 0;
68
69 /**
70 * If this RT is multisampled, this is the buffer it is resolved to.
71 * Otherwise, same as getRenderTargetHandle().
72 * (In GL a separate FBO ID is used for the msaa and resolved buffers)
73 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
74 */
75 virtual intptr_t getRenderTargetResolvedHandle() const = 0;
76
77 /**
78 * @return true if the render target is multisampled, false otherwise
79 */
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000080 bool isMultisampled() const { return 0 != fSampleCnt; }
81
82 /**
83 * @return the number of samples-per-pixel or zero if non-MSAA.
84 */
85 int numSamples() const { return fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000086
87 /**
88 * Call to indicate the multisample contents were modified such that the
89 * render target needs to be resolved before it can be used as texture. Gr
90 * tracks this for its own drawing and thus this only needs to be called
91 * when the render target has been modified outside of Gr. Only meaningful
92 * for Gr-created RT/Textures and Platform RT/Textures created with the
93 * kGrCanResolve flag.
94 * @param rect a rect bounding the area needing resolve. NULL indicates
95 * the whole RT needs resolving.
96 */
97 void flagAsNeedingResolve(const GrIRect* rect = NULL);
98
99 /**
100 * Call to override the region that needs to be resolved.
101 */
102 void overrideResolveRect(const GrIRect rect);
103
104 /**
105 * Call to indicate that GrRenderTarget was externally resolved. This may
106 * allow Gr to skip a redundant resolve step.
107 */
108 void flagAsResolved() { fResolveRect.setLargestInverted(); }
109
110 /**
111 * @return true if the GrRenderTarget requires MSAA resolving
112 */
113 bool needsResolve() const { return !fResolveRect.isEmpty(); }
114
115 /**
116 * Returns a rect bounding the region needing resolving.
117 */
118 const GrIRect& getResolveRect() const { return fResolveRect; }
119
120 // GrResource overrides
121 virtual size_t sizeInBytes() const;
122
123 /**
124 * Reads a rectangle of pixels from the render target.
125 * @param left left edge of the rectangle to read (inclusive)
126 * @param top top edge of the rectangle to read (inclusive)
127 * @param width width of rectangle to read in pixels.
128 * @param height height of rectangle to read in pixels.
129 * @param config the pixel config of the destination buffer
130 * @param buffer memory to read the rectangle into.
131 *
132 * @return true if the read succeeded, false if not. The read can fail
133 * because of a unsupported pixel config.
134 */
135 bool readPixels(int left, int top, int width, int height,
136 GrPixelConfig config, void* buffer);
137
138 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
139 // 0 in GL), or be unresolvable because the client didn't give us the
140 // resolve destination.
141 enum ResolveType {
142 kCanResolve_ResolveType,
143 kAutoResolves_ResolveType,
144 kCantResolve_ResolveType,
145 };
146 virtual ResolveType getResolveType() const = 0;
147
148protected:
149 GrRenderTarget(GrGpu* gpu,
150 GrTexture* texture,
151 int width,
152 int height,
153 GrPixelConfig config,
154 int stencilBits,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000155 int sampleCnt)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000156 : INHERITED(gpu)
157 , fTexture(texture)
158 , fWidth(width)
159 , fHeight(height)
160 , fConfig(config)
161 , fStencilBits(stencilBits)
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000162 , fSampleCnt(sampleCnt)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000163 {
164 fResolveRect.setLargestInverted();
165 }
166
167 friend class GrTexture;
168 // When a texture unrefs an owned rendertarget this func
169 // removes the back pointer. This could be done called from
170 // texture's destructor but would have to be done in derived
171 // class. By the time of texture base destructor it has already
172 // lost its pointer to the rt.
173 void onTextureReleaseRenderTarget() {
174 GrAssert(NULL != fTexture);
175 fTexture = NULL;
176 }
177
178private:
179 GrTexture* fTexture; // not ref'ed
180 int fWidth;
181 int fHeight;
182 GrPixelConfig fConfig;
183 int fStencilBits;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000184 int fSampleCnt;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000185 GrIRect fResolveRect;
186
187 // GrGpu keeps a cached clip in the render target to avoid redundantly
188 // rendering the clip into the same stencil buffer.
189 friend class GrGpu;
190 GrClip fLastStencilClip;
191
192 typedef GrResource INHERITED;
193};
194
195#endif