blob: ced544723e40ff4364bb7067df7ab3556f423a31 [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +00001/*
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
cblume33e0cb52016-08-30 12:09:23 -07008#include <cstddef>
9#include <cstring>
10#include <type_traits>
11
robertphillipsc5035e72016-03-17 06:58:39 -070012#include "SkAutoPixmapStorage.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050013#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070014#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050016#include "GrContextPriv.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050017#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050018#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040019#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050020#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050021#include "GrSemaphore.h"
Brian Osmane8e54582016-11-28 10:06:27 -050022#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070023#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050024#include "GrTextureProxy.h"
bsalomonf267c1e2016-02-01 13:16:14 -080025#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070026#include "SkCanvas.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050027#include "SkCrossContextImageData.h"
reed262a71b2015-12-05 13:07:27 -080028#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070029#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080030#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050031#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050032#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070033#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070034#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050035#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070036
bungeman6bd52842016-10-27 09:30:08 -070037SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070038 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070039 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070040 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070041 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080042 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070043 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070044 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070045{
bungeman6bd52842016-10-27 09:30:08 -070046 SkASSERT(fTexture->width() == w);
47 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070048}
piotaixrcef04f82014-07-14 07:48:04 -070049
reed6f1216a2015-08-04 08:10:13 -070050SkImage_Gpu::~SkImage_Gpu() {
51 if (fAddedRasterVersionToCache.load()) {
52 SkNotifyBitmapGenIDIsStale(this->uniqueID());
53 }
54}
55
bsalomoneaaaf0b2015-01-23 08:08:04 -080056extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070057 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070058 ((SkImage_Gpu*)image)->applyBudgetDecision();
59 }
60}
61
brianosman396fcdb2016-07-22 06:26:11 -070062SkImageInfo SkImage_Gpu::onImageInfo() const {
63 SkColorType ct;
64 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
65 ct = kUnknown_SkColorType;
66 }
67 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
68}
69
brianosman69c166d2016-08-17 14:01:05 -070070static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
71 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070072}
73
Brian Osman61624f02016-12-09 14:51:59 -050074bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050075 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070076 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
77 SkASSERT(dst->getGenerationID() == this->uniqueID());
78 SkASSERT(dst->isImmutable());
79 SkASSERT(dst->getPixels());
80 return true;
81 }
82
brianosman69c166d2016-08-17 14:01:05 -070083 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070084 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070085 return false;
86 }
87 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
88 dst->getPixels(), dst->rowBytes())) {
89 return false;
90 }
reed6f1216a2015-08-04 08:10:13 -070091
92 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080093 if (kAllow_CachingHint == chint) {
94 SkBitmapCache::Add(this->uniqueID(), *dst);
95 fAddedRasterVersionToCache.store(true);
96 }
reed8b26b992015-05-07 15:36:17 -070097 return true;
98}
99
Robert Phillips6de99042017-01-31 11:31:39 -0500100sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef() const {
Robert Phillips63c67462017-02-15 14:19:01 -0500101 return GrSurfaceProxy::MakeWrapped(fTexture);
Robert Phillips2c862492017-01-18 10:08:39 -0500102}
103
Brian Salomon514baff2016-11-17 15:17:07 -0500104GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500105 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500106 sk_sp<SkColorSpace>* texColorSpace,
107 SkScalar scaleAdjust[2]) const {
Brian Osman7992da32016-11-18 11:28:24 -0500108 if (texColorSpace) {
109 *texColorSpace = this->fColorSpace;
110 }
Robert Phillips6de99042017-01-31 11:31:39 -0500111 GrTextureAdjuster adjuster(fTexture.get(), this->alphaType(), this->bounds(),
Brian Osman7992da32016-11-18 11:28:24 -0500112 this->uniqueID(), this->fColorSpace.get());
Robert Phillips67c18d62017-01-20 12:44:06 -0500113 return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700114}
115
reed8b26b992015-05-07 15:36:17 -0700116static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
117 switch (info.colorType()) {
118 case kRGBA_8888_SkColorType:
119 case kBGRA_8888_SkColorType:
120 break;
121 default:
122 return; // nothing to do
123 }
124
125 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
126 // and in either case, the alpha-byte is always in the same place, so we can safely call
127 // SkPreMultiplyColor()
128 //
129 SkColor* row = (SkColor*)pixels;
130 for (int y = 0; y < info.height(); ++y) {
131 for (int x = 0; x < info.width(); ++x) {
132 row[x] = SkPreMultiplyColor(row[x]);
133 }
134 }
135}
136
Matt Sarett03dd6d52017-01-23 12:15:09 -0500137bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800138 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500139 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500140 return false;
141 }
142
Matt Sarett03dd6d52017-01-23 12:15:09 -0500143 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
144 if (!rec.trim(this->width(), this->height())) {
145 return false;
146 }
147
148 GrPixelConfig config = SkImageInfo2GrPixelConfig(rec.fInfo, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700149 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500150 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700151 // let the GPU perform this transformation for us
152 flags = GrContext::kUnpremul_PixelOpsFlag;
153 }
Matt Sarett03dd6d52017-01-23 12:15:09 -0500154 if (!fTexture->readPixels(fColorSpace.get(), rec.fX, rec.fY, rec.fInfo.width(),
155 rec.fInfo.height(), config, rec.fInfo.colorSpace(), rec.fPixels,
156 rec.fRowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700157 return false;
158 }
159 // do we have to manually fix-up the alpha channel?
160 // src dst
161 // unpremul premul fix manually
162 // premul unpremul done by kUnpremul_PixelOpsFlag
163 // all other combos need to change.
164 //
165 // Should this be handled by Ganesh? todo:?
166 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500167 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
168 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700169 }
170 return true;
171}
172
reed7fb4f8b2016-03-11 04:33:52 -0800173sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700174 GrContext* ctx = fTexture->getContext();
175 GrSurfaceDesc desc = fTexture->desc();
176 desc.fWidth = subset.width();
177 desc.fHeight = subset.height();
178
Robert Phillipse2f7d182016-12-15 09:23:05 -0500179 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
180 desc,
181 SkBackingFit::kExact,
182 fBudgeted));
183 if (!sContext) {
184 return nullptr;
185 }
186
187 // TODO: make gpu images be proxy-backed so we don't need to do this
188 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
189 if (!tmpSrc) {
190 return nullptr;
191 }
192
193 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
194 return nullptr;
195 }
196
197 // TODO: make gpu images be proxy-backed so we don't need to do this
Brian Osman32342f02017-03-04 08:12:46 -0500198 GrSurface* subTx = sContext->asSurfaceProxy()->instantiate(ctx->resourceProvider());
reed7b6945b2015-09-24 00:50:58 -0700199 if (!subTx) {
200 return nullptr;
201 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500202
reed7fb4f8b2016-03-11 04:33:52 -0800203 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500204 fAlphaType, sk_ref_sp(subTx->asTexture()),
205 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700206}
207
reed8b26b992015-05-07 15:36:17 -0700208///////////////////////////////////////////////////////////////////////////////////////////////////
209
reed7fb4f8b2016-03-11 04:33:52 -0800210static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700211 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
212 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800213 SkImage::TextureReleaseProc releaseProc,
214 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700215 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700216 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700217 }
Brian Osman32342f02017-03-04 08:12:46 -0500218 sk_sp<GrTexture> tex = ctx->resourceProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700219 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700220 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700221 }
reedde499882015-06-18 13:41:40 -0700222 if (releaseProc) {
223 tex->setRelease(releaseProc, releaseCtx);
224 }
225
bsalomon5ec26ae2016-02-25 08:33:02 -0800226 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800227 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700228 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229}
230
reed7fb4f8b2016-03-11 04:33:52 -0800231sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700232 SkAlphaType at, sk_sp<SkColorSpace> cs,
233 TextureReleaseProc releaseP, ReleaseContext releaseC) {
234 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
235 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700236}
237
reed7fb4f8b2016-03-11 04:33:52 -0800238sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700239 SkAlphaType at, sk_sp<SkColorSpace> cs) {
240 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
241 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700242}
243
jbaumanb445a572016-06-09 13:24:48 -0700244static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
245 bool nv12,
246 const GrBackendObject yuvTextureHandles[],
247 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700248 GrSurfaceOrigin origin,
249 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800250 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700251
jbaumanb445a572016-06-09 13:24:48 -0700252 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
253 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700254 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700255 }
jbaumanb445a572016-06-09 13:24:48 -0700256 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
257 return nullptr;
258 }
259
260 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
261
bsalomon993a4212015-05-29 11:37:25 -0700262 GrBackendTextureDesc yDesc;
263 yDesc.fConfig = kConfig;
264 yDesc.fOrigin = origin;
265 yDesc.fSampleCnt = 0;
266 yDesc.fTextureHandle = yuvTextureHandles[0];
267 yDesc.fWidth = yuvSizes[0].fWidth;
268 yDesc.fHeight = yuvSizes[0].fHeight;
269
270 GrBackendTextureDesc uDesc;
271 uDesc.fConfig = kConfig;
272 uDesc.fOrigin = origin;
273 uDesc.fSampleCnt = 0;
274 uDesc.fTextureHandle = yuvTextureHandles[1];
275 uDesc.fWidth = yuvSizes[1].fWidth;
276 uDesc.fHeight = yuvSizes[1].fHeight;
277
Robert Phillips26caf892017-01-27 10:58:31 -0500278 sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yDesc);
279 sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uDesc);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500280 sk_sp<GrSurfaceProxy> vProxy;
281
jbaumanb445a572016-06-09 13:24:48 -0700282 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500283 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700284 } else {
285 GrBackendTextureDesc vDesc;
286 vDesc.fConfig = kConfig;
287 vDesc.fOrigin = origin;
288 vDesc.fSampleCnt = 0;
289 vDesc.fTextureHandle = yuvTextureHandles[2];
290 vDesc.fWidth = yuvSizes[2].fWidth;
291 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700292
Robert Phillips26caf892017-01-27 10:58:31 -0500293 vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vDesc);
jbaumanb445a572016-06-09 13:24:48 -0700294 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500295 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700297 }
298
robertphillipsd4c741e2016-04-28 09:55:15 -0700299 const int width = yuvSizes[0].fWidth;
300 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700301
robertphillipsd4c741e2016-04-28 09:55:15 -0700302 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400303 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
304 SkBackingFit::kExact,
305 width, height,
306 kRGBA_8888_GrPixelConfig,
307 std::move(imageColorSpace),
308 0,
309 origin));
310 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700312 }
313
314 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400315 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700316 paint.addColorFragmentProcessor(
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500317 GrYUVEffect::MakeYUVToRGB(ctx,
318 sk_ref_sp(yProxy->asTextureProxy()),
319 sk_ref_sp(uProxy->asTextureProxy()),
320 sk_ref_sp(vProxy->asTextureProxy()), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700321
robertphillipsd4c741e2016-04-28 09:55:15 -0700322 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700323
Brian Salomon82f44312017-01-11 13:42:54 -0500324 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500325
326 if (!renderTargetContext->accessRenderTarget()) {
327 return nullptr;
328 }
Brian Osman11052242016-10-27 14:47:55 -0400329 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700330 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400331 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500332 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700333}
reed56179002015-07-07 06:11:19 -0700334
jbaumanb445a572016-06-09 13:24:48 -0700335sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
336 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700337 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
338 sk_sp<SkColorSpace> imageColorSpace) {
339 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
340 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700341}
342
343sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
344 const GrBackendObject yuvTextureHandles[2],
345 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700346 GrSurfaceOrigin origin,
347 sk_sp<SkColorSpace> imageColorSpace) {
348 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
349 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700350}
351
Brian Osman041f7df2017-02-07 11:23:28 -0500352static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id,
353 SkColorSpace* dstColorSpace) {
354 sk_sp<SkColorSpace> texColorSpace;
355 sk_sp<GrTexture> texture(maker->refTextureForParams(GrSamplerParams::ClampNoFilter(),
356 dstColorSpace, &texColorSpace, nullptr));
357 if (!texture) {
358 return nullptr;
359 }
360 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture),
361 std::move(texColorSpace), SkBudgeted::kNo);
362}
363
Brian Osman2c2bc112017-02-28 10:02:49 -0500364sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500365 if (!context) {
366 return nullptr;
367 }
368 if (GrTexture* peek = as_IB(this)->peekTexture()) {
369 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
370 }
371
372 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
373 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
374 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace);
375 }
376
377 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
378 GrBitmapTextureMaker maker(context, *bmp);
379 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace);
380 }
381 return nullptr;
382}
383
Brian Osman2c2bc112017-02-28 10:02:49 -0500384std::unique_ptr<SkCrossContextImageData> SkCrossContextImageData::MakeFromEncoded(
385 GrContext* context, sk_sp<SkData> encoded, SkColorSpace* dstColorSpace) {
386 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
387 if (!codecImage) {
388 return nullptr;
389 }
390
391 // Some backends or drivers don't support (safely) moving resources between contexts
392 if (!context->caps()->crossContextTextureSupport()) {
393 return std::unique_ptr<SkCrossContextImageData>(
394 new SkCrossContextImageData(std::move(codecImage)));
395 }
396
397 sk_sp<SkImage> textureImage = codecImage->makeTextureImage(context, dstColorSpace);
398 if (!textureImage) {
399 // TODO: Force decode to raster here? Do mip-mapping, like getDeferredTextureImageData?
400 return std::unique_ptr<SkCrossContextImageData>(
401 new SkCrossContextImageData(std::move(codecImage)));
402 }
403
404 // Crack open the gpu image, extract the backend data, stick it in the SkCCID
405 GrTexture* texture = as_IB(textureImage)->peekTexture();
406 SkASSERT(texture);
407
408 GrBackendTextureDesc desc;
409 desc.fFlags = kNone_GrBackendTextureFlag;
410 desc.fOrigin = texture->origin();
411 desc.fWidth = texture->width();
412 desc.fHeight = texture->height();
413 desc.fConfig = texture->config();
414 desc.fSampleCnt = 0;
415
416 auto textureData = texture->texturePriv().detachBackendTexture();
417 SkASSERT(textureData);
418
419 SkImageInfo info = as_IB(textureImage)->onImageInfo();
420 return std::unique_ptr<SkCrossContextImageData>(new SkCrossContextImageData(
421 desc, std::move(textureData), info.alphaType(), info.refColorSpace()));
422}
423
424sk_sp<SkImage> SkImage::MakeFromCrossContextImageData(
425 GrContext* context, std::unique_ptr<SkCrossContextImageData> ccid) {
426 if (ccid->fImage) {
427 // No pre-existing GPU resource. We could upload it now (with makeTextureImage),
428 // but we'd need a dstColorSpace.
429 return ccid->fImage;
430 }
431
432 if (ccid->fTextureData) {
Brian Osmanfe3b5162017-03-02 15:09:20 -0500433 ccid->fTextureData->attachToContext(context);
Brian Osman2c2bc112017-02-28 10:02:49 -0500434 }
435
436 return MakeFromAdoptedTexture(context, ccid->fDesc, ccid->fAlphaType,
437 std::move(ccid->fColorSpace));
438}
439
bsalomon634b4302016-07-12 18:11:17 -0700440sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700441 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700442 return sk_ref_sp(const_cast<SkImage*>(this));
443 }
brianosman396fcdb2016-07-22 06:26:11 -0700444 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700445 size_t rowBytes = info.minRowBytes();
446 size_t size = info.getSafeSize(rowBytes);
447 auto data = SkData::MakeUninitialized(size);
448 if (!data) {
449 return nullptr;
450 }
451 SkPixmap pm(info, data->writable_data(), rowBytes);
452 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
453 return nullptr;
454 }
455 return MakeRasterData(info, data, rowBytes);
456}
457
Brian Osmanb92234a2017-01-25 14:13:00 +0000458sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
459 SkBudgeted budgeted) {
460 if (!ctx) {
461 return nullptr;
462 }
463 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
464 if (!texture) {
465 return nullptr;
466 }
467 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
468 pixmap.alphaType(), std::move(texture),
469 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
470}
471
reed56179002015-07-07 06:11:19 -0700472///////////////////////////////////////////////////////////////////////////////////////////////////
473
bsalomon4d516a62016-07-28 13:37:31 -0700474namespace {
475struct MipMapLevelData {
476 void* fPixelData;
477 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800478};
479
bsalomon4d516a62016-07-28 13:37:31 -0700480struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500481 uint32_t fContextUniqueID;
482 // Right now, the destination color mode is only considered when generating mipmaps
483 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700484 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500485 int fWidth;
486 int fHeight;
487 SkColorType fColorType;
488 SkAlphaType fAlphaType;
489 void* fColorSpace;
490 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500491 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700492 // The fMipMapLevelData array may contain more than 1 element.
493 // It contains fMipMapLevelCount elements.
494 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500495 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700496};
497} // anonymous namespace
498
cblume33e0cb52016-08-30 12:09:23 -0700499static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800500 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
501 // This can cause runaway memory leaks, so we are disabling this path until we can
502 // investigate further. crbug.com/669775
503 return false;
cblume33e0cb52016-08-30 12:09:23 -0700504}
505
506namespace {
507
508class DTIBufferFiller
509{
510public:
cblume921bc672016-09-22 05:25:26 -0700511 explicit DTIBufferFiller(char* bufferAsCharPtr)
512 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700513
514 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700515 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700516 }
517
518private:
519
cblume921bc672016-09-22 05:25:26 -0700520 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700521};
522}
523
524#define FILL_MEMBER(bufferFiller, member, source) \
525 bufferFiller.fillMember(source, \
526 offsetof(DeferredTextureImage, member), \
527 sizeof(DeferredTextureImage::member));
528
bsalomon41b952c2016-03-11 06:46:33 -0800529size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700530 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700531 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400532 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700533 // Extract relevant min/max values from the params array.
534 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
535 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700536 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700537 for (int i = 1; i < paramCnt; ++i) {
538 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
539 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
540 if (highestFilterQuality < params[i].fQuality)
541 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700542 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700543 }
544
bsalomon41b952c2016-03-11 06:46:33 -0800545 const bool fillMode = SkToBool(buffer);
546 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
547 return 0;
548 }
549
ericrkb4da01d2016-06-13 11:18:14 -0700550 // Calculate scaling parameters.
551 bool isScaled = lowestPreScaleMipLevel != 0;
552
553 SkISize scaledSize;
554 if (isScaled) {
555 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
556 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
557 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
558 lowestPreScaleMipLevel - 1);
559 } else {
560 scaledSize = SkISize::Make(this->width(), this->height());
561 }
562
563 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
564 SkFilterQuality scaleFilterQuality = highestFilterQuality;
565 if (scaleFilterQuality > kMedium_SkFilterQuality) {
566 scaleFilterQuality = kMedium_SkFilterQuality;
567 }
568
ericrkc429baf2016-03-24 15:35:45 -0700569 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700570 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700571 return 0;
572 }
573
bsalomon41b952c2016-03-11 06:46:33 -0800574 SkAutoPixmapStorage pixmap;
575 SkImageInfo info;
576 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500577 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800578 info = pixmap.info();
579 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800580 } else {
581 // Here we're just using presence of data to know whether there is a codec behind the image.
582 // In the future we will access the cacherator and get the exact data that we want to (e.g.
583 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700584 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700585 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800586 return 0;
587 }
Brian Osman7992da32016-11-18 11:28:24 -0500588 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
589 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500590 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500591 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500592 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
593 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500594 } else {
595 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
596 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500597 if (kIndex_8_SkColorType == info.colorType()) {
598 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
599 info = info.makeColorType(kN32_SkColorType);
600 }
bsalomon41b952c2016-03-11 06:46:33 -0800601 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
602 if (fillMode) {
603 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700604 if (isScaled) {
605 if (!this->scalePixels(pixmap, scaleFilterQuality,
606 SkImage::kDisallow_CachingHint)) {
607 return 0;
608 }
609 } else {
610 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
611 return 0;
612 }
bsalomon41b952c2016-03-11 06:46:33 -0800613 }
614 SkASSERT(!pixmap.ctable());
615 }
616 }
cblume2c052802016-05-31 09:55:08 -0700617 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700618 if (useMipMaps) {
619 // SkMipMap only deals with the mipmap levels it generates, which does
620 // not include the base level.
621 // That means it generates and holds levels 1-x instead of 0-x.
622 // So the total mipmap level count is 1 more than what
623 // SkMipMap::ComputeLevelCount returns.
624 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
625
626 // We already initialized pixelSize to the size of the base level.
627 // SkMipMap will generate the extra mipmap levels. Their sizes need to
628 // be added to the total.
629 // Index 0 here does not refer to the base mipmap level -- it is
630 // SkMipMap's first generated mipmap level (level 1).
631 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
632 currentMipMapLevelIndex--) {
633 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
634 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700635 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700636 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
637 }
638 }
bsalomon41b952c2016-03-11 06:46:33 -0800639 size_t size = 0;
640 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
641 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700642 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
643 // We subtract 1 because DeferredTextureImage already includes the base
644 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800645 size_t pixelOffset = size;
646 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700647 size_t colorSpaceOffset = 0;
648 size_t colorSpaceSize = 0;
649 if (info.colorSpace()) {
650 colorSpaceOffset = size;
651 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
652 size += colorSpaceSize;
653 }
bsalomon41b952c2016-03-11 06:46:33 -0800654 if (!fillMode) {
655 return size;
656 }
cblume921bc672016-09-22 05:25:26 -0700657 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
658 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
659 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800660
cblume921bc672016-09-22 05:25:26 -0700661 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
662 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800663
Brian Osman6c15cc72016-10-17 09:51:37 -0400664 // If the context has sRGB support, and we're intending to render to a surface with an attached
665 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
666 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500667 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400668 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
669 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500670 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400671 }
672
bsalomon41b952c2016-03-11 06:46:33 -0800673 SkASSERT(info == pixmap.info());
674 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700675 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
676 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700677 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500678 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700679 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
680 int width = info.width();
681 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
682 int height = info.height();
683 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
684 SkColorType colorType = info.colorType();
685 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
686 SkAlphaType alphaType = info.alphaType();
687 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700688 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700689 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700690 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700691 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700692 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700693 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700694 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700695 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
696 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700697 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700698 } else {
cblume921bc672016-09-22 05:25:26 -0700699 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700700 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700701 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700702 0, sizeof(DeferredTextureImage::fColorSpaceSize));
703 }
704
705 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700706 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700707
708 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700709 static_assert(std::is_standard_layout<MipMapLevelData>::value,
710 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700711
Brian Osman7b8400d2016-11-08 17:08:54 -0500712 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700713 // SkMipMap holds only the mipmap levels it generates.
714 // A programmer can use the data they provided to SkMipMap::Build as level 0.
715 // So the SkMipMap provides levels 1-x but it stores them in its own
716 // range 0-(x-1).
717 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
718 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700719 SkMipMap::Level mipLevel;
720 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
721
722 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700723 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700724 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700725 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700726 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700727 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700728 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700729
730 // getSafeSize includes rowbyte padding except for the last row,
731 // right?
732
cblume921bc672016-09-22 05:25:26 -0700733 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700734
cblume921bc672016-09-22 05:25:26 -0700735 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
736 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
737 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700738 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700739 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
740 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
741 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700742
743 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
744 }
bsalomon4d516a62016-07-28 13:37:31 -0700745 }
bsalomon41b952c2016-03-11 06:46:33 -0800746 return size;
747}
748
749sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
750 SkBudgeted budgeted) {
751 if (!data) {
752 return nullptr;
753 }
754 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
755
756 if (!context || context->uniqueID() != dti->fContextUniqueID) {
757 return nullptr;
758 }
cblume33e0cb52016-08-30 12:09:23 -0700759 int mipLevelCount = dti->fMipMapLevelCount;
760 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700761 sk_sp<SkColorSpace> colorSpace;
762 if (dti->fColorSpaceSize) {
763 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
764 }
765 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
766 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000767 if (mipLevelCount == 1) {
768 SkPixmap pixmap;
769 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
770 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
771 } else {
772 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
773 for (int i = 0; i < mipLevelCount; i++) {
774 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
775 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
776 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500777
Brian Osmanb92234a2017-01-25 14:13:00 +0000778 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
779 mipLevelCount, SkBudgeted::kYes,
780 dti->fColorMode);
781 }
bsalomon41b952c2016-03-11 06:46:33 -0800782}
783
784///////////////////////////////////////////////////////////////////////////////////////////////////
785
cblume186d2d42016-06-03 11:17:42 -0700786sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
787 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700788 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500789 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700790 if (!ctx) {
791 return nullptr;
792 }
bungeman6bd52842016-10-27 09:30:08 -0700793 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700794 if (!texture) {
795 return nullptr;
796 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500797 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700798 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700799 info.alphaType(), std::move(texture),
800 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700801}