blob: d172df829ea1e811405e536c894efd82295eb07f [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
reed6f1216a2015-08-04 08:10:13 -07008#include "SkBitmapCache.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -08009#include "SkImage_Gpu.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000010#include "GrContext.h"
bsalomon993a4212015-05-29 11:37:25 -070011#include "GrDrawContext.h"
12#include "effects/GrYUVtoRGBEffect.h"
13#include "SkCanvas.h"
reed8b26b992015-05-07 15:36:17 -070014#include "SkGpuDevice.h"
reed6f1216a2015-08-04 08:10:13 -070015#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070016
reed80c772b2015-07-30 18:58:23 -070017SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex,
reed8b26b992015-05-07 15:36:17 -070018 int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted)
reed80c772b2015-07-30 18:58:23 -070019 : INHERITED(w, h, uniqueID, NULL)
reed8b26b992015-05-07 15:36:17 -070020 , fTexture(SkRef(tex))
reed3f10b9d2014-11-21 10:27:53 -080021 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces)
reed8b26b992015-05-07 15:36:17 -070022 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080023 , fBudgeted(budgeted)
bsalomon1cd63112015-08-05 06:58:39 -070024 , fAddedRasterVersionToCache(false)
reed8b26b992015-05-07 15:36:17 -070025 {}
piotaixrcef04f82014-07-14 07:48:04 -070026
reed6f1216a2015-08-04 08:10:13 -070027SkImage_Gpu::~SkImage_Gpu() {
28 if (fAddedRasterVersionToCache.load()) {
29 SkNotifyBitmapGenIDIsStale(this->uniqueID());
30 }
31}
32
reed4af267b2014-11-21 08:46:37 -080033SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
reed8b26b992015-05-07 15:36:17 -070034 GrTexture* tex = this->getTexture();
35 SkASSERT(tex);
36 GrContext* ctx = tex->getContext();
37 if (!ctx) {
38 // the texture may have been abandoned, so we have to check
robertphillips@google.com97b6b072012-10-31 14:48:39 +000039 return NULL;
40 }
reed8b26b992015-05-07 15:36:17 -070041 // TODO: Change signature of onNewSurface to take a budgeted param.
42 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
43 return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSurfaces, &props);
robertphillips@google.com97b6b072012-10-31 14:48:39 +000044}
reed4af267b2014-11-21 08:46:37 -080045
bsalomoneaaaf0b2015-01-23 08:08:04 -080046extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
bsalomon55812362015-06-10 08:49:28 -070047 if (as_IB(image)->getTexture()) {
reed8b26b992015-05-07 15:36:17 -070048 ((SkImage_Gpu*)image)->applyBudgetDecision();
49 }
50}
51
52SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
53 const SkMatrix* localMatrix) const {
54 SkBitmap bm;
55 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &bm);
56 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix);
57}
58
59bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
reed6f1216a2015-08-04 08:10:13 -070060 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
61 SkASSERT(dst->getGenerationID() == this->uniqueID());
62 SkASSERT(dst->isImmutable());
63 SkASSERT(dst->getPixels());
64 return true;
65 }
66
reed8b26b992015-05-07 15:36:17 -070067 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
68 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) {
69 return false;
70 }
71 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
72 dst->getPixels(), dst->rowBytes())) {
73 return false;
74 }
reed6f1216a2015-08-04 08:10:13 -070075
76 dst->pixelRef()->setImmutableWithID(this->uniqueID());
77 SkBitmapCache::Add(this->uniqueID(), *dst);
78 fAddedRasterVersionToCache.store(true);
reed8b26b992015-05-07 15:36:17 -070079 return true;
80}
81
82bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -070083 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -070084}
85
86static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
87 switch (info.colorType()) {
88 case kRGBA_8888_SkColorType:
89 case kBGRA_8888_SkColorType:
90 break;
91 default:
92 return; // nothing to do
93 }
94
95 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
96 // and in either case, the alpha-byte is always in the same place, so we can safely call
97 // SkPreMultiplyColor()
98 //
99 SkColor* row = (SkColor*)pixels;
100 for (int y = 0; y < info.height(); ++y) {
101 for (int x = 0; x < info.width(); ++x) {
102 row[x] = SkPreMultiplyColor(row[x]);
103 }
104 }
105}
106
107bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
108 int srcX, int srcY) const {
109 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
110 info.profileType());
111 uint32_t flags = 0;
112 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
113 // let the GPU perform this transformation for us
114 flags = GrContext::kUnpremul_PixelOpsFlag;
115 }
116 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
117 pixels, rowBytes, flags)) {
118 return false;
119 }
120 // do we have to manually fix-up the alpha channel?
121 // src dst
122 // unpremul premul fix manually
123 // premul unpremul done by kUnpremul_PixelOpsFlag
124 // all other combos need to change.
125 //
126 // Should this be handled by Ganesh? todo:?
127 //
128 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
129 apply_premul(info, pixels, rowBytes);
130 }
131 return true;
132}
133
134///////////////////////////////////////////////////////////////////////////////////////////////////
135
bsalomon6dc6f5f2015-06-18 09:12:16 -0700136static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
reedde499882015-06-18 13:41:40 -0700137 SkAlphaType at, GrWrapOwnership ownership,
138 SkImage::TextureReleaseProc releaseProc,
139 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700140 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
141 return NULL;
142 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700143 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700144 if (!tex) {
145 return NULL;
146 }
reedde499882015-06-18 13:41:40 -0700147 if (releaseProc) {
148 tex->setRelease(releaseProc, releaseCtx);
149 }
150
reed8b26b992015-05-07 15:36:17 -0700151 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
reed80c772b2015-07-30 18:58:23 -0700152 return SkNEW_ARGS(SkImage_Gpu,
153 (desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, 0, budgeted));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700154
155}
156
reedde499882015-06-18 13:41:40 -0700157SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at,
158 TextureReleaseProc releaseP, ReleaseContext releaseC) {
159 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700160}
161
162SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
163 SkAlphaType at) {
reedde499882015-06-18 13:41:40 -0700164 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, NULL, NULL);
reed8b26b992015-05-07 15:36:17 -0700165}
166
reed56179002015-07-07 06:11:19 -0700167SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
reed8b26b992015-05-07 15:36:17 -0700168 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700169 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
reed8b26b992015-05-07 15:36:17 -0700170 return NULL;
171 }
reed56179002015-07-07 06:11:19 -0700172
bsalomon6dc6f5f2015-06-18 09:12:16 -0700173 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700174 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700175 if (!src) {
176 return NULL;
177 }
178
reed56179002015-07-07 06:11:19 -0700179 const bool isBudgeted = true;
180 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted));
reed8b26b992015-05-07 15:36:17 -0700181 if (!dst) {
182 return NULL;
183 }
184
reed56179002015-07-07 06:11:19 -0700185 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
reed8b26b992015-05-07 15:36:17 -0700186 const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()?
reed80c772b2015-07-30 18:58:23 -0700187 return SkNEW_ARGS(SkImage_Gpu, (desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
188 at, dst, sampleCount, budgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800189}
bsalomon993a4212015-05-29 11:37:25 -0700190
191SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
192 const GrBackendObject yuvTextureHandles[3],
193 const SkISize yuvSizes[3],
194 GrSurfaceOrigin origin) {
195 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
196
197 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
198 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
199 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
200 return NULL;
201 }
202 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
203 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
219 GrBackendTextureDesc vDesc;
220 vDesc.fConfig = kConfig;
221 vDesc.fOrigin = origin;
222 vDesc.fSampleCnt = 0;
223 vDesc.fTextureHandle = yuvTextureHandles[2];
224 vDesc.fWidth = yuvSizes[2].fWidth;
225 vDesc.fHeight = yuvSizes[2].fHeight;
226
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
228 yDesc, kBorrow_GrWrapOwnership));
229 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
230 uDesc, kBorrow_GrWrapOwnership));
231 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
232 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700233 if (!yTex || !uTex || !vTex) {
234 return NULL;
235 }
236
237 GrSurfaceDesc dstDesc;
238 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
239 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
240 dstDesc.fOrigin = origin;
241 dstDesc.fWidth = yuvSizes[0].fWidth;
242 dstDesc.fHeight = yuvSizes[0].fHeight;
243 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
244 dstDesc.fSampleCnt = 0;
245
bsalomoneae62002015-07-31 13:59:30 -0700246 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true));
bsalomon993a4212015-05-29 11:37:25 -0700247 if (!dst) {
248 return NULL;
249 }
250
251 GrPaint paint;
252 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
joshualitt2cdec312015-07-09 07:31:31 -0700253 paint.addColorProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yTex, uTex,
254 vTex, yuvSizes, colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700255
256 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
257 SkIntToScalar(dstDesc.fHeight));
robertphillips2334fb62015-06-17 05:43:33 -0700258 GrDrawContext* drawContext = ctx->drawContext();
259 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700260 ctx->flushSurfaceWrites(dst);
reed80c772b2015-07-30 18:58:23 -0700261 return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
262 kOpaque_SkAlphaType, dst, 0, budgeted));
bsalomon993a4212015-05-29 11:37:25 -0700263}
reed56179002015-07-07 06:11:19 -0700264
265///////////////////////////////////////////////////////////////////////////////////////////////////
266
267GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) {
268 GrContext* ctx = src->getContext();
269
270 GrSurfaceDesc desc = src->desc();
reed56179002015-07-07 06:11:19 -0700271 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, NULL, 0);
272 if (!dst) {
273 return NULL;
274 }
275
276 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
277 const SkIPoint dstP = SkIPoint::Make(0, 0);
278 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
279 return dst;
280}
281