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