blob: 33718502b06aa47ce543137905c445e1e3c86493 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
2 * Copyright 2016 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.
6 */
7
8#ifndef GrRenderTargetProxy_DEFINED
9#define GrRenderTargetProxy_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrTypesPriv.h"
Chris Daltoneffee202019-07-01 22:28:03 -060012#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrSurfaceProxy.h"
Greg Daniel2c19e7f2019-06-18 13:29:21 -040014#include "src/gpu/GrSwizzle.h"
robertphillips76948d42016-05-04 12:47:41 -070015
Brian Osman32342f02017-03-04 08:12:46 -050016class GrResourceProvider;
Greg Daniela070ed72018-04-26 16:31:38 -040017class GrRenderTargetProxyPriv;
robertphillips76948d42016-05-04 12:47:41 -070018
19// This class delays the acquisition of RenderTargets until they are actually
20// required
21// Beware: the uniqueID of the RenderTargetProxy will usually be different than
22// the uniqueID of the RenderTarget it represents!
Robert Phillips84a81202016-11-04 11:59:10 -040023class GrRenderTargetProxy : virtual public GrSurfaceProxy {
robertphillips76948d42016-05-04 12:47:41 -070024public:
robertphillips76948d42016-05-04 12:47:41 -070025 GrRenderTargetProxy* asRenderTargetProxy() override { return this; }
26 const GrRenderTargetProxy* asRenderTargetProxy() const override { return this; }
27
28 // Actually instantiate the backing rendertarget, if necessary.
Robert Phillips10d17212019-04-24 14:09:10 -040029 bool instantiate(GrResourceProvider*) override;
robertphillips76948d42016-05-04 12:47:41 -070030
Chris Daltoneffee202019-07-01 22:28:03 -060031 bool canUseMixedSamples(const GrCaps& caps) const {
Chris Daltoneffee202019-07-01 22:28:03 -060032 return caps.mixedSamplesSupport() && !this->glRTFBOIDIs0() &&
Greg Danieleadfac92019-08-02 09:03:53 -040033 caps.internalMultisampleCount(this->backendFormat()) > 0 &&
Chris Dalton215ff332019-07-02 09:38:22 -060034 this->canChangeStencilAttachment();
Chris Daltoneffee202019-07-01 22:28:03 -060035 }
36
Robert Phillips65048132017-08-10 08:44:49 -040037 /*
Chris Daltoneffee202019-07-01 22:28:03 -060038 * Indicate that a draw to this proxy requires stencil, and how many stencil samples it needs.
39 * The number of stencil samples on this proxy will be equal to the largest sample count passed
40 * to this method.
Robert Phillips65048132017-08-10 08:44:49 -040041 */
Chris Daltoneffee202019-07-01 22:28:03 -060042 void setNeedsStencil(int8_t numStencilSamples) {
43 SkASSERT(numStencilSamples >= fSampleCnt);
44 fNumStencilSamples = SkTMax(numStencilSamples, fNumStencilSamples);
45 }
46
47 /**
48 * Returns the number of stencil samples required by this proxy.
49 * NOTE: Once instantiated, the actual render target may have more samples, but it is guaranteed
50 * to have at least this many. (After a multisample stencil buffer has been attached to a render
51 * target, we never "downgrade" it to one with fewer samples.)
52 */
53 int numStencilSamples() const { return fNumStencilSamples; }
Robert Phillips65048132017-08-10 08:44:49 -040054
Brian Salomonbb5711a2017-05-17 13:49:59 -040055 /**
Chris Dalton6ce447a2019-06-23 18:07:38 -060056 * Returns the number of samples/pixel in the color buffer (One if non-MSAA).
Brian Salomonbb5711a2017-05-17 13:49:59 -040057 */
Chris Dalton6ce447a2019-06-23 18:07:38 -060058 int numSamples() const { return fSampleCnt; }
robertphillips76948d42016-05-04 12:47:41 -070059
Robert Phillipsec2249f2016-11-09 08:54:35 -050060 int maxWindowRectangles(const GrCaps& caps) const;
61
Greg Daniel2c19e7f2019-06-18 13:29:21 -040062 const GrSwizzle& outputSwizzle() const { return fOutputSwizzle; }
63
Greg Danielb46add82019-01-02 14:51:29 -050064 bool wrapsVkSecondaryCB() const { return fWrapsVkSecondaryCB == WrapsVkSecondaryCB::kYes; }
65
Robert Phillipse2f7d182016-12-15 09:23:05 -050066 // TODO: move this to a priv class!
67 bool refsWrappedObjects() const;
68
Greg Daniela070ed72018-04-26 16:31:38 -040069 // Provides access to special purpose functions.
70 GrRenderTargetProxyPriv rtPriv();
71 const GrRenderTargetProxyPriv rtPriv() const;
72
Robert Phillips84a81202016-11-04 11:59:10 -040073protected:
Robert Phillips0bd24dc2018-01-16 08:06:32 -050074 friend class GrProxyProvider; // for ctors
Greg Daniela070ed72018-04-26 16:31:38 -040075 friend class GrRenderTargetProxyPriv;
Robert Phillips37430132016-11-09 06:50:43 -050076
csmartdaltonf9635992016-08-10 11:09:07 -070077 // Deferred version
Greg Daniel4065d452018-11-16 15:43:41 -050078 GrRenderTargetProxy(const GrCaps&, const GrBackendFormat&, const GrSurfaceDesc&,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040079 int sampleCount, GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -040080 const GrSwizzle& outputSwizzle, SkBackingFit, SkBudgeted, GrProtected,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040081 GrInternalSurfaceFlags);
robertphillips76948d42016-05-04 12:47:41 -070082
Greg Danielb085fa92019-03-05 16:55:12 -050083 enum class WrapsVkSecondaryCB : bool { kNo = false, kYes = true };
84
Chris Dalton706a6ff2017-11-29 22:01:06 -070085 // Lazy-callback version
Greg Daniel65fa8ca2018-01-10 17:06:31 -050086 // There are two main use cases for lazily-instantiated proxies:
87 // basic knowledge - width, height, config, samples, origin are known
88 // minimal knowledge - only config is known.
89 //
90 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
91 // use, but we don't have access to the GPU yet to instantiate it.
92 //
93 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
94 // know the final size until flush time.
Greg Daniel457469c2018-02-08 15:05:44 -050095 GrRenderTargetProxy(LazyInstantiateCallback&&, LazyInstantiationType lazyType,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040096 const GrBackendFormat&, const GrSurfaceDesc&, int sampleCount,
97 GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
98 const GrSwizzle& outputSwizzle, SkBackingFit, SkBudgeted, GrProtected,
99 GrInternalSurfaceFlags, WrapsVkSecondaryCB wrapsVkSecondaryCB);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700100
robertphillips76948d42016-05-04 12:47:41 -0700101 // Wrapped version
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400102 GrRenderTargetProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
103 const GrSwizzle& outputSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500104 WrapsVkSecondaryCB wrapsVkSecondaryCB = WrapsVkSecondaryCB::kNo);
robertphillips76948d42016-05-04 12:47:41 -0700105
Robert Phillips5af44de2017-07-18 14:49:38 -0400106 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
107
Robert Phillips84a81202016-11-04 11:59:10 -0400108private:
Greg Daniela070ed72018-04-26 16:31:38 -0400109 void setGLRTFBOIDIs0() {
110 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
111 }
112 bool glRTFBOIDIs0() const {
113 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
114 }
Chris Dalton215ff332019-07-02 09:38:22 -0600115 bool canChangeStencilAttachment() const;
Greg Daniela070ed72018-04-26 16:31:38 -0400116
Brian Salomonbb5711a2017-05-17 13:49:59 -0400117 size_t onUninstantiatedGpuMemorySize() const override;
Greg Daniel849dce12018-04-24 14:32:53 -0400118 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400119
Brian Osman086679b2018-09-10 16:26:51 -0400120 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
121 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
122 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
123 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
124 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
125 // that particular class don't require it. Changing the size of this object can move the start
126 // address of other types, leading to this problem.
Chris Daltoneffee202019-07-01 22:28:03 -0600127 int8_t fSampleCnt;
128 int8_t fNumStencilSamples = 0;
Greg Danielb46add82019-01-02 14:51:29 -0500129 WrapsVkSecondaryCB fWrapsVkSecondaryCB;
Chris Daltoneffee202019-07-01 22:28:03 -0600130 GrSwizzle fOutputSwizzle;
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400131 // This is to fix issue in large comment above. Without the padding we end 6 bytes into a 16
132 // byte range, so the GrTextureProxy ends up starting 8 byte aligned by not 16. We add the
133 // padding here to get us right up to the 16 byte alignment (technically any padding of 3-10
134 // bytes would work since it always goes up to 8 byte alignment, but we use 10 to more explicit
135 // about what we're doing).
136 char fDummyPadding[10];
Robert Phillips65048132017-08-10 08:44:49 -0400137
robertphillips76948d42016-05-04 12:47:41 -0700138 typedef GrSurfaceProxy INHERITED;
139};
140
141#endif