1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTargetProxy.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "src/core/SkMathPriv.h"
11cb93a386Sopenharmony_ci#include "src/gpu/GrCaps.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrGpuResourcePriv.h"
13cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTarget.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrResourceProvider.h"
15cb93a386Sopenharmony_ci#include "src/gpu/GrSurface.h"
16cb93a386Sopenharmony_ci#include "src/gpu/GrSurfaceProxyPriv.h"
17cb93a386Sopenharmony_ci#include "src/gpu/GrTextureRenderTargetProxy.h"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci#ifdef SK_DEBUG
20cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
21cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
22cb93a386Sopenharmony_ci#endif
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci// Deferred version
25cb93a386Sopenharmony_ci// TODO: we can probably munge the 'desc' in both the wrapped and deferred
26cb93a386Sopenharmony_ci// cases to make the sampleConfig/numSamples stuff more rational.
27cb93a386Sopenharmony_ciGrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps,
28cb93a386Sopenharmony_ci                                         const GrBackendFormat& format,
29cb93a386Sopenharmony_ci                                         SkISize dimensions,
30cb93a386Sopenharmony_ci                                         int sampleCount,
31cb93a386Sopenharmony_ci                                         SkBackingFit fit,
32cb93a386Sopenharmony_ci                                         SkBudgeted budgeted,
33cb93a386Sopenharmony_ci                                         GrProtected isProtected,
34cb93a386Sopenharmony_ci                                         GrInternalSurfaceFlags surfaceFlags,
35cb93a386Sopenharmony_ci                                         UseAllocator useAllocator)
36cb93a386Sopenharmony_ci        : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
37cb93a386Sopenharmony_ci        , fSampleCnt(sampleCount)
38cb93a386Sopenharmony_ci        , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo) {}
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci// Lazy-callback version
41cb93a386Sopenharmony_ciGrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
42cb93a386Sopenharmony_ci                                         const GrBackendFormat& format,
43cb93a386Sopenharmony_ci                                         SkISize dimensions,
44cb93a386Sopenharmony_ci                                         int sampleCount,
45cb93a386Sopenharmony_ci                                         SkBackingFit fit,
46cb93a386Sopenharmony_ci                                         SkBudgeted budgeted,
47cb93a386Sopenharmony_ci                                         GrProtected isProtected,
48cb93a386Sopenharmony_ci                                         GrInternalSurfaceFlags surfaceFlags,
49cb93a386Sopenharmony_ci                                         UseAllocator useAllocator,
50cb93a386Sopenharmony_ci                                         WrapsVkSecondaryCB wrapsVkSecondaryCB)
51cb93a386Sopenharmony_ci        : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
52cb93a386Sopenharmony_ci                    surfaceFlags, useAllocator)
53cb93a386Sopenharmony_ci        , fSampleCnt(sampleCount)
54cb93a386Sopenharmony_ci        , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {}
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci// Wrapped version
57cb93a386Sopenharmony_ciGrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf,
58cb93a386Sopenharmony_ci                                         UseAllocator useAllocator,
59cb93a386Sopenharmony_ci                                         WrapsVkSecondaryCB wrapsVkSecondaryCB)
60cb93a386Sopenharmony_ci        : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
61cb93a386Sopenharmony_ci        , fSampleCnt(fTarget->asRenderTarget()->numSamples())
62cb93a386Sopenharmony_ci        , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {
63cb93a386Sopenharmony_ci    // The kRequiresManualMSAAResolve flag better not be set if we are not multisampled or if
64cb93a386Sopenharmony_ci    // MSAA resolve should happen automatically.
65cb93a386Sopenharmony_ci    //
66cb93a386Sopenharmony_ci    // From the other side, we don't know enough about the wrapped surface to assert when
67cb93a386Sopenharmony_ci    // kRequiresManualMSAAResolve *should* be set. e.g., The caller might be wrapping a backend
68cb93a386Sopenharmony_ci    // texture as a render target at this point but we wouldn't know it.
69cb93a386Sopenharmony_ci    SkASSERT(!(this->numSamples() <= 1 ||
70cb93a386Sopenharmony_ci               fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) ||
71cb93a386Sopenharmony_ci             !this->requiresManualMSAAResolve());
72cb93a386Sopenharmony_ci}
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ciint GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
75cb93a386Sopenharmony_ci    return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
76cb93a386Sopenharmony_ci}
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_cibool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
79cb93a386Sopenharmony_ci    if (this->isLazy()) {
80cb93a386Sopenharmony_ci        return false;
81cb93a386Sopenharmony_ci    }
82cb93a386Sopenharmony_ci    if (!this->instantiateImpl(resourceProvider, fSampleCnt, GrRenderable::kYes, GrMipmapped::kNo,
83cb93a386Sopenharmony_ci                               nullptr)) {
84cb93a386Sopenharmony_ci        return false;
85cb93a386Sopenharmony_ci    }
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci    SkASSERT(this->peekRenderTarget());
88cb93a386Sopenharmony_ci    SkASSERT(!this->peekTexture());
89cb93a386Sopenharmony_ci    return true;
90cb93a386Sopenharmony_ci}
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_cibool GrRenderTargetProxy::canUseStencil(const GrCaps& caps) const {
93cb93a386Sopenharmony_ci    if (caps.avoidStencilBuffers() || this->wrapsVkSecondaryCB()) {
94cb93a386Sopenharmony_ci        return false;
95cb93a386Sopenharmony_ci    }
96cb93a386Sopenharmony_ci    if (!this->isInstantiated()) {
97cb93a386Sopenharmony_ci        if (this->isLazy() && this->backendFormat().backend() == GrBackendApi::kOpenGL) {
98cb93a386Sopenharmony_ci            // It's possible for wrapped GL render targets to not have stencil. We don't currently
99cb93a386Sopenharmony_ci            // have an exact way of knowing whether the target will be able to use stencil, so we do
100cb93a386Sopenharmony_ci            // the best we can: if a lazy GL proxy doesn't have a texture, then it might be a
101cb93a386Sopenharmony_ci            // wrapped target without stencil, so we conservatively block stencil.
102cb93a386Sopenharmony_ci            // FIXME: skbug.com/11943: SkSurfaceCharacterization needs a "canUseStencil" flag.
103cb93a386Sopenharmony_ci            return SkToBool(this->asTextureProxy());
104cb93a386Sopenharmony_ci        } else {
105cb93a386Sopenharmony_ci            // Otherwise the target will definitely not be wrapped. Ganesh is free to attach
106cb93a386Sopenharmony_ci            // stencils on internal render targets.
107cb93a386Sopenharmony_ci            return true;
108cb93a386Sopenharmony_ci        }
109cb93a386Sopenharmony_ci    }
110cb93a386Sopenharmony_ci    // Just ask the actual target if we can use stencil.
111cb93a386Sopenharmony_ci    GrRenderTarget* rt = this->peekRenderTarget();
112cb93a386Sopenharmony_ci    // The dmsaa attachment (if any) always supports stencil. The real question is whether the
113cb93a386Sopenharmony_ci    // non-dmsaa attachment supports stencil.
114cb93a386Sopenharmony_ci    bool useMSAASurface = rt->numSamples() > 1;
115cb93a386Sopenharmony_ci    return rt->getStencilAttachment(useMSAASurface) ||
116cb93a386Sopenharmony_ci           rt->canAttemptStencilAttachment(useMSAASurface);
117cb93a386Sopenharmony_ci}
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_cisk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
120cb93a386Sopenharmony_ci    sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, fSampleCnt,
121cb93a386Sopenharmony_ci                                                       GrRenderable::kYes, GrMipmapped::kNo);
122cb93a386Sopenharmony_ci    if (!surface) {
123cb93a386Sopenharmony_ci        return nullptr;
124cb93a386Sopenharmony_ci    }
125cb93a386Sopenharmony_ci    SkASSERT(surface->asRenderTarget());
126cb93a386Sopenharmony_ci    SkASSERT(!surface->asTexture());
127cb93a386Sopenharmony_ci    return surface;
128cb93a386Sopenharmony_ci}
129cb93a386Sopenharmony_ci
130cb93a386Sopenharmony_cisize_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
131cb93a386Sopenharmony_ci    int colorSamplesPerPixel = this->numSamples();
132cb93a386Sopenharmony_ci    if (colorSamplesPerPixel > 1) {
133cb93a386Sopenharmony_ci        // Add one for the resolve buffer.
134cb93a386Sopenharmony_ci        ++colorSamplesPerPixel;
135cb93a386Sopenharmony_ci    }
136cb93a386Sopenharmony_ci
137cb93a386Sopenharmony_ci    // TODO: do we have enough information to improve this worst case estimate?
138cb93a386Sopenharmony_ci    return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
139cb93a386Sopenharmony_ci                                  colorSamplesPerPixel, GrMipmapped::kNo, !this->priv().isExact());
140cb93a386Sopenharmony_ci}
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_cibool GrRenderTargetProxy::refsWrappedObjects() const {
143cb93a386Sopenharmony_ci    if (!this->isInstantiated()) {
144cb93a386Sopenharmony_ci        return false;
145cb93a386Sopenharmony_ci    }
146cb93a386Sopenharmony_ci
147cb93a386Sopenharmony_ci    GrSurface* surface = this->peekSurface();
148cb93a386Sopenharmony_ci    return surface->resourcePriv().refsWrappedObjects();
149cb93a386Sopenharmony_ci}
150cb93a386Sopenharmony_ci
151cb93a386Sopenharmony_ciGrSurfaceProxy::LazySurfaceDesc GrRenderTargetProxy::callbackDesc() const {
152cb93a386Sopenharmony_ci    // We only expect exactly sized lazy RT proxies.
153cb93a386Sopenharmony_ci    SkASSERT(!this->isFullyLazy());
154cb93a386Sopenharmony_ci    SkASSERT(this->isFunctionallyExact());
155cb93a386Sopenharmony_ci    return {
156cb93a386Sopenharmony_ci            this->dimensions(),
157cb93a386Sopenharmony_ci            SkBackingFit::kExact,
158cb93a386Sopenharmony_ci            GrRenderable::kYes,
159cb93a386Sopenharmony_ci            GrMipmapped::kNo,
160cb93a386Sopenharmony_ci            this->numSamples(),
161cb93a386Sopenharmony_ci            this->backendFormat(),
162cb93a386Sopenharmony_ci            GrTextureType::kNone,
163cb93a386Sopenharmony_ci            this->isProtected(),
164cb93a386Sopenharmony_ci            this->isBudgeted(),
165cb93a386Sopenharmony_ci    };
166cb93a386Sopenharmony_ci}
167cb93a386Sopenharmony_ci
168cb93a386Sopenharmony_ci#ifdef SK_DEBUG
169cb93a386Sopenharmony_civoid GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
170cb93a386Sopenharmony_ci    // We do not check that surface->asTexture returns null since, when replaying DDLs we
171cb93a386Sopenharmony_ci    // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
172cb93a386Sopenharmony_ci
173cb93a386Sopenharmony_ci    // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
174cb93a386Sopenharmony_ci    SkASSERT(surface->asRenderTarget());
175cb93a386Sopenharmony_ci    SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
176cb93a386Sopenharmony_ci
177cb93a386Sopenharmony_ci    GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
178cb93a386Sopenharmony_ci    GrInternalSurfaceFlags surfaceFlags = surface->flags();
179cb93a386Sopenharmony_ci    if (proxyFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0 && this->numSamples() == 1) {
180cb93a386Sopenharmony_ci        // Ganesh never internally creates FBO0 proxies or surfaces so this must be a wrapped
181cb93a386Sopenharmony_ci        // proxy. In this case, with no MSAA, rendering to FBO0 is strictly more limited than
182cb93a386Sopenharmony_ci        // rendering to an arbitrary surface so we allow a non-FBO0 surface to be matched with
183cb93a386Sopenharmony_ci        // the proxy.
184cb93a386Sopenharmony_ci        surfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
185cb93a386Sopenharmony_ci    }
186cb93a386Sopenharmony_ci    SkASSERT(((int)proxyFlags & kGrInternalRenderTargetFlagsMask) ==
187cb93a386Sopenharmony_ci             ((int)surfaceFlags & kGrInternalRenderTargetFlagsMask));
188cb93a386Sopenharmony_ci
189cb93a386Sopenharmony_ci    // We manually check the kVkRTSupportsInputAttachment since we only require it on the surface if
190cb93a386Sopenharmony_ci    // the proxy has it set. If the proxy doesn't have the flag it is legal for the surface to
191cb93a386Sopenharmony_ci    // have the flag.
192cb93a386Sopenharmony_ci    if (proxyFlags & GrInternalSurfaceFlags::kVkRTSupportsInputAttachment) {
193cb93a386Sopenharmony_ci        SkASSERT(surfaceFlags & GrInternalSurfaceFlags::kVkRTSupportsInputAttachment);
194cb93a386Sopenharmony_ci    }
195cb93a386Sopenharmony_ci}
196cb93a386Sopenharmony_ci#endif
197