blob: 163e62e02df540362952e9b55be47d3284d886a7 [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
reed85d91782015-09-10 14:33:38 -070082GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, SkImageUsageType usage) const {
83 fTexture->ref();
84 return fTexture;
85}
86
reed8b26b992015-05-07 15:36:17 -070087bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -070088 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -070089}
90
91static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
92 switch (info.colorType()) {
93 case kRGBA_8888_SkColorType:
94 case kBGRA_8888_SkColorType:
95 break;
96 default:
97 return; // nothing to do
98 }
99
100 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
101 // and in either case, the alpha-byte is always in the same place, so we can safely call
102 // SkPreMultiplyColor()
103 //
104 SkColor* row = (SkColor*)pixels;
105 for (int y = 0; y < info.height(); ++y) {
106 for (int x = 0; x < info.width(); ++x) {
107 row[x] = SkPreMultiplyColor(row[x]);
108 }
109 }
110}
111
112bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
113 int srcX, int srcY) const {
114 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
115 info.profileType());
116 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
139///////////////////////////////////////////////////////////////////////////////////////////////////
140
bsalomon6dc6f5f2015-06-18 09:12:16 -0700141static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
reedde499882015-06-18 13:41:40 -0700142 SkAlphaType at, GrWrapOwnership ownership,
143 SkImage::TextureReleaseProc releaseProc,
144 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700145 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700146 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700147 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700148 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700149 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700150 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700151 }
reedde499882015-06-18 13:41:40 -0700152 if (releaseProc) {
153 tex->setRelease(releaseProc, releaseCtx);
154 }
155
reed8b26b992015-05-07 15:36:17 -0700156 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
halcanary385fe4d2015-08-26 13:07:48 -0700157 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, 0, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700158}
159
reedde499882015-06-18 13:41:40 -0700160SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at,
161 TextureReleaseProc releaseP, ReleaseContext releaseC) {
162 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700163}
164
165SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
166 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700168}
169
reed56179002015-07-07 06:11:19 -0700170SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
reed8b26b992015-05-07 15:36:17 -0700171 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700172 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700173 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700174 }
reed56179002015-07-07 06:11:19 -0700175
bsalomon6dc6f5f2015-06-18 09:12:16 -0700176 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700177 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700178 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700179 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700180 }
181
reed56179002015-07-07 06:11:19 -0700182 const bool isBudgeted = true;
183 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted));
reed8b26b992015-05-07 15:36:17 -0700184 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700185 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700186 }
187
reed56179002015-07-07 06:11:19 -0700188 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
reed8b26b992015-05-07 15:36:17 -0700189 const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()?
halcanary385fe4d2015-08-26 13:07:48 -0700190 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, sampleCount,
191 budgeted);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800192}
bsalomon993a4212015-05-29 11:37:25 -0700193
194SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
195 const GrBackendObject yuvTextureHandles[3],
196 const SkISize yuvSizes[3],
197 GrSurfaceOrigin origin) {
198 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
199
200 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
201 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
202 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700203 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700204 }
205 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
206 GrBackendTextureDesc yDesc;
207 yDesc.fConfig = kConfig;
208 yDesc.fOrigin = origin;
209 yDesc.fSampleCnt = 0;
210 yDesc.fTextureHandle = yuvTextureHandles[0];
211 yDesc.fWidth = yuvSizes[0].fWidth;
212 yDesc.fHeight = yuvSizes[0].fHeight;
213
214 GrBackendTextureDesc uDesc;
215 uDesc.fConfig = kConfig;
216 uDesc.fOrigin = origin;
217 uDesc.fSampleCnt = 0;
218 uDesc.fTextureHandle = yuvTextureHandles[1];
219 uDesc.fWidth = yuvSizes[1].fWidth;
220 uDesc.fHeight = yuvSizes[1].fHeight;
221
222 GrBackendTextureDesc vDesc;
223 vDesc.fConfig = kConfig;
224 vDesc.fOrigin = origin;
225 vDesc.fSampleCnt = 0;
226 vDesc.fTextureHandle = yuvTextureHandles[2];
227 vDesc.fWidth = yuvSizes[2].fWidth;
228 vDesc.fHeight = yuvSizes[2].fHeight;
229
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
231 yDesc, kBorrow_GrWrapOwnership));
232 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
233 uDesc, kBorrow_GrWrapOwnership));
234 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
235 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700236 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700237 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700238 }
239
240 GrSurfaceDesc dstDesc;
241 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
242 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
243 dstDesc.fOrigin = origin;
244 dstDesc.fWidth = yuvSizes[0].fWidth;
245 dstDesc.fHeight = yuvSizes[0].fHeight;
246 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
247 dstDesc.fSampleCnt = 0;
248
bsalomoneae62002015-07-31 13:59:30 -0700249 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true));
bsalomon993a4212015-05-29 11:37:25 -0700250 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700251 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700252 }
253
254 GrPaint paint;
255 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonac856c92015-08-27 06:30:17 -0700256 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(),
257 yTex, uTex, vTex, yuvSizes,
258 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700259
260 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
261 SkIntToScalar(dstDesc.fHeight));
robertphillipsc9a37062015-09-01 08:34:28 -0700262 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext());
263 if (!drawContext) {
264 return nullptr;
265 }
266
robertphillips2334fb62015-06-17 05:43:33 -0700267 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700268 ctx->flushSurfaceWrites(dst);
halcanary385fe4d2015-08-26 13:07:48 -0700269 return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
270 kOpaque_SkAlphaType, dst, 0, budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700271}
reed56179002015-07-07 06:11:19 -0700272
273///////////////////////////////////////////////////////////////////////////////////////////////////
274
275GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) {
276 GrContext* ctx = src->getContext();
277
278 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700279 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700280 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
reed56179002015-07-07 06:11:19 -0700282 }
283
284 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
285 const SkIPoint dstP = SkIPoint::Make(0, 0);
286 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
287 return dst;
288}
289