blob: 07d352a961c103c43809423386278fa599b263e6 [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"
Brian Osman3b66ab62016-11-28 09:26:31 -050016#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040017#include "GrRenderTargetContext.h"
Brian Osmane8e54582016-11-28 10:06:27 -050018#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070019#include "GrTexturePriv.h"
bsalomonf267c1e2016-02-01 13:16:14 -080020#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070021#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080022#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070023#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080024#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050025#include "SkImageCacherator.h"
ericrkb4da01d2016-06-13 11:18:14 -070026#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070027#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070028
bungeman6bd52842016-10-27 09:30:08 -070029SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070030 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070031 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070032 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070033 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080034 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070035 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070036 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070037{
bungeman6bd52842016-10-27 09:30:08 -070038 SkASSERT(fTexture->width() == w);
39 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070040}
piotaixrcef04f82014-07-14 07:48:04 -070041
reed6f1216a2015-08-04 08:10:13 -070042SkImage_Gpu::~SkImage_Gpu() {
43 if (fAddedRasterVersionToCache.load()) {
44 SkNotifyBitmapGenIDIsStale(this->uniqueID());
45 }
46}
47
bsalomoneaaaf0b2015-01-23 08:08:04 -080048extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070049 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070050 ((SkImage_Gpu*)image)->applyBudgetDecision();
51 }
52}
53
brianosman396fcdb2016-07-22 06:26:11 -070054SkImageInfo SkImage_Gpu::onImageInfo() const {
55 SkColorType ct;
56 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
57 ct = kUnknown_SkColorType;
58 }
59 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
60}
61
brianosman69c166d2016-08-17 14:01:05 -070062static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
63 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070064}
65
Brian Osman7992da32016-11-18 11:28:24 -050066bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkDestinationSurfaceColorMode,
67 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070068 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
69 SkASSERT(dst->getGenerationID() == this->uniqueID());
70 SkASSERT(dst->isImmutable());
71 SkASSERT(dst->getPixels());
72 return true;
73 }
74
brianosman69c166d2016-08-17 14:01:05 -070075 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070076 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070077 return false;
78 }
79 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
80 dst->getPixels(), dst->rowBytes())) {
81 return false;
82 }
reed6f1216a2015-08-04 08:10:13 -070083
84 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080085 if (kAllow_CachingHint == chint) {
86 SkBitmapCache::Add(this->uniqueID(), *dst);
87 fAddedRasterVersionToCache.store(true);
88 }
reed8b26b992015-05-07 15:36:17 -070089 return true;
90}
91
Brian Salomon514baff2016-11-17 15:17:07 -050092GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman7992da32016-11-18 11:28:24 -050093 SkDestinationSurfaceColorMode colorMode,
94 sk_sp<SkColorSpace>* texColorSpace) const {
95 if (texColorSpace) {
96 *texColorSpace = this->fColorSpace;
97 }
98 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
99 this->uniqueID(), this->fColorSpace.get());
Brian Osman7b8400d2016-11-08 17:08:54 -0500100 return adjuster.refTextureSafeForParams(params, colorMode, nullptr);
reed85d91782015-09-10 14:33:38 -0700101}
102
reed8b26b992015-05-07 15:36:17 -0700103static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
104 switch (info.colorType()) {
105 case kRGBA_8888_SkColorType:
106 case kBGRA_8888_SkColorType:
107 break;
108 default:
109 return; // nothing to do
110 }
111
112 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
113 // and in either case, the alpha-byte is always in the same place, so we can safely call
114 // SkPreMultiplyColor()
115 //
116 SkColor* row = (SkColor*)pixels;
117 for (int y = 0; y < info.height(); ++y) {
118 for (int x = 0; x < info.width(); ++x) {
119 row[x] = SkPreMultiplyColor(row[x]);
120 }
121 }
122}
123
124bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800125 int srcX, int srcY, CachingHint) const {
brianosmanb109b8c2016-06-16 13:03:24 -0700126 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700127 uint32_t flags = 0;
128 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
129 // let the GPU perform this transformation for us
130 flags = GrContext::kUnpremul_PixelOpsFlag;
131 }
132 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
133 pixels, rowBytes, flags)) {
134 return false;
135 }
136 // do we have to manually fix-up the alpha channel?
137 // src dst
138 // unpremul premul fix manually
139 // premul unpremul done by kUnpremul_PixelOpsFlag
140 // all other combos need to change.
141 //
142 // Should this be handled by Ganesh? todo:?
143 //
144 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
145 apply_premul(info, pixels, rowBytes);
146 }
147 return true;
148}
149
reed7fb4f8b2016-03-11 04:33:52 -0800150sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700151 GrContext* ctx = fTexture->getContext();
152 GrSurfaceDesc desc = fTexture->desc();
153 desc.fWidth = subset.width();
154 desc.fHeight = subset.height();
155
senorblanco87751122016-05-20 07:27:38 -0700156 sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted));
reed7b6945b2015-09-24 00:50:58 -0700157 if (!subTx) {
158 return nullptr;
159 }
bungeman6bd52842016-10-27 09:30:08 -0700160 ctx->copySurface(subTx.get(), fTexture.get(), subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800161 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700162 fAlphaType, std::move(subTx), fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700163}
164
reed8b26b992015-05-07 15:36:17 -0700165///////////////////////////////////////////////////////////////////////////////////////////////////
166
reed7fb4f8b2016-03-11 04:33:52 -0800167static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700168 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
169 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800170 SkImage::TextureReleaseProc releaseProc,
171 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700172 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700173 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700174 }
bungeman6bd52842016-10-27 09:30:08 -0700175 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700176 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700177 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700178 }
reedde499882015-06-18 13:41:40 -0700179 if (releaseProc) {
180 tex->setRelease(releaseProc, releaseCtx);
181 }
182
bsalomon5ec26ae2016-02-25 08:33:02 -0800183 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800184 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700185 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700186}
187
reed7fb4f8b2016-03-11 04:33:52 -0800188sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700189 SkAlphaType at, sk_sp<SkColorSpace> cs,
190 TextureReleaseProc releaseP, ReleaseContext releaseC) {
191 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
192 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700193}
194
reed7fb4f8b2016-03-11 04:33:52 -0800195sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700196 SkAlphaType at, sk_sp<SkColorSpace> cs) {
197 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
198 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700199}
200
jbaumanb445a572016-06-09 13:24:48 -0700201static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
202 bool nv12,
203 const GrBackendObject yuvTextureHandles[],
204 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700205 GrSurfaceOrigin origin,
206 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800207 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700208
jbaumanb445a572016-06-09 13:24:48 -0700209 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
210 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700211 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700212 }
jbaumanb445a572016-06-09 13:24:48 -0700213 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
214 return nullptr;
215 }
216
217 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
218
bsalomon993a4212015-05-29 11:37:25 -0700219 GrBackendTextureDesc yDesc;
220 yDesc.fConfig = kConfig;
221 yDesc.fOrigin = origin;
222 yDesc.fSampleCnt = 0;
223 yDesc.fTextureHandle = yuvTextureHandles[0];
224 yDesc.fWidth = yuvSizes[0].fWidth;
225 yDesc.fHeight = yuvSizes[0].fHeight;
226
227 GrBackendTextureDesc uDesc;
228 uDesc.fConfig = kConfig;
229 uDesc.fOrigin = origin;
230 uDesc.fSampleCnt = 0;
231 uDesc.fTextureHandle = yuvTextureHandles[1];
232 uDesc.fWidth = yuvSizes[1].fWidth;
233 uDesc.fHeight = yuvSizes[1].fHeight;
234
jbaumanb445a572016-06-09 13:24:48 -0700235 sk_sp<GrTexture> yTex(
236 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
237 sk_sp<GrTexture> uTex(
238 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
239 sk_sp<GrTexture> vTex;
240 if (nv12) {
241 vTex = uTex;
242 } else {
243 GrBackendTextureDesc vDesc;
244 vDesc.fConfig = kConfig;
245 vDesc.fOrigin = origin;
246 vDesc.fSampleCnt = 0;
247 vDesc.fTextureHandle = yuvTextureHandles[2];
248 vDesc.fWidth = yuvSizes[2].fWidth;
249 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700250
jbaumanb445a572016-06-09 13:24:48 -0700251 vTex = sk_sp<GrTexture>(
252 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
253 }
bsalomon993a4212015-05-29 11:37:25 -0700254 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700255 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700256 }
257
robertphillipsd4c741e2016-04-28 09:55:15 -0700258 const int width = yuvSizes[0].fWidth;
259 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700260
robertphillipsd4c741e2016-04-28 09:55:15 -0700261 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400262 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
263 SkBackingFit::kExact,
264 width, height,
265 kRGBA_8888_GrPixelConfig,
266 std::move(imageColorSpace),
267 0,
268 origin));
269 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700270 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700271 }
272
273 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400274 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700275 paint.addColorFragmentProcessor(
276 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700277
robertphillipsd4c741e2016-04-28 09:55:15 -0700278 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700279
Brian Osman11052242016-10-27 14:47:55 -0400280 renderTargetContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500281
282 if (!renderTargetContext->accessRenderTarget()) {
283 return nullptr;
284 }
Brian Osman11052242016-10-27 14:47:55 -0400285 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700286 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400287 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
288 sk_ref_sp(renderTargetContext->getColorSpace()), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700289}
reed56179002015-07-07 06:11:19 -0700290
jbaumanb445a572016-06-09 13:24:48 -0700291sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
292 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700293 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
294 sk_sp<SkColorSpace> imageColorSpace) {
295 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
296 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700297}
298
299sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
300 const GrBackendObject yuvTextureHandles[2],
301 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700302 GrSurfaceOrigin origin,
303 sk_sp<SkColorSpace> imageColorSpace) {
304 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
305 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700306}
307
reed7fb4f8b2016-03-11 04:33:52 -0800308static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
Brian Osman7992da32016-11-18 11:28:24 -0500309 sk_sp<SkColorSpace> texColorSpace;
Brian Osman7b8400d2016-11-08 17:08:54 -0500310 sk_sp<GrTexture> texture(
Brian Salomon514baff2016-11-17 15:17:07 -0500311 maker->refTextureForParams(GrSamplerParams::ClampNoFilter(),
Brian Osman7992da32016-11-18 11:28:24 -0500312 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware,
313 &texColorSpace));
bsalomon8e74f802016-01-30 10:01:40 -0800314 if (!texture) {
315 return nullptr;
316 }
bungeman6bd52842016-10-27 09:30:08 -0700317 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture),
Brian Osman7992da32016-11-18 11:28:24 -0500318 std::move(texColorSpace), SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800319}
320
reed7fb4f8b2016-03-11 04:33:52 -0800321sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800322 if (!context) {
323 return nullptr;
324 }
325 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800326 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800327 }
bsalomon8e74f802016-01-30 10:01:40 -0800328
329 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
330 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
brianosman69c166d2016-08-17 14:01:05 -0700331 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800332 }
reed9a5a2012016-08-08 09:00:29 -0700333
334 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
335 GrBitmapTextureMaker maker(context, *bmp);
brianosman69c166d2016-08-17 14:01:05 -0700336 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800337 }
reed9a5a2012016-08-08 09:00:29 -0700338 return nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800339}
340
bsalomon634b4302016-07-12 18:11:17 -0700341sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700342 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700343 return sk_ref_sp(const_cast<SkImage*>(this));
344 }
brianosman396fcdb2016-07-22 06:26:11 -0700345 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700346 size_t rowBytes = info.minRowBytes();
347 size_t size = info.getSafeSize(rowBytes);
348 auto data = SkData::MakeUninitialized(size);
349 if (!data) {
350 return nullptr;
351 }
352 SkPixmap pm(info, data->writable_data(), rowBytes);
353 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
354 return nullptr;
355 }
356 return MakeRasterData(info, data, rowBytes);
357}
358
reed7fb4f8b2016-03-11 04:33:52 -0800359sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
360 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800361 if (!ctx) {
362 return nullptr;
363 }
bungeman6bd52842016-10-27 09:30:08 -0700364 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800365 if (!texture) {
366 return nullptr;
367 }
reed7fb4f8b2016-03-11 04:33:52 -0800368 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700369 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700370 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800371}
372
reed56179002015-07-07 06:11:19 -0700373///////////////////////////////////////////////////////////////////////////////////////////////////
374
bsalomon4d516a62016-07-28 13:37:31 -0700375namespace {
376struct MipMapLevelData {
377 void* fPixelData;
378 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800379};
380
bsalomon4d516a62016-07-28 13:37:31 -0700381struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500382 uint32_t fContextUniqueID;
383 // Right now, the destination color mode is only considered when generating mipmaps
384 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700385 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500386 int fWidth;
387 int fHeight;
388 SkColorType fColorType;
389 SkAlphaType fAlphaType;
390 void* fColorSpace;
391 size_t fColorSpaceSize;
392 int fColorTableCnt;
393 uint32_t* fColorTableData;
394 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700395 // The fMipMapLevelData array may contain more than 1 element.
396 // It contains fMipMapLevelCount elements.
397 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500398 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700399};
400} // anonymous namespace
401
cblume33e0cb52016-08-30 12:09:23 -0700402static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800403 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
404 // This can cause runaway memory leaks, so we are disabling this path until we can
405 // investigate further. crbug.com/669775
406 return false;
cblume33e0cb52016-08-30 12:09:23 -0700407}
408
409namespace {
410
411class DTIBufferFiller
412{
413public:
cblume921bc672016-09-22 05:25:26 -0700414 explicit DTIBufferFiller(char* bufferAsCharPtr)
415 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700416
417 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700418 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700419 }
420
421private:
422
cblume921bc672016-09-22 05:25:26 -0700423 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700424};
425}
426
427#define FILL_MEMBER(bufferFiller, member, source) \
428 bufferFiller.fillMember(source, \
429 offsetof(DeferredTextureImage, member), \
430 sizeof(DeferredTextureImage::member));
431
bsalomon41b952c2016-03-11 06:46:33 -0800432size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700433 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700434 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400435 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700436 // Extract relevant min/max values from the params array.
437 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
438 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700439 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700440 for (int i = 1; i < paramCnt; ++i) {
441 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
442 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
443 if (highestFilterQuality < params[i].fQuality)
444 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700445 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700446 }
447
bsalomon41b952c2016-03-11 06:46:33 -0800448 const bool fillMode = SkToBool(buffer);
449 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
450 return 0;
451 }
452
ericrkb4da01d2016-06-13 11:18:14 -0700453 // Calculate scaling parameters.
454 bool isScaled = lowestPreScaleMipLevel != 0;
455
456 SkISize scaledSize;
457 if (isScaled) {
458 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
459 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
460 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
461 lowestPreScaleMipLevel - 1);
462 } else {
463 scaledSize = SkISize::Make(this->width(), this->height());
464 }
465
466 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
467 SkFilterQuality scaleFilterQuality = highestFilterQuality;
468 if (scaleFilterQuality > kMedium_SkFilterQuality) {
469 scaleFilterQuality = kMedium_SkFilterQuality;
470 }
471
ericrkc429baf2016-03-24 15:35:45 -0700472 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700473 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700474 return 0;
475 }
476
bsalomon41b952c2016-03-11 06:46:33 -0800477 SkAutoPixmapStorage pixmap;
478 SkImageInfo info;
479 size_t pixelSize = 0;
480 size_t ctSize = 0;
481 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700482 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800483 info = pixmap.info();
484 pixelSize = SkAlign8(pixmap.getSafeSize());
485 if (pixmap.ctable()) {
486 ctCount = pixmap.ctable()->count();
487 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
488 }
489 } else {
490 // Here we're just using presence of data to know whether there is a codec behind the image.
491 // In the future we will access the cacherator and get the exact data that we want to (e.g.
492 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700493 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700494 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800495 return 0;
496 }
Brian Osman7992da32016-11-18 11:28:24 -0500497 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
498 // Generator backed image. Tweak info to trigger correct kind of decode.
499 SkDestinationSurfaceColorMode decodeColorMode = dstColorSpace
500 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
501 : SkDestinationSurfaceColorMode::kLegacy;
502 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
503 decodeColorMode, proxy.fCaps.get());
504 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
505 scaledSize.height());
506
507 } else {
508 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
509 }
bsalomon41b952c2016-03-11 06:46:33 -0800510 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
511 if (fillMode) {
512 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700513 if (isScaled) {
514 if (!this->scalePixels(pixmap, scaleFilterQuality,
515 SkImage::kDisallow_CachingHint)) {
516 return 0;
517 }
518 } else {
519 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
520 return 0;
521 }
bsalomon41b952c2016-03-11 06:46:33 -0800522 }
523 SkASSERT(!pixmap.ctable());
524 }
525 }
cblume2c052802016-05-31 09:55:08 -0700526 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700527 if (useMipMaps) {
528 // SkMipMap only deals with the mipmap levels it generates, which does
529 // not include the base level.
530 // That means it generates and holds levels 1-x instead of 0-x.
531 // So the total mipmap level count is 1 more than what
532 // SkMipMap::ComputeLevelCount returns.
533 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
534
535 // We already initialized pixelSize to the size of the base level.
536 // SkMipMap will generate the extra mipmap levels. Their sizes need to
537 // be added to the total.
538 // Index 0 here does not refer to the base mipmap level -- it is
539 // SkMipMap's first generated mipmap level (level 1).
540 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
541 currentMipMapLevelIndex--) {
542 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
543 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700544 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700545 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
546 }
547 }
bsalomon41b952c2016-03-11 06:46:33 -0800548 size_t size = 0;
549 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
550 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700551 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
552 // We subtract 1 because DeferredTextureImage already includes the base
553 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800554 size_t pixelOffset = size;
555 size += pixelSize;
556 size_t ctOffset = size;
557 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700558 size_t colorSpaceOffset = 0;
559 size_t colorSpaceSize = 0;
560 if (info.colorSpace()) {
561 colorSpaceOffset = size;
562 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
563 size += colorSpaceSize;
564 }
bsalomon41b952c2016-03-11 06:46:33 -0800565 if (!fillMode) {
566 return size;
567 }
cblume921bc672016-09-22 05:25:26 -0700568 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
569 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
570 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700571 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800572 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700573 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800574 }
575
cblume921bc672016-09-22 05:25:26 -0700576 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
577 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800578 if (ctSize) {
579 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
580 }
581
Brian Osman6c15cc72016-10-17 09:51:37 -0400582 // If the context has sRGB support, and we're intending to render to a surface with an attached
583 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
584 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500585 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400586 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
587 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500588 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400589 }
590
bsalomon41b952c2016-03-11 06:46:33 -0800591 SkASSERT(info == pixmap.info());
592 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700593 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
594 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700595 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500596 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700597 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
598 int width = info.width();
599 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
600 int height = info.height();
601 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
602 SkColorType colorType = info.colorType();
603 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
604 SkAlphaType alphaType = info.alphaType();
605 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
606 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
607 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
608 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700609 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700610 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700611 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700612 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700613 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700614 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700615 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
616 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700617 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700618 } else {
cblume921bc672016-09-22 05:25:26 -0700619 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700620 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700621 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700622 0, sizeof(DeferredTextureImage::fColorSpaceSize));
623 }
624
625 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700626 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700627
628 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700629 static_assert(std::is_standard_layout<MipMapLevelData>::value,
630 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700631
Brian Osman7b8400d2016-11-08 17:08:54 -0500632 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700633 // SkMipMap holds only the mipmap levels it generates.
634 // A programmer can use the data they provided to SkMipMap::Build as level 0.
635 // So the SkMipMap provides levels 1-x but it stores them in its own
636 // range 0-(x-1).
637 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
638 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700639 SkMipMap::Level mipLevel;
640 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
641
642 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700643 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700644 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700645 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700646 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700647 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700648 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700649
650 // getSafeSize includes rowbyte padding except for the last row,
651 // right?
652
cblume921bc672016-09-22 05:25:26 -0700653 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700654
cblume921bc672016-09-22 05:25:26 -0700655 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
656 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
657 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700658 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700659 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
660 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
661 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700662
663 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
664 }
bsalomon4d516a62016-07-28 13:37:31 -0700665 }
bsalomon41b952c2016-03-11 06:46:33 -0800666 return size;
667}
668
669sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
670 SkBudgeted budgeted) {
671 if (!data) {
672 return nullptr;
673 }
674 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
675
676 if (!context || context->uniqueID() != dti->fContextUniqueID) {
677 return nullptr;
678 }
Hal Canary67b39de2016-11-07 11:47:44 -0500679 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700680 if (dti->fColorTableCnt) {
681 SkASSERT(dti->fColorTableData);
682 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800683 }
cblume33e0cb52016-08-30 12:09:23 -0700684 int mipLevelCount = dti->fMipMapLevelCount;
685 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700686 sk_sp<SkColorSpace> colorSpace;
687 if (dti->fColorSpaceSize) {
688 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
689 }
690 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
691 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700692 if (mipLevelCount == 1) {
693 SkPixmap pixmap;
694 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
695 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
696 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
697 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400698 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700699 for (int i = 0; i < mipLevelCount; i++) {
700 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
701 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
702 }
703
704 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
705 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500706 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700707 }
bsalomon41b952c2016-03-11 06:46:33 -0800708}
709
710///////////////////////////////////////////////////////////////////////////////////////////////////
711
cblume186d2d42016-06-03 11:17:42 -0700712sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
713 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700714 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500715 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700716 if (!ctx) {
717 return nullptr;
718 }
bungeman6bd52842016-10-27 09:30:08 -0700719 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700720 if (!texture) {
721 return nullptr;
722 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500723 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700724 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700725 info.alphaType(), std::move(texture),
726 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700727}