blob: dcbe8ea974aa64d3c8269a1a2adfd42d740ba21a [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
csmartdaltonf9635992016-08-10 11:09:07 -070033 bool isStencilBufferMultisampled() const { return fDesc.fSampleCnt > 0; }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000034
35 /**
csmartdaltonf9635992016-08-10 11:09:07 -070036 * For our purposes, "Mixed Sampled" means the stencil buffer is multisampled but the color
37 * buffer is not.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000038 */
csmartdaltonf9635992016-08-10 11:09:07 -070039 bool isMixedSampled() const { return fFlags & Flags::kMixedSampled; }
vbuzinovdded6962015-06-12 08:59:45 -070040
41 /**
csmartdaltonf9635992016-08-10 11:09:07 -070042 * "Unified Sampled" means the stencil and color buffers are both multisampled.
vbuzinovdded6962015-06-12 08:59:45 -070043 */
csmartdaltonf9635992016-08-10 11:09:07 -070044 bool isUnifiedMultisampled() const { return fDesc.fSampleCnt > 0 && !this->isMixedSampled(); }
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 */
csmartdaltonf9635992016-08-10 11:09:07 -070049 int numStencilSamples() const { return fDesc.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 */
csmartdaltonf9635992016-08-10 11:09:07 -070054 int numColorSamples() const { return this->isMixedSampled() ? 0 : fDesc.fSampleCnt; }
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.com75f9f252012-01-31 13:35:56 +000089 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000090 * Provide a performance hint that the render target's contents are allowed
91 * to become undefined.
92 */
93 void discard();
94
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000095 // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096 // 0 in GL), or be unresolvable because the client didn't give us the
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000097 // resolve destination.
98 enum ResolveType {
99 kCanResolve_ResolveType,
100 kAutoResolves_ResolveType,
101 kCantResolve_ResolveType,
102 };
103 virtual ResolveType getResolveType() const = 0;
104
joshualitt81793412015-07-08 12:54:04 -0700105 /**
106 * Return the native ID or handle to the rendertarget, depending on the
107 * platform. e.g. on OpenGL, return the FBO ID.
108 */
109 virtual GrBackendObject getRenderTargetHandle() 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
Robert Phillipsf2361d22016-10-25 14:20:06 -0400118 GrRenderTargetOpList* getLastRenderTargetOpList() {
119 return (GrRenderTargetOpList*) this->getLastOpList();
120 }
robertphillipsa106c622015-10-16 09:07:06 -0700121
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000122protected:
csmartdaltonf9635992016-08-10 11:09:07 -0700123 enum class Flags {
124 kNone = 0,
125 kMixedSampled = 1 << 0,
126 kWindowRectsSupport = 1 << 1
127 };
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000128
csmartdaltonf9635992016-08-10 11:09:07 -0700129 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(Flags);
130
131 GrRenderTarget(GrGpu*, const GrSurfaceDesc&, Flags = Flags::kNone,
132 GrStencilAttachment* = nullptr);
robertphillips498d7ac2015-10-30 10:11:30 -0700133
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000134 // override of GrResource
mtklein36352bf2015-03-25 18:17:31 -0700135 void onAbandon() override;
136 void onRelease() override;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000137
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000138private:
egdanielec00d942015-09-14 12:56:10 -0700139 // Allows the backends to perform any additional work that is required for attaching a
140 // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
141 // the GrRenderTarget. This function must return false if any failures occur when completing the
142 // stencil attachment.
143 virtual bool completeStencilAttachment() = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800144
145 friend class GrRenderTargetPriv;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400146 friend class GrRenderTargetProxy; // for Flags
bsalomon6bc1b5f2015-02-23 09:06:38 -0800147
egdaniel8dc7c3a2015-04-16 11:22:42 -0700148 GrStencilAttachment* fStencilAttachment;
csmartdalton0d28e572016-07-06 09:59:43 -0700149 uint8_t fMultisampleSpecsID;
csmartdaltonf9635992016-08-10 11:09:07 -0700150 Flags fFlags;
robertphillips@google.come98ade42012-06-13 12:53:07 +0000151
egdaniel8dc7c3a2015-04-16 11:22:42 -0700152 SkIRect fResolveRect;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000153
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000154 typedef GrSurface INHERITED;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000155};
156
csmartdaltonf9635992016-08-10 11:09:07 -0700157GR_MAKE_BITFIELD_CLASS_OPS(GrRenderTarget::Flags);
vbuzinovdded6962015-06-12 08:59:45 -0700158
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000159#endif