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 | |
| 21 | #include "GrClip.h" |
| 22 | #include "GrRect.h" |
| 23 | #include "GrResource.h" |
| 24 | |
| 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 { |
| 35 | |
| 36 | public: |
| 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 | /** |
| 47 | * @return the pixel config. Can be kUnknown_GrPixelConfig |
| 48 | * if client asked us to render to a target that has a pixel |
| 49 | * config that isn't equivalent with one of our configs. |
| 50 | */ |
| 51 | int config() const { return fConfig; } |
| 52 | |
| 53 | /** |
| 54 | * @return the number of stencil bits in the rendertarget |
| 55 | */ |
| 56 | int stencilBits() const { return fStencilBits; } |
| 57 | |
| 58 | /** |
| 59 | * @return the texture associated with the rendertarget, may be NULL. |
| 60 | */ |
| 61 | GrTexture* asTexture() {return fTexture;} |
| 62 | |
| 63 | /** |
| 64 | * If this RT is multisampled, this is the multisample buffer |
| 65 | * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL) |
| 66 | */ |
| 67 | virtual intptr_t getRenderTargetHandle() const = 0; |
| 68 | |
| 69 | /** |
| 70 | * If this RT is multisampled, this is the buffer it is resolved to. |
| 71 | * Otherwise, same as getRenderTargetHandle(). |
| 72 | * (In GL a separate FBO ID is used for the msaa and resolved buffers) |
| 73 | * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL) |
| 74 | */ |
| 75 | virtual intptr_t getRenderTargetResolvedHandle() const = 0; |
| 76 | |
| 77 | /** |
| 78 | * @return true if the render target is multisampled, false otherwise |
| 79 | */ |
| 80 | bool isMultisampled() { return fIsMultisampled; } |
| 81 | |
| 82 | /** |
| 83 | * Call to indicate the multisample contents were modified such that the |
| 84 | * render target needs to be resolved before it can be used as texture. Gr |
| 85 | * tracks this for its own drawing and thus this only needs to be called |
| 86 | * when the render target has been modified outside of Gr. Only meaningful |
| 87 | * for Gr-created RT/Textures and Platform RT/Textures created with the |
| 88 | * kGrCanResolve flag. |
| 89 | * @param rect a rect bounding the area needing resolve. NULL indicates |
| 90 | * the whole RT needs resolving. |
| 91 | */ |
| 92 | void flagAsNeedingResolve(const GrIRect* rect = NULL); |
| 93 | |
| 94 | /** |
| 95 | * Call to override the region that needs to be resolved. |
| 96 | */ |
| 97 | void overrideResolveRect(const GrIRect rect); |
| 98 | |
| 99 | /** |
| 100 | * Call to indicate that GrRenderTarget was externally resolved. This may |
| 101 | * allow Gr to skip a redundant resolve step. |
| 102 | */ |
| 103 | void flagAsResolved() { fResolveRect.setLargestInverted(); } |
| 104 | |
| 105 | /** |
| 106 | * @return true if the GrRenderTarget requires MSAA resolving |
| 107 | */ |
| 108 | bool needsResolve() const { return !fResolveRect.isEmpty(); } |
| 109 | |
| 110 | /** |
| 111 | * Returns a rect bounding the region needing resolving. |
| 112 | */ |
| 113 | const GrIRect& getResolveRect() const { return fResolveRect; } |
| 114 | |
| 115 | // GrResource overrides |
| 116 | virtual size_t sizeInBytes() const; |
| 117 | |
| 118 | /** |
| 119 | * Reads a rectangle of pixels from the render target. |
| 120 | * @param left left edge of the rectangle to read (inclusive) |
| 121 | * @param top top edge of the rectangle to read (inclusive) |
| 122 | * @param width width of rectangle to read in pixels. |
| 123 | * @param height height of rectangle to read in pixels. |
| 124 | * @param config the pixel config of the destination buffer |
| 125 | * @param buffer memory to read the rectangle into. |
| 126 | * |
| 127 | * @return true if the read succeeded, false if not. The read can fail |
| 128 | * because of a unsupported pixel config. |
| 129 | */ |
| 130 | bool readPixels(int left, int top, int width, int height, |
| 131 | GrPixelConfig config, void* buffer); |
| 132 | |
| 133 | // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO |
| 134 | // 0 in GL), or be unresolvable because the client didn't give us the |
| 135 | // resolve destination. |
| 136 | enum ResolveType { |
| 137 | kCanResolve_ResolveType, |
| 138 | kAutoResolves_ResolveType, |
| 139 | kCantResolve_ResolveType, |
| 140 | }; |
| 141 | virtual ResolveType getResolveType() const = 0; |
| 142 | |
| 143 | protected: |
| 144 | GrRenderTarget(GrGpu* gpu, |
| 145 | GrTexture* texture, |
| 146 | int width, |
| 147 | int height, |
| 148 | GrPixelConfig config, |
| 149 | int stencilBits, |
| 150 | bool isMultisampled) |
| 151 | : INHERITED(gpu) |
| 152 | , fTexture(texture) |
| 153 | , fWidth(width) |
| 154 | , fHeight(height) |
| 155 | , fConfig(config) |
| 156 | , fStencilBits(stencilBits) |
| 157 | , fIsMultisampled(isMultisampled) |
| 158 | { |
| 159 | fResolveRect.setLargestInverted(); |
| 160 | } |
| 161 | |
| 162 | friend class GrTexture; |
| 163 | // When a texture unrefs an owned rendertarget this func |
| 164 | // removes the back pointer. This could be done called from |
| 165 | // texture's destructor but would have to be done in derived |
| 166 | // class. By the time of texture base destructor it has already |
| 167 | // lost its pointer to the rt. |
| 168 | void onTextureReleaseRenderTarget() { |
| 169 | GrAssert(NULL != fTexture); |
| 170 | fTexture = NULL; |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | GrTexture* fTexture; // not ref'ed |
| 175 | int fWidth; |
| 176 | int fHeight; |
| 177 | GrPixelConfig fConfig; |
| 178 | int fStencilBits; |
| 179 | bool fIsMultisampled; |
| 180 | GrIRect fResolveRect; |
| 181 | |
| 182 | // GrGpu keeps a cached clip in the render target to avoid redundantly |
| 183 | // rendering the clip into the same stencil buffer. |
| 184 | friend class GrGpu; |
| 185 | GrClip fLastStencilClip; |
| 186 | |
| 187 | typedef GrResource INHERITED; |
| 188 | }; |
| 189 | |
| 190 | #endif |