blob: 38bc0679eb56826811a55ab5cd9dfcf8ea0e8b03 [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
Greg Danielf91aeb22019-06-18 09:58:02 -04008#include "src/gpu/GrRenderTargetProxy.h"
robertphillips76948d42016-05-04 12:47:41 -07009
Chris Dalton3f7932e2019-08-19 00:39:13 -060010#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkMathPriv.h"
12#include "src/gpu/GrCaps.h"
Chris Dalton3f7932e2019-08-19 00:39:13 -060013#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040015#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrRenderTargetPriv.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrSurfacePriv.h"
19#include "src/gpu/GrTextureRenderTargetProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070020
csmartdaltonf9635992016-08-10 11:09:07 -070021// Deferred version
22// TODO: we can probably munge the 'desc' in both the wrapped and deferred
23// cases to make the sampleConfig/numSamples stuff more rational.
Brian Salomonbeb7f522019-08-30 16:19:42 -040024GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps,
25 const GrBackendFormat& format,
26 const GrSurfaceDesc& desc,
27 int sampleCount,
28 GrSurfaceOrigin origin,
29 const GrSwizzle& textureSwizzle,
30 const GrSwizzle& outputSwizzle,
31 SkBackingFit fit,
32 SkBudgeted budgeted,
33 GrProtected isProtected,
34 GrInternalSurfaceFlags surfaceFlags,
35 UseAllocator useAllocator)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040036 : INHERITED(format, desc, GrRenderable::kYes, origin, textureSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -040037 isProtected, surfaceFlags, useAllocator)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040038 , fSampleCnt(sampleCount)
Chris Daltoneffee202019-07-01 22:28:03 -060039 , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040040 , fOutputSwizzle(outputSwizzle) {}
csmartdaltonf9635992016-08-10 11:09:07 -070041
Chris Dalton706a6ff2017-11-29 22:01:06 -070042// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040043GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
44 const GrBackendFormat& format,
45 const GrSurfaceDesc& desc,
46 int sampleCount,
47 GrSurfaceOrigin origin,
48 const GrSwizzle& textureSwizzle,
49 const GrSwizzle& outputSwizzle,
50 SkBackingFit fit,
51 SkBudgeted budgeted,
52 GrProtected isProtected,
53 GrInternalSurfaceFlags surfaceFlags,
54 UseAllocator useAllocator,
55 WrapsVkSecondaryCB wrapsVkSecondaryCB)
56 : INHERITED(std::move(callback), format, desc, GrRenderable::kYes, origin, textureSwizzle,
57 fit, budgeted, isProtected, surfaceFlags, useAllocator)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040058 , fSampleCnt(sampleCount)
Chris Daltoneffee202019-07-01 22:28:03 -060059 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040060 , fOutputSwizzle(outputSwizzle) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070061
csmartdaltonf9635992016-08-10 11:09:07 -070062// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040063GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf,
64 GrSurfaceOrigin origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040065 const GrSwizzle& textureSwizzle,
66 const GrSwizzle& outputSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -040067 UseAllocator useAllocator,
Greg Danielb46add82019-01-02 14:51:29 -050068 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Brian Salomonbeb7f522019-08-30 16:19:42 -040069 : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact, useAllocator)
Chris Dalton6ce447a2019-06-23 18:07:38 -060070 , fSampleCnt(fTarget->asRenderTarget()->numSamples())
Chris Daltoneffee202019-07-01 22:28:03 -060071 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB)
72 , fOutputSwizzle(outputSwizzle) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060073 // The kRequiresManualMSAAResolve flag better not be set if we are not multisampled or if
74 // MSAA resolve should happen automatically.
75 //
76 // From the other side, we don't know enough about the wrapped surface to assert when
77 // kRequiresManualMSAAResolve *should* be set. e.g., The caller might be wrapping a backend
78 // texture as a render target at this point but we wouldn't know it.
79 SkASSERT(!(this->numSamples() <= 1 ||
80 fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) ||
81 !this->requiresManualMSAAResolve());
Robert Phillipsc4f0a822017-06-13 08:11:36 -040082}
robertphillips76948d42016-05-04 12:47:41 -070083
Robert Phillipsec2249f2016-11-09 08:54:35 -050084int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
Brian Salomon34a20302018-12-18 12:03:07 -050085 return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
Robert Phillipsec2249f2016-11-09 08:54:35 -050086}
87
Robert Phillips10d17212019-04-24 14:09:10 -040088bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040089 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -050090 return false;
91 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -040092 if (!this->instantiateImpl(resourceProvider, fSampleCnt, fNumStencilSamples, GrRenderable::kYes,
Robert Phillips10d17212019-04-24 14:09:10 -040093 GrMipMapped::kNo, nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040094 return false;
robertphillips76948d42016-05-04 12:47:41 -070095 }
Robert Phillipsb5204762019-06-19 14:12:13 -040096
97 SkASSERT(this->peekRenderTarget());
98 SkASSERT(!this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040099 return true;
robertphillips76948d42016-05-04 12:47:41 -0700100}
101
Chris Dalton215ff332019-07-02 09:38:22 -0600102bool GrRenderTargetProxy::canChangeStencilAttachment() const {
103 if (!fTarget) {
104 // If we aren't instantiated, then we definitely are an internal render target. Ganesh is
105 // free to change stencil attachments on internal render targets.
106 return true;
107 }
108 return fTarget->asRenderTarget()->canAttemptStencilAttachment();
109}
110
Robert Phillips5af44de2017-07-18 14:49:38 -0400111sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Daltoneffee202019-07-01 22:28:03 -0600112 sk_sp<GrSurface> surface = this->createSurfaceImpl(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400113 resourceProvider, fSampleCnt, fNumStencilSamples, GrRenderable::kYes, GrMipMapped::kNo);
Robert Phillips5af44de2017-07-18 14:49:38 -0400114 if (!surface) {
115 return nullptr;
116 }
117 SkASSERT(surface->asRenderTarget());
Greg Daniele252f082017-10-23 16:05:23 -0400118 SkASSERT(!surface->asTexture());
Robert Phillips5af44de2017-07-18 14:49:38 -0400119 return surface;
120}
121
Brian Salomonbb5711a2017-05-17 13:49:59 -0400122size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600123 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500124 if (colorSamplesPerPixel > 1) {
125 // Add one for the resolve buffer.
126 ++colorSamplesPerPixel;
127 }
Greg Daniele252f082017-10-23 16:05:23 -0400128
Robert Phillips8bc06d02016-11-01 17:28:40 -0400129 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -0700130 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
131 colorSamplesPerPixel, GrMipMapped::kNo, !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400132}
133
Robert Phillipse2f7d182016-12-15 09:23:05 -0500134bool GrRenderTargetProxy::refsWrappedObjects() const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400135 if (!this->isInstantiated()) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500136 return false;
137 }
138
Robert Phillipsb5204762019-06-19 14:12:13 -0400139 GrSurface* surface = this->peekSurface();
140 return surface->resourcePriv().refsWrappedObjects();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500141}
Robert Phillipse8fabb22018-02-04 14:33:21 -0500142
143#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400144void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500145 // We do not check that surface->asTexture returns null since, when replaying DDLs we
146 // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
Robert Phillipse8fabb22018-02-04 14:33:21 -0500147
148 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
149 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600150 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400151
Greg Daniela070ed72018-04-26 16:31:38 -0400152 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
153 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600154 SkASSERT(((int)proxyFlags & kGrInternalRenderTargetFlagsMask) ==
155 ((int)surfaceFlags & kGrInternalRenderTargetFlagsMask));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500156}
157#endif