blob: 332e7259e9bbea846abf9a6fb834105904a10265 [file] [log] [blame]
Jim Van Verth8026ccc2018-10-04 13:10:39 -04001/*
2 * Copyright 2018 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
8#include "GrBackendSurface.h"
9#include "GrClip.h"
10#include "GrContext.h"
11#include "GrContextPriv.h"
12#include "GrRenderTargetContext.h"
13#include "GrTexture.h"
14#include "GrTextureAdjuster.h"
Jim Van Verth0e671942018-11-09 12:03:57 -050015#include "effects/GrYUVtoRGBEffect.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040016#include "SkBitmapCache.h"
17#include "SkImage_Gpu.h"
18#include "SkImage_GpuBase.h"
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040019#include "SkReadPixelsRec.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040020
21SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID,
22 SkAlphaType at, SkBudgeted budgeted, sk_sp<SkColorSpace> cs)
23 : INHERITED(width, height, uniqueID)
24 , fContext(std::move(context))
25 , fAlphaType(at)
26 , fBudgeted(budgeted)
27 , fColorSpace(std::move(cs)) {}
28
29SkImage_GpuBase::~SkImage_GpuBase() {}
30
31//////////////////////////////////////////////////////////////////////////////////////////////////
32
33bool SkImage_GpuBase::ValidateBackendTexture(GrContext* ctx, const GrBackendTexture& tex,
34 GrPixelConfig* config, SkColorType ct, SkAlphaType at,
35 sk_sp<SkColorSpace> cs) {
36 if (!tex.isValid()) {
37 return false;
38 }
39 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
40 // create a fake image info here.
41 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
42 if (!SkImageInfoIsValid(info)) {
43 return false;
44 }
45
46 return ctx->contextPriv().caps()->validateBackendTexture(tex, ct, config);
47}
48
49//////////////////////////////////////////////////////////////////////////////////////////////////
50
Brian Osmane50cdf02018-10-19 13:02:14 -040051bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040052 if (!fContext->contextPriv().resourceProvider()) {
53 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
54 return false;
55 }
56
Jim Van Verth8026ccc2018-10-04 13:10:39 -040057 const auto desc = SkBitmapCacheDesc::Make(this);
58 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040059 SkASSERT(dst->isImmutable());
60 SkASSERT(dst->getPixels());
61 return true;
62 }
63
64 SkBitmapCache::RecPtr rec = nullptr;
65 SkPixmap pmap;
66 if (kAllow_CachingHint == chint) {
67 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
68 if (!rec) {
69 return false;
70 }
71 } else {
72 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
73 return false;
74 }
75 }
76
77 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
78 this->asTextureProxyRef(),
79 fColorSpace);
80 if (!sContext) {
81 return false;
82 }
83
84 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
85 return false;
86 }
87
88 if (rec) {
89 SkBitmapCache::Add(std::move(rec), dst);
90 this->notifyAddedToRasterCache();
91 }
92 return true;
93}
94
95sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const {
96 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef();
97
98 GrSurfaceDesc desc;
99 desc.fWidth = subset.width();
100 desc.fHeight = subset.height();
101 desc.fConfig = proxy->config();
102
103 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Greg Daniel919c9e72018-11-09 10:31:55 -0500104 desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact, fBudgeted));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400105 if (!sContext) {
106 return nullptr;
107 }
108
109 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
110 return nullptr;
111 }
112
113 // MDB: this call is okay bc we know 'sContext' was kExact
114 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
115 fAlphaType, sContext->asTextureProxyRef(),
116 fColorSpace, fBudgeted);
117}
118
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400119static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
120 switch (info.colorType()) {
121 case kRGBA_8888_SkColorType:
122 case kBGRA_8888_SkColorType:
123 break;
124 default:
125 return; // nothing to do
126 }
127
128 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
129 // and in either case, the alpha-byte is always in the same place, so we can safely call
130 // SkPreMultiplyColor()
131 //
132 SkColor* row = (SkColor*)pixels;
133 for (int y = 0; y < info.height(); ++y) {
134 for (int x = 0; x < info.width(); ++x) {
135 row[x] = SkPreMultiplyColor(row[x]);
136 }
137 row = (SkColor*)((char*)(row)+rowBytes);
138 }
139}
140
141bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
142 int srcX, int srcY, CachingHint) const {
143 if (!fContext->contextPriv().resourceProvider()) {
144 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
145 return false;
146 }
147
148 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
149 return false;
150 }
151
152 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
153 if (!rec.trim(this->width(), this->height())) {
154 return false;
155 }
156
157 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
158 // GrRenderTargetContext::onReadPixels
159 uint32_t flags = 0;
160 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
161 // let the GPU perform this transformation for us
162 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
163 }
164
165 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
166 this->asTextureProxyRef(), fColorSpace);
167 if (!sContext) {
168 return false;
169 }
170
171 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
172 return false;
173 }
174
175 // do we have to manually fix-up the alpha channel?
176 // src dst
177 // unpremul premul fix manually
178 // premul unpremul done by kUnpremul_PixelOpsFlag
179 // all other combos need to change.
180 //
181 // Should this be handled by Ganesh? todo:?
182 //
183 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
184 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
185 }
186 return true;
187}
188
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400189sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrContext* context,
190 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400191 SkScalar scaleAdjust[2]) const {
192 if (context->uniqueID() != fContext->uniqueID()) {
193 SkASSERT(0);
194 return nullptr;
195 }
196
197 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType,
198 this->uniqueID(), fColorSpace.get());
Brian Osman6064e1c2018-10-19 14:27:54 -0400199 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400200}
201
202GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
203 GrSurfaceOrigin* origin) const {
204 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
205 SkASSERT(proxy);
206
207 if (!fContext->contextPriv().resourceProvider() && !proxy->isInstantiated()) {
208 // This image was created with a DDL context and cannot be instantiated.
209 return GrBackendTexture();
210}
211
212 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
213 return GrBackendTexture(); // invalid
214 }
215
216 GrTexture* texture = proxy->peekTexture();
217
218 if (texture) {
219 if (flushPendingGrContextIO) {
220 fContext->contextPriv().prepareSurfaceForExternalIO(proxy.get());
221 }
222 if (origin) {
223 *origin = proxy->origin();
224 }
225 return texture->getBackendTexture();
226 }
227 return GrBackendTexture(); // invalid
228}
229
230GrTexture* SkImage_GpuBase::onGetTexture() const {
231 GrTextureProxy* proxy = this->peekProxy();
232 if (!proxy) {
233 return nullptr;
234 }
235
236 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef();
237 if (!fContext->contextPriv().resourceProvider() && !proxyRef->isInstantiated()) {
238 // This image was created with a DDL context and cannot be instantiated.
239 return nullptr;
240 }
241
242 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
243 return nullptr;
244 }
245
246 return proxy->peekTexture();
247}
248
249sk_sp<SkImage> SkImage_GpuBase::onMakeColorSpace(sk_sp<SkColorSpace> target) const {
Brian Osmanbe686f02018-10-12 11:18:02 -0400250 auto xform = GrColorSpaceXformEffect::Make(fColorSpace.get(), fAlphaType,
251 target.get(), fAlphaType);
Brian Osmanb4ae4992018-10-18 11:16:14 -0400252 SkASSERT(xform);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400253
Brian Osman9c111352018-10-16 10:18:26 -0400254 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
255
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400256 sk_sp<GrRenderTargetContext> renderTargetContext(
Brian Osman9c111352018-10-16 10:18:26 -0400257 fContext->contextPriv().makeDeferredRenderTargetContextWithFallback(
Greg Daniel919c9e72018-11-09 10:31:55 -0500258 SkBackingFit::kExact, this->width(), this->height(), proxy->config(), nullptr));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400259 if (!renderTargetContext) {
260 return nullptr;
261 }
262
263 GrPaint paint;
264 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman9c111352018-10-16 10:18:26 -0400265 paint.addColorTextureProcessor(std::move(proxy), SkMatrix::I());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400266 paint.addColorFragmentProcessor(std::move(xform));
267
Brian Osmanb4ae4992018-10-18 11:16:14 -0400268 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
269 SkRect::MakeIWH(this->width(), this->height()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400270 if (!renderTargetContext->asTextureProxy()) {
271 return nullptr;
272 }
273
274 // MDB: this call is okay bc we know 'renderTargetContext' was exact
275 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
Brian Osmanbe686f02018-10-12 11:18:02 -0400276 fAlphaType, renderTargetContext->asTextureProxyRef(),
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400277 std::move(target), fBudgeted);
278}
279
280bool SkImage_GpuBase::onIsValid(GrContext* context) const {
281 // The base class has already checked that context isn't abandoned (if it's not nullptr)
282 if (fContext->abandoned()) {
283 return false;
284 }
285
286 if (context && context != fContext.get()) {
287 return false;
288 }
289
290 return true;
291}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400292
Jim Van Verth0e671942018-11-09 12:03:57 -0500293bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
294 int numTextures, GrSurfaceOrigin imageOrigin,
295 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
296 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
297
298 // We need to make a copy of the input backend textures because we need to preserve the result
299 // of validate_backend_texture.
300 GrBackendTexture yuvaTexturesCopy[4];
301 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
302 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
303 if (!ctx->contextPriv().caps()->getYUVAConfigFromBackendTexture(
304 yuvaTexturesCopy[textureIndex],
305 &yuvaTexturesCopy[textureIndex].fConfig)) {
306 return false;
307 }
308 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
309
310 tempTextureProxies[textureIndex] =
311 proxyProvider->wrapBackendTexture(yuvaTexturesCopy[textureIndex], imageOrigin);
312 if (!tempTextureProxies[textureIndex]) {
313 return false;
314 }
315 }
316
317 return true;
318}
319
320bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
321 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
322 const sk_sp<GrTextureProxy> proxies[4],
323 const SkYUVAIndex yuvaIndices[4]) {
324 SkASSERT(renderTargetContext);
325 if (!renderTargetContext->asSurfaceProxy()) {
326 return false;
327 }
328
329 GrPaint paint;
330 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
331
332 // TODO: modify the YUVtoRGBEffect to do premul if needed
333 // (e.g., if SkImage_GpuYUVA::fImageAlphaType is kPremul_AlphaType)
334 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(proxies, yuvaIndices,
335 yuvColorSpace,
336 GrSamplerState::Filter::kNearest));
337
338 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
339
340 // DDL TODO: in the promise image version we must not flush here
341 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
342
343 return true;
344}
345
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400346/////////////////////////////////////////////////////////////////////////////////////////////////
347sk_sp<GrTexture> SkPromiseImageHelper::getTexture(GrResourceProvider* resourceProvider,
348 GrPixelConfig config) {
349 // Releases the promise helper if there are no outstanding hard refs. This means that we
350 // don't have any ReleaseProcs waiting to be called so we will need to do a fulfill.
351 if (fReleaseHelper && fReleaseHelper->weak_expired()) {
352 this->resetReleaseHelper();
353 }
354
355 sk_sp<GrTexture> tex;
356 if (!fReleaseHelper) {
357 fFulfillProc(fContext, &fBackendTex);
358 fBackendTex.fConfig = config;
359 if (!fBackendTex.isValid()) {
360 // Even though the GrBackendTexture is not valid, we must call the release
361 // proc to keep our contract of always calling Fulfill and Release in pairs.
362 fReleaseProc(fContext);
363 return sk_sp<GrTexture>();
364 }
365
366 tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership);
367 if (!tex) {
368 // Even though the GrBackendTexture is not valid, we must call the release
369 // proc to keep our contract of always calling Fulfill and Release in pairs.
370 fReleaseProc(fContext);
371 return sk_sp<GrTexture>();
372 }
373 fReleaseHelper = new SkPromiseReleaseProcHelper(fReleaseProc, fContext, fDoneHelper);
374 // Take a weak ref
375 fReleaseHelper->weak_ref();
376 } else {
377 SkASSERT(fBackendTex.isValid());
378 tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership);
379 if (!tex) {
380 // We weren't able to make a texture here, but since we are in this branch
381 // of the calls (promiseHelper.fReleaseHelper is valid) there is already a
382 // texture out there which will call the release proc so we don't need to
383 // call it here.
384 return sk_sp<GrTexture>();
385 }
386
387 SkAssertResult(fReleaseHelper->try_ref());
388 }
389 SkASSERT(tex);
390 // Pass the hard ref off to the texture
391 tex->setRelease(sk_sp<GrReleaseProcHelper>(fReleaseHelper));
392 return tex;
393}