blob: 973f4ba3041497ec60e89f325bcf4ecd1e2aee91 [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
bsalomoneaaaf0b2015-01-23 08:08:04 -08008#include "SkImage_Gpu.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +00009#include "GrContext.h"
bsalomon993a4212015-05-29 11:37:25 -070010#include "GrDrawContext.h"
11#include "effects/GrYUVtoRGBEffect.h"
12#include "SkCanvas.h"
reed8b26b992015-05-07 15:36:17 -070013#include "SkGpuDevice.h"
reed@google.com5d4ba882012-07-31 15:45:27 +000014
bsalomon993a4212015-05-29 11:37:25 -070015
reed8b26b992015-05-07 15:36:17 -070016SkImage_Gpu::SkImage_Gpu(int w, int h, SkAlphaType at, GrTexture* tex,
17 int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted)
18 : INHERITED(w, h, NULL)
19 , fTexture(SkRef(tex))
reed3f10b9d2014-11-21 10:27:53 -080020 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces)
reed8b26b992015-05-07 15:36:17 -070021 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080022 , fBudgeted(budgeted)
reed8b26b992015-05-07 15:36:17 -070023 {}
piotaixrcef04f82014-07-14 07:48:04 -070024
reed4af267b2014-11-21 08:46:37 -080025SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
reed8b26b992015-05-07 15:36:17 -070026 GrTexture* tex = this->getTexture();
27 SkASSERT(tex);
28 GrContext* ctx = tex->getContext();
29 if (!ctx) {
30 // the texture may have been abandoned, so we have to check
robertphillips@google.com97b6b072012-10-31 14:48:39 +000031 return NULL;
32 }
reed8b26b992015-05-07 15:36:17 -070033 // TODO: Change signature of onNewSurface to take a budgeted param.
34 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
35 return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSurfaces, &props);
robertphillips@google.com97b6b072012-10-31 14:48:39 +000036}
reed4af267b2014-11-21 08:46:37 -080037
bsalomoneaaaf0b2015-01-23 08:08:04 -080038extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
bsalomon55812362015-06-10 08:49:28 -070039 if (as_IB(image)->getTexture()) {
reed8b26b992015-05-07 15:36:17 -070040 ((SkImage_Gpu*)image)->applyBudgetDecision();
41 }
42}
43
44SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
45 const SkMatrix* localMatrix) const {
46 SkBitmap bm;
47 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &bm);
48 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix);
49}
50
51bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
52 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
53 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) {
54 return false;
55 }
56 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
57 dst->getPixels(), dst->rowBytes())) {
58 return false;
59 }
60 return true;
61}
62
63bool SkImage_Gpu::isOpaque() const {
64 return GrPixelConfigIsOpaque(fTexture->config());
65}
66
67static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
68 switch (info.colorType()) {
69 case kRGBA_8888_SkColorType:
70 case kBGRA_8888_SkColorType:
71 break;
72 default:
73 return; // nothing to do
74 }
75
76 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
77 // and in either case, the alpha-byte is always in the same place, so we can safely call
78 // SkPreMultiplyColor()
79 //
80 SkColor* row = (SkColor*)pixels;
81 for (int y = 0; y < info.height(); ++y) {
82 for (int x = 0; x < info.width(); ++x) {
83 row[x] = SkPreMultiplyColor(row[x]);
84 }
85 }
86}
87
88bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
89 int srcX, int srcY) const {
90 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
91 info.profileType());
92 uint32_t flags = 0;
93 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
94 // let the GPU perform this transformation for us
95 flags = GrContext::kUnpremul_PixelOpsFlag;
96 }
97 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
98 pixels, rowBytes, flags)) {
99 return false;
100 }
101 // do we have to manually fix-up the alpha channel?
102 // src dst
103 // unpremul premul fix manually
104 // premul unpremul done by kUnpremul_PixelOpsFlag
105 // all other combos need to change.
106 //
107 // Should this be handled by Ganesh? todo:?
108 //
109 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
110 apply_premul(info, pixels, rowBytes);
111 }
112 return true;
113}
114
115///////////////////////////////////////////////////////////////////////////////////////////////////
116
bsalomon6dc6f5f2015-06-18 09:12:16 -0700117static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
reedde499882015-06-18 13:41:40 -0700118 SkAlphaType at, GrWrapOwnership ownership,
119 SkImage::TextureReleaseProc releaseProc,
120 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700121 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
122 return NULL;
123 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700124 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership));
reed8b26b992015-05-07 15:36:17 -0700125 if (!tex) {
126 return NULL;
127 }
reedde499882015-06-18 13:41:40 -0700128 if (releaseProc) {
129 tex->setRelease(releaseProc, releaseCtx);
130 }
131
reed8b26b992015-05-07 15:36:17 -0700132 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
133 return SkNEW_ARGS(SkImage_Gpu, (desc.fWidth, desc.fHeight, at, tex, 0, budgeted));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700134
135}
136
reedde499882015-06-18 13:41:40 -0700137SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at,
138 TextureReleaseProc releaseP, ReleaseContext releaseC) {
139 return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700140}
141
142SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
143 SkAlphaType at) {
reedde499882015-06-18 13:41:40 -0700144 return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, NULL, NULL);
reed8b26b992015-05-07 15:36:17 -0700145}
146
147SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& srcDesc,
148 SkAlphaType at) {
149 const bool isBudgeted = true;
150 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
151
152 if (srcDesc.fWidth <= 0 || srcDesc.fHeight <= 0) {
153 return NULL;
154 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700155 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(
156 srcDesc, kBorrow_GrWrapOwnership));
reed8b26b992015-05-07 15:36:17 -0700157 if (!src) {
158 return NULL;
159 }
160
161 GrSurfaceDesc dstDesc;
162 // need to be a rendertarget for readpixels to work, instead of kNone_GrSurfaceFlags
163 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
164 dstDesc.fOrigin = srcDesc.fOrigin;
165 dstDesc.fWidth = srcDesc.fWidth;
166 dstDesc.fHeight = srcDesc.fHeight;
167 dstDesc.fConfig = srcDesc.fConfig;
168 dstDesc.fSampleCnt = srcDesc.fSampleCnt;
169
170 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(
171 dstDesc, isBudgeted, NULL, 0));
172 if (!dst) {
173 return NULL;
174 }
175
176 const SkIRect srcR = SkIRect::MakeWH(dstDesc.fWidth, dstDesc.fHeight);
177 const SkIPoint dstP = SkIPoint::Make(0, 0);
178 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
179
180 const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()?
181 return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, at, dst, sampleCount,
182 budgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800183}
bsalomon993a4212015-05-29 11:37:25 -0700184
185SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace,
186 const GrBackendObject yuvTextureHandles[3],
187 const SkISize yuvSizes[3],
188 GrSurfaceOrigin origin) {
189 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
190
191 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
192 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 ||
193 yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) {
194 return NULL;
195 }
196 static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig;
197 GrBackendTextureDesc yDesc;
198 yDesc.fConfig = kConfig;
199 yDesc.fOrigin = origin;
200 yDesc.fSampleCnt = 0;
201 yDesc.fTextureHandle = yuvTextureHandles[0];
202 yDesc.fWidth = yuvSizes[0].fWidth;
203 yDesc.fHeight = yuvSizes[0].fHeight;
204
205 GrBackendTextureDesc uDesc;
206 uDesc.fConfig = kConfig;
207 uDesc.fOrigin = origin;
208 uDesc.fSampleCnt = 0;
209 uDesc.fTextureHandle = yuvTextureHandles[1];
210 uDesc.fWidth = yuvSizes[1].fWidth;
211 uDesc.fHeight = yuvSizes[1].fHeight;
212
213 GrBackendTextureDesc vDesc;
214 vDesc.fConfig = kConfig;
215 vDesc.fOrigin = origin;
216 vDesc.fSampleCnt = 0;
217 vDesc.fTextureHandle = yuvTextureHandles[2];
218 vDesc.fWidth = yuvSizes[2].fWidth;
219 vDesc.fHeight = yuvSizes[2].fHeight;
220
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221 SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture(
222 yDesc, kBorrow_GrWrapOwnership));
223 SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture(
224 uDesc, kBorrow_GrWrapOwnership));
225 SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture(
226 vDesc, kBorrow_GrWrapOwnership));
bsalomon993a4212015-05-29 11:37:25 -0700227 if (!yTex || !uTex || !vTex) {
228 return NULL;
229 }
230
231 GrSurfaceDesc dstDesc;
232 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
233 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
234 dstDesc.fOrigin = origin;
235 dstDesc.fWidth = yuvSizes[0].fWidth;
236 dstDesc.fHeight = yuvSizes[0].fHeight;
237 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
238 dstDesc.fSampleCnt = 0;
239
240 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->refScratchTexture(
241 dstDesc, GrTextureProvider::kExact_ScratchTexMatch));
242 if (!dst) {
243 return NULL;
244 }
245
246 GrPaint paint;
247 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
248 paint.addColorProcessor(GrYUVtoRGBEffect::Create(yTex, uTex, vTex, yuvSizes,
249 colorSpace))->unref();
250
251 const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth),
252 SkIntToScalar(dstDesc.fHeight));
robertphillips2334fb62015-06-17 05:43:33 -0700253 GrDrawContext* drawContext = ctx->drawContext();
254 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect);
bsalomon993a4212015-05-29 11:37:25 -0700255 ctx->flushSurfaceWrites(dst);
256 return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, kOpaque_SkAlphaType, dst, 0,
257 budgeted));
258}