blob: f67d7aa34b4dc0f9618be3db8157769b0342b5c1 [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"
reed6f1216a2015-08-04 08:10:13 -070019#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070020
reed80c772b2015-07-30 18:58:23 -070021SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex,
bsalomon5ec26ae2016-02-25 08:33:02 -080022 SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070023 : INHERITED(w, h, uniqueID)
reed8b26b992015-05-07 15:36:17 -070024 , fTexture(SkRef(tex))
reed8b26b992015-05-07 15:36:17 -070025 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080026 , fBudgeted(budgeted)
bsalomon1cd63112015-08-05 06:58:39 -070027 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070028{
29 SkASSERT(tex->width() == w);
30 SkASSERT(tex->height() == h);
31}
piotaixrcef04f82014-07-14 07:48:04 -070032
reed6f1216a2015-08-04 08:10:13 -070033SkImage_Gpu::~SkImage_Gpu() {
34 if (fAddedRasterVersionToCache.load()) {
35 SkNotifyBitmapGenIDIsStale(this->uniqueID());
36 }
37}
38
bsalomoneaaaf0b2015-01-23 08:08:04 -080039extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -080040 if (as_IB(image)->peekTexture()) {
reed8b26b992015-05-07 15:36:17 -070041 ((SkImage_Gpu*)image)->applyBudgetDecision();
42 }
43}
44
reed88d064d2015-10-12 11:30:02 -070045static SkImageInfo make_info(int w, int h, bool isOpaque) {
46 return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
47}
48
reed09553032015-11-23 12:32:16 -080049bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070050 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
51 SkASSERT(dst->getGenerationID() == this->uniqueID());
52 SkASSERT(dst->isImmutable());
53 SkASSERT(dst->getPixels());
54 return true;
55 }
56
reed88d064d2015-10-12 11:30:02 -070057 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) {
reed8b26b992015-05-07 15:36:17 -070058 return false;
59 }
60 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
61 dst->getPixels(), dst->rowBytes())) {
62 return false;
63 }
reed6f1216a2015-08-04 08:10:13 -070064
65 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080066 if (kAllow_CachingHint == chint) {
67 SkBitmapCache::Add(this->uniqueID(), *dst);
68 fAddedRasterVersionToCache.store(true);
69 }
reed8b26b992015-05-07 15:36:17 -070070 return true;
71}
72
reed262a71b2015-12-05 13:07:27 -080073bool SkImage_Gpu::asBitmapForImageFilters(SkBitmap* bitmap) const {
74 bitmap->setInfo(make_info(this->width(), this->height(), this->isOpaque()));
75 bitmap->setPixelRef(new SkGrPixelRef(bitmap->info(), fTexture))->unref();
76 bitmap->pixelRef()->setImmutableWithID(this->uniqueID());
77 return true;
78}
79
bsalomonafa95e22015-10-12 10:39:46 -070080GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const {
bsalomonf1ecd212015-12-09 17:06:02 -080081 return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, nullptr);
reed85d91782015-09-10 14:33:38 -070082}
83
reed8b26b992015-05-07 15:36:17 -070084bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -070085 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -070086}
87
88static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
89 switch (info.colorType()) {
90 case kRGBA_8888_SkColorType:
91 case kBGRA_8888_SkColorType:
92 break;
93 default:
94 return; // nothing to do
95 }
96
97 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
98 // and in either case, the alpha-byte is always in the same place, so we can safely call
99 // SkPreMultiplyColor()
100 //
101 SkColor* row = (SkColor*)pixels;
102 for (int y = 0; y < info.height(); ++y) {
103 for (int x = 0; x < info.width(); ++x) {
104 row[x] = SkPreMultiplyColor(row[x]);
105 }
106 }
107}
108
109bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800110 int srcX, int srcY, CachingHint) const {
reed8b26b992015-05-07 15:36:17 -0700111 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
brianosmana6359362016-03-21 06:55:37 -0700112 info.profileType(),
113 *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700114 uint32_t flags = 0;
115 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
116 // let the GPU perform this transformation for us
117 flags = GrContext::kUnpremul_PixelOpsFlag;
118 }
119 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
120 pixels, rowBytes, flags)) {
121 return false;
122 }
123 // do we have to manually fix-up the alpha channel?
124 // src dst
125 // unpremul premul fix manually
126 // premul unpremul done by kUnpremul_PixelOpsFlag
127 // all other combos need to change.
128 //
129 // Should this be handled by Ganesh? todo:?
130 //
131 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
132 apply_premul(info, pixels, rowBytes);
133 }
134 return true;
135}
136
reed7fb4f8b2016-03-11 04:33:52 -0800137sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700138 GrContext* ctx = fTexture->getContext();
139 GrSurfaceDesc desc = fTexture->desc();
140 desc.fWidth = subset.width();
141 desc.fHeight = subset.height();
142
senorblanco87751122016-05-20 07:27:38 -0700143 sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted));
reed7b6945b2015-09-24 00:50:58 -0700144 if (!subTx) {
145 return nullptr;
146 }
senorblanco87751122016-05-20 07:27:38 -0700147 ctx->copySurface(subTx.get(), fTexture, subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800148 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
senorblanco87751122016-05-20 07:27:38 -0700149 fAlphaType, subTx.get(), fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700150}
151
reed8b26b992015-05-07 15:36:17 -0700152///////////////////////////////////////////////////////////////////////////////////////////////////
153
reed7fb4f8b2016-03-11 04:33:52 -0800154static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
155 SkAlphaType at, GrWrapOwnership ownership,
156 SkImage::TextureReleaseProc releaseProc,
157 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700158 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700160 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700161 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700162 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700163 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700164 }
reedde499882015-06-18 13:41:40 -0700165 if (releaseProc) {
166 tex->setRelease(releaseProc, releaseCtx);
167 }
168
bsalomon5ec26ae2016-02-25 08:33:02 -0800169 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800170 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
171 at, tex, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700172}
173
reed7fb4f8b2016-03-11 04:33:52 -0800174sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
175 SkAlphaType at, TextureReleaseProc releaseP,
176 ReleaseContext releaseC) {
reedde499882015-06-18 13:41:40 -0700177 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700178}
179
reed7fb4f8b2016-03-11 04:33:52 -0800180sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
181 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700182 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700183}
184
reed7fb4f8b2016-03-11 04:33:52 -0800185sk_sp<SkImage> SkImage::MakeFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
186 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700187 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700188 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700189 }
reed56179002015-07-07 06:11:19 -0700190
bsalomon6dc6f5f2015-06-18 09:12:16 -0700191 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700192 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700193 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700195 }
196
bsalomon5ec26ae2016-02-25 08:33:02 -0800197 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, SkBudgeted::kYes));
reed8b26b992015-05-07 15:36:17 -0700198 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700200 }
201
reed7fb4f8b2016-03-11 04:33:52 -0800202 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst,
203 SkBudgeted::kYes);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800204}
bsalomon993a4212015-05-29 11:37:25 -0700205
reed7fb4f8b2016-03-11 04:33:52 -0800206sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
207 const GrBackendObject yuvTextureHandles[3],
208 const SkISize yuvSizes[3],
209 GrSurfaceOrigin origin) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800210 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700211
212 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
213 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
214 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700215 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700216 }
217 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
218 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
234 GrBackendTextureDesc vDesc;
235 vDesc.fConfig = kConfig;
236 vDesc.fOrigin = origin;
237 vDesc.fSampleCnt = 0;
238 vDesc.fTextureHandle = yuvTextureHandles[2];
239 vDesc.fWidth = yuvSizes[2].fWidth;
240 vDesc.fHeight = yuvSizes[2].fHeight;
241
bsalomon6dc6f5f2015-06-18 09:12:16 -0700242 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
243 yDesc, kBorrow_GrWrapOwnership));
244 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
245 uDesc, kBorrow_GrWrapOwnership));
246 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
247 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700248 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700250 }
251
robertphillipsd4c741e2016-04-28 09:55:15 -0700252 const int width = yuvSizes[0].fWidth;
253 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700254
robertphillipsd4c741e2016-04-28 09:55:15 -0700255 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
robertphillips76948d42016-05-04 12:47:41 -0700256 sk_sp<GrDrawContext> drawContext(ctx->newDrawContext(SkBackingFit::kExact,
robertphillipsd4c741e2016-04-28 09:55:15 -0700257 width, height,
258 kRGBA_8888_GrPixelConfig,
259 0,
260 origin));
261 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700262 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700263 }
264
265 GrPaint paint;
266 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf267c1e2016-02-01 13:16:14 -0800267 paint.addColorFragmentProcessor(GrYUVEffect::CreateYUVToRGB(yTex, uTex, vTex, yuvSizes,
268 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700269
robertphillipsd4c741e2016-04-28 09:55:15 -0700270 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700271
cdalton846c0512016-05-13 10:25:00 -0700272 drawContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
robertphillipsd4c741e2016-04-28 09:55:15 -0700273 ctx->flushSurfaceWrites(drawContext->accessRenderTarget());
274 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
275 kOpaque_SkAlphaType,
276 drawContext->asTexture().get(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700277}
reed56179002015-07-07 06:11:19 -0700278
reed7fb4f8b2016-03-11 04:33:52 -0800279static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
bsalomon8e74f802016-01-30 10:01:40 -0800280 SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter()));
281 if (!texture) {
282 return nullptr;
283 }
reed7fb4f8b2016-03-11 04:33:52 -0800284 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture,
285 SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800286}
287
reed7fb4f8b2016-03-11 04:33:52 -0800288sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800289 if (!context) {
290 return nullptr;
291 }
292 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800293 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800294 }
295 // No way to check whether a image is premul or not?
296 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
297
298 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
299 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
300 return create_image_from_maker(&maker, at, this->uniqueID());
301 }
302 SkBitmap bmp;
303 if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) {
304 return nullptr;
305 }
306 GrBitmapTextureMaker maker(context, bmp);
307 return create_image_from_maker(&maker, at, this->uniqueID());
308}
309
reed7fb4f8b2016-03-11 04:33:52 -0800310sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
311 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800312 if (!ctx) {
313 return nullptr;
314 }
ericrk8bea8902016-03-18 11:52:20 -0700315 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800316 if (!texture) {
317 return nullptr;
318 }
reed7fb4f8b2016-03-11 04:33:52 -0800319 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
320 pixmap.alphaType(), texture, budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800321}
322
reed56179002015-07-07 06:11:19 -0700323///////////////////////////////////////////////////////////////////////////////////////////////////
324
bsalomon41b952c2016-03-11 06:46:33 -0800325class DeferredTextureImage {
326public:
327 SkImage* newImage(GrContext* context, SkBudgeted) const;
328
329private:
330 uint32_t fContextUniqueID;
cblume2c052802016-05-31 09:55:08 -0700331 struct MipMapLevelData {
332 void* fPixelData;
333 size_t fRowBytes;
334 };
bsalomon41b952c2016-03-11 06:46:33 -0800335 struct Data {
336 SkImageInfo fInfo;
bsalomon41b952c2016-03-11 06:46:33 -0800337 int fColorTableCnt;
338 uint32_t* fColorTableData;
cblume2c052802016-05-31 09:55:08 -0700339 int fMipMapLevelCount;
340 // The fMipMapLevelData array may contain more than 1 element.
341 // It contains fMipMapLevelCount elements.
342 // That means this struct's size is not known at compile-time.
343 MipMapLevelData fMipMapLevelData[1];
bsalomon41b952c2016-03-11 06:46:33 -0800344 };
345 Data fData;
346
347 friend class SkImage;
348};
349
350size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
351 const DeferredTextureImageUsageParams[],
352 int paramCnt, void* buffer) const {
353 const bool fillMode = SkToBool(buffer);
354 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
355 return 0;
356 }
357
ericrkc429baf2016-03-24 15:35:45 -0700358 const int maxTextureSize = proxy.fCaps->maxTextureSize();
359 if (width() > maxTextureSize || height() > maxTextureSize) {
360 return 0;
361 }
362
bsalomon41b952c2016-03-11 06:46:33 -0800363 SkAutoPixmapStorage pixmap;
364 SkImageInfo info;
365 size_t pixelSize = 0;
366 size_t ctSize = 0;
367 int ctCount = 0;
368 if (this->peekPixels(&pixmap)) {
369 info = pixmap.info();
370 pixelSize = SkAlign8(pixmap.getSafeSize());
371 if (pixmap.ctable()) {
372 ctCount = pixmap.ctable()->count();
373 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
374 }
375 } else {
376 // Here we're just using presence of data to know whether there is a codec behind the image.
377 // In the future we will access the cacherator and get the exact data that we want to (e.g.
378 // yuv planes) upload.
379 SkAutoTUnref<SkData> data(this->refEncoded());
380 if (!data) {
381 return 0;
382 }
383 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
384 info = SkImageInfo::MakeN32(this->width(), this->height(), at);
385 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
386 if (fillMode) {
387 pixmap.alloc(info);
388 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
389 return 0;
390 }
391 SkASSERT(!pixmap.ctable());
392 }
393 }
cblume2c052802016-05-31 09:55:08 -0700394 int mipMapLevelCount = 1;
bsalomon41b952c2016-03-11 06:46:33 -0800395 size_t size = 0;
396 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
397 size += dtiSize;
cblume2c052802016-05-31 09:55:08 -0700398 size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData);
bsalomon41b952c2016-03-11 06:46:33 -0800399 size_t pixelOffset = size;
400 size += pixelSize;
401 size_t ctOffset = size;
402 size += ctSize;
403 if (!fillMode) {
404 return size;
405 }
406 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer);
407 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset);
408 SkPMColor* ct = nullptr;
409 if (ctSize) {
410 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset);
411 }
412
413 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize());
414 if (ctSize) {
415 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
416 }
417
418 SkASSERT(info == pixmap.info());
419 size_t rowBytes = pixmap.rowBytes();
420 DeferredTextureImage* dti = new (buffer) DeferredTextureImage();
421 dti->fContextUniqueID = proxy.fContextUniqueID;
422 dti->fData.fInfo = info;
bsalomon41b952c2016-03-11 06:46:33 -0800423 dti->fData.fColorTableCnt = ctCount;
424 dti->fData.fColorTableData = ct;
cblume2c052802016-05-31 09:55:08 -0700425 dti->fData.fMipMapLevelCount = mipMapLevelCount;
426 dti->fData.fMipMapLevelData[0].fPixelData = pixels;
427 dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800428 return size;
429}
430
431sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
432 SkBudgeted budgeted) {
433 if (!data) {
434 return nullptr;
435 }
436 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
437
438 if (!context || context->uniqueID() != dti->fContextUniqueID) {
439 return nullptr;
440 }
441 SkAutoTUnref<SkColorTable> colorTable;
442 if (dti->fData.fColorTableCnt) {
443 SkASSERT(dti->fData.fColorTableData);
444 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt));
445 }
cblume2c052802016-05-31 09:55:08 -0700446 SkASSERT(dti->fData.fMipMapLevelCount == 1);
bsalomon41b952c2016-03-11 06:46:33 -0800447 SkPixmap pixmap;
cblume2c052802016-05-31 09:55:08 -0700448 pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData,
449 dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get());
bsalomon41b952c2016-03-11 06:46:33 -0800450 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
451}
452
453///////////////////////////////////////////////////////////////////////////////////////////////////
454
bsalomon5ec26ae2016-02-25 08:33:02 -0800455GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) {
reed56179002015-07-07 06:11:19 -0700456 GrContext* ctx = src->getContext();
457
458 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700459 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700460 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700461 return nullptr;
reed56179002015-07-07 06:11:19 -0700462 }
cblume61214052016-01-26 09:10:48 -0800463
reed56179002015-07-07 06:11:19 -0700464 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
465 const SkIPoint dstP = SkIPoint::Make(0, 0);
bsalomonb8fea972016-02-16 07:34:17 -0800466 ctx->copySurface(dst, src, srcR, dstP);
467 ctx->flushSurfaceWrites(dst);
reed56179002015-07-07 06:11:19 -0700468 return dst;
469}
470