commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkPictureShader.h" |
| 9 | |
Yuqian Li | 8d2fb47 | 2017-01-30 11:33:46 -0500 | [diff] [blame] | 10 | #include "SkArenaAlloc.h" |
robertphillips | 25a5b0d | 2015-09-28 09:32:50 -0700 | [diff] [blame] | 11 | #include "SkBitmap.h" |
| 12 | #include "SkBitmapProcShader.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 14 | #include "SkColorSpaceXformCanvas.h" |
fmalita | 1b46a57 | 2016-02-01 02:34:03 -0800 | [diff] [blame] | 15 | #include "SkImage.h" |
fmalita | c9a9ca9 | 2016-10-12 13:43:43 -0700 | [diff] [blame] | 16 | #include "SkImageShader.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 17 | #include "SkMatrixUtils.h" |
| 18 | #include "SkPicture.h" |
Matt Sarett | 9df70bb | 2017-02-14 13:50:43 -0500 | [diff] [blame] | 19 | #include "SkPictureImageGenerator.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 20 | #include "SkReadBuffer.h" |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 21 | #include "SkResourceCache.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 22 | |
| 23 | #if SK_SUPPORT_GPU |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 24 | #include "GrCaps.h" |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 25 | #include "GrColorSpaceInfo.h" |
| 26 | #include "GrContext.h" |
Mike Reed | 84dd857 | 2017-03-08 22:21:00 -0500 | [diff] [blame] | 27 | #include "GrFragmentProcessor.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | static unsigned gBitmapSkaderKeyNamespaceLabel; |
| 32 | |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 33 | struct BitmapShaderKey : public SkResourceCache::Key { |
| 34 | public: |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 35 | BitmapShaderKey(sk_sp<SkColorSpace> colorSpace, |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 36 | uint32_t shaderID, |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 37 | const SkRect& tile, |
| 38 | SkShader::TileMode tmx, |
| 39 | SkShader::TileMode tmy, |
| 40 | const SkSize& scale, |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 41 | const SkMatrix& localMatrix, |
| 42 | SkTransferFunctionBehavior blendBehavior) |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 43 | : fColorSpace(std::move(colorSpace)) |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 44 | , fTile(tile) |
| 45 | , fTmx(tmx) |
| 46 | , fTmy(tmy) |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 47 | , fScale(scale) |
| 48 | , fBlendBehavior(blendBehavior) { |
fmalita | 04b49c3 | 2014-12-10 13:01:43 -0800 | [diff] [blame] | 49 | |
| 50 | for (int i = 0; i < 9; ++i) { |
| 51 | fLocalMatrixStorage[i] = localMatrix[i]; |
| 52 | } |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 53 | |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 54 | static const size_t keySize = sizeof(fColorSpace) + |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 55 | sizeof(fTile) + |
| 56 | sizeof(fTmx) + sizeof(fTmy) + |
| 57 | sizeof(fScale) + |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 58 | sizeof(fLocalMatrixStorage) + |
| 59 | sizeof(fBlendBehavior); |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 60 | // This better be packed. |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 61 | SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - (uint32_t*)&fColorSpace) == keySize); |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 62 | this->init(&gBitmapSkaderKeyNamespaceLabel, MakeSharedID(shaderID), keySize); |
| 63 | } |
| 64 | |
| 65 | static uint64_t MakeSharedID(uint32_t shaderID) { |
| 66 | uint64_t sharedID = SkSetFourByteTag('p', 's', 'd', 'r'); |
| 67 | return (sharedID << 32) | shaderID; |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | private: |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 71 | // TODO: there are some fishy things about using CS sk_sps in the key: |
| 72 | // - false negatives: keys are memcmp'ed, so we don't detect equivalent CSs |
| 73 | // (SkColorspace::Equals) |
| 74 | // - we're keeping the CS alive, even when the client releases it |
| 75 | // |
| 76 | // Ideally we'd be using unique IDs or some other weak ref + purge mechanism |
| 77 | // when the CS is deleted. |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 78 | sk_sp<SkColorSpace> fColorSpace; |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 79 | SkRect fTile; |
| 80 | SkShader::TileMode fTmx, fTmy; |
| 81 | SkSize fScale; |
| 82 | SkScalar fLocalMatrixStorage[9]; |
| 83 | SkTransferFunctionBehavior fBlendBehavior; |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 84 | |
| 85 | SkDEBUGCODE(uint32_t fEndOfStruct;) |
| 86 | }; |
| 87 | |
| 88 | struct BitmapShaderRec : public SkResourceCache::Rec { |
fmalita | c9a9ca9 | 2016-10-12 13:43:43 -0700 | [diff] [blame] | 89 | BitmapShaderRec(const BitmapShaderKey& key, SkShader* tileShader) |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 90 | : fKey(key) |
fmalita | c9a9ca9 | 2016-10-12 13:43:43 -0700 | [diff] [blame] | 91 | , fShader(SkRef(tileShader)) {} |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 92 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 93 | BitmapShaderKey fKey; |
| 94 | sk_sp<SkShader> fShader; |
| 95 | size_t fBitmapBytes; |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 96 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | const Key& getKey() const override { return fKey; } |
| 98 | size_t bytesUsed() const override { |
fmalita | c9a9ca9 | 2016-10-12 13:43:43 -0700 | [diff] [blame] | 99 | // Just the record overhead -- the actual pixels are accounted by SkImageCacherator. |
| 100 | return sizeof(fKey) + sizeof(SkImageShader); |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 101 | } |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 102 | const char* getCategory() const override { return "bitmap-shader"; } |
| 103 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; } |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 104 | |
| 105 | static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextShader) { |
| 106 | const BitmapShaderRec& rec = static_cast<const BitmapShaderRec&>(baseRec); |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 107 | sk_sp<SkShader>* result = reinterpret_cast<sk_sp<SkShader>*>(contextShader); |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 108 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 109 | *result = rec.fShader; |
fmalita | 387a01a | 2014-12-10 12:17:58 -0800 | [diff] [blame] | 110 | |
fmalita | ddc4b46 | 2015-08-31 19:54:03 -0700 | [diff] [blame] | 111 | // The bitmap shader is backed by an image generator, thus it can always re-generate its |
| 112 | // pixels if discarded. |
| 113 | return true; |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 117 | static int32_t gNextID = 1; |
| 118 | uint32_t next_id() { |
| 119 | int32_t id; |
| 120 | do { |
| 121 | id = sk_atomic_inc(&gNextID); |
| 122 | } while (id == SK_InvalidGenID); |
| 123 | return static_cast<uint32_t>(id); |
| 124 | } |
| 125 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 126 | } // namespace |
| 127 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 128 | SkPictureShader::SkPictureShader(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy, |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 129 | const SkMatrix* localMatrix, const SkRect* tile, |
| 130 | sk_sp<SkColorSpace> colorSpace) |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 131 | : INHERITED(localMatrix) |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 132 | , fPicture(std::move(picture)) |
| 133 | , fTile(tile ? *tile : fPicture->cullRect()) |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 134 | , fTmx(tmx) |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 135 | , fTmy(tmy) |
| 136 | , fColorSpace(std::move(colorSpace)) |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 137 | , fUniqueID(next_id()) |
| 138 | , fAddedToCache(false) {} |
| 139 | |
| 140 | SkPictureShader::~SkPictureShader() { |
| 141 | if (fAddedToCache.load()) { |
| 142 | SkResourceCache::PostPurgeSharedID(BitmapShaderKey::MakeSharedID(fUniqueID)); |
| 143 | } |
| 144 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 145 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 146 | sk_sp<SkShader> SkPictureShader::Make(sk_sp<SkPicture> picture, TileMode tmx, TileMode tmy, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 147 | const SkMatrix* localMatrix, const SkRect* tile) { |
Robert Phillips | 54be5c9 | 2017-02-11 01:19:30 +0000 | [diff] [blame] | 148 | if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 149 | return SkShader::MakeEmptyShader(); |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 150 | } |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 151 | return sk_sp<SkShader>(new SkPictureShader(std::move(picture), tmx, tmy, localMatrix, tile, |
| 152 | nullptr)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 153 | } |
| 154 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 155 | sk_sp<SkFlattenable> SkPictureShader::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 156 | SkMatrix lm; |
| 157 | buffer.readMatrix(&lm); |
| 158 | TileMode mx = (TileMode)buffer.read32(); |
| 159 | TileMode my = (TileMode)buffer.read32(); |
| 160 | SkRect tile; |
| 161 | buffer.readRect(&tile); |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 162 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 163 | sk_sp<SkPicture> picture; |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 164 | |
| 165 | if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) { |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 166 | // Newer code won't serialize pictures in disallow-cross-process-picture mode. |
| 167 | // Assert that they didn't serialize anything except a false here. |
| 168 | buffer.validate(!buffer.readBool()); |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 169 | } else { |
Mike Reed | 70bc94f | 2017-06-08 12:45:52 -0400 | [diff] [blame] | 170 | bool didSerialize = buffer.readBool(); |
| 171 | if (didSerialize) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 172 | picture = SkPicture::MakeFromBuffer(buffer); |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 175 | return SkPictureShader::Make(picture, mx, my, &lm, &tile); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 176 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 177 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 178 | void SkPictureShader::flatten(SkWriteBuffer& buffer) const { |
| 179 | buffer.writeMatrix(this->getLocalMatrix()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 180 | buffer.write32(fTmx); |
| 181 | buffer.write32(fTmy); |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 182 | buffer.writeRect(fTile); |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 183 | |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 184 | // The deserialization code won't trust that our serialized picture is safe to deserialize. |
| 185 | // So write a 'false' telling it that we're not serializing a picture. |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 186 | if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) { |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 187 | buffer.writeBool(false); |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 188 | } else { |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 189 | buffer.writeBool(true); |
| 190 | fPicture->flatten(buffer); |
| 191 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 194 | sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, const SkMatrix* localM, |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 195 | SkColorSpace* dstColorSpace, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 196 | const int maxTextureSize) const { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 197 | SkASSERT(fPicture && !fPicture->cullRect().isEmpty()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 198 | |
| 199 | SkMatrix m; |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 200 | m.setConcat(viewMatrix, this->getLocalMatrix()); |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 201 | if (localM) { |
| 202 | m.preConcat(*localM); |
| 203 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 204 | |
| 205 | // Use a rotation-invariant scale |
| 206 | SkPoint scale; |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 207 | // |
| 208 | // TODO: replace this with decomposeScale() -- but beware LayoutTest rebaselines! |
| 209 | // |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 210 | if (!SkDecomposeUpper2x2(m, nullptr, &scale, nullptr)) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 211 | // Decomposition failed, use an approximation. |
| 212 | scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()), |
| 213 | SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY())); |
| 214 | } |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 215 | SkSize scaledSize = SkSize::Make(SkScalarAbs(scale.x() * fTile.width()), |
| 216 | SkScalarAbs(scale.y() * fTile.height())); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 217 | |
fmalita | 511005b | 2015-03-13 06:50:44 -0700 | [diff] [blame] | 218 | // Clamp the tile size to about 4M pixels |
| 219 | static const SkScalar kMaxTileArea = 2048 * 2048; |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 220 | SkScalar tileArea = scaledSize.width() * scaledSize.height(); |
fmalita | bb204f4 | 2014-08-07 08:39:24 -0700 | [diff] [blame] | 221 | if (tileArea > kMaxTileArea) { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 222 | SkScalar clampScale = SkScalarSqrt(kMaxTileArea / tileArea); |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 223 | scaledSize.set(scaledSize.width() * clampScale, |
| 224 | scaledSize.height() * clampScale); |
fmalita | bb204f4 | 2014-08-07 08:39:24 -0700 | [diff] [blame] | 225 | } |
gen.kim | b9ed884 | 2015-05-03 22:36:30 -0700 | [diff] [blame] | 226 | #if SK_SUPPORT_GPU |
| 227 | // Scale down the tile size if larger than maxTextureSize for GPU Path or it should fail on create texture |
| 228 | if (maxTextureSize) { |
| 229 | if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) { |
ericrk | f469fc0 | 2015-10-14 17:33:29 -0700 | [diff] [blame] | 230 | SkScalar downScale = maxTextureSize / SkMaxScalar(scaledSize.width(), scaledSize.height()); |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 231 | scaledSize.set(SkScalarFloorToScalar(scaledSize.width() * downScale), |
| 232 | SkScalarFloorToScalar(scaledSize.height() * downScale)); |
gen.kim | b9ed884 | 2015-05-03 22:36:30 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | #endif |
fmalita | bb204f4 | 2014-08-07 08:39:24 -0700 | [diff] [blame] | 236 | |
fmalita | 85c6f98 | 2016-02-29 09:18:31 -0800 | [diff] [blame] | 237 | const SkISize tileSize = scaledSize.toCeil(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 238 | if (tileSize.isEmpty()) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 239 | return SkShader::MakeEmptyShader(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 240 | } |
| 241 | |
fmalita | bb204f4 | 2014-08-07 08:39:24 -0700 | [diff] [blame] | 242 | // The actual scale, compensating for rounding & clamping. |
fmalita | 85c6f98 | 2016-02-29 09:18:31 -0800 | [diff] [blame] | 243 | const SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(), |
| 244 | SkIntToScalar(tileSize.height()) / fTile.height()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 245 | |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 246 | // |fColorSpace| will only be set when using an SkColorSpaceXformCanvas to do pre-draw xforms. |
| 247 | // This canvas is strictly for legacy mode. A non-null |dstColorSpace| indicates that we |
| 248 | // should perform color correct rendering and xform at draw time. |
| 249 | SkASSERT(!fColorSpace || !dstColorSpace); |
| 250 | sk_sp<SkColorSpace> keyCS = dstColorSpace ? sk_ref_sp(dstColorSpace) : fColorSpace; |
| 251 | SkTransferFunctionBehavior blendBehavior = dstColorSpace ? SkTransferFunctionBehavior::kRespect |
| 252 | : SkTransferFunctionBehavior::kIgnore; |
| 253 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 254 | sk_sp<SkShader> tileShader; |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 255 | BitmapShaderKey key(std::move(keyCS), |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 256 | fUniqueID, |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 257 | fTile, |
| 258 | fTmx, |
| 259 | fTmy, |
| 260 | tileScale, |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 261 | this->getLocalMatrix(), |
| 262 | blendBehavior); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 263 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 264 | if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) { |
robertphillips | 25a5b0d | 2015-09-28 09:32:50 -0700 | [diff] [blame] | 265 | SkMatrix tileMatrix; |
| 266 | tileMatrix.setRectToRect(fTile, SkRect::MakeIWH(tileSize.width(), tileSize.height()), |
fmalita | 1b46a57 | 2016-02-01 02:34:03 -0800 | [diff] [blame] | 267 | SkMatrix::kFill_ScaleToFit); |
| 268 | |
Matt Sarett | 9df70bb | 2017-02-14 13:50:43 -0500 | [diff] [blame] | 269 | sk_sp<SkImage> tileImage = SkImage::MakeFromGenerator( |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 270 | SkPictureImageGenerator::Make(tileSize, fPicture, &tileMatrix, nullptr, |
| 271 | SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace))); |
fmalita | 1b46a57 | 2016-02-01 02:34:03 -0800 | [diff] [blame] | 272 | if (!tileImage) { |
fmalita | ddc4b46 | 2015-08-31 19:54:03 -0700 | [diff] [blame] | 273 | return nullptr; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 274 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 275 | |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 276 | if (fColorSpace) { |
| 277 | tileImage = tileImage->makeColorSpace(fColorSpace, SkTransferFunctionBehavior::kIgnore); |
| 278 | } |
| 279 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 280 | SkMatrix shaderMatrix = this->getLocalMatrix(); |
| 281 | shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 282 | tileShader = tileImage->makeShader(fTmx, fTmy, &shaderMatrix); |
fmalita | 23df2d6 | 2014-10-22 07:39:08 -0700 | [diff] [blame] | 283 | |
fmalita | c9a9ca9 | 2016-10-12 13:43:43 -0700 | [diff] [blame] | 284 | SkResourceCache::Add(new BitmapShaderRec(key, tileShader.get())); |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 285 | fAddedToCache.store(true); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 286 | } |
| 287 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 288 | return tileShader; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Mike Reed | 3404207 | 2017-08-08 16:29:22 -0400 | [diff] [blame] | 291 | bool SkPictureShader::onIsRasterPipelineOnly(const SkMatrix& ctm) const { |
| 292 | return SkImageShader::IsRasterPipelineOnly(ctm, kN32_SkColorType, kPremul_SkAlphaType, |
Mike Reed | eefe9f9 | 2017-08-21 11:39:15 -0400 | [diff] [blame] | 293 | fTmx, fTmy, this->getLocalMatrix()); |
Mike Reed | 980e279 | 2017-06-21 13:15:58 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 296 | bool SkPictureShader::onAppendStages(const StageRec& rec) const { |
Yuqian Li | 8d2fb47 | 2017-01-30 11:33:46 -0500 | [diff] [blame] | 297 | // Keep bitmapShader alive by using alloc instead of stack memory |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 298 | auto& bitmapShader = *rec.fAlloc->make<sk_sp<SkShader>>(); |
| 299 | bitmapShader = this->refBitmapShader(rec.fCTM, rec.fLocalM, rec.fDstCS); |
| 300 | return bitmapShader && as_SB(bitmapShader)->appendStages(rec); |
Yuqian Li | 8d2fb47 | 2017-01-30 11:33:46 -0500 | [diff] [blame] | 301 | } |
| 302 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 303 | ///////////////////////////////////////////////////////////////////////////////////////// |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 304 | SkShaderBase::Context* SkPictureShader::onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 305 | const { |
| 306 | sk_sp<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix, |
| 307 | rec.fDstColorSpace)); |
| 308 | if (!bitmapShader) { |
| 309 | return nullptr; |
| 310 | } |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 311 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 312 | PictureShaderContext* ctx = |
| 313 | alloc->make<PictureShaderContext>(*this, rec, std::move(bitmapShader), alloc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 314 | if (nullptr == ctx->fBitmapShaderContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 315 | ctx = nullptr; |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 316 | } |
| 317 | return ctx; |
| 318 | } |
| 319 | |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 320 | sk_sp<SkShader> SkPictureShader::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
Florin Malita | 604f0d5 | 2017-07-13 14:29:12 -0400 | [diff] [blame] | 321 | sk_sp<SkColorSpace> dstCS = xformer->dst(); |
| 322 | if (SkColorSpace::Equals(dstCS.get(), fColorSpace.get())) { |
| 323 | return sk_ref_sp(const_cast<SkPictureShader*>(this)); |
| 324 | } |
| 325 | |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 326 | return sk_sp<SkPictureShader>(new SkPictureShader(fPicture, fTmx, fTmy, &this->getLocalMatrix(), |
Florin Malita | 604f0d5 | 2017-07-13 14:29:12 -0400 | [diff] [blame] | 327 | &fTile, std::move(dstCS))); |
Matt Sarett | 28a7ad2 | 2017-04-21 15:12:34 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 330 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 331 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 332 | SkPictureShader::PictureShaderContext::PictureShaderContext( |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 333 | const SkPictureShader& shader, const ContextRec& rec, sk_sp<SkShader> bitmapShader, |
| 334 | SkArenaAlloc* alloc) |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 335 | : INHERITED(shader, rec) |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 336 | , fBitmapShader(std::move(bitmapShader)) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 337 | { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 338 | fBitmapShaderContext = as_SB(fBitmapShader)->makeContext(rec, alloc); |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 339 | //if fBitmapShaderContext is null, we are invalid |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 340 | } |
| 341 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 342 | uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 343 | SkASSERT(fBitmapShaderContext); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 344 | return fBitmapShaderContext->getFlags(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 345 | } |
| 346 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 347 | void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 348 | SkASSERT(fBitmapShaderContext); |
| 349 | fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
| 350 | } |
| 351 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 352 | #ifndef SK_IGNORE_TO_STRING |
| 353 | void SkPictureShader::toString(SkString* str) const { |
| 354 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 355 | "clamp", "repeat", "mirror" |
| 356 | }; |
| 357 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 358 | str->appendf("PictureShader: [%f:%f:%f:%f] ", |
fmalita | ddc4b46 | 2015-08-31 19:54:03 -0700 | [diff] [blame] | 359 | fPicture->cullRect().fLeft, |
| 360 | fPicture->cullRect().fTop, |
| 361 | fPicture->cullRect().fRight, |
| 362 | fPicture->cullRect().fBottom); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 363 | |
| 364 | str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| 365 | |
| 366 | this->INHERITED::toString(str); |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | #if SK_SUPPORT_GPU |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 371 | std::unique_ptr<GrFragmentProcessor> SkPictureShader::asFragmentProcessor( |
| 372 | const AsFPArgs& args) const { |
gen.kim | b9ed884 | 2015-05-03 22:36:30 -0700 | [diff] [blame] | 373 | int maxTextureSize = 0; |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 374 | if (args.fContext) { |
| 375 | maxTextureSize = args.fContext->caps()->maxTextureSize(); |
gen.kim | b9ed884 | 2015-05-03 22:36:30 -0700 | [diff] [blame] | 376 | } |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 377 | sk_sp<SkShader> bitmapShader(this->refBitmapShader(*args.fViewMatrix, args.fLocalMatrix, |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 378 | args.fDstColorSpaceInfo->colorSpace(), |
| 379 | maxTextureSize)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 380 | if (!bitmapShader) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 381 | return nullptr; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 382 | } |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 383 | return as_SB(bitmapShader) |
| 384 | ->asFragmentProcessor(SkShaderBase::AsFPArgs(args.fContext, args.fViewMatrix, nullptr, |
| 385 | args.fFilterQuality, |
| 386 | args.fDstColorSpaceInfo)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 387 | } |
| 388 | #endif |