blob: 1a63a0d6ba3cf51af625d616d77cb27de4c459fb [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,
reed7b6945b2015-09-24 00:50:58 -070018 SkSurface::Budgeted budgeted)
halcanary96fcdcc2015-08-27 07:41:13 -070019 : INHERITED(w, h, uniqueID, nullptr)
reed8b26b992015-05-07 15:36:17 -070020 , fTexture(SkRef(tex))
reed8b26b992015-05-07 15:36:17 -070021 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080022 , fBudgeted(budgeted)
bsalomon1cd63112015-08-05 06:58:39 -070023 , fAddedRasterVersionToCache(false)
reed8b26b992015-05-07 15:36:17 -070024 {}
piotaixrcef04f82014-07-14 07:48:04 -070025
reed6f1216a2015-08-04 08:10:13 -070026SkImage_Gpu::~SkImage_Gpu() {
27 if (fAddedRasterVersionToCache.load()) {
28 SkNotifyBitmapGenIDIsStale(this->uniqueID());
29 }
30}
31
bsalomoneaaaf0b2015-01-23 08:08:04 -080032extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
bsalomon55812362015-06-10 08:49:28 -070033 if (as_IB(image)->getTexture()) {
reed8b26b992015-05-07 15:36:17 -070034 ((SkImage_Gpu*)image)->applyBudgetDecision();
35 }
36}
37
tomhudsonf2608512015-09-18 09:36:12 -070038SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
39 const SkMatrix* localMatrix) const {
40 SkBitmap bm;
41 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &bm);
42 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix);
43}
44
reed8b26b992015-05-07 15:36:17 -070045bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
reed6f1216a2015-08-04 08:10:13 -070046 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
47 SkASSERT(dst->getGenerationID() == this->uniqueID());
48 SkASSERT(dst->isImmutable());
49 SkASSERT(dst->getPixels());
50 return true;
51 }
52
reed8b26b992015-05-07 15:36:17 -070053 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
54 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) {
55 return false;
56 }
57 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
58 dst->getPixels(), dst->rowBytes())) {
59 return false;
60 }
reed6f1216a2015-08-04 08:10:13 -070061
62 dst->pixelRef()->setImmutableWithID(this->uniqueID());
63 SkBitmapCache::Add(this->uniqueID(), *dst);
64 fAddedRasterVersionToCache.store(true);
reed8b26b992015-05-07 15:36:17 -070065 return true;
66}
67
reed85d91782015-09-10 14:33:38 -070068GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, SkImageUsageType usage) const {
69 fTexture->ref();
70 return fTexture;
71}
72
reed8b26b992015-05-07 15:36:17 -070073bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -070074 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -070075}
76
77static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
78 switch (info.colorType()) {
79 case kRGBA_8888_SkColorType:
80 case kBGRA_8888_SkColorType:
81 break;
82 default:
83 return; // nothing to do
84 }
85
86 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
87 // and in either case, the alpha-byte is always in the same place, so we can safely call
88 // SkPreMultiplyColor()
89 //
90 SkColor* row = (SkColor*)pixels;
91 for (int y = 0; y < info.height(); ++y) {
92 for (int x = 0; x < info.width(); ++x) {
93 row[x] = SkPreMultiplyColor(row[x]);
94 }
95 }
96}
97
98bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
99 int srcX, int srcY) const {
100 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
101 info.profileType());
102 uint32_t flags = 0;
103 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
104 // let the GPU perform this transformation for us
105 flags = GrContext::kUnpremul_PixelOpsFlag;
106 }
107 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
108 pixels, rowBytes, flags)) {
109 return false;
110 }
111 // do we have to manually fix-up the alpha channel?
112 // src dst
113 // unpremul premul fix manually
114 // premul unpremul done by kUnpremul_PixelOpsFlag
115 // all other combos need to change.
116 //
117 // Should this be handled by Ganesh? todo:?
118 //
119 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
120 apply_premul(info, pixels, rowBytes);
121 }
122 return true;
123}
124
reed7b6945b2015-09-24 00:50:58 -0700125SkImage* SkImage_Gpu::onNewSubset(const SkIRect& subset) const {
126 GrContext* ctx = fTexture->getContext();
127 GrSurfaceDesc desc = fTexture->desc();
128 desc.fWidth = subset.width();
129 desc.fHeight = subset.height();
130
131 GrTexture* subTx = ctx->textureProvider()->createTexture(desc,
132 SkSurface::kYes_Budgeted == fBudgeted);
133 if (!subTx) {
134 return nullptr;
135 }
136 ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0));
137 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, fAlphaType, subTx,
138 fBudgeted);
139}
140
reed8b26b992015-05-07 15:36:17 -0700141///////////////////////////////////////////////////////////////////////////////////////////////////
142
bsalomon6dc6f5f2015-06-18 09:12:16 -0700143static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
reedde499882015-06-18 13:41:40 -0700144 SkAlphaType at, GrWrapOwnership ownership,
145 SkImage::TextureReleaseProc releaseProc,
146 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700147 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700149 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700150 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700151 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700152 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700153 }
reedde499882015-06-18 13:41:40 -0700154 if (releaseProc) {
155 tex->setRelease(releaseProc, releaseCtx);
156 }
157
reed8b26b992015-05-07 15:36:17 -0700158 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
reed7b6945b2015-09-24 00:50:58 -0700159 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700160}
161
reedde499882015-06-18 13:41:40 -0700162SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at,
163 TextureReleaseProc releaseP, ReleaseContext releaseC) {
164 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700165}
166
167SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
168 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700170}
171
reed56179002015-07-07 06:11:19 -0700172SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
reed8b26b992015-05-07 15:36:17 -0700173 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700174 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700175 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700176 }
reed56179002015-07-07 06:11:19 -0700177
bsalomon6dc6f5f2015-06-18 09:12:16 -0700178 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700179 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700180 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700181 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700182 }
183
reed56179002015-07-07 06:11:19 -0700184 const bool isBudgeted = true;
185 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted));
reed8b26b992015-05-07 15:36:17 -0700186 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700187 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700188 }
189
reed56179002015-07-07 06:11:19 -0700190 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
reed7b6945b2015-09-24 00:50:58 -0700191 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst,
halcanary385fe4d2015-08-26 13:07:48 -0700192 budgeted);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800193}
bsalomon993a4212015-05-29 11:37:25 -0700194
195SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
196 const GrBackendObject yuvTextureHandles[3],
197 const SkISize yuvSizes[3],
198 GrSurfaceOrigin origin) {
199 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
200
201 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
202 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
203 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700204 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700205 }
206 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
207 GrBackendTextureDesc yDesc;
208 yDesc.fConfig = kConfig;
209 yDesc.fOrigin = origin;
210 yDesc.fSampleCnt = 0;
211 yDesc.fTextureHandle = yuvTextureHandles[0];
212 yDesc.fWidth = yuvSizes[0].fWidth;
213 yDesc.fHeight = yuvSizes[0].fHeight;
214
215 GrBackendTextureDesc uDesc;
216 uDesc.fConfig = kConfig;
217 uDesc.fOrigin = origin;
218 uDesc.fSampleCnt = 0;
219 uDesc.fTextureHandle = yuvTextureHandles[1];
220 uDesc.fWidth = yuvSizes[1].fWidth;
221 uDesc.fHeight = yuvSizes[1].fHeight;
222
223 GrBackendTextureDesc vDesc;
224 vDesc.fConfig = kConfig;
225 vDesc.fOrigin = origin;
226 vDesc.fSampleCnt = 0;
227 vDesc.fTextureHandle = yuvTextureHandles[2];
228 vDesc.fWidth = yuvSizes[2].fWidth;
229 vDesc.fHeight = yuvSizes[2].fHeight;
230
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
232 yDesc, kBorrow_GrWrapOwnership));
233 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
234 uDesc, kBorrow_GrWrapOwnership));
235 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
236 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700237 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700238 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700239 }
240
241 GrSurfaceDesc dstDesc;
242 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
243 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
244 dstDesc.fOrigin = origin;
245 dstDesc.fWidth = yuvSizes[0].fWidth;
246 dstDesc.fHeight = yuvSizes[0].fHeight;
247 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
248 dstDesc.fSampleCnt = 0;
249
bsalomoneae62002015-07-31 13:59:30 -0700250 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true));
bsalomon993a4212015-05-29 11:37:25 -0700251 if (!dst) {
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);
bsalomonac856c92015-08-27 06:30:17 -0700257 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(),
258 yTex, uTex, vTex, yuvSizes,
259 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700260
261 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
262 SkIntToScalar(dstDesc.fHeight));
robertphillipsc9a37062015-09-01 08:34:28 -0700263 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext());
264 if (!drawContext) {
265 return nullptr;
266 }
267
robertphillips2334fb62015-06-17 05:43:33 -0700268 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700269 ctx->flushSurfaceWrites(dst);
halcanary385fe4d2015-08-26 13:07:48 -0700270 return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
reed7b6945b2015-09-24 00:50:58 -0700271 kOpaque_SkAlphaType, dst, budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700272}
reed56179002015-07-07 06:11:19 -0700273
274///////////////////////////////////////////////////////////////////////////////////////////////////
275
276GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) {
277 GrContext* ctx = src->getContext();
278
279 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700280 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700281 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700282 return nullptr;
reed56179002015-07-07 06:11:19 -0700283 }
284
285 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
286 const SkIPoint dstP = SkIPoint::Make(0, 0);
287 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
288 return dst;
289}
290