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" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 15 | #include "GrDrawContext.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 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 26 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, 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) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 29 | , fTexture(SkRef(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 | { |
| 35 | SkASSERT(tex->width() == w); |
| 36 | SkASSERT(tex->height() == h); |
| 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, |
| 89 | SkSourceGammaTreatment gammaTreatment) 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()); |
| 92 | return adjuster.refTextureSafeForParams(params, gammaTreatment, 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 | } |
senorblanco | 8775112 | 2016-05-20 07:27:38 -0700 | [diff] [blame] | 152 | ctx->copySurface(subTx.get(), fTexture, 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, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 154 | fAlphaType, subTx.get(), 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 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 167 | SkAutoTUnref<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, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 177 | at, tex, 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. |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 254 | sk_sp<GrDrawContext> drawContext(ctx->makeDrawContext(SkBackingFit::kExact, |
| 255 | width, height, |
| 256 | kRGBA_8888_GrPixelConfig, |
| 257 | std::move(imageColorSpace), |
| 258 | 0, |
| 259 | origin)); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 260 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 261 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | GrPaint paint; |
| 265 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 266 | paint.addColorFragmentProcessor( |
| 267 | GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 268 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 269 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 270 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 271 | drawContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 272 | ctx->flushSurfaceWrites(drawContext->accessRenderTarget()); |
| 273 | return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 274 | kOpaque_SkAlphaType, drawContext->asTexture().get(), |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 275 | sk_ref_sp(drawContext->getColorSpace()), budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 276 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 277 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 278 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 279 | const GrBackendObject yuvTextureHandles[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 280 | const SkISize yuvSizes[3], GrSurfaceOrigin origin, |
| 281 | sk_sp<SkColorSpace> imageColorSpace) { |
| 282 | return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin, |
| 283 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 287 | const GrBackendObject yuvTextureHandles[2], |
| 288 | const SkISize yuvSizes[2], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 289 | GrSurfaceOrigin origin, |
| 290 | sk_sp<SkColorSpace> imageColorSpace) { |
| 291 | return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin, |
| 292 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 293 | } |
| 294 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 295 | static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) { |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 296 | SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter(), |
| 297 | SkSourceGammaTreatment::kRespect)); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 298 | if (!texture) { |
| 299 | return nullptr; |
| 300 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 301 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 302 | sk_ref_sp(maker->getColorSpace()), SkBudgeted::kNo); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 303 | } |
| 304 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 305 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 306 | if (!context) { |
| 307 | return nullptr; |
| 308 | } |
| 309 | if (GrTexture* peek = as_IB(this)->peekTexture()) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 310 | return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr; |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 311 | } |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 312 | |
| 313 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 314 | GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 315 | return create_image_from_maker(&maker, this->alphaType(), this->uniqueID()); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 316 | } |
reed | 9a5a201 | 2016-08-08 09:00:29 -0700 | [diff] [blame] | 317 | |
| 318 | if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) { |
| 319 | GrBitmapTextureMaker maker(context, *bmp); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 320 | return create_image_from_maker(&maker, this->alphaType(), this->uniqueID()); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 321 | } |
reed | 9a5a201 | 2016-08-08 09:00:29 -0700 | [diff] [blame] | 322 | return nullptr; |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 325 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 326 | if (!this->isTextureBacked()) { |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 327 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 328 | } |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 329 | SkImageInfo info = as_IB(this)->onImageInfo(); |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 330 | size_t rowBytes = info.minRowBytes(); |
| 331 | size_t size = info.getSafeSize(rowBytes); |
| 332 | auto data = SkData::MakeUninitialized(size); |
| 333 | if (!data) { |
| 334 | return nullptr; |
| 335 | } |
| 336 | SkPixmap pm(info, data->writable_data(), rowBytes); |
| 337 | if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) { |
| 338 | return nullptr; |
| 339 | } |
| 340 | return MakeRasterData(info, data, rowBytes); |
| 341 | } |
| 342 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 343 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap, |
| 344 | SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 345 | if (!ctx) { |
| 346 | return nullptr; |
| 347 | } |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 348 | SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted)); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 349 | if (!texture) { |
| 350 | return nullptr; |
| 351 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 352 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 353 | pixmap.alphaType(), texture, |
| 354 | sk_ref_sp(pixmap.info().colorSpace()), budgeted); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 355 | } |
| 356 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 357 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 358 | |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 359 | namespace { |
| 360 | struct MipMapLevelData { |
| 361 | void* fPixelData; |
| 362 | size_t fRowBytes; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 365 | struct DeferredTextureImage { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 366 | uint32_t fContextUniqueID; |
| 367 | // Right now, the gamma treatment is only considered when generating mipmaps |
| 368 | SkSourceGammaTreatment fGammaTreatment; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 369 | // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace. |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 370 | int fWidth; |
| 371 | int fHeight; |
| 372 | SkColorType fColorType; |
| 373 | SkAlphaType fAlphaType; |
| 374 | void* fColorSpace; |
| 375 | size_t fColorSpaceSize; |
| 376 | int fColorTableCnt; |
| 377 | uint32_t* fColorTableData; |
| 378 | int fMipMapLevelCount; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 379 | // The fMipMapLevelData array may contain more than 1 element. |
| 380 | // It contains fMipMapLevelCount elements. |
| 381 | // That means this struct's size is not known at compile-time. |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 382 | MipMapLevelData fMipMapLevelData[1]; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 383 | }; |
| 384 | } // anonymous namespace |
| 385 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 386 | static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) { |
| 387 | bool shouldUseMipMaps = false; |
| 388 | |
| 389 | // Use mipmaps if either |
| 390 | // 1.) it is a perspective matrix, or |
| 391 | // 2.) the quality is med/high and the scale is < 1 |
| 392 | if (param.fMatrix.hasPerspective()) { |
| 393 | shouldUseMipMaps = true; |
| 394 | } |
| 395 | if (param.fQuality == kMedium_SkFilterQuality || |
| 396 | param.fQuality == kHigh_SkFilterQuality) { |
| 397 | SkScalar minAxisScale = param.fMatrix.getMinScale(); |
| 398 | if (minAxisScale != -1.f && minAxisScale < 1.f) { |
| 399 | shouldUseMipMaps = true; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
| 404 | return shouldUseMipMaps; |
| 405 | } |
| 406 | |
| 407 | namespace { |
| 408 | |
| 409 | class DTIBufferFiller |
| 410 | { |
| 411 | public: |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 412 | explicit DTIBufferFiller(char* bufferAsCharPtr) |
| 413 | : bufferAsCharPtr_(bufferAsCharPtr) {} |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 414 | |
| 415 | void fillMember(const void* source, size_t memberOffset, size_t size) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 416 | memcpy(bufferAsCharPtr_ + memberOffset, source, size); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | private: |
| 420 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 421 | char* bufferAsCharPtr_; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 422 | }; |
| 423 | } |
| 424 | |
| 425 | #define FILL_MEMBER(bufferFiller, member, source) \ |
| 426 | bufferFiller.fillMember(source, \ |
| 427 | offsetof(DeferredTextureImage, member), \ |
| 428 | sizeof(DeferredTextureImage::member)); |
| 429 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 430 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 431 | const DeferredTextureImageUsageParams params[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 432 | int paramCnt, void* buffer, |
| 433 | SkSourceGammaTreatment gammaTreatment) const { |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 434 | // Extract relevant min/max values from the params array. |
| 435 | int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; |
| 436 | SkFilterQuality highestFilterQuality = params[0].fQuality; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 437 | bool useMipMaps = should_use_mip_maps(params[0]); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 438 | for (int i = 1; i < paramCnt; ++i) { |
| 439 | if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) |
| 440 | lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; |
| 441 | if (highestFilterQuality < params[i].fQuality) |
| 442 | highestFilterQuality = params[i].fQuality; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 443 | useMipMaps |= should_use_mip_maps(params[i]); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 444 | } |
| 445 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 446 | const bool fillMode = SkToBool(buffer); |
| 447 | if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 448 | return 0; |
| 449 | } |
| 450 | |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 451 | // Calculate scaling parameters. |
| 452 | bool isScaled = lowestPreScaleMipLevel != 0; |
| 453 | |
| 454 | SkISize scaledSize; |
| 455 | if (isScaled) { |
| 456 | // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the |
| 457 | // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level. |
| 458 | scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(), |
| 459 | lowestPreScaleMipLevel - 1); |
| 460 | } else { |
| 461 | scaledSize = SkISize::Make(this->width(), this->height()); |
| 462 | } |
| 463 | |
| 464 | // We never want to scale at higher than SW medium quality, as SW medium matches GPU high. |
| 465 | SkFilterQuality scaleFilterQuality = highestFilterQuality; |
| 466 | if (scaleFilterQuality > kMedium_SkFilterQuality) { |
| 467 | scaleFilterQuality = kMedium_SkFilterQuality; |
| 468 | } |
| 469 | |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 470 | const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 471 | if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) { |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 475 | SkAutoPixmapStorage pixmap; |
| 476 | SkImageInfo info; |
| 477 | size_t pixelSize = 0; |
| 478 | size_t ctSize = 0; |
| 479 | int ctCount = 0; |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 480 | if (!isScaled && this->peekPixels(&pixmap)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 481 | info = pixmap.info(); |
| 482 | pixelSize = SkAlign8(pixmap.getSafeSize()); |
| 483 | if (pixmap.ctable()) { |
| 484 | ctCount = pixmap.ctable()->count(); |
| 485 | ctSize = SkAlign8(pixmap.ctable()->count() * 4); |
| 486 | } |
| 487 | } else { |
| 488 | // Here we're just using presence of data to know whether there is a codec behind the image. |
| 489 | // In the future we will access the cacherator and get the exact data that we want to (e.g. |
| 490 | // yuv planes) upload. |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 491 | sk_sp<SkData> data(this->refEncoded()); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 492 | if (!data && !this->peekPixels(nullptr)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 493 | return 0; |
| 494 | } |
brianosman | 32b5e70 | 2016-10-13 06:44:23 -0700 | [diff] [blame^] | 495 | info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 496 | pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 497 | if (fillMode) { |
| 498 | pixmap.alloc(info); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 499 | if (isScaled) { |
| 500 | if (!this->scalePixels(pixmap, scaleFilterQuality, |
| 501 | SkImage::kDisallow_CachingHint)) { |
| 502 | return 0; |
| 503 | } |
| 504 | } else { |
| 505 | if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 506 | return 0; |
| 507 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 508 | } |
| 509 | SkASSERT(!pixmap.ctable()); |
| 510 | } |
| 511 | } |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 512 | int mipMapLevelCount = 1; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 513 | if (useMipMaps) { |
| 514 | // SkMipMap only deals with the mipmap levels it generates, which does |
| 515 | // not include the base level. |
| 516 | // That means it generates and holds levels 1-x instead of 0-x. |
| 517 | // So the total mipmap level count is 1 more than what |
| 518 | // SkMipMap::ComputeLevelCount returns. |
| 519 | mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1; |
| 520 | |
| 521 | // We already initialized pixelSize to the size of the base level. |
| 522 | // SkMipMap will generate the extra mipmap levels. Their sizes need to |
| 523 | // be added to the total. |
| 524 | // Index 0 here does not refer to the base mipmap level -- it is |
| 525 | // SkMipMap's first generated mipmap level (level 1). |
| 526 | for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0; |
| 527 | currentMipMapLevelIndex--) { |
| 528 | SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(), |
| 529 | currentMipMapLevelIndex); |
brianosman | 32b5e70 | 2016-10-13 06:44:23 -0700 | [diff] [blame^] | 530 | SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 531 | pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr)); |
| 532 | } |
| 533 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 534 | size_t size = 0; |
| 535 | size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 536 | size += dtiSize; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 537 | size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData); |
| 538 | // We subtract 1 because DeferredTextureImage already includes the base |
| 539 | // level in its size |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 540 | size_t pixelOffset = size; |
| 541 | size += pixelSize; |
| 542 | size_t ctOffset = size; |
| 543 | size += ctSize; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 544 | size_t colorSpaceOffset = 0; |
| 545 | size_t colorSpaceSize = 0; |
| 546 | if (info.colorSpace()) { |
| 547 | colorSpaceOffset = size; |
| 548 | colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); |
| 549 | size += colorSpaceSize; |
| 550 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 551 | if (!fillMode) { |
| 552 | return size; |
| 553 | } |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 554 | char* bufferAsCharPtr = reinterpret_cast<char*>(buffer); |
| 555 | char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset; |
| 556 | void* pixels = pixelsAsCharPtr; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 557 | void* ct = nullptr; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 558 | if (ctSize) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 559 | ct = bufferAsCharPtr + ctOffset; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 560 | } |
| 561 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 562 | memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))), |
| 563 | pixmap.addr(), pixmap.getSafeSize()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 564 | if (ctSize) { |
| 565 | memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
| 566 | } |
| 567 | |
| 568 | SkASSERT(info == pixmap.info()); |
| 569 | size_t rowBytes = pixmap.rowBytes(); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 570 | static_assert(std::is_standard_layout<DeferredTextureImage>::value, |
| 571 | "offsetof, which we use below, requires the type have standard layout"); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 572 | auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr}; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 573 | FILL_MEMBER(dtiBufferFiller, fGammaTreatment, &gammaTreatment); |
| 574 | FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID); |
| 575 | int width = info.width(); |
| 576 | FILL_MEMBER(dtiBufferFiller, fWidth, &width); |
| 577 | int height = info.height(); |
| 578 | FILL_MEMBER(dtiBufferFiller, fHeight, &height); |
| 579 | SkColorType colorType = info.colorType(); |
| 580 | FILL_MEMBER(dtiBufferFiller, fColorType, &colorType); |
| 581 | SkAlphaType alphaType = info.alphaType(); |
| 582 | FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType); |
| 583 | FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount); |
| 584 | FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct); |
| 585 | FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 586 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 587 | &pixels, sizeof(pixels)); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 588 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 589 | &rowBytes, sizeof(rowBytes)); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 590 | if (colorSpaceSize) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 591 | void* colorSpace = bufferAsCharPtr + colorSpaceOffset; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 592 | FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace); |
| 593 | FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 594 | info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 595 | } else { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 596 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 597 | 0, sizeof(DeferredTextureImage::fColorSpace)); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 598 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 599 | 0, sizeof(DeferredTextureImage::fColorSpaceSize)); |
| 600 | } |
| 601 | |
| 602 | // Fill in the mipmap levels if they exist |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 603 | char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 604 | |
| 605 | if (useMipMaps) { |
cblume | 94ddf54 | 2016-09-16 10:07:32 -0700 | [diff] [blame] | 606 | static_assert(std::is_standard_layout<MipMapLevelData>::value, |
| 607 | "offsetof, which we use below, requires the type have a standard layout"); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 608 | |
| 609 | SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment, nullptr)); |
| 610 | // SkMipMap holds only the mipmap levels it generates. |
| 611 | // A programmer can use the data they provided to SkMipMap::Build as level 0. |
| 612 | // So the SkMipMap provides levels 1-x but it stores them in its own |
| 613 | // range 0-(x-1). |
| 614 | for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1; |
| 615 | generatedMipLevelIndex++) { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 616 | SkMipMap::Level mipLevel; |
| 617 | mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); |
| 618 | |
| 619 | // Make sure the mipmap data is after the start of the buffer |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 620 | SkASSERT(mipLevelPtr > bufferAsCharPtr); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 621 | // Make sure the mipmap data starts before the end of the buffer |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 622 | SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 623 | // Make sure the mipmap data ends before the end of the buffer |
cblume | 12f7527 | 2016-09-19 06:18:03 -0700 | [diff] [blame] | 624 | SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <= |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 625 | bufferAsCharPtr + pixelOffset + pixelSize); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 626 | |
| 627 | // getSafeSize includes rowbyte padding except for the last row, |
| 628 | // right? |
| 629 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 630 | memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 631 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 632 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 633 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 634 | offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 635 | size_t rowBytes = mipLevel.fPixmap.rowBytes(); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 636 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 637 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 638 | offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 639 | |
| 640 | mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize()); |
| 641 | } |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 642 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 643 | return size; |
| 644 | } |
| 645 | |
| 646 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data, |
| 647 | SkBudgeted budgeted) { |
| 648 | if (!data) { |
| 649 | return nullptr; |
| 650 | } |
| 651 | const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data); |
| 652 | |
| 653 | if (!context || context->uniqueID() != dti->fContextUniqueID) { |
| 654 | return nullptr; |
| 655 | } |
| 656 | SkAutoTUnref<SkColorTable> colorTable; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 657 | if (dti->fColorTableCnt) { |
| 658 | SkASSERT(dti->fColorTableData); |
| 659 | colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt)); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 660 | } |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 661 | int mipLevelCount = dti->fMipMapLevelCount; |
| 662 | SkASSERT(mipLevelCount >= 1); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 663 | sk_sp<SkColorSpace> colorSpace; |
| 664 | if (dti->fColorSpaceSize) { |
| 665 | colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize); |
| 666 | } |
| 667 | SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight, |
| 668 | dti->fColorType, dti->fAlphaType, colorSpace); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 669 | if (mipLevelCount == 1) { |
| 670 | SkPixmap pixmap; |
| 671 | pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, |
| 672 | dti->fMipMapLevelData[0].fRowBytes, colorTable.get()); |
| 673 | return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
| 674 | } else { |
| 675 | SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]); |
| 676 | for (int i = 0; i < mipLevelCount; i++) { |
| 677 | texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData; |
| 678 | texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes; |
| 679 | } |
| 680 | |
| 681 | return SkImage::MakeTextureFromMipMap(context, info, texels.get(), |
| 682 | mipLevelCount, SkBudgeted::kYes, |
| 683 | dti->fGammaTreatment); |
| 684 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 688 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 689 | sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info, |
| 690 | const GrMipLevel* texels, int mipLevelCount, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 691 | SkBudgeted budgeted, |
| 692 | SkSourceGammaTreatment gammaTreatment) { |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 693 | if (!ctx) { |
| 694 | return nullptr; |
| 695 | } |
| 696 | SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount)); |
| 697 | if (!texture) { |
| 698 | return nullptr; |
| 699 | } |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 700 | texture->texturePriv().setGammaTreatment(gammaTreatment); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 701 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 702 | info.alphaType(), texture, sk_ref_sp(info.colorSpace()), |
| 703 | budgeted); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 704 | } |