bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | /* |
reed@google.com | dd335ae | 2012-12-13 19:24:05 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 8 | #ifndef GrRenderTarget_DEFINED |
| 9 | #define GrRenderTarget_DEFINED |
| 10 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 11 | #include "GrSurface.h" |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 12 | #include "SkRect.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 14 | class GrStencilBuffer; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | class GrTexture; |
| 16 | |
| 17 | /** |
| 18 | * GrRenderTarget represents a 2D buffer of pixels that can be rendered to. |
| 19 | * A context's render target is set by setRenderTarget(). Render targets are |
| 20 | * created by a createTexture with the kRenderTarget_TextureFlag flag. |
| 21 | * Additionally, GrContext provides methods for creating GrRenderTargets |
| 22 | * that wrap externally created render targets. |
| 23 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 24 | class GrRenderTarget : public GrSurface { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 25 | public: |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 26 | SK_DECLARE_INST_COUNT(GrRenderTarget) |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 27 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 28 | // GrResource overrides |
| 29 | virtual size_t sizeInBytes() const SK_OVERRIDE; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 30 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 31 | // GrSurface overrides |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 32 | /** |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 33 | * @return the texture associated with the render target, may be NULL. |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 34 | */ |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 35 | virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; } |
| 36 | virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 39 | * @return this render target. |
| 40 | */ |
| 41 | virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 42 | virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { |
| 43 | return this; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 44 | } |
| 45 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 46 | virtual bool readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 47 | GrPixelConfig config, |
| 48 | void* buffer, |
| 49 | size_t rowBytes = 0, |
| 50 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 51 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 52 | virtual void writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 53 | GrPixelConfig config, |
| 54 | const void* buffer, |
| 55 | size_t rowBytes = 0, |
| 56 | uint32_t pixelOpsFlags = 0) SK_OVERRIDE; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 57 | |
| 58 | // GrRenderTarget |
| 59 | /** |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 60 | * If this RT is multisampled, this is the multisample buffer |
| 61 | * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL) |
| 62 | */ |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 63 | virtual GrBackendObject getRenderTargetHandle() const = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * If this RT is multisampled, this is the buffer it is resolved to. |
| 67 | * Otherwise, same as getRenderTargetHandle(). |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 68 | * (In GL a separate FBO ID is used for the MSAA and resolved buffers) |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 69 | * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL) |
| 70 | */ |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 71 | virtual GrBackendObject getRenderTargetResolvedHandle() const = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 74 | * @return true if the surface is multisampled, false otherwise |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 75 | */ |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 76 | bool isMultisampled() const { return 0 != fDesc.fSampleCnt; } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * @return the number of samples-per-pixel or zero if non-MSAA. |
| 80 | */ |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 81 | int numSamples() const { return fDesc.fSampleCnt; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Call to indicate the multisample contents were modified such that the |
| 85 | * render target needs to be resolved before it can be used as texture. Gr |
| 86 | * tracks this for its own drawing and thus this only needs to be called |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 87 | * when the render target has been modified outside of Gr. This has no |
| 88 | * effect on wrapped backend render targets. |
| 89 | * |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 90 | * @param rect a rect bounding the area needing resolve. NULL indicates |
| 91 | * the whole RT needs resolving. |
| 92 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 93 | void flagAsNeedingResolve(const SkIRect* rect = NULL); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Call to override the region that needs to be resolved. |
| 97 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 98 | void overrideResolveRect(const SkIRect rect); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Call to indicate that GrRenderTarget was externally resolved. This may |
| 102 | * allow Gr to skip a redundant resolve step. |
| 103 | */ |
| 104 | void flagAsResolved() { fResolveRect.setLargestInverted(); } |
| 105 | |
| 106 | /** |
| 107 | * @return true if the GrRenderTarget requires MSAA resolving |
| 108 | */ |
| 109 | bool needsResolve() const { return !fResolveRect.isEmpty(); } |
| 110 | |
| 111 | /** |
| 112 | * Returns a rect bounding the region needing resolving. |
| 113 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 114 | const SkIRect& getResolveRect() const { return fResolveRect; } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 115 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 116 | /** |
| 117 | * If the render target is multisampled this will perform a multisample |
| 118 | * resolve. Any pending draws to the target are first flushed. This only |
| 119 | * applies to render targets that are associated with GrTextures. After the |
| 120 | * function returns the GrTexture will contain the resolved pixels. |
| 121 | */ |
| 122 | void resolve(); |
| 123 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 124 | // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 125 | // 0 in GL), or be unresolvable because the client didn't give us the |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 126 | // resolve destination. |
| 127 | enum ResolveType { |
| 128 | kCanResolve_ResolveType, |
| 129 | kAutoResolves_ResolveType, |
| 130 | kCantResolve_ResolveType, |
| 131 | }; |
| 132 | virtual ResolveType getResolveType() const = 0; |
| 133 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 134 | /** |
| 135 | * GrStencilBuffer is not part of the public API. |
| 136 | */ |
| 137 | GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; } |
| 138 | void setStencilBuffer(GrStencilBuffer* stencilBuffer); |
| 139 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 140 | protected: |
| 141 | GrRenderTarget(GrGpu* gpu, |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 142 | bool isWrapped, |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 143 | GrTexture* texture, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 144 | const GrTextureDesc& desc) |
| 145 | : INHERITED(gpu, isWrapped, desc) |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 146 | , fStencilBuffer(NULL) |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 147 | , fTexture(texture) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 148 | fResolveRect.setLargestInverted(); |
| 149 | } |
| 150 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 151 | // override of GrResource |
| 152 | virtual void onAbandon() SK_OVERRIDE; |
| 153 | virtual void onRelease() SK_OVERRIDE; |
| 154 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 155 | private: |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 156 | friend class GrTexture; |
| 157 | // called by ~GrTexture to remove the non-ref'ed back ptr. |
| 158 | void owningTextureDestroyed() { |
| 159 | GrAssert(NULL != fTexture); |
| 160 | fTexture = NULL; |
| 161 | } |
| 162 | |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 163 | GrStencilBuffer* fStencilBuffer; |
| 164 | GrTexture* fTexture; // not ref'ed |
robertphillips@google.com | e98ade4 | 2012-06-13 12:53:07 +0000 | [diff] [blame] | 165 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 166 | SkIRect fResolveRect; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 167 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 168 | typedef GrSurface INHERITED; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | #endif |