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" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 22 | #include "GrSurface.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 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 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 34 | class GrRenderTarget : public GrSurface { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 35 | public: |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 36 | SK_DECLARE_INST_COUNT(GrRenderTarget) |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 37 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 38 | // GrResource overrides |
| 39 | virtual size_t sizeInBytes() const SK_OVERRIDE; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 40 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 41 | // GrSurface overrides |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 42 | /** |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 43 | * @return the texture associated with the rendertarget, may be NULL. |
| 44 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 45 | virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; } |
| 46 | virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 49 | * @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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 92 | * 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.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 106 | * @return true if the surface is multisampled, false otherwise |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 107 | */ |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 108 | bool isMultisampled() const { return 0 != fDesc.fSampleCnt; } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * @return the number of samples-per-pixel or zero if non-MSAA. |
| 112 | */ |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 113 | int numSamples() const { return fDesc.fSampleCnt; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 114 | |
| 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.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 148 | /** |
| 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 156 | // 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.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 166 | /** |
| 167 | * GrStencilBuffer is not part of the public API. |
| 168 | */ |
| 169 | GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; } |
| 170 | void setStencilBuffer(GrStencilBuffer* stencilBuffer); |
| 171 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 172 | protected: |
| 173 | GrRenderTarget(GrGpu* gpu, |
| 174 | GrTexture* texture, |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 175 | const GrTextureDesc& desc) |
| 176 | : INHERITED(gpu, desc) |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 177 | , fStencilBuffer(NULL) |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 178 | , fTexture(texture) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 179 | fResolveRect.setLargestInverted(); |
| 180 | } |
| 181 | |
| 182 | friend class GrTexture; |
| 183 | // When a texture unrefs an owned rendertarget this func |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 184 | // removes the back pointer. This could be called from |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 185 | // texture's destructor but would have to be done in derived |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 186 | // classes. By the time of texture base destructor it has already |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 187 | // lost its pointer to the rt. |
| 188 | void onTextureReleaseRenderTarget() { |
| 189 | GrAssert(NULL != fTexture); |
| 190 | fTexture = NULL; |
| 191 | } |
| 192 | |
| 193 | private: |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 194 | GrStencilBuffer* fStencilBuffer; |
| 195 | GrTexture* fTexture; // not ref'ed |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 197 | GrIRect fResolveRect; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 198 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame^] | 199 | typedef GrSurface INHERITED; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | #endif |