blob: fdf6f9cddc6e19e6f40d31697d0f03f4499f4fb7 [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;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018
19/**
20 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
21 * A context's render target is set by setRenderTarget(). Render targets are
bsalomon37dd3312014-11-03 08:47:23 -080022 * created by a createTexture with the kRenderTarget_SurfaceFlag flag.
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000023 * Additionally, GrContext provides methods for creating GrRenderTargets
24 * that wrap externally created render targets.
25 */
bsalomon37dd3312014-11-03 08:47:23 -080026class GrRenderTarget : virtual public GrSurface {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000027public:
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000028 // GrSurface overrides
mtklein36352bf2015-03-25 18:17:31 -070029 GrRenderTarget* asRenderTarget() override { return this; }
30 const GrRenderTarget* asRenderTarget() const override { return this; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000031
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000032 // GrRenderTarget
Greg Daniel4b30a962017-05-18 18:13:40 +000033 bool isStencilBufferMultisampled() const { return fDesc.fSampleCnt > 0; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000034
Brian Salomon7c8460e2017-05-12 11:36:10 -040035 GrFSAAType fsaaType() const {
Greg Daniel4b30a962017-05-18 18:13:40 +000036 if (!fDesc.fSampleCnt) {
Brian Salomon7c8460e2017-05-12 11:36:10 -040037 SkASSERT(!(fFlags & Flags::kMixedSampled));
38 return GrFSAAType::kNone;
39 }
40 return (fFlags & Flags::kMixedSampled) ? GrFSAAType::kMixedSamples
41 : GrFSAAType::kUnifiedMSAA;
42 }
vbuzinovdded6962015-06-12 08:59:45 -070043
44 /**
csmartdaltonf9635992016-08-10 11:09:07 -070045 * Returns the number of samples/pixel in the stencil buffer (Zero if non-MSAA).
vbuzinovdded6962015-06-12 08:59:45 -070046 */
Greg Daniel4b30a962017-05-18 18:13:40 +000047 int numStencilSamples() const { return fDesc.fSampleCnt; }
vbuzinovdded6962015-06-12 08:59:45 -070048
49 /**
csmartdaltonf9635992016-08-10 11:09:07 -070050 * Returns the number of samples/pixel in the color buffer (Zero if non-MSAA or mixed sampled).
vbuzinovdded6962015-06-12 08:59:45 -070051 */
Brian Salomon7c8460e2017-05-12 11:36:10 -040052 int numColorSamples() const {
Greg Daniel4b30a962017-05-18 18:13:40 +000053 return GrFSAAType::kMixedSamples == this->fsaaType() ? 0 : fDesc.fSampleCnt;
Brian Salomon7c8460e2017-05-12 11:36:10 -040054 }
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.comaa5b6732011-07-29 15:13:20 +000089 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000091 // resolve destination.
92 enum ResolveType {
93 kCanResolve_ResolveType,
94 kAutoResolves_ResolveType,
95 kCantResolve_ResolveType,
96 };
97 virtual ResolveType getResolveType() const = 0;
98
joshualitt81793412015-07-08 12:54:04 -070099 /**
100 * Return the native ID or handle to the rendertarget, depending on the
101 * platform. e.g. on OpenGL, return the FBO ID.
102 */
103 virtual GrBackendObject getRenderTargetHandle() const = 0;
104
egdanielec00d942015-09-14 12:56:10 -0700105 // Checked when this object is asked to attach a stencil buffer.
106 virtual bool canAttemptStencilAttachment() const = 0;
107
bsalomon6bc1b5f2015-02-23 09:06:38 -0800108 // Provides access to functions that aren't part of the public API.
109 GrRenderTargetPriv renderTargetPriv();
110 const GrRenderTargetPriv renderTargetPriv() const;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000111
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000112protected:
csmartdaltonf9635992016-08-10 11:09:07 -0700113 enum class Flags {
114 kNone = 0,
115 kMixedSampled = 1 << 0,
116 kWindowRectsSupport = 1 << 1
117 };
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000118
csmartdaltonf9635992016-08-10 11:09:07 -0700119 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
120
121 GrRenderTarget(GrGpu*, const GrSurfaceDesc&, Flags = Flags::kNone,
122 GrStencilAttachment* = nullptr);
robertphillips498d7ac2015-10-30 10:11:30 -0700123
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000124 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700125 void onAbandon() override;
126 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000127
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000128private:
egdanielec00d942015-09-14 12:56:10 -0700129 // Allows the backends to perform any additional work that is required for attaching a
130 // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
131 // the GrRenderTarget. This function must return false if any failures occur when completing the
132 // stencil attachment.
133 virtual bool completeStencilAttachment() = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800134
135 friend class GrRenderTargetPriv;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400136 friend class GrRenderTargetProxy; // for Flags
bsalomon6bc1b5f2015-02-23 09:06:38 -0800137
egdaniel8dc7c3a2015-04-16 11:22:42 -0700138 GrStencilAttachment* fStencilAttachment;
csmartdalton0d28e572016-07-06 09:59:43 -0700139 uint8_t fMultisampleSpecsID;
csmartdaltonf9635992016-08-10 11:09:07 -0700140 Flags fFlags;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000141
egdaniel8dc7c3a2015-04-16 11:22:42 -0700142 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000143
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000144 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000145};
146
csmartdaltonf9635992016-08-10 11:09:07 -0700147GR_MAKE_BITFIELD_CLASS_OPS(GrRenderTarget::Flags);
vbuzinovdded6962015-06-12 08:59:45 -0700148
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000149#endif