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" |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 10 | #include "SkColorShader.h" |
| 11 | #include "SkColorTable.h" |
| 12 | #include "SkEmptyShader.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 13 | #include "SkImage_Base.h" |
| 14 | #include "SkImageShader.h" |
| 15 | #include "SkReadBuffer.h" |
| 16 | #include "SkWriteBuffer.h" |
| 17 | |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 18 | SkImageShader::SkImageShader(sk_sp<SkImage> img, TileMode tmx, TileMode tmy, const SkMatrix* matrix) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 19 | : INHERITED(matrix) |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 20 | , fImage(std::move(img)) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 21 | , fTileModeX(tmx) |
| 22 | , fTileModeY(tmy) |
| 23 | {} |
| 24 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 25 | sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 26 | const TileMode tx = (TileMode)buffer.readUInt(); |
| 27 | const TileMode ty = (TileMode)buffer.readUInt(); |
| 28 | SkMatrix matrix; |
| 29 | buffer.readMatrix(&matrix); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 30 | sk_sp<SkImage> img = buffer.readImage(); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 31 | if (!img) { |
| 32 | return nullptr; |
| 33 | } |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 34 | return SkImageShader::Make(std::move(img), tx, ty, &matrix); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void SkImageShader::flatten(SkWriteBuffer& buffer) const { |
| 38 | buffer.writeUInt(fTileModeX); |
| 39 | buffer.writeUInt(fTileModeY); |
| 40 | buffer.writeMatrix(this->getLocalMatrix()); |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 41 | buffer.writeImage(fImage.get()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool SkImageShader::isOpaque() const { |
| 45 | return fImage->isOpaque(); |
| 46 | } |
| 47 | |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 48 | size_t SkImageShader::onContextSize(const ContextRec& rec) const { |
fmalita | dc87a7d | 2016-10-17 06:09:26 -0700 | [diff] [blame] | 49 | return SkBitmapProcLegacyShader::ContextSize(rec, as_IB(fImage)->onImageInfo()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | SkShader::Context* SkImageShader::onCreateContext(const ContextRec& rec, void* storage) const { |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 53 | return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 54 | SkBitmapProvider(fImage.get()), rec, storage); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 55 | } |
| 56 | |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 57 | SkImage* SkImageShader::onIsAImage(SkMatrix* texM, TileMode xy[]) const { |
| 58 | if (texM) { |
| 59 | *texM = this->getLocalMatrix(); |
| 60 | } |
| 61 | if (xy) { |
| 62 | xy[0] = (TileMode)fTileModeX; |
| 63 | xy[1] = (TileMode)fTileModeY; |
| 64 | } |
| 65 | return const_cast<SkImage*>(fImage.get()); |
| 66 | } |
| 67 | |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 68 | #ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 69 | bool SkImageShader::onIsABitmap(SkBitmap* texture, SkMatrix* texM, TileMode xy[]) const { |
| 70 | const SkBitmap* bm = as_IB(fImage)->onPeekBitmap(); |
| 71 | if (!bm) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (texture) { |
| 76 | *texture = *bm; |
| 77 | } |
| 78 | if (texM) { |
| 79 | *texM = this->getLocalMatrix(); |
| 80 | } |
| 81 | if (xy) { |
| 82 | xy[0] = (TileMode)fTileModeX; |
| 83 | xy[1] = (TileMode)fTileModeY; |
| 84 | } |
| 85 | return true; |
| 86 | } |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 87 | #endif |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 88 | |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 89 | static bool bitmap_is_too_big(int w, int h) { |
| 90 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it |
| 91 | // communicates between its matrix-proc and its sampler-proc. Until we can |
| 92 | // widen that, we have to reject bitmaps that are larger. |
| 93 | // |
| 94 | static const int kMaxSize = 65535; |
| 95 | |
| 96 | return w > kMaxSize || h > kMaxSize; |
| 97 | } |
| 98 | |
| 99 | // returns true and set color if the bitmap can be drawn as a single color |
| 100 | // (for efficiency) |
| 101 | static bool can_use_color_shader(const SkImage* image, SkColor* color) { |
| 102 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 103 | // HWUI does not support color shaders (see b/22390304) |
| 104 | return false; |
| 105 | #endif |
| 106 | |
| 107 | if (1 != image->width() || 1 != image->height()) { |
| 108 | return false; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 109 | } |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 110 | |
| 111 | SkPixmap pmap; |
| 112 | if (!image->peekPixels(&pmap)) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | switch (pmap.colorType()) { |
| 117 | case kN32_SkColorType: |
| 118 | *color = SkUnPreMultiply::PMColorToColor(*pmap.addr32(0, 0)); |
| 119 | return true; |
| 120 | case kRGB_565_SkColorType: |
| 121 | *color = SkPixel16ToColor(*pmap.addr16(0, 0)); |
| 122 | return true; |
| 123 | case kIndex_8_SkColorType: { |
| 124 | const SkColorTable& ctable = *pmap.ctable(); |
| 125 | *color = SkUnPreMultiply::PMColorToColor(ctable[*pmap.addr8(0, 0)]); |
| 126 | return true; |
| 127 | } |
| 128 | default: // just skip the other configs for now |
| 129 | break; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 134 | sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, TileMode tx, TileMode ty, |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 135 | const SkMatrix* localMatrix, |
| 136 | SkTBlitterAllocator* allocator) { |
| 137 | SkShader* shader; |
| 138 | SkColor color; |
| 139 | if (!image || bitmap_is_too_big(image->width(), image->height())) { |
| 140 | if (nullptr == allocator) { |
| 141 | shader = new SkEmptyShader; |
| 142 | } else { |
| 143 | shader = allocator->createT<SkEmptyShader>(); |
| 144 | } |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 145 | } else if (can_use_color_shader(image.get(), &color)) { |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 146 | if (nullptr == allocator) { |
| 147 | shader = new SkColorShader(color); |
| 148 | } else { |
| 149 | shader = allocator->createT<SkColorShader>(color); |
| 150 | } |
| 151 | } else { |
| 152 | if (nullptr == allocator) { |
| 153 | shader = new SkImageShader(image, tx, ty, localMatrix); |
| 154 | } else { |
| 155 | shader = allocator->createT<SkImageShader>(image, tx, ty, localMatrix); |
| 156 | } |
| 157 | } |
| 158 | return sk_sp<SkShader>(shader); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | #ifndef SK_IGNORE_TO_STRING |
| 162 | void SkImageShader::toString(SkString* str) const { |
| 163 | const char* gTileModeName[SkShader::kTileModeCount] = { |
| 164 | "clamp", "repeat", "mirror" |
| 165 | }; |
| 166 | |
| 167 | str->appendf("ImageShader: ((%s %s) ", gTileModeName[fTileModeX], gTileModeName[fTileModeY]); |
| 168 | fImage->toString(str); |
| 169 | this->INHERITED::toString(str); |
| 170 | str->append(")"); |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 175 | |
| 176 | #if SK_SUPPORT_GPU |
| 177 | |
| 178 | #include "GrTextureAccess.h" |
| 179 | #include "SkGr.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 180 | #include "SkGrPriv.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 181 | #include "effects/GrSimpleTextureEffect.h" |
| 182 | #include "effects/GrBicubicEffect.h" |
| 183 | #include "effects/GrSimpleTextureEffect.h" |
| 184 | |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 185 | sk_sp<GrFragmentProcessor> SkImageShader::asFragmentProcessor(const AsFPArgs& args) const { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 186 | SkMatrix matrix; |
| 187 | matrix.setIDiv(fImage->width(), fImage->height()); |
| 188 | |
| 189 | SkMatrix lmInverse; |
| 190 | if (!this->getLocalMatrix().invert(&lmInverse)) { |
| 191 | return nullptr; |
| 192 | } |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 193 | if (args.fLocalMatrix) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 194 | SkMatrix inv; |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 195 | if (!args.fLocalMatrix->invert(&inv)) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 196 | return nullptr; |
| 197 | } |
| 198 | lmInverse.postConcat(inv); |
| 199 | } |
| 200 | matrix.preConcat(lmInverse); |
| 201 | |
| 202 | SkShader::TileMode tm[] = { fTileModeX, fTileModeY }; |
| 203 | |
| 204 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 205 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 206 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 207 | // are provided by the caller. |
| 208 | bool doBicubic; |
| 209 | GrTextureParams::FilterMode textureFilterMode = |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 210 | GrSkFilterQualityToGrFilterMode(args.fFilterQuality, *args.fViewMatrix, this->getLocalMatrix(), |
| 211 | &doBicubic); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 212 | GrTextureParams params(tm, textureFilterMode); |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame^] | 213 | sk_sp<GrTexture> texture(as_IB(fImage)->asTextureRef(args.fContext, params, |
| 214 | args.fGammaTreatment)); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 215 | if (!texture) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 219 | SkImageInfo info = as_IB(fImage)->onImageInfo(); |
| 220 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(info.colorSpace(), |
brianosman | 5192475 | 2016-09-12 08:50:19 -0700 | [diff] [blame] | 221 | args.fDstColorSpace); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 222 | sk_sp<GrFragmentProcessor> inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 223 | if (doBicubic) { |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame^] | 224 | inner = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), matrix, tm); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 225 | } else { |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame^] | 226 | inner = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), |
| 227 | matrix, params); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 231 | return inner; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 232 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 233 | return sk_sp<GrFragmentProcessor>(GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner))); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | #endif |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 237 | |
| 238 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 239 | #include "SkImagePriv.h" |
| 240 | |
| 241 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkShader::TileMode tmx, |
| 242 | SkShader::TileMode tmy, const SkMatrix* localMatrix, |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 243 | SkCopyPixelsMode cpm, SkTBlitterAllocator* allocator) { |
| 244 | // Until we learn otherwise, it seems that any caller that is passing an allocator must be |
| 245 | // assuming that the returned shader will have a stack-frame lifetime, so we assert that |
| 246 | // they are also asking for kNever_SkCopyPixelsMode. If that proves otherwise, we can remove |
| 247 | // or modify this assert. |
| 248 | SkASSERT(!allocator || (kNever_SkCopyPixelsMode == cpm)); |
| 249 | |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 250 | return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm, allocator), |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 251 | tmx, tmy, localMatrix, allocator); |
| 252 | } |
| 253 | |
| 254 | static sk_sp<SkFlattenable> SkBitmapProcShader_CreateProc(SkReadBuffer& buffer) { |
| 255 | SkMatrix lm; |
| 256 | buffer.readMatrix(&lm); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 257 | sk_sp<SkImage> image = buffer.readBitmapAsImage(); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 258 | SkShader::TileMode mx = (SkShader::TileMode)buffer.readUInt(); |
| 259 | SkShader::TileMode my = (SkShader::TileMode)buffer.readUInt(); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 260 | return image ? image->makeShader(mx, my, &lm) : nullptr; |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShader) |
| 264 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkImageShader) |
| 265 | SkFlattenable::Register("SkBitmapProcShader", SkBitmapProcShader_CreateProc, kSkShader_Type); |
| 266 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 267 | |