blob: d69e5cada90bebc61116c2ea781c3c71f7e31658 [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"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000022#include "GrSurface.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000023
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 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000034class GrRenderTarget : public GrSurface {
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
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000038 // GrResource overrides
39 virtual size_t sizeInBytes() const SK_OVERRIDE;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000040
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000041 // GrSurface overrides
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000042 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000043 * @return the texture associated with the rendertarget, may be NULL.
44 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000045 virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; }
46 virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000047
48 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000049 * @return this render target.
50 */
51 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
52 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
53 return this;
54 }
55
56 /**
57 * Reads a rectangle of pixels from the render target.
58 * @param left left edge of the rectangle to read (inclusive)
59 * @param top top edge of the rectangle to read (inclusive)
60 * @param width width of rectangle to read in pixels.
61 * @param height height of rectangle to read in pixels.
62 * @param config the pixel config of the destination buffer
63 * @param buffer memory to read the rectangle into.
64 * @param rowBytes number of bytes bewtween consecutive rows. Zero
65 * means rows are tightly packed.
66 *
67 * @return true if the read succeeded, false if not. The read can fail
68 * because of an unsupported pixel config.
69 */
70 virtual bool readPixels(int left, int top, int width, int height,
71 GrPixelConfig config, void* buffer,
72 size_t rowBytes) SK_OVERRIDE;
73
74 /**
75 * Copy the src pixels [buffer, rowbytes, pixelconfig] into the render
76 * target at the specified rectangle.
77 * @param left left edge of the rectangle to write (inclusive)
78 * @param top top edge of the rectangle to write (inclusive)
79 * @param width width of rectangle to write in pixels.
80 * @param height height of rectangle to write in pixels.
81 * @param config the pixel config of the source buffer
82 * @param buffer memory to read the rectangle from.
83 * @param rowBytes number of bytes bewtween consecutive rows. Zero
84 * means rows are tightly packed.
85 */
86 virtual void writePixels(int left, int top, int width, int height,
87 GrPixelConfig config, const void* buffer,
88 size_t rowBytes) SK_OVERRIDE;
89
90 // GrRenderTarget
91 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092 * If this RT is multisampled, this is the multisample buffer
93 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
94 */
95 virtual intptr_t getRenderTargetHandle() const = 0;
96
97 /**
98 * If this RT is multisampled, this is the buffer it is resolved to.
99 * Otherwise, same as getRenderTargetHandle().
100 * (In GL a separate FBO ID is used for the msaa and resolved buffers)
101 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
102 */
103 virtual intptr_t getRenderTargetResolvedHandle() const = 0;
104
105 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000106 * @return true if the surface is multisampled, false otherwise
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000107 */
robertphillips@google.come98ade42012-06-13 12:53:07 +0000108 bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000109
110 /**
111 * @return the number of samples-per-pixel or zero if non-MSAA.
112 */
robertphillips@google.come98ade42012-06-13 12:53:07 +0000113 int numSamples() const { return fDesc.fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000114
115 /**
116 * Call to indicate the multisample contents were modified such that the
117 * render target needs to be resolved before it can be used as texture. Gr
118 * tracks this for its own drawing and thus this only needs to be called
119 * when the render target has been modified outside of Gr. Only meaningful
120 * for Gr-created RT/Textures and Platform RT/Textures created with the
121 * kGrCanResolve flag.
122 * @param rect a rect bounding the area needing resolve. NULL indicates
123 * the whole RT needs resolving.
124 */
125 void flagAsNeedingResolve(const GrIRect* rect = NULL);
126
127 /**
128 * Call to override the region that needs to be resolved.
129 */
130 void overrideResolveRect(const GrIRect rect);
131
132 /**
133 * Call to indicate that GrRenderTarget was externally resolved. This may
134 * allow Gr to skip a redundant resolve step.
135 */
136 void flagAsResolved() { fResolveRect.setLargestInverted(); }
137
138 /**
139 * @return true if the GrRenderTarget requires MSAA resolving
140 */
141 bool needsResolve() const { return !fResolveRect.isEmpty(); }
142
143 /**
144 * Returns a rect bounding the region needing resolving.
145 */
146 const GrIRect& getResolveRect() const { return fResolveRect; }
147
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000148 /**
149 * If the render target is multisampled this will perform a multisample
150 * resolve. Any pending draws to the target are first flushed. This only
151 * applies to render targets that are associated with GrTextures. After the
152 * function returns the GrTexture will contain the resolved pixels.
153 */
154 void resolve();
155
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000156 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
157 // 0 in GL), or be unresolvable because the client didn't give us the
158 // resolve destination.
159 enum ResolveType {
160 kCanResolve_ResolveType,
161 kAutoResolves_ResolveType,
162 kCantResolve_ResolveType,
163 };
164 virtual ResolveType getResolveType() const = 0;
165
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000166 /**
167 * GrStencilBuffer is not part of the public API.
168 */
169 GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
170 void setStencilBuffer(GrStencilBuffer* stencilBuffer);
171
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172protected:
173 GrRenderTarget(GrGpu* gpu,
174 GrTexture* texture,
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000175 const GrTextureDesc& desc)
176 : INHERITED(gpu, desc)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000177 , fStencilBuffer(NULL)
robertphillips@google.come98ade42012-06-13 12:53:07 +0000178 , fTexture(texture) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000179 fResolveRect.setLargestInverted();
180 }
181
182 friend class GrTexture;
183 // When a texture unrefs an owned rendertarget this func
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000184 // removes the back pointer. This could be called from
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000185 // texture's destructor but would have to be done in derived
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000186 // classes. By the time of texture base destructor it has already
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000187 // lost its pointer to the rt.
188 void onTextureReleaseRenderTarget() {
189 GrAssert(NULL != fTexture);
190 fTexture = NULL;
191 }
192
193private:
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000194 GrStencilBuffer* fStencilBuffer;
195 GrTexture* fTexture; // not ref'ed
robertphillips@google.come98ade42012-06-13 12:53:07 +0000196
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000197 GrIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000198
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000199 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000200};
201
202#endif