blob: 98a7101e6b1b8e22a7dbc1ccceedcad271da59bb [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
cblume33e0cb52016-08-30 12:09:23 -07008#include <cstddef>
9#include <cstring>
10#include <type_traits>
11
robertphillipsc5035e72016-03-17 06:58:39 -070012#include "SkAutoPixmapStorage.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050013#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070014#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050016#include "GrContextPriv.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
Brian Osmane8e54582016-11-28 10:06:27 -050019#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070020#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050021#include "GrTextureProxy.h"
bsalomonf267c1e2016-02-01 13:16:14 -080022#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070023#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080024#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070025#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080026#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050027#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050028#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070029#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070030#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050031#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070032
bungeman6bd52842016-10-27 09:30:08 -070033SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070034 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070035 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070036 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070037 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080038 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070039 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070040 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070041{
bungeman6bd52842016-10-27 09:30:08 -070042 SkASSERT(fTexture->width() == w);
43 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070044}
piotaixrcef04f82014-07-14 07:48:04 -070045
reed6f1216a2015-08-04 08:10:13 -070046SkImage_Gpu::~SkImage_Gpu() {
47 if (fAddedRasterVersionToCache.load()) {
48 SkNotifyBitmapGenIDIsStale(this->uniqueID());
49 }
50}
51
bsalomoneaaaf0b2015-01-23 08:08:04 -080052extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070053 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070054 ((SkImage_Gpu*)image)->applyBudgetDecision();
55 }
56}
57
brianosman396fcdb2016-07-22 06:26:11 -070058SkImageInfo SkImage_Gpu::onImageInfo() const {
59 SkColorType ct;
60 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
61 ct = kUnknown_SkColorType;
62 }
63 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
64}
65
brianosman69c166d2016-08-17 14:01:05 -070066static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
67 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070068}
69
Brian Osman61624f02016-12-09 14:51:59 -050070bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050071 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070072 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
73 SkASSERT(dst->getGenerationID() == this->uniqueID());
74 SkASSERT(dst->isImmutable());
75 SkASSERT(dst->getPixels());
76 return true;
77 }
78
brianosman69c166d2016-08-17 14:01:05 -070079 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070080 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070081 return false;
82 }
83 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
84 dst->getPixels(), dst->rowBytes())) {
85 return false;
86 }
reed6f1216a2015-08-04 08:10:13 -070087
88 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080089 if (kAllow_CachingHint == chint) {
90 SkBitmapCache::Add(this->uniqueID(), *dst);
91 fAddedRasterVersionToCache.store(true);
92 }
reed8b26b992015-05-07 15:36:17 -070093 return true;
94}
95
Robert Phillips6de99042017-01-31 11:31:39 -050096sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef() const {
97 sk_sp<GrSurfaceProxy> sProxy = GrSurfaceProxy::MakeWrapped(fTexture);
98 if (!sProxy) {
99 return nullptr;
100 }
101
102 return sk_ref_sp(sProxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500103}
104
Brian Salomon514baff2016-11-17 15:17:07 -0500105GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500106 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500107 sk_sp<SkColorSpace>* texColorSpace,
108 SkScalar scaleAdjust[2]) const {
Brian Osman7992da32016-11-18 11:28:24 -0500109 if (texColorSpace) {
110 *texColorSpace = this->fColorSpace;
111 }
Robert Phillips6de99042017-01-31 11:31:39 -0500112 GrTextureAdjuster adjuster(fTexture.get(), this->alphaType(), this->bounds(),
Brian Osman7992da32016-11-18 11:28:24 -0500113 this->uniqueID(), this->fColorSpace.get());
Robert Phillips67c18d62017-01-20 12:44:06 -0500114 return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700115}
116
reed8b26b992015-05-07 15:36:17 -0700117static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
118 switch (info.colorType()) {
119 case kRGBA_8888_SkColorType:
120 case kBGRA_8888_SkColorType:
121 break;
122 default:
123 return; // nothing to do
124 }
125
126 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
127 // and in either case, the alpha-byte is always in the same place, so we can safely call
128 // SkPreMultiplyColor()
129 //
130 SkColor* row = (SkColor*)pixels;
131 for (int y = 0; y < info.height(); ++y) {
132 for (int x = 0; x < info.width(); ++x) {
133 row[x] = SkPreMultiplyColor(row[x]);
134 }
135 }
136}
137
Matt Sarett03dd6d52017-01-23 12:15:09 -0500138bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800139 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500140 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500141 return false;
142 }
143
Matt Sarett03dd6d52017-01-23 12:15:09 -0500144 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
145 if (!rec.trim(this->width(), this->height())) {
146 return false;
147 }
148
149 GrPixelConfig config = SkImageInfo2GrPixelConfig(rec.fInfo, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700150 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500151 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700152 // let the GPU perform this transformation for us
153 flags = GrContext::kUnpremul_PixelOpsFlag;
154 }
Matt Sarett03dd6d52017-01-23 12:15:09 -0500155 if (!fTexture->readPixels(fColorSpace.get(), rec.fX, rec.fY, rec.fInfo.width(),
156 rec.fInfo.height(), config, rec.fInfo.colorSpace(), rec.fPixels,
157 rec.fRowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700158 return false;
159 }
160 // do we have to manually fix-up the alpha channel?
161 // src dst
162 // unpremul premul fix manually
163 // premul unpremul done by kUnpremul_PixelOpsFlag
164 // all other combos need to change.
165 //
166 // Should this be handled by Ganesh? todo:?
167 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500168 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
169 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700170 }
171 return true;
172}
173
reed7fb4f8b2016-03-11 04:33:52 -0800174sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700175 GrContext* ctx = fTexture->getContext();
176 GrSurfaceDesc desc = fTexture->desc();
177 desc.fWidth = subset.width();
178 desc.fHeight = subset.height();
179
Robert Phillipse2f7d182016-12-15 09:23:05 -0500180 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
181 desc,
182 SkBackingFit::kExact,
183 fBudgeted));
184 if (!sContext) {
185 return nullptr;
186 }
187
188 // TODO: make gpu images be proxy-backed so we don't need to do this
189 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
190 if (!tmpSrc) {
191 return nullptr;
192 }
193
194 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
195 return nullptr;
196 }
197
198 // TODO: make gpu images be proxy-backed so we don't need to do this
Robert Phillipsf200a902017-01-30 13:27:37 -0500199 GrSurface* subTx = sContext->asSurfaceProxy()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700200 if (!subTx) {
201 return nullptr;
202 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500203
reed7fb4f8b2016-03-11 04:33:52 -0800204 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500205 fAlphaType, sk_ref_sp(subTx->asTexture()),
206 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700207}
208
reed8b26b992015-05-07 15:36:17 -0700209///////////////////////////////////////////////////////////////////////////////////////////////////
210
reed7fb4f8b2016-03-11 04:33:52 -0800211static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700212 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
213 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800214 SkImage::TextureReleaseProc releaseProc,
215 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700216 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700217 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700218 }
bungeman6bd52842016-10-27 09:30:08 -0700219 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700220 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700221 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700222 }
reedde499882015-06-18 13:41:40 -0700223 if (releaseProc) {
224 tex->setRelease(releaseProc, releaseCtx);
225 }
226
bsalomon5ec26ae2016-02-25 08:33:02 -0800227 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800228 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700229 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230}
231
reed7fb4f8b2016-03-11 04:33:52 -0800232sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700233 SkAlphaType at, sk_sp<SkColorSpace> cs,
234 TextureReleaseProc releaseP, ReleaseContext releaseC) {
235 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
236 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700237}
238
reed7fb4f8b2016-03-11 04:33:52 -0800239sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700240 SkAlphaType at, sk_sp<SkColorSpace> cs) {
241 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
242 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700243}
244
jbaumanb445a572016-06-09 13:24:48 -0700245static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
246 bool nv12,
247 const GrBackendObject yuvTextureHandles[],
248 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700249 GrSurfaceOrigin origin,
250 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800251 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700252
jbaumanb445a572016-06-09 13:24:48 -0700253 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
254 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700255 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700256 }
jbaumanb445a572016-06-09 13:24:48 -0700257 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
258 return nullptr;
259 }
260
261 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
262
bsalomon993a4212015-05-29 11:37:25 -0700263 GrBackendTextureDesc yDesc;
264 yDesc.fConfig = kConfig;
265 yDesc.fOrigin = origin;
266 yDesc.fSampleCnt = 0;
267 yDesc.fTextureHandle = yuvTextureHandles[0];
268 yDesc.fWidth = yuvSizes[0].fWidth;
269 yDesc.fHeight = yuvSizes[0].fHeight;
270
271 GrBackendTextureDesc uDesc;
272 uDesc.fConfig = kConfig;
273 uDesc.fOrigin = origin;
274 uDesc.fSampleCnt = 0;
275 uDesc.fTextureHandle = yuvTextureHandles[1];
276 uDesc.fWidth = yuvSizes[1].fWidth;
277 uDesc.fHeight = yuvSizes[1].fHeight;
278
Robert Phillips26caf892017-01-27 10:58:31 -0500279 sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yDesc);
280 sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uDesc);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500281 sk_sp<GrSurfaceProxy> vProxy;
282
jbaumanb445a572016-06-09 13:24:48 -0700283 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500284 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700285 } else {
286 GrBackendTextureDesc vDesc;
287 vDesc.fConfig = kConfig;
288 vDesc.fOrigin = origin;
289 vDesc.fSampleCnt = 0;
290 vDesc.fTextureHandle = yuvTextureHandles[2];
291 vDesc.fWidth = yuvSizes[2].fWidth;
292 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700293
Robert Phillips26caf892017-01-27 10:58:31 -0500294 vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vDesc);
jbaumanb445a572016-06-09 13:24:48 -0700295 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500296 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700297 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700298 }
299
robertphillipsd4c741e2016-04-28 09:55:15 -0700300 const int width = yuvSizes[0].fWidth;
301 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700302
robertphillipsd4c741e2016-04-28 09:55:15 -0700303 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400304 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
305 SkBackingFit::kExact,
306 width, height,
307 kRGBA_8888_GrPixelConfig,
308 std::move(imageColorSpace),
309 0,
310 origin));
311 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700312 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700313 }
314
315 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400316 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700317 paint.addColorFragmentProcessor(
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500318 GrYUVEffect::MakeYUVToRGB(ctx,
319 sk_ref_sp(yProxy->asTextureProxy()),
320 sk_ref_sp(uProxy->asTextureProxy()),
321 sk_ref_sp(vProxy->asTextureProxy()), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700322
robertphillipsd4c741e2016-04-28 09:55:15 -0700323 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700324
Brian Salomon82f44312017-01-11 13:42:54 -0500325 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500326
327 if (!renderTargetContext->accessRenderTarget()) {
328 return nullptr;
329 }
Brian Osman11052242016-10-27 14:47:55 -0400330 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700331 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400332 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500333 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700334}
reed56179002015-07-07 06:11:19 -0700335
jbaumanb445a572016-06-09 13:24:48 -0700336sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
337 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700338 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
339 sk_sp<SkColorSpace> imageColorSpace) {
340 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
341 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700342}
343
344sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
345 const GrBackendObject yuvTextureHandles[2],
346 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700347 GrSurfaceOrigin origin,
348 sk_sp<SkColorSpace> imageColorSpace) {
349 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
350 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700351}
352
Brian Osman041f7df2017-02-07 11:23:28 -0500353static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id,
354 SkColorSpace* dstColorSpace) {
355 sk_sp<SkColorSpace> texColorSpace;
356 sk_sp<GrTexture> texture(maker->refTextureForParams(GrSamplerParams::ClampNoFilter(),
357 dstColorSpace, &texColorSpace, nullptr));
358 if (!texture) {
359 return nullptr;
360 }
361 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture),
362 std::move(texColorSpace), SkBudgeted::kNo);
363}
364
365sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context, SkColorSpace* dstColorSpace) const {
366 if (!context) {
367 return nullptr;
368 }
369 if (GrTexture* peek = as_IB(this)->peekTexture()) {
370 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
371 }
372
373 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
374 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
375 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace);
376 }
377
378 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
379 GrBitmapTextureMaker maker(context, *bmp);
380 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace);
381 }
382 return nullptr;
383}
384
bsalomon634b4302016-07-12 18:11:17 -0700385sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700386 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700387 return sk_ref_sp(const_cast<SkImage*>(this));
388 }
brianosman396fcdb2016-07-22 06:26:11 -0700389 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700390 size_t rowBytes = info.minRowBytes();
391 size_t size = info.getSafeSize(rowBytes);
392 auto data = SkData::MakeUninitialized(size);
393 if (!data) {
394 return nullptr;
395 }
396 SkPixmap pm(info, data->writable_data(), rowBytes);
397 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
398 return nullptr;
399 }
400 return MakeRasterData(info, data, rowBytes);
401}
402
Brian Osmanb92234a2017-01-25 14:13:00 +0000403sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
404 SkBudgeted budgeted) {
405 if (!ctx) {
406 return nullptr;
407 }
408 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
409 if (!texture) {
410 return nullptr;
411 }
412 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
413 pixmap.alphaType(), std::move(texture),
414 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
415}
416
reed56179002015-07-07 06:11:19 -0700417///////////////////////////////////////////////////////////////////////////////////////////////////
418
bsalomon4d516a62016-07-28 13:37:31 -0700419namespace {
420struct MipMapLevelData {
421 void* fPixelData;
422 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800423};
424
bsalomon4d516a62016-07-28 13:37:31 -0700425struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500426 uint32_t fContextUniqueID;
427 // Right now, the destination color mode is only considered when generating mipmaps
428 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700429 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500430 int fWidth;
431 int fHeight;
432 SkColorType fColorType;
433 SkAlphaType fAlphaType;
434 void* fColorSpace;
435 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500436 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700437 // The fMipMapLevelData array may contain more than 1 element.
438 // It contains fMipMapLevelCount elements.
439 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500440 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700441};
442} // anonymous namespace
443
cblume33e0cb52016-08-30 12:09:23 -0700444static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800445 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
446 // This can cause runaway memory leaks, so we are disabling this path until we can
447 // investigate further. crbug.com/669775
448 return false;
cblume33e0cb52016-08-30 12:09:23 -0700449}
450
451namespace {
452
453class DTIBufferFiller
454{
455public:
cblume921bc672016-09-22 05:25:26 -0700456 explicit DTIBufferFiller(char* bufferAsCharPtr)
457 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700458
459 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700460 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700461 }
462
463private:
464
cblume921bc672016-09-22 05:25:26 -0700465 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700466};
467}
468
469#define FILL_MEMBER(bufferFiller, member, source) \
470 bufferFiller.fillMember(source, \
471 offsetof(DeferredTextureImage, member), \
472 sizeof(DeferredTextureImage::member));
473
bsalomon41b952c2016-03-11 06:46:33 -0800474size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700475 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700476 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400477 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700478 // Extract relevant min/max values from the params array.
479 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
480 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700481 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700482 for (int i = 1; i < paramCnt; ++i) {
483 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
484 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
485 if (highestFilterQuality < params[i].fQuality)
486 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700487 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700488 }
489
bsalomon41b952c2016-03-11 06:46:33 -0800490 const bool fillMode = SkToBool(buffer);
491 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
492 return 0;
493 }
494
ericrkb4da01d2016-06-13 11:18:14 -0700495 // Calculate scaling parameters.
496 bool isScaled = lowestPreScaleMipLevel != 0;
497
498 SkISize scaledSize;
499 if (isScaled) {
500 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
501 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
502 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
503 lowestPreScaleMipLevel - 1);
504 } else {
505 scaledSize = SkISize::Make(this->width(), this->height());
506 }
507
508 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
509 SkFilterQuality scaleFilterQuality = highestFilterQuality;
510 if (scaleFilterQuality > kMedium_SkFilterQuality) {
511 scaleFilterQuality = kMedium_SkFilterQuality;
512 }
513
ericrkc429baf2016-03-24 15:35:45 -0700514 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700515 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700516 return 0;
517 }
518
bsalomon41b952c2016-03-11 06:46:33 -0800519 SkAutoPixmapStorage pixmap;
520 SkImageInfo info;
521 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500522 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800523 info = pixmap.info();
524 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800525 } else {
526 // Here we're just using presence of data to know whether there is a codec behind the image.
527 // In the future we will access the cacherator and get the exact data that we want to (e.g.
528 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700529 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700530 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800531 return 0;
532 }
Brian Osman7992da32016-11-18 11:28:24 -0500533 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
534 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500535 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500536 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500537 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
538 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500539 } else {
540 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
541 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500542 if (kIndex_8_SkColorType == info.colorType()) {
543 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
544 info = info.makeColorType(kN32_SkColorType);
545 }
bsalomon41b952c2016-03-11 06:46:33 -0800546 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
547 if (fillMode) {
548 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700549 if (isScaled) {
550 if (!this->scalePixels(pixmap, scaleFilterQuality,
551 SkImage::kDisallow_CachingHint)) {
552 return 0;
553 }
554 } else {
555 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
556 return 0;
557 }
bsalomon41b952c2016-03-11 06:46:33 -0800558 }
559 SkASSERT(!pixmap.ctable());
560 }
561 }
cblume2c052802016-05-31 09:55:08 -0700562 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700563 if (useMipMaps) {
564 // SkMipMap only deals with the mipmap levels it generates, which does
565 // not include the base level.
566 // That means it generates and holds levels 1-x instead of 0-x.
567 // So the total mipmap level count is 1 more than what
568 // SkMipMap::ComputeLevelCount returns.
569 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
570
571 // We already initialized pixelSize to the size of the base level.
572 // SkMipMap will generate the extra mipmap levels. Their sizes need to
573 // be added to the total.
574 // Index 0 here does not refer to the base mipmap level -- it is
575 // SkMipMap's first generated mipmap level (level 1).
576 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
577 currentMipMapLevelIndex--) {
578 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
579 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700580 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700581 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
582 }
583 }
bsalomon41b952c2016-03-11 06:46:33 -0800584 size_t size = 0;
585 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
586 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700587 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
588 // We subtract 1 because DeferredTextureImage already includes the base
589 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800590 size_t pixelOffset = size;
591 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700592 size_t colorSpaceOffset = 0;
593 size_t colorSpaceSize = 0;
594 if (info.colorSpace()) {
595 colorSpaceOffset = size;
596 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
597 size += colorSpaceSize;
598 }
bsalomon41b952c2016-03-11 06:46:33 -0800599 if (!fillMode) {
600 return size;
601 }
cblume921bc672016-09-22 05:25:26 -0700602 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
603 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
604 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800605
cblume921bc672016-09-22 05:25:26 -0700606 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
607 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800608
Brian Osman6c15cc72016-10-17 09:51:37 -0400609 // If the context has sRGB support, and we're intending to render to a surface with an attached
610 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
611 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500612 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400613 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
614 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500615 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400616 }
617
bsalomon41b952c2016-03-11 06:46:33 -0800618 SkASSERT(info == pixmap.info());
619 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700620 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
621 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700622 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500623 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700624 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
625 int width = info.width();
626 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
627 int height = info.height();
628 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
629 SkColorType colorType = info.colorType();
630 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
631 SkAlphaType alphaType = info.alphaType();
632 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700633 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700634 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700635 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700636 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700637 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700638 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700639 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700640 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
641 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700642 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700643 } else {
cblume921bc672016-09-22 05:25:26 -0700644 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700645 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700646 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700647 0, sizeof(DeferredTextureImage::fColorSpaceSize));
648 }
649
650 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700651 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700652
653 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700654 static_assert(std::is_standard_layout<MipMapLevelData>::value,
655 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700656
Brian Osman7b8400d2016-11-08 17:08:54 -0500657 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700658 // SkMipMap holds only the mipmap levels it generates.
659 // A programmer can use the data they provided to SkMipMap::Build as level 0.
660 // So the SkMipMap provides levels 1-x but it stores them in its own
661 // range 0-(x-1).
662 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
663 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700664 SkMipMap::Level mipLevel;
665 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
666
667 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700668 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700669 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700670 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700671 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700672 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700673 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700674
675 // getSafeSize includes rowbyte padding except for the last row,
676 // right?
677
cblume921bc672016-09-22 05:25:26 -0700678 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700679
cblume921bc672016-09-22 05:25:26 -0700680 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
681 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
682 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700683 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700684 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
685 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
686 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700687
688 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
689 }
bsalomon4d516a62016-07-28 13:37:31 -0700690 }
bsalomon41b952c2016-03-11 06:46:33 -0800691 return size;
692}
693
694sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
695 SkBudgeted budgeted) {
696 if (!data) {
697 return nullptr;
698 }
699 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
700
701 if (!context || context->uniqueID() != dti->fContextUniqueID) {
702 return nullptr;
703 }
cblume33e0cb52016-08-30 12:09:23 -0700704 int mipLevelCount = dti->fMipMapLevelCount;
705 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700706 sk_sp<SkColorSpace> colorSpace;
707 if (dti->fColorSpaceSize) {
708 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
709 }
710 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
711 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000712 if (mipLevelCount == 1) {
713 SkPixmap pixmap;
714 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
715 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
716 } else {
717 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
718 for (int i = 0; i < mipLevelCount; i++) {
719 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
720 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
721 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500722
Brian Osmanb92234a2017-01-25 14:13:00 +0000723 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
724 mipLevelCount, SkBudgeted::kYes,
725 dti->fColorMode);
726 }
bsalomon41b952c2016-03-11 06:46:33 -0800727}
728
729///////////////////////////////////////////////////////////////////////////////////////////////////
730
cblume186d2d42016-06-03 11:17:42 -0700731sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
732 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700733 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500734 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700735 if (!ctx) {
736 return nullptr;
737 }
bungeman6bd52842016-10-27 09:30:08 -0700738 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700739 if (!texture) {
740 return nullptr;
741 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500742 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700743 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700744 info.alphaType(), std::move(texture),
745 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700746}