blob: 8360361314ce4ea10863ba4c0fb9400b1208d468 [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[],
Jim Van Verth53275362018-11-09 15:42:35 -0500294 int numTextures, const SkYUVAIndex yuvaIndices[4],
295 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500296 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
297 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
298
299 // We need to make a copy of the input backend textures because we need to preserve the result
300 // of validate_backend_texture.
301 GrBackendTexture yuvaTexturesCopy[4];
302 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
303 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
304 if (!ctx->contextPriv().caps()->getYUVAConfigFromBackendTexture(
305 yuvaTexturesCopy[textureIndex],
306 &yuvaTexturesCopy[textureIndex].fConfig)) {
307 return false;
308 }
309 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
310
311 tempTextureProxies[textureIndex] =
312 proxyProvider->wrapBackendTexture(yuvaTexturesCopy[textureIndex], imageOrigin);
313 if (!tempTextureProxies[textureIndex]) {
314 return false;
315 }
Jim Van Verth53275362018-11-09 15:42:35 -0500316
317 // Check that each texture contains the channel data for the corresponding YUVA index
318 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
319 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
320 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
321 switch (yuvaIndices[yuvaIndex].fChannel) {
322 case SkColorChannel::kR:
323 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
324 return false;
325 }
326 break;
327 case SkColorChannel::kG:
328 case SkColorChannel::kB:
329 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
330 kAlpha_8_as_Red_GrPixelConfig == config) {
331 return false;
332 }
333 break;
334 case SkColorChannel::kA:
335 default:
336 if (kRGB_888_GrPixelConfig == config) {
337 return false;
338 }
339 break;
340 }
341 }
342 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500343 }
344
345 return true;
346}
347
348bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
349 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
350 const sk_sp<GrTextureProxy> proxies[4],
351 const SkYUVAIndex yuvaIndices[4]) {
352 SkASSERT(renderTargetContext);
353 if (!renderTargetContext->asSurfaceProxy()) {
354 return false;
355 }
356
357 GrPaint paint;
358 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
359
Jim Van Verth0e671942018-11-09 12:03:57 -0500360 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(proxies, yuvaIndices,
361 yuvColorSpace,
362 GrSamplerState::Filter::kNearest));
363
364 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
365
366 // DDL TODO: in the promise image version we must not flush here
367 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
368
369 return true;
370}
371
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400372/////////////////////////////////////////////////////////////////////////////////////////////////
373sk_sp<GrTexture> SkPromiseImageHelper::getTexture(GrResourceProvider* resourceProvider,
374 GrPixelConfig config) {
375 // Releases the promise helper if there are no outstanding hard refs. This means that we
376 // don't have any ReleaseProcs waiting to be called so we will need to do a fulfill.
377 if (fReleaseHelper && fReleaseHelper->weak_expired()) {
378 this->resetReleaseHelper();
379 }
380
381 sk_sp<GrTexture> tex;
382 if (!fReleaseHelper) {
383 fFulfillProc(fContext, &fBackendTex);
384 fBackendTex.fConfig = config;
385 if (!fBackendTex.isValid()) {
386 // Even though the GrBackendTexture is not valid, we must call the release
387 // proc to keep our contract of always calling Fulfill and Release in pairs.
388 fReleaseProc(fContext);
389 return sk_sp<GrTexture>();
390 }
391
392 tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership);
393 if (!tex) {
394 // Even though the GrBackendTexture is not valid, we must call the release
395 // proc to keep our contract of always calling Fulfill and Release in pairs.
396 fReleaseProc(fContext);
397 return sk_sp<GrTexture>();
398 }
399 fReleaseHelper = new SkPromiseReleaseProcHelper(fReleaseProc, fContext, fDoneHelper);
400 // Take a weak ref
401 fReleaseHelper->weak_ref();
402 } else {
403 SkASSERT(fBackendTex.isValid());
404 tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership);
405 if (!tex) {
406 // We weren't able to make a texture here, but since we are in this branch
407 // of the calls (promiseHelper.fReleaseHelper is valid) there is already a
408 // texture out there which will call the release proc so we don't need to
409 // call it here.
410 return sk_sp<GrTexture>();
411 }
412
413 SkAssertResult(fReleaseHelper->try_ref());
414 }
415 SkASSERT(tex);
416 // Pass the hard ref off to the texture
417 tex->setRelease(sk_sp<GrReleaseProcHelper>(fReleaseHelper));
418 return tex;
419}