reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkBitmapProcShader.h" |
| 9 | #include "SkBitmapProvider.h" |
| 10 | #include "SkImage_Base.h" |
| 11 | #include "SkImageShader.h" |
| 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
| 14 | |
| 15 | SkImageShader::SkImageShader(const SkImage* img, TileMode tmx, TileMode tmy, const SkMatrix* matrix) |
| 16 | : INHERITED(matrix) |
| 17 | , fImage(SkRef(img)) |
| 18 | , fTileModeX(tmx) |
| 19 | , fTileModeY(tmy) |
| 20 | {} |
| 21 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 22 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 23 | const TileMode tx = (TileMode)buffer.readUInt(); |
| 24 | const TileMode ty = (TileMode)buffer.readUInt(); |
| 25 | SkMatrix matrix; |
| 26 | buffer.readMatrix(&matrix); |
| 27 | SkAutoTUnref<SkImage> img(buffer.readImage()); |
| 28 | if (!img) { |
| 29 | return nullptr; |
| 30 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 31 | return SkImageShader::Make(img, tx, ty, &matrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
| 35 | buffer.writeUInt(fTileModeX); |
| 36 | buffer.writeUInt(fTileModeY); |
| 37 | buffer.writeMatrix(this->getLocalMatrix()); |
| 38 | buffer.writeImage(fImage); |
| 39 | } |
| 40 | |
| 41 | bool SkImageShader::isOpaque() const { |
| 42 | return fImage->isOpaque(); |
| 43 | } |
| 44 | |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 45 | size_t SkImageShader::onContextSize(const ContextRec& rec) const { |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 46 | return SkBitmapProcShader::ContextSize(rec, SkBitmapProvider(fImage).info()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | SkShader::Context* SkImageShader::onCreateContext(const ContextRec& rec, void* storage) const { |
| 50 | return SkBitmapProcShader::MakeContext(*this, fTileModeX, fTileModeY, |
| 51 | SkBitmapProvider(fImage), rec, storage); |
| 52 | } |
| 53 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 54 | sk_sp<SkShader> SkImageShader::Make(const SkImage* image, TileMode tx, TileMode ty, |
| 55 | const SkMatrix* localMatrix) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 56 | if (!image) { |
| 57 | return nullptr; |
| 58 | } |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 59 | return sk_sp<SkShader>(new SkImageShader(image, tx, ty, localMatrix)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #ifndef SK_IGNORE_TO_STRING |
| 63 | void SkImageShader::toString(SkString* str) const { |
| 64 | const char* gTileModeName[SkShader::kTileModeCount] = { |
| 65 | "clamp", "repeat", "mirror" |
| 66 | }; |
| 67 | |
| 68 | str->appendf("ImageShader: ((%s %s) ", gTileModeName[fTileModeX], gTileModeName[fTileModeY]); |
| 69 | fImage->toString(str); |
| 70 | this->INHERITED::toString(str); |
| 71 | str->append(")"); |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | #if SK_SUPPORT_GPU |
| 78 | |
| 79 | #include "GrTextureAccess.h" |
| 80 | #include "SkGr.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 81 | #include "SkGrPriv.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 82 | #include "effects/GrSimpleTextureEffect.h" |
| 83 | #include "effects/GrBicubicEffect.h" |
| 84 | #include "effects/GrSimpleTextureEffect.h" |
| 85 | |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame^] | 86 | sk_sp<GrFragmentProcessor> SkImageShader::asFragmentProcessor(const AsFPArgs& args) const { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 87 | SkMatrix matrix; |
| 88 | matrix.setIDiv(fImage->width(), fImage->height()); |
| 89 | |
| 90 | SkMatrix lmInverse; |
| 91 | if (!this->getLocalMatrix().invert(&lmInverse)) { |
| 92 | return nullptr; |
| 93 | } |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame^] | 94 | if (args.fLocalMatrix) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 95 | SkMatrix inv; |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame^] | 96 | if (!args.fLocalMatrix->invert(&inv)) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 97 | return nullptr; |
| 98 | } |
| 99 | lmInverse.postConcat(inv); |
| 100 | } |
| 101 | matrix.preConcat(lmInverse); |
| 102 | |
| 103 | SkShader::TileMode tm[] = { fTileModeX, fTileModeY }; |
| 104 | |
| 105 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 106 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 107 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 108 | // are provided by the caller. |
| 109 | bool doBicubic; |
| 110 | GrTextureParams::FilterMode textureFilterMode = |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame^] | 111 | GrSkFilterQualityToGrFilterMode(args.fFilterQuality, *args.fViewMatrix, this->getLocalMatrix(), |
| 112 | &doBicubic); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 113 | GrTextureParams params(tm, textureFilterMode); |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame^] | 114 | SkAutoTUnref<GrTexture> texture(as_IB(fImage)->asTextureRef(args.fContext, params, |
| 115 | args.fGammaTreatment)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 116 | if (!texture) { |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 120 | sk_sp<GrFragmentProcessor> inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 121 | if (doBicubic) { |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 122 | inner = GrBicubicEffect::Make(texture, nullptr, matrix, tm); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 123 | } else { |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 124 | inner = GrSimpleTextureEffect::Make(texture, nullptr, matrix, params); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 128 | return inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 129 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 130 | return sk_sp<GrFragmentProcessor>(GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner))); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #endif |