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