blob: 188e6d1e0f76bea79531638d4e25bfa9d263ab31 [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
robertphillips76948d42016-05-04 12:47:41 -070014class GrCaps;
Robert Phillipsf2361d22016-10-25 14:20:06 -040015class GrRenderTargetOpList;
bsalomon6bc1b5f2015-02-23 09:06:38 -080016class GrRenderTargetPriv;
Robert Phillipsf2361d22016-10-25 14:20:06 -040017class GrStencilAttachment;
Robert Phillipsb67821d2017-12-13 15:00:45 -050018class GrBackendRenderTarget;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000019
20/**
21 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
22 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon37dd3312014-11-03 08:47:23 -080023 * created by a createTexture with the kRenderTarget_SurfaceFlag flag.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000024 * Additionally, GrContext provides methods for creating GrRenderTargets
25 * that wrap externally created render targets.
26 */
bsalomon37dd3312014-11-03 08:47:23 -080027class GrRenderTarget : virtual public GrSurface {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000028public:
Robert Phillipscb2e2352017-08-30 16:44:40 -040029 virtual bool alwaysClearStencil() const { return false; }
30
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000031 // GrSurface overrides
mtklein36352bf2015-03-25 18:17:31 -070032 GrRenderTarget* asRenderTarget() override { return this; }
33 const GrRenderTarget* asRenderTarget() const override { return this; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000034
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000035 // GrRenderTarget
Brian Salomonbdecacf2018-02-02 20:32:49 -050036 bool isStencilBufferMultisampled() const { return fSampleCnt > 1; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000037
Brian Salomon7c8460e2017-05-12 11:36:10 -040038 GrFSAAType fsaaType() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050039 SkASSERT(fSampleCnt >= 1);
40 if (fSampleCnt <= 1) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040041 SkASSERT(!(fFlags & GrRenderTargetFlags::kMixedSampled));
Brian Salomon7c8460e2017-05-12 11:36:10 -040042 return GrFSAAType::kNone;
43 }
Robert Phillipsc4f0a822017-06-13 08:11:36 -040044 return (fFlags & GrRenderTargetFlags::kMixedSampled) ? GrFSAAType::kMixedSamples
45 : GrFSAAType::kUnifiedMSAA;
Brian Salomon7c8460e2017-05-12 11:36:10 -040046 }
vbuzinovdded6962015-06-12 08:59:45 -070047
48 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -050049 * Returns the number of samples/pixel in the stencil buffer (One if non-MSAA).
vbuzinovdded6962015-06-12 08:59:45 -070050 */
Brian Salomond34edf32017-05-19 15:45:48 -040051 int numStencilSamples() const { return fSampleCnt; }
vbuzinovdded6962015-06-12 08:59:45 -070052
53 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -050054 * Returns the number of samples/pixel in the color buffer (One if non-MSAA or mixed sampled).
vbuzinovdded6962015-06-12 08:59:45 -070055 */
Brian Salomon7c8460e2017-05-12 11:36:10 -040056 int numColorSamples() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050057 return GrFSAAType::kMixedSamples == this->fsaaType() ? 1 : fSampleCnt;
Brian Salomon7c8460e2017-05-12 11:36:10 -040058 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000059
60 /**
61 * Call to indicate the multisample contents were modified such that the
62 * render target needs to be resolved before it can be used as texture. Gr
63 * tracks this for its own drawing and thus this only needs to be called
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000064 * when the render target has been modified outside of Gr. This has no
65 * effect on wrapped backend render targets.
66 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000067 * @param rect a rect bounding the area needing resolve. NULL indicates
68 * the whole RT needs resolving.
69 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040070 void flagAsNeedingResolve(const SkIRect* rect = nullptr);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000071
72 /**
73 * Call to override the region that needs to be resolved.
74 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000075 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000076
77 /**
78 * Call to indicate that GrRenderTarget was externally resolved. This may
79 * allow Gr to skip a redundant resolve step.
80 */
Mike Reed274218e2018-01-08 15:05:02 -050081 void flagAsResolved();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000082
83 /**
84 * @return true if the GrRenderTarget requires MSAA resolving
85 */
86 bool needsResolve() const { return !fResolveRect.isEmpty(); }
87
88 /**
89 * Returns a rect bounding the region needing resolving.
90 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000091 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000093 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000095 // resolve destination.
96 enum ResolveType {
97 kCanResolve_ResolveType,
98 kAutoResolves_ResolveType,
99 kCantResolve_ResolveType,
100 };
101 virtual ResolveType getResolveType() const = 0;
102
joshualitt81793412015-07-08 12:54:04 -0700103 /**
104 * Return the native ID or handle to the rendertarget, depending on the
105 * platform. e.g. on OpenGL, return the FBO ID.
106 */
107 virtual GrBackendObject getRenderTargetHandle() const = 0;
108
Robert Phillipsb67821d2017-12-13 15:00:45 -0500109 virtual GrBackendRenderTarget getBackendRenderTarget() const = 0;
110
egdanielec00d942015-09-14 12:56:10 -0700111 // Checked when this object is asked to attach a stencil buffer.
112 virtual bool canAttemptStencilAttachment() const = 0;
113
bsalomon6bc1b5f2015-02-23 09:06:38 -0800114 // Provides access to functions that aren't part of the public API.
115 GrRenderTargetPriv renderTargetPriv();
116 const GrRenderTargetPriv renderTargetPriv() const;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000117
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000118protected:
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400119 GrRenderTarget(GrGpu*, const GrSurfaceDesc&,
120 GrRenderTargetFlags = GrRenderTargetFlags::kNone,
csmartdaltonf9635992016-08-10 11:09:07 -0700121 GrStencilAttachment* = nullptr);
robertphillips498d7ac2015-10-30 10:11:30 -0700122
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000123 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700124 void onAbandon() override;
125 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000126
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000127private:
egdanielec00d942015-09-14 12:56:10 -0700128 // Allows the backends to perform any additional work that is required for attaching a
129 // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
130 // the GrRenderTarget. This function must return false if any failures occur when completing the
131 // stencil attachment.
132 virtual bool completeStencilAttachment() = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800133
134 friend class GrRenderTargetPriv;
135
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400136 int fSampleCnt;
137 GrStencilAttachment* fStencilAttachment;
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400138 GrRenderTargetFlags fFlags;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000139
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400140 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000141
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000142 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000143};
144
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000145#endif