blob: 02f4d69519de3259d0261f402d8c37f2a3dde722 [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"
reed856e9d92015-09-30 12:21:45 -070013#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000014#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
bsalomonf1ecd212015-12-09 17:06:02 -080016#include "GrImageIDTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070017#include "GrTexturePriv.h"
bsalomonf267c1e2016-02-01 13:16:14 -080018#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070019#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080020#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070021#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080022#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050023#include "SkImageCacherator.h"
ericrkb4da01d2016-06-13 11:18:14 -070024#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070025#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070026
bungeman6bd52842016-10-27 09:30:08 -070027SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070028 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070029 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070030 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070031 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080032 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070033 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070034 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070035{
bungeman6bd52842016-10-27 09:30:08 -070036 SkASSERT(fTexture->width() == w);
37 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070038}
piotaixrcef04f82014-07-14 07:48:04 -070039
reed6f1216a2015-08-04 08:10:13 -070040SkImage_Gpu::~SkImage_Gpu() {
41 if (fAddedRasterVersionToCache.load()) {
42 SkNotifyBitmapGenIDIsStale(this->uniqueID());
43 }
44}
45
bsalomoneaaaf0b2015-01-23 08:08:04 -080046extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070047 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070048 ((SkImage_Gpu*)image)->applyBudgetDecision();
49 }
50}
51
brianosman396fcdb2016-07-22 06:26:11 -070052SkImageInfo SkImage_Gpu::onImageInfo() const {
53 SkColorType ct;
54 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
55 ct = kUnknown_SkColorType;
56 }
57 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
58}
59
brianosman69c166d2016-08-17 14:01:05 -070060static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
61 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070062}
63
Brian Osman7992da32016-11-18 11:28:24 -050064bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkDestinationSurfaceColorMode,
65 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070066 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
67 SkASSERT(dst->getGenerationID() == this->uniqueID());
68 SkASSERT(dst->isImmutable());
69 SkASSERT(dst->getPixels());
70 return true;
71 }
72
brianosman69c166d2016-08-17 14:01:05 -070073 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070074 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070075 return false;
76 }
77 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
78 dst->getPixels(), dst->rowBytes())) {
79 return false;
80 }
reed6f1216a2015-08-04 08:10:13 -070081
82 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080083 if (kAllow_CachingHint == chint) {
84 SkBitmapCache::Add(this->uniqueID(), *dst);
85 fAddedRasterVersionToCache.store(true);
86 }
reed8b26b992015-05-07 15:36:17 -070087 return true;
88}
89
Brian Salomon514baff2016-11-17 15:17:07 -050090GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman7992da32016-11-18 11:28:24 -050091 SkDestinationSurfaceColorMode colorMode,
92 sk_sp<SkColorSpace>* texColorSpace) const {
93 if (texColorSpace) {
94 *texColorSpace = this->fColorSpace;
95 }
96 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
97 this->uniqueID(), this->fColorSpace.get());
Brian Osman7b8400d2016-11-08 17:08:54 -050098 return adjuster.refTextureSafeForParams(params, colorMode, nullptr);
reed85d91782015-09-10 14:33:38 -070099}
100
reed8b26b992015-05-07 15:36:17 -0700101static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
102 switch (info.colorType()) {
103 case kRGBA_8888_SkColorType:
104 case kBGRA_8888_SkColorType:
105 break;
106 default:
107 return; // nothing to do
108 }
109
110 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
111 // and in either case, the alpha-byte is always in the same place, so we can safely call
112 // SkPreMultiplyColor()
113 //
114 SkColor* row = (SkColor*)pixels;
115 for (int y = 0; y < info.height(); ++y) {
116 for (int x = 0; x < info.width(); ++x) {
117 row[x] = SkPreMultiplyColor(row[x]);
118 }
119 }
120}
121
122bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800123 int srcX, int srcY, CachingHint) const {
brianosmanb109b8c2016-06-16 13:03:24 -0700124 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700125 uint32_t flags = 0;
126 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
127 // let the GPU perform this transformation for us
128 flags = GrContext::kUnpremul_PixelOpsFlag;
129 }
130 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
131 pixels, rowBytes, flags)) {
132 return false;
133 }
134 // do we have to manually fix-up the alpha channel?
135 // src dst
136 // unpremul premul fix manually
137 // premul unpremul done by kUnpremul_PixelOpsFlag
138 // all other combos need to change.
139 //
140 // Should this be handled by Ganesh? todo:?
141 //
142 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
143 apply_premul(info, pixels, rowBytes);
144 }
145 return true;
146}
147
reed7fb4f8b2016-03-11 04:33:52 -0800148sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700149 GrContext* ctx = fTexture->getContext();
150 GrSurfaceDesc desc = fTexture->desc();
151 desc.fWidth = subset.width();
152 desc.fHeight = subset.height();
153
senorblanco87751122016-05-20 07:27:38 -0700154 sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted));
reed7b6945b2015-09-24 00:50:58 -0700155 if (!subTx) {
156 return nullptr;
157 }
bungeman6bd52842016-10-27 09:30:08 -0700158 ctx->copySurface(subTx.get(), fTexture.get(), subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800159 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700160 fAlphaType, std::move(subTx), fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700161}
162
reed8b26b992015-05-07 15:36:17 -0700163///////////////////////////////////////////////////////////////////////////////////////////////////
164
reed7fb4f8b2016-03-11 04:33:52 -0800165static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700166 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
167 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800168 SkImage::TextureReleaseProc releaseProc,
169 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700170 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700171 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700172 }
bungeman6bd52842016-10-27 09:30:08 -0700173 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700174 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700175 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700176 }
reedde499882015-06-18 13:41:40 -0700177 if (releaseProc) {
178 tex->setRelease(releaseProc, releaseCtx);
179 }
180
bsalomon5ec26ae2016-02-25 08:33:02 -0800181 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800182 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700183 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700184}
185
reed7fb4f8b2016-03-11 04:33:52 -0800186sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700187 SkAlphaType at, sk_sp<SkColorSpace> cs,
188 TextureReleaseProc releaseP, ReleaseContext releaseC) {
189 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
190 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700191}
192
reed7fb4f8b2016-03-11 04:33:52 -0800193sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700194 SkAlphaType at, sk_sp<SkColorSpace> cs) {
195 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
196 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700197}
198
jbaumanb445a572016-06-09 13:24:48 -0700199static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
200 bool nv12,
201 const GrBackendObject yuvTextureHandles[],
202 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700203 GrSurfaceOrigin origin,
204 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800205 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700206
jbaumanb445a572016-06-09 13:24:48 -0700207 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
208 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700209 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700210 }
jbaumanb445a572016-06-09 13:24:48 -0700211 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
212 return nullptr;
213 }
214
215 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
216
bsalomon993a4212015-05-29 11:37:25 -0700217 GrBackendTextureDesc yDesc;
218 yDesc.fConfig = kConfig;
219 yDesc.fOrigin = origin;
220 yDesc.fSampleCnt = 0;
221 yDesc.fTextureHandle = yuvTextureHandles[0];
222 yDesc.fWidth = yuvSizes[0].fWidth;
223 yDesc.fHeight = yuvSizes[0].fHeight;
224
225 GrBackendTextureDesc uDesc;
226 uDesc.fConfig = kConfig;
227 uDesc.fOrigin = origin;
228 uDesc.fSampleCnt = 0;
229 uDesc.fTextureHandle = yuvTextureHandles[1];
230 uDesc.fWidth = yuvSizes[1].fWidth;
231 uDesc.fHeight = yuvSizes[1].fHeight;
232
jbaumanb445a572016-06-09 13:24:48 -0700233 sk_sp<GrTexture> yTex(
234 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
235 sk_sp<GrTexture> uTex(
236 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
237 sk_sp<GrTexture> vTex;
238 if (nv12) {
239 vTex = uTex;
240 } else {
241 GrBackendTextureDesc vDesc;
242 vDesc.fConfig = kConfig;
243 vDesc.fOrigin = origin;
244 vDesc.fSampleCnt = 0;
245 vDesc.fTextureHandle = yuvTextureHandles[2];
246 vDesc.fWidth = yuvSizes[2].fWidth;
247 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700248
jbaumanb445a572016-06-09 13:24:48 -0700249 vTex = sk_sp<GrTexture>(
250 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
251 }
bsalomon993a4212015-05-29 11:37:25 -0700252 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700253 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700254 }
255
robertphillipsd4c741e2016-04-28 09:55:15 -0700256 const int width = yuvSizes[0].fWidth;
257 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700258
robertphillipsd4c741e2016-04-28 09:55:15 -0700259 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400260 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
261 SkBackingFit::kExact,
262 width, height,
263 kRGBA_8888_GrPixelConfig,
264 std::move(imageColorSpace),
265 0,
266 origin));
267 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700268 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700269 }
270
271 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400272 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700273 paint.addColorFragmentProcessor(
274 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700275
robertphillipsd4c741e2016-04-28 09:55:15 -0700276 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700277
Brian Osman11052242016-10-27 14:47:55 -0400278 renderTargetContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500279
280 if (!renderTargetContext->accessRenderTarget()) {
281 return nullptr;
282 }
Brian Osman11052242016-10-27 14:47:55 -0400283 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700284 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400285 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
286 sk_ref_sp(renderTargetContext->getColorSpace()), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700287}
reed56179002015-07-07 06:11:19 -0700288
jbaumanb445a572016-06-09 13:24:48 -0700289sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
290 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700291 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
292 sk_sp<SkColorSpace> imageColorSpace) {
293 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
294 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700295}
296
297sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
298 const GrBackendObject yuvTextureHandles[2],
299 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700300 GrSurfaceOrigin origin,
301 sk_sp<SkColorSpace> imageColorSpace) {
302 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
303 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700304}
305
reed7fb4f8b2016-03-11 04:33:52 -0800306static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
Brian Osman7992da32016-11-18 11:28:24 -0500307 sk_sp<SkColorSpace> texColorSpace;
Brian Osman7b8400d2016-11-08 17:08:54 -0500308 sk_sp<GrTexture> texture(
Brian Salomon514baff2016-11-17 15:17:07 -0500309 maker->refTextureForParams(GrSamplerParams::ClampNoFilter(),
Brian Osman7992da32016-11-18 11:28:24 -0500310 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware,
311 &texColorSpace));
bsalomon8e74f802016-01-30 10:01:40 -0800312 if (!texture) {
313 return nullptr;
314 }
bungeman6bd52842016-10-27 09:30:08 -0700315 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture),
Brian Osman7992da32016-11-18 11:28:24 -0500316 std::move(texColorSpace), SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800317}
318
reed7fb4f8b2016-03-11 04:33:52 -0800319sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800320 if (!context) {
321 return nullptr;
322 }
323 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800324 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800325 }
bsalomon8e74f802016-01-30 10:01:40 -0800326
327 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
328 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
brianosman69c166d2016-08-17 14:01:05 -0700329 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800330 }
reed9a5a2012016-08-08 09:00:29 -0700331
332 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
333 GrBitmapTextureMaker maker(context, *bmp);
brianosman69c166d2016-08-17 14:01:05 -0700334 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800335 }
reed9a5a2012016-08-08 09:00:29 -0700336 return nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800337}
338
bsalomon634b4302016-07-12 18:11:17 -0700339sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700340 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700341 return sk_ref_sp(const_cast<SkImage*>(this));
342 }
brianosman396fcdb2016-07-22 06:26:11 -0700343 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700344 size_t rowBytes = info.minRowBytes();
345 size_t size = info.getSafeSize(rowBytes);
346 auto data = SkData::MakeUninitialized(size);
347 if (!data) {
348 return nullptr;
349 }
350 SkPixmap pm(info, data->writable_data(), rowBytes);
351 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
352 return nullptr;
353 }
354 return MakeRasterData(info, data, rowBytes);
355}
356
reed7fb4f8b2016-03-11 04:33:52 -0800357sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
358 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800359 if (!ctx) {
360 return nullptr;
361 }
bungeman6bd52842016-10-27 09:30:08 -0700362 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800363 if (!texture) {
364 return nullptr;
365 }
reed7fb4f8b2016-03-11 04:33:52 -0800366 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700367 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700368 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800369}
370
reed56179002015-07-07 06:11:19 -0700371///////////////////////////////////////////////////////////////////////////////////////////////////
372
bsalomon4d516a62016-07-28 13:37:31 -0700373namespace {
374struct MipMapLevelData {
375 void* fPixelData;
376 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800377};
378
bsalomon4d516a62016-07-28 13:37:31 -0700379struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500380 uint32_t fContextUniqueID;
381 // Right now, the destination color mode is only considered when generating mipmaps
382 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700383 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500384 int fWidth;
385 int fHeight;
386 SkColorType fColorType;
387 SkAlphaType fAlphaType;
388 void* fColorSpace;
389 size_t fColorSpaceSize;
390 int fColorTableCnt;
391 uint32_t* fColorTableData;
392 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700393 // The fMipMapLevelData array may contain more than 1 element.
394 // It contains fMipMapLevelCount elements.
395 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500396 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700397};
398} // anonymous namespace
399
cblume33e0cb52016-08-30 12:09:23 -0700400static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
401 bool shouldUseMipMaps = false;
402
403 // Use mipmaps if either
404 // 1.) it is a perspective matrix, or
405 // 2.) the quality is med/high and the scale is < 1
406 if (param.fMatrix.hasPerspective()) {
407 shouldUseMipMaps = true;
408 }
409 if (param.fQuality == kMedium_SkFilterQuality ||
410 param.fQuality == kHigh_SkFilterQuality) {
411 SkScalar minAxisScale = param.fMatrix.getMinScale();
412 if (minAxisScale != -1.f && minAxisScale < 1.f) {
413 shouldUseMipMaps = true;
414 }
415 }
416
417
418 return shouldUseMipMaps;
419}
420
421namespace {
422
423class DTIBufferFiller
424{
425public:
cblume921bc672016-09-22 05:25:26 -0700426 explicit DTIBufferFiller(char* bufferAsCharPtr)
427 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700428
429 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700430 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700431 }
432
433private:
434
cblume921bc672016-09-22 05:25:26 -0700435 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700436};
437}
438
439#define FILL_MEMBER(bufferFiller, member, source) \
440 bufferFiller.fillMember(source, \
441 offsetof(DeferredTextureImage, member), \
442 sizeof(DeferredTextureImage::member));
443
bsalomon41b952c2016-03-11 06:46:33 -0800444size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700445 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700446 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400447 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700448 // Extract relevant min/max values from the params array.
449 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
450 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700451 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700452 for (int i = 1; i < paramCnt; ++i) {
453 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
454 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
455 if (highestFilterQuality < params[i].fQuality)
456 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700457 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700458 }
459
bsalomon41b952c2016-03-11 06:46:33 -0800460 const bool fillMode = SkToBool(buffer);
461 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
462 return 0;
463 }
464
ericrkb4da01d2016-06-13 11:18:14 -0700465 // Calculate scaling parameters.
466 bool isScaled = lowestPreScaleMipLevel != 0;
467
468 SkISize scaledSize;
469 if (isScaled) {
470 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
471 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
472 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
473 lowestPreScaleMipLevel - 1);
474 } else {
475 scaledSize = SkISize::Make(this->width(), this->height());
476 }
477
478 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
479 SkFilterQuality scaleFilterQuality = highestFilterQuality;
480 if (scaleFilterQuality > kMedium_SkFilterQuality) {
481 scaleFilterQuality = kMedium_SkFilterQuality;
482 }
483
ericrkc429baf2016-03-24 15:35:45 -0700484 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700485 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700486 return 0;
487 }
488
bsalomon41b952c2016-03-11 06:46:33 -0800489 SkAutoPixmapStorage pixmap;
490 SkImageInfo info;
491 size_t pixelSize = 0;
492 size_t ctSize = 0;
493 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700494 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800495 info = pixmap.info();
496 pixelSize = SkAlign8(pixmap.getSafeSize());
497 if (pixmap.ctable()) {
498 ctCount = pixmap.ctable()->count();
499 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
500 }
501 } else {
502 // Here we're just using presence of data to know whether there is a codec behind the image.
503 // In the future we will access the cacherator and get the exact data that we want to (e.g.
504 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700505 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700506 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800507 return 0;
508 }
Brian Osman7992da32016-11-18 11:28:24 -0500509 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
510 // Generator backed image. Tweak info to trigger correct kind of decode.
511 SkDestinationSurfaceColorMode decodeColorMode = dstColorSpace
512 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
513 : SkDestinationSurfaceColorMode::kLegacy;
514 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
515 decodeColorMode, proxy.fCaps.get());
516 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
517 scaledSize.height());
518
519 } else {
520 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
521 }
bsalomon41b952c2016-03-11 06:46:33 -0800522 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
523 if (fillMode) {
524 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700525 if (isScaled) {
526 if (!this->scalePixels(pixmap, scaleFilterQuality,
527 SkImage::kDisallow_CachingHint)) {
528 return 0;
529 }
530 } else {
531 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
532 return 0;
533 }
bsalomon41b952c2016-03-11 06:46:33 -0800534 }
535 SkASSERT(!pixmap.ctable());
536 }
537 }
cblume2c052802016-05-31 09:55:08 -0700538 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700539 if (useMipMaps) {
540 // SkMipMap only deals with the mipmap levels it generates, which does
541 // not include the base level.
542 // That means it generates and holds levels 1-x instead of 0-x.
543 // So the total mipmap level count is 1 more than what
544 // SkMipMap::ComputeLevelCount returns.
545 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
546
547 // We already initialized pixelSize to the size of the base level.
548 // SkMipMap will generate the extra mipmap levels. Their sizes need to
549 // be added to the total.
550 // Index 0 here does not refer to the base mipmap level -- it is
551 // SkMipMap's first generated mipmap level (level 1).
552 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
553 currentMipMapLevelIndex--) {
554 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
555 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700556 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700557 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
558 }
559 }
bsalomon41b952c2016-03-11 06:46:33 -0800560 size_t size = 0;
561 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
562 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700563 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
564 // We subtract 1 because DeferredTextureImage already includes the base
565 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800566 size_t pixelOffset = size;
567 size += pixelSize;
568 size_t ctOffset = size;
569 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700570 size_t colorSpaceOffset = 0;
571 size_t colorSpaceSize = 0;
572 if (info.colorSpace()) {
573 colorSpaceOffset = size;
574 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
575 size += colorSpaceSize;
576 }
bsalomon41b952c2016-03-11 06:46:33 -0800577 if (!fillMode) {
578 return size;
579 }
cblume921bc672016-09-22 05:25:26 -0700580 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
581 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
582 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700583 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800584 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700585 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800586 }
587
cblume921bc672016-09-22 05:25:26 -0700588 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
589 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800590 if (ctSize) {
591 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
592 }
593
Brian Osman6c15cc72016-10-17 09:51:37 -0400594 // If the context has sRGB support, and we're intending to render to a surface with an attached
595 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
596 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500597 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400598 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
599 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500600 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400601 }
602
bsalomon41b952c2016-03-11 06:46:33 -0800603 SkASSERT(info == pixmap.info());
604 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700605 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
606 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700607 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500608 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700609 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
610 int width = info.width();
611 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
612 int height = info.height();
613 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
614 SkColorType colorType = info.colorType();
615 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
616 SkAlphaType alphaType = info.alphaType();
617 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
618 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
619 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
620 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700621 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700622 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700623 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700624 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700625 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700626 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700627 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
628 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700629 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700630 } else {
cblume921bc672016-09-22 05:25:26 -0700631 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700632 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700633 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700634 0, sizeof(DeferredTextureImage::fColorSpaceSize));
635 }
636
637 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700638 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700639
640 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700641 static_assert(std::is_standard_layout<MipMapLevelData>::value,
642 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700643
Brian Osman7b8400d2016-11-08 17:08:54 -0500644 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700645 // SkMipMap holds only the mipmap levels it generates.
646 // A programmer can use the data they provided to SkMipMap::Build as level 0.
647 // So the SkMipMap provides levels 1-x but it stores them in its own
648 // range 0-(x-1).
649 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
650 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700651 SkMipMap::Level mipLevel;
652 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
653
654 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700655 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700656 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700657 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700658 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700659 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700660 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700661
662 // getSafeSize includes rowbyte padding except for the last row,
663 // right?
664
cblume921bc672016-09-22 05:25:26 -0700665 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700666
cblume921bc672016-09-22 05:25:26 -0700667 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
668 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
669 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700670 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700671 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
672 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
673 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700674
675 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
676 }
bsalomon4d516a62016-07-28 13:37:31 -0700677 }
bsalomon41b952c2016-03-11 06:46:33 -0800678 return size;
679}
680
681sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
682 SkBudgeted budgeted) {
683 if (!data) {
684 return nullptr;
685 }
686 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
687
688 if (!context || context->uniqueID() != dti->fContextUniqueID) {
689 return nullptr;
690 }
Hal Canary67b39de2016-11-07 11:47:44 -0500691 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700692 if (dti->fColorTableCnt) {
693 SkASSERT(dti->fColorTableData);
694 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800695 }
cblume33e0cb52016-08-30 12:09:23 -0700696 int mipLevelCount = dti->fMipMapLevelCount;
697 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700698 sk_sp<SkColorSpace> colorSpace;
699 if (dti->fColorSpaceSize) {
700 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
701 }
702 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
703 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700704 if (mipLevelCount == 1) {
705 SkPixmap pixmap;
706 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
707 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
708 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
709 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400710 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700711 for (int i = 0; i < mipLevelCount; i++) {
712 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
713 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
714 }
715
716 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
717 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500718 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700719 }
bsalomon41b952c2016-03-11 06:46:33 -0800720}
721
722///////////////////////////////////////////////////////////////////////////////////////////////////
723
cblume186d2d42016-06-03 11:17:42 -0700724sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
725 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700726 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500727 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700728 if (!ctx) {
729 return nullptr;
730 }
bungeman6bd52842016-10-27 09:30:08 -0700731 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700732 if (!texture) {
733 return nullptr;
734 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500735 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700736 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700737 info.alphaType(), std::move(texture),
738 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700739}