| 1 |
/* |
| 2 |
* Copyright 2012 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/surface/SkSurface_Ganesh.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkColorType.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkSurface.h" |
| 17 |
#include "include/core/SkSurfaceProps.h" |
| 18 |
#include "include/gpu/GpuTypes.h" |
| 19 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 20 |
#include "include/gpu/ganesh/GrContextThreadSafeProxy.h" |
| 21 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 22 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 23 |
#include "include/gpu/ganesh/GrTypes.h" |
| 24 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 25 |
#include "include/private/base/SkTo.h" |
| 26 |
#include "include/private/chromium/GrDeferredDisplayList.h" |
| 27 |
#include "include/private/chromium/GrSurfaceCharacterization.h" |
| 28 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 29 |
#include "src/core/SkDevice.h" |
| 30 |
#include "src/core/SkSurfacePriv.h" |
| 31 |
#include "src/gpu/RefCntedCallback.h" |
| 32 |
#include "src/gpu/SkBackingFit.h" |
| 33 |
#include "src/gpu/SkRenderEngineAbortf.h" |
| 34 |
#include "src/gpu/ganesh/Device.h" |
| 35 |
#include "src/gpu/ganesh/GrCaps.h" |
| 36 |
#include "src/gpu/ganesh/GrContextThreadSafeProxyPriv.h" |
| 37 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 38 |
#include "src/gpu/ganesh/GrGpuResourcePriv.h" |
| 39 |
#include "src/gpu/ganesh/GrProxyProvider.h" |
| 40 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 41 |
#include "src/gpu/ganesh/GrRenderTarget.h" |
| 42 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 43 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 44 |
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h" |
| 45 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 46 |
#include "src/gpu/ganesh/GrTexture.h" |
| 47 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 48 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 49 |
#include "src/image/SkImage_Base.h" |
| 50 |
|
| 51 |
#include <algorithm> |
| 52 |
#include <cstddef> |
| 53 |
#include <utility> |
| 54 |
|
| 55 |
class GrBackendSemaphore; |
| 56 |
class SkCapabilities; |
| 57 |
class SkPaint; |
| 58 |
class SkPixmap; |
| 59 |
|
| 60 |
SkSurface_Ganesh::SkSurface_Ganesh(sk_sp<skgpu::ganesh::Device> device) |
| 61 |
: INHERITED(device->width(), device->height(), &device->surfaceProps()) |
| 62 |
, fDevice(std::move(device)) { |
| 63 |
SkASSERT(fDevice->targetProxy()->priv().isExact()); |
| 64 |
} |
| 65 |
|
| 66 |
SkSurface_Ganesh::~SkSurface_Ganesh() { |
| 67 |
if (this->hasCachedImage()) { |
| 68 |
as_IB(this->refCachedImage())->generatingSurfaceIsDeleted(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
GrRecordingContext* SkSurface_Ganesh::onGetRecordingContext() const { |
| 73 |
return fDevice->recordingContext(); |
| 74 |
} |
| 75 |
|
| 76 |
skgpu::ganesh::Device* SkSurface_Ganesh::getDevice() { return fDevice.get(); } |
| 77 |
|
| 78 |
SkImageInfo SkSurface_Ganesh::imageInfo() const { return fDevice->imageInfo(); } |
| 79 |
|
| 80 |
static GrRenderTarget* prepare_rt_for_external_access(SkSurface_Ganesh* surface, |
| 81 |
SkSurfaces::BackendHandleAccess access) { |
| 82 |
auto dContext = surface->recordingContext()->asDirectContext(); |
| 83 |
if (!dContext) { |
| 84 |
return nullptr; |
| 85 |
} |
| 86 |
if (dContext->abandoned()) { |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
switch (access) { |
| 91 |
case SkSurfaces::BackendHandleAccess::kFlushRead: |
| 92 |
break; |
| 93 |
case SkSurfaces::BackendHandleAccess::kFlushWrite: |
| 94 |
case SkSurfaces::BackendHandleAccess::kDiscardWrite: |
| 95 |
// for now we don't special-case on Discard, but we may in the future. |
| 96 |
surface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode); |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
dContext->priv().flushSurface(surface->getDevice()->targetProxy()); |
| 101 |
|
| 102 |
// Grab the render target *after* firing notifications, as it may get switched if CoW kicks in. |
| 103 |
return surface->getDevice()->targetProxy()->peekRenderTarget(); |
| 104 |
} |
| 105 |
|
| 106 |
GrBackendTexture SkSurface_Ganesh::getBackendTexture(BackendHandleAccess access) { |
| 107 |
GrRenderTarget* rt = prepare_rt_for_external_access(this, access); |
| 108 |
if (!rt) { |
| 109 |
return GrBackendTexture(); // invalid |
| 110 |
} |
| 111 |
GrTexture* texture = rt->asTexture(); |
| 112 |
if (texture) { |
| 113 |
return texture->getBackendTexture(); |
| 114 |
} |
| 115 |
return GrBackendTexture(); // invalid |
| 116 |
} |
| 117 |
|
| 118 |
GrBackendRenderTarget SkSurface_Ganesh::getBackendRenderTarget(BackendHandleAccess access) { |
| 119 |
GrRenderTarget* rt = prepare_rt_for_external_access(this, access); |
| 120 |
if (!rt) { |
| 121 |
return GrBackendRenderTarget(); // invalid |
| 122 |
} |
| 123 |
|
| 124 |
return rt->getBackendRenderTarget(); |
| 125 |
} |
| 126 |
|
| 127 |
SkCanvas* SkSurface_Ganesh::onNewCanvas() { return new SkCanvas(fDevice); } |
| 128 |
|
| 129 |
sk_sp<SkSurface> SkSurface_Ganesh::onNewSurface(const SkImageInfo& info) { |
| 130 |
GrSurfaceProxyView targetView = fDevice->readSurfaceView(); |
| 131 |
int sampleCount = targetView.asRenderTargetProxy()->numSamples(); |
| 132 |
GrSurfaceOrigin origin = targetView.origin(); |
| 133 |
// TODO: Make caller specify this (change virtual signature of onNewSurface). |
| 134 |
static const skgpu::Budgeted kBudgeted = skgpu::Budgeted::kNo; |
| 135 |
|
| 136 |
bool isProtected = targetView.asRenderTargetProxy()->isProtected() == GrProtected::kYes; |
| 137 |
return SkSurfaces::RenderTarget( |
| 138 |
fDevice->recordingContext(), kBudgeted, info, sampleCount, origin, &this->props(), |
| 139 |
/* shouldCreateWithMips= */ false, isProtected); |
| 140 |
} |
| 141 |
|
| 142 |
sk_sp<SkImage> SkSurface_Ganesh::onNewImageSnapshot(const SkIRect* subset) { |
| 143 |
GrRenderTargetProxy* rtp = fDevice->targetProxy(); |
| 144 |
if (!rtp) { |
| 145 |
return nullptr; |
| 146 |
} |
| 147 |
|
| 148 |
auto rContext = fDevice->recordingContext(); |
| 149 |
|
| 150 |
GrSurfaceProxyView srcView = fDevice->readSurfaceView(); |
| 151 |
|
| 152 |
skgpu::Budgeted budgeted = rtp->isBudgeted(); |
| 153 |
|
| 154 |
if (subset || !srcView.asTextureProxy() || rtp->refsWrappedObjects()) { |
| 155 |
// If the original render target is a buffer originally created by the client, then we don't |
| 156 |
// want to ever retarget the SkSurface at another buffer we create. If the source is a |
| 157 |
// texture (and the image is not subsetted) we make a dual-proxied SkImage that will |
| 158 |
// attempt to share the backing store until the surface writes to the shared backing store |
| 159 |
// at which point it uses a copy. |
| 160 |
if (!subset && srcView.asTextureProxy()) { |
| 161 |
return SkImage_Ganesh::MakeWithVolatileSrc( |
| 162 |
sk_ref_sp(rContext), srcView, fDevice->imageInfo().colorInfo()); |
| 163 |
} |
| 164 |
auto rect = subset ? *subset : SkIRect::MakeSize(srcView.dimensions()); |
| 165 |
skgpu::Mipmapped mipmapped = srcView.mipmapped(); |
| 166 |
srcView = GrSurfaceProxyView::Copy(rContext, |
| 167 |
std::move(srcView), |
| 168 |
mipmapped, |
| 169 |
rect, |
| 170 |
SkBackingFit::kExact, |
| 171 |
budgeted, |
| 172 |
/*label=*/"SurfaceGpu_NewImageSnapshot"); |
| 173 |
} |
| 174 |
|
| 175 |
const SkImageInfo info = fDevice->imageInfo(); |
| 176 |
if (!srcView.asTextureProxy()) { |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
// The surfaceDrawContext coming out of SkGpuDevice should always be exact and the |
| 180 |
// above copy creates a kExact surfaceContext. |
| 181 |
SkASSERT(srcView.proxy()->priv().isExact()); |
| 182 |
return sk_make_sp<SkImage_Ganesh>( |
| 183 |
sk_ref_sp(rContext), kNeedNewImageUniqueID, std::move(srcView), info.colorInfo()); |
| 184 |
} |
| 185 |
|
| 186 |
void SkSurface_Ganesh::onWritePixels(const SkPixmap& src, int x, int y) { |
| 187 |
fDevice->writePixels(src, x, y); |
| 188 |
} |
| 189 |
|
| 190 |
void SkSurface_Ganesh::onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 191 |
SkIRect srcRect, |
| 192 |
RescaleGamma rescaleGamma, |
| 193 |
RescaleMode rescaleMode, |
| 194 |
ReadPixelsCallback callback, |
| 195 |
ReadPixelsContext context) { |
| 196 |
fDevice->asyncRescaleAndReadPixels(info, srcRect, rescaleGamma, rescaleMode, callback, context); |
| 197 |
} |
| 198 |
|
| 199 |
void SkSurface_Ganesh::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 200 |
bool readAlpha, |
| 201 |
sk_sp<SkColorSpace> dstColorSpace, |
| 202 |
SkIRect srcRect, |
| 203 |
SkISize dstSize, |
| 204 |
RescaleGamma rescaleGamma, |
| 205 |
RescaleMode rescaleMode, |
| 206 |
ReadPixelsCallback callback, |
| 207 |
ReadPixelsContext context) { |
| 208 |
fDevice->asyncRescaleAndReadPixelsYUV420(yuvColorSpace, |
| 209 |
readAlpha, |
| 210 |
std::move(dstColorSpace), |
| 211 |
srcRect, |
| 212 |
dstSize, |
| 213 |
rescaleGamma, |
| 214 |
rescaleMode, |
| 215 |
callback, |
| 216 |
context); |
| 217 |
} |
| 218 |
|
| 219 |
// Create a new render target and, if necessary, copy the contents of the old |
| 220 |
// render target into it. Note that this flushes the SkGpuDevice but |
| 221 |
// doesn't force an OpenGL flush. |
| 222 |
bool SkSurface_Ganesh::onCopyOnWrite(ContentChangeMode mode) { |
| 223 |
GrSurfaceProxyView readSurfaceView = fDevice->readSurfaceView(); |
| 224 |
|
| 225 |
// are we sharing our backing proxy with the image? Note this call should never create a new |
| 226 |
// image because onCopyOnWrite is only called when there is a cached image. |
| 227 |
sk_sp<SkImage> image = this->refCachedImage(); |
| 228 |
SkASSERT(image); |
| 229 |
|
| 230 |
if (static_cast<SkImage_Ganesh*>(image.get()) |
| 231 |
->surfaceMustCopyOnWrite(readSurfaceView.proxy())) { |
| 232 |
if (!fDevice->replaceBackingProxy(mode)) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
} else if (kDiscard_ContentChangeMode == mode) { |
| 236 |
this->SkSurface_Ganesh::onDiscard(); |
| 237 |
} |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
void SkSurface_Ganesh::onDiscard() { fDevice->discard(); } |
| 242 |
|
| 243 |
void SkSurface_Ganesh::resolveMSAA() { fDevice->resolveMSAA(); } |
| 244 |
|
| 245 |
bool SkSurface_Ganesh::onWait(int numSemaphores, |
| 246 |
const GrBackendSemaphore* waitSemaphores, |
| 247 |
bool deleteSemaphoresAfterWait) { |
| 248 |
return fDevice->wait(numSemaphores, waitSemaphores, deleteSemaphoresAfterWait); |
| 249 |
} |
| 250 |
|
| 251 |
bool SkSurface_Ganesh::onCharacterize(GrSurfaceCharacterization* characterization) const { |
| 252 |
auto direct = fDevice->recordingContext()->asDirectContext(); |
| 253 |
if (!direct) { |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
SkImageInfo ii = fDevice->imageInfo(); |
| 258 |
if (ii.colorType() == kUnknown_SkColorType) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
GrSurfaceProxyView readSurfaceView = fDevice->readSurfaceView(); |
| 263 |
size_t maxResourceBytes = direct->getResourceCacheLimit(); |
| 264 |
|
| 265 |
skgpu::Mipmapped mipmapped = readSurfaceView.asTextureProxy() |
| 266 |
? readSurfaceView.asTextureProxy()->mipmapped() |
| 267 |
: skgpu::Mipmapped::kNo; |
| 268 |
|
| 269 |
bool usesGLFBO0 = readSurfaceView.asRenderTargetProxy()->glRTFBOIDIs0(); |
| 270 |
// We should never get in the situation where we have a texture render target that is also |
| 271 |
// backend by FBO 0. |
| 272 |
SkASSERT(!usesGLFBO0 || !SkToBool(readSurfaceView.asTextureProxy())); |
| 273 |
|
| 274 |
bool vkRTSupportsInputAttachment = |
| 275 |
readSurfaceView.asRenderTargetProxy()->supportsVkInputAttachment(); |
| 276 |
|
| 277 |
GrBackendFormat format = readSurfaceView.proxy()->backendFormat(); |
| 278 |
int numSamples = readSurfaceView.asRenderTargetProxy()->numSamples(); |
| 279 |
GrProtected isProtected = readSurfaceView.asRenderTargetProxy()->isProtected(); |
| 280 |
|
| 281 |
characterization->set( |
| 282 |
direct->threadSafeProxy(), |
| 283 |
maxResourceBytes, |
| 284 |
ii, |
| 285 |
format, |
| 286 |
readSurfaceView.origin(), |
| 287 |
numSamples, |
| 288 |
GrSurfaceCharacterization::Textureable(SkToBool(readSurfaceView.asTextureProxy())), |
| 289 |
mipmapped, |
| 290 |
GrSurfaceCharacterization::UsesGLFBO0(usesGLFBO0), |
| 291 |
GrSurfaceCharacterization::VkRTSupportsInputAttachment(vkRTSupportsInputAttachment), |
| 292 |
GrSurfaceCharacterization::VulkanSecondaryCBCompatible(false), |
| 293 |
isProtected, |
| 294 |
this->props()); |
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
void SkSurface_Ganesh::onDraw(SkCanvas* canvas, |
| 299 |
SkScalar x, |
| 300 |
SkScalar y, |
| 301 |
const SkSamplingOptions& sampling, |
| 302 |
const SkPaint* paint) { |
| 303 |
// If the dst is also GPU we try to not force a new image snapshot (by calling the base class |
| 304 |
// onDraw) since that may not always perform the copy-on-write optimization. |
| 305 |
auto tryDraw = [&] { |
| 306 |
auto surfaceContext = fDevice->recordingContext(); |
| 307 |
auto canvasContext = GrAsDirectContext(canvas->recordingContext()); |
| 308 |
if (!canvasContext) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
if (canvasContext->priv().contextID() != surfaceContext->priv().contextID()) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
GrSurfaceProxyView srcView = fDevice->readSurfaceView(); |
| 315 |
if (!srcView.asTextureProxyRef()) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
// Possibly we could skip making an image here if SkGpuDevice exposed a lower level way |
| 319 |
// of drawing a texture proxy. |
| 320 |
const SkImageInfo info = fDevice->imageInfo(); |
| 321 |
sk_sp<SkImage> image = sk_make_sp<SkImage_Ganesh>(sk_ref_sp(canvasContext), |
| 322 |
kNeedNewImageUniqueID, |
| 323 |
std::move(srcView), |
| 324 |
info.colorInfo()); |
| 325 |
canvas->drawImage(image.get(), x, y, sampling, paint); |
| 326 |
return true; |
| 327 |
}; |
| 328 |
if (!tryDraw()) { |
| 329 |
INHERITED::onDraw(canvas, x, y, sampling, paint); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
bool SkSurface_Ganesh::onIsCompatible(const GrSurfaceCharacterization& characterization) const { |
| 334 |
auto direct = fDevice->recordingContext()->asDirectContext(); |
| 335 |
if (!direct) { |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
if (!characterization.isValid()) { |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
if (characterization.vulkanSecondaryCBCompatible()) { |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
SkImageInfo ii = fDevice->imageInfo(); |
| 348 |
if (ii.colorType() == kUnknown_SkColorType) { |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
GrSurfaceProxyView targetView = fDevice->readSurfaceView(); |
| 353 |
// As long as the current state if the context allows for greater or equal resources, |
| 354 |
// we allow the DDL to be replayed. |
| 355 |
// DDL TODO: should we just remove the resource check and ignore the cache limits on playback? |
| 356 |
size_t maxResourceBytes = direct->getResourceCacheLimit(); |
| 357 |
|
| 358 |
if (characterization.isTextureable()) { |
| 359 |
if (!targetView.asTextureProxy()) { |
| 360 |
// If the characterization was textureable we require the replay dest to also be |
| 361 |
// textureable. If the characterized surface wasn't textureable we allow the replay |
| 362 |
// dest to be textureable. |
| 363 |
return false; |
| 364 |
} |
| 365 |
|
| 366 |
if (characterization.isMipMapped() && |
| 367 |
skgpu::Mipmapped::kNo == targetView.asTextureProxy()->mipmapped()) { |
| 368 |
// Fail if the DDL's surface was mipmapped but the replay surface is not. |
| 369 |
// Allow drawing to proceed if the DDL was not mipmapped but the replay surface is. |
| 370 |
return false; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if (characterization.usesGLFBO0() != targetView.asRenderTargetProxy()->glRTFBOIDIs0()) { |
| 375 |
// FBO0-ness effects how MSAA and window rectangles work. If the characterization was |
| 376 |
// tagged as FBO0 it would never have been allowed to use window rectangles. If MSAA |
| 377 |
// was also never used then a DDL recorded with this characterization should be replayable |
| 378 |
// on a non-FBO0 surface. |
| 379 |
if (!characterization.usesGLFBO0() || characterization.sampleCount() > 1) { |
| 380 |
return false; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
GrBackendFormat format = targetView.asRenderTargetProxy()->backendFormat(); |
| 385 |
int numSamples = targetView.asRenderTargetProxy()->numSamples(); |
| 386 |
GrProtected isProtected = targetView.proxy()->isProtected(); |
| 387 |
|
| 388 |
return characterization.contextInfo() && |
| 389 |
characterization.contextInfo()->priv().matches(direct) && |
| 390 |
characterization.cacheMaxResourceBytes() <= maxResourceBytes && |
| 391 |
characterization.origin() == targetView.origin() && |
| 392 |
characterization.backendFormat() == format && characterization.width() == ii.width() && |
| 393 |
characterization.height() == ii.height() && |
| 394 |
characterization.colorType() == ii.colorType() && |
| 395 |
characterization.sampleCount() == numSamples && |
| 396 |
SkColorSpace::Equals(characterization.colorSpace(), ii.colorInfo().colorSpace()) && |
| 397 |
characterization.isProtected() == isProtected && |
| 398 |
characterization.surfaceProps() == fDevice->surfaceProps(); |
| 399 |
} |
| 400 |
|
| 401 |
bool SkSurface_Ganesh::draw(sk_sp<const GrDeferredDisplayList> ddl) { |
| 402 |
if (!ddl || !this->isCompatible(ddl->characterization())) { |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
auto direct = fDevice->recordingContext()->asDirectContext(); |
| 407 |
if (!direct || direct->abandoned()) { |
| 408 |
return false; |
| 409 |
} |
| 410 |
|
| 411 |
GrSurfaceProxyView view = fDevice->readSurfaceView(); |
| 412 |
|
| 413 |
direct->priv().createDDLTask(std::move(ddl), view.asRenderTargetProxyRef()); |
| 414 |
return true; |
| 415 |
} |
| 416 |
|
| 417 |
sk_sp<const SkCapabilities> SkSurface_Ganesh::onCapabilities() { |
| 418 |
return fDevice->recordingContext()->skCapabilities(); |
| 419 |
} |
| 420 |
|
| 421 |
/////////////////////////////////////////////////////////////////////////////// |
| 422 |
|
| 423 |
static bool validate_backend_texture(const GrCaps* caps, |
| 424 |
const GrBackendTexture& tex, |
| 425 |
int sampleCnt, |
| 426 |
GrColorType grCT, |
| 427 |
bool texturable) { |
| 428 |
if (!tex.isValid()) { |
| 429 |
RENDERENGINE_ABORTF("%s failed due to input texture being invalid", __func__); |
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
GrBackendFormat backendFormat = tex.getBackendFormat(); |
| 434 |
if (!backendFormat.isValid()) { |
| 435 |
RENDERENGINE_ABORTF("%s failed due to an invalid format", __func__); |
| 436 |
return false; |
| 437 |
} |
| 438 |
|
| 439 |
if (!caps->areColorTypeAndFormatCompatible(grCT, backendFormat)) { |
| 440 |
RENDERENGINE_ABORTF("%s failed due to an invalid format and colorType combination", |
| 441 |
__func__); |
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
if (!caps->isFormatAsColorTypeRenderable(grCT, backendFormat, sampleCnt)) { |
| 446 |
RENDERENGINE_ABORTF( |
| 447 |
"%s failed due to no supported rendering path for the selected " |
| 448 |
"format and colorType", |
| 449 |
__func__); |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
if (texturable && !caps->isFormatTexturable(backendFormat, tex.textureType())) { |
| 454 |
RENDERENGINE_ABORTF( |
| 455 |
"%s failed due to no texturing support for the selected format and " |
| 456 |
"colorType", |
| 457 |
__func__); |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
return true; |
| 462 |
} |
| 463 |
|
| 464 |
bool SkSurface_Ganesh::replaceBackendTexture(const GrBackendTexture& backendTexture, |
| 465 |
GrSurfaceOrigin origin, |
| 466 |
ContentChangeMode mode, |
| 467 |
TextureReleaseProc releaseProc, |
| 468 |
ReleaseContext releaseContext) { |
| 469 |
auto releaseHelper = skgpu::RefCntedCallback::Make(releaseProc, releaseContext); |
| 470 |
|
| 471 |
auto rContext = fDevice->recordingContext(); |
| 472 |
if (rContext->abandoned()) { |
| 473 |
return false; |
| 474 |
} |
| 475 |
if (!backendTexture.isValid()) { |
| 476 |
return false; |
| 477 |
} |
| 478 |
if (backendTexture.width() != this->width() || backendTexture.height() != this->height()) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
auto* oldRTP = fDevice->targetProxy(); |
| 482 |
auto oldProxy = sk_ref_sp(oldRTP->asTextureProxy()); |
| 483 |
if (!oldProxy) { |
| 484 |
return false; |
| 485 |
} |
| 486 |
auto* oldTexture = oldProxy->peekTexture(); |
| 487 |
if (!oldTexture) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
if (!oldTexture->resourcePriv().refsWrappedObjects()) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
if (oldTexture->backendFormat() != backendTexture.getBackendFormat()) { |
| 494 |
return false; |
| 495 |
} |
| 496 |
if (oldTexture->getBackendTexture().isSameTexture(backendTexture)) { |
| 497 |
return false; |
| 498 |
} |
| 499 |
SkASSERT(oldTexture->asRenderTarget()); |
| 500 |
int sampleCnt = oldTexture->asRenderTarget()->numSamples(); |
| 501 |
GrColorType grColorType = SkColorTypeToGrColorType(this->getCanvas()->imageInfo().colorType()); |
| 502 |
if (!validate_backend_texture( |
| 503 |
rContext->priv().caps(), backendTexture, sampleCnt, grColorType, |
| 504 |
/* texturable= */ true)) { |
| 505 |
return false; |
| 506 |
} |
| 507 |
|
| 508 |
sk_sp<SkColorSpace> colorSpace = fDevice->imageInfo().refColorSpace(); |
| 509 |
|
| 510 |
SkASSERT(sampleCnt > 0); |
| 511 |
sk_sp<GrTextureProxy> proxy(rContext->priv().proxyProvider()->wrapRenderableBackendTexture( |
| 512 |
backendTexture, |
| 513 |
sampleCnt, |
| 514 |
kBorrow_GrWrapOwnership, |
| 515 |
GrWrapCacheable::kNo, |
| 516 |
std::move(releaseHelper))); |
| 517 |
if (!proxy) { |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
return fDevice->replaceBackingProxy(mode, |
| 522 |
sk_ref_sp(proxy->asRenderTargetProxy()), |
| 523 |
grColorType, |
| 524 |
std::move(colorSpace), |
| 525 |
origin, |
| 526 |
this->props()); |
| 527 |
} |
| 528 |
|
| 529 |
bool validate_backend_render_target(const GrCaps* caps, |
| 530 |
const GrBackendRenderTarget& rt, |
| 531 |
GrColorType grCT) { |
| 532 |
if (!caps->areColorTypeAndFormatCompatible(grCT, rt.getBackendFormat())) { |
| 533 |
return false; |
| 534 |
} |
| 535 |
|
| 536 |
if (!caps->isFormatAsColorTypeRenderable(grCT, rt.getBackendFormat(), rt.sampleCnt())) { |
| 537 |
return false; |
| 538 |
} |
| 539 |
|
| 540 |
// We require the stencil bits to be either 0, 8, or 16. |
| 541 |
int stencilBits = rt.stencilBits(); |
| 542 |
if (stencilBits != 0 && stencilBits != 8 && stencilBits != 16) { |
| 543 |
return false; |
| 544 |
} |
| 545 |
|
| 546 |
return true; |
| 547 |
} |
| 548 |
|
| 549 |
namespace SkSurfaces { |
| 550 |
sk_sp<SkSurface> RenderTarget(GrRecordingContext* rContext, |
| 551 |
const GrSurfaceCharacterization& c, |
| 552 |
skgpu::Budgeted budgeted) { |
| 553 |
if (!rContext || !c.isValid()) { |
| 554 |
return nullptr; |
| 555 |
} |
| 556 |
|
| 557 |
if (c.usesGLFBO0()) { |
| 558 |
// If we are making the surface we will never use FBO0. |
| 559 |
return nullptr; |
| 560 |
} |
| 561 |
|
| 562 |
if (c.vulkanSecondaryCBCompatible()) { |
| 563 |
return nullptr; |
| 564 |
} |
| 565 |
|
| 566 |
auto device = rContext->priv().createDevice(budgeted, |
| 567 |
c.imageInfo(), |
| 568 |
SkBackingFit::kExact, |
| 569 |
c.sampleCount(), |
| 570 |
skgpu::Mipmapped(c.isMipMapped()), |
| 571 |
c.isProtected(), |
| 572 |
c.origin(), |
| 573 |
c.surfaceProps(), |
| 574 |
skgpu::ganesh::Device::InitContents::kClear); |
| 575 |
if (!device) { |
| 576 |
return nullptr; |
| 577 |
} |
| 578 |
|
| 579 |
sk_sp<SkSurface> result = sk_make_sp<SkSurface_Ganesh>(std::move(device)); |
| 580 |
#ifdef SK_DEBUG |
| 581 |
if (result) { |
| 582 |
SkASSERT(result->isCompatible(c)); |
| 583 |
} |
| 584 |
#endif |
| 585 |
|
| 586 |
return result; |
| 587 |
} |
| 588 |
|
| 589 |
sk_sp<SkSurface> RenderTarget(GrRecordingContext* rContext, |
| 590 |
skgpu::Budgeted budgeted, |
| 591 |
const SkImageInfo& info, |
| 592 |
int sampleCount, |
| 593 |
GrSurfaceOrigin origin, |
| 594 |
const SkSurfaceProps* props, |
| 595 |
bool shouldCreateWithMips, |
| 596 |
bool isProtected) { |
| 597 |
if (!rContext) { |
| 598 |
return nullptr; |
| 599 |
} |
| 600 |
sampleCount = std::max(1, sampleCount); |
| 601 |
skgpu::Mipmapped mipmapped = |
| 602 |
shouldCreateWithMips ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo; |
| 603 |
|
| 604 |
if (!rContext->priv().caps()->mipmapSupport()) { |
| 605 |
mipmapped = skgpu::Mipmapped::kNo; |
| 606 |
} |
| 607 |
|
| 608 |
auto device = rContext->priv().createDevice(budgeted, |
| 609 |
info, |
| 610 |
SkBackingFit::kExact, |
| 611 |
sampleCount, |
| 612 |
mipmapped, |
| 613 |
GrProtected(isProtected), |
| 614 |
origin, |
| 615 |
SkSurfacePropsCopyOrDefault(props), |
| 616 |
skgpu::ganesh::Device::InitContents::kClear); |
| 617 |
if (!device) { |
| 618 |
return nullptr; |
| 619 |
} |
| 620 |
return sk_make_sp<SkSurface_Ganesh>(std::move(device)); |
| 621 |
} |
| 622 |
|
| 623 |
sk_sp<SkSurface> WrapBackendTexture(GrRecordingContext* rContext, |
| 624 |
const GrBackendTexture& tex, |
| 625 |
GrSurfaceOrigin origin, |
| 626 |
int sampleCnt, |
| 627 |
SkColorType colorType, |
| 628 |
sk_sp<SkColorSpace> colorSpace, |
| 629 |
const SkSurfaceProps* props, |
| 630 |
TextureReleaseProc textureReleaseProc, |
| 631 |
ReleaseContext releaseContext) { |
| 632 |
auto releaseHelper = skgpu::RefCntedCallback::Make(textureReleaseProc, releaseContext); |
| 633 |
|
| 634 |
if (!rContext) { |
| 635 |
RENDERENGINE_ABORTF("%s failed due to a null context ", __func__); |
| 636 |
return nullptr; |
| 637 |
} |
| 638 |
sampleCnt = std::max(1, sampleCnt); |
| 639 |
|
| 640 |
GrColorType grColorType = SkColorTypeToGrColorType(colorType); |
| 641 |
if (grColorType == GrColorType::kUnknown) { |
| 642 |
RENDERENGINE_ABORTF("%s failed due to an unsupported colorType %d", __func__, colorType); |
| 643 |
return nullptr; |
| 644 |
} |
| 645 |
|
| 646 |
if (!validate_backend_texture(rContext->priv().caps(), tex, sampleCnt, grColorType, true)) { |
| 647 |
return nullptr; |
| 648 |
} |
| 649 |
|
| 650 |
sk_sp<GrTextureProxy> proxy(rContext->priv().proxyProvider()->wrapRenderableBackendTexture( |
| 651 |
tex, |
| 652 |
sampleCnt, |
| 653 |
kBorrow_GrWrapOwnership, |
| 654 |
GrWrapCacheable::kNo, |
| 655 |
std::move(releaseHelper))); |
| 656 |
if (!proxy) { |
| 657 |
RENDERENGINE_ABORTF("%s failed to wrap the texture into a renderable target", __func__); |
| 658 |
return nullptr; |
| 659 |
} |
| 660 |
|
| 661 |
auto device = rContext->priv().createDevice(grColorType, |
| 662 |
std::move(proxy), |
| 663 |
std::move(colorSpace), |
| 664 |
origin, |
| 665 |
SkSurfacePropsCopyOrDefault(props), |
| 666 |
skgpu::ganesh::Device::InitContents::kUninit); |
| 667 |
if (!device) { |
| 668 |
RENDERENGINE_ABORTF("%s failed to wrap the renderTarget into a surface", __func__); |
| 669 |
return nullptr; |
| 670 |
} |
| 671 |
|
| 672 |
return sk_make_sp<SkSurface_Ganesh>(std::move(device)); |
| 673 |
} |
| 674 |
|
| 675 |
sk_sp<SkSurface> WrapBackendRenderTarget(GrRecordingContext* rContext, |
| 676 |
const GrBackendRenderTarget& rt, |
| 677 |
GrSurfaceOrigin origin, |
| 678 |
SkColorType colorType, |
| 679 |
sk_sp<SkColorSpace> colorSpace, |
| 680 |
const SkSurfaceProps* props, |
| 681 |
RenderTargetReleaseProc relProc, |
| 682 |
ReleaseContext releaseContext) { |
| 683 |
auto releaseHelper = skgpu::RefCntedCallback::Make(relProc, releaseContext); |
| 684 |
|
| 685 |
if (!rContext || !rt.isValid()) { |
| 686 |
return nullptr; |
| 687 |
} |
| 688 |
|
| 689 |
GrColorType grColorType = SkColorTypeToGrColorType(colorType); |
| 690 |
if (grColorType == GrColorType::kUnknown) { |
| 691 |
return nullptr; |
| 692 |
} |
| 693 |
|
| 694 |
if (!validate_backend_render_target(rContext->priv().caps(), rt, grColorType)) { |
| 695 |
return nullptr; |
| 696 |
} |
| 697 |
|
| 698 |
auto proxyProvider = rContext->priv().proxyProvider(); |
| 699 |
auto proxy = proxyProvider->wrapBackendRenderTarget(rt, std::move(releaseHelper)); |
| 700 |
if (!proxy) { |
| 701 |
return nullptr; |
| 702 |
} |
| 703 |
|
| 704 |
auto device = rContext->priv().createDevice(grColorType, |
| 705 |
std::move(proxy), |
| 706 |
std::move(colorSpace), |
| 707 |
origin, |
| 708 |
SkSurfacePropsCopyOrDefault(props), |
| 709 |
skgpu::ganesh::Device::InitContents::kUninit); |
| 710 |
if (!device) { |
| 711 |
return nullptr; |
| 712 |
} |
| 713 |
|
| 714 |
return sk_make_sp<SkSurface_Ganesh>(std::move(device)); |
| 715 |
} |
| 716 |
|
| 717 |
GrBackendTexture GetBackendTexture(SkSurface* surface, BackendHandleAccess access) { |
| 718 |
if (surface == nullptr) { |
| 719 |
return GrBackendTexture(); |
| 720 |
} |
| 721 |
auto sb = asSB(surface); |
| 722 |
if (!sb->isGaneshBacked()) { |
| 723 |
return GrBackendTexture(); |
| 724 |
} |
| 725 |
return static_cast<SkSurface_Ganesh*>(surface)->getBackendTexture(access); |
| 726 |
} |
| 727 |
|
| 728 |
GrBackendRenderTarget GetBackendRenderTarget(SkSurface* surface, BackendHandleAccess access) { |
| 729 |
if (surface == nullptr) { |
| 730 |
return GrBackendRenderTarget(); |
| 731 |
} |
| 732 |
auto sb = asSB(surface); |
| 733 |
if (!sb->isGaneshBacked()) { |
| 734 |
return GrBackendRenderTarget(); |
| 735 |
} |
| 736 |
return static_cast<SkSurface_Ganesh*>(surface)->getBackendRenderTarget(access); |
| 737 |
} |
| 738 |
|
| 739 |
void ResolveMSAA(SkSurface* surface) { |
| 740 |
if (!surface) { |
| 741 |
return; |
| 742 |
} |
| 743 |
auto sb = asSB(surface); |
| 744 |
if (!sb->isGaneshBacked()) { |
| 745 |
return; |
| 746 |
} |
| 747 |
auto gs = static_cast<SkSurface_Ganesh*>(surface); |
| 748 |
gs->resolveMSAA(); |
| 749 |
} |
| 750 |
|
| 751 |
} // namespace SkSurfaces |
| 752 |
|
| 753 |
namespace skgpu::ganesh { |
| 754 |
GrSemaphoresSubmitted Flush(SkSurface* surface) { |
| 755 |
if (!surface) { |
| 756 |
return GrSemaphoresSubmitted::kNo; |
| 757 |
} |
| 758 |
if (auto rContext = surface->recordingContext(); rContext != nullptr) { |
| 759 |
return rContext->asDirectContext()->flush(surface, {}); |
| 760 |
} |
| 761 |
return GrSemaphoresSubmitted::kNo; |
| 762 |
} |
| 763 |
|
| 764 |
GrSemaphoresSubmitted Flush(sk_sp<SkSurface> surface) { |
| 765 |
if (!surface) { |
| 766 |
return GrSemaphoresSubmitted::kNo; |
| 767 |
} |
| 768 |
if (auto rContext = surface->recordingContext(); rContext != nullptr) { |
| 769 |
return rContext->asDirectContext()->flush(surface.get(), {}); |
| 770 |
} |
| 771 |
return GrSemaphoresSubmitted::kNo; |
| 772 |
} |
| 773 |
|
| 774 |
void FlushAndSubmit(SkSurface* surface) { |
| 775 |
if (!surface) { |
| 776 |
return; |
| 777 |
} |
| 778 |
if (auto rContext = surface->recordingContext(); rContext != nullptr) { |
| 779 |
rContext->asDirectContext()->flushAndSubmit(surface, GrSyncCpu::kNo); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
void FlushAndSubmit(sk_sp<SkSurface> surface) { |
| 784 |
if (!surface) { |
| 785 |
return; |
| 786 |
} |
| 787 |
if (auto rContext = surface->recordingContext(); rContext != nullptr) { |
| 788 |
rContext->asDirectContext()->flushAndSubmit(surface.get(), GrSyncCpu::kNo); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
} // namespace skgpu::ganesh |