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