blob: e6af3265b629361b74415a50765115a270956370 [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"
reed856e9d92015-09-30 12:21:45 -070010#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000011#include "GrContext.h"
bsalomon993a4212015-05-29 11:37:25 -070012#include "GrDrawContext.h"
reed856e9d92015-09-30 12:21:45 -070013#include "GrTextureMaker.h"
bsalomon993a4212015-05-29 11:37:25 -070014#include "effects/GrYUVtoRGBEffect.h"
15#include "SkCanvas.h"
reed8b26b992015-05-07 15:36:17 -070016#include "SkGpuDevice.h"
reed856e9d92015-09-30 12:21:45 -070017#include "SkGrPriv.h"
reed6f1216a2015-08-04 08:10:13 -070018#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070019
reed80c772b2015-07-30 18:58:23 -070020SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex,
reed7b6945b2015-09-24 00:50:58 -070021 SkSurface::Budgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070022 : INHERITED(w, h, uniqueID)
reed8b26b992015-05-07 15:36:17 -070023 , fTexture(SkRef(tex))
reed8b26b992015-05-07 15:36:17 -070024 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080025 , fBudgeted(budgeted)
bsalomon1cd63112015-08-05 06:58:39 -070026 , fAddedRasterVersionToCache(false)
reed8b26b992015-05-07 15:36:17 -070027 {}
piotaixrcef04f82014-07-14 07:48:04 -070028
reed6f1216a2015-08-04 08:10:13 -070029SkImage_Gpu::~SkImage_Gpu() {
30 if (fAddedRasterVersionToCache.load()) {
31 SkNotifyBitmapGenIDIsStale(this->uniqueID());
32 }
33}
34
bsalomoneaaaf0b2015-01-23 08:08:04 -080035extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
bsalomon55812362015-06-10 08:49:28 -070036 if (as_IB(image)->getTexture()) {
reed8b26b992015-05-07 15:36:17 -070037 ((SkImage_Gpu*)image)->applyBudgetDecision();
38 }
39}
40
reed8b26b992015-05-07 15:36:17 -070041bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
reed6f1216a2015-08-04 08:10:13 -070042 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
43 SkASSERT(dst->getGenerationID() == this->uniqueID());
44 SkASSERT(dst->isImmutable());
45 SkASSERT(dst->getPixels());
46 return true;
47 }
48
reed8b26b992015-05-07 15:36:17 -070049 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
50 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) {
51 return false;
52 }
53 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
54 dst->getPixels(), dst->rowBytes())) {
55 return false;
56 }
reed6f1216a2015-08-04 08:10:13 -070057
58 dst->pixelRef()->setImmutableWithID(this->uniqueID());
59 SkBitmapCache::Add(this->uniqueID(), *dst);
60 fAddedRasterVersionToCache.store(true);
reed8b26b992015-05-07 15:36:17 -070061 return true;
62}
63
reed856e9d92015-09-30 12:21:45 -070064static void make_raw_texture_stretched_key(uint32_t imageID, const SkGrStretch& stretch,
65 GrUniqueKey* stretchedKey) {
66 SkASSERT(SkGrStretch::kNone_Type != stretch.fType);
67
68 uint32_t width = SkToU16(stretch.fWidth);
69 uint32_t height = SkToU16(stretch.fHeight);
70
71 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
72 GrUniqueKey::Builder builder(stretchedKey, kDomain, 3);
73 builder[0] = imageID;
74 builder[1] = stretch.fType;
75 builder[2] = width | (height << 16);
76 builder.finish();
77}
78
79class Texture_GrTextureMaker : public GrTextureMaker {
80public:
81 Texture_GrTextureMaker(const SkImage* image, GrTexture* unstretched)
82 : INHERITED(image->width(), image->height())
83 , fImage(image)
84 , fUnstretched(unstretched)
85 {}
86
87protected:
88 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override {
89 return SkRef(fUnstretched);
90 }
91
92 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override {
93 make_raw_texture_stretched_key(fImage->uniqueID(), stretch, stretchedKey);
94 return stretchedKey->isValid();
95 }
96
97 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
98 as_IB(fImage)->notifyAddedToCache();
99 }
100
101 bool onGetROBitmap(SkBitmap* bitmap) override {
102 return as_IB(fImage)->getROPixels(bitmap);
103 }
104
105private:
106 const SkImage* fImage;
107 GrTexture* fUnstretched;
108
109 typedef GrTextureMaker INHERITED;
110};
111
reed85d91782015-09-10 14:33:38 -0700112GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, SkImageUsageType usage) const {
reed856e9d92015-09-30 12:21:45 -0700113 return Texture_GrTextureMaker(this, fTexture).refCachedTexture(ctx, usage);
reed85d91782015-09-10 14:33:38 -0700114}
115
reed8b26b992015-05-07 15:36:17 -0700116bool SkImage_Gpu::isOpaque() const {
bsalomon74f681d2015-06-23 14:38:48 -0700117 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
reed8b26b992015-05-07 15:36:17 -0700118}
119
120static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
121 switch (info.colorType()) {
122 case kRGBA_8888_SkColorType:
123 case kBGRA_8888_SkColorType:
124 break;
125 default:
126 return; // nothing to do
127 }
128
129 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
130 // and in either case, the alpha-byte is always in the same place, so we can safely call
131 // SkPreMultiplyColor()
132 //
133 SkColor* row = (SkColor*)pixels;
134 for (int y = 0; y < info.height(); ++y) {
135 for (int x = 0; x < info.width(); ++x) {
136 row[x] = SkPreMultiplyColor(row[x]);
137 }
138 }
139}
140
141bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
142 int srcX, int srcY) const {
143 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
144 info.profileType());
145 uint32_t flags = 0;
146 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
147 // let the GPU perform this transformation for us
148 flags = GrContext::kUnpremul_PixelOpsFlag;
149 }
150 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
151 pixels, rowBytes, flags)) {
152 return false;
153 }
154 // do we have to manually fix-up the alpha channel?
155 // src dst
156 // unpremul premul fix manually
157 // premul unpremul done by kUnpremul_PixelOpsFlag
158 // all other combos need to change.
159 //
160 // Should this be handled by Ganesh? todo:?
161 //
162 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
163 apply_premul(info, pixels, rowBytes);
164 }
165 return true;
166}
167
reed7b6945b2015-09-24 00:50:58 -0700168SkImage* SkImage_Gpu::onNewSubset(const SkIRect& subset) const {
169 GrContext* ctx = fTexture->getContext();
170 GrSurfaceDesc desc = fTexture->desc();
171 desc.fWidth = subset.width();
172 desc.fHeight = subset.height();
173
174 GrTexture* subTx = ctx->textureProvider()->createTexture(desc,
175 SkSurface::kYes_Budgeted == fBudgeted);
176 if (!subTx) {
177 return nullptr;
178 }
179 ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0));
180 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, fAlphaType, subTx,
181 fBudgeted);
182}
183
reed8b26b992015-05-07 15:36:17 -0700184///////////////////////////////////////////////////////////////////////////////////////////////////
185
bsalomon6dc6f5f2015-06-18 09:12:16 -0700186static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
reedde499882015-06-18 13:41:40 -0700187 SkAlphaType at, GrWrapOwnership ownership,
188 SkImage::TextureReleaseProc releaseProc,
189 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700190 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700192 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700193 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700194 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700196 }
reedde499882015-06-18 13:41:40 -0700197 if (releaseProc) {
198 tex->setRelease(releaseProc, releaseCtx);
199 }
200
reed8b26b992015-05-07 15:36:17 -0700201 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
reed7b6945b2015-09-24 00:50:58 -0700202 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700203}
204
reedde499882015-06-18 13:41:40 -0700205SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at,
206 TextureReleaseProc releaseP, ReleaseContext releaseC) {
207 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700208}
209
210SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
211 SkAlphaType at) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700213}
214
reed56179002015-07-07 06:11:19 -0700215SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc,
reed8b26b992015-05-07 15:36:17 -0700216 SkAlphaType at) {
reed56179002015-07-07 06:11:19 -0700217 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700218 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700219 }
reed56179002015-07-07 06:11:19 -0700220
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
reed56179002015-07-07 06:11:19 -0700222 desc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700223 if (!src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700224 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700225 }
226
reed56179002015-07-07 06:11:19 -0700227 const bool isBudgeted = true;
228 SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted));
reed8b26b992015-05-07 15:36:17 -0700229 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700230 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700231 }
232
reed56179002015-07-07 06:11:19 -0700233 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
reed7b6945b2015-09-24 00:50:58 -0700234 return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst,
halcanary385fe4d2015-08-26 13:07:48 -0700235 budgeted);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800236}
bsalomon993a4212015-05-29 11:37:25 -0700237
238SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
239 const GrBackendObject yuvTextureHandles[3],
240 const SkISize yuvSizes[3],
241 GrSurfaceOrigin origin) {
242 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
243
244 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
245 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
246 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700247 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700248 }
249 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
250 GrBackendTextureDesc yDesc;
251 yDesc.fConfig = kConfig;
252 yDesc.fOrigin = origin;
253 yDesc.fSampleCnt = 0;
254 yDesc.fTextureHandle = yuvTextureHandles[0];
255 yDesc.fWidth = yuvSizes[0].fWidth;
256 yDesc.fHeight = yuvSizes[0].fHeight;
257
258 GrBackendTextureDesc uDesc;
259 uDesc.fConfig = kConfig;
260 uDesc.fOrigin = origin;
261 uDesc.fSampleCnt = 0;
262 uDesc.fTextureHandle = yuvTextureHandles[1];
263 uDesc.fWidth = yuvSizes[1].fWidth;
264 uDesc.fHeight = yuvSizes[1].fHeight;
265
266 GrBackendTextureDesc vDesc;
267 vDesc.fConfig = kConfig;
268 vDesc.fOrigin = origin;
269 vDesc.fSampleCnt = 0;
270 vDesc.fTextureHandle = yuvTextureHandles[2];
271 vDesc.fWidth = yuvSizes[2].fWidth;
272 vDesc.fHeight = yuvSizes[2].fHeight;
273
bsalomon6dc6f5f2015-06-18 09:12:16 -0700274 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
275 yDesc, kBorrow_GrWrapOwnership));
276 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
277 uDesc, kBorrow_GrWrapOwnership));
278 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
279 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700280 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700282 }
283
284 GrSurfaceDesc dstDesc;
285 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
286 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
287 dstDesc.fOrigin = origin;
288 dstDesc.fWidth = yuvSizes[0].fWidth;
289 dstDesc.fHeight = yuvSizes[0].fHeight;
290 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
291 dstDesc.fSampleCnt = 0;
292
bsalomoneae62002015-07-31 13:59:30 -0700293 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true));
bsalomon993a4212015-05-29 11:37:25 -0700294 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700295 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700296 }
297
298 GrPaint paint;
299 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonac856c92015-08-27 06:30:17 -0700300 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(),
301 yTex, uTex, vTex, yuvSizes,
302 colorSpace))->unref();
bsalomon993a4212015-05-29 11:37:25 -0700303
304 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
305 SkIntToScalar(dstDesc.fHeight));
robertphillipsc9a37062015-09-01 08:34:28 -0700306 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext());
307 if (!drawContext) {
308 return nullptr;
309 }
310
robertphillips2334fb62015-06-17 05:43:33 -0700311 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700312 ctx->flushSurfaceWrites(dst);
halcanary385fe4d2015-08-26 13:07:48 -0700313 return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID,
reed7b6945b2015-09-24 00:50:58 -0700314 kOpaque_SkAlphaType, dst, budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700315}
reed56179002015-07-07 06:11:19 -0700316
317///////////////////////////////////////////////////////////////////////////////////////////////////
318
319GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) {
320 GrContext* ctx = src->getContext();
321
322 GrSurfaceDesc desc = src->desc();
halcanary96fcdcc2015-08-27 07:41:13 -0700323 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
reed56179002015-07-07 06:11:19 -0700324 if (!dst) {
halcanary96fcdcc2015-08-27 07:41:13 -0700325 return nullptr;
reed56179002015-07-07 06:11:19 -0700326 }
327
328 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
329 const SkIPoint dstP = SkIPoint::Make(0, 0);
330 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
331 return dst;
332}
333