Coverage Report

Created: 2024-05-20 07:14

/src/skia/src/gpu/ganesh/GrTextureProxy.cpp
Line
Count
Source (jump to first uncovered line)
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/ganesh/GrTextureProxy.h"
9
#include "src/gpu/ganesh/GrTextureProxyPriv.h"
10
11
#include "include/core/SkSize.h"
12
#include "include/gpu/GpuTypes.h"
13
#include "include/gpu/GrBackendSurface.h"
14
#include "include/gpu/GrDirectContext.h"
15
#include "include/gpu/GrTypes.h"
16
#include "src/gpu/SkBackingFit.h"
17
#include "src/gpu/ganesh/GrDeferredProxyUploader.h"
18
#include "src/gpu/ganesh/GrDirectContextPriv.h"
19
#include "src/gpu/ganesh/GrGpuResourcePriv.h"
20
#include "src/gpu/ganesh/GrProxyProvider.h"
21
#include "src/gpu/ganesh/GrSurface.h"
22
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h"
23
#include "src/gpu/ganesh/GrTexture.h"
24
25
#include <utility>
26
27
class GrOpFlushState;
28
29
// Deferred version - no data
30
GrTextureProxy::GrTextureProxy(const GrBackendFormat& format,
31
                               SkISize dimensions,
32
                               skgpu::Mipmapped mipmapped,
33
                               GrMipmapStatus mipmapStatus,
34
                               SkBackingFit fit,
35
                               skgpu::Budgeted budgeted,
36
                               GrProtected isProtected,
37
                               GrInternalSurfaceFlags surfaceFlags,
38
                               UseAllocator useAllocator,
39
                               GrDDLProvider creatingProvider,
40
                               std::string_view label)
41
        : INHERITED(
42
                  format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator, label)
43
        , fMipmapped(mipmapped)
44
        , fMipmapStatus(mipmapStatus) SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
45
        , fCreatingProvider(creatingProvider)
46
        , fProxyProvider(nullptr)
47
103k
        , fDeferredUploader(nullptr) {
48
103k
    SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
49
103k
    if (this->textureType() == GrTextureType::kExternal) {
50
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
51
0
    }
52
103k
}
GrTextureProxy::GrTextureProxy(GrBackendFormat const&, SkISize, skgpu::Mipmapped, GrMipmapStatus, SkBackingFit, skgpu::Budgeted, skgpu::Protected, GrInternalSurfaceFlags, GrSurfaceProxy::UseAllocator, GrDDLProvider, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Line
Count
Source
47
84.7k
        , fDeferredUploader(nullptr) {
48
84.7k
    SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
49
84.7k
    if (this->textureType() == GrTextureType::kExternal) {
50
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
51
0
    }
52
84.7k
}
GrTextureProxy::GrTextureProxy(GrBackendFormat const&, SkISize, skgpu::Mipmapped, GrMipmapStatus, SkBackingFit, skgpu::Budgeted, skgpu::Protected, GrInternalSurfaceFlags, GrSurfaceProxy::UseAllocator, GrDDLProvider, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Line
Count
Source
47
18.9k
        , fDeferredUploader(nullptr) {
48
18.9k
    SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
49
18.9k
    if (this->textureType() == GrTextureType::kExternal) {
50
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
51
0
    }
52
18.9k
}
53
54
// Lazy-callback version
55
GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
56
                               const GrBackendFormat& format,
57
                               SkISize dimensions,
58
                               skgpu::Mipmapped mipmapped,
59
                               GrMipmapStatus mipmapStatus,
60
                               SkBackingFit fit,
61
                               skgpu::Budgeted budgeted,
62
                               GrProtected isProtected,
63
                               GrInternalSurfaceFlags surfaceFlags,
64
                               UseAllocator useAllocator,
65
                               GrDDLProvider creatingProvider,
66
                               std::string_view label)
67
        : INHERITED(std::move(callback),
68
                    format,
69
                    dimensions,
70
                    fit,
71
                    budgeted,
72
                    isProtected,
73
                    surfaceFlags,
74
                    useAllocator,
75
                    label)
76
        , fMipmapped(mipmapped)
77
        , fMipmapStatus(mipmapStatus) SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
78
        , fCreatingProvider(creatingProvider)
79
        , fProxyProvider(nullptr)
80
60.3k
        , fDeferredUploader(nullptr) {
81
60.3k
    SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
82
60.3k
    if (this->textureType() == GrTextureType::kExternal) {
83
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
84
0
    }
85
60.3k
}
GrTextureProxy::GrTextureProxy(std::__1::function<GrSurfaceProxy::LazyCallbackResult (GrResourceProvider*, GrSurfaceProxy::LazySurfaceDesc const&)>&&, GrBackendFormat const&, SkISize, skgpu::Mipmapped, GrMipmapStatus, SkBackingFit, skgpu::Budgeted, skgpu::Protected, GrInternalSurfaceFlags, GrSurfaceProxy::UseAllocator, GrDDLProvider, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Line
Count
Source
80
285
        , fDeferredUploader(nullptr) {
81
285
    SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
82
285
    if (this->textureType() == GrTextureType::kExternal) {
83
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
84
0
    }
85
285
}
GrTextureProxy::GrTextureProxy(std::__1::function<GrSurfaceProxy::LazyCallbackResult (GrResourceProvider*, GrSurfaceProxy::LazySurfaceDesc const&)>&&, GrBackendFormat const&, SkISize, skgpu::Mipmapped, GrMipmapStatus, SkBackingFit, skgpu::Budgeted, skgpu::Protected, GrInternalSurfaceFlags, GrSurfaceProxy::UseAllocator, GrDDLProvider, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Line
Count
Source
80
60.0k
        , fDeferredUploader(nullptr) {
81
60.0k
    SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
82
60.0k
    if (this->textureType() == GrTextureType::kExternal) {
83
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
84
0
    }
85
60.0k
}
86
87
// Wrapped version
88
GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
89
                               UseAllocator useAllocator,
90
                               GrDDLProvider creatingProvider)
91
        : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
92
        , fMipmapped(fTarget->asTexture()->mipmapped())
93
        , fMipmapStatus(fTarget->asTexture()->mipmapStatus())
94
        SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
95
        , fCreatingProvider(creatingProvider)
96
        , fProxyProvider(nullptr)
97
10.5k
        , fDeferredUploader(nullptr) {
98
10.5k
    if (fTarget->getUniqueKey().isValid()) {
99
10.5k
        fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
100
10.5k
        fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
101
10.5k
    }
102
10.5k
    if (this->textureType() == GrTextureType::kExternal) {
103
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
104
0
    }
105
10.5k
}
Unexecuted instantiation: GrTextureProxy::GrTextureProxy(sk_sp<GrSurface>, GrSurfaceProxy::UseAllocator, GrDDLProvider)
GrTextureProxy::GrTextureProxy(sk_sp<GrSurface>, GrSurfaceProxy::UseAllocator, GrDDLProvider)
Line
Count
Source
97
10.5k
        , fDeferredUploader(nullptr) {
98
10.5k
    if (fTarget->getUniqueKey().isValid()) {
99
10.5k
        fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
100
10.5k
        fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
101
10.5k
    }
102
10.5k
    if (this->textureType() == GrTextureType::kExternal) {
103
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
104
0
    }
105
10.5k
}
106
107
174k
GrTextureProxy::~GrTextureProxy() {
108
    // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
109
    // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
110
174k
    fTarget = nullptr;
111
112
    // In DDL-mode, uniquely keyed proxies keep their key even after their originating
113
    // proxy provider has gone away. In that case there is noone to send the invalid key
114
    // message to (Note: in this case we don't want to remove its cached resource).
115
174k
    if (fUniqueKey.isValid() && fProxyProvider) {
116
63.6k
        fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
117
63.6k
                                                GrProxyProvider::InvalidateGPUResource::kNo);
118
110k
    } else {
119
110k
        SkASSERT(!fProxyProvider);
120
110k
    }
121
174k
}
122
123
253
bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
124
253
    if (this->isLazy()) {
125
0
        return false;
126
0
    }
127
253
    if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped,
128
253
                               fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
129
0
        return false;
130
0
    }
131
132
253
    SkASSERT(!this->peekRenderTarget());
133
253
    SkASSERT(this->peekTexture());
134
253
    return true;
135
253
}
GrTextureProxy::instantiate(GrResourceProvider*)
Line
Count
Source
123
253
bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
124
253
    if (this->isLazy()) {
125
0
        return false;
126
0
    }
127
253
    if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped,
128
253
                               fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
129
0
        return false;
130
0
    }
131
132
253
    SkASSERT(!this->peekRenderTarget());
133
253
    SkASSERT(this->peekTexture());
134
253
    return true;
135
253
}
Unexecuted instantiation: GrTextureProxy::instantiate(GrResourceProvider*)
136
137
1.49k
sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
138
1.49k
    sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo,
139
1.49k
                                                       fMipmapped);
140
1.49k
    if (!surface) {
141
0
        return nullptr;
142
0
    }
143
144
1.49k
    SkASSERT(!surface->asRenderTarget());
145
1.49k
    SkASSERT(surface->asTexture());
146
1.49k
    return surface;
147
1.49k
}
148
149
0
void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
150
0
    SkASSERT(!fTextureProxy->fDeferredUploader);
151
0
    fTextureProxy->fDeferredUploader = std::move(uploader);
152
0
}
Unexecuted instantiation: GrTextureProxyPriv::setDeferredUploader(std::__1::unique_ptr<GrDeferredProxyUploader, std::__1::default_delete<GrDeferredProxyUploader> >)
Unexecuted instantiation: GrTextureProxyPriv::setDeferredUploader(std::__1::unique_ptr<GrDeferredProxyUploader, std::__1::default_delete<GrDeferredProxyUploader> >)
153
154
0
void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
155
    // The texture proxy's contents may already have been uploaded or instantiation may have failed
156
0
    if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
157
0
        fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
158
0
    }
159
0
}
160
161
0
void GrTextureProxyPriv::resetDeferredUploader() {
162
0
    SkASSERT(fTextureProxy->fDeferredUploader);
163
0
    fTextureProxy->fDeferredUploader.reset();
164
0
}
Unexecuted instantiation: GrTextureProxyPriv::resetDeferredUploader()
Unexecuted instantiation: GrTextureProxyPriv::resetDeferredUploader()
165
166
282k
skgpu::Mipmapped GrTextureProxy::mipmapped() const {
167
282k
    if (this->isInstantiated()) {
168
4.01k
        return this->peekTexture()->mipmapped();
169
4.01k
    }
170
278k
    return fMipmapped;
171
282k
}
172
173
378k
GrTextureType GrTextureProxy::textureType() const { return this->backendFormat().textureType(); }
174
175
886
size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
176
886
    return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
177
886
                                  /*colorSamplesPerPixel=*/1, this->proxyMipmapped(),
178
886
                                  !this->priv().isExact());
179
886
}
180
181
bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
182
14.1k
                                                        const GrSurfaceProxy* second) {
183
    // In order to be compatible, the proxies should also have the same texture type. This is
184
    // checked explicitly since the GrBackendFormat == operator does not compare texture type
185
14.1k
    return first->backendFormat().textureType() == second->backendFormat().textureType() &&
186
14.1k
           first->backendFormat() == second->backendFormat();
187
14.1k
}
188
189
64.1k
void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const skgpu::UniqueKey& key) {
190
64.1k
    SkASSERT(key.isValid());
191
64.1k
    SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
192
193
64.1k
    if (fTarget && fSyncTargetKey) {
194
64.1k
        if (!fTarget->getUniqueKey().isValid()) {
195
53.5k
            fTarget->resourcePriv().setUniqueKey(key);
196
53.5k
        }
197
64.1k
        SkASSERT(fTarget->getUniqueKey() == key);
198
64.1k
    }
199
200
64.1k
    fUniqueKey = key;
201
64.1k
    fProxyProvider = proxyProvider;
202
64.1k
}
GrTextureProxy::setUniqueKey(GrProxyProvider*, skgpu::UniqueKey const&)
Line
Count
Source
189
64.1k
void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const skgpu::UniqueKey& key) {
190
64.1k
    SkASSERT(key.isValid());
191
64.1k
    SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
192
193
64.1k
    if (fTarget && fSyncTargetKey) {
194
64.1k
        if (!fTarget->getUniqueKey().isValid()) {
195
53.5k
            fTarget->resourcePriv().setUniqueKey(key);
196
53.5k
        }
197
64.1k
        SkASSERT(fTarget->getUniqueKey() == key);
198
64.1k
    }
199
200
64.1k
    fUniqueKey = key;
201
64.1k
    fProxyProvider = proxyProvider;
202
64.1k
}
Unexecuted instantiation: GrTextureProxy::setUniqueKey(GrProxyProvider*, skgpu::UniqueKey const&)
203
204
64.1k
void GrTextureProxy::clearUniqueKey() {
205
64.1k
    fUniqueKey.reset();
206
64.1k
    fProxyProvider = nullptr;
207
64.1k
}
208
209
60.0k
GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const {
210
60.0k
    SkISize dims;
211
60.0k
    SkBackingFit fit;
212
60.0k
    if (this->isFullyLazy()) {
213
0
        fit = SkBackingFit::kApprox;
214
0
        dims = {-1, -1};
215
60.0k
    } else {
216
60.0k
        fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
217
60.0k
        dims = this->dimensions();
218
60.0k
    }
219
60.0k
    return {
220
60.0k
            dims,
221
60.0k
            fit,
222
60.0k
            GrRenderable::kNo,
223
60.0k
            fMipmapped,
224
60.0k
            1,
225
60.0k
            this->backendFormat(),
226
60.0k
            this->textureType(),
227
60.0k
            this->isProtected(),
228
60.0k
            this->isBudgeted(),
229
60.0k
            this->getLabel(),
230
60.0k
    };
231
60.0k
}
232
233
#ifdef SK_DEBUG
234
0
void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
235
0
    SkASSERT(!surface->asRenderTarget());
236
237
    // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
238
0
    SkASSERT(surface->asTexture());
239
    // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
240
0
    SkASSERT(skgpu::Mipmapped::kNo == this->proxyMipmapped() ||
241
0
             skgpu::Mipmapped::kYes == surface->asTexture()->mipmapped());
242
243
0
    SkASSERT(surface->asTexture()->textureType() == this->textureType());
244
245
0
    GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
246
0
    GrInternalSurfaceFlags surfaceFlags = surface->flags();
247
0
    SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
248
0
             ((int)surfaceFlags & kGrInternalTextureFlagsMask));
249
0
}
250
251
#endif
252