blob: 13b1a3acf14744006dcf2ed5a40c2ec547c3abf6 [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
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021#include "GrRect.h"
22#include "GrResource.h"
23
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000024class GrStencilBuffer;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000025class 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 /**
bsalomon@google.com0168afc2011-08-08 13:21:05 +000047 * Retrieves the allocated width. It may differ from width for
48 * NPOT or min-RT size reasons.
49 * @return allocated width in pixels
50 */
51 int allocatedWidth() const { return fAllocatedWidth; }
52
53 /**
54 * Retrieves the allocated height. It may differ from height for
55 * NPOT or min-RT size reasons.
56 * @return allocated height in pixels
57 */
58 int allocatedHeight() const { return fAllocatedHeight; }
59
60 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000061 * @return the pixel config. Can be kUnknown_GrPixelConfig
62 * if client asked us to render to a target that has a pixel
63 * config that isn't equivalent with one of our configs.
64 */
65 int config() const { return fConfig; }
66
67 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000068 * @return the texture associated with the rendertarget, may be NULL.
69 */
70 GrTexture* asTexture() {return fTexture;}
71
72 /**
73 * If this RT is multisampled, this is the multisample buffer
74 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
75 */
76 virtual intptr_t getRenderTargetHandle() const = 0;
77
78 /**
79 * If this RT is multisampled, this is the buffer it is resolved to.
80 * Otherwise, same as getRenderTargetHandle().
81 * (In GL a separate FBO ID is used for the msaa and resolved buffers)
82 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
83 */
84 virtual intptr_t getRenderTargetResolvedHandle() const = 0;
85
86 /**
87 * @return true if the render target is multisampled, false otherwise
88 */
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000089 bool isMultisampled() const { return 0 != fSampleCnt; }
90
91 /**
92 * @return the number of samples-per-pixel or zero if non-MSAA.
93 */
94 int numSamples() const { return fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000095
96 /**
97 * Call to indicate the multisample contents were modified such that the
98 * render target needs to be resolved before it can be used as texture. Gr
99 * tracks this for its own drawing and thus this only needs to be called
100 * when the render target has been modified outside of Gr. Only meaningful
101 * for Gr-created RT/Textures and Platform RT/Textures created with the
102 * kGrCanResolve flag.
103 * @param rect a rect bounding the area needing resolve. NULL indicates
104 * the whole RT needs resolving.
105 */
106 void flagAsNeedingResolve(const GrIRect* rect = NULL);
107
108 /**
109 * Call to override the region that needs to be resolved.
110 */
111 void overrideResolveRect(const GrIRect rect);
112
113 /**
114 * Call to indicate that GrRenderTarget was externally resolved. This may
115 * allow Gr to skip a redundant resolve step.
116 */
117 void flagAsResolved() { fResolveRect.setLargestInverted(); }
118
119 /**
120 * @return true if the GrRenderTarget requires MSAA resolving
121 */
122 bool needsResolve() const { return !fResolveRect.isEmpty(); }
123
124 /**
125 * Returns a rect bounding the region needing resolving.
126 */
127 const GrIRect& getResolveRect() const { return fResolveRect; }
128
129 // GrResource overrides
130 virtual size_t sizeInBytes() const;
131
132 /**
133 * Reads a rectangle of pixels from the render target.
134 * @param left left edge of the rectangle to read (inclusive)
135 * @param top top edge of the rectangle to read (inclusive)
136 * @param width width of rectangle to read in pixels.
137 * @param height height of rectangle to read in pixels.
138 * @param config the pixel config of the destination buffer
139 * @param buffer memory to read the rectangle into.
140 *
141 * @return true if the read succeeded, false if not. The read can fail
142 * because of a unsupported pixel config.
143 */
144 bool readPixels(int left, int top, int width, int height,
145 GrPixelConfig config, void* buffer);
146
147 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
148 // 0 in GL), or be unresolvable because the client didn't give us the
149 // resolve destination.
150 enum ResolveType {
151 kCanResolve_ResolveType,
152 kAutoResolves_ResolveType,
153 kCantResolve_ResolveType,
154 };
155 virtual ResolveType getResolveType() const = 0;
156
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000157 /**
158 * GrStencilBuffer is not part of the public API.
159 */
160 GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
161 void setStencilBuffer(GrStencilBuffer* stencilBuffer);
162
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000163protected:
164 GrRenderTarget(GrGpu* gpu,
165 GrTexture* texture,
166 int width,
167 int height,
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000168 int allocatedWidth,
169 int allocatedHeight,
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000170 GrPixelConfig config,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000171 int sampleCnt)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172 : INHERITED(gpu)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000173 , fStencilBuffer(NULL)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000174 , fTexture(texture)
175 , fWidth(width)
176 , fHeight(height)
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000177 , fAllocatedWidth(allocatedWidth)
178 , fAllocatedHeight(allocatedHeight)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000179 , fConfig(config)
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000180 , fSampleCnt(sampleCnt)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000181 {
182 fResolveRect.setLargestInverted();
183 }
184
185 friend class GrTexture;
186 // When a texture unrefs an owned rendertarget this func
187 // removes the back pointer. This could be done called from
188 // texture's destructor but would have to be done in derived
189 // class. By the time of texture base destructor it has already
190 // lost its pointer to the rt.
191 void onTextureReleaseRenderTarget() {
192 GrAssert(NULL != fTexture);
193 fTexture = NULL;
194 }
195
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000196 GrStencilBuffer* fStencilBuffer;
197
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000198private:
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000199 GrTexture* fTexture; // not ref'ed
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000200 int fWidth;
201 int fHeight;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000202 int fAllocatedWidth;
203 int fAllocatedHeight;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000204 GrPixelConfig fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000205 int fSampleCnt;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000206 GrIRect fResolveRect;
207
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000208 typedef GrResource INHERITED;
209};
210
211#endif