blob: ebfa4fd77fdc40c0878cbe75a04bc4238d37f96d [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 Phillipsfe0253f2018-03-16 16:47:25 -040041 SkASSERT(!this->hasMixedSamples());
Brian Salomon7c8460e2017-05-12 11:36:10 -040042 return GrFSAAType::kNone;
43 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -040044 return this->hasMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
Brian Salomon7c8460e2017-05-12 11:36:10 -040045 }
vbuzinovdded6962015-06-12 08:59:45 -070046
47 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -050048 * Returns the number of samples/pixel in the stencil buffer (One if non-MSAA).
vbuzinovdded6962015-06-12 08:59:45 -070049 */
Brian Salomond34edf32017-05-19 15:45:48 -040050 int numStencilSamples() const { return fSampleCnt; }
vbuzinovdded6962015-06-12 08:59:45 -070051
52 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -050053 * Returns the number of samples/pixel in the color buffer (One if non-MSAA or mixed sampled).
vbuzinovdded6962015-06-12 08:59:45 -070054 */
Brian Salomon7c8460e2017-05-12 11:36:10 -040055 int numColorSamples() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050056 return GrFSAAType::kMixedSamples == this->fsaaType() ? 1 : fSampleCnt;
Brian Salomon7c8460e2017-05-12 11:36:10 -040057 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000058
59 /**
60 * Call to indicate the multisample contents were modified such that the
61 * render target needs to be resolved before it can be used as texture. Gr
62 * tracks this for its own drawing and thus this only needs to be called
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000063 * when the render target has been modified outside of Gr. This has no
64 * effect on wrapped backend render targets.
65 *
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000066 * @param rect a rect bounding the area needing resolve. NULL indicates
67 * the whole RT needs resolving.
68 */
Ben Wagnera93a14a2017-08-28 10:34:05 -040069 void flagAsNeedingResolve(const SkIRect* rect = nullptr);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000070
71 /**
72 * Call to override the region that needs to be resolved.
73 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000074 void overrideResolveRect(const SkIRect rect);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000075
76 /**
77 * Call to indicate that GrRenderTarget was externally resolved. This may
78 * allow Gr to skip a redundant resolve step.
79 */
Mike Reed274218e2018-01-08 15:05:02 -050080 void flagAsResolved();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000081
82 /**
83 * @return true if the GrRenderTarget requires MSAA resolving
84 */
85 bool needsResolve() const { return !fResolveRect.isEmpty(); }
86
87 /**
88 * Returns a rect bounding the region needing resolving.
89 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000090 const SkIRect& getResolveRect() const { return fResolveRect; }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000091
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000094 // resolve destination.
95 enum ResolveType {
96 kCanResolve_ResolveType,
97 kAutoResolves_ResolveType,
98 kCantResolve_ResolveType,
99 };
100 virtual ResolveType getResolveType() const = 0;
101
joshualitt81793412015-07-08 12:54:04 -0700102 /**
103 * Return the native ID or handle to the rendertarget, depending on the
104 * platform. e.g. on OpenGL, return the FBO ID.
105 */
106 virtual GrBackendObject getRenderTargetHandle() const = 0;
107
Robert Phillipsb67821d2017-12-13 15:00:45 -0500108 virtual GrBackendRenderTarget getBackendRenderTarget() const = 0;
109
egdanielec00d942015-09-14 12:56:10 -0700110 // Checked when this object is asked to attach a stencil buffer.
111 virtual bool canAttemptStencilAttachment() const = 0;
112
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 // Provides access to functions that aren't part of the public API.
114 GrRenderTargetPriv renderTargetPriv();
115 const GrRenderTargetPriv renderTargetPriv() const;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000116
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000117protected:
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400118 GrRenderTarget(GrGpu*, const GrSurfaceDesc&, GrStencilAttachment* = nullptr);
robertphillips498d7ac2015-10-30 10:11:30 -0700119
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000120 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700121 void onAbandon() override;
122 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000123
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000124private:
egdanielec00d942015-09-14 12:56:10 -0700125 // Allows the backends to perform any additional work that is required for attaching a
126 // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
127 // the GrRenderTarget. This function must return false if any failures occur when completing the
128 // stencil attachment.
129 virtual bool completeStencilAttachment() = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800130
131 friend class GrRenderTargetPriv;
132
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400133 int fSampleCnt;
134 GrStencilAttachment* fStencilAttachment;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000135
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400136 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000137
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000138 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000139};
140
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000141#endif