| 1 |
/* |
| 2 |
* Copyright 2022 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/task/UploadTask.h" |
| 9 |
|
| 10 |
#include "include/core/SkColorType.h" |
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkSize.h" |
| 14 |
#include "include/core/SkTextureCompressionType.h" |
| 15 |
#include "include/gpu/GpuTypes.h" |
| 16 |
#include "include/gpu/graphite/Recorder.h" |
| 17 |
#include "include/gpu/graphite/TextureInfo.h" |
| 18 |
#include "include/private/base/SkAlign.h" |
| 19 |
#include "include/private/base/SkAssert.h" |
| 20 |
#include "include/private/base/SkDebug.h" |
| 21 |
#include "include/private/base/SkTemplates.h" |
| 22 |
#include "src/core/SkAutoPixmapStorage.h" |
| 23 |
#include "src/core/SkCompressedDataUtils.h" |
| 24 |
#include "src/core/SkConvertPixels.h" |
| 25 |
#include "src/core/SkMipmap.h" |
| 26 |
#include "src/core/SkTraceEvent.h" // IWYU pragma: keep |
| 27 |
#include "src/gpu/DataUtils.h" |
| 28 |
#include "src/gpu/graphite/Caps.h" |
| 29 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 30 |
#include "src/gpu/graphite/Log.h" |
| 31 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 32 |
#include "src/gpu/graphite/Texture.h" // IWYU pragma: keep |
| 33 |
#include "src/gpu/graphite/TextureFormat.h" |
| 34 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 35 |
#include "src/gpu/graphite/TextureProxy.h" |
| 36 |
#include "src/gpu/graphite/UploadBufferManager.h" |
| 37 |
|
| 38 |
#include <algorithm> |
| 39 |
#include <cstdint> |
| 40 |
#include <tuple> |
| 41 |
#include <utility> |
| 42 |
#include <vector> |
| 43 |
|
| 44 |
using namespace skia_private; |
| 45 |
|
| 46 |
namespace skgpu::graphite { |
| 47 |
|
| 48 |
// Returns total buffer size to allocate, and required offset alignment of that allocation. |
| 49 |
// Updates 'levelOffsetsAndRowBytes' with offsets relative to start of the allocation, as well as |
| 50 |
// the aligned destination rowBytes for each level. |
| 51 |
std::pair<size_t, size_t> compute_combined_buffer_size( |
| 52 |
const Caps* caps, |
| 53 |
int mipLevelCount, |
| 54 |
size_t bytesPerBlock, |
| 55 |
const SkISize& baseDimensions, |
| 56 |
SkTextureCompressionType compressionType, |
| 57 |
TArray<std::pair<size_t, size_t>>* levelOffsetsAndRowBytes) { |
| 58 |
SkASSERT(levelOffsetsAndRowBytes && levelOffsetsAndRowBytes->empty()); |
| 59 |
SkASSERT(mipLevelCount >= 1); |
| 60 |
|
| 61 |
SkISize compressedBlockDimensions = CompressedDimensionsInBlocks(compressionType, |
| 62 |
baseDimensions); |
| 63 |
|
| 64 |
size_t minTransferBufferAlignment = |
| 65 |
std::max(bytesPerBlock, caps->requiredTransferBufferAlignment()); |
| 66 |
size_t alignedBytesPerRow = |
| 67 |
caps->getAlignedTextureDataRowBytes(compressedBlockDimensions.width() * bytesPerBlock); |
| 68 |
|
| 69 |
levelOffsetsAndRowBytes->push_back({0, alignedBytesPerRow}); |
| 70 |
size_t combinedBufferSize = SkAlignTo(alignedBytesPerRow * baseDimensions.height(), |
| 71 |
minTransferBufferAlignment); |
| 72 |
SkISize levelDimensions = baseDimensions; |
| 73 |
|
| 74 |
for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; ++currentMipLevel) { |
| 75 |
levelDimensions = {std::max(1, levelDimensions.width() / 2), |
| 76 |
std::max(1, levelDimensions.height() / 2)}; |
| 77 |
compressedBlockDimensions = CompressedDimensionsInBlocks(compressionType, levelDimensions); |
| 78 |
alignedBytesPerRow = caps->getAlignedTextureDataRowBytes( |
| 79 |
compressedBlockDimensions.width() * bytesPerBlock); |
| 80 |
size_t alignedSize = SkAlignTo(alignedBytesPerRow * compressedBlockDimensions.height(), |
| 81 |
minTransferBufferAlignment); |
| 82 |
SkASSERT(combinedBufferSize % minTransferBufferAlignment == 0); |
| 83 |
|
| 84 |
levelOffsetsAndRowBytes->push_back({combinedBufferSize, alignedBytesPerRow}); |
| 85 |
combinedBufferSize += alignedSize; |
| 86 |
} |
| 87 |
|
| 88 |
SkASSERT(levelOffsetsAndRowBytes->size() == mipLevelCount); |
| 89 |
SkASSERT(combinedBufferSize % minTransferBufferAlignment == 0); |
| 90 |
return {combinedBufferSize, minTransferBufferAlignment}; |
| 91 |
} |
| 92 |
|
| 93 |
UploadSource::UploadSource() : fCompression(SkTextureCompressionType::kNone) {} |
| 94 |
UploadSource::UploadSource(UploadSource&&) = default; |
| 95 |
UploadSource& UploadSource::operator=(UploadSource&&) = default; |
| 96 |
UploadSource::~UploadSource() = default; |
| 97 |
|
| 98 |
UploadSource UploadSource::Make(const Caps* caps, |
| 99 |
const TextureProxy& textureProxy, |
| 100 |
const SkColorInfo& srcColorInfo, |
| 101 |
const SkColorInfo& dstColorInfo, |
| 102 |
SkSpan<const MipLevel> levels, |
| 103 |
const SkIRect& dstRect) { |
| 104 |
const TextureInfo& texInfo = textureProxy.textureInfo(); |
| 105 |
|
| 106 |
SkASSERT(caps->isTexturable(texInfo)); |
| 107 |
SkASSERT(caps->areColorTypeAndTextureInfoCompatible(dstColorInfo.colorType(), texInfo)); |
| 108 |
|
| 109 |
unsigned int mipLevelCount = levels.size(); |
| 110 |
// The assumption is either that we have no mipmaps, or that our rect is the entire texture |
| 111 |
SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(textureProxy.dimensions())); |
| 112 |
|
| 113 |
// We assume that if the texture has mip levels, we either upload to all the levels or just the |
| 114 |
// first. |
| 115 |
#ifdef SK_DEBUG |
| 116 |
unsigned int numExpectedLevels = 1; |
| 117 |
if (texInfo.mipmapped() == Mipmapped::kYes) { |
| 118 |
numExpectedLevels = SkMipmap::ComputeLevelCount(textureProxy.dimensions()) + 1; |
| 119 |
} |
| 120 |
SkASSERT(mipLevelCount == 1 || mipLevelCount == numExpectedLevels); |
| 121 |
#endif |
| 122 |
|
| 123 |
if (dstRect.isEmpty()) { |
| 124 |
return Invalid(); |
| 125 |
} |
| 126 |
|
| 127 |
UploadSource source; |
| 128 |
for (unsigned int i = 0; i < mipLevelCount; ++i) { |
| 129 |
// We do not allow any gaps in the mip data |
| 130 |
if (!levels[i].fPixels) { |
| 131 |
return Invalid(); |
| 132 |
} |
| 133 |
source.fLevels.push_back(levels[i]); |
| 134 |
} |
| 135 |
|
| 136 |
SkColorType supportedColorType; |
| 137 |
bool isRGB888Format; |
| 138 |
std::tie(supportedColorType, isRGB888Format) = caps->supportedTransferColorType( |
| 139 |
dstColorInfo.colorType(), texInfo); |
| 140 |
if (supportedColorType == kUnknown_SkColorType) { |
| 141 |
return Invalid(); |
| 142 |
} |
| 143 |
|
| 144 |
SkASSERT(!source.isRGB888Format() || (supportedColorType == kRGB_888x_SkColorType && |
| 145 |
dstColorInfo.colorType() == kRGB_888x_SkColorType)); |
| 146 |
|
| 147 |
constexpr size_t kRGB888Bytes = 3; |
| 148 |
|
| 149 |
source.fIsRGB888Format = isRGB888Format; |
| 150 |
source.fBytesPerPixel = |
| 151 |
isRGB888Format ? kRGB888Bytes : SkColorTypeBytesPerPixel(supportedColorType); |
| 152 |
source.fCanUploadOnHost = |
| 153 |
textureProxy.isInstantiated() ? textureProxy.texture()->canUploadOnHost(source) : false; |
| 154 |
|
| 155 |
return source; |
| 156 |
} |
| 157 |
|
| 158 |
UploadSource UploadSource::MakeCompressed(const Caps* caps, |
| 159 |
const TextureProxy& textureProxy, |
| 160 |
const void* data, |
| 161 |
size_t dataSize) { |
| 162 |
if (!data) { |
| 163 |
return Invalid(); // no data to upload |
| 164 |
} |
| 165 |
|
| 166 |
const TextureInfo& texInfo = textureProxy.textureInfo(); |
| 167 |
SkASSERT(caps->isTexturable(texInfo)); |
| 168 |
|
| 169 |
SkTextureCompressionType compression = |
| 170 |
TextureFormatCompressionType(TextureInfoPriv::ViewFormat(texInfo)); |
| 171 |
if (compression == SkTextureCompressionType::kNone) { |
| 172 |
return Invalid(); |
| 173 |
} |
| 174 |
|
| 175 |
// Create a transfer buffer and fill with data. |
| 176 |
const SkISize dimensions = textureProxy.dimensions(); |
| 177 |
skia_private::STArray<16, size_t> srcMipOffsets; |
| 178 |
SkDEBUGCODE(size_t computedSize =) SkCompressedDataSize( |
| 179 |
compression, dimensions, &srcMipOffsets, texInfo.mipmapped() == Mipmapped::kYes); |
| 180 |
SkASSERT(computedSize == dataSize); |
| 181 |
|
| 182 |
const unsigned int mipLevelCount = srcMipOffsets.size(); |
| 183 |
|
| 184 |
UploadSource source; |
| 185 |
source.fLevels.resize(mipLevelCount); |
| 186 |
for (unsigned int i = 0; i < mipLevelCount; ++i) { |
| 187 |
source.fLevels[i].fPixels = SkTAddOffset<const void>(data, srcMipOffsets[i]); |
| 188 |
source.fLevels[i].fRowBytes = 0; // Tightly packed |
| 189 |
} |
| 190 |
|
| 191 |
source.fCompression = compression; |
| 192 |
source.fBytesPerPixel = SkCompressedBlockSize(compression); |
| 193 |
source.fCanUploadOnHost = |
| 194 |
textureProxy.isInstantiated() ? textureProxy.texture()->canUploadOnHost(source) : false; |
| 195 |
|
| 196 |
return source; |
| 197 |
} |
| 198 |
|
| 199 |
UploadInstance::UploadInstance() = default; |
| 200 |
UploadInstance::UploadInstance(UploadInstance&&) = default; |
| 201 |
UploadInstance& UploadInstance::operator=(UploadInstance&&) = default; |
| 202 |
UploadInstance::~UploadInstance() = default; |
| 203 |
|
| 204 |
UploadInstance::UploadInstance(const Buffer* buffer, |
| 205 |
size_t bytesPerPixel, |
| 206 |
sk_sp<TextureProxy> textureProxy, |
| 207 |
std::unique_ptr<ConditionalUploadContext> condContext) |
| 208 |
: fBuffer(buffer) |
| 209 |
, fBytesPerPixel(bytesPerPixel) |
| 210 |
, fTextureProxy(textureProxy) |
| 211 |
, fConditionalContext(std::move(condContext)) {} |
| 212 |
|
| 213 |
UploadInstance UploadInstance::Make(Recorder* recorder, |
| 214 |
sk_sp<TextureProxy> textureProxy, |
| 215 |
const SkColorInfo& srcColorInfo, |
| 216 |
const SkColorInfo& dstColorInfo, |
| 217 |
const UploadSource& source, |
| 218 |
const SkIRect& dstRect, |
| 219 |
std::unique_ptr<ConditionalUploadContext> condContext) { |
| 220 |
const Caps* caps = recorder->priv().caps(); |
| 221 |
SkSpan<const MipLevel> levels = source.levels(); |
| 222 |
uint32_t mipLevelCount = static_cast<uint32_t>(levels.size()); |
| 223 |
|
| 224 |
TArray<std::pair<size_t, size_t>> levelOffsetsAndRowBytes(mipLevelCount); |
| 225 |
|
| 226 |
auto [combinedBufferSize, minAlignment] = |
| 227 |
compute_combined_buffer_size(caps, |
| 228 |
mipLevelCount, |
| 229 |
source.bytesPerPixel(), |
| 230 |
dstRect.size(), |
| 231 |
source.compression(), |
| 232 |
&levelOffsetsAndRowBytes); |
| 233 |
SkASSERT(combinedBufferSize); |
| 234 |
|
| 235 |
UploadBufferManager* bufferMgr = recorder->priv().uploadBufferManager(); |
| 236 |
auto [writer, bufferInfo] = bufferMgr->getTextureUploadWriter(combinedBufferSize, minAlignment); |
| 237 |
if (!writer) { |
| 238 |
SKGPU_LOG_W("Failed to get write-mapped buffer for texture upload of size %zu", |
| 239 |
combinedBufferSize); |
| 240 |
return Invalid(); |
| 241 |
} |
| 242 |
|
| 243 |
UploadInstance upload{bufferInfo.fBuffer, |
| 244 |
source.bytesPerPixel(), |
| 245 |
std::move(textureProxy), |
| 246 |
std::move(condContext)}; |
| 247 |
|
| 248 |
// Fill in copy data |
| 249 |
int32_t currentWidth = dstRect.width(); |
| 250 |
int32_t currentHeight = dstRect.height(); |
| 251 |
bool needsConversion = (srcColorInfo != dstColorInfo); |
| 252 |
for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 253 |
const size_t trimRowBytes = currentWidth * source.bytesPerPixel(); |
| 254 |
const size_t srcRowBytes = levels[currentMipLevel].fRowBytes; |
| 255 |
const auto [mipOffset, dstRowBytes] = levelOffsetsAndRowBytes[currentMipLevel]; |
| 256 |
|
| 257 |
// copy data into the buffer, skipping any trailing bytes |
| 258 |
const char* src = (const char*)levels[currentMipLevel].fPixels; |
| 259 |
|
| 260 |
if (source.isRGB888Format()) { |
| 261 |
SkISize dims = {currentWidth, currentHeight}; |
| 262 |
SkImageInfo srcImageInfo = SkImageInfo::Make(dims, srcColorInfo); |
| 263 |
SkImageInfo dstImageInfo = SkImageInfo::Make(dims, dstColorInfo); |
| 264 |
|
| 265 |
const void* rgbConvertSrc = src; |
| 266 |
size_t rgbSrcRowBytes = srcRowBytes; |
| 267 |
SkAutoPixmapStorage temp; |
| 268 |
if (needsConversion) { |
| 269 |
temp.alloc(dstImageInfo); |
| 270 |
SkAssertResult(SkConvertPixels(dstImageInfo, |
| 271 |
temp.writable_addr(), |
| 272 |
temp.rowBytes(), |
| 273 |
srcImageInfo, |
| 274 |
src, |
| 275 |
srcRowBytes)); |
| 276 |
rgbConvertSrc = temp.addr(); |
| 277 |
rgbSrcRowBytes = temp.rowBytes(); |
| 278 |
} |
| 279 |
writer.writeRGBFromRGBx(mipOffset, |
| 280 |
rgbConvertSrc, |
| 281 |
rgbSrcRowBytes, |
| 282 |
dstRowBytes, |
| 283 |
currentWidth, |
| 284 |
currentHeight); |
| 285 |
} else if (needsConversion) { |
| 286 |
SkISize dims = {currentWidth, currentHeight}; |
| 287 |
SkImageInfo srcImageInfo = SkImageInfo::Make(dims, srcColorInfo); |
| 288 |
SkImageInfo dstImageInfo = SkImageInfo::Make(dims, dstColorInfo); |
| 289 |
|
| 290 |
writer.convertAndWrite( |
| 291 |
mipOffset, srcImageInfo, src, srcRowBytes, dstImageInfo, dstRowBytes); |
| 292 |
} else { |
| 293 |
writer.write(mipOffset, src, srcRowBytes, dstRowBytes, trimRowBytes, currentHeight); |
| 294 |
} |
| 295 |
|
| 296 |
// For mipped data, the dstRect is always the full texture so we don't need to worry about |
| 297 |
// modifying the TL coord as it will always be 0,0,for all levels. |
| 298 |
upload.fCopyData.push_back({ |
| 299 |
/*fBufferOffset=*/bufferInfo.fOffset + mipOffset, |
| 300 |
/*fBufferRowBytes=*/dstRowBytes, |
| 301 |
/*fRect=*/SkIRect::MakeXYWH(dstRect.left(), dstRect.top(), currentWidth, currentHeight), |
| 302 |
/*fMipLevel=*/currentMipLevel |
| 303 |
}); |
| 304 |
|
| 305 |
currentWidth = std::max(1, currentWidth / 2); |
| 306 |
currentHeight = std::max(1, currentHeight / 2); |
| 307 |
} |
| 308 |
|
| 309 |
ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%dx%d]", |
| 310 |
mipLevelCount > 1 ? "MipMap " : "", |
| 311 |
dstRect.width(), dstRect.height()); |
| 312 |
|
| 313 |
return upload; |
| 314 |
} |
| 315 |
|
| 316 |
UploadInstance UploadInstance::MakeCompressed(Recorder* recorder, |
| 317 |
sk_sp<TextureProxy> textureProxy, |
| 318 |
const UploadSource& source) { |
| 319 |
const Caps* caps = recorder->priv().caps(); |
| 320 |
const SkISize dimensions = textureProxy->dimensions(); |
| 321 |
SkSpan<const MipLevel> levels = source.levels(); |
| 322 |
uint32_t mipLevelCount = static_cast<uint32_t>(levels.size()); |
| 323 |
|
| 324 |
TArray<std::pair<size_t, size_t>> levelOffsetsAndRowBytes(mipLevelCount); |
| 325 |
auto [combinedBufferSize, minAlignment] = |
| 326 |
compute_combined_buffer_size(caps, |
| 327 |
mipLevelCount, |
| 328 |
source.bytesPerPixel(), |
| 329 |
dimensions, |
| 330 |
source.compression(), |
| 331 |
&levelOffsetsAndRowBytes); |
| 332 |
SkASSERT(combinedBufferSize); |
| 333 |
|
| 334 |
UploadBufferManager* bufferMgr = recorder->priv().uploadBufferManager(); |
| 335 |
auto [writer, bufferInfo] = bufferMgr->getTextureUploadWriter(combinedBufferSize, minAlignment); |
| 336 |
|
| 337 |
std::vector<BufferTextureCopyData> copyData(mipLevelCount); |
| 338 |
|
| 339 |
if (!bufferInfo.fBuffer) { |
| 340 |
SKGPU_LOG_W("Failed to get write-mapped buffer for texture upload of size %zu", |
| 341 |
combinedBufferSize); |
| 342 |
return Invalid(); |
| 343 |
} |
| 344 |
|
| 345 |
UploadInstance upload{bufferInfo.fBuffer, source.bytesPerPixel(), std::move(textureProxy)}; |
| 346 |
|
| 347 |
// Fill in copy data |
| 348 |
int32_t currentWidth = dimensions.width(); |
| 349 |
int32_t currentHeight = dimensions.height(); |
| 350 |
for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 351 |
SkISize blockDimensions = |
| 352 |
CompressedDimensionsInBlocks(source.compression(), {currentWidth, currentHeight}); |
| 353 |
int32_t blockHeight = blockDimensions.height(); |
| 354 |
|
| 355 |
const size_t trimRowBytes = CompressedRowBytes(source.compression(), currentWidth); |
| 356 |
const size_t srcRowBytes = trimRowBytes; |
| 357 |
const auto [dstMipOffset, dstRowBytes] = levelOffsetsAndRowBytes[currentMipLevel]; |
| 358 |
|
| 359 |
// copy data into the buffer, skipping any trailing bytes |
| 360 |
const void* src = levels[currentMipLevel].fPixels; |
| 361 |
|
| 362 |
writer.write(dstMipOffset, src, srcRowBytes, dstRowBytes, trimRowBytes, blockHeight); |
| 363 |
|
| 364 |
int32_t copyWidth = currentWidth; |
| 365 |
int32_t copyHeight = currentHeight; |
| 366 |
if (caps->fullCompressedUploadSizeMustAlignToBlockDims()) { |
| 367 |
SkISize oneBlockDims = CompressedDimensions(source.compression(), {1, 1}); |
| 368 |
copyWidth = SkAlignTo(copyWidth, oneBlockDims.fWidth); |
| 369 |
copyHeight = SkAlignTo(copyHeight, oneBlockDims.fHeight); |
| 370 |
} |
| 371 |
|
| 372 |
upload.fCopyData.push_back({ |
| 373 |
/*fBufferOffset=*/bufferInfo.fOffset + dstMipOffset, |
| 374 |
/*fBufferRowBytes=*/dstRowBytes, |
| 375 |
/*fRect=*/SkIRect::MakeXYWH(0, 0, copyWidth, copyHeight), |
| 376 |
/*fMipLevel=*/currentMipLevel |
| 377 |
}); |
| 378 |
|
| 379 |
currentWidth = std::max(1, currentWidth / 2); |
| 380 |
currentHeight = std::max(1, currentHeight / 2); |
| 381 |
} |
| 382 |
|
| 383 |
ATRACE_ANDROID_FRAMEWORK("Upload Compressed %sTexture [%dx%d]", |
| 384 |
mipLevelCount > 1 ? "MipMap " : "", |
| 385 |
dimensions.width(), |
| 386 |
dimensions.height()); |
| 387 |
|
| 388 |
return upload; |
| 389 |
} |
| 390 |
|
| 391 |
bool UploadInstance::prepareResources(ResourceProvider* resourceProvider) { |
| 392 |
// While most uploads are to already instantiated proxies (e.g. for client-created texture |
| 393 |
// images) it is possible that writePixels() was issued as the first operation on a scratch |
| 394 |
// Device, or that this is the first upload to the raster or text atlas proxies. |
| 395 |
// TODO: Determine how to instantatiate textues in this case; atlas proxies shouldn't really be |
| 396 |
// "scratch" because they aren't going to be reused for anything else in a Recording. At the |
| 397 |
// same time, it could still go through the ScratchResourceManager and just never return them, |
| 398 |
// which is no different from instantiating them directly with the ResourceProvider. |
| 399 |
if (!TextureProxy::InstantiateIfNotLazy(resourceProvider, fTextureProxy.get())) { |
| 400 |
SKGPU_LOG_E("Could not instantiate texture proxy for UploadTask!"); |
| 401 |
return false; |
| 402 |
} |
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
Task::Status UploadInstance::addCommand(Context* context, |
| 407 |
CommandBuffer* commandBuffer, |
| 408 |
Task::ReplayTargetData replayData) const { |
| 409 |
using Status = Task::Status; |
| 410 |
SkASSERT(fTextureProxy && fTextureProxy->isInstantiated()); |
| 411 |
|
| 412 |
if (fConditionalContext && !fConditionalContext->needsUpload(context)) { |
| 413 |
// Assume that if a conditional context says to dynamically not upload that another |
| 414 |
// time through the tasks should try to upload again. |
| 415 |
return Status::kSuccess; |
| 416 |
} |
| 417 |
|
| 418 |
if (fTextureProxy->texture() != replayData.fTarget) { |
| 419 |
// The CommandBuffer doesn't take ownership of the upload buffer here; it's owned by |
| 420 |
// UploadBufferManager, which will transfer ownership in transferToCommandBuffer. |
| 421 |
if (!commandBuffer->copyBufferToTexture(fBuffer, |
| 422 |
fTextureProxy->refTexture(), |
| 423 |
fCopyData.data(), |
| 424 |
fCopyData.size())) { |
| 425 |
return Status::kFail; |
| 426 |
} |
| 427 |
} else { |
| 428 |
// Here we assume that multiple copies in a single UploadInstance are always used for |
| 429 |
// mipmaps of a single image, and that we won't ever upload to a replay target's mipmaps |
| 430 |
// directly. |
| 431 |
SkASSERT(fCopyData.size() == 1); |
| 432 |
const BufferTextureCopyData& copyData = fCopyData[0]; |
| 433 |
SkIRect dstRect = copyData.fRect; |
| 434 |
dstRect.offset(replayData.fTranslation); |
| 435 |
SkIRect croppedDstRect = dstRect; |
| 436 |
|
| 437 |
if (!replayData.fClip.isEmpty()) { |
| 438 |
SkIRect dstClip = replayData.fClip; |
| 439 |
dstClip.offset(replayData.fTranslation); |
| 440 |
if (!croppedDstRect.intersect(dstClip)) { |
| 441 |
// The replay clip can change on each insert, so subsequent replays may actually |
| 442 |
// intersect the copy rect. |
| 443 |
return Status::kSuccess; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (!croppedDstRect.intersect(SkIRect::MakeSize(fTextureProxy->dimensions()))) { |
| 448 |
// The replay translation can change on each insert, so subsequent replays may |
| 449 |
// actually intersect the copy rect. |
| 450 |
return Status::kSuccess; |
| 451 |
} |
| 452 |
|
| 453 |
BufferTextureCopyData transformedCopyData = copyData; |
| 454 |
transformedCopyData.fBufferOffset += |
| 455 |
(croppedDstRect.y() - dstRect.y()) * copyData.fBufferRowBytes + |
| 456 |
(croppedDstRect.x() - dstRect.x()) * fBytesPerPixel; |
| 457 |
transformedCopyData.fRect = croppedDstRect; |
| 458 |
|
| 459 |
if (!commandBuffer->copyBufferToTexture(fBuffer, |
| 460 |
fTextureProxy->refTexture(), |
| 461 |
&transformedCopyData, 1)) { |
| 462 |
return Status::kFail; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// The conditional context will return false if the upload should not happen anymore. If there's |
| 467 |
// no context assume that the upload should always be executed on replay. |
| 468 |
if (!fConditionalContext || fConditionalContext->uploadSubmitted()) { |
| 469 |
return Status::kSuccess; |
| 470 |
} else { |
| 471 |
return Status::kDiscard; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
//--------------------------------------------------------------------------- |
| 476 |
|
| 477 |
bool UploadList::recordUpload(Recorder* recorder, |
| 478 |
sk_sp<TextureProxy> textureProxy, |
| 479 |
const SkColorInfo& srcColorInfo, |
| 480 |
const SkColorInfo& dstColorInfo, |
| 481 |
const UploadSource& source, |
| 482 |
const SkIRect& dstRect, |
| 483 |
std::unique_ptr<ConditionalUploadContext> condContext) { |
| 484 |
// If possible, upload the data directly on host. |
| 485 |
if (source.canUploadOnHost()) { |
| 486 |
return textureProxy->texture()->uploadDataOnHost(source, dstRect); |
| 487 |
} |
| 488 |
|
| 489 |
UploadInstance instance = UploadInstance::Make(recorder, |
| 490 |
std::move(textureProxy), |
| 491 |
srcColorInfo, |
| 492 |
dstColorInfo, |
| 493 |
source, |
| 494 |
dstRect, |
| 495 |
std::move(condContext)); |
| 496 |
if (!instance.isValid()) { |
| 497 |
return false; |
| 498 |
} |
| 499 |
|
| 500 |
fInstances.emplace_back(std::move(instance)); |
| 501 |
return true; |
| 502 |
} |
| 503 |
|
| 504 |
//--------------------------------------------------------------------------- |
| 505 |
|
| 506 |
sk_sp<UploadTask> UploadTask::Make(UploadList* uploadList) { |
| 507 |
SkASSERT(uploadList); |
| 508 |
if (!uploadList->size()) { |
| 509 |
return nullptr; |
| 510 |
} |
| 511 |
return sk_sp<UploadTask>(new UploadTask(std::move(uploadList->fInstances))); |
| 512 |
} |
| 513 |
|
| 514 |
sk_sp<UploadTask> UploadTask::Make(UploadInstance instance) { |
| 515 |
if (!instance.isValid()) { |
| 516 |
return nullptr; |
| 517 |
} |
| 518 |
return sk_sp<UploadTask>(new UploadTask(std::move(instance))); |
| 519 |
} |
| 520 |
|
| 521 |
UploadTask::UploadTask(skia_private::TArray<UploadInstance>&& instances) |
| 522 |
: fInstances(std::move(instances)) {} |
| 523 |
|
| 524 |
UploadTask::UploadTask(UploadInstance instance) { |
| 525 |
fInstances.emplace_back(std::move(instance)); |
| 526 |
} |
| 527 |
|
| 528 |
UploadTask::~UploadTask() {} |
| 529 |
|
| 530 |
Task::Status UploadTask::prepareResources(ResourceProvider* resourceProvider, |
| 531 |
ScratchResourceManager*, |
| 532 |
sk_sp<const RuntimeEffectDictionary>) { |
| 533 |
for (int i = 0; i < fInstances.size(); ++i) { |
| 534 |
// No upload should be invalidated before prepareResources() is called. |
| 535 |
SkASSERT(fInstances[i].isValid()); |
| 536 |
if (!fInstances[i].prepareResources(resourceProvider)) { |
| 537 |
return Status::kFail; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
return Status::kSuccess; |
| 542 |
} |
| 543 |
|
| 544 |
Task::Status UploadTask::addCommands(Context* context, |
| 545 |
CommandBuffer* commandBuffer, |
| 546 |
ReplayTargetData replayData) { |
| 547 |
int discardCount = 0; |
| 548 |
for (int i = 0; i < fInstances.size(); ++i) { |
| 549 |
if (!fInstances[i].isValid()) { |
| 550 |
discardCount++; |
| 551 |
continue; |
| 552 |
} |
| 553 |
Status status = fInstances[i].addCommand(context, commandBuffer, replayData); |
| 554 |
if (status == Status::kFail) { |
| 555 |
return Status::kFail; |
| 556 |
} else if (status == Status::kDiscard) { |
| 557 |
fInstances[i] = UploadInstance::Invalid(); |
| 558 |
discardCount++; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
if (discardCount == fInstances.size()) { |
| 563 |
return Status::kDiscard; |
| 564 |
} else { |
| 565 |
return Status::kSuccess; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
} // namespace skgpu::graphite |