blob: de67d6ca9e65ddeff91221ddd087469c65950499 [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
robertphillipsc5035e72016-03-17 06:58:39 -07008#include "SkAutoPixmapStorage.h"
reed856e9d92015-09-30 12:21:45 -07009#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000010#include "GrContext.h"
bsalomon993a4212015-05-29 11:37:25 -070011#include "GrDrawContext.h"
bsalomonf1ecd212015-12-09 17:06:02 -080012#include "GrImageIDTextureAdjuster.h"
bsalomonf267c1e2016-02-01 13:16:14 -080013#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070014#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080015#include "SkBitmapCache.h"
reed262a71b2015-12-05 13:07:27 -080016#include "SkGrPixelRef.h"
bsalomon89fe56b2015-10-29 10:49:28 -070017#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080018#include "SkImage_Gpu.h"
ericrkb4da01d2016-06-13 11:18:14 -070019#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070020#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070021
reed80c772b2015-07-30 18:58:23 -070022SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex,
bsalomon5ec26ae2016-02-25 08:33:02 -080023 SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070024 : INHERITED(w, h, uniqueID)
reed8b26b992015-05-07 15:36:17 -070025 , fTexture(SkRef(tex))
reed8b26b992015-05-07 15:36:17 -070026 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080027 , fBudgeted(budgeted)
bsalomon1cd63112015-08-05 06:58:39 -070028 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070029{
30 SkASSERT(tex->width() == w);
31 SkASSERT(tex->height() == h);
32}
piotaixrcef04f82014-07-14 07:48:04 -070033
reed6f1216a2015-08-04 08:10:13 -070034SkImage_Gpu::~SkImage_Gpu() {
35 if (fAddedRasterVersionToCache.load()) {
36 SkNotifyBitmapGenIDIsStale(this->uniqueID());
37 }
38}
39
bsalomoneaaaf0b2015-01-23 08:08:04 -080040extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070041 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070042 ((SkImage_Gpu*)image)->applyBudgetDecision();
43 }
44}
45
reed88d064d2015-10-12 11:30:02 -070046static SkImageInfo make_info(int w, int h, bool isOpaque) {
47 return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
48}
49
reed09553032015-11-23 12:32:16 -080050bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070051 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
52 SkASSERT(dst->getGenerationID() == this->uniqueID());
53 SkASSERT(dst->isImmutable());
54 SkASSERT(dst->getPixels());
55 return true;
56 }
57
reed88d064d2015-10-12 11:30:02 -070058 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) {
reed8b26b992015-05-07 15:36:17 -070059 return false;
60 }
61 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
62 dst->getPixels(), dst->rowBytes())) {
63 return false;
64 }
reed6f1216a2015-08-04 08:10:13 -070065
66 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080067 if (kAllow_CachingHint == chint) {
68 SkBitmapCache::Add(this->uniqueID(), *dst);
69 fAddedRasterVersionToCache.store(true);
70 }
reed8b26b992015-05-07 15:36:17 -070071 return true;
72}
73
reed262a71b2015-12-05 13:07:27 -080074bool SkImage_Gpu::asBitmapForImageFilters(SkBitmap* bitmap) const {
75 bitmap->setInfo(make_info(this->width(), this->height(), this->isOpaque()));
76 bitmap->setPixelRef(new SkGrPixelRef(bitmap->info(), fTexture))->unref();
77 bitmap->pixelRef()->setImmutableWithID(this->uniqueID());
78 return true;
79}
80
brianosman982eb7f2016-06-06 13:10:58 -070081GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params,
82 SkSourceGammaTreatment gammaTreatment) const {
83 return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, gammaTreatment,
84 nullptr);
reed85d91782015-09-10 14:33:38 -070085}
86
reed8b26b992015-05-07 15:36:17 -070087bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -070088 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -070089}
90
91static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
92 switch (info.colorType()) {
93 case kRGBA_8888_SkColorType:
94 case kBGRA_8888_SkColorType:
95 break;
96 default:
97 return; // nothing to do
98 }
99
100 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
101 // and in either case, the alpha-byte is always in the same place, so we can safely call
102 // SkPreMultiplyColor()
103 //
104 SkColor* row = (SkColor*)pixels;
105 for (int y = 0; y < info.height(); ++y) {
106 for (int x = 0; x < info.width(); ++x) {
107 row[x] = SkPreMultiplyColor(row[x]);
108 }
109 }
110}
111
112bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800113 int srcX, int srcY, CachingHint) const {
brianosmanb109b8c2016-06-16 13:03:24 -0700114 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700115 uint32_t flags = 0;
116 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
117 // let the GPU perform this transformation for us
118 flags = GrContext::kUnpremul_PixelOpsFlag;
119 }
120 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
121 pixels, rowBytes, flags)) {
122 return false;
123 }
124 // do we have to manually fix-up the alpha channel?
125 // src dst
126 // unpremul premul fix manually
127 // premul unpremul done by kUnpremul_PixelOpsFlag
128 // all other combos need to change.
129 //
130 // Should this be handled by Ganesh? todo:?
131 //
132 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
133 apply_premul(info, pixels, rowBytes);
134 }
135 return true;
136}
137
reed7fb4f8b2016-03-11 04:33:52 -0800138sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700139 GrContext* ctx = fTexture->getContext();
140 GrSurfaceDesc desc = fTexture->desc();
141 desc.fWidth = subset.width();
142 desc.fHeight = subset.height();
143
senorblanco87751122016-05-20 07:27:38 -0700144 sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted));
reed7b6945b2015-09-24 00:50:58 -0700145 if (!subTx) {
146 return nullptr;
147 }
senorblanco87751122016-05-20 07:27:38 -0700148 ctx->copySurface(subTx.get(), fTexture, subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800149 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
senorblanco87751122016-05-20 07:27:38 -0700150 fAlphaType, subTx.get(), fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700151}
152
reed8b26b992015-05-07 15:36:17 -0700153///////////////////////////////////////////////////////////////////////////////////////////////////
154
reed7fb4f8b2016-03-11 04:33:52 -0800155static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
156 SkAlphaType at, GrWrapOwnership ownership,
157 SkImage::TextureReleaseProc releaseProc,
158 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700159 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700161 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700162 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700163 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700164 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700165 }
reedde499882015-06-18 13:41:40 -0700166 if (releaseProc) {
167 tex->setRelease(releaseProc, releaseCtx);
168 }
169
bsalomon5ec26ae2016-02-25 08:33:02 -0800170 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800171 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
172 at, tex, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700173}
174
reed7fb4f8b2016-03-11 04:33:52 -0800175sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
176 SkAlphaType at, TextureReleaseProc releaseP,
177 ReleaseContext releaseC) {
reedde499882015-06-18 13:41:40 -0700178 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700179}
180
reed7fb4f8b2016-03-11 04:33:52 -0800181sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
182 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700183 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700184}
185
jbaumanb445a572016-06-09 13:24:48 -0700186static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
187 bool nv12,
188 const GrBackendObject yuvTextureHandles[],
189 const SkISize yuvSizes[],
190 GrSurfaceOrigin origin) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800191 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700192
jbaumanb445a572016-06-09 13:24:48 -0700193 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
194 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700196 }
jbaumanb445a572016-06-09 13:24:48 -0700197 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
198 return nullptr;
199 }
200
201 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
202
bsalomon993a4212015-05-29 11:37:25 -0700203 GrBackendTextureDesc yDesc;
204 yDesc.fConfig = kConfig;
205 yDesc.fOrigin = origin;
206 yDesc.fSampleCnt = 0;
207 yDesc.fTextureHandle = yuvTextureHandles[0];
208 yDesc.fWidth = yuvSizes[0].fWidth;
209 yDesc.fHeight = yuvSizes[0].fHeight;
210
211 GrBackendTextureDesc uDesc;
212 uDesc.fConfig = kConfig;
213 uDesc.fOrigin = origin;
214 uDesc.fSampleCnt = 0;
215 uDesc.fTextureHandle = yuvTextureHandles[1];
216 uDesc.fWidth = yuvSizes[1].fWidth;
217 uDesc.fHeight = yuvSizes[1].fHeight;
218
jbaumanb445a572016-06-09 13:24:48 -0700219 sk_sp<GrTexture> yTex(
220 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
221 sk_sp<GrTexture> uTex(
222 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
223 sk_sp<GrTexture> vTex;
224 if (nv12) {
225 vTex = uTex;
226 } else {
227 GrBackendTextureDesc vDesc;
228 vDesc.fConfig = kConfig;
229 vDesc.fOrigin = origin;
230 vDesc.fSampleCnt = 0;
231 vDesc.fTextureHandle = yuvTextureHandles[2];
232 vDesc.fWidth = yuvSizes[2].fWidth;
233 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700234
jbaumanb445a572016-06-09 13:24:48 -0700235 vTex = sk_sp<GrTexture>(
236 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
237 }
bsalomon993a4212015-05-29 11:37:25 -0700238 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700239 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700240 }
241
robertphillipsd4c741e2016-04-28 09:55:15 -0700242 const int width = yuvSizes[0].fWidth;
243 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700244
robertphillipsd4c741e2016-04-28 09:55:15 -0700245 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
robertphillips76948d42016-05-04 12:47:41 -0700246 sk_sp<GrDrawContext> drawContext(ctx->newDrawContext(SkBackingFit::kExact,
robertphillipsd4c741e2016-04-28 09:55:15 -0700247 width, height,
248 kRGBA_8888_GrPixelConfig,
249 0,
250 origin));
251 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700252 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700253 }
254
255 GrPaint paint;
256 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
jbaumanb445a572016-06-09 13:24:48 -0700257 paint.addColorFragmentProcessor(
258 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700259
robertphillipsd4c741e2016-04-28 09:55:15 -0700260 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700261
cdalton846c0512016-05-13 10:25:00 -0700262 drawContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
robertphillipsd4c741e2016-04-28 09:55:15 -0700263 ctx->flushSurfaceWrites(drawContext->accessRenderTarget());
264 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
265 kOpaque_SkAlphaType,
266 drawContext->asTexture().get(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700267}
reed56179002015-07-07 06:11:19 -0700268
jbaumanb445a572016-06-09 13:24:48 -0700269sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
270 const GrBackendObject yuvTextureHandles[3],
271 const SkISize yuvSizes[3], GrSurfaceOrigin origin) {
272 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin);
273}
274
275sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
276 const GrBackendObject yuvTextureHandles[2],
277 const SkISize yuvSizes[2],
278 GrSurfaceOrigin origin) {
279 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin);
280}
281
reed7fb4f8b2016-03-11 04:33:52 -0800282static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
brianosman982eb7f2016-06-06 13:10:58 -0700283 SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter(),
284 SkSourceGammaTreatment::kRespect));
bsalomon8e74f802016-01-30 10:01:40 -0800285 if (!texture) {
286 return nullptr;
287 }
reed7fb4f8b2016-03-11 04:33:52 -0800288 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture,
289 SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800290}
291
reed7fb4f8b2016-03-11 04:33:52 -0800292sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800293 if (!context) {
294 return nullptr;
295 }
296 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800297 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800298 }
299 // No way to check whether a image is premul or not?
300 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
301
302 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
303 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
304 return create_image_from_maker(&maker, at, this->uniqueID());
305 }
306 SkBitmap bmp;
307 if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) {
308 return nullptr;
309 }
310 GrBitmapTextureMaker maker(context, bmp);
311 return create_image_from_maker(&maker, at, this->uniqueID());
312}
313
bsalomon634b4302016-07-12 18:11:17 -0700314sk_sp<SkImage> SkImage::makeNonTextureImage() const {
315 GrTexture* texture = as_IB(this)->peekTexture();
316 if (!texture) {
317 return sk_ref_sp(const_cast<SkImage*>(this));
318 }
319 SkColorType ct;
320 sk_sp<SkColorSpace> cs;
321 if (!GrPixelConfigToColorAndColorSpace(texture->config(), &ct, &cs)) {
322 return nullptr;
323 }
324 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
325 auto info = SkImageInfo::Make(this->width(), this->height(), ct, at, cs);
326 size_t rowBytes = info.minRowBytes();
327 size_t size = info.getSafeSize(rowBytes);
328 auto data = SkData::MakeUninitialized(size);
329 if (!data) {
330 return nullptr;
331 }
332 SkPixmap pm(info, data->writable_data(), rowBytes);
333 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
334 return nullptr;
335 }
336 return MakeRasterData(info, data, rowBytes);
337}
338
reed7fb4f8b2016-03-11 04:33:52 -0800339sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
340 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800341 if (!ctx) {
342 return nullptr;
343 }
ericrk8bea8902016-03-18 11:52:20 -0700344 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800345 if (!texture) {
346 return nullptr;
347 }
reed7fb4f8b2016-03-11 04:33:52 -0800348 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
349 pixmap.alphaType(), texture, budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800350}
351
reed56179002015-07-07 06:11:19 -0700352///////////////////////////////////////////////////////////////////////////////////////////////////
353
bsalomon41b952c2016-03-11 06:46:33 -0800354class DeferredTextureImage {
355public:
356 SkImage* newImage(GrContext* context, SkBudgeted) const;
357
358private:
359 uint32_t fContextUniqueID;
cblume2c052802016-05-31 09:55:08 -0700360 struct MipMapLevelData {
361 void* fPixelData;
362 size_t fRowBytes;
363 };
bsalomon41b952c2016-03-11 06:46:33 -0800364 struct Data {
365 SkImageInfo fInfo;
bsalomon41b952c2016-03-11 06:46:33 -0800366 int fColorTableCnt;
367 uint32_t* fColorTableData;
cblume2c052802016-05-31 09:55:08 -0700368 int fMipMapLevelCount;
369 // The fMipMapLevelData array may contain more than 1 element.
370 // It contains fMipMapLevelCount elements.
371 // That means this struct's size is not known at compile-time.
372 MipMapLevelData fMipMapLevelData[1];
bsalomon41b952c2016-03-11 06:46:33 -0800373 };
374 Data fData;
375
376 friend class SkImage;
377};
378
379size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700380 const DeferredTextureImageUsageParams params[],
brianosman6efeda42016-06-22 07:10:06 -0700381 int paramCnt, void* buffer) const {
ericrkb4da01d2016-06-13 11:18:14 -0700382 // Extract relevant min/max values from the params array.
383 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
384 SkFilterQuality highestFilterQuality = params[0].fQuality;
385 for (int i = 1; i < paramCnt; ++i) {
386 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
387 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
388 if (highestFilterQuality < params[i].fQuality)
389 highestFilterQuality = params[i].fQuality;
390 }
391
bsalomon41b952c2016-03-11 06:46:33 -0800392 const bool fillMode = SkToBool(buffer);
393 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
394 return 0;
395 }
396
ericrkb4da01d2016-06-13 11:18:14 -0700397 // Calculate scaling parameters.
398 bool isScaled = lowestPreScaleMipLevel != 0;
399
400 SkISize scaledSize;
401 if (isScaled) {
402 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
403 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
404 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
405 lowestPreScaleMipLevel - 1);
406 } else {
407 scaledSize = SkISize::Make(this->width(), this->height());
408 }
409
410 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
411 SkFilterQuality scaleFilterQuality = highestFilterQuality;
412 if (scaleFilterQuality > kMedium_SkFilterQuality) {
413 scaleFilterQuality = kMedium_SkFilterQuality;
414 }
415
ericrkc429baf2016-03-24 15:35:45 -0700416 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700417 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700418 return 0;
419 }
420
bsalomon41b952c2016-03-11 06:46:33 -0800421 SkAutoPixmapStorage pixmap;
422 SkImageInfo info;
423 size_t pixelSize = 0;
424 size_t ctSize = 0;
425 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700426 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800427 info = pixmap.info();
428 pixelSize = SkAlign8(pixmap.getSafeSize());
429 if (pixmap.ctable()) {
430 ctCount = pixmap.ctable()->count();
431 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
432 }
433 } else {
434 // Here we're just using presence of data to know whether there is a codec behind the image.
435 // In the future we will access the cacherator and get the exact data that we want to (e.g.
436 // yuv planes) upload.
437 SkAutoTUnref<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700438 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800439 return 0;
440 }
441 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
ericrkb4da01d2016-06-13 11:18:14 -0700442 info = SkImageInfo::MakeN32(scaledSize.width(), scaledSize.height(), at);
bsalomon41b952c2016-03-11 06:46:33 -0800443 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
444 if (fillMode) {
445 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700446 if (isScaled) {
447 if (!this->scalePixels(pixmap, scaleFilterQuality,
448 SkImage::kDisallow_CachingHint)) {
449 return 0;
450 }
451 } else {
452 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
453 return 0;
454 }
bsalomon41b952c2016-03-11 06:46:33 -0800455 }
456 SkASSERT(!pixmap.ctable());
457 }
458 }
cblume2c052802016-05-31 09:55:08 -0700459 int mipMapLevelCount = 1;
bsalomon41b952c2016-03-11 06:46:33 -0800460 size_t size = 0;
461 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
462 size += dtiSize;
cblume2c052802016-05-31 09:55:08 -0700463 size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData);
bsalomon41b952c2016-03-11 06:46:33 -0800464 size_t pixelOffset = size;
465 size += pixelSize;
466 size_t ctOffset = size;
467 size += ctSize;
468 if (!fillMode) {
469 return size;
470 }
471 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer);
472 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset);
473 SkPMColor* ct = nullptr;
474 if (ctSize) {
475 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset);
476 }
477
478 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize());
479 if (ctSize) {
480 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
481 }
482
483 SkASSERT(info == pixmap.info());
484 size_t rowBytes = pixmap.rowBytes();
485 DeferredTextureImage* dti = new (buffer) DeferredTextureImage();
486 dti->fContextUniqueID = proxy.fContextUniqueID;
487 dti->fData.fInfo = info;
bsalomon41b952c2016-03-11 06:46:33 -0800488 dti->fData.fColorTableCnt = ctCount;
489 dti->fData.fColorTableData = ct;
cblume2c052802016-05-31 09:55:08 -0700490 dti->fData.fMipMapLevelCount = mipMapLevelCount;
491 dti->fData.fMipMapLevelData[0].fPixelData = pixels;
492 dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800493 return size;
494}
495
496sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
497 SkBudgeted budgeted) {
498 if (!data) {
499 return nullptr;
500 }
501 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
502
503 if (!context || context->uniqueID() != dti->fContextUniqueID) {
504 return nullptr;
505 }
506 SkAutoTUnref<SkColorTable> colorTable;
507 if (dti->fData.fColorTableCnt) {
508 SkASSERT(dti->fData.fColorTableData);
509 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt));
510 }
brianosman6efeda42016-06-22 07:10:06 -0700511 SkASSERT(dti->fData.fMipMapLevelCount == 1);
512 SkPixmap pixmap;
513 pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData,
514 dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get());
515 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
bsalomon41b952c2016-03-11 06:46:33 -0800516}
517
518///////////////////////////////////////////////////////////////////////////////////////////////////
519
bsalomon5ec26ae2016-02-25 08:33:02 -0800520GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) {
reed56179002015-07-07 06:11:19 -0700521 GrContext* ctx = src->getContext();
522
523 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700524 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700525 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700526 return nullptr;
reed56179002015-07-07 06:11:19 -0700527 }
cblume61214052016-01-26 09:10:48 -0800528
reed56179002015-07-07 06:11:19 -0700529 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
530 const SkIPoint dstP = SkIPoint::Make(0, 0);
bsalomonb8fea972016-02-16 07:34:17 -0800531 ctx->copySurface(dst, src, srcR, dstP);
532 ctx->flushSurfaceWrites(dst);
reed56179002015-07-07 06:11:19 -0700533 return dst;
534}
535
cblume186d2d42016-06-03 11:17:42 -0700536sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
537 const GrMipLevel* texels, int mipLevelCount,
brianosman6efeda42016-06-22 07:10:06 -0700538 SkBudgeted budgeted) {
cblume186d2d42016-06-03 11:17:42 -0700539 if (!ctx) {
540 return nullptr;
541 }
542 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
543 if (!texture) {
544 return nullptr;
545 }
546 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
547 info.alphaType(), texture, budgeted);
548}