blob: 6655e39c17d400963708767639e3981df22b1fc0 [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)
halcanary96fcdcc2015-08-27 07:41:13 -070019 : INHERITED(w, h, uniqueID, nullptr)
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
halcanary96fcdcc2015-08-27 07:41:13 -070039 return nullptr;
robertphillips@google.com97b6b072012-10-31 14:48:39 +000040 }
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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700141 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700142 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700143 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700144 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700146 }
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;
halcanary385fe4d2015-08-26 13:07:48 -0700152 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, 0, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700153}
154
reedde499882015-06-18 13:41:40 -0700155SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at,
156 TextureReleaseProc releaseP, ReleaseContext releaseC) {
157 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700158}
159
160SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
161 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700163}
164
reed56179002015-07-07 06:11:19 -0700165SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
reed8b26b992015-05-07 15:36:17 -0700166 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700167 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700168 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700169 }
reed56179002015-07-07 06:11:19 -0700170
bsalomon6dc6f5f2015-06-18 09:12:16 -0700171 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700172 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700173 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700174 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700175 }
176
reed56179002015-07-07 06:11:19 -0700177 const bool isBudgeted = true;
178 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted));
reed8b26b992015-05-07 15:36:17 -0700179 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700180 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700181 }
182
reed56179002015-07-07 06:11:19 -0700183 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
reed8b26b992015-05-07 15:36:17 -0700184 const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()?
halcanary385fe4d2015-08-26 13:07:48 -0700185 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, sampleCount,
186 budgeted);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800187}
bsalomon993a4212015-05-29 11:37:25 -0700188
189SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
190 const GrBackendObject yuvTextureHandles[3],
191 const SkISize yuvSizes[3],
192 GrSurfaceOrigin origin) {
193 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
194
195 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
196 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
197 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700198 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700199 }
200 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
201 GrBackendTextureDesc yDesc;
202 yDesc.fConfig = kConfig;
203 yDesc.fOrigin = origin;
204 yDesc.fSampleCnt = 0;
205 yDesc.fTextureHandle = yuvTextureHandles[0];
206 yDesc.fWidth = yuvSizes[0].fWidth;
207 yDesc.fHeight = yuvSizes[0].fHeight;
208
209 GrBackendTextureDesc uDesc;
210 uDesc.fConfig = kConfig;
211 uDesc.fOrigin = origin;
212 uDesc.fSampleCnt = 0;
213 uDesc.fTextureHandle = yuvTextureHandles[1];
214 uDesc.fWidth = yuvSizes[1].fWidth;
215 uDesc.fHeight = yuvSizes[1].fHeight;
216
217 GrBackendTextureDesc vDesc;
218 vDesc.fConfig = kConfig;
219 vDesc.fOrigin = origin;
220 vDesc.fSampleCnt = 0;
221 vDesc.fTextureHandle = yuvTextureHandles[2];
222 vDesc.fWidth = yuvSizes[2].fWidth;
223 vDesc.fHeight = yuvSizes[2].fHeight;
224
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
226 yDesc, kBorrow_GrWrapOwnership));
227 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
228 uDesc, kBorrow_GrWrapOwnership));
229 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
230 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700231 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700232 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700233 }
234
235 GrSurfaceDesc dstDesc;
236 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
237 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
238 dstDesc.fOrigin = origin;
239 dstDesc.fWidth = yuvSizes[0].fWidth;
240 dstDesc.fHeight = yuvSizes[0].fHeight;
241 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
242 dstDesc.fSampleCnt = 0;
243
bsalomoneae62002015-07-31 13:59:30 -0700244 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true));
bsalomon993a4212015-05-29 11:37:25 -0700245 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700246 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700247 }
248
249 GrPaint paint;
250 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonac856c92015-08-27 06:30:17 -0700251 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(),
252 yTex, uTex, vTex, yuvSizes,
253 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700254
255 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
256 SkIntToScalar(dstDesc.fHeight));
robertphillips2334fb62015-06-17 05:43:33 -0700257 GrDrawContext* drawContext = ctx->drawContext();
258 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700259 ctx->flushSurfaceWrites(dst);
halcanary385fe4d2015-08-26 13:07:48 -0700260 return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
261 kOpaque_SkAlphaType, dst, 0, budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700262}
reed56179002015-07-07 06:11:19 -0700263
264///////////////////////////////////////////////////////////////////////////////////////////////////
265
266GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) {
267 GrContext* ctx = src->getContext();
268
269 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700270 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700271 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700272 return nullptr;
reed56179002015-07-07 06:11:19 -0700273 }
274
275 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
276 const SkIPoint dstP = SkIPoint::Make(0, 0);
277 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
278 return dst;
279}
280