blob: 780bfbe4bf60f179a94e1cdc0e1f69dcfdb2cb3e [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"
cblume33e0cb52016-08-30 12:09:23 -070018#include "GrTexturePriv.h"
bsalomonf267c1e2016-02-01 13:16:14 -080019#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070020#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080021#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070022#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080023#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050024#include "SkImageCacherator.h"
ericrkb4da01d2016-06-13 11:18:14 -070025#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070026#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070027
bungeman6bd52842016-10-27 09:30:08 -070028SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070029 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070030 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070031 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070032 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080033 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070034 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070035 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070036{
bungeman6bd52842016-10-27 09:30:08 -070037 SkASSERT(fTexture->width() == w);
38 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070039}
piotaixrcef04f82014-07-14 07:48:04 -070040
reed6f1216a2015-08-04 08:10:13 -070041SkImage_Gpu::~SkImage_Gpu() {
42 if (fAddedRasterVersionToCache.load()) {
43 SkNotifyBitmapGenIDIsStale(this->uniqueID());
44 }
45}
46
bsalomoneaaaf0b2015-01-23 08:08:04 -080047extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070048 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070049 ((SkImage_Gpu*)image)->applyBudgetDecision();
50 }
51}
52
brianosman396fcdb2016-07-22 06:26:11 -070053SkImageInfo SkImage_Gpu::onImageInfo() const {
54 SkColorType ct;
55 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
56 ct = kUnknown_SkColorType;
57 }
58 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
59}
60
brianosman69c166d2016-08-17 14:01:05 -070061static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
62 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070063}
64
Brian Osman7992da32016-11-18 11:28:24 -050065bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkDestinationSurfaceColorMode,
66 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070067 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
68 SkASSERT(dst->getGenerationID() == this->uniqueID());
69 SkASSERT(dst->isImmutable());
70 SkASSERT(dst->getPixels());
71 return true;
72 }
73
brianosman69c166d2016-08-17 14:01:05 -070074 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070075 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070076 return false;
77 }
78 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
79 dst->getPixels(), dst->rowBytes())) {
80 return false;
81 }
reed6f1216a2015-08-04 08:10:13 -070082
83 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080084 if (kAllow_CachingHint == chint) {
85 SkBitmapCache::Add(this->uniqueID(), *dst);
86 fAddedRasterVersionToCache.store(true);
87 }
reed8b26b992015-05-07 15:36:17 -070088 return true;
89}
90
Brian Salomon514baff2016-11-17 15:17:07 -050091GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman7992da32016-11-18 11:28:24 -050092 SkDestinationSurfaceColorMode colorMode,
93 sk_sp<SkColorSpace>* texColorSpace) const {
94 if (texColorSpace) {
95 *texColorSpace = this->fColorSpace;
96 }
97 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
98 this->uniqueID(), this->fColorSpace.get());
Brian Osman7b8400d2016-11-08 17:08:54 -050099 return adjuster.refTextureSafeForParams(params, colorMode, nullptr);
reed85d91782015-09-10 14:33:38 -0700100}
101
reed8b26b992015-05-07 15:36:17 -0700102static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
103 switch (info.colorType()) {
104 case kRGBA_8888_SkColorType:
105 case kBGRA_8888_SkColorType:
106 break;
107 default:
108 return; // nothing to do
109 }
110
111 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
112 // and in either case, the alpha-byte is always in the same place, so we can safely call
113 // SkPreMultiplyColor()
114 //
115 SkColor* row = (SkColor*)pixels;
116 for (int y = 0; y < info.height(); ++y) {
117 for (int x = 0; x < info.width(); ++x) {
118 row[x] = SkPreMultiplyColor(row[x]);
119 }
120 }
121}
122
123bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800124 int srcX, int srcY, CachingHint) const {
brianosmanb109b8c2016-06-16 13:03:24 -0700125 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700126 uint32_t flags = 0;
127 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
128 // let the GPU perform this transformation for us
129 flags = GrContext::kUnpremul_PixelOpsFlag;
130 }
131 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
132 pixels, rowBytes, flags)) {
133 return false;
134 }
135 // do we have to manually fix-up the alpha channel?
136 // src dst
137 // unpremul premul fix manually
138 // premul unpremul done by kUnpremul_PixelOpsFlag
139 // all other combos need to change.
140 //
141 // Should this be handled by Ganesh? todo:?
142 //
143 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
144 apply_premul(info, pixels, rowBytes);
145 }
146 return true;
147}
148
reed7fb4f8b2016-03-11 04:33:52 -0800149sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700150 GrContext* ctx = fTexture->getContext();
151 GrSurfaceDesc desc = fTexture->desc();
152 desc.fWidth = subset.width();
153 desc.fHeight = subset.height();
154
senorblanco87751122016-05-20 07:27:38 -0700155 sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted));
reed7b6945b2015-09-24 00:50:58 -0700156 if (!subTx) {
157 return nullptr;
158 }
bungeman6bd52842016-10-27 09:30:08 -0700159 ctx->copySurface(subTx.get(), fTexture.get(), subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800160 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700161 fAlphaType, std::move(subTx), fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700162}
163
reed8b26b992015-05-07 15:36:17 -0700164///////////////////////////////////////////////////////////////////////////////////////////////////
165
reed7fb4f8b2016-03-11 04:33:52 -0800166static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700167 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
168 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800169 SkImage::TextureReleaseProc releaseProc,
170 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700171 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700172 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700173 }
bungeman6bd52842016-10-27 09:30:08 -0700174 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700175 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700177 }
reedde499882015-06-18 13:41:40 -0700178 if (releaseProc) {
179 tex->setRelease(releaseProc, releaseCtx);
180 }
181
bsalomon5ec26ae2016-02-25 08:33:02 -0800182 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800183 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700184 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700185}
186
reed7fb4f8b2016-03-11 04:33:52 -0800187sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700188 SkAlphaType at, sk_sp<SkColorSpace> cs,
189 TextureReleaseProc releaseP, ReleaseContext releaseC) {
190 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
191 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700192}
193
reed7fb4f8b2016-03-11 04:33:52 -0800194sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700195 SkAlphaType at, sk_sp<SkColorSpace> cs) {
196 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
197 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700198}
199
jbaumanb445a572016-06-09 13:24:48 -0700200static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
201 bool nv12,
202 const GrBackendObject yuvTextureHandles[],
203 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700204 GrSurfaceOrigin origin,
205 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800206 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700207
jbaumanb445a572016-06-09 13:24:48 -0700208 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
209 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700211 }
jbaumanb445a572016-06-09 13:24:48 -0700212 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
213 return nullptr;
214 }
215
216 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
217
bsalomon993a4212015-05-29 11:37:25 -0700218 GrBackendTextureDesc yDesc;
219 yDesc.fConfig = kConfig;
220 yDesc.fOrigin = origin;
221 yDesc.fSampleCnt = 0;
222 yDesc.fTextureHandle = yuvTextureHandles[0];
223 yDesc.fWidth = yuvSizes[0].fWidth;
224 yDesc.fHeight = yuvSizes[0].fHeight;
225
226 GrBackendTextureDesc uDesc;
227 uDesc.fConfig = kConfig;
228 uDesc.fOrigin = origin;
229 uDesc.fSampleCnt = 0;
230 uDesc.fTextureHandle = yuvTextureHandles[1];
231 uDesc.fWidth = yuvSizes[1].fWidth;
232 uDesc.fHeight = yuvSizes[1].fHeight;
233
jbaumanb445a572016-06-09 13:24:48 -0700234 sk_sp<GrTexture> yTex(
235 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
236 sk_sp<GrTexture> uTex(
237 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
238 sk_sp<GrTexture> vTex;
239 if (nv12) {
240 vTex = uTex;
241 } else {
242 GrBackendTextureDesc vDesc;
243 vDesc.fConfig = kConfig;
244 vDesc.fOrigin = origin;
245 vDesc.fSampleCnt = 0;
246 vDesc.fTextureHandle = yuvTextureHandles[2];
247 vDesc.fWidth = yuvSizes[2].fWidth;
248 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700249
jbaumanb445a572016-06-09 13:24:48 -0700250 vTex = sk_sp<GrTexture>(
251 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
252 }
bsalomon993a4212015-05-29 11:37:25 -0700253 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700254 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700255 }
256
robertphillipsd4c741e2016-04-28 09:55:15 -0700257 const int width = yuvSizes[0].fWidth;
258 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700259
robertphillipsd4c741e2016-04-28 09:55:15 -0700260 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400261 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
262 SkBackingFit::kExact,
263 width, height,
264 kRGBA_8888_GrPixelConfig,
265 std::move(imageColorSpace),
266 0,
267 origin));
268 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700269 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700270 }
271
272 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400273 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700274 paint.addColorFragmentProcessor(
275 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700276
robertphillipsd4c741e2016-04-28 09:55:15 -0700277 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700278
Brian Osman11052242016-10-27 14:47:55 -0400279 renderTargetContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500280
281 if (!renderTargetContext->accessRenderTarget()) {
282 return nullptr;
283 }
Brian Osman11052242016-10-27 14:47:55 -0400284 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700285 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400286 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
287 sk_ref_sp(renderTargetContext->getColorSpace()), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700288}
reed56179002015-07-07 06:11:19 -0700289
jbaumanb445a572016-06-09 13:24:48 -0700290sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
291 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700292 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
293 sk_sp<SkColorSpace> imageColorSpace) {
294 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
295 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700296}
297
298sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
299 const GrBackendObject yuvTextureHandles[2],
300 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700301 GrSurfaceOrigin origin,
302 sk_sp<SkColorSpace> imageColorSpace) {
303 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
304 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700305}
306
reed7fb4f8b2016-03-11 04:33:52 -0800307static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
Brian Osman7992da32016-11-18 11:28:24 -0500308 sk_sp<SkColorSpace> texColorSpace;
Brian Osman7b8400d2016-11-08 17:08:54 -0500309 sk_sp<GrTexture> texture(
Brian Salomon514baff2016-11-17 15:17:07 -0500310 maker->refTextureForParams(GrSamplerParams::ClampNoFilter(),
Brian Osman7992da32016-11-18 11:28:24 -0500311 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware,
312 &texColorSpace));
bsalomon8e74f802016-01-30 10:01:40 -0800313 if (!texture) {
314 return nullptr;
315 }
bungeman6bd52842016-10-27 09:30:08 -0700316 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture),
Brian Osman7992da32016-11-18 11:28:24 -0500317 std::move(texColorSpace), SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800318}
319
reed7fb4f8b2016-03-11 04:33:52 -0800320sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800321 if (!context) {
322 return nullptr;
323 }
324 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800325 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800326 }
bsalomon8e74f802016-01-30 10:01:40 -0800327
328 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
329 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
brianosman69c166d2016-08-17 14:01:05 -0700330 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800331 }
reed9a5a2012016-08-08 09:00:29 -0700332
333 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
334 GrBitmapTextureMaker maker(context, *bmp);
brianosman69c166d2016-08-17 14:01:05 -0700335 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800336 }
reed9a5a2012016-08-08 09:00:29 -0700337 return nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800338}
339
bsalomon634b4302016-07-12 18:11:17 -0700340sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700341 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700342 return sk_ref_sp(const_cast<SkImage*>(this));
343 }
brianosman396fcdb2016-07-22 06:26:11 -0700344 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700345 size_t rowBytes = info.minRowBytes();
346 size_t size = info.getSafeSize(rowBytes);
347 auto data = SkData::MakeUninitialized(size);
348 if (!data) {
349 return nullptr;
350 }
351 SkPixmap pm(info, data->writable_data(), rowBytes);
352 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
353 return nullptr;
354 }
355 return MakeRasterData(info, data, rowBytes);
356}
357
reed7fb4f8b2016-03-11 04:33:52 -0800358sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
359 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800360 if (!ctx) {
361 return nullptr;
362 }
bungeman6bd52842016-10-27 09:30:08 -0700363 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800364 if (!texture) {
365 return nullptr;
366 }
reed7fb4f8b2016-03-11 04:33:52 -0800367 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700368 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700369 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800370}
371
reed56179002015-07-07 06:11:19 -0700372///////////////////////////////////////////////////////////////////////////////////////////////////
373
bsalomon4d516a62016-07-28 13:37:31 -0700374namespace {
375struct MipMapLevelData {
376 void* fPixelData;
377 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800378};
379
bsalomon4d516a62016-07-28 13:37:31 -0700380struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500381 uint32_t fContextUniqueID;
382 // Right now, the destination color mode is only considered when generating mipmaps
383 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700384 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500385 int fWidth;
386 int fHeight;
387 SkColorType fColorType;
388 SkAlphaType fAlphaType;
389 void* fColorSpace;
390 size_t fColorSpaceSize;
391 int fColorTableCnt;
392 uint32_t* fColorTableData;
393 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700394 // The fMipMapLevelData array may contain more than 1 element.
395 // It contains fMipMapLevelCount elements.
396 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500397 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700398};
399} // anonymous namespace
400
cblume33e0cb52016-08-30 12:09:23 -0700401static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
402 bool shouldUseMipMaps = false;
403
404 // Use mipmaps if either
405 // 1.) it is a perspective matrix, or
406 // 2.) the quality is med/high and the scale is < 1
407 if (param.fMatrix.hasPerspective()) {
408 shouldUseMipMaps = true;
409 }
410 if (param.fQuality == kMedium_SkFilterQuality ||
411 param.fQuality == kHigh_SkFilterQuality) {
412 SkScalar minAxisScale = param.fMatrix.getMinScale();
413 if (minAxisScale != -1.f && minAxisScale < 1.f) {
414 shouldUseMipMaps = true;
415 }
416 }
417
418
419 return shouldUseMipMaps;
420}
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;
493 size_t ctSize = 0;
494 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700495 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800496 info = pixmap.info();
497 pixelSize = SkAlign8(pixmap.getSafeSize());
498 if (pixmap.ctable()) {
499 ctCount = pixmap.ctable()->count();
500 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
501 }
502 } else {
503 // Here we're just using presence of data to know whether there is a codec behind the image.
504 // In the future we will access the cacherator and get the exact data that we want to (e.g.
505 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700506 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700507 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800508 return 0;
509 }
Brian Osman7992da32016-11-18 11:28:24 -0500510 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
511 // Generator backed image. Tweak info to trigger correct kind of decode.
512 SkDestinationSurfaceColorMode decodeColorMode = dstColorSpace
513 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
514 : SkDestinationSurfaceColorMode::kLegacy;
515 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
516 decodeColorMode, proxy.fCaps.get());
517 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
518 scaledSize.height());
519
520 } else {
521 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
522 }
bsalomon41b952c2016-03-11 06:46:33 -0800523 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
524 if (fillMode) {
525 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700526 if (isScaled) {
527 if (!this->scalePixels(pixmap, scaleFilterQuality,
528 SkImage::kDisallow_CachingHint)) {
529 return 0;
530 }
531 } else {
532 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
533 return 0;
534 }
bsalomon41b952c2016-03-11 06:46:33 -0800535 }
536 SkASSERT(!pixmap.ctable());
537 }
538 }
cblume2c052802016-05-31 09:55:08 -0700539 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700540 if (useMipMaps) {
541 // SkMipMap only deals with the mipmap levels it generates, which does
542 // not include the base level.
543 // That means it generates and holds levels 1-x instead of 0-x.
544 // So the total mipmap level count is 1 more than what
545 // SkMipMap::ComputeLevelCount returns.
546 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
547
548 // We already initialized pixelSize to the size of the base level.
549 // SkMipMap will generate the extra mipmap levels. Their sizes need to
550 // be added to the total.
551 // Index 0 here does not refer to the base mipmap level -- it is
552 // SkMipMap's first generated mipmap level (level 1).
553 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
554 currentMipMapLevelIndex--) {
555 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
556 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700557 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700558 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
559 }
560 }
bsalomon41b952c2016-03-11 06:46:33 -0800561 size_t size = 0;
562 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
563 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700564 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
565 // We subtract 1 because DeferredTextureImage already includes the base
566 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800567 size_t pixelOffset = size;
568 size += pixelSize;
569 size_t ctOffset = size;
570 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700571 size_t colorSpaceOffset = 0;
572 size_t colorSpaceSize = 0;
573 if (info.colorSpace()) {
574 colorSpaceOffset = size;
575 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
576 size += colorSpaceSize;
577 }
bsalomon41b952c2016-03-11 06:46:33 -0800578 if (!fillMode) {
579 return size;
580 }
cblume921bc672016-09-22 05:25:26 -0700581 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
582 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
583 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700584 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800585 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700586 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800587 }
588
cblume921bc672016-09-22 05:25:26 -0700589 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
590 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800591 if (ctSize) {
592 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
593 }
594
Brian Osman6c15cc72016-10-17 09:51:37 -0400595 // If the context has sRGB support, and we're intending to render to a surface with an attached
596 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
597 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500598 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400599 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
600 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500601 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400602 }
603
bsalomon41b952c2016-03-11 06:46:33 -0800604 SkASSERT(info == pixmap.info());
605 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700606 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
607 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700608 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500609 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700610 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
611 int width = info.width();
612 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
613 int height = info.height();
614 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
615 SkColorType colorType = info.colorType();
616 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
617 SkAlphaType alphaType = info.alphaType();
618 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
619 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
620 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
621 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700622 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700623 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700624 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700625 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700626 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700627 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700628 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
629 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700630 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700631 } else {
cblume921bc672016-09-22 05:25:26 -0700632 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700633 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700634 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700635 0, sizeof(DeferredTextureImage::fColorSpaceSize));
636 }
637
638 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700639 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700640
641 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700642 static_assert(std::is_standard_layout<MipMapLevelData>::value,
643 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700644
Brian Osman7b8400d2016-11-08 17:08:54 -0500645 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700646 // SkMipMap holds only the mipmap levels it generates.
647 // A programmer can use the data they provided to SkMipMap::Build as level 0.
648 // So the SkMipMap provides levels 1-x but it stores them in its own
649 // range 0-(x-1).
650 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
651 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700652 SkMipMap::Level mipLevel;
653 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
654
655 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700656 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700657 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700658 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700659 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700660 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700661 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700662
663 // getSafeSize includes rowbyte padding except for the last row,
664 // right?
665
cblume921bc672016-09-22 05:25:26 -0700666 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700667
cblume921bc672016-09-22 05:25:26 -0700668 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
669 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
670 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700671 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700672 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
673 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
674 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700675
676 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
677 }
bsalomon4d516a62016-07-28 13:37:31 -0700678 }
bsalomon41b952c2016-03-11 06:46:33 -0800679 return size;
680}
681
682sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
683 SkBudgeted budgeted) {
684 if (!data) {
685 return nullptr;
686 }
687 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
688
689 if (!context || context->uniqueID() != dti->fContextUniqueID) {
690 return nullptr;
691 }
Hal Canary67b39de2016-11-07 11:47:44 -0500692 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700693 if (dti->fColorTableCnt) {
694 SkASSERT(dti->fColorTableData);
695 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800696 }
cblume33e0cb52016-08-30 12:09:23 -0700697 int mipLevelCount = dti->fMipMapLevelCount;
698 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700699 sk_sp<SkColorSpace> colorSpace;
700 if (dti->fColorSpaceSize) {
701 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
702 }
703 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
704 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700705 if (mipLevelCount == 1) {
706 SkPixmap pixmap;
707 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
708 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
709 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
710 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400711 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700712 for (int i = 0; i < mipLevelCount; i++) {
713 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
714 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
715 }
716
717 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
718 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500719 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700720 }
bsalomon41b952c2016-03-11 06:46:33 -0800721}
722
723///////////////////////////////////////////////////////////////////////////////////////////////////
724
cblume186d2d42016-06-03 11:17:42 -0700725sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
726 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700727 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500728 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700729 if (!ctx) {
730 return nullptr;
731 }
bungeman6bd52842016-10-27 09:30:08 -0700732 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700733 if (!texture) {
734 return nullptr;
735 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500736 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700737 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700738 info.alphaType(), std::move(texture),
739 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700740}