1 /*
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 #include "src/gpu/GrRenderTargetProxy.h"
9
10 #include "src/core/SkMathPriv.h"
11 #include "src/gpu/GrCaps.h"
12 #include "src/gpu/GrGpuResourcePriv.h"
13 #include "src/gpu/GrRenderTargetOpList.h"
14 #include "src/gpu/GrRenderTargetPriv.h"
15 #include "src/gpu/GrResourceProvider.h"
16 #include "src/gpu/GrSurfacePriv.h"
17 #include "src/gpu/GrTextureRenderTargetProxy.h"
18
19 // Deferred version
20 // TODO: we can probably munge the 'desc' in both the wrapped and deferred
21 // cases to make the sampleConfig/numSamples stuff more rational.
GrRenderTargetProxy(const GrCaps & caps,const GrBackendFormat & format,const GrSurfaceDesc & desc,int sampleCount,GrSurfaceOrigin origin,const GrSwizzle & textureSwizzle,const GrSwizzle & outputSwizzle,SkBackingFit fit,SkBudgeted budgeted,GrProtected isProtected,GrInternalSurfaceFlags surfaceFlags)22 GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, const GrBackendFormat& format,
23 const GrSurfaceDesc& desc, int sampleCount,
24 GrSurfaceOrigin origin, const GrSwizzle& textureSwizzle,
25 const GrSwizzle& outputSwizzle, SkBackingFit fit,
26 SkBudgeted budgeted, GrProtected isProtected,
27 GrInternalSurfaceFlags surfaceFlags)
28 : INHERITED(format, desc, GrRenderable::kYes, origin, textureSwizzle, fit, budgeted,
29 isProtected, surfaceFlags)
30 , fSampleCnt(sampleCount)
31 , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo)
32 , fOutputSwizzle(outputSwizzle) {}
33
34 // Lazy-callback version
GrRenderTargetProxy(LazyInstantiateCallback && callback,LazyInstantiationType lazyType,const GrBackendFormat & format,const GrSurfaceDesc & desc,int sampleCount,GrSurfaceOrigin origin,const GrSwizzle & textureSwizzle,const GrSwizzle & outputSwizzle,SkBackingFit fit,SkBudgeted budgeted,GrProtected isProtected,GrInternalSurfaceFlags surfaceFlags,WrapsVkSecondaryCB wrapsVkSecondaryCB)35 GrRenderTargetProxy::GrRenderTargetProxy(
36 LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
37 const GrBackendFormat& format, const GrSurfaceDesc& desc, int sampleCount,
38 GrSurfaceOrigin origin, const GrSwizzle& textureSwizzle, const GrSwizzle& outputSwizzle,
39 SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected,
40 GrInternalSurfaceFlags surfaceFlags, WrapsVkSecondaryCB wrapsVkSecondaryCB)
41 : INHERITED(std::move(callback), lazyType, format, desc, GrRenderable::kYes, origin,
42 textureSwizzle, fit, budgeted, isProtected, surfaceFlags)
43 , fSampleCnt(sampleCount)
44 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB)
45 , fOutputSwizzle(outputSwizzle) {}
46
47 // Wrapped version
GrRenderTargetProxy(sk_sp<GrSurface> surf,GrSurfaceOrigin origin,const GrSwizzle & textureSwizzle,const GrSwizzle & outputSwizzle,WrapsVkSecondaryCB wrapsVkSecondaryCB)48 GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
49 const GrSwizzle& textureSwizzle,
50 const GrSwizzle& outputSwizzle,
51 WrapsVkSecondaryCB wrapsVkSecondaryCB)
52 : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
53 , fSampleCnt(fTarget->asRenderTarget()->numSamples())
54 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB)
55 , fOutputSwizzle(outputSwizzle) {
56 }
57
maxWindowRectangles(const GrCaps & caps) const58 int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
59 return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
60 }
61
instantiate(GrResourceProvider * resourceProvider)62 bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
63 if (LazyState::kNot != this->lazyInstantiationState()) {
64 return false;
65 }
66 if (!this->instantiateImpl(resourceProvider, fSampleCnt, fNumStencilSamples, GrRenderable::kYes,
67 GrMipMapped::kNo, nullptr)) {
68 return false;
69 }
70
71 SkASSERT(this->peekRenderTarget());
72 SkASSERT(!this->peekTexture());
73 return true;
74 }
75
canChangeStencilAttachment() const76 bool GrRenderTargetProxy::canChangeStencilAttachment() const {
77 if (!fTarget) {
78 // If we aren't instantiated, then we definitely are an internal render target. Ganesh is
79 // free to change stencil attachments on internal render targets.
80 return true;
81 }
82 return fTarget->asRenderTarget()->canAttemptStencilAttachment();
83 }
84
createSurface(GrResourceProvider * resourceProvider) const85 sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
86 sk_sp<GrSurface> surface = this->createSurfaceImpl(
87 resourceProvider, fSampleCnt, fNumStencilSamples, GrRenderable::kYes, GrMipMapped::kNo);
88 if (!surface) {
89 return nullptr;
90 }
91 SkASSERT(surface->asRenderTarget());
92 SkASSERT(!surface->asTexture());
93 return surface;
94 }
95
onUninstantiatedGpuMemorySize() const96 size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
97 int colorSamplesPerPixel = this->numSamples();
98 if (colorSamplesPerPixel > 1) {
99 // Add one for the resolve buffer.
100 ++colorSamplesPerPixel;
101 }
102
103 // TODO: do we have enough information to improve this worst case estimate?
104 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
105 colorSamplesPerPixel, GrMipMapped::kNo, !this->priv().isExact());
106 }
107
refsWrappedObjects() const108 bool GrRenderTargetProxy::refsWrappedObjects() const {
109 if (!this->isInstantiated()) {
110 return false;
111 }
112
113 GrSurface* surface = this->peekSurface();
114 return surface->resourcePriv().refsWrappedObjects();
115 }
116
117 #ifdef SK_DEBUG
onValidateSurface(const GrSurface * surface)118 void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
119 // We do not check that surface->asTexture returns null since, when replaying DDLs we
120 // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
121
122 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
123 SkASSERT(surface->asRenderTarget());
124 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
125
126 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
127 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
128 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
129 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
130 }
131 #endif
132