| 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/TextureUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkSurface.h" |
| 15 |
#include "include/effects/SkRuntimeEffect.h" |
| 16 |
#include "src/core/SkBlurEngine.h" |
| 17 |
#include "src/core/SkCompressedDataUtils.h" |
| 18 |
#include "src/core/SkDevice.h" |
| 19 |
#include "src/core/SkImageFilterCache.h" |
| 20 |
#include "src/core/SkImageFilterTypes.h" |
| 21 |
#include "src/core/SkMipmap.h" |
| 22 |
#include "src/core/SkSamplingPriv.h" |
| 23 |
#include "src/core/SkTraceEvent.h" |
| 24 |
#include "src/image/SkImage_Base.h" |
| 25 |
|
| 26 |
#include "include/gpu/graphite/BackendTexture.h" |
| 27 |
#include "include/gpu/graphite/Context.h" |
| 28 |
#include "include/gpu/graphite/GraphiteTypes.h" |
| 29 |
#include "include/gpu/graphite/Image.h" |
| 30 |
#include "include/gpu/graphite/ImageProvider.h" |
| 31 |
#include "include/gpu/graphite/Recorder.h" |
| 32 |
#include "include/gpu/graphite/Recording.h" |
| 33 |
#include "include/gpu/graphite/Surface.h" |
| 34 |
#include "src/gpu/BlurUtils.h" |
| 35 |
#include "src/gpu/RefCntedCallback.h" |
| 36 |
#include "src/gpu/SkBackingFit.h" |
| 37 |
#include "src/gpu/graphite/Buffer.h" |
| 38 |
#include "src/gpu/graphite/Caps.h" |
| 39 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 40 |
#include "src/gpu/graphite/Device.h" |
| 41 |
#include "src/gpu/graphite/DrawContext.h" |
| 42 |
#include "src/gpu/graphite/Image_Graphite.h" |
| 43 |
#include "src/gpu/graphite/Log.h" |
| 44 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 45 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 46 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 47 |
#include "src/gpu/graphite/RuntimeEffectDictionary.h" |
| 48 |
#include "src/gpu/graphite/SpecialImage_Graphite.h" |
| 49 |
#include "src/gpu/graphite/Surface_Graphite.h" |
| 50 |
#include "src/gpu/graphite/Texture.h" |
| 51 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 52 |
#include "src/gpu/graphite/task/CopyTask.h" |
| 53 |
#include "src/gpu/graphite/task/SynchronizeToCpuTask.h" |
| 54 |
#include "src/gpu/graphite/task/UploadTask.h" |
| 55 |
|
| 56 |
#include <array> |
| 57 |
|
| 58 |
|
| 59 |
using SkImages::GraphitePromiseTextureFulfillProc; |
| 60 |
using SkImages::GraphitePromiseTextureFulfillContext; |
| 61 |
using SkImages::GraphitePromiseTextureReleaseProc; |
| 62 |
|
| 63 |
namespace skgpu::graphite { |
| 64 |
|
| 65 |
namespace { |
| 66 |
|
| 67 |
sk_sp<Surface> make_renderable_scratch_surface( |
| 68 |
Recorder* recorder, |
| 69 |
const SkImageInfo& info, |
| 70 |
SkBackingFit backingFit, |
| 71 |
std::string_view label, |
| 72 |
const SkSurfaceProps* surfaceProps = nullptr) { |
| 73 |
SkColorType ct = recorder->priv().caps()->getRenderableColorType(info.colorType()); |
| 74 |
if (ct == kUnknown_SkColorType) { |
| 75 |
return nullptr; |
| 76 |
} |
| 77 |
|
| 78 |
// TODO(b/323886870): Historically the scratch surfaces used here were exact-fit but they should |
| 79 |
// be able to be approx-fit and uninstantiated. |
| 80 |
return Surface::MakeScratch( |
| 81 |
recorder, info.makeColorType(ct), label, Budgeted::kYes, Mipmapped::kNo, backingFit); |
| 82 |
} |
| 83 |
|
| 84 |
bool valid_client_provided_image(const SkImage* clientProvided, |
| 85 |
const SkImage* original, |
| 86 |
SkImage::RequiredProperties requiredProps) { |
| 87 |
if (!clientProvided || |
| 88 |
!as_IB(clientProvided)->isGraphiteBacked() || |
| 89 |
original->dimensions() != clientProvided->dimensions() || |
| 90 |
original->alphaType() != clientProvided->alphaType()) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
uint32_t origChannels = SkColorTypeChannelFlags(original->colorType()); |
| 95 |
uint32_t clientChannels = SkColorTypeChannelFlags(clientProvided->colorType()); |
| 96 |
if ((origChannels & clientChannels) != origChannels) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
// We require provided images to have a TopLeft origin |
| 101 |
auto graphiteImage = static_cast<const Image*>(clientProvided); |
| 102 |
if (graphiteImage->textureProxyView().origin() != Origin::kTopLeft) { |
| 103 |
SKGPU_LOG_E("Client provided image must have a TopLeft origin."); |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
// This class is the lazy instantiation callback for promise images. It manages calling the |
| 111 |
// client's Fulfill, ImageRelease, and TextureRelease procs. |
| 112 |
class PromiseLazyInstantiateCallback { |
| 113 |
public: |
| 114 |
PromiseLazyInstantiateCallback(sk_sp<RefCntedCallback> releaseHelper, |
| 115 |
GraphitePromiseTextureFulfillProc fulfillProc, |
| 116 |
GraphitePromiseTextureFulfillContext fulfillContext, |
| 117 |
GraphitePromiseTextureReleaseProc textureReleaseProc, |
| 118 |
std::string_view label) |
| 119 |
: fReleaseHelper(std::move(releaseHelper)) |
| 120 |
, fFulfillProc(fulfillProc) |
| 121 |
, fFulfillContext(fulfillContext) |
| 122 |
, fTextureReleaseProc(textureReleaseProc) |
| 123 |
, fLabel(label) { |
| 124 |
} |
| 125 |
PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default; |
| 126 |
PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) { |
| 127 |
// Because we get wrapped in std::function we must be copyable. But we should never |
| 128 |
// be copied. |
| 129 |
SkASSERT(false); |
| 130 |
} |
| 131 |
PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default; |
| 132 |
PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) { |
| 133 |
SkASSERT(false); |
| 134 |
return *this; |
| 135 |
} |
| 136 |
|
| 137 |
sk_sp<Texture> operator()(ResourceProvider* resourceProvider) { |
| 138 |
// Invoke the fulfill proc to get the promised backend texture. |
| 139 |
auto [ backendTexture, textureReleaseCtx ] = fFulfillProc(fFulfillContext); |
| 140 |
if (!backendTexture.isValid()) { |
| 141 |
SKGPU_LOG_W("FulfillProc returned an invalid backend texture"); |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
sk_sp<RefCntedCallback> textureReleaseCB = RefCntedCallback::Make(fTextureReleaseProc, |
| 146 |
textureReleaseCtx); |
| 147 |
|
| 148 |
sk_sp<Texture> texture = resourceProvider->createWrappedTexture(backendTexture, |
| 149 |
std::move(fLabel)); |
| 150 |
if (!texture) { |
| 151 |
SKGPU_LOG_W("Failed to wrap BackendTexture returned by fulfill proc"); |
| 152 |
return nullptr; |
| 153 |
} |
| 154 |
texture->setReleaseCallback(std::move(textureReleaseCB)); |
| 155 |
return texture; |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
sk_sp<RefCntedCallback> fReleaseHelper; |
| 160 |
GraphitePromiseTextureFulfillProc fFulfillProc; |
| 161 |
GraphitePromiseTextureFulfillContext fFulfillContext; |
| 162 |
GraphitePromiseTextureReleaseProc fTextureReleaseProc; |
| 163 |
std::string fLabel; |
| 164 |
}; |
| 165 |
|
| 166 |
} // anonymous namespace |
| 167 |
|
| 168 |
std::tuple<TextureProxyView, SkColorType> MakeBitmapProxyView(Recorder* recorder, |
| 169 |
const SkBitmap& bitmap, |
| 170 |
sk_sp<SkMipmap> mipmapsIn, |
| 171 |
Mipmapped mipmapped, |
| 172 |
Budgeted budgeted, |
| 173 |
std::string_view label) { |
| 174 |
// Adjust params based on input and Caps |
| 175 |
const Caps* caps = recorder->priv().caps(); |
| 176 |
SkColorType ct = bitmap.info().colorType(); |
| 177 |
|
| 178 |
if (bitmap.dimensions().area() <= 1) { |
| 179 |
mipmapped = Mipmapped::kNo; |
| 180 |
} |
| 181 |
|
| 182 |
Protected isProtected = recorder->priv().isProtected(); |
| 183 |
auto textureInfo = caps->getDefaultSampledTextureInfo(ct, mipmapped, isProtected, |
| 184 |
Renderable::kNo); |
| 185 |
if (!textureInfo.isValid()) { |
| 186 |
ct = kRGBA_8888_SkColorType; |
| 187 |
textureInfo = caps->getDefaultSampledTextureInfo(ct, mipmapped, isProtected, |
| 188 |
Renderable::kNo); |
| 189 |
} |
| 190 |
SkASSERT(textureInfo.isValid()); |
| 191 |
|
| 192 |
// Convert bitmap to texture colortype if necessary |
| 193 |
SkBitmap bmpToUpload; |
| 194 |
if (ct != bitmap.info().colorType()) { |
| 195 |
if (!bmpToUpload.tryAllocPixels(bitmap.info().makeColorType(ct)) || |
| 196 |
!bitmap.readPixels(bmpToUpload.pixmap())) { |
| 197 |
return {}; |
| 198 |
} |
| 199 |
bmpToUpload.setImmutable(); |
| 200 |
} else { |
| 201 |
bmpToUpload = bitmap; |
| 202 |
} |
| 203 |
|
| 204 |
if (!SkImageInfoIsValid(bmpToUpload.info())) { |
| 205 |
return {}; |
| 206 |
} |
| 207 |
|
| 208 |
int mipLevelCount = (mipmapped == Mipmapped::kYes) ? |
| 209 |
SkMipmap::ComputeLevelCount(bitmap.width(), bitmap.height()) + 1 : 1; |
| 210 |
|
| 211 |
|
| 212 |
// setup MipLevels |
| 213 |
sk_sp<SkMipmap> mipmaps; |
| 214 |
std::vector<MipLevel> texels; |
| 215 |
if (mipLevelCount == 1) { |
| 216 |
texels.resize(mipLevelCount); |
| 217 |
texels[0].fPixels = bmpToUpload.getPixels(); |
| 218 |
texels[0].fRowBytes = bmpToUpload.rowBytes(); |
| 219 |
} else { |
| 220 |
mipmaps = SkToBool(mipmapsIn) |
| 221 |
? mipmapsIn |
| 222 |
: sk_sp<SkMipmap>(SkMipmap::Build(bmpToUpload.pixmap(), nullptr)); |
| 223 |
if (!mipmaps) { |
| 224 |
return {}; |
| 225 |
} |
| 226 |
|
| 227 |
SkASSERT(mipLevelCount == mipmaps->countLevels() + 1); |
| 228 |
texels.resize(mipLevelCount); |
| 229 |
|
| 230 |
texels[0].fPixels = bmpToUpload.getPixels(); |
| 231 |
texels[0].fRowBytes = bmpToUpload.rowBytes(); |
| 232 |
|
| 233 |
for (int i = 1; i < mipLevelCount; ++i) { |
| 234 |
SkMipmap::Level generatedMipLevel; |
| 235 |
mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 236 |
texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 237 |
texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 238 |
SkASSERT(texels[i].fPixels); |
| 239 |
SkASSERT(generatedMipLevel.fPixmap.colorType() == bmpToUpload.colorType()); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// Create proxy |
| 244 |
sk_sp<TextureProxy> proxy = TextureProxy::Make(caps, |
| 245 |
recorder->priv().resourceProvider(), |
| 246 |
bmpToUpload.dimensions(), |
| 247 |
textureInfo, |
| 248 |
label, |
| 249 |
budgeted); |
| 250 |
if (!proxy) { |
| 251 |
return {}; |
| 252 |
} |
| 253 |
SkASSERT(caps->areColorTypeAndTextureInfoCompatible(ct, proxy->textureInfo())); |
| 254 |
SkASSERT(mipmapped == Mipmapped::kNo || proxy->mipmapped() == Mipmapped::kYes); |
| 255 |
|
| 256 |
// Src and dst colorInfo are the same |
| 257 |
const SkColorInfo& colorInfo = bmpToUpload.info().colorInfo(); |
| 258 |
// Add upload to the root upload list. These bitmaps are uploaded to unique textures so there is |
| 259 |
// no need to coordinate resource sharing. It is better to then group them into a single task |
| 260 |
// at the start of the Recording. |
| 261 |
const SkIRect dimensions = SkIRect::MakeSize(bmpToUpload.dimensions()); |
| 262 |
UploadSource uploadSource = UploadSource::Make( |
| 263 |
recorder->priv().caps(), *proxy, colorInfo, colorInfo, texels, dimensions); |
| 264 |
if (!uploadSource.isValid()) { |
| 265 |
SKGPU_LOG_E("MakeBitmapProxyView: Could not create UploadSource"); |
| 266 |
return {}; |
| 267 |
} |
| 268 |
if (!recorder->priv().rootUploadList()->recordUpload(recorder, |
| 269 |
proxy, |
| 270 |
colorInfo, |
| 271 |
colorInfo, |
| 272 |
uploadSource, |
| 273 |
dimensions, |
| 274 |
std::make_unique<ImageUploadContext>())) { |
| 275 |
SKGPU_LOG_E("MakeBitmapProxyView: Could not create UploadInstance"); |
| 276 |
return {}; |
| 277 |
} |
| 278 |
|
| 279 |
Swizzle swizzle = caps->getReadSwizzle(ct, textureInfo); |
| 280 |
// If the color type is alpha-only, propagate the alpha value to the other channels. |
| 281 |
if (SkColorTypeIsAlphaOnly(colorInfo.colorType())) { |
| 282 |
swizzle = Swizzle::Concat(swizzle, Swizzle("aaaa")); |
| 283 |
} |
| 284 |
return {{std::move(proxy), swizzle}, ct}; |
| 285 |
} |
| 286 |
|
| 287 |
sk_sp<TextureProxy> MakePromiseImageLazyProxy( |
| 288 |
const Caps* caps, |
| 289 |
SkISize dimensions, |
| 290 |
TextureInfo textureInfo, |
| 291 |
Volatile isVolatile, |
| 292 |
sk_sp<RefCntedCallback> releaseHelper, |
| 293 |
GraphitePromiseTextureFulfillProc fulfillProc, |
| 294 |
GraphitePromiseTextureFulfillContext fulfillContext, |
| 295 |
GraphitePromiseTextureReleaseProc textureReleaseProc, |
| 296 |
std::string_view label) { |
| 297 |
SkASSERT(!dimensions.isEmpty()); |
| 298 |
SkASSERT(releaseHelper); |
| 299 |
|
| 300 |
if (!fulfillProc) { |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
|
| 304 |
PromiseLazyInstantiateCallback callback{std::move(releaseHelper), fulfillProc, |
| 305 |
fulfillContext, textureReleaseProc, label}; |
| 306 |
// Proxies for promise images are assumed to always be destined for a client's SkImage so |
| 307 |
// are never considered budgeted. |
| 308 |
return TextureProxy::MakeLazy(caps, dimensions, textureInfo, Budgeted::kNo, isVolatile, |
| 309 |
std::move(callback)); |
| 310 |
} |
| 311 |
|
| 312 |
sk_sp<SkImage> MakeFromBitmap(Recorder* recorder, |
| 313 |
const SkColorInfo& colorInfo, |
| 314 |
const SkBitmap& bitmap, |
| 315 |
sk_sp<SkMipmap> mipmaps, |
| 316 |
Budgeted budgeted, |
| 317 |
SkImage::RequiredProperties requiredProps, |
| 318 |
std::string_view label) { |
| 319 |
auto mm = requiredProps.fMipmapped ? Mipmapped::kYes : Mipmapped::kNo; |
| 320 |
auto [view, ct] = |
| 321 |
MakeBitmapProxyView(recorder, bitmap, std::move(mipmaps), mm, budgeted, label); |
| 322 |
if (!view) { |
| 323 |
return nullptr; |
| 324 |
} |
| 325 |
|
| 326 |
SkASSERT(!requiredProps.fMipmapped || view.proxy()->mipmapped() == Mipmapped::kYes); |
| 327 |
return sk_make_sp<skgpu::graphite::Image>(std::move(view), colorInfo.makeColorType(ct)); |
| 328 |
} |
| 329 |
|
| 330 |
size_t ComputeSize(SkISize dimensions, const TextureInfo& info) { |
| 331 |
TextureFormat format = TextureInfoPriv::ViewFormat(info); |
| 332 |
SkTextureCompressionType compression = TextureFormatCompressionType(format); |
| 333 |
|
| 334 |
size_t colorSize = 0; |
| 335 |
|
| 336 |
if (compression != SkTextureCompressionType::kNone) { |
| 337 |
colorSize = SkCompressedFormatDataSize(compression, |
| 338 |
dimensions, |
| 339 |
info.mipmapped() == Mipmapped::kYes); |
| 340 |
} else { |
| 341 |
// TODO(b/401016699): Add logic to handle multiplanar formats |
| 342 |
size_t bytesPerPixel = TextureFormatBytesPerBlock(format); |
| 343 |
|
| 344 |
colorSize = (size_t)dimensions.width() * dimensions.height() * bytesPerPixel; |
| 345 |
} |
| 346 |
|
| 347 |
size_t finalSize = colorSize * (uint8_t) info.sampleCount(); |
| 348 |
|
| 349 |
if (info.mipmapped() == Mipmapped::kYes) { |
| 350 |
finalSize += colorSize/3; |
| 351 |
} |
| 352 |
return finalSize; |
| 353 |
} |
| 354 |
|
| 355 |
sk_sp<Image> CopyAsDraw(Recorder* recorder, |
| 356 |
DrawContext* drawContext, |
| 357 |
const SkImage* image, |
| 358 |
const SkIRect& subset, |
| 359 |
const SkColorInfo& dstColorInfo, |
| 360 |
Budgeted budgeted, |
| 361 |
Mipmapped mipmapped, |
| 362 |
SkBackingFit backingFit, |
| 363 |
std::string_view label) { |
| 364 |
SkColorType ct = recorder->priv().caps()->getRenderableColorType(dstColorInfo.colorType()); |
| 365 |
if (ct == kUnknown_SkColorType) { |
| 366 |
return nullptr; |
| 367 |
} |
| 368 |
SkImageInfo dstInfo = SkImageInfo::Make(subset.size(), |
| 369 |
dstColorInfo.makeColorType(ct) |
| 370 |
.makeAlphaType(kPremul_SkAlphaType)); |
| 371 |
// The surface goes out of scope when we return, so it can be scratch, but it may or may |
| 372 |
// not be budgeted depending on how the copied image is used (or returned to the client). |
| 373 |
sk_sp<Surface> surface = |
| 374 |
Surface::MakeScratch(recorder, dstInfo, label, budgeted, mipmapped, backingFit); |
| 375 |
if (!surface) { |
| 376 |
return nullptr; |
| 377 |
} |
| 378 |
|
| 379 |
SkPaint paint; |
| 380 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 381 |
surface->getCanvas()->drawImage(image, -subset.left(), -subset.top(), |
| 382 |
SkFilterMode::kNearest, &paint); |
| 383 |
surface->flushToDrawContext(drawContext); |
| 384 |
return surface->asImage(); |
| 385 |
} |
| 386 |
|
| 387 |
sk_sp<SkImage> RescaleImage(Recorder* recorder, |
| 388 |
const SkImage* srcImage, |
| 389 |
SkIRect srcIRect, |
| 390 |
const SkImageInfo& dstInfo, |
| 391 |
SkImage::RescaleGamma rescaleGamma, |
| 392 |
SkImage::RescaleMode rescaleMode) { |
| 393 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 394 |
TRACE_EVENT_INSTANT2("skia.gpu", "RescaleImage Src", TRACE_EVENT_SCOPE_THREAD, |
| 395 |
"width", srcIRect.width(), "height", srcIRect.height()); |
| 396 |
TRACE_EVENT_INSTANT2("skia.gpu", "RescaleImage Dst", TRACE_EVENT_SCOPE_THREAD, |
| 397 |
"width", dstInfo.width(), "height", dstInfo.height()); |
| 398 |
|
| 399 |
// RescaleImage() should only be called when we already know that srcImage is graphite-backed |
| 400 |
SkASSERT(srcImage && as_IB(srcImage)->isGraphiteBacked()); |
| 401 |
|
| 402 |
// For now this needs to be texturable because we can't depend on copies to scale. |
| 403 |
// NOTE: srcView may be empty if srcImage is YUVA. |
| 404 |
const TextureProxyView srcView = AsView(srcImage); |
| 405 |
if (srcView && !recorder->priv().caps()->isTexturable(srcView.proxy()->textureInfo())) { |
| 406 |
// With the current definition of SkImage, this shouldn't happen. If we allow non-texturable |
| 407 |
// formats for compute, we'll need to copy to a texturable format. |
| 408 |
SkASSERT(false); |
| 409 |
return nullptr; |
| 410 |
} |
| 411 |
|
| 412 |
// make a Surface *exactly* matching dstInfo to rescale into |
| 413 |
SkSurfaceProps surfaceProps = {}; |
| 414 |
sk_sp<SkSurface> dst = make_renderable_scratch_surface(recorder, |
| 415 |
dstInfo, |
| 416 |
SkBackingFit::kExact, |
| 417 |
"RescaleDstTexture", |
| 418 |
&surfaceProps); |
| 419 |
if (!dst) { |
| 420 |
return nullptr; |
| 421 |
} |
| 422 |
|
| 423 |
SkRect srcRect = SkRect::Make(srcIRect); |
| 424 |
SkRect dstRect = SkRect::Make(dstInfo.dimensions()); |
| 425 |
|
| 426 |
SkISize finalSize = SkISize::Make(dstRect.width(), dstRect.height()); |
| 427 |
if (finalSize == srcIRect.size()) { |
| 428 |
rescaleGamma = Image::RescaleGamma::kSrc; |
| 429 |
rescaleMode = Image::RescaleMode::kNearest; |
| 430 |
} |
| 431 |
|
| 432 |
// Within a rescaling pass tempInput is read from and tempOutput is written to. |
| 433 |
// At the end of the pass tempOutput's texture is wrapped and assigned to tempInput. |
| 434 |
sk_sp<SkImage> tempInput = sk_ref_sp(srcImage); |
| 435 |
sk_sp<SkSurface> tempOutput; |
| 436 |
|
| 437 |
// Assume we should ignore the rescale linear request if the surface has no color space since |
| 438 |
// it's unclear how we'd linearize from an unknown color space. |
| 439 |
const SkImageInfo& srcImageInfo = srcImage->imageInfo(); |
| 440 |
if (rescaleGamma == Image::RescaleGamma::kLinear && |
| 441 |
srcImageInfo.colorSpace() && |
| 442 |
!srcImageInfo.colorSpace()->gammaIsLinear()) { |
| 443 |
// Draw the src image into a new surface with linear gamma, and make that the new tempInput |
| 444 |
sk_sp<SkColorSpace> linearGamma = srcImageInfo.colorSpace()->makeLinearGamma(); |
| 445 |
SkImageInfo gammaDstInfo = SkImageInfo::Make(srcIRect.size(), |
| 446 |
tempInput->imageInfo().colorType(), |
| 447 |
kPremul_SkAlphaType, |
| 448 |
std::move(linearGamma)); |
| 449 |
tempOutput = make_renderable_scratch_surface(recorder, gammaDstInfo, SkBackingFit::kApprox, |
| 450 |
"RescaleLinearGammaTexture", &surfaceProps); |
| 451 |
if (!tempOutput) { |
| 452 |
return nullptr; |
| 453 |
} |
| 454 |
SkCanvas* gammaDst = tempOutput->getCanvas(); |
| 455 |
SkRect gammaDstRect = SkRect::Make(srcIRect.size()); |
| 456 |
|
| 457 |
SkPaint paint; |
| 458 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 459 |
gammaDst->drawImageRect(tempInput, srcRect, gammaDstRect, |
| 460 |
SkSamplingOptions(SkFilterMode::kNearest), &paint, |
| 461 |
SkCanvas::kStrict_SrcRectConstraint); |
| 462 |
tempInput = SkSurfaces::AsImage(std::move(tempOutput)); |
| 463 |
srcRect = gammaDstRect; |
| 464 |
} |
| 465 |
|
| 466 |
SkImageInfo outImageInfo = tempInput->imageInfo().makeAlphaType(kPremul_SkAlphaType); |
| 467 |
do { |
| 468 |
SkISize nextDims = finalSize; |
| 469 |
if (rescaleMode != Image::RescaleMode::kNearest && |
| 470 |
rescaleMode != Image::RescaleMode::kLinear) { |
| 471 |
if (srcRect.width() > finalSize.width()) { |
| 472 |
nextDims.fWidth = std::max((srcRect.width() + 1)/2, (float)finalSize.width()); |
| 473 |
} else if (srcRect.width() < finalSize.width()) { |
| 474 |
nextDims.fWidth = std::min(srcRect.width()*2, (float)finalSize.width()); |
| 475 |
} |
| 476 |
if (srcRect.height() > finalSize.height()) { |
| 477 |
nextDims.fHeight = std::max((srcRect.height() + 1)/2, (float)finalSize.height()); |
| 478 |
} else if (srcRect.height() < finalSize.height()) { |
| 479 |
nextDims.fHeight = std::min(srcRect.height()*2, (float)finalSize.height()); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
SkRect stepDstRect; |
| 484 |
if (nextDims == finalSize) { |
| 485 |
tempOutput = dst; |
| 486 |
stepDstRect = dstRect; |
| 487 |
} else { |
| 488 |
SkImageInfo nextInfo = outImageInfo.makeDimensions(nextDims); |
| 489 |
tempOutput = make_renderable_scratch_surface(recorder, nextInfo, SkBackingFit::kApprox, |
| 490 |
"RescaleImageTempTexture", &surfaceProps); |
| 491 |
if (!tempOutput) { |
| 492 |
return nullptr; |
| 493 |
} |
| 494 |
stepDstRect = SkRect::Make(tempOutput->imageInfo().dimensions()); |
| 495 |
} |
| 496 |
|
| 497 |
SkSamplingOptions samplingOptions; |
| 498 |
if (rescaleMode == Image::RescaleMode::kRepeatedCubic) { |
| 499 |
samplingOptions = SkSamplingOptions(SkCubicResampler::CatmullRom()); |
| 500 |
} else { |
| 501 |
samplingOptions = (rescaleMode == Image::RescaleMode::kNearest) ? |
| 502 |
SkSamplingOptions(SkFilterMode::kNearest) : |
| 503 |
SkSamplingOptions(SkFilterMode::kLinear); |
| 504 |
} |
| 505 |
SkPaint paint; |
| 506 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 507 |
tempOutput->getCanvas()->drawImageRect(tempInput, srcRect, stepDstRect, samplingOptions, |
| 508 |
&paint, SkCanvas::kStrict_SrcRectConstraint); |
| 509 |
|
| 510 |
tempInput = SkSurfaces::AsImage(std::move(tempOutput)); |
| 511 |
srcRect = SkRect::Make(nextDims); |
| 512 |
} while (srcRect.width() != finalSize.width() || srcRect.height() != finalSize.height()); |
| 513 |
|
| 514 |
return SkSurfaces::AsImage(std::move(dst)); |
| 515 |
} |
| 516 |
|
| 517 |
bool GenerateMipmaps(Recorder* recorder, |
| 518 |
DrawContext* drawContext, |
| 519 |
sk_sp<TextureProxy> texture, |
| 520 |
const SkColorInfo& colorInfo) { |
| 521 |
constexpr SkSamplingOptions kSamplingOptions = SkSamplingOptions(SkFilterMode::kLinear); |
| 522 |
|
| 523 |
SkASSERT(texture->mipmapped() == Mipmapped::kYes); |
| 524 |
|
| 525 |
// Within a rescaling pass scratchImg is read from and a scratch surface is written to. |
| 526 |
// At the end of the pass the scratch surface's texture is wrapped and assigned to scratchImg. |
| 527 |
|
| 528 |
// The scratch surface we create below will use a write swizzle derived from SkColorType and |
| 529 |
// pixel format. We have to be consistent and swizzle on the read. |
| 530 |
auto imgSwizzle = recorder->priv().caps()->getReadSwizzle(colorInfo.colorType(), |
| 531 |
texture->textureInfo()); |
| 532 |
sk_sp<SkImage> scratchImg(new Image(TextureProxyView(texture, imgSwizzle), colorInfo)); |
| 533 |
|
| 534 |
SkISize srcSize = texture->dimensions(); |
| 535 |
const SkColorInfo outColorInfo = colorInfo.makeAlphaType(kPremul_SkAlphaType); |
| 536 |
|
| 537 |
// Alternate between two scratch surfaces to avoid reading from and writing to a texture in the |
| 538 |
// same pass. The dimensions of the first usages of the two scratch textures will be 1/2 and 1/4 |
| 539 |
// those of the original texture, respectively. |
| 540 |
sk_sp<Surface> scratchSurfaces[2]; |
| 541 |
for (int i = 0; i < 2; ++i) { |
| 542 |
scratchSurfaces[i] = make_renderable_scratch_surface( |
| 543 |
recorder, |
| 544 |
SkImageInfo::Make(SkISize::Make(std::max(1, srcSize.width() >> (i + 1)), |
| 545 |
std::max(1, srcSize.height() >> (i + 1))), |
| 546 |
outColorInfo), |
| 547 |
SkBackingFit::kApprox, |
| 548 |
"GenerateMipmapsScratchTexture"); |
| 549 |
if (!scratchSurfaces[i]) { |
| 550 |
return false; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
for (int mipLevel = 1; srcSize.width() > 1 || srcSize.height() > 1; ++mipLevel) { |
| 555 |
const SkISize dstSize = SkISize::Make(std::max(srcSize.width() >> 1, 1), |
| 556 |
std::max(srcSize.height() >> 1, 1)); |
| 557 |
|
| 558 |
Surface* scratchSurface = scratchSurfaces[(mipLevel - 1) & 1].get(); |
| 559 |
|
| 560 |
SkPaint paint; |
| 561 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 562 |
scratchSurface->getCanvas()->drawImageRect(scratchImg, |
| 563 |
SkRect::Make(srcSize), |
| 564 |
SkRect::Make(dstSize), |
| 565 |
kSamplingOptions, |
| 566 |
&paint, |
| 567 |
SkCanvas::kStrict_SrcRectConstraint); |
| 568 |
|
| 569 |
// Make sure the rescaling draw finishes before copying the results. |
| 570 |
scratchSurface->flushToDrawContext(drawContext); |
| 571 |
|
| 572 |
sk_sp<CopyTextureToTextureTask> copyTask = CopyTextureToTextureTask::Make( |
| 573 |
static_cast<const Surface*>(scratchSurface)->readSurfaceView().refProxy(), |
| 574 |
SkIRect::MakeSize(dstSize), |
| 575 |
texture, |
| 576 |
{0, 0}, |
| 577 |
mipLevel); |
| 578 |
if (!copyTask) { |
| 579 |
return false; |
| 580 |
} |
| 581 |
|
| 582 |
if (drawContext) { |
| 583 |
drawContext->recordDependency(std::move(copyTask)); |
| 584 |
} else { |
| 585 |
recorder->priv().add(std::move(copyTask)); |
| 586 |
} |
| 587 |
|
| 588 |
scratchImg = scratchSurface->asImage(); |
| 589 |
srcSize = dstSize; |
| 590 |
} |
| 591 |
|
| 592 |
return true; |
| 593 |
} |
| 594 |
|
| 595 |
std::pair<sk_sp<SkImage>, SkSamplingOptions> GetGraphiteBacked(Recorder* recorder, |
| 596 |
const SkImage* imageIn, |
| 597 |
SkSamplingOptions sampling) { |
| 598 |
Mipmapped mipmapped = (sampling.mipmap != SkMipmapMode::kNone) |
| 599 |
? Mipmapped::kYes : Mipmapped::kNo; |
| 600 |
|
| 601 |
if (imageIn->dimensions().area() <= 1 && mipmapped == Mipmapped::kYes) { |
| 602 |
mipmapped = Mipmapped::kNo; |
| 603 |
sampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone); |
| 604 |
} |
| 605 |
|
| 606 |
sk_sp<SkImage> result; |
| 607 |
if (as_IB(imageIn)->isGraphiteBacked()) { |
| 608 |
result = sk_ref_sp(imageIn); |
| 609 |
|
| 610 |
// If the preexisting Graphite-backed image doesn't have the required mipmaps we will drop |
| 611 |
// down the sampling |
| 612 |
if (mipmapped == Mipmapped::kYes && !result->hasMipmaps()) { |
| 613 |
mipmapped = Mipmapped::kNo; |
| 614 |
sampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone); |
| 615 |
} |
| 616 |
} else { |
| 617 |
auto clientImageProvider = recorder->clientImageProvider(); |
| 618 |
result = clientImageProvider->findOrCreate( |
| 619 |
recorder, imageIn, {mipmapped == Mipmapped::kYes}); |
| 620 |
|
| 621 |
if (!valid_client_provided_image( |
| 622 |
result.get(), imageIn, {mipmapped == Mipmapped::kYes})) { |
| 623 |
// The client did not fulfill the ImageProvider contract so drop the image. |
| 624 |
result = nullptr; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
if (sampling.isAniso() && result) { |
| 629 |
sampling = SkSamplingPriv::AnisoFallback(result->hasMipmaps()); |
| 630 |
} |
| 631 |
|
| 632 |
return { result, sampling }; |
| 633 |
} |
| 634 |
|
| 635 |
TextureProxyView AsView(const SkImage* image) { |
| 636 |
if (!image) { |
| 637 |
return {}; |
| 638 |
} |
| 639 |
if (!as_IB(image)->isGraphiteBacked()) { |
| 640 |
return {}; |
| 641 |
} |
| 642 |
// A YUVA image (even if backed by graphite textures) is not a single texture |
| 643 |
if (as_IB(image)->isYUVA()) { |
| 644 |
return {}; |
| 645 |
} |
| 646 |
|
| 647 |
auto gi = reinterpret_cast<const Image*>(image); |
| 648 |
return gi->textureProxyView(); |
| 649 |
} |
| 650 |
|
| 651 |
SkColorType ComputeShaderCoverageMaskTargetFormat(const Caps* caps) { |
| 652 |
// GPU compute coverage mask renderers need to bind the mask texture as a storage binding, which |
| 653 |
// support a limited set of color formats. In general, we use RGBA8 if Alpha8 can't be |
| 654 |
// supported. |
| 655 |
if (caps->isStorage(caps->getDefaultStorageTextureInfo(kAlpha_8_SkColorType))) { |
| 656 |
return kAlpha_8_SkColorType; |
| 657 |
} |
| 658 |
return kRGBA_8888_SkColorType; |
| 659 |
} |
| 660 |
|
| 661 |
} // namespace skgpu::graphite |
| 662 |
|
| 663 |
namespace skif { |
| 664 |
|
| 665 |
namespace { |
| 666 |
|
| 667 |
// TODO(michaelludwig): The skgpu::BlurUtils effects will be migrated to src/core to implement a |
| 668 |
// shader BlurEngine that can be shared by rastr, Ganesh, and Graphite. This is blocked by having |
| 669 |
// skif::FilterResult handle the resizing to the max supported sigma. |
| 670 |
class GraphiteBackend : |
| 671 |
public Backend, |
| 672 |
private SkShaderBlurAlgorithm, |
| 673 |
private SkBlurEngine { |
| 674 |
public: |
| 675 |
|
| 676 |
GraphiteBackend(skgpu::graphite::Recorder* recorder, |
| 677 |
const SkSurfaceProps& surfaceProps, |
| 678 |
SkColorType colorType) |
| 679 |
: Backend(SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize), |
| 680 |
surfaceProps, colorType) |
| 681 |
, fRecorder(recorder) {} |
| 682 |
|
| 683 |
// Backend |
| 684 |
sk_sp<SkDevice> makeDevice(SkISize size, |
| 685 |
sk_sp<SkColorSpace> colorSpace, |
| 686 |
const SkSurfaceProps* props) const override { |
| 687 |
SkImageInfo imageInfo = SkImageInfo::Make(size, |
| 688 |
this->colorType(), |
| 689 |
kPremul_SkAlphaType, |
| 690 |
std::move(colorSpace)); |
| 691 |
return skgpu::graphite::Device::Make(fRecorder, |
| 692 |
imageInfo, |
| 693 |
skgpu::Budgeted::kYes, |
| 694 |
skgpu::Mipmapped::kNo, |
| 695 |
SkBackingFit::kApprox, |
| 696 |
props ? *props : this->surfaceProps(), |
| 697 |
skgpu::graphite::LoadOp::kDiscard, |
| 698 |
"ImageFilterResult"); |
| 699 |
} |
| 700 |
|
| 701 |
sk_sp<SkSpecialImage> makeImage(const SkIRect& subset, sk_sp<SkImage> image) const override { |
| 702 |
return SkSpecialImages::MakeGraphite(fRecorder, subset, image, this->surfaceProps()); |
| 703 |
} |
| 704 |
|
| 705 |
sk_sp<SkImage> getCachedBitmap(const SkBitmap& data) const override { |
| 706 |
auto proxy = skgpu::graphite::RecorderPriv::CreateCachedProxy(fRecorder, data, |
| 707 |
"ImageFilterCachedBitmap"); |
| 708 |
if (!proxy) { |
| 709 |
return nullptr; |
| 710 |
} |
| 711 |
|
| 712 |
const SkColorInfo& colorInfo = data.info().colorInfo(); |
| 713 |
skgpu::Swizzle swizzle = fRecorder->priv().caps()->getReadSwizzle(colorInfo.colorType(), |
| 714 |
proxy->textureInfo()); |
| 715 |
return sk_make_sp<skgpu::graphite::Image>( |
| 716 |
skgpu::graphite::TextureProxyView(std::move(proxy), swizzle), |
| 717 |
colorInfo); |
| 718 |
} |
| 719 |
|
| 720 |
const SkBlurEngine* getBlurEngine() const override { return this; } |
| 721 |
|
| 722 |
// SkBlurEngine |
| 723 |
const SkBlurEngine::Algorithm* findAlgorithm(SkSize sigma, |
| 724 |
SkColorType colorType) const override { |
| 725 |
// The runtime effect blurs handle all tilemodes and color types |
| 726 |
return this; |
| 727 |
} |
| 728 |
|
| 729 |
// SkShaderBlurAlgorithm |
| 730 |
sk_sp<SkDevice> makeDevice(const SkImageInfo& imageInfo) const override { |
| 731 |
return skgpu::graphite::Device::Make(fRecorder, |
| 732 |
imageInfo, |
| 733 |
skgpu::Budgeted::kYes, |
| 734 |
skgpu::Mipmapped::kNo, |
| 735 |
SkBackingFit::kApprox, |
| 736 |
this->surfaceProps(), |
| 737 |
skgpu::graphite::LoadOp::kDiscard, |
| 738 |
"EvalBlurTexture"); |
| 739 |
} |
| 740 |
|
| 741 |
private: |
| 742 |
skgpu::graphite::Recorder* fRecorder; |
| 743 |
}; |
| 744 |
|
| 745 |
} // anonymous namespace |
| 746 |
|
| 747 |
sk_sp<Backend> MakeGraphiteBackend(skgpu::graphite::Recorder* recorder, |
| 748 |
const SkSurfaceProps& surfaceProps, |
| 749 |
SkColorType colorType) { |
| 750 |
SkASSERT(recorder); |
| 751 |
return sk_make_sp<GraphiteBackend>(recorder, surfaceProps, colorType); |
| 752 |
} |
| 753 |
|
| 754 |
} // namespace skif |