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" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 13 | #include "GrBitmapTextureMaker.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 14 | #include "GrCaps.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 16 | #include "GrContextPriv.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 17 | #include "GrImageTextureMaker.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 18 | #include "GrRenderTargetContext.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 19 | #include "GrTextureAdjuster.h" |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 20 | #include "GrTexturePriv.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 21 | #include "effects/GrYUVEffect.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 22 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 23 | #include "SkBitmapCache.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 24 | #include "SkGrPriv.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 25 | #include "SkImage_Gpu.h" |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 26 | #include "SkImageCacherator.h" |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 27 | #include "SkMipMap.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 28 | #include "SkPixelRef.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 29 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 30 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 31 | sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 32 | : INHERITED(w, h, uniqueID) |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 33 | , fTexture(std::move(tex)) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 34 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 35 | , fBudgeted(budgeted) |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 36 | , fColorSpace(std::move(colorSpace)) |
bsalomon | 1cd6311 | 2015-08-05 06:58:39 -0700 | [diff] [blame] | 37 | , fAddedRasterVersionToCache(false) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 38 | { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 39 | SkASSERT(fTexture->width() == w); |
| 40 | SkASSERT(fTexture->height() == h); |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 41 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 42 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 43 | SkImage_Gpu::~SkImage_Gpu() { |
| 44 | if (fAddedRasterVersionToCache.load()) { |
| 45 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 46 | } |
| 47 | } |
| 48 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 49 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
robertphillips | ea70c4b | 2016-07-20 08:54:31 -0700 | [diff] [blame] | 50 | if (image->isTextureBacked()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 51 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 52 | } |
| 53 | } |
| 54 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 55 | SkImageInfo SkImage_Gpu::onImageInfo() const { |
| 56 | SkColorType ct; |
| 57 | if (!GrPixelConfigToColorType(fTexture->config(), &ct)) { |
| 58 | ct = kUnknown_SkColorType; |
| 59 | } |
| 60 | return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace); |
| 61 | } |
| 62 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 63 | static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) { |
| 64 | return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace)); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 67 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace, |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 68 | CachingHint chint) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 69 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 70 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 71 | SkASSERT(dst->isImmutable()); |
| 72 | SkASSERT(dst->getPixels()); |
| 73 | return true; |
| 74 | } |
| 75 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 76 | if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(), |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 77 | this->fColorSpace))) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 81 | dst->getPixels(), dst->rowBytes())) { |
| 82 | return false; |
| 83 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 84 | |
| 85 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 86 | if (kAllow_CachingHint == chint) { |
| 87 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 88 | fAddedRasterVersionToCache.store(true); |
| 89 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 93 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 94 | SkColorSpace* dstColorSpace, |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 95 | sk_sp<SkColorSpace>* texColorSpace) const { |
| 96 | if (texColorSpace) { |
| 97 | *texColorSpace = this->fColorSpace; |
| 98 | } |
| 99 | GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(), |
| 100 | this->uniqueID(), this->fColorSpace.get()); |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 101 | return adjuster.refTextureSafeForParams(params, nullptr); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 102 | } |
| 103 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 104 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 105 | switch (info.colorType()) { |
| 106 | case kRGBA_8888_SkColorType: |
| 107 | case kBGRA_8888_SkColorType: |
| 108 | break; |
| 109 | default: |
| 110 | return; // nothing to do |
| 111 | } |
| 112 | |
| 113 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 114 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 115 | // SkPreMultiplyColor() |
| 116 | // |
| 117 | SkColor* row = (SkColor*)pixels; |
| 118 | for (int y = 0; y < info.height(); ++y) { |
| 119 | for (int x = 0; x < info.width(); ++x) { |
| 120 | row[x] = SkPreMultiplyColor(row[x]); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 126 | int srcX, int srcY, CachingHint) const { |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 127 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps()); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 128 | uint32_t flags = 0; |
| 129 | if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 130 | // let the GPU perform this transformation for us |
| 131 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 132 | } |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 133 | if (!fTexture->readPixels(fColorSpace.get(), srcX, srcY, info.width(), info.height(), config, |
| 134 | info.colorSpace(), pixels, rowBytes, flags)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | // do we have to manually fix-up the alpha channel? |
| 138 | // src dst |
| 139 | // unpremul premul fix manually |
| 140 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 141 | // all other combos need to change. |
| 142 | // |
| 143 | // Should this be handled by Ganesh? todo:? |
| 144 | // |
| 145 | if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 146 | apply_premul(info, pixels, rowBytes); |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 151 | sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 152 | GrContext* ctx = fTexture->getContext(); |
| 153 | GrSurfaceDesc desc = fTexture->desc(); |
| 154 | desc.fWidth = subset.width(); |
| 155 | desc.fHeight = subset.height(); |
| 156 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 157 | sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext( |
| 158 | desc, |
| 159 | SkBackingFit::kExact, |
| 160 | fBudgeted)); |
| 161 | if (!sContext) { |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | // TODO: make gpu images be proxy-backed so we don't need to do this |
| 166 | sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture)); |
| 167 | if (!tmpSrc) { |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
| 171 | if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) { |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | // TODO: make gpu images be proxy-backed so we don't need to do this |
| 176 | GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider()); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 177 | if (!subTx) { |
| 178 | return nullptr; |
| 179 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 180 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 181 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 182 | fAlphaType, sk_ref_sp(subTx->asTexture()), |
| 183 | fColorSpace, fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 184 | } |
| 185 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 186 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 187 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 188 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 189 | SkAlphaType at, sk_sp<SkColorSpace> colorSpace, |
| 190 | GrWrapOwnership ownership, |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 191 | SkImage::TextureReleaseProc releaseProc, |
| 192 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 193 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 195 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 196 | sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 197 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 198 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 199 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 200 | if (releaseProc) { |
| 201 | tex->setRelease(releaseProc, releaseCtx); |
| 202 | } |
| 203 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 204 | const SkBudgeted budgeted = SkBudgeted::kNo; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 205 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 206 | at, std::move(tex), std::move(colorSpace), budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 207 | } |
| 208 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 209 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 210 | SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 211 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 212 | return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership, |
| 213 | releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 214 | } |
| 215 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 216 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 217 | SkAlphaType at, sk_sp<SkColorSpace> cs) { |
| 218 | return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership, |
| 219 | nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 220 | } |
| 221 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 222 | static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 223 | bool nv12, |
| 224 | const GrBackendObject yuvTextureHandles[], |
| 225 | const SkISize yuvSizes[], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 226 | GrSurfaceOrigin origin, |
| 227 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 228 | const SkBudgeted budgeted = SkBudgeted::kYes; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 229 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 230 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 || |
| 231 | yuvSizes[1].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 233 | } |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 234 | if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) { |
| 235 | return nullptr; |
| 236 | } |
| 237 | |
| 238 | const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig; |
| 239 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 240 | GrBackendTextureDesc yDesc; |
| 241 | yDesc.fConfig = kConfig; |
| 242 | yDesc.fOrigin = origin; |
| 243 | yDesc.fSampleCnt = 0; |
| 244 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 245 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 246 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 247 | |
| 248 | GrBackendTextureDesc uDesc; |
| 249 | uDesc.fConfig = kConfig; |
| 250 | uDesc.fOrigin = origin; |
| 251 | uDesc.fSampleCnt = 0; |
| 252 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 253 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 254 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 255 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 256 | sk_sp<GrTexture> yTex( |
| 257 | ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership)); |
| 258 | sk_sp<GrTexture> uTex( |
| 259 | ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership)); |
| 260 | sk_sp<GrTexture> vTex; |
| 261 | if (nv12) { |
| 262 | vTex = uTex; |
| 263 | } else { |
| 264 | GrBackendTextureDesc vDesc; |
| 265 | vDesc.fConfig = kConfig; |
| 266 | vDesc.fOrigin = origin; |
| 267 | vDesc.fSampleCnt = 0; |
| 268 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 269 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 270 | vDesc.fHeight = yuvSizes[2].fHeight; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 271 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 272 | vTex = sk_sp<GrTexture>( |
| 273 | ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership)); |
| 274 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 275 | if (!yTex || !uTex || !vTex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 276 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 277 | } |
| 278 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 279 | const int width = yuvSizes[0].fWidth; |
| 280 | const int height = yuvSizes[0].fHeight; |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 281 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 282 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 283 | sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext( |
| 284 | SkBackingFit::kExact, |
| 285 | width, height, |
| 286 | kRGBA_8888_GrPixelConfig, |
| 287 | std::move(imageColorSpace), |
| 288 | 0, |
| 289 | origin)); |
| 290 | if (!renderTargetContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 291 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 295 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 296 | paint.addColorFragmentProcessor( |
| 297 | GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 298 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 299 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 300 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame^] | 301 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 302 | |
| 303 | if (!renderTargetContext->accessRenderTarget()) { |
| 304 | return nullptr; |
| 305 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 306 | ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget()); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 307 | return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 308 | kOpaque_SkAlphaType, renderTargetContext->asTexture(), |
| 309 | sk_ref_sp(renderTargetContext->getColorSpace()), budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 310 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 311 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 312 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 313 | const GrBackendObject yuvTextureHandles[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 314 | const SkISize yuvSizes[3], GrSurfaceOrigin origin, |
| 315 | sk_sp<SkColorSpace> imageColorSpace) { |
| 316 | return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin, |
| 317 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 321 | const GrBackendObject yuvTextureHandles[2], |
| 322 | const SkISize yuvSizes[2], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 323 | GrSurfaceOrigin origin, |
| 324 | sk_sp<SkColorSpace> imageColorSpace) { |
| 325 | return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin, |
| 326 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 327 | } |
| 328 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 329 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 330 | if (!this->isTextureBacked()) { |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 331 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 332 | } |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 333 | SkImageInfo info = as_IB(this)->onImageInfo(); |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 334 | size_t rowBytes = info.minRowBytes(); |
| 335 | size_t size = info.getSafeSize(rowBytes); |
| 336 | auto data = SkData::MakeUninitialized(size); |
| 337 | if (!data) { |
| 338 | return nullptr; |
| 339 | } |
| 340 | SkPixmap pm(info, data->writable_data(), rowBytes); |
| 341 | if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) { |
| 342 | return nullptr; |
| 343 | } |
| 344 | return MakeRasterData(info, data, rowBytes); |
| 345 | } |
| 346 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 347 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap, |
| 348 | SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 349 | if (!ctx) { |
| 350 | return nullptr; |
| 351 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 352 | sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted)); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 353 | if (!texture) { |
| 354 | return nullptr; |
| 355 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 356 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 357 | pixmap.alphaType(), std::move(texture), |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 358 | sk_ref_sp(pixmap.info().colorSpace()), budgeted); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 359 | } |
| 360 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 361 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 362 | |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 363 | namespace { |
| 364 | struct MipMapLevelData { |
| 365 | void* fPixelData; |
| 366 | size_t fRowBytes; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 367 | }; |
| 368 | |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 369 | struct DeferredTextureImage { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 370 | uint32_t fContextUniqueID; |
| 371 | // Right now, the destination color mode is only considered when generating mipmaps |
| 372 | SkDestinationSurfaceColorMode fColorMode; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 373 | // 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] | 374 | int fWidth; |
| 375 | int fHeight; |
| 376 | SkColorType fColorType; |
| 377 | SkAlphaType fAlphaType; |
| 378 | void* fColorSpace; |
| 379 | size_t fColorSpaceSize; |
| 380 | int fColorTableCnt; |
| 381 | uint32_t* fColorTableData; |
| 382 | int fMipMapLevelCount; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 383 | // The fMipMapLevelData array may contain more than 1 element. |
| 384 | // It contains fMipMapLevelCount elements. |
| 385 | // That means this struct's size is not known at compile-time. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 386 | MipMapLevelData fMipMapLevelData[1]; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 387 | }; |
| 388 | } // anonymous namespace |
| 389 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 390 | static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) { |
Eric Karl | a422d70 | 2016-11-30 11:09:29 -0800 | [diff] [blame] | 391 | // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData. |
| 392 | // This can cause runaway memory leaks, so we are disabling this path until we can |
| 393 | // investigate further. crbug.com/669775 |
| 394 | return false; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | namespace { |
| 398 | |
| 399 | class DTIBufferFiller |
| 400 | { |
| 401 | public: |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 402 | explicit DTIBufferFiller(char* bufferAsCharPtr) |
| 403 | : bufferAsCharPtr_(bufferAsCharPtr) {} |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 404 | |
| 405 | void fillMember(const void* source, size_t memberOffset, size_t size) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 406 | memcpy(bufferAsCharPtr_ + memberOffset, source, size); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | private: |
| 410 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 411 | char* bufferAsCharPtr_; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 412 | }; |
| 413 | } |
| 414 | |
| 415 | #define FILL_MEMBER(bufferFiller, member, source) \ |
| 416 | bufferFiller.fillMember(source, \ |
| 417 | offsetof(DeferredTextureImage, member), \ |
| 418 | sizeof(DeferredTextureImage::member)); |
| 419 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 420 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 421 | const DeferredTextureImageUsageParams params[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 422 | int paramCnt, void* buffer, |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 423 | SkColorSpace* dstColorSpace) const { |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 424 | // Extract relevant min/max values from the params array. |
| 425 | int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; |
| 426 | SkFilterQuality highestFilterQuality = params[0].fQuality; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 427 | bool useMipMaps = should_use_mip_maps(params[0]); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 428 | for (int i = 1; i < paramCnt; ++i) { |
| 429 | if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) |
| 430 | lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; |
| 431 | if (highestFilterQuality < params[i].fQuality) |
| 432 | highestFilterQuality = params[i].fQuality; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 433 | useMipMaps |= should_use_mip_maps(params[i]); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 434 | } |
| 435 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 436 | const bool fillMode = SkToBool(buffer); |
| 437 | if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 438 | return 0; |
| 439 | } |
| 440 | |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 441 | // Calculate scaling parameters. |
| 442 | bool isScaled = lowestPreScaleMipLevel != 0; |
| 443 | |
| 444 | SkISize scaledSize; |
| 445 | if (isScaled) { |
| 446 | // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the |
| 447 | // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level. |
| 448 | scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(), |
| 449 | lowestPreScaleMipLevel - 1); |
| 450 | } else { |
| 451 | scaledSize = SkISize::Make(this->width(), this->height()); |
| 452 | } |
| 453 | |
| 454 | // We never want to scale at higher than SW medium quality, as SW medium matches GPU high. |
| 455 | SkFilterQuality scaleFilterQuality = highestFilterQuality; |
| 456 | if (scaleFilterQuality > kMedium_SkFilterQuality) { |
| 457 | scaleFilterQuality = kMedium_SkFilterQuality; |
| 458 | } |
| 459 | |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 460 | const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 461 | if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) { |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 465 | SkAutoPixmapStorage pixmap; |
| 466 | SkImageInfo info; |
| 467 | size_t pixelSize = 0; |
| 468 | size_t ctSize = 0; |
| 469 | int ctCount = 0; |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 470 | if (!isScaled && this->peekPixels(&pixmap)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 471 | info = pixmap.info(); |
| 472 | pixelSize = SkAlign8(pixmap.getSafeSize()); |
| 473 | if (pixmap.ctable()) { |
| 474 | ctCount = pixmap.ctable()->count(); |
| 475 | ctSize = SkAlign8(pixmap.ctable()->count() * 4); |
| 476 | } |
| 477 | } else { |
| 478 | // Here we're just using presence of data to know whether there is a codec behind the image. |
| 479 | // In the future we will access the cacherator and get the exact data that we want to (e.g. |
| 480 | // yuv planes) upload. |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 481 | sk_sp<SkData> data(this->refEncoded()); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 482 | if (!data && !this->peekPixels(nullptr)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 483 | return 0; |
| 484 | } |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 485 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 486 | // Generator backed image. Tweak info to trigger correct kind of decode. |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 487 | SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 488 | dstColorSpace, proxy.fCaps.get()); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 489 | info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(), |
| 490 | scaledSize.height()); |
| 491 | |
| 492 | } else { |
| 493 | info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height()); |
| 494 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 495 | pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 496 | if (fillMode) { |
| 497 | pixmap.alloc(info); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 498 | if (isScaled) { |
| 499 | if (!this->scalePixels(pixmap, scaleFilterQuality, |
| 500 | SkImage::kDisallow_CachingHint)) { |
| 501 | return 0; |
| 502 | } |
| 503 | } else { |
| 504 | if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 505 | return 0; |
| 506 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 507 | } |
| 508 | SkASSERT(!pixmap.ctable()); |
| 509 | } |
| 510 | } |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 511 | int mipMapLevelCount = 1; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 512 | if (useMipMaps) { |
| 513 | // SkMipMap only deals with the mipmap levels it generates, which does |
| 514 | // not include the base level. |
| 515 | // That means it generates and holds levels 1-x instead of 0-x. |
| 516 | // So the total mipmap level count is 1 more than what |
| 517 | // SkMipMap::ComputeLevelCount returns. |
| 518 | mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1; |
| 519 | |
| 520 | // We already initialized pixelSize to the size of the base level. |
| 521 | // SkMipMap will generate the extra mipmap levels. Their sizes need to |
| 522 | // be added to the total. |
| 523 | // Index 0 here does not refer to the base mipmap level -- it is |
| 524 | // SkMipMap's first generated mipmap level (level 1). |
| 525 | for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0; |
| 526 | currentMipMapLevelIndex--) { |
| 527 | SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(), |
| 528 | currentMipMapLevelIndex); |
brianosman | 32b5e70 | 2016-10-13 06:44:23 -0700 | [diff] [blame] | 529 | SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 530 | pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr)); |
| 531 | } |
| 532 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 533 | size_t size = 0; |
| 534 | size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 535 | size += dtiSize; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 536 | size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData); |
| 537 | // We subtract 1 because DeferredTextureImage already includes the base |
| 538 | // level in its size |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 539 | size_t pixelOffset = size; |
| 540 | size += pixelSize; |
| 541 | size_t ctOffset = size; |
| 542 | size += ctSize; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 543 | size_t colorSpaceOffset = 0; |
| 544 | size_t colorSpaceSize = 0; |
| 545 | if (info.colorSpace()) { |
| 546 | colorSpaceOffset = size; |
| 547 | colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); |
| 548 | size += colorSpaceSize; |
| 549 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 550 | if (!fillMode) { |
| 551 | return size; |
| 552 | } |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 553 | char* bufferAsCharPtr = reinterpret_cast<char*>(buffer); |
| 554 | char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset; |
| 555 | void* pixels = pixelsAsCharPtr; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 556 | void* ct = nullptr; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 557 | if (ctSize) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 558 | ct = bufferAsCharPtr + ctOffset; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 559 | } |
| 560 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 561 | memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))), |
| 562 | pixmap.addr(), pixmap.getSafeSize()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 563 | if (ctSize) { |
| 564 | memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
| 565 | } |
| 566 | |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 567 | // If the context has sRGB support, and we're intending to render to a surface with an attached |
| 568 | // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB) |
| 569 | // aware mip-mapping. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 570 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 571 | if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) && |
| 572 | info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 573 | colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware; |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 574 | } |
| 575 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 576 | SkASSERT(info == pixmap.info()); |
| 577 | size_t rowBytes = pixmap.rowBytes(); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 578 | static_assert(std::is_standard_layout<DeferredTextureImage>::value, |
| 579 | "offsetof, which we use below, requires the type have standard layout"); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 580 | auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr}; |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 581 | FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 582 | FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID); |
| 583 | int width = info.width(); |
| 584 | FILL_MEMBER(dtiBufferFiller, fWidth, &width); |
| 585 | int height = info.height(); |
| 586 | FILL_MEMBER(dtiBufferFiller, fHeight, &height); |
| 587 | SkColorType colorType = info.colorType(); |
| 588 | FILL_MEMBER(dtiBufferFiller, fColorType, &colorType); |
| 589 | SkAlphaType alphaType = info.alphaType(); |
| 590 | FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType); |
| 591 | FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount); |
| 592 | FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct); |
| 593 | FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 594 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 595 | &pixels, sizeof(pixels)); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 596 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 597 | &rowBytes, sizeof(rowBytes)); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 598 | if (colorSpaceSize) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 599 | void* colorSpace = bufferAsCharPtr + colorSpaceOffset; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 600 | FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace); |
| 601 | FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 602 | info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 603 | } else { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 604 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 605 | 0, sizeof(DeferredTextureImage::fColorSpace)); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 606 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 607 | 0, sizeof(DeferredTextureImage::fColorSpaceSize)); |
| 608 | } |
| 609 | |
| 610 | // Fill in the mipmap levels if they exist |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 611 | char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 612 | |
| 613 | if (useMipMaps) { |
cblume | 94ddf54 | 2016-09-16 10:07:32 -0700 | [diff] [blame] | 614 | static_assert(std::is_standard_layout<MipMapLevelData>::value, |
| 615 | "offsetof, which we use below, requires the type have a standard layout"); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 616 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 617 | std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 618 | // SkMipMap holds only the mipmap levels it generates. |
| 619 | // A programmer can use the data they provided to SkMipMap::Build as level 0. |
| 620 | // So the SkMipMap provides levels 1-x but it stores them in its own |
| 621 | // range 0-(x-1). |
| 622 | for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1; |
| 623 | generatedMipLevelIndex++) { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 624 | SkMipMap::Level mipLevel; |
| 625 | mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); |
| 626 | |
| 627 | // Make sure the mipmap data is after the start of the buffer |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 628 | SkASSERT(mipLevelPtr > bufferAsCharPtr); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 629 | // Make sure the mipmap data starts before the end of the buffer |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 630 | SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 631 | // Make sure the mipmap data ends before the end of the buffer |
cblume | 12f7527 | 2016-09-19 06:18:03 -0700 | [diff] [blame] | 632 | SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <= |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 633 | bufferAsCharPtr + pixelOffset + pixelSize); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 634 | |
| 635 | // getSafeSize includes rowbyte padding except for the last row, |
| 636 | // right? |
| 637 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 638 | memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 639 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 640 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 641 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 642 | offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 643 | size_t rowBytes = mipLevel.fPixmap.rowBytes(); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 644 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 645 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 646 | offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 647 | |
| 648 | mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize()); |
| 649 | } |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 650 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 651 | return size; |
| 652 | } |
| 653 | |
| 654 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data, |
| 655 | SkBudgeted budgeted) { |
| 656 | if (!data) { |
| 657 | return nullptr; |
| 658 | } |
| 659 | const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data); |
| 660 | |
| 661 | if (!context || context->uniqueID() != dti->fContextUniqueID) { |
| 662 | return nullptr; |
| 663 | } |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 664 | sk_sp<SkColorTable> colorTable; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 665 | if (dti->fColorTableCnt) { |
| 666 | SkASSERT(dti->fColorTableData); |
| 667 | colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt)); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 668 | } |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 669 | int mipLevelCount = dti->fMipMapLevelCount; |
| 670 | SkASSERT(mipLevelCount >= 1); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 671 | sk_sp<SkColorSpace> colorSpace; |
| 672 | if (dti->fColorSpaceSize) { |
| 673 | colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize); |
| 674 | } |
| 675 | SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight, |
| 676 | dti->fColorType, dti->fAlphaType, colorSpace); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 677 | if (mipLevelCount == 1) { |
| 678 | SkPixmap pixmap; |
| 679 | pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, |
| 680 | dti->fMipMapLevelData[0].fRowBytes, colorTable.get()); |
| 681 | return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
| 682 | } else { |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 683 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 684 | for (int i = 0; i < mipLevelCount; i++) { |
| 685 | texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData; |
| 686 | texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes; |
| 687 | } |
| 688 | |
| 689 | return SkImage::MakeTextureFromMipMap(context, info, texels.get(), |
| 690 | mipLevelCount, SkBudgeted::kYes, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 691 | dti->fColorMode); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 692 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 696 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 697 | sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info, |
| 698 | const GrMipLevel* texels, int mipLevelCount, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 699 | SkBudgeted budgeted, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 700 | SkDestinationSurfaceColorMode colorMode) { |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 701 | if (!ctx) { |
| 702 | return nullptr; |
| 703 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 704 | sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount)); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 705 | if (!texture) { |
| 706 | return nullptr; |
| 707 | } |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 708 | texture->texturePriv().setMipColorMode(colorMode); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 709 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 710 | info.alphaType(), std::move(texture), |
| 711 | sk_ref_sp(info.colorSpace()), budgeted); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 712 | } |