bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | /* |
| 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 21 | #include "GrRect.h" |
| 22 | #include "GrResource.h" |
| 23 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 24 | class GrStencilBuffer; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 25 | class 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 | */ |
| 34 | class GrRenderTarget : public GrResource { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 35 | public: |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 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.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 47 | * 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 61 | * @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 | */ |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame^] | 65 | GrPixelConfig config() const { return fConfig; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 68 | * @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.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 89 | 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 95 | |
| 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 157 | /** |
| 158 | * GrStencilBuffer is not part of the public API. |
| 159 | */ |
| 160 | GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; } |
| 161 | void setStencilBuffer(GrStencilBuffer* stencilBuffer); |
| 162 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 163 | protected: |
| 164 | GrRenderTarget(GrGpu* gpu, |
| 165 | GrTexture* texture, |
| 166 | int width, |
| 167 | int height, |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 168 | int allocatedWidth, |
| 169 | int allocatedHeight, |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 170 | GrPixelConfig config, |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 171 | int sampleCnt) |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 172 | : INHERITED(gpu) |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 173 | , fStencilBuffer(NULL) |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 174 | , fTexture(texture) |
| 175 | , fWidth(width) |
| 176 | , fHeight(height) |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 177 | , fAllocatedWidth(allocatedWidth) |
| 178 | , fAllocatedHeight(allocatedHeight) |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 179 | , fConfig(config) |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 180 | , fSampleCnt(sampleCnt) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 181 | fResolveRect.setLargestInverted(); |
| 182 | } |
| 183 | |
| 184 | friend class GrTexture; |
| 185 | // When a texture unrefs an owned rendertarget this func |
| 186 | // removes the back pointer. This could be done called from |
| 187 | // texture's destructor but would have to be done in derived |
| 188 | // class. By the time of texture base destructor it has already |
| 189 | // lost its pointer to the rt. |
| 190 | void onTextureReleaseRenderTarget() { |
| 191 | GrAssert(NULL != fTexture); |
| 192 | fTexture = NULL; |
| 193 | } |
| 194 | |
| 195 | private: |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 196 | GrStencilBuffer* fStencilBuffer; |
| 197 | GrTexture* fTexture; // not ref'ed |
| 198 | int fWidth; |
| 199 | int fHeight; |
| 200 | int fAllocatedWidth; |
| 201 | int fAllocatedHeight; |
| 202 | GrPixelConfig fConfig; |
| 203 | int fSampleCnt; |
| 204 | GrIRect fResolveRect; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 205 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 206 | typedef GrResource INHERITED; |
| 207 | }; |
| 208 | |
| 209 | #endif |