blob: eab65b5b1d7cf76ac4112623a14511328eb6bd5b [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
reed@google.comdd335ae2012-12-13 19:24:05 +00002 * 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.comaa5b6732011-07-29 15:13:20 +00006 */
7
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00008#ifndef GrRenderTarget_DEFINED
9#define GrRenderTarget_DEFINED
10
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000011#include "GrSurface.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000012#include "SkRect.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000013
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000014class GrStencilBuffer;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015class 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.com7d501ab2012-06-21 21:09:06 +000024class GrRenderTarget : public GrSurface {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000025public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000026 SK_DECLARE_INST_COUNT(GrRenderTarget)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000027
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000028 // GrResource overrides
29 virtual size_t sizeInBytes() const SK_OVERRIDE;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000030
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000031 // GrSurface overrides
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000032 /**
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000033 * @return the texture associated with the render target, may be NULL.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000034 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000035 virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; }
36 virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000037
38 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000039 * @return this render target.
40 */
41 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
43 return this;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000044 }
45
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000046 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000047 GrPixelConfig config,
48 void* buffer,
49 size_t rowBytes = 0,
50 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000051
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000052 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000053 GrPixelConfig config,
54 const void* buffer,
55 size_t rowBytes = 0,
56 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000057
58 // GrRenderTarget
59 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000060 * 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.com16e3dde2012-10-25 18:43:28 +000063 virtual GrBackendObject getRenderTargetHandle() const = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000064
65 /**
66 * If this RT is multisampled, this is the buffer it is resolved to.
67 * Otherwise, same as getRenderTargetHandle().
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000068 * (In GL a separate FBO ID is used for the MSAA and resolved buffers)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000069 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
70 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000071 virtual GrBackendObject getRenderTargetResolvedHandle() const = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000072
73 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000074 * @return true if the surface is multisampled, false otherwise
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000075 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000076 bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000077
78 /**
79 * @return the number of samples-per-pixel or zero if non-MSAA.
80 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000081 int numSamples() const { return fDesc.fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000082
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.com16e3dde2012-10-25 18:43:28 +000087 * when the render target has been modified outside of Gr. This has no
88 * effect on wrapped backend render targets.
89 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000090 * @param rect a rect bounding the area needing resolve. NULL indicates
91 * the whole RT needs resolving.
92 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000093 void flagAsNeedingResolve(const SkIRect* rect = NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000094
95 /**
96 * Call to override the region that needs to be resolved.
97 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000098 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000099
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.orgfd03d4a2013-07-17 21:39:42 +0000114 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000115
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000116 /**
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.comaa5b6732011-07-29 15:13:20 +0000124 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000126 // resolve destination.
127 enum ResolveType {
128 kCanResolve_ResolveType,
129 kAutoResolves_ResolveType,
130 kCantResolve_ResolveType,
131 };
132 virtual ResolveType getResolveType() const = 0;
133
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000134 /**
135 * GrStencilBuffer is not part of the public API.
136 */
137 GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
138 void setStencilBuffer(GrStencilBuffer* stencilBuffer);
139
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000140protected:
141 GrRenderTarget(GrGpu* gpu,
bsalomon@google.com72830222013-01-23 20:25:22 +0000142 bool isWrapped,
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000143 GrTexture* texture,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000144 const GrTextureDesc& desc)
145 : INHERITED(gpu, isWrapped, desc)
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000146 , fStencilBuffer(NULL)
robertphillips@google.come98ade42012-06-13 12:53:07 +0000147 , fTexture(texture) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000148 fResolveRect.setLargestInverted();
149 }
150
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000151 // override of GrResource
152 virtual void onAbandon() SK_OVERRIDE;
153 virtual void onRelease() SK_OVERRIDE;
154
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000155private:
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000156 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.com558a75b2011-08-08 17:01:14 +0000163 GrStencilBuffer* fStencilBuffer;
164 GrTexture* fTexture; // not ref'ed
robertphillips@google.come98ade42012-06-13 12:53:07 +0000165
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000166 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000167
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000168 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000169};
170
171#endif