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 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 8 | #include <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <type_traits> |
| 11 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 12 | #include "SkAutoPixmapStorage.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 13 | #include "GrBackendSurface.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 14 | #include "GrBackendTextureImageGenerator.h" |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 15 | #include "GrAHardwareBufferImageGenerator.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 16 | #include "GrBitmapTextureMaker.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 17 | #include "GrCaps.h" |
Brian Osman | 47c2751 | 2018-06-18 10:58:51 -0400 | [diff] [blame] | 18 | #include "GrColorSpaceXform.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 19 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 20 | #include "GrContextPriv.h" |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 21 | #include "GrGpu.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 22 | #include "GrImageTextureMaker.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 23 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 24 | #include "GrRenderTargetContext.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 25 | #include "GrResourceProvider.h" |
Brian Osman | fe3b516 | 2017-03-02 15:09:20 -0500 | [diff] [blame] | 26 | #include "GrSemaphore.h" |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 27 | #include "GrSurfacePriv.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 28 | #include "GrTextureAdjuster.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 29 | #include "GrTexture.h" |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 30 | #include "GrTexturePriv.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 31 | #include "GrTextureProxy.h" |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 32 | #include "GrTextureProxyPriv.h" |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 33 | #include "gl/GrGLDefines.h" |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 34 | #include "effects/GrYUVtoRGBEffect.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 35 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 36 | #include "SkBitmapCache.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 37 | #include "SkGr.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 38 | #include "SkImage_Gpu.h" |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 39 | #include "SkImageCacherator.h" |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 40 | #include "SkImageInfoPriv.h" |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 41 | #include "SkMipMap.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 42 | #include "SkPixelRef.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 43 | #include "SkReadPixelsRec.h" |
Greg Daniel | bc54fad | 2018-02-09 16:40:32 -0500 | [diff] [blame] | 44 | #include "SkTraceEvent.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 45 | |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 46 | SkImage_Gpu::SkImage_Gpu(sk_sp<GrContext> context, uint32_t uniqueID, SkAlphaType at, |
| 47 | sk_sp<GrTextureProxy> proxy, sk_sp<SkColorSpace> colorSpace, |
| 48 | SkBudgeted budgeted) |
| 49 | : INHERITED(proxy->worstCaseWidth(), proxy->worstCaseHeight(), uniqueID) |
| 50 | , fContext(std::move(context)) |
| 51 | , fProxy(std::move(proxy)) |
| 52 | , fAlphaType(at) |
| 53 | , fBudgeted(budgeted) |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame^] | 54 | , fColorSpace(std::move(colorSpace)) {} |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 55 | |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame^] | 56 | SkImage_Gpu::~SkImage_Gpu() {} |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 57 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 58 | SkImageInfo SkImage_Gpu::onImageInfo() const { |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 59 | return SkImageInfo::Make(fProxy->width(), fProxy->height(), this->onColorType(), fAlphaType, |
| 60 | fColorSpace); |
| 61 | } |
| 62 | |
| 63 | SkColorType SkImage_Gpu::onColorType() const { |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 64 | SkColorType ct; |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 65 | if (!GrPixelConfigToColorType(fProxy->config(), &ct)) { |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 66 | ct = kUnknown_SkColorType; |
| 67 | } |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 68 | return ct; |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 71 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace*, CachingHint chint) const { |
Robert Phillips | a6aef2a | 2018-04-11 10:41:20 -0400 | [diff] [blame] | 72 | if (!fContext->contextPriv().resourceProvider()) { |
| 73 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 74 | return false; |
| 75 | } |
| 76 | |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 77 | // The SkColorSpace parameter "dstColorSpace" is really just a hint about how/where the bitmap |
| 78 | // will be used. The client doesn't expect that we convert to that color space, it's intended |
| 79 | // for codec-backed images, to drive our decoding heuristic. In theory we *could* read directly |
| 80 | // into that color space (to save the client some effort in whatever they're about to do), but |
| 81 | // that would make our use of the bitmap cache incorrect (or much less efficient, assuming we |
| 82 | // rolled the dstColorSpace into the key). |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 83 | const auto desc = SkBitmapCacheDesc::Make(this); |
| 84 | if (SkBitmapCache::Find(desc, dst)) { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 85 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 86 | SkASSERT(dst->isImmutable()); |
| 87 | SkASSERT(dst->getPixels()); |
| 88 | return true; |
| 89 | } |
| 90 | |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 91 | SkBitmapCache::RecPtr rec = nullptr; |
| 92 | SkPixmap pmap; |
| 93 | if (kAllow_CachingHint == chint) { |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 94 | rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap); |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 95 | if (!rec) { |
| 96 | return false; |
| 97 | } |
| 98 | } else { |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 99 | if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) { |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 100 | return false; |
| 101 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 102 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 103 | |
Brian Salomon | 2084ffa | 2017-03-27 17:32:18 -0400 | [diff] [blame] | 104 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
| 105 | fProxy, |
| 106 | fColorSpace); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 107 | if (!sContext) { |
| 108 | return false; |
| 109 | } |
| 110 | |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 111 | if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 112 | return false; |
| 113 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 114 | |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 115 | if (rec) { |
| 116 | SkBitmapCache::Add(std::move(rec), dst); |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame^] | 117 | this->notifyAddedToRasterCache(); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 118 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 122 | sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef(GrContext* context, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 123 | const GrSamplerState& params, |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 124 | SkColorSpace* dstColorSpace, |
| 125 | sk_sp<SkColorSpace>* texColorSpace, |
| 126 | SkScalar scaleAdjust[2]) const { |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 127 | if (context != fContext.get()) { |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 128 | SkASSERT(0); |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 132 | GrTextureAdjuster adjuster(fContext.get(), fProxy, this->alphaType(), this->uniqueID(), |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 133 | this->fColorSpace.get()); |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 134 | return adjuster.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 135 | } |
| 136 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 137 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 138 | switch (info.colorType()) { |
| 139 | case kRGBA_8888_SkColorType: |
| 140 | case kBGRA_8888_SkColorType: |
| 141 | break; |
| 142 | default: |
| 143 | return; // nothing to do |
| 144 | } |
| 145 | |
| 146 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 147 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 148 | // SkPreMultiplyColor() |
| 149 | // |
| 150 | SkColor* row = (SkColor*)pixels; |
| 151 | for (int y = 0; y < info.height(); ++y) { |
| 152 | for (int x = 0; x < info.width(); ++x) { |
| 153 | row[x] = SkPreMultiplyColor(row[x]); |
| 154 | } |
Brian Salomon | 9708af8 | 2018-02-05 12:57:10 -0500 | [diff] [blame] | 155 | row = (SkColor*)((char*)(row) + rowBytes); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 159 | GrBackendTexture SkImage_Gpu::onGetBackendTexture(bool flushPendingGrContextIO, |
| 160 | GrSurfaceOrigin* origin) const { |
| 161 | SkASSERT(fProxy); |
| 162 | |
Robert Phillips | a6aef2a | 2018-04-11 10:41:20 -0400 | [diff] [blame] | 163 | if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) { |
| 164 | // This image was created with a DDL context and cannot be instantiated. |
| 165 | return GrBackendTexture(); |
| 166 | } |
| 167 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 168 | if (!fProxy->instantiate(fContext->contextPriv().resourceProvider())) { |
| 169 | return GrBackendTexture(); // invalid |
| 170 | } |
| 171 | |
| 172 | GrTexture* texture = fProxy->priv().peekTexture(); |
| 173 | |
| 174 | if (texture) { |
| 175 | if (flushPendingGrContextIO) { |
| 176 | fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get()); |
| 177 | } |
| 178 | if (origin) { |
| 179 | *origin = fProxy->origin(); |
| 180 | } |
| 181 | return texture->getBackendTexture(); |
| 182 | } |
| 183 | return GrBackendTexture(); // invalid |
| 184 | } |
| 185 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 186 | GrTexture* SkImage_Gpu::onGetTexture() const { |
| 187 | GrTextureProxy* proxy = this->peekProxy(); |
| 188 | if (!proxy) { |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
Robert Phillips | a6aef2a | 2018-04-11 10:41:20 -0400 | [diff] [blame] | 192 | if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) { |
| 193 | // This image was created with a DDL context and cannot be instantiated. |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 197 | if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | return proxy->priv().peekTexture(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 204 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 205 | int srcX, int srcY, CachingHint) const { |
Robert Phillips | a6aef2a | 2018-04-11 10:41:20 -0400 | [diff] [blame] | 206 | if (!fContext->contextPriv().resourceProvider()) { |
| 207 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 208 | return false; |
| 209 | } |
| 210 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 211 | if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 215 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 216 | if (!rec.trim(this->width(), this->height())) { |
| 217 | return false; |
| 218 | } |
| 219 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 220 | // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and |
| 221 | // GrRenderTargetContext::onReadPixels |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 222 | uint32_t flags = 0; |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 223 | if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 224 | // let the GPU perform this transformation for us |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 225 | flags = GrContextPriv::kUnpremul_PixelOpsFlag; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 226 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 227 | |
Brian Salomon | 2084ffa | 2017-03-27 17:32:18 -0400 | [diff] [blame] | 228 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 229 | fProxy, fColorSpace); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 230 | if (!sContext) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 233 | |
| 234 | if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) { |
| 235 | return false; |
| 236 | } |
| 237 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 238 | // do we have to manually fix-up the alpha channel? |
| 239 | // src dst |
| 240 | // unpremul premul fix manually |
| 241 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 242 | // all other combos need to change. |
| 243 | // |
| 244 | // Should this be handled by Ganesh? todo:? |
| 245 | // |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 246 | if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 247 | apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 252 | sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const { |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 253 | GrSurfaceDesc desc; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 254 | desc.fWidth = subset.width(); |
| 255 | desc.fHeight = subset.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 256 | desc.fConfig = fProxy->config(); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 257 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 258 | sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext( |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 259 | desc, fProxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact, fBudgeted)); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 260 | if (!sContext) { |
| 261 | return nullptr; |
| 262 | } |
| 263 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 264 | if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 265 | return nullptr; |
| 266 | } |
| 267 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 268 | // MDB: this call is okay bc we know 'sContext' was kExact |
| 269 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, |
| 270 | fAlphaType, sContext->asTextureProxyRef(), |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 271 | fColorSpace, fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 272 | } |
| 273 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 274 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 275 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 276 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, |
| 277 | const GrBackendTexture& backendTex, |
| 278 | GrSurfaceOrigin origin, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 279 | SkAlphaType at, sk_sp<SkColorSpace> colorSpace, |
| 280 | GrWrapOwnership ownership, |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 281 | SkImage::TextureReleaseProc releaseProc, |
| 282 | SkImage::ReleaseContext releaseCtx) { |
Greg Daniel | 66aebf3 | 2018-04-09 09:15:56 -0400 | [diff] [blame] | 283 | if (!backendTex.isValid() || backendTex.width() <= 0 || backendTex.height() <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 284 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 285 | } |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 286 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 287 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 288 | sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(backendTex, origin, ownership, |
| 289 | releaseProc, releaseCtx); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 290 | if (!proxy) { |
Robert Phillips | e201ebc | 2018-01-17 18:12:50 +0000 | [diff] [blame] | 291 | return nullptr; |
| 292 | } |
Brian Salomon | 9457546 | 2018-06-21 17:00:26 -0400 | [diff] [blame] | 293 | #if 1 |
| 294 | // Temporary fix for crbug.com/850617 that can be cleanly merged back to older branches. Assume |
| 295 | // any MIP levels on the incoming texture are dirty. The proper fix is to make them clean on |
| 296 | // export. See #if 0'ed out code in GrDrawingManager::prepareSurfaceForExternalIO(). |
| 297 | SkASSERT(proxy->priv().isInstantiated()); |
| 298 | if (auto* tex = proxy->priv().peekTexture()) { |
| 299 | if (tex->texturePriv().mipMapped() == GrMipMapped::kYes) { |
| 300 | proxy->priv().peekTexture()->texturePriv().markMipMapsDirty(); |
| 301 | } |
| 302 | } |
| 303 | #endif |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 304 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, at, std::move(proxy), |
| 305 | std::move(colorSpace), SkBudgeted::kNo); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 308 | bool validate_backend_texture(GrContext* ctx, const GrBackendTexture& tex, GrPixelConfig* config, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 309 | SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) { |
Greg Daniel | 66aebf3 | 2018-04-09 09:15:56 -0400 | [diff] [blame] | 310 | if (!tex.isValid()) { |
| 311 | return false; |
| 312 | } |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 313 | // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to |
| 314 | // create a fake image info here. |
| 315 | SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs); |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 316 | if (!SkImageInfoIsValid(info)) { |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 320 | return ctx->contextPriv().caps()->validateBackendTexture(tex, ct, config); |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, |
| 324 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 325 | SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 326 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 327 | if (!ctx) { |
| 328 | return nullptr; |
| 329 | } |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 330 | GrBackendTexture texCopy = tex; |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 331 | if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) { |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 332 | return nullptr; |
| 333 | } |
Brian Salomon | bfd2749 | 2018-03-19 14:08:51 -0400 | [diff] [blame] | 334 | return new_wrapped_texture_common(ctx, texCopy, origin, at, std::move(cs), |
| 335 | kBorrow_GrWrapOwnership, releaseP, releaseC); |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 338 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, |
| 339 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
Brian Salomon | 42409c2 | 2018-03-20 13:48:41 -0400 | [diff] [blame] | 340 | SkColorType ct, SkAlphaType at, |
| 341 | sk_sp<SkColorSpace> cs) { |
| 342 | if (!ctx || !ctx->contextPriv().resourceProvider()) { |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 343 | // We have a DDL context and we don't support adopted textures for them. |
| 344 | return nullptr; |
| 345 | } |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 346 | GrBackendTexture texCopy = tex; |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 347 | if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) { |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 348 | return nullptr; |
| 349 | } |
Brian Salomon | 42409c2 | 2018-03-20 13:48:41 -0400 | [diff] [blame] | 350 | return new_wrapped_texture_common(ctx, texCopy, origin, at, std::move(cs), |
| 351 | kAdopt_GrWrapOwnership, nullptr, nullptr); |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 354 | sk_sp<SkImage> SkImage_Gpu::MakeFromYUVATexturesCopyImpl(GrContext* ctx, |
| 355 | SkYUVColorSpace colorSpace, |
| 356 | const GrBackendTexture yuvaTextures[], |
| 357 | SkYUVAIndex yuvaIndices[4], |
| 358 | SkISize size, |
| 359 | GrSurfaceOrigin origin, |
| 360 | sk_sp<SkColorSpace> imageColorSpace) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 361 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 362 | |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 363 | // Right now this still only deals with YUV and NV12 formats. Assuming that YUV has different |
| 364 | // textures for U and V planes, while NV12 uses same texture for U and V planes. |
| 365 | bool nv12 = (yuvaIndices[1].fIndex == yuvaIndices[2].fIndex); |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 366 | auto ct = nv12 ? kRGBA_8888_SkColorType : kAlpha_8_SkColorType; |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 367 | |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 368 | // We need to make a copy of the input backend textures because we need to preserve the result |
| 369 | // of validate_backend_texture. |
| 370 | GrBackendTexture yuvaTexturesCopy[4]; |
| 371 | |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 372 | for (int i = 0; i < 4; ++i) { |
| 373 | // Validate that the yuvaIndices refer to valid backend textures. |
| 374 | SkYUVAIndex& yuvaIndex = yuvaIndices[i]; |
| 375 | if (i == 3 && yuvaIndex.fIndex == -1) { |
| 376 | // Meaning the A plane isn't passed in. |
| 377 | continue; |
| 378 | } |
| 379 | if (yuvaIndex.fIndex == -1 || yuvaIndex.fIndex > 3) { |
| 380 | // Y plane, U plane, and V plane must refer to image sources being passed in. There are |
| 381 | // at most 4 images sources being passed in, could not have a index more than 3. |
Brian Salomon | 3afdab1 | 2018-03-19 16:29:37 -0400 | [diff] [blame] | 382 | return nullptr; |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 383 | } |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 384 | if (!yuvaTexturesCopy[yuvaIndex.fIndex].isValid()) { |
| 385 | yuvaTexturesCopy[yuvaIndex.fIndex] = yuvaTextures[yuvaIndex.fIndex]; |
| 386 | // TODO: Instead of using assumption about whether it is NV12 format to guess colorType, |
| 387 | // actually use channel information here. |
| 388 | if (!validate_backend_texture(ctx, yuvaTexturesCopy[i], &yuvaTexturesCopy[i].fConfig, |
| 389 | ct, kPremul_SkAlphaType, nullptr)) { |
| 390 | return nullptr; |
| 391 | } |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 392 | } |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 393 | |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 394 | // TODO: Check that for each plane, the channel actually exist in the image source we are |
| 395 | // reading from. |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 396 | } |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 397 | |
| 398 | sk_sp<GrTextureProxy> tempTextureProxies[4]; |
| 399 | for (int i = 0; i < 4; ++i) { |
| 400 | // Fill in tempTextureProxies to avoid duplicate texture proxies. |
| 401 | int textureIndex = yuvaIndices[i].fIndex; |
| 402 | |
| 403 | // Safely ignore since this means we are missing the A plane. |
| 404 | if (textureIndex == -1) { |
| 405 | SkASSERT(3 == i); |
| 406 | continue; |
| 407 | } |
| 408 | |
| 409 | if (!tempTextureProxies[textureIndex]) { |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 410 | SkASSERT(yuvaTexturesCopy[textureIndex].isValid()); |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 411 | tempTextureProxies[textureIndex] = |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 412 | proxyProvider->wrapBackendTexture(yuvaTexturesCopy[textureIndex], origin); |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | sk_sp<GrTextureProxy> yProxy = tempTextureProxies[yuvaIndices[0].fIndex]; |
| 416 | sk_sp<GrTextureProxy> uProxy = tempTextureProxies[yuvaIndices[1].fIndex]; |
| 417 | sk_sp<GrTextureProxy> vProxy = tempTextureProxies[yuvaIndices[2].fIndex]; |
| 418 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 419 | if (!yProxy || !uProxy || !vProxy) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 420 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 423 | const int width = size.width(); |
| 424 | const int height = size.height(); |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 425 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 426 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 427 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 428 | ctx->contextPriv().makeDeferredRenderTargetContext( |
| 429 | SkBackingFit::kExact, width, height, kRGBA_8888_GrPixelConfig, |
| 430 | std::move(imageColorSpace), 1, GrMipMapped::kNo, origin)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 431 | if (!renderTargetContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 432 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 436 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 437 | // TODO: Modify the fragment processor to sample from different channel instead of taking nv12 |
| 438 | // bool. |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 439 | paint.addColorFragmentProcessor( |
Weiliang Chen | 3e95e57 | 2018-05-30 15:15:23 -0400 | [diff] [blame] | 440 | GrYUVtoRGBEffect::Make(yProxy, uProxy, vProxy, colorSpace, nv12)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 441 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 442 | const SkRect rect = SkRect::MakeIWH(width, height); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 443 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 444 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 445 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 446 | if (!renderTargetContext->asSurfaceProxy()) { |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 447 | return nullptr; |
| 448 | } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 449 | ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 450 | |
| 451 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 452 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, kOpaque_SkAlphaType, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 453 | renderTargetContext->asTextureProxyRef(), |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 454 | renderTargetContext->colorSpaceInfo().refColorSpace(), |
| 455 | SkBudgeted::kYes); |
| 456 | } |
| 457 | |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 458 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 459 | const GrBackendTexture yuvTextures[3], |
| 460 | GrSurfaceOrigin origin, |
| 461 | sk_sp<SkColorSpace> imageColorSpace) { |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 462 | // TODO: SkImageSourceChannel input is being ingored right now. Setup correctly in the future. |
| 463 | SkYUVAIndex yuvaIndices[4] = { |
| 464 | SkYUVAIndex{0, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}, |
| 465 | SkYUVAIndex{1, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}, |
| 466 | SkYUVAIndex{2, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}, |
| 467 | SkYUVAIndex{-1, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}}; |
| 468 | SkISize size{yuvTextures[0].width(), yuvTextures[0].height()}; |
| 469 | return SkImage_Gpu::MakeFromYUVATexturesCopyImpl(ctx, colorSpace, yuvTextures, yuvaIndices, |
| 470 | size, origin, std::move(imageColorSpace)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 471 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 472 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 473 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 474 | const GrBackendTexture nv12Textures[2], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 475 | GrSurfaceOrigin origin, |
| 476 | sk_sp<SkColorSpace> imageColorSpace) { |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 477 | // TODO: SkImageSourceChannel input is being ingored right now. Setup correctly in the future. |
| 478 | SkYUVAIndex yuvaIndices[4] = { |
| 479 | SkYUVAIndex{0, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}, |
| 480 | SkYUVAIndex{1, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}, |
| 481 | SkYUVAIndex{1, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}, |
| 482 | SkYUVAIndex{-1, SkImageSourceChannel::kLastEnum_SkImageSourceChannel}}; |
| 483 | SkISize size{nv12Textures[0].width(), nv12Textures[0].height()}; |
| 484 | return SkImage_Gpu::MakeFromYUVATexturesCopyImpl(ctx, colorSpace, nv12Textures, yuvaIndices, |
| 485 | size, origin, std::move(imageColorSpace)); |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 486 | } |
| 487 | |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 488 | static sk_sp<SkImage> create_image_from_producer(GrContext* context, GrTextureProducer* producer, |
| 489 | SkAlphaType at, uint32_t id, |
| 490 | SkColorSpace* dstColorSpace, |
| 491 | GrMipMapped mipMapped) { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 492 | sk_sp<SkColorSpace> texColorSpace; |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 493 | sk_sp<GrTextureProxy> proxy(producer->refTextureProxy(mipMapped, dstColorSpace, |
| 494 | &texColorSpace)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 495 | if (!proxy) { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 496 | return nullptr; |
| 497 | } |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 498 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), id, at, std::move(proxy), |
| 499 | std::move(texColorSpace), SkBudgeted::kNo); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 502 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace, |
| 503 | GrMipMapped mipMapped) const { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 504 | if (!context) { |
| 505 | return nullptr; |
| 506 | } |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 507 | if (GrContext* incumbent = as_IB(this)->context()) { |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 508 | if (incumbent != context) { |
| 509 | return nullptr; |
| 510 | } |
| 511 | sk_sp<GrTextureProxy> proxy = as_IB(this)->asTextureProxyRef(); |
| 512 | SkASSERT(proxy); |
| 513 | if (GrMipMapped::kNo == mipMapped || proxy->mipMapped() == mipMapped) { |
| 514 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 515 | } |
| 516 | GrTextureAdjuster adjuster(context, std::move(proxy), this->alphaType(), |
| 517 | this->uniqueID(), this->colorSpace()); |
| 518 | return create_image_from_producer(context, &adjuster, this->alphaType(), |
| 519 | this->uniqueID(), dstColorSpace, mipMapped); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 520 | } |
| 521 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 522 | if (this->isLazyGenerated()) { |
| 523 | GrImageTextureMaker maker(context, this, kDisallow_CachingHint); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 524 | return create_image_from_producer(context, &maker, this->alphaType(), |
| 525 | this->uniqueID(), dstColorSpace, mipMapped); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) { |
| 529 | GrBitmapTextureMaker maker(context, *bmp); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 530 | return create_image_from_producer(context, &maker, this->alphaType(), |
| 531 | this->uniqueID(), dstColorSpace, mipMapped); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 532 | } |
| 533 | return nullptr; |
| 534 | } |
| 535 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 536 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 537 | |
| 538 | /** |
| 539 | * This helper holds the normal hard ref for the Release proc as well as a hard ref on the DoneProc. |
| 540 | * Thus when a GrTexture is being released, it will unref both the ReleaseProc and DoneProc. |
| 541 | */ |
| 542 | class PromiseReleaseProcHelper : public GrReleaseProcHelper { |
| 543 | public: |
| 544 | PromiseReleaseProcHelper(SkImage_Gpu::TextureReleaseProc releaseProc, |
| 545 | SkImage_Gpu::TextureContext context, |
| 546 | sk_sp<GrReleaseProcHelper> doneHelper) |
| 547 | : INHERITED(releaseProc, context) |
| 548 | , fDoneProcHelper(std::move(doneHelper)) {} |
| 549 | |
| 550 | void weak_dispose() const override { |
| 551 | // Call the inherited weak_dispose first so that we call the ReleaseProc before the DoneProc |
| 552 | // if we hold the last ref to the DoneProc. |
| 553 | INHERITED::weak_dispose(); |
| 554 | fDoneProcHelper.reset(); |
| 555 | } |
| 556 | |
| 557 | private: |
| 558 | mutable sk_sp<GrReleaseProcHelper> fDoneProcHelper; |
| 559 | |
| 560 | typedef GrReleaseProcHelper INHERITED; |
| 561 | }; |
| 562 | |
| 563 | /** |
| 564 | * This helper class manages the ref counting for the the ReleaseProc and DoneProc for promise |
| 565 | * images. It holds a weak ref on the ReleaseProc (hard refs are owned by GrTextures). The weak ref |
| 566 | * allows us to reuse an outstanding ReleaseProc (because we dropped our GrTexture but the GrTexture |
| 567 | * isn't done on the GPU) without needing to call FulfillProc again. It also holds a hard ref on the |
| 568 | * DoneProc. The idea is that after every flush we may call the ReleaseProc so that the client can |
| 569 | * free up their GPU memory if they want to. The life time of the DoneProc matches that of any |
| 570 | * outstanding ReleaseProc as well as the PromiseImageHelper. Thus we won't call the DoneProc until |
| 571 | * all ReleaseProcs are finished and we are finished with the PromiseImageHelper (i.e. won't call |
| 572 | * FulfillProc again). |
| 573 | */ |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 574 | class PromiseImageHelper { |
| 575 | public: |
| 576 | PromiseImageHelper(SkImage_Gpu::TextureFulfillProc fulFillProc, |
| 577 | SkImage_Gpu::TextureReleaseProc releaseProc, |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 578 | SkImage_Gpu::PromiseDoneProc doneProc, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 579 | SkImage_Gpu::TextureContext context) |
| 580 | : fFulfillProc(fulFillProc) |
| 581 | , fReleaseProc(releaseProc) |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 582 | , fContext(context) |
| 583 | , fDoneHelper(new GrReleaseProcHelper(doneProc, context)) {} |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 584 | |
| 585 | void reset() { |
| 586 | this->resetReleaseHelper(); |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 587 | fDoneHelper.reset(); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 588 | } |
| 589 | |
Greg Daniel | 057627f | 2018-03-14 15:51:58 -0400 | [diff] [blame] | 590 | sk_sp<GrTexture> getTexture(GrResourceProvider* resourceProvider, GrPixelConfig config) { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 591 | // Releases the promise helper if there are no outstanding hard refs. This means that we |
| 592 | // don't have any ReleaseProcs waiting to be called so we will need to do a fulfill. |
| 593 | if (fReleaseHelper && fReleaseHelper->weak_expired()) { |
| 594 | this->resetReleaseHelper(); |
| 595 | } |
| 596 | |
| 597 | sk_sp<GrTexture> tex; |
| 598 | if (!fReleaseHelper) { |
| 599 | fFulfillProc(fContext, &fBackendTex); |
Greg Daniel | 057627f | 2018-03-14 15:51:58 -0400 | [diff] [blame] | 600 | fBackendTex.fConfig = config; |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 601 | if (!fBackendTex.isValid()) { |
| 602 | // Even though the GrBackendTexture is not valid, we must call the release |
| 603 | // proc to keep our contract of always calling Fulfill and Release in pairs. |
| 604 | fReleaseProc(fContext); |
| 605 | return sk_sp<GrTexture>(); |
| 606 | } |
| 607 | |
| 608 | tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership); |
| 609 | if (!tex) { |
| 610 | // Even though the GrBackendTexture is not valid, we must call the release |
| 611 | // proc to keep our contract of always calling Fulfill and Release in pairs. |
| 612 | fReleaseProc(fContext); |
| 613 | return sk_sp<GrTexture>(); |
| 614 | } |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 615 | fReleaseHelper = new PromiseReleaseProcHelper(fReleaseProc, fContext, fDoneHelper); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 616 | // Take a weak ref |
| 617 | fReleaseHelper->weak_ref(); |
| 618 | } else { |
| 619 | SkASSERT(fBackendTex.isValid()); |
| 620 | tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership); |
| 621 | if (!tex) { |
| 622 | // We weren't able to make a texture here, but since we are in this branch |
| 623 | // of the calls (promiseHelper.fReleaseHelper is valid) there is already a |
| 624 | // texture out there which will call the release proc so we don't need to |
| 625 | // call it here. |
| 626 | return sk_sp<GrTexture>(); |
| 627 | } |
| 628 | |
| 629 | SkAssertResult(fReleaseHelper->try_ref()); |
| 630 | } |
| 631 | SkASSERT(tex); |
| 632 | // Pass the hard ref off to the texture |
| 633 | tex->setRelease(sk_sp<GrReleaseProcHelper>(fReleaseHelper)); |
| 634 | return tex; |
| 635 | } |
| 636 | |
| 637 | private: |
| 638 | // Weak unrefs fReleaseHelper and sets it to null |
| 639 | void resetReleaseHelper() { |
| 640 | if (fReleaseHelper) { |
| 641 | fReleaseHelper->weak_unref(); |
| 642 | fReleaseHelper = nullptr; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | SkImage_Gpu::TextureFulfillProc fFulfillProc; |
| 647 | SkImage_Gpu::TextureReleaseProc fReleaseProc; |
| 648 | SkImage_Gpu::TextureContext fContext; |
| 649 | |
| 650 | // We cache the GrBackendTexture so that if we deleted the GrTexture but the the release proc |
| 651 | // has yet not been called (this can happen on Vulkan), then we can create a new texture without |
| 652 | // needing to call the fulfill proc again. |
| 653 | GrBackendTexture fBackendTex; |
| 654 | // The fReleaseHelper is used to track a weak ref on the release proc. This helps us make sure |
| 655 | // we are always pairing fulfill and release proc calls correctly. |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 656 | PromiseReleaseProcHelper* fReleaseHelper = nullptr; |
| 657 | // We don't want to call the fDoneHelper until we are done with the PromiseImageHelper and all |
| 658 | // ReleaseHelpers are finished. Thus we hold a hard ref here and we will pass a hard ref to each |
| 659 | // fReleaseHelper we make. |
| 660 | sk_sp<GrReleaseProcHelper> fDoneHelper; |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 661 | }; |
| 662 | |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 663 | static GrInternalSurfaceFlags get_flags_from_format(const GrBackendFormat& backendFormat) { |
| 664 | if (const GrGLenum* target = backendFormat.getGLTarget()) { |
| 665 | if (GR_GL_TEXTURE_RECTANGLE == *target || GR_GL_TEXTURE_EXTERNAL == *target) { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 666 | return GrInternalSurfaceFlags::kIsGLTextureRectangleOrExternal; |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | return GrInternalSurfaceFlags::kNone; |
| 671 | } |
| 672 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 673 | sk_sp<SkImage> SkImage_Gpu::MakePromiseTexture(GrContext* context, |
| 674 | const GrBackendFormat& backendFormat, |
| 675 | int width, |
| 676 | int height, |
| 677 | GrMipMapped mipMapped, |
| 678 | GrSurfaceOrigin origin, |
| 679 | SkColorType colorType, |
| 680 | SkAlphaType alphaType, |
| 681 | sk_sp<SkColorSpace> colorSpace, |
| 682 | TextureFulfillProc textureFulfillProc, |
| 683 | TextureReleaseProc textureReleaseProc, |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 684 | PromiseDoneProc promiseDoneProc, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 685 | TextureContext textureContext) { |
| 686 | if (!context) { |
| 687 | return nullptr; |
| 688 | } |
| 689 | |
| 690 | if (width <= 0 || height <= 0) { |
| 691 | return nullptr; |
| 692 | } |
| 693 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 694 | if (!textureFulfillProc || !textureReleaseProc || !promiseDoneProc) { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 695 | return nullptr; |
| 696 | } |
| 697 | |
| 698 | SkImageInfo info = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace); |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 699 | if (!SkImageInfoIsValid(info)) { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 700 | return nullptr; |
| 701 | } |
| 702 | GrPixelConfig config = kUnknown_GrPixelConfig; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 703 | if (!context->contextPriv().caps()->getConfigFromBackendFormat(backendFormat, colorType, |
| 704 | &config)) { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 705 | return nullptr; |
| 706 | } |
| 707 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 708 | GrInternalSurfaceFlags formatFlags = get_flags_from_format(backendFormat); |
| 709 | |
| 710 | if (mipMapped == GrMipMapped::kYes && |
| 711 | SkToBool(formatFlags & GrInternalSurfaceFlags::kIsGLTextureRectangleOrExternal)) { |
| 712 | // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as |
| 713 | // well. |
| 714 | return nullptr; |
| 715 | } |
| 716 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 717 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 718 | |
| 719 | GrSurfaceDesc desc; |
| 720 | desc.fWidth = width; |
| 721 | desc.fHeight = height; |
| 722 | desc.fConfig = config; |
| 723 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 724 | PromiseImageHelper promiseHelper(textureFulfillProc, textureReleaseProc, promiseDoneProc, |
| 725 | textureContext); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 726 | |
| 727 | sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy( |
Greg Daniel | 057627f | 2018-03-14 15:51:58 -0400 | [diff] [blame] | 728 | [promiseHelper, config] (GrResourceProvider* resourceProvider) mutable { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 729 | if (!resourceProvider) { |
| 730 | promiseHelper.reset(); |
| 731 | return sk_sp<GrTexture>(); |
| 732 | } |
| 733 | |
Greg Daniel | 057627f | 2018-03-14 15:51:58 -0400 | [diff] [blame] | 734 | return promiseHelper.getTexture(resourceProvider, config); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 735 | }, desc, origin, mipMapped, formatFlags, SkBackingFit::kExact, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 736 | SkBudgeted::kNo, GrSurfaceProxy::LazyInstantiationType::kUninstantiate); |
| 737 | |
| 738 | if (!proxy) { |
| 739 | return nullptr; |
| 740 | } |
| 741 | |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 742 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, alphaType, |
| 743 | std::move(proxy), std::move(colorSpace), SkBudgeted::kNo); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 744 | } |
| 745 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 746 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 747 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 748 | sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded, |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 749 | bool buildMips, SkColorSpace* dstColorSpace, |
| 750 | bool limitToMaxTextureSize) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 751 | sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded)); |
| 752 | if (!codecImage) { |
| 753 | return nullptr; |
| 754 | } |
| 755 | |
| 756 | // Some backends or drivers don't support (safely) moving resources between contexts |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 757 | if (!context || !context->contextPriv().caps()->crossContextTextureSupport()) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 758 | return codecImage; |
| 759 | } |
| 760 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 761 | auto maxTextureSize = context->contextPriv().caps()->maxTextureSize(); |
| 762 | if (limitToMaxTextureSize && |
| 763 | (codecImage->width() > maxTextureSize || codecImage->height() > maxTextureSize)) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 764 | SkAutoPixmapStorage pmap; |
| 765 | SkImageInfo info = as_IB(codecImage)->onImageInfo(); |
| 766 | if (!dstColorSpace) { |
| 767 | info = info.makeColorSpace(nullptr); |
| 768 | } |
| 769 | if (!pmap.tryAlloc(info) || !codecImage->readPixels(pmap, 0, 0, kDisallow_CachingHint)) { |
| 770 | return nullptr; |
| 771 | } |
| 772 | return MakeCrossContextFromPixmap(context, pmap, buildMips, dstColorSpace, true); |
| 773 | } |
| 774 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 775 | // Turn the codec image into a GrTextureProxy |
| 776 | GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint); |
| 777 | sk_sp<SkColorSpace> texColorSpace; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 778 | GrSamplerState samplerState( |
| 779 | GrSamplerState::WrapMode::kClamp, |
| 780 | buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp); |
| 781 | sk_sp<GrTextureProxy> proxy( |
| 782 | maker.refTextureProxyForParams(samplerState, dstColorSpace, &texColorSpace, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 783 | if (!proxy) { |
| 784 | return codecImage; |
| 785 | } |
| 786 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 787 | if (!proxy->instantiate(context->contextPriv().resourceProvider())) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 788 | return codecImage; |
| 789 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 790 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 791 | |
| 792 | // Flush any writes or uploads |
| 793 | context->contextPriv().prepareSurfaceForExternalIO(proxy.get()); |
| 794 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 795 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 796 | sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 797 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 798 | auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(), |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 799 | std::move(sema), |
| 800 | as_IB(codecImage)->onImageInfo().colorType(), |
| 801 | codecImage->alphaType(), |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 802 | std::move(texColorSpace)); |
| 803 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 804 | } |
| 805 | |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 806 | sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrContext* context, |
| 807 | const SkPixmap& originalPixmap, bool buildMips, |
| 808 | SkColorSpace* dstColorSpace, |
| 809 | bool limitToMaxTextureSize) { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 810 | // Some backends or drivers don't support (safely) moving resources between contexts |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 811 | if (!context || !context->contextPriv().caps()->crossContextTextureSupport()) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 812 | return SkImage::MakeRasterCopy(originalPixmap); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Greg Daniel | 9e78811 | 2018-02-08 14:29:37 -0500 | [diff] [blame] | 815 | // If we don't have access to the resource provider and gpu (i.e. in a DDL context) we will not |
| 816 | // be able to make everything needed for a GPU CrossContext image. Thus return a raster copy |
| 817 | // instead. |
| 818 | if (!context->contextPriv().resourceProvider()) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 819 | return SkImage::MakeRasterCopy(originalPixmap); |
Greg Daniel | 9e78811 | 2018-02-08 14:29:37 -0500 | [diff] [blame] | 820 | } |
| 821 | |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 822 | const SkPixmap* pixmap = &originalPixmap; |
| 823 | SkAutoPixmapStorage resized; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 824 | int maxTextureSize = context->contextPriv().caps()->maxTextureSize(); |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 825 | int maxDim = SkTMax(originalPixmap.width(), originalPixmap.height()); |
| 826 | if (limitToMaxTextureSize && maxDim > maxTextureSize) { |
| 827 | float scale = static_cast<float>(maxTextureSize) / maxDim; |
| 828 | int newWidth = SkTMin(static_cast<int>(originalPixmap.width() * scale), maxTextureSize); |
| 829 | int newHeight = SkTMin(static_cast<int>(originalPixmap.height() * scale), maxTextureSize); |
| 830 | SkImageInfo info = originalPixmap.info().makeWH(newWidth, newHeight); |
| 831 | if (!resized.tryAlloc(info) || !originalPixmap.scalePixels(resized, kLow_SkFilterQuality)) { |
| 832 | return nullptr; |
| 833 | } |
| 834 | pixmap = &resized; |
| 835 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 836 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 837 | // Turn the pixmap into a GrTextureProxy |
| 838 | sk_sp<GrTextureProxy> proxy; |
| 839 | if (buildMips) { |
| 840 | SkBitmap bmp; |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 841 | bmp.installPixels(*pixmap); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 842 | proxy = proxyProvider->createMipMapProxyFromBitmap(bmp); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 843 | } else { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 844 | if (SkImageInfoIsValid(pixmap->info())) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 845 | ATRACE_ANDROID_FRAMEWORK("Upload Texture [%ux%u]", pixmap->width(), pixmap->height()); |
Greg Daniel | abadbee | 2018-03-20 12:09:09 -0400 | [diff] [blame] | 846 | // We don't need a release proc on the data in pixmap since we know we are in a |
| 847 | // GrContext that has a resource provider. Thus the createTextureProxy call will |
| 848 | // immediately upload the data. |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 849 | sk_sp<SkImage> image = SkImage::MakeFromRaster(*pixmap, nullptr, nullptr); |
Greg Daniel | abadbee | 2018-03-20 12:09:09 -0400 | [diff] [blame] | 850 | proxy = proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1, |
| 851 | SkBudgeted::kYes, SkBackingFit::kExact); |
Greg Daniel | bc54fad | 2018-02-09 16:40:32 -0500 | [diff] [blame] | 852 | } |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | if (!proxy) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 856 | return SkImage::MakeRasterCopy(*pixmap); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture()); |
| 860 | |
| 861 | // Flush any writes or uploads |
| 862 | context->contextPriv().prepareSurfaceForExternalIO(proxy.get()); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 863 | GrGpu* gpu = context->contextPriv().getGpu(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 864 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 865 | sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get()); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 866 | |
| 867 | auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(), |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 868 | std::move(sema), pixmap->colorType(), |
| 869 | pixmap->alphaType(), |
| 870 | pixmap->info().refColorSpace()); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 871 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 872 | } |
| 873 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 874 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 875 | sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at, |
| 876 | sk_sp<SkColorSpace> cs) { |
| 877 | auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs); |
| 878 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 879 | } |
| 880 | #endif |
| 881 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 882 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 883 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 884 | bool SkImage::MakeBackendTextureFromSkImage(GrContext* ctx, |
| 885 | sk_sp<SkImage> image, |
| 886 | GrBackendTexture* backendTexture, |
| 887 | BackendTextureReleaseProc* releaseProc) { |
| 888 | if (!image || !ctx || !backendTexture || !releaseProc) { |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | // Ensure we have a texture backed image. |
| 893 | if (!image->isTextureBacked()) { |
| 894 | image = image->makeTextureImage(ctx, nullptr); |
| 895 | if (!image) { |
| 896 | return false; |
| 897 | } |
| 898 | } |
| 899 | GrTexture* texture = image->getTexture(); |
Eric Karl | 36591e5 | 2018-01-19 13:45:02 -0800 | [diff] [blame] | 900 | if (!texture) { |
| 901 | // In context-loss cases, we may not have a texture. |
| 902 | return false; |
| 903 | } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 904 | |
| 905 | // If the image's context doesn't match the provided context, fail. |
| 906 | if (texture->getContext() != ctx) { |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | // Flush any pending IO on the texture. |
| 911 | ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy()); |
| 912 | SkASSERT(!texture->surfacePriv().hasPendingIO()); |
| 913 | |
| 914 | // We must make a copy of the image if the image is not unique, if the GrTexture owned by the |
| 915 | // image is not unique, or if the texture wraps an external object. |
| 916 | if (!image->unique() || !texture->surfacePriv().hasUniqueRef() || |
| 917 | texture->resourcePriv().refsWrappedObjects()) { |
| 918 | // onMakeSubset will always copy the image. |
| 919 | image = as_IB(image)->onMakeSubset(image->bounds()); |
| 920 | if (!image) { |
| 921 | return false; |
| 922 | } |
| 923 | |
| 924 | texture = image->getTexture(); |
Eric Karl | 36591e5 | 2018-01-19 13:45:02 -0800 | [diff] [blame] | 925 | if (!texture) { |
| 926 | return false; |
| 927 | } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 928 | |
| 929 | // Flush to ensure that the copy is completed before we return the texture. |
| 930 | ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy()); |
| 931 | SkASSERT(!texture->surfacePriv().hasPendingIO()); |
| 932 | } |
| 933 | |
| 934 | SkASSERT(!texture->resourcePriv().refsWrappedObjects()); |
| 935 | SkASSERT(texture->surfacePriv().hasUniqueRef()); |
| 936 | SkASSERT(image->unique()); |
| 937 | |
| 938 | // Take a reference to the GrTexture and release the image. |
| 939 | sk_sp<GrTexture> textureRef(SkSafeRef(texture)); |
| 940 | image = nullptr; |
| 941 | |
| 942 | // Steal the backend texture from the GrTexture, releasing the GrTexture in the process. |
| 943 | return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc); |
| 944 | } |
| 945 | |
| 946 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 947 | |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 948 | sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType) const { |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 949 | sk_sp<SkColorSpace> srcSpace = fColorSpace; |
| 950 | if (!fColorSpace) { |
| 951 | if (target->isSRGB()) { |
| 952 | return sk_ref_sp(const_cast<SkImage*>((SkImage*)this)); |
| 953 | } |
| 954 | |
| 955 | srcSpace = SkColorSpace::MakeSRGB(); |
| 956 | } |
| 957 | |
Brian Osman | 47c2751 | 2018-06-18 10:58:51 -0400 | [diff] [blame] | 958 | auto xform = GrColorSpaceXformEffect::Make(srcSpace.get(), target.get()); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 959 | if (!xform) { |
| 960 | return sk_ref_sp(const_cast<SkImage_Gpu*>(this)); |
| 961 | } |
| 962 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 963 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 964 | fContext->contextPriv().makeDeferredRenderTargetContext( |
| 965 | SkBackingFit::kExact, this->width(), this->height(), |
| 966 | kRGBA_8888_GrPixelConfig, nullptr)); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 967 | if (!renderTargetContext) { |
| 968 | return nullptr; |
| 969 | } |
| 970 | |
| 971 | GrPaint paint; |
| 972 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 973 | paint.addColorTextureProcessor(fProxy, SkMatrix::I()); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 974 | paint.addColorFragmentProcessor(std::move(xform)); |
| 975 | |
| 976 | const SkRect rect = SkRect::MakeIWH(this->width(), this->height()); |
| 977 | |
| 978 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
| 979 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 980 | if (!renderTargetContext->asTextureProxy()) { |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 981 | return nullptr; |
| 982 | } |
| 983 | |
| 984 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
| 985 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, |
| 986 | fAlphaType, renderTargetContext->asTextureProxyRef(), |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 987 | std::move(target), fBudgeted); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 988 | |
| 989 | } |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 990 | |
| 991 | bool SkImage_Gpu::onIsValid(GrContext* context) const { |
| 992 | // The base class has already checked that context isn't abandoned (if it's not nullptr) |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 993 | if (fContext->abandoned()) { |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 994 | return false; |
| 995 | } |
| 996 | |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 997 | if (context && context != fContext.get()) { |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | return true; |
| 1002 | } |