reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkSurfaceCharacterization.h" |
| 10 | #include "include/gpu/GrBackendSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrTexture.h" |
| 12 | #include "include/private/GrRecordingContext.h" |
| 13 | #include "include/private/SkDeferredDisplayList.h" |
| 14 | #include "src/core/SkImagePriv.h" |
| 15 | #include "src/gpu/GrAHardwareBufferUtils.h" |
| 16 | #include "src/gpu/GrCaps.h" |
| 17 | #include "src/gpu/GrContextPriv.h" |
| 18 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
| 19 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 22 | #include "src/gpu/GrRenderTargetProxyPriv.h" |
| 23 | #include "src/gpu/SkGpuDevice.h" |
| 24 | #include "src/image/SkImage_Base.h" |
| 25 | #include "src/image/SkImage_Gpu.h" |
| 26 | #include "src/image/SkSurface_Base.h" |
| 27 | #include "src/image/SkSurface_Gpu.h" |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 28 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 29 | #if SK_SUPPORT_GPU |
| 30 | |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 31 | SkSurface_Gpu::SkSurface_Gpu(sk_sp<SkGpuDevice> device) |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 32 | : INHERITED(device->width(), device->height(), &device->surfaceProps()) |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 33 | , fDevice(std::move(device)) { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 34 | SkASSERT(fDevice->accessRenderTargetContext()->asSurfaceProxy()->priv().isExact()); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 35 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 36 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 37 | SkSurface_Gpu::~SkSurface_Gpu() { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 38 | } |
| 39 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 40 | static GrRenderTarget* prepare_rt_for_external_access(SkSurface_Gpu* surface, |
| 41 | SkSurface::BackendHandleAccess access) { |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 42 | switch (access) { |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 43 | case SkSurface::kFlushRead_BackendHandleAccess: |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 44 | break; |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 45 | case SkSurface::kFlushWrite_BackendHandleAccess: |
| 46 | case SkSurface::kDiscardWrite_BackendHandleAccess: |
reed | 884200e | 2015-06-29 09:00:20 -0700 | [diff] [blame] | 47 | // for now we don't special-case on Discard, but we may in the future. |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 48 | surface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode); |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 49 | break; |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 50 | } |
fmalita | e263908 | 2015-08-06 07:04:51 -0700 | [diff] [blame] | 51 | |
| 52 | // Grab the render target *after* firing notifications, as it may get switched if CoW kicks in. |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 53 | surface->getDevice()->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo()); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 54 | GrRenderTargetContext* rtc = surface->getDevice()->accessRenderTargetContext(); |
| 55 | return rtc->accessRenderTarget(); |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 58 | GrBackendTexture SkSurface_Gpu::onGetBackendTexture(BackendHandleAccess access) { |
| 59 | GrRenderTarget* rt = prepare_rt_for_external_access(this, access); |
| 60 | if (!rt) { |
| 61 | return GrBackendTexture(); // invalid |
| 62 | } |
| 63 | GrTexture* texture = rt->asTexture(); |
| 64 | if (texture) { |
| 65 | return texture->getBackendTexture(); |
| 66 | } |
| 67 | return GrBackendTexture(); // invalid |
| 68 | } |
| 69 | |
| 70 | GrBackendRenderTarget SkSurface_Gpu::onGetBackendRenderTarget(BackendHandleAccess access) { |
| 71 | GrRenderTarget* rt = prepare_rt_for_external_access(this, access); |
| 72 | if (!rt) { |
| 73 | return GrBackendRenderTarget(); // invalid |
| 74 | } |
| 75 | |
| 76 | return rt->getBackendRenderTarget(); |
| 77 | } |
| 78 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 79 | SkCanvas* SkSurface_Gpu::onNewCanvas() { return new SkCanvas(fDevice); } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 80 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 81 | sk_sp<SkSurface> SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 82 | int sampleCount = fDevice->accessRenderTargetContext()->numSamples(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 83 | GrSurfaceOrigin origin = fDevice->accessRenderTargetContext()->origin(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 84 | // TODO: Make caller specify this (change virtual signature of onNewSurface). |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 85 | static const SkBudgeted kBudgeted = SkBudgeted::kNo; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 86 | return SkSurface::MakeRenderTarget(fDevice->context(), kBudgeted, info, sampleCount, |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 87 | origin, &this->props()); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 90 | sk_sp<SkImage> SkSurface_Gpu::onNewImageSnapshot(const SkIRect* subset) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 91 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 92 | if (!rtc) { |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 93 | return nullptr; |
| 94 | } |
| 95 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 96 | GrContext* ctx = fDevice->context(); |
| 97 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 98 | if (!rtc->asSurfaceProxy()) { |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | SkBudgeted budgeted = rtc->asSurfaceProxy()->isBudgeted(); |
| 103 | |
| 104 | sk_sp<GrTextureProxy> srcProxy = rtc->asTextureProxyRef(); |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 105 | |
| 106 | if (subset) { |
Brian Salomon | 078e8fa | 2019-11-22 04:10:18 +0000 | [diff] [blame^] | 107 | srcProxy = GrSurfaceProxy::Copy(ctx, rtc->asSurfaceProxy(), rtc->colorInfo().colorType(), |
| 108 | rtc->mipMapped(), *subset, SkBackingFit::kExact, budgeted); |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 109 | } else if (!srcProxy || rtc->priv().refsWrappedObjects()) { |
| 110 | // If the original render target is a buffer originally created by the client, then we don't |
| 111 | // want to ever retarget the SkSurface at another buffer we create. Force a copy now to avoid |
| 112 | // copy-on-write. |
Robert Phillips | 81dd3e0 | 2017-06-23 11:59:24 -0400 | [diff] [blame] | 113 | SkASSERT(rtc->origin() == rtc->asSurfaceProxy()->origin()); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 114 | |
Brian Salomon | 078e8fa | 2019-11-22 04:10:18 +0000 | [diff] [blame^] | 115 | srcProxy = GrSurfaceProxy::Copy(ctx, rtc->asSurfaceProxy(), rtc->colorInfo().colorType(), |
| 116 | rtc->mipMapped(), SkBackingFit::kExact, budgeted); |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 117 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 118 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 119 | const SkImageInfo info = fDevice->imageInfo(); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 120 | sk_sp<SkImage> image; |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 121 | if (srcProxy) { |
| 122 | // The renderTargetContext coming out of SkGpuDevice should always be exact and the |
| 123 | // above copy creates a kExact surfaceContext. |
| 124 | SkASSERT(srcProxy->priv().isExact()); |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 125 | image = sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, info.alphaType(), |
| 126 | std::move(srcProxy), info.refColorSpace()); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 127 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 128 | return image; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 131 | void SkSurface_Gpu::onWritePixels(const SkPixmap& src, int x, int y) { |
| 132 | fDevice->writePixels(src, x, y); |
| 133 | } |
| 134 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 135 | void SkSurface_Gpu::onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 136 | const SkIRect& srcRect, |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 137 | RescaleGamma rescaleGamma, |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 138 | SkFilterQuality rescaleQuality, |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 139 | ReadPixelsCallback callback, |
| 140 | ReadPixelsContext context) { |
| 141 | auto* rtc = this->fDevice->accessRenderTargetContext(); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 142 | rtc->asyncRescaleAndReadPixels(info, srcRect, rescaleGamma, rescaleQuality, callback, context); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 145 | void SkSurface_Gpu::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 146 | sk_sp<SkColorSpace> dstColorSpace, |
| 147 | const SkIRect& srcRect, |
| 148 | const SkISize& dstSize, |
| 149 | RescaleGamma rescaleGamma, |
| 150 | SkFilterQuality rescaleQuality, |
| 151 | ReadPixelsCallback callback, |
| 152 | ReadPixelsContext context) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 153 | auto* rtc = this->fDevice->accessRenderTargetContext(); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 154 | rtc->asyncRescaleAndReadPixelsYUV420(yuvColorSpace, |
| 155 | std::move(dstColorSpace), |
| 156 | srcRect, |
| 157 | dstSize, |
| 158 | rescaleGamma, |
| 159 | rescaleQuality, |
| 160 | callback, |
| 161 | context); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 162 | } |
| 163 | |
kkinnunen | abcfab4 | 2015-02-22 22:53:44 -0800 | [diff] [blame] | 164 | // Create a new render target and, if necessary, copy the contents of the old |
| 165 | // render target into it. Note that this flushes the SkGpuDevice but |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 166 | // doesn't force an OpenGL flush. |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 167 | void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) { |
Robert Phillips | 602b79c | 2017-06-23 11:26:28 -0400 | [diff] [blame] | 168 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 169 | |
| 170 | // are we sharing our backing proxy with the image? Note this call should never create a new |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 171 | // image because onCopyOnWrite is only called when there is a cached image. |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 172 | sk_sp<SkImage> image(this->refCachedImage()); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 173 | SkASSERT(image); |
Robert Phillips | 602b79c | 2017-06-23 11:26:28 -0400 | [diff] [blame] | 174 | |
| 175 | GrSurfaceProxy* imageProxy = ((SkImage_Base*) image.get())->peekProxy(); |
| 176 | SkASSERT(imageProxy); |
| 177 | |
| 178 | if (rtc->asSurfaceProxy()->underlyingUniqueID() == imageProxy->underlyingUniqueID()) { |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 179 | fDevice->replaceRenderTargetContext(SkSurface::kRetain_ContentChangeMode == mode); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 180 | } else if (kDiscard_ContentChangeMode == mode) { |
| 181 | this->SkSurface_Gpu::onDiscard(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 182 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 183 | } |
| 184 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 185 | void SkSurface_Gpu::onDiscard() { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 186 | fDevice->accessRenderTargetContext()->discard(); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 189 | GrSemaphoresSubmitted SkSurface_Gpu::onFlush(BackendSurfaceAccess access, |
| 190 | const GrFlushInfo& info) { |
| 191 | return fDevice->flush(access, info); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 194 | bool SkSurface_Gpu::onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) { |
| 195 | return fDevice->wait(numSemaphores, waitSemaphores); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 198 | bool SkSurface_Gpu::onCharacterize(SkSurfaceCharacterization* characterization) const { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 199 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
Robert Phillips | 8def8bf | 2017-11-30 08:46:03 -0500 | [diff] [blame] | 200 | GrContext* ctx = fDevice->context(); |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 201 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 202 | size_t maxResourceBytes = ctx->getResourceCacheLimit(); |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 203 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 204 | bool mipmapped = rtc->asTextureProxy() ? GrMipMapped::kYes == rtc->asTextureProxy()->mipMapped() |
| 205 | : false; |
| 206 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 207 | SkColorType ct = GrColorTypeToSkColorType(rtc->colorInfo().colorType()); |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 208 | if (ct == kUnknown_SkColorType) { |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 212 | bool usesGLFBO0 = rtc->asRenderTargetProxy()->rtPriv().glRTFBOIDIs0(); |
| 213 | // We should never get in the situation where we have a texture render target that is also |
| 214 | // backend by FBO 0. |
| 215 | SkASSERT(!usesGLFBO0 || !SkToBool(rtc->asTextureProxy())); |
| 216 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 217 | SkImageInfo ii = SkImageInfo::Make(rtc->width(), rtc->height(), ct, kPremul_SkAlphaType, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 218 | rtc->colorInfo().refColorSpace()); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 219 | |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 220 | GrBackendFormat format = rtc->asSurfaceProxy()->backendFormat(); |
Robert Phillips | 8def8bf | 2017-11-30 08:46:03 -0500 | [diff] [blame] | 221 | |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 222 | characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, format, |
| 223 | rtc->origin(), rtc->numSamples(), |
| 224 | SkSurfaceCharacterization::Textureable(SkToBool(rtc->asTextureProxy())), |
| 225 | SkSurfaceCharacterization::MipMapped(mipmapped), |
| 226 | SkSurfaceCharacterization::UsesGLFBO0(usesGLFBO0), |
| 227 | SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false), |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 228 | GrProtected(rtc->asRenderTargetProxy()->isProtected()), |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 229 | this->props()); |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
Brian Salomon | c552eaf | 2019-03-20 15:45:50 -0400 | [diff] [blame] | 233 | void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) { |
| 234 | // If the dst is also GPU we try to not force a new image snapshot (by calling the base class |
| 235 | // onDraw) since that may not always perform the copy-on-write optimization. |
| 236 | auto tryDraw = [&] { |
| 237 | SkASSERT(fDevice->context()->priv().asDirectContext()); |
| 238 | GrContext* context = fDevice->context(); |
| 239 | GrContext* canvasContext = canvas->getGrContext(); |
| 240 | if (!canvasContext) { |
| 241 | return false; |
| 242 | } |
| 243 | if (!canvasContext->priv().asDirectContext() || |
| 244 | canvasContext->priv().contextID() != context->priv().contextID()) { |
| 245 | return false; |
| 246 | } |
| 247 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 248 | if (!rtc) { |
| 249 | return false; |
| 250 | } |
| 251 | sk_sp<GrTextureProxy> srcProxy = rtc->asTextureProxyRef(); |
| 252 | if (!srcProxy) { |
| 253 | return false; |
| 254 | } |
| 255 | // Possibly we could skip making an image here if SkGpuDevice exposed a lower level way |
| 256 | // of drawing a texture proxy. |
| 257 | const SkImageInfo info = fDevice->imageInfo(); |
| 258 | sk_sp<SkImage> image; |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 259 | image = sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, info.alphaType(), |
| 260 | std::move(srcProxy), info.refColorSpace()); |
Brian Salomon | c552eaf | 2019-03-20 15:45:50 -0400 | [diff] [blame] | 261 | canvas->drawImage(image, x, y, paint); |
| 262 | return true; |
| 263 | }; |
| 264 | if (!tryDraw()) { |
| 265 | INHERITED::onDraw(canvas, x, y, paint); |
| 266 | } |
| 267 | } |
| 268 | |
Robert Phillips | 9907e6e | 2019-06-25 14:47:04 -0400 | [diff] [blame] | 269 | bool SkSurface_Gpu::onIsCompatible(const SkSurfaceCharacterization& characterization) const { |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 270 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
Robert Phillips | 8def8bf | 2017-11-30 08:46:03 -0500 | [diff] [blame] | 271 | GrContext* ctx = fDevice->context(); |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 272 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 273 | if (!characterization.isValid()) { |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 277 | if (characterization.vulkanSecondaryCBCompatible()) { |
| 278 | return false; |
| 279 | } |
| 280 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 281 | // As long as the current state if the context allows for greater or equal resources, |
| 282 | // we allow the DDL to be replayed. |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 283 | // DDL TODO: should we just remove the resource check and ignore the cache limits on playback? |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 284 | size_t maxResourceBytes = ctx->getResourceCacheLimit(); |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 285 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 286 | if (characterization.isTextureable()) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 287 | if (!rtc->asTextureProxy()) { |
| 288 | // If the characterization was textureable we require the replay dest to also be |
| 289 | // textureable. If the characterized surface wasn't textureable we allow the replay |
| 290 | // dest to be textureable. |
| 291 | return false; |
| 292 | } |
| 293 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 294 | if (characterization.isMipMapped() && |
| 295 | GrMipMapped::kNo == rtc->asTextureProxy()->mipMapped()) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 296 | // Fail if the DDL's surface was mipmapped but the replay surface is not. |
| 297 | // Allow drawing to proceed if the DDL was not mipmapped but the replay surface is. |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 302 | if (characterization.usesGLFBO0() != rtc->asRenderTargetProxy()->rtPriv().glRTFBOIDIs0()) { |
| 303 | return false; |
| 304 | } |
| 305 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 306 | SkColorType rtcColorType = GrColorTypeToSkColorType(rtc->colorInfo().colorType()); |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 307 | if (rtcColorType == kUnknown_SkColorType) { |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 311 | GrProtected isProtected = GrProtected(rtc->asSurfaceProxy()->isProtected()); |
| 312 | |
Robert Phillips | fe0963c | 2019-02-07 13:25:07 -0500 | [diff] [blame] | 313 | return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) && |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 314 | characterization.cacheMaxResourceBytes() <= maxResourceBytes && |
| 315 | characterization.origin() == rtc->origin() && |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 316 | characterization.backendFormat() == rtc->asSurfaceProxy()->backendFormat() && |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 317 | characterization.width() == rtc->width() && characterization.height() == rtc->height() && |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 318 | characterization.colorType() == rtcColorType && |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 319 | characterization.sampleCount() == rtc->numSamples() && |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 320 | SkColorSpace::Equals(characterization.colorSpace(), rtc->colorInfo().colorSpace()) && |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 321 | characterization.isProtected() == isProtected && |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 322 | characterization.surfaceProps() == rtc->surfaceProps(); |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 325 | bool SkSurface_Gpu::onDraw(const SkDeferredDisplayList* ddl) { |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 326 | if (!ddl || !this->isCompatible(ddl->characterization())) { |
Robert Phillips | 7ffbcf9 | 2017-12-04 12:52:46 -0500 | [diff] [blame] | 327 | return false; |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 330 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 331 | GrContext* ctx = fDevice->context(); |
| 332 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 333 | ctx->priv().copyRenderTasksFromDDL(ddl, rtc->asRenderTargetProxy()); |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 334 | return true; |
Robert Phillips | ad8a43f | 2017-08-30 12:06:35 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 338 | /////////////////////////////////////////////////////////////////////////////// |
| 339 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 340 | sk_sp<SkSurface> SkSurface::MakeRenderTarget(GrRecordingContext* context, |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 341 | const SkSurfaceCharacterization& c, |
| 342 | SkBudgeted budgeted) { |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 343 | if (!context || !c.isValid()) { |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 344 | return nullptr; |
| 345 | } |
| 346 | |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 347 | if (c.usesGLFBO0()) { |
| 348 | // If we are making the surface we will never use FBO0. |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
Robert Phillips | c046ff0 | 2019-07-01 10:34:03 -0400 | [diff] [blame] | 352 | if (c.vulkanSecondaryCBCompatible()) { |
| 353 | return nullptr; |
| 354 | } |
| 355 | |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 356 | GrColorType grColorType = SkColorTypeToGrColorType(c.colorType()); |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 357 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 358 | auto rtc = context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, |
| 359 | c.width(), |
| 360 | c.height(), |
| 361 | grColorType, |
| 362 | c.refColorSpace(), |
| 363 | c.sampleCount(), |
| 364 | GrMipMapped(c.isMipMapped()), |
| 365 | c.origin(), |
| 366 | &c.surfaceProps(), |
| 367 | budgeted, |
| 368 | c.isProtected()); |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 369 | if (!rtc) { |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 370 | return nullptr; |
| 371 | } |
| 372 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 373 | // CONTEXT TODO: remove this use of 'backdoor' to create an SkGpuDevice |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 374 | sk_sp<SkGpuDevice> device(SkGpuDevice::Make(context->priv().backdoor(), std::move(rtc), |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 375 | SkGpuDevice::kClear_InitContents)); |
| 376 | if (!device) { |
| 377 | return nullptr; |
| 378 | } |
| 379 | |
Robert Phillips | 9907e6e | 2019-06-25 14:47:04 -0400 | [diff] [blame] | 380 | sk_sp<SkSurface> result = sk_make_sp<SkSurface_Gpu>(std::move(device)); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 381 | #ifdef SK_DEBUG |
Robert Phillips | 9907e6e | 2019-06-25 14:47:04 -0400 | [diff] [blame] | 382 | if (result) { |
| 383 | SkASSERT(result->isCompatible(c)); |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 384 | } |
| 385 | #endif |
| 386 | |
Robert Phillips | 9907e6e | 2019-06-25 14:47:04 -0400 | [diff] [blame] | 387 | return result; |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 390 | static bool validate_backend_texture(const GrCaps* caps, const GrBackendTexture& tex, |
| 391 | int sampleCnt, GrColorType grCT, |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 392 | bool texturable) { |
| 393 | if (!tex.isValid()) { |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | GrBackendFormat backendFormat = tex.getBackendFormat(); |
| 398 | if (!backendFormat.isValid()) { |
| 399 | return false; |
| 400 | } |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 401 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 402 | if (!caps->areColorTypeAndFormatCompatible(grCT, backendFormat)) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 406 | if (!caps->isFormatAsColorTypeRenderable(grCT, backendFormat, sampleCnt)) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 410 | if (texturable && !caps->isFormatTexturable(backendFormat)) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 411 | return false; |
| 412 | } |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 413 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 414 | return true; |
| 415 | } |
| 416 | |
| 417 | sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext* context, |
| 418 | const SkSurfaceCharacterization& c, |
| 419 | const GrBackendTexture& backendTexture, |
| 420 | TextureReleaseProc textureReleaseProc, |
| 421 | ReleaseContext releaseContext) { |
| 422 | if (!context || !c.isValid()) { |
| 423 | return nullptr; |
| 424 | } |
| 425 | |
| 426 | if (c.usesGLFBO0()) { |
| 427 | // If we are making the surface we will never use FBO0. |
| 428 | return nullptr; |
| 429 | } |
| 430 | |
| 431 | if (!c.isCompatible(backendTexture)) { |
| 432 | return nullptr; |
| 433 | } |
| 434 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 435 | GrColorType grCT = SkColorTypeAndFormatToGrColorType(context->priv().caps(), c.colorType(), |
| 436 | backendTexture.getBackendFormat()); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 437 | if (grCT == GrColorType::kUnknown) { |
| 438 | return nullptr; |
| 439 | } |
Greg Daniel | 5c96db8 | 2019-07-09 14:06:58 -0400 | [diff] [blame] | 440 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 441 | if (!validate_backend_texture(context->priv().caps(), backendTexture, |
Greg Daniel | 5c96db8 | 2019-07-09 14:06:58 -0400 | [diff] [blame] | 442 | c.sampleCount(), grCT, true)) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 443 | return nullptr; |
| 444 | } |
| 445 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 446 | auto rtc = context->priv().makeBackendTextureRenderTargetContext( |
| 447 | backendTexture, c.origin(), c.sampleCount(), grCT, c.refColorSpace(), &c.surfaceProps(), |
| 448 | textureReleaseProc, releaseContext); |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 449 | if (!rtc) { |
| 450 | return nullptr; |
| 451 | } |
| 452 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 453 | auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents); |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 454 | if (!device) { |
| 455 | return nullptr; |
| 456 | } |
| 457 | |
| 458 | sk_sp<SkSurface> result = sk_make_sp<SkSurface_Gpu>(std::move(device)); |
| 459 | #ifdef SK_DEBUG |
| 460 | if (result) { |
| 461 | SkASSERT(result->isCompatible(c)); |
| 462 | } |
| 463 | #endif |
| 464 | |
| 465 | return result; |
| 466 | } |
| 467 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 468 | sk_sp<SkSurface> SkSurface::MakeRenderTarget(GrContext* ctx, SkBudgeted budgeted, |
| 469 | const SkImageInfo& info, int sampleCount, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 470 | GrSurfaceOrigin origin, const SkSurfaceProps* props, |
| 471 | bool shouldCreateWithMips) { |
Greg Daniel | f47069c | 2017-10-24 11:57:01 -0400 | [diff] [blame] | 472 | if (!ctx) { |
| 473 | return nullptr; |
| 474 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 475 | sampleCount = SkTMax(1, sampleCount); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 476 | GrMipMapped mipMapped = shouldCreateWithMips ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 477 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 478 | if (!ctx->priv().caps()->mipMapSupport()) { |
Greg Daniel | f47069c | 2017-10-24 11:57:01 -0400 | [diff] [blame] | 479 | mipMapped = GrMipMapped::kNo; |
| 480 | } |
| 481 | |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 482 | sk_sp<SkGpuDevice> device(SkGpuDevice::Make( |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 483 | ctx, budgeted, info, sampleCount, origin, props, mipMapped, |
| 484 | SkGpuDevice::kClear_InitContents)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 485 | if (!device) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 486 | return nullptr; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 487 | } |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 488 | return sk_make_sp<SkSurface_Gpu>(std::move(device)); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 489 | } |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 490 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 491 | sk_sp<SkSurface> SkSurface_Gpu::MakeWrappedRenderTarget( |
| 492 | GrContext* context, std::unique_ptr<GrRenderTargetContext> rtc) { |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 493 | if (!context) { |
| 494 | return nullptr; |
| 495 | } |
| 496 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 497 | auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents); |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 498 | if (!device) { |
| 499 | return nullptr; |
| 500 | } |
| 501 | |
| 502 | return sk_make_sp<SkSurface_Gpu>(std::move(device)); |
| 503 | } |
| 504 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 505 | sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext* context, const GrBackendTexture& tex, |
| 506 | GrSurfaceOrigin origin, int sampleCnt, |
| 507 | SkColorType colorType, |
| 508 | sk_sp<SkColorSpace> colorSpace, |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 509 | const SkSurfaceProps* props, |
| 510 | SkSurface::TextureReleaseProc textureReleaseProc, |
| 511 | SkSurface::ReleaseContext releaseContext) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 512 | if (!context) { |
| 513 | return nullptr; |
| 514 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 515 | sampleCnt = SkTMax(1, sampleCnt); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 516 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 517 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(context->priv().caps(), colorType, |
| 518 | tex.getBackendFormat()); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 519 | if (grColorType == GrColorType::kUnknown) { |
| 520 | return nullptr; |
| 521 | } |
| 522 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 523 | if (!validate_backend_texture(context->priv().caps(), tex, sampleCnt, grColorType, true)) { |
Brian Salomon | 5370677 | 2018-03-19 14:18:08 -0400 | [diff] [blame] | 524 | return nullptr; |
| 525 | } |
Brian Salomon | 5370677 | 2018-03-19 14:18:08 -0400 | [diff] [blame] | 526 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 527 | auto rtc = context->priv().makeBackendTextureRenderTargetContext( |
| 528 | tex, origin, sampleCnt, grColorType, std::move(colorSpace), props, textureReleaseProc, |
| 529 | releaseContext); |
Brian Salomon | 5370677 | 2018-03-19 14:18:08 -0400 | [diff] [blame] | 530 | if (!rtc) { |
| 531 | return nullptr; |
| 532 | } |
| 533 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 534 | auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents); |
Brian Salomon | 5370677 | 2018-03-19 14:18:08 -0400 | [diff] [blame] | 535 | if (!device) { |
| 536 | return nullptr; |
| 537 | } |
| 538 | return sk_make_sp<SkSurface_Gpu>(std::move(device)); |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 539 | } |
| 540 | |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 541 | bool SkSurface_Gpu::onReplaceBackendTexture(const GrBackendTexture& backendTexture, |
| 542 | GrSurfaceOrigin origin, TextureReleaseProc releaseProc, |
| 543 | ReleaseContext releaseContext) { |
| 544 | auto context = this->fDevice->context(); |
| 545 | if (context->abandoned()) { |
| 546 | return false; |
| 547 | } |
| 548 | if (!backendTexture.isValid()) { |
| 549 | return false; |
| 550 | } |
| 551 | if (backendTexture.width() != this->width() || backendTexture.height() != this->height()) { |
| 552 | return false; |
| 553 | } |
| 554 | auto* oldRTC = fDevice->accessRenderTargetContext(); |
| 555 | auto oldProxy = sk_ref_sp(oldRTC->asTextureProxy()); |
| 556 | if (!oldProxy) { |
| 557 | return false; |
| 558 | } |
| 559 | auto* oldTexture = oldProxy->peekTexture(); |
| 560 | if (!oldTexture) { |
| 561 | return false; |
| 562 | } |
| 563 | if (!oldTexture->resourcePriv().refsWrappedObjects()) { |
| 564 | return false; |
| 565 | } |
| 566 | if (oldTexture->backendFormat() != backendTexture.getBackendFormat()) { |
| 567 | return false; |
| 568 | } |
| 569 | if (oldTexture->getBackendTexture().isSameTexture(backendTexture)) { |
| 570 | return false; |
| 571 | } |
| 572 | SkASSERT(oldTexture->asRenderTarget()); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 573 | int sampleCnt = oldTexture->asRenderTarget()->numSamples(); |
Greg Daniel | 5c96db8 | 2019-07-09 14:06:58 -0400 | [diff] [blame] | 574 | GrColorType grColorType = SkColorTypeToGrColorType(this->getCanvas()->imageInfo().colorType()); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 575 | auto colorSpace = sk_ref_sp(oldRTC->colorInfo().colorSpace()); |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 576 | if (!validate_backend_texture(context->priv().caps(), backendTexture, |
| 577 | sampleCnt, grColorType, true)) { |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 578 | return false; |
| 579 | } |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 580 | auto rtc = |
| 581 | context->priv().makeBackendTextureRenderTargetContext(backendTexture, |
| 582 | origin, |
| 583 | sampleCnt, |
| 584 | oldRTC->colorInfo().colorType(), |
| 585 | std::move(colorSpace), |
| 586 | &this->props(), |
| 587 | releaseProc, |
| 588 | releaseContext); |
Brian Salomon | aad8315 | 2019-05-24 10:16:35 -0400 | [diff] [blame] | 589 | if (!rtc) { |
| 590 | return false; |
| 591 | } |
| 592 | fDevice->replaceRenderTargetContext(std::move(rtc), true); |
| 593 | return true; |
| 594 | } |
| 595 | |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 596 | bool validate_backend_render_target(const GrCaps* caps, const GrBackendRenderTarget& rt, |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 597 | GrColorType grCT) { |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 598 | if (!caps->areColorTypeAndFormatCompatible(grCT, rt.getBackendFormat())) { |
| 599 | return false; |
| 600 | } |
| 601 | |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 602 | if (!caps->isFormatAsColorTypeRenderable(grCT, rt.getBackendFormat(), rt.sampleCnt())) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 603 | return false; |
| 604 | } |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 605 | return true; |
| 606 | } |
| 607 | |
| 608 | sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext* context, |
| 609 | const GrBackendRenderTarget& rt, |
| 610 | GrSurfaceOrigin origin, |
| 611 | SkColorType colorType, |
| 612 | sk_sp<SkColorSpace> colorSpace, |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 613 | const SkSurfaceProps* props, |
| 614 | SkSurface::RenderTargetReleaseProc relProc, |
| 615 | SkSurface::ReleaseContext releaseContext) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 616 | if (!context) { |
| 617 | return nullptr; |
| 618 | } |
Brian Salomon | 49edccd | 2018-03-23 15:31:32 -0400 | [diff] [blame] | 619 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 620 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(context->priv().caps(), colorType, |
| 621 | rt.getBackendFormat()); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 622 | if (grColorType == GrColorType::kUnknown) { |
| 623 | return nullptr; |
| 624 | } |
| 625 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 626 | if (!validate_backend_render_target(context->priv().caps(), rt, grColorType)) { |
Brian Salomon | 49edccd | 2018-03-23 15:31:32 -0400 | [diff] [blame] | 627 | return nullptr; |
| 628 | } |
| 629 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 630 | auto rtc = context->priv().makeBackendRenderTargetRenderTargetContext( |
| 631 | rt, origin, grColorType, std::move(colorSpace), props, relProc, releaseContext); |
Brian Salomon | 49edccd | 2018-03-23 15:31:32 -0400 | [diff] [blame] | 632 | if (!rtc) { |
| 633 | return nullptr; |
| 634 | } |
| 635 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 636 | auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents); |
Brian Salomon | 49edccd | 2018-03-23 15:31:32 -0400 | [diff] [blame] | 637 | if (!device) { |
| 638 | return nullptr; |
| 639 | } |
| 640 | |
| 641 | return sk_make_sp<SkSurface_Gpu>(std::move(device)); |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 642 | } |
| 643 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 644 | sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext* context, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 645 | const GrBackendTexture& tex, |
| 646 | GrSurfaceOrigin origin, |
| 647 | int sampleCnt, |
Brian Salomon | cb88470 | 2018-03-19 14:01:21 -0400 | [diff] [blame] | 648 | SkColorType colorType, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 649 | sk_sp<SkColorSpace> colorSpace, |
| 650 | const SkSurfaceProps* props) { |
| 651 | if (!context) { |
| 652 | return nullptr; |
| 653 | } |
Justin Novosad | 21a99d4 | 2018-07-17 16:53:43 -0400 | [diff] [blame] | 654 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 655 | sampleCnt = SkTMax(1, sampleCnt); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 656 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(context->priv().caps(), colorType, |
| 657 | tex.getBackendFormat()); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 658 | if (grColorType == GrColorType::kUnknown) { |
| 659 | return nullptr; |
| 660 | } |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 661 | if (!validate_backend_texture(context->priv().caps(), tex, sampleCnt, grColorType, false)) { |
Justin Novosad | 21a99d4 | 2018-07-17 16:53:43 -0400 | [diff] [blame] | 662 | return nullptr; |
| 663 | } |
| 664 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 665 | auto rtc = context->priv().makeBackendTextureAsRenderTargetRenderTargetContext( |
| 666 | tex, origin, sampleCnt, grColorType, std::move(colorSpace), props); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 667 | if (!rtc) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 668 | return nullptr; |
| 669 | } |
robertphillips | 15c42ca | 2016-08-04 08:45:02 -0700 | [diff] [blame] | 670 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 671 | auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 672 | if (!device) { |
| 673 | return nullptr; |
| 674 | } |
robertphillips | 24e9128 | 2016-04-29 06:46:36 -0700 | [diff] [blame] | 675 | return sk_make_sp<SkSurface_Gpu>(std::move(device)); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 678 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
| 679 | sk_sp<SkSurface> SkSurface::MakeFromAHardwareBuffer(GrContext* context, |
| 680 | AHardwareBuffer* hardwareBuffer, |
| 681 | GrSurfaceOrigin origin, |
| 682 | sk_sp<SkColorSpace> colorSpace, |
| 683 | const SkSurfaceProps* surfaceProps) { |
| 684 | AHardwareBuffer_Desc bufferDesc; |
| 685 | AHardwareBuffer_describe(hardwareBuffer, &bufferDesc); |
| 686 | |
| 687 | if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT)) { |
| 688 | return nullptr; |
| 689 | } |
| 690 | |
| 691 | bool isTextureable = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE); |
| 692 | bool isProtectedContent = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
| 693 | |
| 694 | // We currently don't support protected content |
| 695 | if (isProtectedContent) { |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 696 | SkDebugf("We currently don't support protected content on android\n"); |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 697 | return nullptr; |
| 698 | } |
| 699 | |
| 700 | GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(context, |
| 701 | hardwareBuffer, |
| 702 | bufferDesc.format, |
| 703 | true); |
| 704 | if (!backendFormat.isValid()) { |
| 705 | return nullptr; |
| 706 | } |
| 707 | |
| 708 | if (isTextureable) { |
| 709 | GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
Stan Iliev | a56b04a | 2019-08-01 14:22:34 -0400 | [diff] [blame] | 710 | GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr; |
| 711 | GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr; |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 712 | |
| 713 | GrBackendTexture backendTexture = |
| 714 | GrAHardwareBufferUtils::MakeBackendTexture(context, hardwareBuffer, |
| 715 | bufferDesc.width, bufferDesc.height, |
Stan Iliev | a56b04a | 2019-08-01 14:22:34 -0400 | [diff] [blame] | 716 | &deleteImageProc, &updateImageProc, |
| 717 | &deleteImageCtx, isProtectedContent, |
| 718 | backendFormat, true); |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 719 | if (!backendTexture.isValid()) { |
| 720 | return nullptr; |
| 721 | } |
| 722 | |
| 723 | SkColorType colorType = |
| 724 | GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format); |
| 725 | |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 726 | sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, backendTexture, |
| 727 | origin, 0, colorType, std::move(colorSpace), surfaceProps, deleteImageProc, |
| 728 | deleteImageCtx); |
| 729 | |
| 730 | if (!surface) { |
| 731 | SkASSERT(deleteImageProc); |
| 732 | deleteImageProc(deleteImageCtx); |
| 733 | } |
| 734 | return surface; |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 735 | } else { |
| 736 | return nullptr; |
| 737 | } |
| 738 | } |
| 739 | #endif |
| 740 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 741 | #endif |