• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrTextureRenderTargetProxy.h"
9 
10 #include "include/gpu/GrTexture.h"
11 #include "src/gpu/GrCaps.h"
12 #include "src/gpu/GrRenderTarget.h"
13 #include "src/gpu/GrSurfacePriv.h"
14 #include "src/gpu/GrSurfaceProxyPriv.h"
15 #include "src/gpu/GrTexturePriv.h"
16 #include "src/gpu/GrTextureProxyPriv.h"
17 
18 // Deferred version
19 // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
20 // GrRenderTargetProxy) so its constructor must be explicitly called.
GrTextureRenderTargetProxy(const GrCaps & caps,const GrBackendFormat & format,const GrSurfaceDesc & desc,int sampleCnt,GrSurfaceOrigin origin,GrMipMapped mipMapped,GrMipMapsStatus mipMapsStatus,const GrSwizzle & texSwizzle,const GrSwizzle & outSwizzle,SkBackingFit fit,SkBudgeted budgeted,GrProtected isProtected,GrInternalSurfaceFlags surfaceFlags)21 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
22                                                        const GrBackendFormat& format,
23                                                        const GrSurfaceDesc& desc,
24                                                        int sampleCnt,
25                                                        GrSurfaceOrigin origin,
26                                                        GrMipMapped mipMapped,
27                                                        GrMipMapsStatus mipMapsStatus,
28                                                        const GrSwizzle& texSwizzle,
29                                                        const GrSwizzle& outSwizzle,
30                                                        SkBackingFit fit,
31                                                        SkBudgeted budgeted,
32                                                        GrProtected isProtected,
33                                                        GrInternalSurfaceFlags surfaceFlags)
34         : GrSurfaceProxy(format, desc, GrRenderable::kYes, origin, texSwizzle, fit, budgeted,
35                          isProtected, surfaceFlags)
36         // for now textures w/ data are always wrapped
37         , GrRenderTargetProxy(caps, format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
38                               budgeted, isProtected, surfaceFlags)
39         , GrTextureProxy(format, desc, origin, mipMapped, mipMapsStatus, texSwizzle, fit, budgeted,
40                          isProtected, surfaceFlags) {}
41 
42 // Lazy-callback version
GrTextureRenderTargetProxy(LazyInstantiateCallback && callback,LazyInstantiationType lazyType,const GrBackendFormat & format,const GrSurfaceDesc & desc,int sampleCnt,GrSurfaceOrigin origin,GrMipMapped mipMapped,GrMipMapsStatus mipMapsStatus,const GrSwizzle & texSwizzle,const GrSwizzle & outSwizzle,SkBackingFit fit,SkBudgeted budgeted,GrProtected isProtected,GrInternalSurfaceFlags surfaceFlags)43 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
44                                                        LazyInstantiationType lazyType,
45                                                        const GrBackendFormat& format,
46                                                        const GrSurfaceDesc& desc,
47                                                        int sampleCnt,
48                                                        GrSurfaceOrigin origin,
49                                                        GrMipMapped mipMapped,
50                                                        GrMipMapsStatus mipMapsStatus,
51                                                        const GrSwizzle& texSwizzle,
52                                                        const GrSwizzle& outSwizzle,
53                                                        SkBackingFit fit,
54                                                        SkBudgeted budgeted,
55                                                        GrProtected isProtected,
56                                                        GrInternalSurfaceFlags surfaceFlags)
57         : GrSurfaceProxy(std::move(callback), lazyType, format, desc, GrRenderable::kYes, origin,
58                          texSwizzle, fit, budgeted, isProtected, surfaceFlags)
59         // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
60         // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
61         , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, sampleCnt, origin,
62                               texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags,
63                               WrapsVkSecondaryCB::kNo)
64         , GrTextureProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, mipMapped,
65                          mipMapsStatus, texSwizzle, fit, budgeted, isProtected, surfaceFlags) {}
66 
67 // Wrapped version
68 // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
69 // GrRenderTargetProxy) so its constructor must be explicitly called.
GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,GrSurfaceOrigin origin,const GrSwizzle & texSwizzle,const GrSwizzle & outSwizzle)70 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
71                                                        GrSurfaceOrigin origin,
72                                                        const GrSwizzle& texSwizzle,
73                                                        const GrSwizzle& outSwizzle)
74         : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact)
75         , GrRenderTargetProxy(surf, origin, texSwizzle, outSwizzle)
76         , GrTextureProxy(surf, origin, texSwizzle) {
77     SkASSERT(surf->asTexture());
78     SkASSERT(surf->asRenderTarget());
79 }
80 
onUninstantiatedGpuMemorySize() const81 size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
82     int colorSamplesPerPixel = this->numSamples();
83     if (colorSamplesPerPixel > 1) {
84         // Add one to account for the resolve buffer.
85         ++colorSamplesPerPixel;
86     }
87 
88     // TODO: do we have enough information to improve this worst case estimate?
89     return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
90                                   colorSamplesPerPixel, this->proxyMipMapped(),
91                                   !this->priv().isExact());
92 }
93 
instantiate(GrResourceProvider * resourceProvider)94 bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
95     if (LazyState::kNot != this->lazyInstantiationState()) {
96         return false;
97     }
98 
99     const GrUniqueKey& key = this->getUniqueKey();
100 
101     if (!this->instantiateImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
102                                GrRenderable::kYes, this->mipMapped(),
103                                key.isValid() ? &key : nullptr)) {
104         return false;
105     }
106     if (key.isValid()) {
107         SkASSERT(key == this->getUniqueKey());
108     }
109 
110     SkASSERT(this->peekRenderTarget());
111     SkASSERT(this->peekTexture());
112 
113     return true;
114 }
115 
createSurface(GrResourceProvider * resourceProvider) const116 sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
117                                                     GrResourceProvider* resourceProvider) const {
118     sk_sp<GrSurface> surface =
119             this->createSurfaceImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
120                                     GrRenderable::kYes, this->mipMapped());
121     if (!surface) {
122         return nullptr;
123     }
124     SkASSERT(surface->asRenderTarget());
125     SkASSERT(surface->asTexture());
126 
127     return surface;
128 }
129 
130 #ifdef SK_DEBUG
onValidateSurface(const GrSurface * surface)131 void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
132     // Anything checked here should also be checking the GrTextureProxy version
133     SkASSERT(surface->asTexture());
134     SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
135              GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
136 
137     // Anything checked here should also be checking the GrRenderTargetProxy version
138     SkASSERT(surface->asRenderTarget());
139     SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
140 
141     SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
142 
143     GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
144     GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
145 
146     // Only non-RT textures can be read only.
147     SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
148     SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
149 
150     SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
151              (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
152     SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
153              (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
154 }
155 #endif
156 
157