blob: e0f11999f44148de0560d7b61ba9e7b825a38da4 [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 +000015
16/**
17 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
18 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon37dd3312014-11-03 08:47:23 -080019 * created by a createTexture with the kRenderTarget_SurfaceFlag flag.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000020 * Additionally, GrContext provides methods for creating GrRenderTargets
21 * that wrap externally created render targets.
22 */
bsalomon37dd3312014-11-03 08:47:23 -080023class GrRenderTarget : virtual public GrSurface {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000024public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000025 SK_DECLARE_INST_COUNT(GrRenderTarget)
bsalomon@google.com558a75b2011-08-08 17:01:14 +000026
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000027 // GrSurface overrides
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000028 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
bsalomon37dd3312014-11-03 08:47:23 -080029 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return this; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000030
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000031 // GrRenderTarget
32 /**
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000033 * If this RT is multisampled, this is the multisample buffer
34 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
35 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000036 virtual GrBackendObject getRenderTargetHandle() const = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000037
38 /**
39 * If this RT is multisampled, this is the buffer it is resolved to.
40 * Otherwise, same as getRenderTargetHandle().
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000041 * (In GL a separate FBO ID is used for the MSAA and resolved buffers)
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000042 * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
43 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000044 virtual GrBackendObject getRenderTargetResolvedHandle() const = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000045
46 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000047 * @return true if the surface is multisampled, false otherwise
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000048 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000049 bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000050
51 /**
52 * @return the number of samples-per-pixel or zero if non-MSAA.
53 */
robertphillips@google.come98ade42012-06-13 12:53:07 +000054 int numSamples() const { return fDesc.fSampleCnt; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000055
56 /**
57 * Call to indicate the multisample contents were modified such that the
58 * render target needs to be resolved before it can be used as texture. Gr
59 * tracks this for its own drawing and thus this only needs to be called
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000060 * when the render target has been modified outside of Gr. This has no
61 * effect on wrapped backend render targets.
62 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000063 * @param rect a rect bounding the area needing resolve. NULL indicates
64 * the whole RT needs resolving.
65 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000066 void flagAsNeedingResolve(const SkIRect* rect = NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000067
68 /**
69 * Call to override the region that needs to be resolved.
70 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000071 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000072
73 /**
74 * Call to indicate that GrRenderTarget was externally resolved. This may
75 * allow Gr to skip a redundant resolve step.
76 */
77 void flagAsResolved() { fResolveRect.setLargestInverted(); }
78
79 /**
80 * @return true if the GrRenderTarget requires MSAA resolving
81 */
82 bool needsResolve() const { return !fResolveRect.isEmpty(); }
83
84 /**
85 * Returns a rect bounding the region needing resolving.
86 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000087 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000088
bsalomon@google.com75f9f252012-01-31 13:35:56 +000089 /**
90 * If the render target is multisampled this will perform a multisample
91 * resolve. Any pending draws to the target are first flushed. This only
92 * applies to render targets that are associated with GrTextures. After the
93 * function returns the GrTexture will contain the resolved pixels.
94 */
95 void resolve();
96
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000097 /**
98 * Provide a performance hint that the render target's contents are allowed
99 * to become undefined.
100 */
101 void discard();
102
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000103 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000105 // resolve destination.
106 enum ResolveType {
107 kCanResolve_ResolveType,
108 kAutoResolves_ResolveType,
109 kCantResolve_ResolveType,
110 };
111 virtual ResolveType getResolveType() const = 0;
112
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000113 /**
114 * GrStencilBuffer is not part of the public API.
115 */
116 GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
117 void setStencilBuffer(GrStencilBuffer* stencilBuffer);
118
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000119protected:
120 GrRenderTarget(GrGpu* gpu,
bsalomon@google.com72830222013-01-23 20:25:22 +0000121 bool isWrapped,
bsalomonf2703d82014-10-28 14:33:06 -0700122 const GrSurfaceDesc& desc)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000123 : INHERITED(gpu, isWrapped, desc)
bsalomon37dd3312014-11-03 08:47:23 -0800124 , fStencilBuffer(NULL) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000125 fResolveRect.setLargestInverted();
126 }
127
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000128 // override of GrResource
129 virtual void onAbandon() SK_OVERRIDE;
130 virtual void onRelease() SK_OVERRIDE;
131
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000132private:
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000133 GrStencilBuffer* fStencilBuffer;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000134
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000135 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000136
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000137 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000138};
139
140#endif