blob: 94b1875e2eb33fd7e42f9b1932b249ac5e81eb75 [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
Brian Salomond34edf32017-05-19 15:45:48 -040033 bool isStencilBufferMultisampled() const { return fSampleCnt > 0; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000034
Brian Salomon7c8460e2017-05-12 11:36:10 -040035 GrFSAAType fsaaType() const {
Brian Salomond34edf32017-05-19 15:45:48 -040036 if (!fSampleCnt) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040037 SkASSERT(!(fFlags & GrRenderTargetFlags::kMixedSampled));
Brian Salomon7c8460e2017-05-12 11:36:10 -040038 return GrFSAAType::kNone;
39 }
Robert Phillipsc4f0a822017-06-13 08:11:36 -040040 return (fFlags & GrRenderTargetFlags::kMixedSampled) ? GrFSAAType::kMixedSamples
41 : GrFSAAType::kUnifiedMSAA;
Brian Salomon7c8460e2017-05-12 11:36:10 -040042 }
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 */
Brian Salomond34edf32017-05-19 15:45:48 -040047 int numStencilSamples() const { return 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 {
Brian Salomond34edf32017-05-19 15:45:48 -040053 return GrFSAAType::kMixedSamples == this->fsaaType() ? 0 : 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 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040066 void flagAsNeedingResolve(const SkIRect* rect = nullptr);
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:
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400113 GrRenderTarget(GrGpu*, const GrSurfaceDesc&,
114 GrRenderTargetFlags = GrRenderTargetFlags::kNone,
csmartdaltonf9635992016-08-10 11:09:07 -0700115 GrStencilAttachment* = nullptr);
robertphillips498d7ac2015-10-30 10:11:30 -0700116
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000117 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700118 void onAbandon() override;
119 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000120
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000121private:
egdanielec00d942015-09-14 12:56:10 -0700122 // Allows the backends to perform any additional work that is required for attaching a
123 // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
124 // the GrRenderTarget. This function must return false if any failures occur when completing the
125 // stencil attachment.
126 virtual bool completeStencilAttachment() = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800127
128 friend class GrRenderTargetPriv;
129
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400130 int fSampleCnt;
131 GrStencilAttachment* fStencilAttachment;
132 uint8_t fMultisampleSpecsID;
133 GrRenderTargetFlags fFlags;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000134
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400135 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
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000140#endif