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