blob: ec1968f5105e30e61025505ddc591184e8cf4d44 [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 Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
Brian Osmane8e54582016-11-28 10:06:27 -050019#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070020#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050021#include "GrTextureProxy.h"
bsalomonf267c1e2016-02-01 13:16:14 -080022#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070023#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080024#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070025#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080026#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050027#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050028#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070029#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070030#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050031#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070032
bungeman6bd52842016-10-27 09:30:08 -070033SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070034 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070035 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070036 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070037 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080038 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070039 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070040 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070041{
bungeman6bd52842016-10-27 09:30:08 -070042 SkASSERT(fTexture->width() == w);
43 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070044}
piotaixrcef04f82014-07-14 07:48:04 -070045
reed6f1216a2015-08-04 08:10:13 -070046SkImage_Gpu::~SkImage_Gpu() {
47 if (fAddedRasterVersionToCache.load()) {
48 SkNotifyBitmapGenIDIsStale(this->uniqueID());
49 }
50}
51
bsalomoneaaaf0b2015-01-23 08:08:04 -080052extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070053 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070054 ((SkImage_Gpu*)image)->applyBudgetDecision();
55 }
56}
57
brianosman396fcdb2016-07-22 06:26:11 -070058SkImageInfo SkImage_Gpu::onImageInfo() const {
59 SkColorType ct;
60 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
61 ct = kUnknown_SkColorType;
62 }
63 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
64}
65
brianosman69c166d2016-08-17 14:01:05 -070066static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
67 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070068}
69
Brian Osman61624f02016-12-09 14:51:59 -050070bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050071 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070072 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
73 SkASSERT(dst->getGenerationID() == this->uniqueID());
74 SkASSERT(dst->isImmutable());
75 SkASSERT(dst->getPixels());
76 return true;
77 }
78
brianosman69c166d2016-08-17 14:01:05 -070079 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070080 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070081 return false;
82 }
83 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
84 dst->getPixels(), dst->rowBytes())) {
85 return false;
86 }
reed6f1216a2015-08-04 08:10:13 -070087
88 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080089 if (kAllow_CachingHint == chint) {
90 SkBitmapCache::Add(this->uniqueID(), *dst);
91 fAddedRasterVersionToCache.store(true);
92 }
reed8b26b992015-05-07 15:36:17 -070093 return true;
94}
95
Robert Phillips2c862492017-01-18 10:08:39 -050096sk_sp<GrSurfaceProxy> SkImage_Gpu::refProxy() const {
97 return GrSurfaceProxy::MakeWrapped(fTexture);
98}
99
Brian Salomon514baff2016-11-17 15:17:07 -0500100GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500101 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500102 sk_sp<SkColorSpace>* texColorSpace,
103 SkScalar scaleAdjust[2]) const {
Brian Osman7992da32016-11-18 11:28:24 -0500104 if (texColorSpace) {
105 *texColorSpace = this->fColorSpace;
106 }
107 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
108 this->uniqueID(), this->fColorSpace.get());
Robert Phillips67c18d62017-01-20 12:44:06 -0500109 return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700110}
111
reed8b26b992015-05-07 15:36:17 -0700112static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
113 switch (info.colorType()) {
114 case kRGBA_8888_SkColorType:
115 case kBGRA_8888_SkColorType:
116 break;
117 default:
118 return; // nothing to do
119 }
120
121 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
122 // and in either case, the alpha-byte is always in the same place, so we can safely call
123 // SkPreMultiplyColor()
124 //
125 SkColor* row = (SkColor*)pixels;
126 for (int y = 0; y < info.height(); ++y) {
127 for (int x = 0; x < info.width(); ++x) {
128 row[x] = SkPreMultiplyColor(row[x]);
129 }
130 }
131}
132
Matt Sarett03dd6d52017-01-23 12:15:09 -0500133bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800134 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500135 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500136 return false;
137 }
138
Matt Sarett03dd6d52017-01-23 12:15:09 -0500139 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
140 if (!rec.trim(this->width(), this->height())) {
141 return false;
142 }
143
144 GrPixelConfig config = SkImageInfo2GrPixelConfig(rec.fInfo, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700145 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500146 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700147 // let the GPU perform this transformation for us
148 flags = GrContext::kUnpremul_PixelOpsFlag;
149 }
Matt Sarett03dd6d52017-01-23 12:15:09 -0500150 if (!fTexture->readPixels(fColorSpace.get(), rec.fX, rec.fY, rec.fInfo.width(),
151 rec.fInfo.height(), config, rec.fInfo.colorSpace(), rec.fPixels,
152 rec.fRowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700153 return false;
154 }
155 // do we have to manually fix-up the alpha channel?
156 // src dst
157 // unpremul premul fix manually
158 // premul unpremul done by kUnpremul_PixelOpsFlag
159 // all other combos need to change.
160 //
161 // Should this be handled by Ganesh? todo:?
162 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500163 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
164 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700165 }
166 return true;
167}
168
reed7fb4f8b2016-03-11 04:33:52 -0800169sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700170 GrContext* ctx = fTexture->getContext();
171 GrSurfaceDesc desc = fTexture->desc();
172 desc.fWidth = subset.width();
173 desc.fHeight = subset.height();
174
Robert Phillipse2f7d182016-12-15 09:23:05 -0500175 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
176 desc,
177 SkBackingFit::kExact,
178 fBudgeted));
179 if (!sContext) {
180 return nullptr;
181 }
182
183 // TODO: make gpu images be proxy-backed so we don't need to do this
184 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
185 if (!tmpSrc) {
186 return nullptr;
187 }
188
189 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
190 return nullptr;
191 }
192
193 // TODO: make gpu images be proxy-backed so we don't need to do this
194 GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700195 if (!subTx) {
196 return nullptr;
197 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500198
reed7fb4f8b2016-03-11 04:33:52 -0800199 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500200 fAlphaType, sk_ref_sp(subTx->asTexture()),
201 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700202}
203
reed8b26b992015-05-07 15:36:17 -0700204///////////////////////////////////////////////////////////////////////////////////////////////////
205
reed7fb4f8b2016-03-11 04:33:52 -0800206static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700207 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
208 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800209 SkImage::TextureReleaseProc releaseProc,
210 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700211 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700213 }
bungeman6bd52842016-10-27 09:30:08 -0700214 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700215 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700216 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700217 }
reedde499882015-06-18 13:41:40 -0700218 if (releaseProc) {
219 tex->setRelease(releaseProc, releaseCtx);
220 }
221
bsalomon5ec26ae2016-02-25 08:33:02 -0800222 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800223 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700224 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225}
226
reed7fb4f8b2016-03-11 04:33:52 -0800227sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700228 SkAlphaType at, sk_sp<SkColorSpace> cs,
229 TextureReleaseProc releaseP, ReleaseContext releaseC) {
230 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
231 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232}
233
reed7fb4f8b2016-03-11 04:33:52 -0800234sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700235 SkAlphaType at, sk_sp<SkColorSpace> cs) {
236 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
237 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700238}
239
jbaumanb445a572016-06-09 13:24:48 -0700240static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
241 bool nv12,
242 const GrBackendObject yuvTextureHandles[],
243 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700244 GrSurfaceOrigin origin,
245 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800246 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700247
jbaumanb445a572016-06-09 13:24:48 -0700248 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
249 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700251 }
jbaumanb445a572016-06-09 13:24:48 -0700252 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
253 return nullptr;
254 }
255
256 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
257
bsalomon993a4212015-05-29 11:37:25 -0700258 GrBackendTextureDesc yDesc;
259 yDesc.fConfig = kConfig;
260 yDesc.fOrigin = origin;
261 yDesc.fSampleCnt = 0;
262 yDesc.fTextureHandle = yuvTextureHandles[0];
263 yDesc.fWidth = yuvSizes[0].fWidth;
264 yDesc.fHeight = yuvSizes[0].fHeight;
265
266 GrBackendTextureDesc uDesc;
267 uDesc.fConfig = kConfig;
268 uDesc.fOrigin = origin;
269 uDesc.fSampleCnt = 0;
270 uDesc.fTextureHandle = yuvTextureHandles[1];
271 uDesc.fWidth = yuvSizes[1].fWidth;
272 uDesc.fHeight = yuvSizes[1].fHeight;
273
jbaumanb445a572016-06-09 13:24:48 -0700274 sk_sp<GrTexture> yTex(
275 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500276 sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrapped(std::move(yTex));
277
jbaumanb445a572016-06-09 13:24:48 -0700278 sk_sp<GrTexture> uTex(
279 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500280 sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrapped(std::move(uTex));
281
282 sk_sp<GrSurfaceProxy> vProxy;
283
jbaumanb445a572016-06-09 13:24:48 -0700284 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500285 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700286 } else {
287 GrBackendTextureDesc vDesc;
288 vDesc.fConfig = kConfig;
289 vDesc.fOrigin = origin;
290 vDesc.fSampleCnt = 0;
291 vDesc.fTextureHandle = yuvTextureHandles[2];
292 vDesc.fWidth = yuvSizes[2].fWidth;
293 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700294
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500295 sk_sp<GrTexture> vTex = sk_sp<GrTexture>(
jbaumanb445a572016-06-09 13:24:48 -0700296 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500297 vProxy = GrSurfaceProxy::MakeWrapped(std::move(vTex));
jbaumanb445a572016-06-09 13:24:48 -0700298 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500299 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700300 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700301 }
302
robertphillipsd4c741e2016-04-28 09:55:15 -0700303 const int width = yuvSizes[0].fWidth;
304 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700305
robertphillipsd4c741e2016-04-28 09:55:15 -0700306 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400307 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
308 SkBackingFit::kExact,
309 width, height,
310 kRGBA_8888_GrPixelConfig,
311 std::move(imageColorSpace),
312 0,
313 origin));
314 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700315 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700316 }
317
318 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400319 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700320 paint.addColorFragmentProcessor(
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500321 GrYUVEffect::MakeYUVToRGB(ctx,
322 sk_ref_sp(yProxy->asTextureProxy()),
323 sk_ref_sp(uProxy->asTextureProxy()),
324 sk_ref_sp(vProxy->asTextureProxy()), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700325
robertphillipsd4c741e2016-04-28 09:55:15 -0700326 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700327
Brian Salomon82f44312017-01-11 13:42:54 -0500328 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500329
330 if (!renderTargetContext->accessRenderTarget()) {
331 return nullptr;
332 }
Brian Osman11052242016-10-27 14:47:55 -0400333 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700334 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400335 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500336 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700337}
reed56179002015-07-07 06:11:19 -0700338
jbaumanb445a572016-06-09 13:24:48 -0700339sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
340 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700341 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
342 sk_sp<SkColorSpace> imageColorSpace) {
343 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
344 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700345}
346
347sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
348 const GrBackendObject yuvTextureHandles[2],
349 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700350 GrSurfaceOrigin origin,
351 sk_sp<SkColorSpace> imageColorSpace) {
352 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
353 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700354}
355
bsalomon634b4302016-07-12 18:11:17 -0700356sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700357 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700358 return sk_ref_sp(const_cast<SkImage*>(this));
359 }
brianosman396fcdb2016-07-22 06:26:11 -0700360 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700361 size_t rowBytes = info.minRowBytes();
362 size_t size = info.getSafeSize(rowBytes);
363 auto data = SkData::MakeUninitialized(size);
364 if (!data) {
365 return nullptr;
366 }
367 SkPixmap pm(info, data->writable_data(), rowBytes);
368 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
369 return nullptr;
370 }
371 return MakeRasterData(info, data, rowBytes);
372}
373
Brian Osmanb92234a2017-01-25 14:13:00 +0000374sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
375 SkBudgeted budgeted) {
376 if (!ctx) {
377 return nullptr;
378 }
379 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
380 if (!texture) {
381 return nullptr;
382 }
383 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
384 pixmap.alphaType(), std::move(texture),
385 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
386}
387
reed56179002015-07-07 06:11:19 -0700388///////////////////////////////////////////////////////////////////////////////////////////////////
389
bsalomon4d516a62016-07-28 13:37:31 -0700390namespace {
391struct MipMapLevelData {
392 void* fPixelData;
393 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800394};
395
bsalomon4d516a62016-07-28 13:37:31 -0700396struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500397 uint32_t fContextUniqueID;
398 // Right now, the destination color mode is only considered when generating mipmaps
399 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700400 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500401 int fWidth;
402 int fHeight;
403 SkColorType fColorType;
404 SkAlphaType fAlphaType;
405 void* fColorSpace;
406 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500407 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700408 // The fMipMapLevelData array may contain more than 1 element.
409 // It contains fMipMapLevelCount elements.
410 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500411 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700412};
413} // anonymous namespace
414
cblume33e0cb52016-08-30 12:09:23 -0700415static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800416 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
417 // This can cause runaway memory leaks, so we are disabling this path until we can
418 // investigate further. crbug.com/669775
419 return false;
cblume33e0cb52016-08-30 12:09:23 -0700420}
421
422namespace {
423
424class DTIBufferFiller
425{
426public:
cblume921bc672016-09-22 05:25:26 -0700427 explicit DTIBufferFiller(char* bufferAsCharPtr)
428 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700429
430 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700431 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700432 }
433
434private:
435
cblume921bc672016-09-22 05:25:26 -0700436 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700437};
438}
439
440#define FILL_MEMBER(bufferFiller, member, source) \
441 bufferFiller.fillMember(source, \
442 offsetof(DeferredTextureImage, member), \
443 sizeof(DeferredTextureImage::member));
444
bsalomon41b952c2016-03-11 06:46:33 -0800445size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700446 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700447 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400448 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700449 // Extract relevant min/max values from the params array.
450 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
451 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700452 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700453 for (int i = 1; i < paramCnt; ++i) {
454 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
455 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
456 if (highestFilterQuality < params[i].fQuality)
457 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700458 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700459 }
460
bsalomon41b952c2016-03-11 06:46:33 -0800461 const bool fillMode = SkToBool(buffer);
462 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
463 return 0;
464 }
465
ericrkb4da01d2016-06-13 11:18:14 -0700466 // Calculate scaling parameters.
467 bool isScaled = lowestPreScaleMipLevel != 0;
468
469 SkISize scaledSize;
470 if (isScaled) {
471 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
472 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
473 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
474 lowestPreScaleMipLevel - 1);
475 } else {
476 scaledSize = SkISize::Make(this->width(), this->height());
477 }
478
479 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
480 SkFilterQuality scaleFilterQuality = highestFilterQuality;
481 if (scaleFilterQuality > kMedium_SkFilterQuality) {
482 scaleFilterQuality = kMedium_SkFilterQuality;
483 }
484
ericrkc429baf2016-03-24 15:35:45 -0700485 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700486 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700487 return 0;
488 }
489
bsalomon41b952c2016-03-11 06:46:33 -0800490 SkAutoPixmapStorage pixmap;
491 SkImageInfo info;
492 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500493 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800494 info = pixmap.info();
495 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800496 } else {
497 // Here we're just using presence of data to know whether there is a codec behind the image.
498 // In the future we will access the cacherator and get the exact data that we want to (e.g.
499 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700500 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700501 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800502 return 0;
503 }
Brian Osman7992da32016-11-18 11:28:24 -0500504 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
505 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500506 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500507 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500508 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
509 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500510 } else {
511 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
512 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500513 if (kIndex_8_SkColorType == info.colorType()) {
514 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
515 info = info.makeColorType(kN32_SkColorType);
516 }
bsalomon41b952c2016-03-11 06:46:33 -0800517 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
518 if (fillMode) {
519 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700520 if (isScaled) {
521 if (!this->scalePixels(pixmap, scaleFilterQuality,
522 SkImage::kDisallow_CachingHint)) {
523 return 0;
524 }
525 } else {
526 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
527 return 0;
528 }
bsalomon41b952c2016-03-11 06:46:33 -0800529 }
530 SkASSERT(!pixmap.ctable());
531 }
532 }
cblume2c052802016-05-31 09:55:08 -0700533 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700534 if (useMipMaps) {
535 // SkMipMap only deals with the mipmap levels it generates, which does
536 // not include the base level.
537 // That means it generates and holds levels 1-x instead of 0-x.
538 // So the total mipmap level count is 1 more than what
539 // SkMipMap::ComputeLevelCount returns.
540 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
541
542 // We already initialized pixelSize to the size of the base level.
543 // SkMipMap will generate the extra mipmap levels. Their sizes need to
544 // be added to the total.
545 // Index 0 here does not refer to the base mipmap level -- it is
546 // SkMipMap's first generated mipmap level (level 1).
547 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
548 currentMipMapLevelIndex--) {
549 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
550 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700551 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700552 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
553 }
554 }
bsalomon41b952c2016-03-11 06:46:33 -0800555 size_t size = 0;
556 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
557 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700558 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
559 // We subtract 1 because DeferredTextureImage already includes the base
560 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800561 size_t pixelOffset = size;
562 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700563 size_t colorSpaceOffset = 0;
564 size_t colorSpaceSize = 0;
565 if (info.colorSpace()) {
566 colorSpaceOffset = size;
567 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
568 size += colorSpaceSize;
569 }
bsalomon41b952c2016-03-11 06:46:33 -0800570 if (!fillMode) {
571 return size;
572 }
cblume921bc672016-09-22 05:25:26 -0700573 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
574 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
575 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800576
cblume921bc672016-09-22 05:25:26 -0700577 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
578 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800579
Brian Osman6c15cc72016-10-17 09:51:37 -0400580 // If the context has sRGB support, and we're intending to render to a surface with an attached
581 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
582 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500583 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400584 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
585 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500586 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400587 }
588
bsalomon41b952c2016-03-11 06:46:33 -0800589 SkASSERT(info == pixmap.info());
590 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700591 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
592 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700593 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500594 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700595 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
596 int width = info.width();
597 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
598 int height = info.height();
599 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
600 SkColorType colorType = info.colorType();
601 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
602 SkAlphaType alphaType = info.alphaType();
603 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700604 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700605 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700606 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700607 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700608 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700609 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700610 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700611 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
612 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700613 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700614 } else {
cblume921bc672016-09-22 05:25:26 -0700615 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700616 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700617 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700618 0, sizeof(DeferredTextureImage::fColorSpaceSize));
619 }
620
621 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700622 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700623
624 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700625 static_assert(std::is_standard_layout<MipMapLevelData>::value,
626 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700627
Brian Osman7b8400d2016-11-08 17:08:54 -0500628 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700629 // SkMipMap holds only the mipmap levels it generates.
630 // A programmer can use the data they provided to SkMipMap::Build as level 0.
631 // So the SkMipMap provides levels 1-x but it stores them in its own
632 // range 0-(x-1).
633 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
634 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700635 SkMipMap::Level mipLevel;
636 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
637
638 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700639 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700640 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700641 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700642 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700643 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700644 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700645
646 // getSafeSize includes rowbyte padding except for the last row,
647 // right?
648
cblume921bc672016-09-22 05:25:26 -0700649 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700650
cblume921bc672016-09-22 05:25:26 -0700651 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
652 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
653 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700654 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700655 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
656 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
657 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700658
659 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
660 }
bsalomon4d516a62016-07-28 13:37:31 -0700661 }
bsalomon41b952c2016-03-11 06:46:33 -0800662 return size;
663}
664
665sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
666 SkBudgeted budgeted) {
667 if (!data) {
668 return nullptr;
669 }
670 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
671
672 if (!context || context->uniqueID() != dti->fContextUniqueID) {
673 return nullptr;
674 }
cblume33e0cb52016-08-30 12:09:23 -0700675 int mipLevelCount = dti->fMipMapLevelCount;
676 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700677 sk_sp<SkColorSpace> colorSpace;
678 if (dti->fColorSpaceSize) {
679 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
680 }
681 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
682 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000683 if (mipLevelCount == 1) {
684 SkPixmap pixmap;
685 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
686 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
687 } else {
688 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
689 for (int i = 0; i < mipLevelCount; i++) {
690 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
691 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
692 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500693
Brian Osmanb92234a2017-01-25 14:13:00 +0000694 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
695 mipLevelCount, SkBudgeted::kYes,
696 dti->fColorMode);
697 }
bsalomon41b952c2016-03-11 06:46:33 -0800698}
699
700///////////////////////////////////////////////////////////////////////////////////////////////////
701
cblume186d2d42016-06-03 11:17:42 -0700702sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
703 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700704 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500705 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700706 if (!ctx) {
707 return nullptr;
708 }
bungeman6bd52842016-10-27 09:30:08 -0700709 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700710 if (!texture) {
711 return nullptr;
712 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500713 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700714 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700715 info.alphaType(), std::move(texture),
716 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700717}