blob: ff5eb0301d59905c81c143875b78dade7e978fda [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:
Robert Phillipscb2e2352017-08-30 16:44:40 -040028 virtual bool alwaysClearStencil() const { return false; }
29
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000030 // GrSurface overrides
mtklein36352bf2015-03-25 18:17:31 -070031 GrRenderTarget* asRenderTarget() override { return this; }
32 const GrRenderTarget* asRenderTarget() const override { return this; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000033
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000034 // GrRenderTarget
Brian Salomond34edf32017-05-19 15:45:48 -040035 bool isStencilBufferMultisampled() const { return fSampleCnt > 0; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000036
Brian Salomon7c8460e2017-05-12 11:36:10 -040037 GrFSAAType fsaaType() const {
Brian Salomond34edf32017-05-19 15:45:48 -040038 if (!fSampleCnt) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040039 SkASSERT(!(fFlags & GrRenderTargetFlags::kMixedSampled));
Brian Salomon7c8460e2017-05-12 11:36:10 -040040 return GrFSAAType::kNone;
41 }
Robert Phillipsc4f0a822017-06-13 08:11:36 -040042 return (fFlags & GrRenderTargetFlags::kMixedSampled) ? GrFSAAType::kMixedSamples
43 : GrFSAAType::kUnifiedMSAA;
Brian Salomon7c8460e2017-05-12 11:36:10 -040044 }
vbuzinovdded6962015-06-12 08:59:45 -070045
46 /**
csmartdaltonf9635992016-08-10 11:09:07 -070047 * Returns the number of samples/pixel in the stencil buffer (Zero if non-MSAA).
vbuzinovdded6962015-06-12 08:59:45 -070048 */
Brian Salomond34edf32017-05-19 15:45:48 -040049 int numStencilSamples() const { return fSampleCnt; }
vbuzinovdded6962015-06-12 08:59:45 -070050
51 /**
csmartdaltonf9635992016-08-10 11:09:07 -070052 * Returns the number of samples/pixel in the color buffer (Zero if non-MSAA or mixed sampled).
vbuzinovdded6962015-06-12 08:59:45 -070053 */
Brian Salomon7c8460e2017-05-12 11:36:10 -040054 int numColorSamples() const {
Brian Salomond34edf32017-05-19 15:45:48 -040055 return GrFSAAType::kMixedSamples == this->fsaaType() ? 0 : fSampleCnt;
Brian Salomon7c8460e2017-05-12 11:36:10 -040056 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000057
58 /**
59 * Call to indicate the multisample contents were modified such that the
60 * render target needs to be resolved before it can be used as texture. Gr
61 * tracks this for its own drawing and thus this only needs to be called
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000062 * when the render target has been modified outside of Gr. This has no
63 * effect on wrapped backend render targets.
64 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000065 * @param rect a rect bounding the area needing resolve. NULL indicates
66 * the whole RT needs resolving.
67 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040068 void flagAsNeedingResolve(const SkIRect* rect = nullptr);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000069
70 /**
71 * Call to override the region that needs to be resolved.
72 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000073 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000074
75 /**
76 * Call to indicate that GrRenderTarget was externally resolved. This may
77 * allow Gr to skip a redundant resolve step.
78 */
79 void flagAsResolved() { fResolveRect.setLargestInverted(); }
80
81 /**
82 * @return true if the GrRenderTarget requires MSAA resolving
83 */
84 bool needsResolve() const { return !fResolveRect.isEmpty(); }
85
86 /**
87 * Returns a rect bounding the region needing resolving.
88 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000089 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000090
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000091 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +000092 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000093 // resolve destination.
94 enum ResolveType {
95 kCanResolve_ResolveType,
96 kAutoResolves_ResolveType,
97 kCantResolve_ResolveType,
98 };
99 virtual ResolveType getResolveType() const = 0;
100
joshualitt81793412015-07-08 12:54:04 -0700101 /**
102 * Return the native ID or handle to the rendertarget, depending on the
103 * platform. e.g. on OpenGL, return the FBO ID.
104 */
105 virtual GrBackendObject getRenderTargetHandle() const = 0;
106
egdanielec00d942015-09-14 12:56:10 -0700107 // Checked when this object is asked to attach a stencil buffer.
108 virtual bool canAttemptStencilAttachment() const = 0;
109
bsalomon6bc1b5f2015-02-23 09:06:38 -0800110 // Provides access to functions that aren't part of the public API.
111 GrRenderTargetPriv renderTargetPriv();
112 const GrRenderTargetPriv renderTargetPriv() const;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000113
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000114protected:
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400115 GrRenderTarget(GrGpu*, const GrSurfaceDesc&,
116 GrRenderTargetFlags = GrRenderTargetFlags::kNone,
csmartdaltonf9635992016-08-10 11:09:07 -0700117 GrStencilAttachment* = nullptr);
robertphillips498d7ac2015-10-30 10:11:30 -0700118
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000119 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700120 void onAbandon() override;
121 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000122
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000123private:
egdanielec00d942015-09-14 12:56:10 -0700124 // Allows the backends to perform any additional work that is required for attaching a
125 // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
126 // the GrRenderTarget. This function must return false if any failures occur when completing the
127 // stencil attachment.
128 virtual bool completeStencilAttachment() = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800129
130 friend class GrRenderTargetPriv;
131
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400132 int fSampleCnt;
133 GrStencilAttachment* fStencilAttachment;
134 uint8_t fMultisampleSpecsID;
135 GrRenderTargetFlags fFlags;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000136
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400137 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000138
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000139 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000140};
141
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000142#endif