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" |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 31 | #include "effects/GrNonlinearColorSpaceXformEffect.h" |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 32 | #include "effects/GrYUVtoRGBEffect.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 33 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 34 | #include "SkBitmapCache.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 35 | #include "SkGr.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 36 | #include "SkImage_Gpu.h" |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 37 | #include "SkImageCacherator.h" |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 38 | #include "SkImageInfoPriv.h" |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 39 | #include "SkMipMap.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 40 | #include "SkPixelRef.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 41 | #include "SkReadPixelsRec.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 42 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 43 | SkImage_Gpu::SkImage_Gpu(GrContext* context, uint32_t uniqueID, SkAlphaType at, |
| 44 | sk_sp<GrTextureProxy> proxy, |
| 45 | sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted) |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 46 | : INHERITED(proxy->worstCaseWidth(), proxy->worstCaseHeight(), uniqueID) |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 47 | , fContext(context) |
| 48 | , fProxy(std::move(proxy)) |
| 49 | , fAlphaType(at) |
| 50 | , fBudgeted(budgeted) |
| 51 | , fColorSpace(std::move(colorSpace)) |
| 52 | , fAddedRasterVersionToCache(false) { |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 53 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 54 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 55 | SkImage_Gpu::~SkImage_Gpu() { |
| 56 | if (fAddedRasterVersionToCache.load()) { |
| 57 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 58 | } |
| 59 | } |
| 60 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 61 | SkImageInfo SkImage_Gpu::onImageInfo() const { |
| 62 | SkColorType ct; |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 63 | if (!GrPixelConfigToColorType(fProxy->config(), &ct)) { |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 64 | ct = kUnknown_SkColorType; |
| 65 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 66 | return SkImageInfo::Make(fProxy->width(), fProxy->height(), ct, fAlphaType, fColorSpace); |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 69 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace*, CachingHint chint) const { |
| 70 | // The SkColorSpace parameter "dstColorSpace" is really just a hint about how/where the bitmap |
| 71 | // will be used. The client doesn't expect that we convert to that color space, it's intended |
| 72 | // for codec-backed images, to drive our decoding heuristic. In theory we *could* read directly |
| 73 | // into that color space (to save the client some effort in whatever they're about to do), but |
| 74 | // that would make our use of the bitmap cache incorrect (or much less efficient, assuming we |
| 75 | // rolled the dstColorSpace into the key). |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 76 | const auto desc = SkBitmapCacheDesc::Make(this); |
| 77 | if (SkBitmapCache::Find(desc, dst)) { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 78 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 79 | SkASSERT(dst->isImmutable()); |
| 80 | SkASSERT(dst->getPixels()); |
| 81 | return true; |
| 82 | } |
| 83 | |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 84 | SkBitmapCache::RecPtr rec = nullptr; |
| 85 | SkPixmap pmap; |
| 86 | if (kAllow_CachingHint == chint) { |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 87 | rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap); |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 88 | if (!rec) { |
| 89 | return false; |
| 90 | } |
| 91 | } else { |
Brian Osman | 6251771 | 2017-04-26 16:26:39 -0400 | [diff] [blame] | 92 | if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) { |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 93 | return false; |
| 94 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 95 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 96 | |
Brian Salomon | 2084ffa | 2017-03-27 17:32:18 -0400 | [diff] [blame] | 97 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
| 98 | fProxy, |
| 99 | fColorSpace); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 100 | if (!sContext) { |
| 101 | return false; |
| 102 | } |
| 103 | |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 104 | if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 107 | |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 108 | if (rec) { |
| 109 | SkBitmapCache::Add(std::move(rec), dst); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 110 | fAddedRasterVersionToCache.store(true); |
| 111 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 115 | sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef(GrContext* context, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 116 | const GrSamplerState& params, |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 117 | SkColorSpace* dstColorSpace, |
| 118 | sk_sp<SkColorSpace>* texColorSpace, |
| 119 | SkScalar scaleAdjust[2]) const { |
Robert Phillips | 30a38ff | 2017-03-22 10:31:11 -0400 | [diff] [blame] | 120 | if (context != fContext) { |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 121 | SkASSERT(0); |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 125 | if (texColorSpace) { |
| 126 | *texColorSpace = this->fColorSpace; |
| 127 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 128 | |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 129 | GrTextureAdjuster adjuster(fContext, fProxy, this->alphaType(), this->uniqueID(), |
| 130 | this->fColorSpace.get()); |
Greg Daniel | 4faa040 | 2017-09-29 15:21:28 -0400 | [diff] [blame] | 131 | return adjuster.refTextureProxySafeForParams(params, scaleAdjust); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 134 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 135 | switch (info.colorType()) { |
| 136 | case kRGBA_8888_SkColorType: |
| 137 | case kBGRA_8888_SkColorType: |
| 138 | break; |
| 139 | default: |
| 140 | return; // nothing to do |
| 141 | } |
| 142 | |
| 143 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 144 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 145 | // SkPreMultiplyColor() |
| 146 | // |
| 147 | SkColor* row = (SkColor*)pixels; |
| 148 | for (int y = 0; y < info.height(); ++y) { |
| 149 | for (int x = 0; x < info.width(); ++x) { |
| 150 | row[x] = SkPreMultiplyColor(row[x]); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 155 | GrBackendObject SkImage_Gpu::onGetTextureHandle(bool flushPendingGrContextIO, |
| 156 | GrSurfaceOrigin* origin) const { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 157 | SkASSERT(fProxy); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 158 | |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 159 | if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) { |
| 160 | // This image was created with a DDL context and cannot be instantiated. Thus we return 0 |
| 161 | // here which is considered invalid for all backends. |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | if (GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState()) { |
| 166 | SkASSERT(fContext->contextPriv().resourceProvider()); |
| 167 | fProxy->priv().doLazyInstantiation(fContext->contextPriv().resourceProvider()); |
| 168 | if (!fProxy->priv().isInstantiated()) { |
| 169 | // We failed to instantiate the lazy proxy. Thus we return 0 here which is considered |
| 170 | // invalid for all backends. |
| 171 | return 0; |
| 172 | } |
| 173 | } |
| 174 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 175 | if (!fProxy->instantiate(fContext->contextPriv().resourceProvider())) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | GrTexture* texture = fProxy->priv().peekTexture(); |
| 180 | |
| 181 | if (texture) { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 182 | if (flushPendingGrContextIO) { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 183 | fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get()); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 184 | } |
| 185 | if (origin) { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 186 | *origin = fProxy->origin(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 187 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 188 | return texture->getTextureHandle(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | GrTexture* SkImage_Gpu::onGetTexture() const { |
| 194 | GrTextureProxy* proxy = this->peekProxy(); |
| 195 | if (!proxy) { |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 199 | if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | return proxy->priv().peekTexture(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 206 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 207 | int srcX, int srcY, CachingHint) const { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 208 | if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 212 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 213 | if (!rec.trim(this->width(), this->height())) { |
| 214 | return false; |
| 215 | } |
| 216 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 217 | // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and |
| 218 | // GrRenderTargetContext::onReadPixels |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 219 | uint32_t flags = 0; |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 220 | if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 221 | // let the GPU perform this transformation for us |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 222 | flags = GrContextPriv::kUnpremul_PixelOpsFlag; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 223 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 224 | |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 225 | // This hack allows us to call makeNonTextureImage on images with arbitrary color spaces. |
| 226 | // Otherwise, we'll be unable to create a render target context. |
| 227 | // TODO: This shouldn't be necessary - we need more robust support for images (and surfaces) |
| 228 | // with arbitrary color spaces. Unfortunately, this is one spot where we go from image to |
| 229 | // surface (rather than the opposite), and our lenient image rules break our (currently) more |
| 230 | // strict surface rules. |
Mike Klein | 98e38e2 | 2018-01-12 15:59:53 +0000 | [diff] [blame] | 231 | // We treat null-dst color space as always equal to fColorSpace for this kind of read-back. |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 232 | sk_sp<SkColorSpace> surfaceColorSpace = fColorSpace; |
Mike Klein | 98e38e2 | 2018-01-12 15:59:53 +0000 | [diff] [blame] | 233 | if (!flags) { |
| 234 | if (!dstInfo.colorSpace() || |
| 235 | SkColorSpace::Equals(fColorSpace.get(), dstInfo.colorSpace())) { |
| 236 | surfaceColorSpace = nullptr; |
| 237 | } |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Brian Salomon | 2084ffa | 2017-03-27 17:32:18 -0400 | [diff] [blame] | 240 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 241 | fProxy, surfaceColorSpace); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 242 | if (!sContext) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 245 | |
| 246 | if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) { |
| 247 | return false; |
| 248 | } |
| 249 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 250 | // do we have to manually fix-up the alpha channel? |
| 251 | // src dst |
| 252 | // unpremul premul fix manually |
| 253 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 254 | // all other combos need to change. |
| 255 | // |
| 256 | // Should this be handled by Ganesh? todo:? |
| 257 | // |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 258 | if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 259 | apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 264 | sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const { |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 265 | GrSurfaceDesc desc; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 266 | desc.fOrigin = fProxy->origin(); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 267 | desc.fWidth = subset.width(); |
| 268 | desc.fHeight = subset.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 269 | desc.fConfig = fProxy->config(); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 270 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 271 | sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext( |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 272 | desc, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 273 | GrMipMapped::kNo, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 274 | SkBackingFit::kExact, |
| 275 | fBudgeted)); |
| 276 | if (!sContext) { |
| 277 | return nullptr; |
| 278 | } |
| 279 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 280 | if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 281 | return nullptr; |
| 282 | } |
| 283 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 284 | // MDB: this call is okay bc we know 'sContext' was kExact |
| 285 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, |
| 286 | fAlphaType, sContext->asTextureProxyRef(), |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 287 | fColorSpace, fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 288 | } |
| 289 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 290 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 291 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 292 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, |
| 293 | const GrBackendTexture& backendTex, |
| 294 | GrSurfaceOrigin origin, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 295 | SkAlphaType at, sk_sp<SkColorSpace> colorSpace, |
| 296 | GrWrapOwnership ownership, |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 297 | SkImage::TextureReleaseProc releaseProc, |
| 298 | SkImage::ReleaseContext releaseCtx) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 299 | if (backendTex.width() <= 0 || backendTex.height() <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 300 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 301 | } |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 302 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 303 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 304 | sk_sp<GrTextureProxy> proxy = proxyProvider->createWrappedTextureProxy( |
| 305 | backendTex, origin, ownership, releaseProc, releaseCtx); |
| 306 | if (!proxy) { |
Robert Phillips | e201ebc | 2018-01-17 18:12:50 +0000 | [diff] [blame] | 307 | return nullptr; |
| 308 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 309 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 310 | return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 311 | at, std::move(proxy), std::move(colorSpace), SkBudgeted::kNo); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 314 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, |
| 315 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 316 | SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 317 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 318 | if (!ctx) { |
| 319 | return nullptr; |
| 320 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 321 | return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kBorrow_GrWrapOwnership, |
| 322 | releaseP, releaseC); |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 325 | bool validate_backend_texture(GrContext* ctx, const GrBackendTexture& tex, GrPixelConfig* config, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 326 | SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) { |
| 327 | // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to |
| 328 | // create a fake image info here. |
| 329 | SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs); |
| 330 | if (!SkImageInfoIsValidAllowNumericalCS(info)) { |
| 331 | return false; |
| 332 | } |
| 333 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 334 | return ctx->caps()->validateBackendTexture(tex, ct, config); |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, |
| 338 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 339 | SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 340 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 341 | if (!ctx) { |
| 342 | return nullptr; |
| 343 | } |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 344 | GrBackendTexture texCopy = tex; |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 345 | if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) { |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 346 | return nullptr; |
| 347 | } |
| 348 | return MakeFromTexture(ctx, texCopy, origin, at, cs, releaseP, releaseC); |
| 349 | } |
| 350 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 351 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, |
| 352 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 353 | SkAlphaType at, sk_sp<SkColorSpace> cs) { |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 354 | if (!ctx->contextPriv().resourceProvider()) { |
| 355 | // We have a DDL context and we don't support adopted textures for them. |
| 356 | return nullptr; |
| 357 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 358 | return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kAdopt_GrWrapOwnership, |
| 359 | nullptr, nullptr); |
| 360 | } |
| 361 | |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 362 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, |
| 363 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 364 | SkColorType ct, SkAlphaType at, |
| 365 | sk_sp<SkColorSpace> cs) { |
| 366 | GrBackendTexture texCopy = tex; |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 367 | if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) { |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 368 | return nullptr; |
| 369 | } |
| 370 | return MakeFromAdoptedTexture(ctx, texCopy, origin, at, cs); |
| 371 | } |
| 372 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 373 | static GrBackendTexture make_backend_texture_from_handle(GrBackend backend, |
| 374 | int width, int height, |
| 375 | GrPixelConfig config, |
| 376 | GrBackendObject handle) { |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 377 | switch (backend) { |
| 378 | case kOpenGL_GrBackend: { |
| 379 | const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle); |
| 380 | return GrBackendTexture(width, height, config, *glInfo); |
| 381 | } |
Mike Reed | d20b5c4 | 2017-06-14 06:03:10 -0400 | [diff] [blame] | 382 | #ifdef SK_VULKAN |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 383 | case kVulkan_GrBackend: { |
| 384 | const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle); |
| 385 | return GrBackendTexture(width, height, *vkInfo); |
| 386 | } |
Robert Phillips | fcd5fdd | 2017-06-14 01:43:29 +0000 | [diff] [blame] | 387 | #endif |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 388 | case kMock_GrBackend: { |
| 389 | const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle); |
| 390 | return GrBackendTexture(width, height, config, *mockInfo); |
| 391 | } |
| 392 | default: |
| 393 | return GrBackendTexture(); |
| 394 | } |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 397 | static bool are_yuv_sizes_valid(const SkISize yuvSizes[], bool nv12) { |
| 398 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 399 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0) { |
| 400 | return false; |
| 401 | } |
| 402 | if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) { |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 409 | static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 410 | bool nv12, |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 411 | const GrBackendTexture yuvBackendTextures[], |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 412 | const SkISize yuvSizes[], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 413 | 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 | |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 417 | if (!are_yuv_sizes_valid(yuvSizes, nv12)) { |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 418 | return nullptr; |
| 419 | } |
| 420 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 421 | sk_sp<GrTextureProxy> yProxy = proxyProvider->createWrappedTextureProxy(yuvBackendTextures[0], |
| 422 | origin); |
| 423 | sk_sp<GrTextureProxy> uProxy = proxyProvider->createWrappedTextureProxy(yuvBackendTextures[1], |
| 424 | origin); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 425 | sk_sp<GrTextureProxy> vProxy; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 426 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 427 | if (nv12) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 428 | vProxy = uProxy; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 429 | } else { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 430 | vProxy = proxyProvider->createWrappedTextureProxy(yuvBackendTextures[2], origin); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 431 | } |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 432 | if (!yProxy || !uProxy || !vProxy) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 433 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 434 | } |
| 435 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 436 | const int width = yuvSizes[0].fWidth; |
| 437 | const int height = yuvSizes[0].fHeight; |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 438 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 439 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 440 | sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext( |
Brian Salomon | d0d7270 | 2018-02-01 14:16:02 -0500 | [diff] [blame^] | 441 | SkBackingFit::kExact, width, height, kRGBA_8888_GrPixelConfig, |
| 442 | std::move(imageColorSpace), 1, GrMipMapped::kNo, origin)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 443 | if (!renderTargetContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 444 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 448 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 449 | paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(yProxy, uProxy, vProxy, |
| 450 | yuvSizes, colorSpace, nv12)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 451 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 452 | const SkRect rect = SkRect::MakeIWH(width, height); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 453 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 454 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 455 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 456 | if (!renderTargetContext->asSurfaceProxy()) { |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 457 | return nullptr; |
| 458 | } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 459 | ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy()); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 460 | |
| 461 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 462 | return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID, kOpaque_SkAlphaType, |
| 463 | renderTargetContext->asTextureProxyRef(), |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 464 | renderTargetContext->colorSpaceInfo().refColorSpace(), |
| 465 | SkBudgeted::kYes); |
| 466 | } |
| 467 | |
| 468 | static sk_sp<SkImage> make_from_yuv_objects_copy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 469 | bool nv12, |
| 470 | const GrBackendObject yuvTextureHandles[], |
| 471 | const SkISize yuvSizes[], |
| 472 | GrSurfaceOrigin origin, |
| 473 | sk_sp<SkColorSpace> imageColorSpace) { |
| 474 | if (!are_yuv_sizes_valid(yuvSizes, nv12)) { |
| 475 | return nullptr; |
| 476 | } |
| 477 | |
| 478 | GrBackendTexture backendTextures[3]; |
| 479 | |
| 480 | const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig; |
| 481 | |
| 482 | GrBackend backend = ctx->contextPriv().getBackend(); |
| 483 | backendTextures[0] = make_backend_texture_from_handle(backend, |
| 484 | yuvSizes[0].fWidth, |
| 485 | yuvSizes[0].fHeight, |
| 486 | kConfig, |
| 487 | yuvTextureHandles[0]); |
| 488 | backendTextures[1] = make_backend_texture_from_handle(backend, |
| 489 | yuvSizes[1].fWidth, |
| 490 | yuvSizes[1].fHeight, |
| 491 | kConfig, |
| 492 | yuvTextureHandles[1]); |
| 493 | |
| 494 | if (!nv12) { |
| 495 | backendTextures[2] = make_backend_texture_from_handle(backend, |
| 496 | yuvSizes[2].fWidth, |
| 497 | yuvSizes[2].fHeight, |
| 498 | kConfig, |
| 499 | yuvTextureHandles[2]); |
| 500 | } |
| 501 | |
| 502 | return make_from_yuv_textures_copy(ctx, colorSpace, nv12, |
| 503 | backendTextures, yuvSizes, origin, |
| 504 | std::move(imageColorSpace)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 505 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 506 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 507 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 508 | const GrBackendObject yuvTextureHandles[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 509 | const SkISize yuvSizes[3], GrSurfaceOrigin origin, |
| 510 | sk_sp<SkColorSpace> imageColorSpace) { |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 511 | return make_from_yuv_objects_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin, |
| 512 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 516 | const GrBackendObject yuvTextureHandles[2], |
| 517 | const SkISize yuvSizes[2], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 518 | GrSurfaceOrigin origin, |
| 519 | sk_sp<SkColorSpace> imageColorSpace) { |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 520 | return make_from_yuv_objects_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin, |
| 521 | std::move(imageColorSpace)); |
| 522 | } |
| 523 | |
| 524 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 525 | const GrBackendTexture yuvBackendTextures[3], |
| 526 | const SkISize yuvSizes[3], GrSurfaceOrigin origin, |
| 527 | sk_sp<SkColorSpace> imageColorSpace) { |
| 528 | return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvBackendTextures, yuvSizes, origin, |
| 529 | std::move(imageColorSpace)); |
| 530 | } |
| 531 | |
| 532 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 533 | const GrBackendTexture yuvBackendTextures[2], |
| 534 | const SkISize yuvSizes[2], |
| 535 | GrSurfaceOrigin origin, |
| 536 | sk_sp<SkColorSpace> imageColorSpace) { |
| 537 | return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvBackendTextures, yuvSizes, origin, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 538 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 541 | static sk_sp<SkImage> create_image_from_maker(GrContext* context, GrTextureMaker* maker, |
| 542 | SkAlphaType at, uint32_t id, |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 543 | SkColorSpace* dstColorSpace) { |
| 544 | sk_sp<SkColorSpace> texColorSpace; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 545 | sk_sp<GrTextureProxy> proxy(maker->refTextureProxyForParams( |
| 546 | GrSamplerState::ClampNearest(), dstColorSpace, &texColorSpace, nullptr)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 547 | if (!proxy) { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 548 | return nullptr; |
| 549 | } |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 550 | return sk_make_sp<SkImage_Gpu>(context, id, at, |
| 551 | std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 552 | } |
| 553 | |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 554 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 555 | if (!context) { |
| 556 | return nullptr; |
| 557 | } |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 558 | if (GrContext* incumbent = as_IB(this)->context()) { |
| 559 | return incumbent == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 562 | if (this->isLazyGenerated()) { |
| 563 | GrImageTextureMaker maker(context, this, kDisallow_CachingHint); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 564 | return create_image_from_maker(context, &maker, this->alphaType(), |
| 565 | this->uniqueID(), dstColorSpace); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) { |
| 569 | GrBitmapTextureMaker maker(context, *bmp); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 570 | return create_image_from_maker(context, &maker, this->alphaType(), |
| 571 | this->uniqueID(), dstColorSpace); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 572 | } |
| 573 | return nullptr; |
| 574 | } |
| 575 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 576 | sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded, |
| 577 | bool buildMips, SkColorSpace* dstColorSpace) { |
| 578 | sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded)); |
| 579 | if (!codecImage) { |
| 580 | return nullptr; |
| 581 | } |
| 582 | |
| 583 | // Some backends or drivers don't support (safely) moving resources between contexts |
| 584 | if (!context || !context->caps()->crossContextTextureSupport()) { |
| 585 | return codecImage; |
| 586 | } |
| 587 | |
| 588 | // Turn the codec image into a GrTextureProxy |
| 589 | GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint); |
| 590 | sk_sp<SkColorSpace> texColorSpace; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 591 | GrSamplerState samplerState( |
| 592 | GrSamplerState::WrapMode::kClamp, |
| 593 | buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp); |
| 594 | sk_sp<GrTextureProxy> proxy( |
| 595 | maker.refTextureProxyForParams(samplerState, dstColorSpace, &texColorSpace, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 596 | if (!proxy) { |
| 597 | return codecImage; |
| 598 | } |
| 599 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 600 | if (!proxy->instantiate(context->contextPriv().resourceProvider())) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 601 | return codecImage; |
| 602 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 603 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 604 | |
| 605 | // Flush any writes or uploads |
| 606 | context->contextPriv().prepareSurfaceForExternalIO(proxy.get()); |
| 607 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 608 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 609 | sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 610 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 611 | auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(), |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 612 | std::move(sema), codecImage->alphaType(), |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 613 | std::move(texColorSpace)); |
| 614 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 615 | } |
| 616 | |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 617 | sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap, |
| 618 | bool buildMips, SkColorSpace* dstColorSpace) { |
| 619 | // Some backends or drivers don't support (safely) moving resources between contexts |
| 620 | if (!context || !context->caps()->crossContextTextureSupport()) { |
| 621 | return SkImage::MakeRasterCopy(pixmap); |
| 622 | } |
| 623 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 624 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 625 | // Turn the pixmap into a GrTextureProxy |
| 626 | sk_sp<GrTextureProxy> proxy; |
| 627 | if (buildMips) { |
| 628 | SkBitmap bmp; |
| 629 | bmp.installPixels(pixmap); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 630 | proxy = GrGenerateMipMapsAndUploadToTextureProxy(proxyProvider, bmp, dstColorSpace); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 631 | } else { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 632 | proxy = GrUploadPixmapToTextureProxy(proxyProvider, pixmap, SkBudgeted::kYes, |
| 633 | dstColorSpace); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (!proxy) { |
| 637 | return SkImage::MakeRasterCopy(pixmap); |
| 638 | } |
| 639 | |
| 640 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture()); |
| 641 | |
| 642 | // Flush any writes or uploads |
| 643 | context->contextPriv().prepareSurfaceForExternalIO(proxy.get()); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 644 | GrGpu* gpu = context->contextPriv().getGpu(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 645 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 646 | sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get()); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 647 | |
| 648 | auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(), |
| 649 | std::move(sema), pixmap.alphaType(), |
| 650 | pixmap.info().refColorSpace()); |
| 651 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 652 | } |
| 653 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 654 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 655 | sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at, |
| 656 | sk_sp<SkColorSpace> cs) { |
| 657 | auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs); |
| 658 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 659 | } |
| 660 | #endif |
| 661 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 662 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 663 | |
Eric Karl | aae533e | 2017-12-15 23:37:45 +0000 | [diff] [blame] | 664 | namespace { |
| 665 | struct MipMapLevelData { |
| 666 | void* fPixelData; |
| 667 | size_t fRowBytes; |
| 668 | }; |
| 669 | |
| 670 | struct DeferredTextureImage { |
| 671 | uint32_t fContextUniqueID; |
| 672 | // Right now, the destination color mode is only considered when generating mipmaps |
| 673 | SkDestinationSurfaceColorMode fColorMode; |
| 674 | // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace. |
| 675 | int fWidth; |
| 676 | int fHeight; |
| 677 | SkColorType fColorType; |
| 678 | SkAlphaType fAlphaType; |
| 679 | void* fColorSpace; |
| 680 | size_t fColorSpaceSize; |
| 681 | int fMipMapLevelCount; |
| 682 | // The fMipMapLevelData array may contain more than 1 element. |
| 683 | // It contains fMipMapLevelCount elements. |
| 684 | // That means this struct's size is not known at compile-time. |
| 685 | MipMapLevelData fMipMapLevelData[1]; |
| 686 | }; |
| 687 | } // anonymous namespace |
| 688 | |
| 689 | static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) { |
| 690 | // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData. |
| 691 | // This can cause runaway memory leaks, so we are disabling this path until we can |
| 692 | // investigate further. crbug.com/669775 |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | namespace { |
| 697 | |
| 698 | class DTIBufferFiller |
| 699 | { |
| 700 | public: |
| 701 | explicit DTIBufferFiller(char* bufferAsCharPtr) |
| 702 | : bufferAsCharPtr_(bufferAsCharPtr) {} |
| 703 | |
| 704 | void fillMember(const void* source, size_t memberOffset, size_t size) { |
| 705 | memcpy(bufferAsCharPtr_ + memberOffset, source, size); |
| 706 | } |
| 707 | |
| 708 | private: |
| 709 | |
| 710 | char* bufferAsCharPtr_; |
| 711 | }; |
| 712 | } |
| 713 | |
| 714 | #define FILL_MEMBER(bufferFiller, member, source) \ |
| 715 | bufferFiller.fillMember(source, \ |
| 716 | offsetof(DeferredTextureImage, member), \ |
| 717 | sizeof(DeferredTextureImage::member)); |
| 718 | |
| 719 | static bool SupportsColorSpace(SkColorType colorType) { |
| 720 | switch (colorType) { |
| 721 | case kRGBA_8888_SkColorType: |
| 722 | case kBGRA_8888_SkColorType: |
| 723 | case kRGBA_F16_SkColorType: |
| 724 | return true; |
| 725 | default: |
| 726 | return false; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
| 731 | const DeferredTextureImageUsageParams params[], |
| 732 | int paramCnt, void* buffer, |
| 733 | SkColorSpace* dstColorSpace, |
| 734 | SkColorType dstColorType) const { |
| 735 | // Some quick-rejects where is makes no sense to return CPU data |
| 736 | // e.g. |
| 737 | // - texture backed |
| 738 | // - picture backed |
| 739 | // |
| 740 | if (this->isTextureBacked()) { |
| 741 | return 0; |
| 742 | } |
| 743 | if (as_IB(this)->onCanLazyGenerateOnGPU()) { |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | bool supportsColorSpace = SupportsColorSpace(dstColorType); |
| 748 | // Quick reject if the caller requests a color space with an unsupported color type. |
| 749 | if (SkToBool(dstColorSpace) && !supportsColorSpace) { |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | // Extract relevant min/max values from the params array. |
| 754 | int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; |
| 755 | SkFilterQuality highestFilterQuality = params[0].fQuality; |
| 756 | bool useMipMaps = should_use_mip_maps(params[0]); |
| 757 | for (int i = 1; i < paramCnt; ++i) { |
| 758 | if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) |
| 759 | lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; |
| 760 | if (highestFilterQuality < params[i].fQuality) |
| 761 | highestFilterQuality = params[i].fQuality; |
| 762 | useMipMaps |= should_use_mip_maps(params[i]); |
| 763 | } |
| 764 | |
| 765 | const bool fillMode = SkToBool(buffer); |
| 766 | if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | // Calculate scaling parameters. |
| 771 | bool isScaled = lowestPreScaleMipLevel != 0; |
| 772 | |
| 773 | SkISize scaledSize; |
| 774 | if (isScaled) { |
| 775 | // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the |
| 776 | // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level. |
| 777 | scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(), |
| 778 | lowestPreScaleMipLevel - 1); |
| 779 | } else { |
| 780 | scaledSize = SkISize::Make(this->width(), this->height()); |
| 781 | } |
| 782 | |
| 783 | // We never want to scale at higher than SW medium quality, as SW medium matches GPU high. |
| 784 | SkFilterQuality scaleFilterQuality = highestFilterQuality; |
| 785 | if (scaleFilterQuality > kMedium_SkFilterQuality) { |
| 786 | scaleFilterQuality = kMedium_SkFilterQuality; |
| 787 | } |
| 788 | |
| 789 | const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
| 790 | if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) { |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | SkAutoPixmapStorage pixmap; |
| 795 | SkImageInfo info; |
| 796 | size_t pixelSize = 0; |
| 797 | if (!isScaled && this->peekPixels(&pixmap) && pixmap.info().colorType() == dstColorType) { |
| 798 | info = pixmap.info(); |
| 799 | pixelSize = SkAlign8(pixmap.computeByteSize()); |
| 800 | if (!dstColorSpace) { |
| 801 | pixmap.setColorSpace(nullptr); |
| 802 | info = info.makeColorSpace(nullptr); |
| 803 | } |
| 804 | } else { |
| 805 | if (!this->isLazyGenerated() && !this->peekPixels(nullptr)) { |
| 806 | return 0; |
| 807 | } |
| 808 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 809 | // Generator backed image. Tweak info to trigger correct kind of decode. |
| 810 | SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat( |
| 811 | dstColorSpace, proxy.fCaps.get()); |
| 812 | info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(), |
| 813 | scaledSize.height()); |
| 814 | } else { |
| 815 | info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height()); |
| 816 | if (!dstColorSpace) { |
| 817 | info = info.makeColorSpace(nullptr); |
| 818 | } |
| 819 | } |
| 820 | // Force color type to be the requested type. |
| 821 | info = info.makeColorType(dstColorType); |
| 822 | pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 823 | if (fillMode) { |
| 824 | // Always decode to N32 and convert to the requested type if necessary. |
| 825 | SkImageInfo decodeInfo = info.makeColorType(kN32_SkColorType); |
| 826 | SkAutoPixmapStorage decodePixmap; |
| 827 | decodePixmap.alloc(decodeInfo); |
| 828 | |
| 829 | if (isScaled) { |
| 830 | if (!this->scalePixels(decodePixmap, scaleFilterQuality, |
| 831 | SkImage::kDisallow_CachingHint)) { |
| 832 | return 0; |
| 833 | } |
| 834 | } else { |
| 835 | if (!this->readPixels(decodePixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 836 | return 0; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | if (decodeInfo.colorType() != info.colorType()) { |
| 841 | pixmap.alloc(info); |
| 842 | // Convert and copy the decoded pixmap to the target pixmap. |
| 843 | decodePixmap.readPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(), 0, |
| 844 | 0); |
| 845 | } else { |
| 846 | pixmap = std::move(decodePixmap); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | int mipMapLevelCount = 1; |
| 851 | if (useMipMaps) { |
| 852 | // SkMipMap only deals with the mipmap levels it generates, which does |
| 853 | // not include the base level. |
| 854 | // That means it generates and holds levels 1-x instead of 0-x. |
| 855 | // So the total mipmap level count is 1 more than what |
| 856 | // SkMipMap::ComputeLevelCount returns. |
| 857 | mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1; |
| 858 | |
| 859 | // We already initialized pixelSize to the size of the base level. |
| 860 | // SkMipMap will generate the extra mipmap levels. Their sizes need to |
| 861 | // be added to the total. |
| 862 | // Index 0 here does not refer to the base mipmap level -- it is |
| 863 | // SkMipMap's first generated mipmap level (level 1). |
| 864 | for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0; |
| 865 | currentMipMapLevelIndex--) { |
| 866 | SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(), |
| 867 | currentMipMapLevelIndex); |
| 868 | SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight); |
| 869 | pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr)); |
| 870 | } |
| 871 | } |
| 872 | size_t size = 0; |
| 873 | size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 874 | size += dtiSize; |
| 875 | size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData); |
| 876 | // We subtract 1 because DeferredTextureImage already includes the base |
| 877 | // level in its size |
| 878 | size_t pixelOffset = size; |
| 879 | size += pixelSize; |
| 880 | size_t colorSpaceOffset = 0; |
| 881 | size_t colorSpaceSize = 0; |
| 882 | SkColorSpaceTransferFn fn; |
| 883 | if (info.colorSpace()) { |
| 884 | SkASSERT(dstColorSpace); |
| 885 | SkASSERT(supportsColorSpace); |
| 886 | colorSpaceOffset = size; |
| 887 | colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); |
| 888 | size += colorSpaceSize; |
| 889 | } else if (supportsColorSpace && this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn)) { |
| 890 | // In legacy mode, preserve the color space tag on the SkImage. This is only |
| 891 | // supported if the color space has a parametric transfer function. |
| 892 | SkASSERT(!dstColorSpace); |
| 893 | colorSpaceOffset = size; |
| 894 | colorSpaceSize = this->colorSpace()->writeToMemory(nullptr); |
| 895 | size += colorSpaceSize; |
| 896 | } |
| 897 | if (!fillMode) { |
| 898 | return size; |
| 899 | } |
| 900 | char* bufferAsCharPtr = reinterpret_cast<char*>(buffer); |
| 901 | char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset; |
| 902 | void* pixels = pixelsAsCharPtr; |
| 903 | |
| 904 | memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))), |
| 905 | pixmap.addr(), pixmap.computeByteSize()); |
| 906 | |
| 907 | // If the context has sRGB support, and we're intending to render to a surface with an attached |
| 908 | // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB) |
| 909 | // aware mip-mapping. |
| 910 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 911 | if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) && |
| 912 | info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) { |
| 913 | SkASSERT(supportsColorSpace); |
| 914 | colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware; |
| 915 | } |
| 916 | |
| 917 | SkASSERT(info == pixmap.info()); |
| 918 | size_t rowBytes = pixmap.rowBytes(); |
| 919 | static_assert(std::is_standard_layout<DeferredTextureImage>::value, |
| 920 | "offsetof, which we use below, requires the type have standard layout"); |
| 921 | auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr}; |
| 922 | FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode); |
| 923 | FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID); |
| 924 | int width = info.width(); |
| 925 | FILL_MEMBER(dtiBufferFiller, fWidth, &width); |
| 926 | int height = info.height(); |
| 927 | FILL_MEMBER(dtiBufferFiller, fHeight, &height); |
| 928 | SkColorType colorType = info.colorType(); |
| 929 | FILL_MEMBER(dtiBufferFiller, fColorType, &colorType); |
| 930 | SkAlphaType alphaType = info.alphaType(); |
| 931 | FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType); |
| 932 | FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount); |
| 933 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData), |
| 934 | &pixels, sizeof(pixels)); |
| 935 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes), |
| 936 | &rowBytes, sizeof(rowBytes)); |
| 937 | if (colorSpaceSize) { |
| 938 | void* colorSpace = bufferAsCharPtr + colorSpaceOffset; |
| 939 | FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace); |
| 940 | FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize); |
| 941 | if (info.colorSpace()) { |
| 942 | info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset); |
| 943 | } else { |
| 944 | SkASSERT(this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn)); |
| 945 | SkASSERT(!dstColorSpace); |
| 946 | this->colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset); |
| 947 | } |
| 948 | } else { |
| 949 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace), |
| 950 | 0, sizeof(DeferredTextureImage::fColorSpace)); |
| 951 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize), |
| 952 | 0, sizeof(DeferredTextureImage::fColorSpaceSize)); |
| 953 | } |
| 954 | |
| 955 | // Fill in the mipmap levels if they exist |
| 956 | char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.computeByteSize()); |
| 957 | |
| 958 | if (useMipMaps) { |
| 959 | static_assert(std::is_standard_layout<MipMapLevelData>::value, |
| 960 | "offsetof, which we use below, requires the type have a standard layout"); |
| 961 | |
| 962 | std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr)); |
| 963 | // SkMipMap holds only the mipmap levels it generates. |
| 964 | // A programmer can use the data they provided to SkMipMap::Build as level 0. |
| 965 | // So the SkMipMap provides levels 1-x but it stores them in its own |
| 966 | // range 0-(x-1). |
| 967 | for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1; |
| 968 | generatedMipLevelIndex++) { |
| 969 | SkMipMap::Level mipLevel; |
| 970 | mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); |
| 971 | |
| 972 | // Make sure the mipmap data is after the start of the buffer |
| 973 | SkASSERT(mipLevelPtr > bufferAsCharPtr); |
| 974 | // Make sure the mipmap data starts before the end of the buffer |
| 975 | SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize); |
| 976 | // Make sure the mipmap data ends before the end of the buffer |
| 977 | SkASSERT(mipLevelPtr + mipLevel.fPixmap.computeByteSize() <= |
| 978 | bufferAsCharPtr + pixelOffset + pixelSize); |
| 979 | |
| 980 | memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.computeByteSize()); |
| 981 | |
| 982 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 983 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 984 | offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*)); |
| 985 | size_t rowBytes = mipLevel.fPixmap.rowBytes(); |
| 986 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 987 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 988 | offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes)); |
| 989 | |
| 990 | mipLevelPtr += SkAlign8(mipLevel.fPixmap.computeByteSize()); |
| 991 | } |
| 992 | } |
| 993 | return size; |
| 994 | } |
| 995 | |
| 996 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data, |
| 997 | SkBudgeted budgeted) { |
| 998 | if (!data) { |
| 999 | return nullptr; |
| 1000 | } |
| 1001 | const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data); |
| 1002 | |
| 1003 | if (!context || context->uniqueID() != dti->fContextUniqueID || context->abandoned()) { |
| 1004 | return nullptr; |
| 1005 | } |
| 1006 | int mipLevelCount = dti->fMipMapLevelCount; |
| 1007 | SkASSERT(mipLevelCount >= 1); |
| 1008 | sk_sp<SkColorSpace> colorSpace; |
| 1009 | if (dti->fColorSpaceSize) { |
| 1010 | colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize); |
| 1011 | } |
| 1012 | SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight, |
| 1013 | dti->fColorType, dti->fAlphaType, colorSpace); |
| 1014 | if (mipLevelCount == 1) { |
| 1015 | SkPixmap pixmap; |
| 1016 | pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes); |
| 1017 | |
| 1018 | // Pass nullptr for the |dstColorSpace|. This opts in to more lenient color space |
| 1019 | // verification. This is ok because we've already verified the color space in |
| 1020 | // getDeferredTextureImageData(). |
| 1021 | sk_sp<GrTextureProxy> proxy(GrUploadPixmapToTextureProxy( |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1022 | context->contextPriv().proxyProvider(), pixmap, budgeted, nullptr)); |
Eric Karl | aae533e | 2017-12-15 23:37:45 +0000 | [diff] [blame] | 1023 | if (!proxy) { |
| 1024 | return nullptr; |
| 1025 | } |
| 1026 | return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, pixmap.alphaType(), |
| 1027 | std::move(proxy), std::move(colorSpace), budgeted); |
| 1028 | } else { |
| 1029 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
| 1030 | for (int i = 0; i < mipLevelCount; i++) { |
| 1031 | texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData; |
| 1032 | texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes; |
| 1033 | } |
| 1034 | |
| 1035 | return SkImage::MakeTextureFromMipMap(context, info, texels.get(), |
| 1036 | mipLevelCount, SkBudgeted::kYes, |
| 1037 | dti->fColorMode); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1042 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1043 | bool SkImage::MakeBackendTextureFromSkImage(GrContext* ctx, |
| 1044 | sk_sp<SkImage> image, |
| 1045 | GrBackendTexture* backendTexture, |
| 1046 | BackendTextureReleaseProc* releaseProc) { |
| 1047 | if (!image || !ctx || !backendTexture || !releaseProc) { |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | // Ensure we have a texture backed image. |
| 1052 | if (!image->isTextureBacked()) { |
| 1053 | image = image->makeTextureImage(ctx, nullptr); |
| 1054 | if (!image) { |
| 1055 | return false; |
| 1056 | } |
| 1057 | } |
| 1058 | GrTexture* texture = image->getTexture(); |
Eric Karl | 36591e5 | 2018-01-19 13:45:02 -0800 | [diff] [blame] | 1059 | if (!texture) { |
| 1060 | // In context-loss cases, we may not have a texture. |
| 1061 | return false; |
| 1062 | } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1063 | |
| 1064 | // If the image's context doesn't match the provided context, fail. |
| 1065 | if (texture->getContext() != ctx) { |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | // Flush any pending IO on the texture. |
| 1070 | ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy()); |
| 1071 | SkASSERT(!texture->surfacePriv().hasPendingIO()); |
| 1072 | |
| 1073 | // We must make a copy of the image if the image is not unique, if the GrTexture owned by the |
| 1074 | // image is not unique, or if the texture wraps an external object. |
| 1075 | if (!image->unique() || !texture->surfacePriv().hasUniqueRef() || |
| 1076 | texture->resourcePriv().refsWrappedObjects()) { |
| 1077 | // onMakeSubset will always copy the image. |
| 1078 | image = as_IB(image)->onMakeSubset(image->bounds()); |
| 1079 | if (!image) { |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
| 1083 | texture = image->getTexture(); |
Eric Karl | 36591e5 | 2018-01-19 13:45:02 -0800 | [diff] [blame] | 1084 | if (!texture) { |
| 1085 | return false; |
| 1086 | } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1087 | |
| 1088 | // Flush to ensure that the copy is completed before we return the texture. |
| 1089 | ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy()); |
| 1090 | SkASSERT(!texture->surfacePriv().hasPendingIO()); |
| 1091 | } |
| 1092 | |
| 1093 | SkASSERT(!texture->resourcePriv().refsWrappedObjects()); |
| 1094 | SkASSERT(texture->surfacePriv().hasUniqueRef()); |
| 1095 | SkASSERT(image->unique()); |
| 1096 | |
| 1097 | // Take a reference to the GrTexture and release the image. |
| 1098 | sk_sp<GrTexture> textureRef(SkSafeRef(texture)); |
| 1099 | image = nullptr; |
| 1100 | |
| 1101 | // Steal the backend texture from the GrTexture, releasing the GrTexture in the process. |
| 1102 | return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc); |
| 1103 | } |
| 1104 | |
| 1105 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1106 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 1107 | sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1108 | const GrMipLevel texels[], int mipLevelCount, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1109 | SkBudgeted budgeted, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 1110 | SkDestinationSurfaceColorMode colorMode) { |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1111 | SkASSERT(mipLevelCount >= 1); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 1112 | if (!ctx) { |
| 1113 | return nullptr; |
| 1114 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1115 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 1116 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1117 | // For images where the client is passing the mip data we require that all the mip levels have |
| 1118 | // valid data. |
| 1119 | for (int i = 0; i < mipLevelCount; ++i) { |
| 1120 | if (!texels[i].fPixels) { |
| 1121 | return nullptr; |
| 1122 | } |
| 1123 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1124 | sk_sp<GrTextureProxy> proxy(GrUploadMipMapToTextureProxy(proxyProvider, info, |
| 1125 | texels, mipLevelCount, colorMode)); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 1126 | if (!proxy) { |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 1127 | return nullptr; |
| 1128 | } |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 1129 | |
| 1130 | SkASSERT(proxy->priv().isExact()); |
| 1131 | return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID, |
| 1132 | info.alphaType(), std::move(proxy), |
| 1133 | info.refColorSpace(), budgeted); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 1134 | } |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 1135 | |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 1136 | sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType, |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 1137 | SkTransferFunctionBehavior premulBehavior) const { |
| 1138 | if (SkTransferFunctionBehavior::kRespect == premulBehavior) { |
| 1139 | // TODO: Implement this. |
| 1140 | return nullptr; |
| 1141 | } |
| 1142 | |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 1143 | sk_sp<SkColorSpace> srcSpace = fColorSpace; |
| 1144 | if (!fColorSpace) { |
| 1145 | if (target->isSRGB()) { |
| 1146 | return sk_ref_sp(const_cast<SkImage*>((SkImage*)this)); |
| 1147 | } |
| 1148 | |
| 1149 | srcSpace = SkColorSpace::MakeSRGB(); |
| 1150 | } |
| 1151 | |
| 1152 | auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), target.get()); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 1153 | if (!xform) { |
| 1154 | return sk_ref_sp(const_cast<SkImage_Gpu*>(this)); |
| 1155 | } |
| 1156 | |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 1157 | sk_sp<GrRenderTargetContext> renderTargetContext(fContext->makeDeferredRenderTargetContext( |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 1158 | SkBackingFit::kExact, this->width(), this->height(), kRGBA_8888_GrPixelConfig, nullptr)); |
| 1159 | if (!renderTargetContext) { |
| 1160 | return nullptr; |
| 1161 | } |
| 1162 | |
| 1163 | GrPaint paint; |
| 1164 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 1165 | paint.addColorTextureProcessor(fProxy, SkMatrix::I()); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 1166 | paint.addColorFragmentProcessor(std::move(xform)); |
| 1167 | |
| 1168 | const SkRect rect = SkRect::MakeIWH(this->width(), this->height()); |
| 1169 | |
| 1170 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
| 1171 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1172 | if (!renderTargetContext->asTextureProxy()) { |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 1173 | return nullptr; |
| 1174 | } |
| 1175 | |
| 1176 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
| 1177 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, |
| 1178 | fAlphaType, renderTargetContext->asTextureProxyRef(), |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 1179 | std::move(target), fBudgeted); |
Brian Osman | 63954c9 | 2017-03-14 12:07:12 -0400 | [diff] [blame] | 1180 | |
| 1181 | } |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 1182 | |
| 1183 | bool SkImage_Gpu::onIsValid(GrContext* context) const { |
| 1184 | // The base class has already checked that context isn't abandoned (if it's not nullptr) |
| 1185 | if (fContext->abandoned()) { |
| 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | if (context && context != fContext) { |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | return true; |
| 1194 | } |