blob: 60eedbc9a7a71fafb198a5fd4fae4d4f00ca47b7 [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"
reed8b26b992015-05-07 15:36:17 -070016#include "SkGpuDevice.h"
reed262a71b2015-12-05 13:07:27 -080017#include "SkGrPixelRef.h"
bsalomon89fe56b2015-10-29 10:49:28 -070018#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080019#include "SkImageFilter.h"
20#include "SkImage_Gpu.h"
reed6f1216a2015-08-04 08:10:13 -070021#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070022
reed80c772b2015-07-30 18:58:23 -070023SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex,
bsalomon5ec26ae2016-02-25 08:33:02 -080024 SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070025 : INHERITED(w, h, uniqueID)
reed8b26b992015-05-07 15:36:17 -070026 , fTexture(SkRef(tex))
reed8b26b992015-05-07 15:36:17 -070027 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080028 , fBudgeted(budgeted)
bsalomon1cd63112015-08-05 06:58:39 -070029 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070030{
31 SkASSERT(tex->width() == w);
32 SkASSERT(tex->height() == h);
33}
piotaixrcef04f82014-07-14 07:48:04 -070034
reed6f1216a2015-08-04 08:10:13 -070035SkImage_Gpu::~SkImage_Gpu() {
36 if (fAddedRasterVersionToCache.load()) {
37 SkNotifyBitmapGenIDIsStale(this->uniqueID());
38 }
39}
40
bsalomoneaaaf0b2015-01-23 08:08:04 -080041extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -080042 if (as_IB(image)->peekTexture()) {
reed8b26b992015-05-07 15:36:17 -070043 ((SkImage_Gpu*)image)->applyBudgetDecision();
44 }
45}
46
reed88d064d2015-10-12 11:30:02 -070047static SkImageInfo make_info(int w, int h, bool isOpaque) {
48 return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
49}
50
reed09553032015-11-23 12:32:16 -080051bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070052 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
53 SkASSERT(dst->getGenerationID() == this->uniqueID());
54 SkASSERT(dst->isImmutable());
55 SkASSERT(dst->getPixels());
56 return true;
57 }
58
reed88d064d2015-10-12 11:30:02 -070059 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) {
reed8b26b992015-05-07 15:36:17 -070060 return false;
61 }
62 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
63 dst->getPixels(), dst->rowBytes())) {
64 return false;
65 }
reed6f1216a2015-08-04 08:10:13 -070066
67 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080068 if (kAllow_CachingHint == chint) {
69 SkBitmapCache::Add(this->uniqueID(), *dst);
70 fAddedRasterVersionToCache.store(true);
71 }
reed8b26b992015-05-07 15:36:17 -070072 return true;
73}
74
reed262a71b2015-12-05 13:07:27 -080075bool SkImage_Gpu::asBitmapForImageFilters(SkBitmap* bitmap) const {
76 bitmap->setInfo(make_info(this->width(), this->height(), this->isOpaque()));
77 bitmap->setPixelRef(new SkGrPixelRef(bitmap->info(), fTexture))->unref();
78 bitmap->pixelRef()->setImmutableWithID(this->uniqueID());
79 return true;
80}
81
bsalomonafa95e22015-10-12 10:39:46 -070082GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const {
bsalomonf1ecd212015-12-09 17:06:02 -080083 return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, nullptr);
reed85d91782015-09-10 14:33:38 -070084}
85
reed8b26b992015-05-07 15:36:17 -070086bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -070087 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -070088}
89
90static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
91 switch (info.colorType()) {
92 case kRGBA_8888_SkColorType:
93 case kBGRA_8888_SkColorType:
94 break;
95 default:
96 return; // nothing to do
97 }
98
99 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
100 // and in either case, the alpha-byte is always in the same place, so we can safely call
101 // SkPreMultiplyColor()
102 //
103 SkColor* row = (SkColor*)pixels;
104 for (int y = 0; y < info.height(); ++y) {
105 for (int x = 0; x < info.width(); ++x) {
106 row[x] = SkPreMultiplyColor(row[x]);
107 }
108 }
109}
110
111bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800112 int srcX, int srcY, CachingHint) const {
reed8b26b992015-05-07 15:36:17 -0700113 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
brianosmana6359362016-03-21 06:55:37 -0700114 info.profileType(),
115 *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700116 uint32_t flags = 0;
117 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
118 // let the GPU perform this transformation for us
119 flags = GrContext::kUnpremul_PixelOpsFlag;
120 }
121 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
122 pixels, rowBytes, flags)) {
123 return false;
124 }
125 // do we have to manually fix-up the alpha channel?
126 // src dst
127 // unpremul premul fix manually
128 // premul unpremul done by kUnpremul_PixelOpsFlag
129 // all other combos need to change.
130 //
131 // Should this be handled by Ganesh? todo:?
132 //
133 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
134 apply_premul(info, pixels, rowBytes);
135 }
136 return true;
137}
138
reed7fb4f8b2016-03-11 04:33:52 -0800139sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700140 GrContext* ctx = fTexture->getContext();
141 GrSurfaceDesc desc = fTexture->desc();
142 desc.fWidth = subset.width();
143 desc.fHeight = subset.height();
144
bsalomon5ec26ae2016-02-25 08:33:02 -0800145 GrTexture* subTx = ctx->textureProvider()->createTexture(desc, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700146 if (!subTx) {
147 return nullptr;
148 }
149 ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800150 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
151 fAlphaType, subTx, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700152}
153
reed8b26b992015-05-07 15:36:17 -0700154///////////////////////////////////////////////////////////////////////////////////////////////////
155
reed7fb4f8b2016-03-11 04:33:52 -0800156static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
157 SkAlphaType at, GrWrapOwnership ownership,
158 SkImage::TextureReleaseProc releaseProc,
159 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700160 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700161 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700162 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700163 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700164 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700166 }
reedde499882015-06-18 13:41:40 -0700167 if (releaseProc) {
168 tex->setRelease(releaseProc, releaseCtx);
169 }
170
bsalomon5ec26ae2016-02-25 08:33:02 -0800171 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800172 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
173 at, tex, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700174}
175
reed7fb4f8b2016-03-11 04:33:52 -0800176sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
177 SkAlphaType at, TextureReleaseProc releaseP,
178 ReleaseContext releaseC) {
reedde499882015-06-18 13:41:40 -0700179 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700180}
181
reed7fb4f8b2016-03-11 04:33:52 -0800182sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
183 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700184 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700185}
186
reed7fb4f8b2016-03-11 04:33:52 -0800187sk_sp<SkImage> SkImage::MakeFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
188 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700189 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700190 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700191 }
reed56179002015-07-07 06:11:19 -0700192
bsalomon6dc6f5f2015-06-18 09:12:16 -0700193 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700194 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700195 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700196 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700197 }
198
bsalomon5ec26ae2016-02-25 08:33:02 -0800199 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, SkBudgeted::kYes));
reed8b26b992015-05-07 15:36:17 -0700200 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700201 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700202 }
203
reed7fb4f8b2016-03-11 04:33:52 -0800204 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst,
205 SkBudgeted::kYes);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800206}
bsalomon993a4212015-05-29 11:37:25 -0700207
reed7fb4f8b2016-03-11 04:33:52 -0800208sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
209 const GrBackendObject yuvTextureHandles[3],
210 const SkISize yuvSizes[3],
211 GrSurfaceOrigin origin) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800212 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700213
214 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
215 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
216 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700217 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700218 }
219 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
220 GrBackendTextureDesc yDesc;
221 yDesc.fConfig = kConfig;
222 yDesc.fOrigin = origin;
223 yDesc.fSampleCnt = 0;
224 yDesc.fTextureHandle = yuvTextureHandles[0];
225 yDesc.fWidth = yuvSizes[0].fWidth;
226 yDesc.fHeight = yuvSizes[0].fHeight;
227
228 GrBackendTextureDesc uDesc;
229 uDesc.fConfig = kConfig;
230 uDesc.fOrigin = origin;
231 uDesc.fSampleCnt = 0;
232 uDesc.fTextureHandle = yuvTextureHandles[1];
233 uDesc.fWidth = yuvSizes[1].fWidth;
234 uDesc.fHeight = yuvSizes[1].fHeight;
235
236 GrBackendTextureDesc vDesc;
237 vDesc.fConfig = kConfig;
238 vDesc.fOrigin = origin;
239 vDesc.fSampleCnt = 0;
240 vDesc.fTextureHandle = yuvTextureHandles[2];
241 vDesc.fWidth = yuvSizes[2].fWidth;
242 vDesc.fHeight = yuvSizes[2].fHeight;
243
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
245 yDesc, kBorrow_GrWrapOwnership));
246 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
247 uDesc, kBorrow_GrWrapOwnership));
248 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
249 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700250 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700251 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700252 }
253
254 GrSurfaceDesc dstDesc;
255 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
256 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
257 dstDesc.fOrigin = origin;
258 dstDesc.fWidth = yuvSizes[0].fWidth;
259 dstDesc.fHeight = yuvSizes[0].fHeight;
260 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
261 dstDesc.fSampleCnt = 0;
262
bsalomon5ec26ae2016-02-25 08:33:02 -0800263 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, SkBudgeted::kYes));
bsalomon993a4212015-05-29 11:37:25 -0700264 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700265 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700266 }
267
268 GrPaint paint;
269 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf267c1e2016-02-01 13:16:14 -0800270 paint.addColorFragmentProcessor(GrYUVEffect::CreateYUVToRGB(yTex, uTex, vTex, yuvSizes,
271 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700272
273 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
274 SkIntToScalar(dstDesc.fHeight));
robertphillips2e1e51f2015-10-15 08:01:48 -0700275 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(dst->asRenderTarget()));
robertphillipsc9a37062015-09-01 08:34:28 -0700276 if (!drawContext) {
277 return nullptr;
278 }
279
robertphillips2e1e51f2015-10-15 08:01:48 -0700280 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700281 ctx->flushSurfaceWrites(dst);
reed7fb4f8b2016-03-11 04:33:52 -0800282 return sk_make_sp<SkImage_Gpu>(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
283 kOpaque_SkAlphaType, dst, budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700284}
reed56179002015-07-07 06:11:19 -0700285
reed7fb4f8b2016-03-11 04:33:52 -0800286static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
bsalomon8e74f802016-01-30 10:01:40 -0800287 SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter()));
288 if (!texture) {
289 return nullptr;
290 }
reed7fb4f8b2016-03-11 04:33:52 -0800291 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture,
292 SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800293}
294
reed7fb4f8b2016-03-11 04:33:52 -0800295sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800296 if (!context) {
297 return nullptr;
298 }
299 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800300 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800301 }
302 // No way to check whether a image is premul or not?
303 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
304
305 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
306 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
307 return create_image_from_maker(&maker, at, this->uniqueID());
308 }
309 SkBitmap bmp;
310 if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) {
311 return nullptr;
312 }
313 GrBitmapTextureMaker maker(context, bmp);
314 return create_image_from_maker(&maker, at, this->uniqueID());
315}
316
reed7fb4f8b2016-03-11 04:33:52 -0800317sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
318 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800319 if (!ctx) {
320 return nullptr;
321 }
ericrk8bea8902016-03-18 11:52:20 -0700322 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800323 if (!texture) {
324 return nullptr;
325 }
reed7fb4f8b2016-03-11 04:33:52 -0800326 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
327 pixmap.alphaType(), texture, budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800328}
329
reed56179002015-07-07 06:11:19 -0700330///////////////////////////////////////////////////////////////////////////////////////////////////
331
bsalomon41b952c2016-03-11 06:46:33 -0800332class DeferredTextureImage {
333public:
334 SkImage* newImage(GrContext* context, SkBudgeted) const;
335
336private:
337 uint32_t fContextUniqueID;
338 struct Data {
339 SkImageInfo fInfo;
340 void* fPixelData;
341 size_t fRowBytes;
342 int fColorTableCnt;
343 uint32_t* fColorTableData;
344 };
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 }
394 size_t size = 0;
395 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
396 size += dtiSize;
397 size_t pixelOffset = size;
398 size += pixelSize;
399 size_t ctOffset = size;
400 size += ctSize;
401 if (!fillMode) {
402 return size;
403 }
404 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer);
405 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset);
406 SkPMColor* ct = nullptr;
407 if (ctSize) {
408 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset);
409 }
410
411 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize());
412 if (ctSize) {
413 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
414 }
415
416 SkASSERT(info == pixmap.info());
417 size_t rowBytes = pixmap.rowBytes();
418 DeferredTextureImage* dti = new (buffer) DeferredTextureImage();
419 dti->fContextUniqueID = proxy.fContextUniqueID;
420 dti->fData.fInfo = info;
421 dti->fData.fPixelData = pixels;
422 dti->fData.fRowBytes = rowBytes;
423 dti->fData.fColorTableCnt = ctCount;
424 dti->fData.fColorTableData = ct;
425 return size;
426}
427
428sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
429 SkBudgeted budgeted) {
430 if (!data) {
431 return nullptr;
432 }
433 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
434
435 if (!context || context->uniqueID() != dti->fContextUniqueID) {
436 return nullptr;
437 }
438 SkAutoTUnref<SkColorTable> colorTable;
439 if (dti->fData.fColorTableCnt) {
440 SkASSERT(dti->fData.fColorTableData);
441 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt));
442 }
443 SkPixmap pixmap;
444 pixmap.reset(dti->fData.fInfo, dti->fData.fPixelData, dti->fData.fRowBytes, colorTable.get());
445 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
446}
447
448///////////////////////////////////////////////////////////////////////////////////////////////////
449
bsalomon5ec26ae2016-02-25 08:33:02 -0800450GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) {
reed56179002015-07-07 06:11:19 -0700451 GrContext* ctx = src->getContext();
452
453 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700454 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700455 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700456 return nullptr;
reed56179002015-07-07 06:11:19 -0700457 }
cblume61214052016-01-26 09:10:48 -0800458
reed56179002015-07-07 06:11:19 -0700459 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
460 const SkIPoint dstP = SkIPoint::Make(0, 0);
bsalomonb8fea972016-02-16 07:34:17 -0800461 ctx->copySurface(dst, src, srcR, dstP);
462 ctx->flushSurfaceWrites(dst);
reed56179002015-07-07 06:11:19 -0700463 return dst;
464}
465