| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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/graphite/TextureProxy.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/Recorder.h" |
| 11 |
#include "src/core/SkMipmap.h" |
| 12 |
#include "src/gpu/graphite/Caps.h" |
| 13 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 14 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 15 |
#include "src/gpu/graphite/ScratchResourceManager.h" |
| 16 |
#include "src/gpu/graphite/Texture.h" |
| 17 |
#include "src/gpu/graphite/TextureUtils.h" |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
TextureProxy::TextureProxy(SkISize dimensions, |
| 22 |
const TextureInfo& info, |
| 23 |
std::string_view label, |
| 24 |
skgpu::Budgeted budgeted) |
| 25 |
: fDimensions(dimensions) |
| 26 |
, fInfo(info) |
| 27 |
, fLabel(label) |
| 28 |
, fBudgeted(budgeted) |
| 29 |
, fVolatile(Volatile::kNo) { |
| 30 |
SkASSERT(fInfo.isValid()); |
| 31 |
} |
| 32 |
|
| 33 |
TextureProxy::TextureProxy(sk_sp<Texture> texture) |
| 34 |
: fDimensions(texture->dimensions()) |
| 35 |
, fInfo(texture->textureInfo()) |
| 36 |
, fLabel(texture->getLabel()) |
| 37 |
, fBudgeted(texture->budgeted()) |
| 38 |
, fVolatile(Volatile::kNo) |
| 39 |
, fTexture(std::move(texture)) { |
| 40 |
SkASSERT(fInfo.isValid()); |
| 41 |
} |
| 42 |
|
| 43 |
TextureProxy::TextureProxy(SkISize dimensions, |
| 44 |
const TextureInfo& textureInfo, |
| 45 |
skgpu::Budgeted budgeted, |
| 46 |
Volatile isVolatile, |
| 47 |
LazyInstantiateCallback&& callback) |
| 48 |
: fDimensions(dimensions) |
| 49 |
, fInfo(textureInfo) |
| 50 |
, fBudgeted(budgeted) |
| 51 |
, fVolatile(isVolatile) |
| 52 |
, fLazyInstantiateCallback(std::move(callback)) { |
| 53 |
SkASSERT(fInfo.isValid()); |
| 54 |
SkASSERT(fLazyInstantiateCallback); |
| 55 |
} |
| 56 |
|
| 57 |
TextureProxy::~TextureProxy() {} |
| 58 |
|
| 59 |
SkISize TextureProxy::dimensions() const { |
| 60 |
SkASSERT(!this->isFullyLazy() || this->isInstantiated()); |
| 61 |
return this->isInstantiated() ? fTexture->dimensions() : fDimensions; |
| 62 |
} |
| 63 |
|
| 64 |
bool TextureProxy::isLazy() const { |
| 65 |
return SkToBool(fLazyInstantiateCallback); |
| 66 |
} |
| 67 |
|
| 68 |
bool TextureProxy::isFullyLazy() const { |
| 69 |
bool result = fDimensions.width() < 0; |
| 70 |
SkASSERT(result == (fDimensions.height() < 0)); |
| 71 |
SkASSERT(!result || this->isLazy()); |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
bool TextureProxy::isVolatile() const { |
| 76 |
SkASSERT(fVolatile == Volatile::kNo || SkToBool(fLazyInstantiateCallback)); |
| 77 |
|
| 78 |
return fVolatile == Volatile::kYes; |
| 79 |
} |
| 80 |
|
| 81 |
size_t TextureProxy::uninstantiatedGpuMemorySize() const { |
| 82 |
return ComputeSize(fDimensions, fInfo); |
| 83 |
} |
| 84 |
|
| 85 |
bool TextureProxy::instantiate(ResourceProvider* resourceProvider) { |
| 86 |
SkASSERT(!this->isLazy()); |
| 87 |
|
| 88 |
if (fTexture) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
// TODO(389908374): Once all tasks use the ScratchResourceManager, this can be updated to just |
| 93 |
// finding and creating a non-shareable AND non-budgeted texture. |
| 94 |
fTexture = resourceProvider->findOrCreateNonShareableTexture( |
| 95 |
fDimensions, fInfo, fLabel, fBudgeted); |
| 96 |
if (!fTexture) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
SkDEBUGCODE(this->validateTexture(fTexture.get())); |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
bool TextureProxy::lazyInstantiate(ResourceProvider* resourceProvider) { |
| 104 |
SkASSERT(this->isLazy()); |
| 105 |
|
| 106 |
if (fTexture) { |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
fTexture = fLazyInstantiateCallback(resourceProvider); |
| 111 |
if (!fTexture) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
SkDEBUGCODE(this->validateTexture(fTexture.get())); |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
bool TextureProxy::InstantiateIfNotLazy(ResourceProvider* resourceProvider, |
| 119 |
TextureProxy* textureProxy) { |
| 120 |
if (textureProxy->isLazy()) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
return textureProxy->instantiate(resourceProvider); |
| 125 |
} |
| 126 |
|
| 127 |
bool TextureProxy::InstantiateIfNotLazy(ScratchResourceManager* scratchManager, |
| 128 |
TextureProxy* textureProxy) { |
| 129 |
if (textureProxy->isLazy() || textureProxy->isInstantiated()) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
textureProxy->fTexture = scratchManager->getScratchTexture(textureProxy->dimensions(), |
| 134 |
textureProxy->textureInfo(), |
| 135 |
textureProxy->fLabel); |
| 136 |
if (!textureProxy->fTexture) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
SkDEBUGCODE(textureProxy->validateTexture(textureProxy->fTexture.get())); |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
void TextureProxy::deinstantiate() { |
| 145 |
SkASSERT(fVolatile == Volatile::kYes && SkToBool(fLazyInstantiateCallback)); |
| 146 |
|
| 147 |
fTexture.reset(); |
| 148 |
} |
| 149 |
|
| 150 |
sk_sp<Texture> TextureProxy::refTexture() const { |
| 151 |
return fTexture; |
| 152 |
} |
| 153 |
|
| 154 |
const Texture* TextureProxy::texture() const { |
| 155 |
return fTexture.get(); |
| 156 |
} |
| 157 |
|
| 158 |
sk_sp<TextureProxy> TextureProxy::Make(const Caps* caps, |
| 159 |
ResourceProvider* resourceProvider, |
| 160 |
SkISize dimensions, |
| 161 |
const TextureInfo& textureInfo, |
| 162 |
std::string_view label, |
| 163 |
skgpu::Budgeted budgeted) { |
| 164 |
if (dimensions.width() < 1 || dimensions.height() < 1 || |
| 165 |
dimensions.width() > caps->maxTextureSize() || |
| 166 |
dimensions.height() > caps->maxTextureSize() || |
| 167 |
!textureInfo.isValid()) { |
| 168 |
return nullptr; |
| 169 |
} |
| 170 |
|
| 171 |
sk_sp<TextureProxy> proxy {new TextureProxy(dimensions, textureInfo, label, budgeted)}; |
| 172 |
if (budgeted == Budgeted::kNo) { |
| 173 |
// Instantiate immediately to avoid races later on if the client starts to use the wrapping |
| 174 |
// object on multiple threads. |
| 175 |
if (!proxy->instantiate(resourceProvider)) { |
| 176 |
return nullptr; |
| 177 |
} |
| 178 |
} |
| 179 |
return proxy; |
| 180 |
} |
| 181 |
|
| 182 |
sk_sp<TextureProxy> TextureProxy::MakeLazy(const Caps* caps, |
| 183 |
SkISize dimensions, |
| 184 |
const TextureInfo& textureInfo, |
| 185 |
skgpu::Budgeted budgeted, |
| 186 |
Volatile isVolatile, |
| 187 |
LazyInstantiateCallback&& callback) { |
| 188 |
SkASSERT(textureInfo.isValid()); |
| 189 |
if (dimensions.width() < 1 || dimensions.height() < 1 || |
| 190 |
dimensions.width() > caps->maxTextureSize() || |
| 191 |
dimensions.height() > caps->maxTextureSize()) { |
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
return sk_sp<TextureProxy>(new TextureProxy(dimensions, |
| 196 |
textureInfo, |
| 197 |
budgeted, |
| 198 |
isVolatile, |
| 199 |
std::move(callback))); |
| 200 |
} |
| 201 |
|
| 202 |
sk_sp<TextureProxy> TextureProxy::MakeFullyLazy(const TextureInfo& textureInfo, |
| 203 |
skgpu::Budgeted budgeted, |
| 204 |
Volatile isVolatile, |
| 205 |
LazyInstantiateCallback&& callback) { |
| 206 |
SkASSERT(textureInfo.isValid()); |
| 207 |
|
| 208 |
return sk_sp<TextureProxy>(new TextureProxy(SkISize::Make(-1, -1), |
| 209 |
textureInfo, |
| 210 |
budgeted, |
| 211 |
isVolatile, |
| 212 |
std::move(callback))); |
| 213 |
} |
| 214 |
|
| 215 |
sk_sp<TextureProxy> TextureProxy::Wrap(sk_sp<Texture> texture) { |
| 216 |
return sk_sp<TextureProxy>(new TextureProxy(std::move(texture))); |
| 217 |
} |
| 218 |
|
| 219 |
#ifdef SK_DEBUG |
| 220 |
void TextureProxy::validateTexture(const Texture* texture) { |
| 221 |
SkASSERT(this->isFullyLazy() || fDimensions == texture->dimensions()); |
| 222 |
SkASSERTF(fInfo.canBeFulfilledBy(texture->textureInfo()), |
| 223 |
"proxy->fInfo[%s] incompatible with texture->fInfo[%s]", |
| 224 |
fInfo.toString().c_str(), |
| 225 |
texture->textureInfo().toString().c_str()); |
| 226 |
} |
| 227 |
#endif |
| 228 |
|
| 229 |
} // namespace skgpu::graphite |