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