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