blob: 5e687b751af60131bc83cda075b7c830a124f815 [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(),
114 info.profileType());
115 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
bsalomon5ec26ae2016-02-25 08:33:02 -0800144 GrTexture* subTx = ctx->textureProvider()->createTexture(desc, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700145 if (!subTx) {
146 return nullptr;
147 }
148 ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800149 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
150 fAlphaType, subTx, 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
reed7fb4f8b2016-03-11 04:33:52 -0800186sk_sp<SkImage> SkImage::MakeFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
187 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700188 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700189 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700190 }
reed56179002015-07-07 06:11:19 -0700191
bsalomon6dc6f5f2015-06-18 09:12:16 -0700192 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700193 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700194 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700196 }
197
bsalomon5ec26ae2016-02-25 08:33:02 -0800198 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, SkBudgeted::kYes));
reed8b26b992015-05-07 15:36:17 -0700199 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700200 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700201 }
202
reed7fb4f8b2016-03-11 04:33:52 -0800203 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst,
204 SkBudgeted::kYes);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800205}
bsalomon993a4212015-05-29 11:37:25 -0700206
reed7fb4f8b2016-03-11 04:33:52 -0800207sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
208 const GrBackendObject yuvTextureHandles[3],
209 const SkISize yuvSizes[3],
210 GrSurfaceOrigin origin) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800211 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700212
213 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
214 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
215 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700216 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700217 }
218 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
219 GrBackendTextureDesc yDesc;
220 yDesc.fConfig = kConfig;
221 yDesc.fOrigin = origin;
222 yDesc.fSampleCnt = 0;
223 yDesc.fTextureHandle = yuvTextureHandles[0];
224 yDesc.fWidth = yuvSizes[0].fWidth;
225 yDesc.fHeight = yuvSizes[0].fHeight;
226
227 GrBackendTextureDesc uDesc;
228 uDesc.fConfig = kConfig;
229 uDesc.fOrigin = origin;
230 uDesc.fSampleCnt = 0;
231 uDesc.fTextureHandle = yuvTextureHandles[1];
232 uDesc.fWidth = yuvSizes[1].fWidth;
233 uDesc.fHeight = yuvSizes[1].fHeight;
234
235 GrBackendTextureDesc vDesc;
236 vDesc.fConfig = kConfig;
237 vDesc.fOrigin = origin;
238 vDesc.fSampleCnt = 0;
239 vDesc.fTextureHandle = yuvTextureHandles[2];
240 vDesc.fWidth = yuvSizes[2].fWidth;
241 vDesc.fHeight = yuvSizes[2].fHeight;
242
bsalomon6dc6f5f2015-06-18 09:12:16 -0700243 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
244 yDesc, kBorrow_GrWrapOwnership));
245 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
246 uDesc, kBorrow_GrWrapOwnership));
247 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
248 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700249 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700251 }
252
253 GrSurfaceDesc dstDesc;
254 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
255 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
256 dstDesc.fOrigin = origin;
257 dstDesc.fWidth = yuvSizes[0].fWidth;
258 dstDesc.fHeight = yuvSizes[0].fHeight;
259 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
260 dstDesc.fSampleCnt = 0;
261
bsalomon5ec26ae2016-02-25 08:33:02 -0800262 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, SkBudgeted::kYes));
bsalomon993a4212015-05-29 11:37:25 -0700263 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700264 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700265 }
266
267 GrPaint paint;
268 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf267c1e2016-02-01 13:16:14 -0800269 paint.addColorFragmentProcessor(GrYUVEffect::CreateYUVToRGB(yTex, uTex, vTex, yuvSizes,
270 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700271
272 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
273 SkIntToScalar(dstDesc.fHeight));
robertphillips2e1e51f2015-10-15 08:01:48 -0700274 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(dst->asRenderTarget()));
robertphillipsc9a37062015-09-01 08:34:28 -0700275 if (!drawContext) {
276 return nullptr;
277 }
278
robertphillips2e1e51f2015-10-15 08:01:48 -0700279 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700280 ctx->flushSurfaceWrites(dst);
reed7fb4f8b2016-03-11 04:33:52 -0800281 return sk_make_sp<SkImage_Gpu>(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
282 kOpaque_SkAlphaType, dst, budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700283}
reed56179002015-07-07 06:11:19 -0700284
reed7fb4f8b2016-03-11 04:33:52 -0800285static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
bsalomon8e74f802016-01-30 10:01:40 -0800286 SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter()));
287 if (!texture) {
288 return nullptr;
289 }
reed7fb4f8b2016-03-11 04:33:52 -0800290 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture,
291 SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800292}
293
reed7fb4f8b2016-03-11 04:33:52 -0800294sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800295 if (!context) {
296 return nullptr;
297 }
298 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800299 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800300 }
301 // No way to check whether a image is premul or not?
302 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
303
304 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
305 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
306 return create_image_from_maker(&maker, at, this->uniqueID());
307 }
308 SkBitmap bmp;
309 if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) {
310 return nullptr;
311 }
312 GrBitmapTextureMaker maker(context, bmp);
313 return create_image_from_maker(&maker, at, this->uniqueID());
314}
315
reed7fb4f8b2016-03-11 04:33:52 -0800316sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
317 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800318 if (!ctx) {
319 return nullptr;
320 }
321 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap));
322 if (!texture) {
323 return nullptr;
324 }
reed7fb4f8b2016-03-11 04:33:52 -0800325 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
326 pixmap.alphaType(), texture, budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800327}
328
reed56179002015-07-07 06:11:19 -0700329///////////////////////////////////////////////////////////////////////////////////////////////////
330
bsalomon41b952c2016-03-11 06:46:33 -0800331class DeferredTextureImage {
332public:
333 SkImage* newImage(GrContext* context, SkBudgeted) const;
334
335private:
336 uint32_t fContextUniqueID;
337 struct Data {
338 SkImageInfo fInfo;
339 void* fPixelData;
340 size_t fRowBytes;
341 int fColorTableCnt;
342 uint32_t* fColorTableData;
343 };
344 Data fData;
345
346 friend class SkImage;
347};
348
349size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
350 const DeferredTextureImageUsageParams[],
351 int paramCnt, void* buffer) const {
352 const bool fillMode = SkToBool(buffer);
353 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
354 return 0;
355 }
356
357 SkAutoPixmapStorage pixmap;
358 SkImageInfo info;
359 size_t pixelSize = 0;
360 size_t ctSize = 0;
361 int ctCount = 0;
362 if (this->peekPixels(&pixmap)) {
363 info = pixmap.info();
364 pixelSize = SkAlign8(pixmap.getSafeSize());
365 if (pixmap.ctable()) {
366 ctCount = pixmap.ctable()->count();
367 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
368 }
369 } else {
370 // Here we're just using presence of data to know whether there is a codec behind the image.
371 // In the future we will access the cacherator and get the exact data that we want to (e.g.
372 // yuv planes) upload.
373 SkAutoTUnref<SkData> data(this->refEncoded());
374 if (!data) {
375 return 0;
376 }
377 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
378 info = SkImageInfo::MakeN32(this->width(), this->height(), at);
379 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
380 if (fillMode) {
381 pixmap.alloc(info);
382 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
383 return 0;
384 }
385 SkASSERT(!pixmap.ctable());
386 }
387 }
388 size_t size = 0;
389 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
390 size += dtiSize;
391 size_t pixelOffset = size;
392 size += pixelSize;
393 size_t ctOffset = size;
394 size += ctSize;
395 if (!fillMode) {
396 return size;
397 }
398 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer);
399 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset);
400 SkPMColor* ct = nullptr;
401 if (ctSize) {
402 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset);
403 }
404
405 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize());
406 if (ctSize) {
407 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
408 }
409
410 SkASSERT(info == pixmap.info());
411 size_t rowBytes = pixmap.rowBytes();
412 DeferredTextureImage* dti = new (buffer) DeferredTextureImage();
413 dti->fContextUniqueID = proxy.fContextUniqueID;
414 dti->fData.fInfo = info;
415 dti->fData.fPixelData = pixels;
416 dti->fData.fRowBytes = rowBytes;
417 dti->fData.fColorTableCnt = ctCount;
418 dti->fData.fColorTableData = ct;
419 return size;
420}
421
422sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
423 SkBudgeted budgeted) {
424 if (!data) {
425 return nullptr;
426 }
427 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
428
429 if (!context || context->uniqueID() != dti->fContextUniqueID) {
430 return nullptr;
431 }
432 SkAutoTUnref<SkColorTable> colorTable;
433 if (dti->fData.fColorTableCnt) {
434 SkASSERT(dti->fData.fColorTableData);
435 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt));
436 }
437 SkPixmap pixmap;
438 pixmap.reset(dti->fData.fInfo, dti->fData.fPixelData, dti->fData.fRowBytes, colorTable.get());
439 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
440}
441
442///////////////////////////////////////////////////////////////////////////////////////////////////
443
bsalomon5ec26ae2016-02-25 08:33:02 -0800444GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) {
reed56179002015-07-07 06:11:19 -0700445 GrContext* ctx = src->getContext();
446
447 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700448 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700449 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700450 return nullptr;
reed56179002015-07-07 06:11:19 -0700451 }
cblume61214052016-01-26 09:10:48 -0800452
reed56179002015-07-07 06:11:19 -0700453 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
454 const SkIPoint dstP = SkIPoint::Make(0, 0);
bsalomonb8fea972016-02-16 07:34:17 -0800455 ctx->copySurface(dst, src, srcR, dstP);
456 ctx->flushSurfaceWrites(dst);
reed56179002015-07-07 06:11:19 -0700457 return dst;
458}
459