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