blob: 5a4f7e7a0ccf3f9c832ac44d1009dc1ec3544d30 [file] [log] [blame]
Brian Osman3b66ab62016-11-28 09:26:31 -05001/*
2 * Copyright 2016 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrImageTextureMaker.h"
Brian Salomone7499c72019-06-24 12:12:36 -04009
10#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonca6b2f42020-01-24 11:31:21 -050011#include "src/gpu/GrContextPriv.h"
Greg Daniel82c6b102020-01-21 10:33:22 -050012#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/SkGr.h"
14#include "src/gpu/effects/GrYUVtoRGBEffect.h"
15#include "src/image/SkImage_GpuYUVA.h"
16#include "src/image/SkImage_Lazy.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017
Greg Daniel82c6b102020-01-21 10:33:22 -050018static GrImageInfo get_image_info(GrRecordingContext* context, const SkImage* client) {
19 SkASSERT(client->isLazyGenerated());
20 const SkImage_Lazy* lazyImage = static_cast<const SkImage_Lazy*>(client);
21
22 GrColorType ct = lazyImage->colorTypeOfLockTextureProxy(context->priv().caps());
23
24 return {ct, client->alphaType(), client->refColorSpace(), client->dimensions()};
25}
26
Brian Salomonbc074a62020-03-18 10:06:13 -040027GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context,
28 const SkImage* client,
29 GrImageTexGenPolicy texGenPolicy)
Brian Salomon777e1462020-02-28 21:10:31 -050030 : INHERITED(context, get_image_info(context, client))
Brian Osmanbd659552018-09-11 10:03:19 -040031 , fImage(static_cast<const SkImage_Lazy*>(client))
Brian Salomonbc074a62020-03-18 10:06:13 -040032 , fTexGenPolicy(texGenPolicy) {
Brian Osmanbd659552018-09-11 10:03:19 -040033 SkASSERT(client->isLazyGenerated());
Brian Osman3b66ab62016-11-28 09:26:31 -050034}
35
Brian Salomonecbb0fb2020-02-28 18:07:32 -050036GrSurfaceProxyView GrImageTextureMaker::refOriginalTextureProxyView(GrMipMapped mipMapped) {
Brian Salomonbc074a62020-03-18 10:06:13 -040037 return fImage->lockTextureProxyView(this->context(), fTexGenPolicy, mipMapped);
Robert Phillips0c984a02017-03-16 07:51:56 -040038}
39
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040040/////////////////////////////////////////////////////////////////////////////////////////////////
41
Brian Salomon777e1462020-02-28 21:10:31 -050042GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client)
43 : INHERITED(context, client->imageInfo())
Brian Salomone7499c72019-06-24 12:12:36 -040044 , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040045 SkASSERT(as_IB(client)->isYUVA());
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040046}
47
Brian Salomonecbb0fb2020-02-28 18:07:32 -050048GrSurfaceProxyView GrYUVAImageTextureMaker::refOriginalTextureProxyView(GrMipMapped mipMapped) {
49 if (mipMapped == GrMipMapped::kYes) {
Greg Daniel37c127f2020-02-05 10:37:27 -050050 return fImage->refMippedView(this->context());
Jim Van Verth803a5022018-11-05 15:55:53 -050051 } else {
Greg Daniel37c127f2020-02-05 10:37:27 -050052 if (const GrSurfaceProxyView* view = fImage->view(this->context())) {
53 return *view;
54 } else {
55 return {};
56 }
Jim Van Verth7da46762018-11-05 14:24:23 -050057 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040058}
59
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040060std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
Brian Salomon777e1462020-02-28 21:10:31 -050061 const SkMatrix& textureMatrix,
62 const SkRect& constraintRect,
63 FilterConstraint filterConstraint,
64 bool coordsLimitedToConstraintRect,
65 GrSamplerState::WrapMode wrapX,
66 GrSamplerState::WrapMode wrapY,
67 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Salomon5575e3c2020-03-25 15:19:37 +000068 // Check simple cases to see if we need to fall back to flattening the image (or whether it's
69 // already been flattened.)
70 if (!filterOrNullForBicubic || fImage->fRGBView.proxy()) {
Brian Salomon777e1462020-02-28 21:10:31 -050071 return this->INHERITED::createFragmentProcessor(
72 textureMatrix, constraintRect, filterConstraint, coordsLimitedToConstraintRect,
73 wrapX, wrapY, filterOrNullForBicubic);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040074 }
75
Jim Van Verthf542cab2018-11-07 12:08:21 -050076 // Check to see if the client has given us pre-mipped textures or we can generate them
Michael Ludwiga6a84002019-04-12 15:03:02 -040077 // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested
Brian Salomon5575e3c2020-03-25 15:19:37 +000078 GrSamplerState::Filter filter = *filterOrNullForBicubic;
Robert Phillips8defcc12019-03-05 15:58:59 -050079 if (GrSamplerState::Filter::kMipMap == filter &&
Michael Ludwiga6a84002019-04-12 15:03:02 -040080 (filterConstraint == GrTextureProducer::kYes_FilterConstraint ||
81 !fImage->setupMipmapsForPlanes(this->context()))) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040082 filter = GrSamplerState::Filter::kBilerp;
83 }
84
Michael Ludwiga6a84002019-04-12 15:03:02 -040085 // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's
86 // per plane, which may be different, so respect the filterConstraint without any additional
87 // analysis.
88 const SkRect* domain = nullptr;
89 if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) {
90 domain = &constraintRect;
91 }
92
Brian Salomonca6b2f42020-01-24 11:31:21 -050093 const auto& caps = *fImage->context()->priv().caps();
Greg Danielc7672092020-02-06 14:32:54 -050094 auto fp = GrYUVtoRGBEffect::Make(fImage->fViews, fImage->fYUVAIndices, fImage->fYUVColorSpace,
Brian Salomon5575e3c2020-03-25 15:19:37 +000095 filter, caps, textureMatrix, domain);
Brian Salomon5ad6fd32019-03-21 15:30:08 -040096 if (fImage->fFromColorSpace) {
97 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
98 fImage->alphaType(), fImage->colorSpace());
Brian Osmane9560492019-02-05 17:00:03 -050099 }
100 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400101}