blob: c26ca4bb7e8c919d168d5586f0337ba42b4ff36f [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 {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000035public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000036 SK_DECLARE_INST_COUNT(GrRenderTarget)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000037
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000038 /**
39 * @return the width of the rendertarget
40 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000041 int width() const { return fDesc.fWidth; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000042 /**
43 * @return the height of the rendertarget
44 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000045 int height() const { return fDesc.fHeight; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000046
47 /**
48 * @return the pixel config. Can be kUnknown_GrPixelConfig
49 * if client asked us to render to a target that has a pixel
50 * config that isn't equivalent with one of our configs.
51 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000052 GrPixelConfig config() const { return fDesc.fConfig; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000053
54 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000055 * @return the texture associated with the rendertarget, may be NULL.
56 */
57 GrTexture* asTexture() {return fTexture;}
58
59 /**
60 * If this RT is multisampled, this is the multisample buffer
61 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
62 */
63 virtual intptr_t getRenderTargetHandle() const = 0;
64
65 /**
66 * If this RT is multisampled, this is the buffer it is resolved to.
67 * Otherwise, same as getRenderTargetHandle().
68 * (In GL a separate FBO ID is used for the msaa and resolved buffers)
69 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
70 */
71 virtual intptr_t getRenderTargetResolvedHandle() const = 0;
72
73 /**
74 * @return true if the render target is multisampled, false otherwise
75 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000076 bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000077
78 /**
79 * @return the number of samples-per-pixel or zero if non-MSAA.
80 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000081 int numSamples() const { return fDesc.fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000082
83 /**
84 * Call to indicate the multisample contents were modified such that the
85 * render target needs to be resolved before it can be used as texture. Gr
86 * tracks this for its own drawing and thus this only needs to be called
87 * when the render target has been modified outside of Gr. Only meaningful
88 * for Gr-created RT/Textures and Platform RT/Textures created with the
89 * kGrCanResolve flag.
90 * @param rect a rect bounding the area needing resolve. NULL indicates
91 * the whole RT needs resolving.
92 */
93 void flagAsNeedingResolve(const GrIRect* rect = NULL);
94
95 /**
96 * Call to override the region that needs to be resolved.
97 */
98 void overrideResolveRect(const GrIRect rect);
99
100 /**
101 * Call to indicate that GrRenderTarget was externally resolved. This may
102 * allow Gr to skip a redundant resolve step.
103 */
104 void flagAsResolved() { fResolveRect.setLargestInverted(); }
105
106 /**
107 * @return true if the GrRenderTarget requires MSAA resolving
108 */
109 bool needsResolve() const { return !fResolveRect.isEmpty(); }
110
111 /**
112 * Returns a rect bounding the region needing resolving.
113 */
114 const GrIRect& getResolveRect() const { return fResolveRect; }
115
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000116 /**
117 * If the render target is multisampled this will perform a multisample
118 * resolve. Any pending draws to the target are first flushed. This only
119 * applies to render targets that are associated with GrTextures. After the
120 * function returns the GrTexture will contain the resolved pixels.
121 */
122 void resolve();
123
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000124 // GrResource overrides
125 virtual size_t sizeInBytes() const;
126
127 /**
128 * Reads a rectangle of pixels from the render target.
129 * @param left left edge of the rectangle to read (inclusive)
130 * @param top top edge of the rectangle to read (inclusive)
131 * @param width width of rectangle to read in pixels.
132 * @param height height of rectangle to read in pixels.
133 * @param config the pixel config of the destination buffer
134 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000135 * @param rowBytes number of bytes bewtween consecutive rows. Zero
136 * means rows are tightly packed.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000137 *
138 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000139 * because of an unsupported pixel config.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000140 */
141 bool readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000142 GrPixelConfig config, void* buffer, size_t rowBytes);
143
144 /**
145 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the render
146 * target at the specified rectangle.
147 * @param left left edge of the rectangle to write (inclusive)
148 * @param top top edge of the rectangle to write (inclusive)
149 * @param width width of rectangle to write in pixels.
150 * @param height height of rectangle to write in pixels.
151 * @param config the pixel config of the source buffer
152 * @param buffer memory to read the rectangle from.
153 * @param rowBytes number of bytes bewtween consecutive rows. Zero
154 * means rows are tightly packed.
155 */
156 void writePixels(int left, int top, int width, int height,
157 GrPixelConfig config, const void* buffer, size_t rowBytes);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000158
159 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
160 // 0 in GL), or be unresolvable because the client didn't give us the
161 // resolve destination.
162 enum ResolveType {
163 kCanResolve_ResolveType,
164 kAutoResolves_ResolveType,
165 kCantResolve_ResolveType,
166 };
167 virtual ResolveType getResolveType() const = 0;
168
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000169 /**
170 * GrStencilBuffer is not part of the public API.
171 */
172 GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
173 void setStencilBuffer(GrStencilBuffer* stencilBuffer);
174
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000175protected:
176 GrRenderTarget(GrGpu* gpu,
177 GrTexture* texture,
178 int width,
179 int height,
180 GrPixelConfig config,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000181 int sampleCnt)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000182 : INHERITED(gpu)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000183 , fStencilBuffer(NULL)
robertphillips@google.come98ade42012-06-13 12:53:07 +0000184 , fTexture(texture) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000185 fResolveRect.setLargestInverted();
robertphillips@google.come98ade42012-06-13 12:53:07 +0000186
187 fDesc.fWidth = width;
188 fDesc.fHeight = height;
189 fDesc.fConfig = config;
190 fDesc.fSampleCnt = sampleCnt;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000191 }
192
193 friend class GrTexture;
194 // When a texture unrefs an owned rendertarget this func
195 // removes the back pointer. This could be done called from
196 // texture's destructor but would have to be done in derived
197 // class. By the time of texture base destructor it has already
198 // lost its pointer to the rt.
199 void onTextureReleaseRenderTarget() {
200 GrAssert(NULL != fTexture);
201 fTexture = NULL;
202 }
203
204private:
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000205 GrStencilBuffer* fStencilBuffer;
206 GrTexture* fTexture; // not ref'ed
robertphillips@google.come98ade42012-06-13 12:53:07 +0000207
208 GrTextureDesc fDesc;
209
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000210 GrIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000211
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000212 typedef GrResource INHERITED;
213};
214
215#endif