blob: d6e8ee0be87ab17cb17f98c72cd0d728390b630a [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; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
53 return this;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000054 }
55
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000056 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000057 GrPixelConfig config,
58 void* buffer,
59 size_t rowBytes = 0,
60 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000061
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000062 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000063 GrPixelConfig config,
64 const void* buffer,
65 size_t rowBytes = 0,
66 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000067
68 // GrRenderTarget
69 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000070 * If this RT is multisampled, this is the multisample buffer
71 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
72 */
73 virtual intptr_t getRenderTargetHandle() const = 0;
74
75 /**
76 * If this RT is multisampled, this is the buffer it is resolved to.
77 * Otherwise, same as getRenderTargetHandle().
78 * (In GL a separate FBO ID is used for the msaa and resolved buffers)
79 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
80 */
81 virtual intptr_t getRenderTargetResolvedHandle() const = 0;
82
83 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000084 * @return true if the surface is multisampled, false otherwise
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000085 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000086 bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000087
88 /**
89 * @return the number of samples-per-pixel or zero if non-MSAA.
90 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000091 int numSamples() const { return fDesc.fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092
93 /**
94 * Call to indicate the multisample contents were modified such that the
95 * render target needs to be resolved before it can be used as texture. Gr
96 * tracks this for its own drawing and thus this only needs to be called
97 * when the render target has been modified outside of Gr. Only meaningful
98 * for Gr-created RT/Textures and Platform RT/Textures created with the
99 * kGrCanResolve flag.
100 * @param rect a rect bounding the area needing resolve. NULL indicates
101 * the whole RT needs resolving.
102 */
103 void flagAsNeedingResolve(const GrIRect* rect = NULL);
104
105 /**
106 * Call to override the region that needs to be resolved.
107 */
108 void overrideResolveRect(const GrIRect rect);
109
110 /**
111 * Call to indicate that GrRenderTarget was externally resolved. This may
112 * allow Gr to skip a redundant resolve step.
113 */
114 void flagAsResolved() { fResolveRect.setLargestInverted(); }
115
116 /**
117 * @return true if the GrRenderTarget requires MSAA resolving
118 */
119 bool needsResolve() const { return !fResolveRect.isEmpty(); }
120
121 /**
122 * Returns a rect bounding the region needing resolving.
123 */
124 const GrIRect& getResolveRect() const { return fResolveRect; }
125
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000126 /**
127 * If the render target is multisampled this will perform a multisample
128 * resolve. Any pending draws to the target are first flushed. This only
129 * applies to render targets that are associated with GrTextures. After the
130 * function returns the GrTexture will contain the resolved pixels.
131 */
132 void resolve();
133
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000134 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000135 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000136 // resolve destination.
137 enum ResolveType {
138 kCanResolve_ResolveType,
139 kAutoResolves_ResolveType,
140 kCantResolve_ResolveType,
141 };
142 virtual ResolveType getResolveType() const = 0;
143
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000144 /**
145 * GrStencilBuffer is not part of the public API.
146 */
147 GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
148 void setStencilBuffer(GrStencilBuffer* stencilBuffer);
149
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000150protected:
151 GrRenderTarget(GrGpu* gpu,
152 GrTexture* texture,
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000153 const GrTextureDesc& desc)
154 : INHERITED(gpu, desc)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000155 , fStencilBuffer(NULL)
robertphillips@google.come98ade42012-06-13 12:53:07 +0000156 , fTexture(texture) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000157 fResolveRect.setLargestInverted();
158 }
159
160 friend class GrTexture;
161 // When a texture unrefs an owned rendertarget this func
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000162 // removes the back pointer. This could be called from
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000163 // texture's destructor but would have to be done in derived
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000164 // classes. By the time of texture base destructor it has already
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000165 // lost its pointer to the rt.
166 void onTextureReleaseRenderTarget() {
167 GrAssert(NULL != fTexture);
168 fTexture = NULL;
169 }
170
171private:
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000172 GrStencilBuffer* fStencilBuffer;
173 GrTexture* fTexture; // not ref'ed
robertphillips@google.come98ade42012-06-13 12:53:07 +0000174
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000175 GrIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000176
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000177 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000178};
179
180#endif