herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "SkLinearBitmapPipeline.h" |
| 9 | |
herb | ed54504 | 2016-02-18 13:55:02 -0800 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <cmath> |
| 12 | #include <limits> |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 13 | #include <tuple> |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 14 | |
herb | 490d30f | 2016-03-04 10:39:14 -0800 | [diff] [blame] | 15 | #include "SkLinearBitmapPipeline_core.h" |
| 16 | #include "SkLinearBitmapPipeline_matrix.h" |
| 17 | #include "SkLinearBitmapPipeline_tile.h" |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 18 | #include "SkLinearBitmapPipeline_sample.h" |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 19 | #include "SkNx.h" |
| 20 | #include "SkOpts.h" |
| 21 | #include "SkPM4f.h" |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 22 | |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 23 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 24 | // SkLinearBitmapPipeline::Stage |
| 25 | template<typename Base, size_t kSize, typename Next> |
| 26 | SkLinearBitmapPipeline::Stage<Base, kSize, Next>::~Stage() { |
| 27 | if (fIsInitialized) { |
| 28 | this->get()->~Base(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | template<typename Base, size_t kSize, typename Next> |
| 33 | template<typename Variant, typename... Args> |
| 34 | void SkLinearBitmapPipeline::Stage<Base, kSize, Next>::initStage(Next* next, Args&& ... args) { |
| 35 | SkASSERTF(sizeof(Variant) <= sizeof(fSpace), |
| 36 | "Size Variant: %d, Space: %d", sizeof(Variant), sizeof(fSpace)); |
| 37 | |
| 38 | new (&fSpace) Variant(next, std::forward<Args>(args)...); |
| 39 | fStageCloner = [this](Next* nextClone, void* addr) { |
| 40 | new (addr) Variant(nextClone, (const Variant&)*this->get()); |
| 41 | }; |
| 42 | fIsInitialized = true; |
| 43 | }; |
| 44 | |
| 45 | template<typename Base, size_t kSize, typename Next> |
| 46 | template<typename Variant, typename... Args> |
| 47 | void SkLinearBitmapPipeline::Stage<Base, kSize, Next>::initSink(Args&& ... args) { |
| 48 | SkASSERTF(sizeof(Variant) <= sizeof(fSpace), |
| 49 | "Size Variant: %d, Space: %d", sizeof(Variant), sizeof(fSpace)); |
| 50 | new (&fSpace) Variant(std::forward<Args>(args)...); |
| 51 | fIsInitialized = true; |
| 52 | }; |
| 53 | |
| 54 | template<typename Base, size_t kSize, typename Next> |
| 55 | template <typename To, typename From> |
| 56 | To* SkLinearBitmapPipeline::Stage<Base, kSize, Next>::getInterface() { |
| 57 | From* down = static_cast<From*>(this->get()); |
| 58 | return static_cast<To*>(down); |
| 59 | } |
| 60 | |
| 61 | template<typename Base, size_t kSize, typename Next> |
| 62 | Base* SkLinearBitmapPipeline::Stage<Base, kSize, Next>::cloneStageTo( |
| 63 | Next* next, Stage* cloneToStage) const |
| 64 | { |
| 65 | if (!fIsInitialized) return nullptr; |
| 66 | fStageCloner(next, &cloneToStage->fSpace); |
| 67 | return cloneToStage->get(); |
| 68 | } |
| 69 | |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 70 | namespace { |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 71 | |
| 72 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 73 | // Matrix Stage |
herb | 933ad43 | 2016-02-22 13:13:28 -0800 | [diff] [blame] | 74 | // PointProcessor uses a strategy to help complete the work of the different stages. The strategy |
| 75 | // must implement the following methods: |
| 76 | // * processPoints(xs, ys) - must mutate the xs and ys for the stage. |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 77 | // * maybeProcessSpan(span, next) - This represents a horizontal series of pixels |
herb | 933ad43 | 2016-02-22 13:13:28 -0800 | [diff] [blame] | 78 | // to work over. |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 79 | // span - encapsulation of span. |
herb | 933ad43 | 2016-02-22 13:13:28 -0800 | [diff] [blame] | 80 | // next - a pointer to the next stage. |
| 81 | // maybeProcessSpan - returns false if it can not process the span and needs to fallback to |
| 82 | // point lists for processing. |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 83 | template<typename Strategy, typename Next> |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 84 | class MatrixStage final : public SkLinearBitmapPipeline::PointProcessorInterface { |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 85 | public: |
| 86 | template <typename... Args> |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 87 | MatrixStage(Next* next, Args&&... args) |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 88 | : fNext{next} |
| 89 | , fStrategy{std::forward<Args>(args)...}{ } |
| 90 | |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 91 | MatrixStage(Next* next, const MatrixStage& stage) |
| 92 | : fNext{next} |
| 93 | , fStrategy{stage.fStrategy} { } |
| 94 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 95 | void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override { |
mtklein | 8889347 | 2016-02-22 16:56:00 -0800 | [diff] [blame] | 96 | fStrategy.processPoints(&xs, &ys); |
| 97 | fNext->pointListFew(n, xs, ys); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 98 | } |
| 99 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 100 | void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override { |
mtklein | 8889347 | 2016-02-22 16:56:00 -0800 | [diff] [blame] | 101 | fStrategy.processPoints(&xs, &ys); |
| 102 | fNext->pointList4(xs, ys); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 103 | } |
| 104 | |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 105 | // The span you pass must not be empty. |
| 106 | void pointSpan(Span span) override { |
| 107 | SkASSERT(!span.isEmpty()); |
| 108 | if (!fStrategy.maybeProcessSpan(span, fNext)) { |
| 109 | span_fallback(span, this); |
herb | 933ad43 | 2016-02-22 13:13:28 -0800 | [diff] [blame] | 110 | } |
herb | 3eb4895 | 2016-02-19 14:39:47 -0800 | [diff] [blame] | 111 | } |
| 112 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 113 | private: |
| 114 | Next* const fNext; |
| 115 | Strategy fStrategy; |
| 116 | }; |
| 117 | |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 118 | template <typename Next = SkLinearBitmapPipeline::PointProcessorInterface> |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 119 | using TranslateMatrix = MatrixStage<TranslateMatrixStrategy, Next>; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 120 | |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 121 | template <typename Next = SkLinearBitmapPipeline::PointProcessorInterface> |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 122 | using ScaleMatrix = MatrixStage<ScaleMatrixStrategy, Next>; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 123 | |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 124 | template <typename Next = SkLinearBitmapPipeline::PointProcessorInterface> |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 125 | using AffineMatrix = MatrixStage<AffineMatrixStrategy, Next>; |
| 126 | |
| 127 | template <typename Next = SkLinearBitmapPipeline::PointProcessorInterface> |
| 128 | using PerspectiveMatrix = MatrixStage<PerspectiveMatrixStrategy, Next>; |
| 129 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 130 | |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 131 | static SkLinearBitmapPipeline::PointProcessorInterface* choose_matrix( |
| 132 | SkLinearBitmapPipeline::PointProcessorInterface* next, |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 133 | const SkMatrix& inverse, |
| 134 | SkLinearBitmapPipeline::MatrixStage* matrixProc) { |
| 135 | if (inverse.hasPerspective()) { |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 136 | matrixProc->initStage<PerspectiveMatrix<>>( |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 137 | next, |
| 138 | SkVector{inverse.getTranslateX(), inverse.getTranslateY()}, |
| 139 | SkVector{inverse.getScaleX(), inverse.getScaleY()}, |
| 140 | SkVector{inverse.getSkewX(), inverse.getSkewY()}, |
| 141 | SkVector{inverse.getPerspX(), inverse.getPerspY()}, |
| 142 | inverse.get(SkMatrix::kMPersp2)); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 143 | } else if (inverse.getSkewX() != 0.0f || inverse.getSkewY() != 0.0f) { |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 144 | matrixProc->initStage<AffineMatrix<>>( |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 145 | next, |
| 146 | SkVector{inverse.getTranslateX(), inverse.getTranslateY()}, |
| 147 | SkVector{inverse.getScaleX(), inverse.getScaleY()}, |
| 148 | SkVector{inverse.getSkewX(), inverse.getSkewY()}); |
| 149 | } else if (inverse.getScaleX() != 1.0f || inverse.getScaleY() != 1.0f) { |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 150 | matrixProc->initStage<ScaleMatrix<>>( |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 151 | next, |
| 152 | SkVector{inverse.getTranslateX(), inverse.getTranslateY()}, |
| 153 | SkVector{inverse.getScaleX(), inverse.getScaleY()}); |
| 154 | } else if (inverse.getTranslateX() != 0.0f || inverse.getTranslateY() != 0.0f) { |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 155 | matrixProc->initStage<TranslateMatrix<>>( |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 156 | next, |
| 157 | SkVector{inverse.getTranslateX(), inverse.getTranslateY()}); |
| 158 | } else { |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 159 | return next; |
| 160 | } |
| 161 | return matrixProc->get(); |
| 162 | } |
| 163 | |
herb | 490d30f | 2016-03-04 10:39:14 -0800 | [diff] [blame] | 164 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 165 | // Tile Stage |
| 166 | |
| 167 | template<typename XStrategy, typename YStrategy, typename Next> |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 168 | class CombinedTileStage final : public SkLinearBitmapPipeline::PointProcessorInterface { |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 169 | public: |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 170 | CombinedTileStage(Next* next, SkISize dimensions) |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 171 | : fNext{next} |
| 172 | , fXStrategy{dimensions.width()} |
| 173 | , fYStrategy{dimensions.height()}{ } |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 174 | |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 175 | CombinedTileStage(Next* next, const CombinedTileStage& stage) |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 176 | : fNext{next} |
| 177 | , fXStrategy{stage.fXStrategy} |
| 178 | , fYStrategy{stage.fYStrategy} { } |
| 179 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 180 | void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override { |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 181 | fXStrategy.tileXPoints(&xs); |
| 182 | fYStrategy.tileYPoints(&ys); |
| 183 | fNext->pointListFew(n, xs, ys); |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 184 | } |
| 185 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 186 | void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override { |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 187 | fXStrategy.tileXPoints(&xs); |
| 188 | fYStrategy.tileYPoints(&ys); |
| 189 | fNext->pointList4(xs, ys); |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 190 | } |
| 191 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 192 | // The span you pass must not be empty. |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 193 | void pointSpan(Span span) override { |
| 194 | SkASSERT(!span.isEmpty()); |
herb | 755539f | 2016-02-29 12:58:35 -0800 | [diff] [blame] | 195 | SkPoint start; SkScalar length; int count; |
| 196 | std::tie(start, length, count) = span; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 197 | |
| 198 | if (span.count() == 1) { |
| 199 | // DANGER: |
| 200 | // The explicit casts from float to Sk4f are not usually necessary, but are here to |
| 201 | // work around an MSVC 2015u2 c++ code generation bug. This is tracked using skia bug |
| 202 | // 5566. |
| 203 | this->pointListFew(1, Sk4f{span.startX()}, Sk4f{span.startY()}); |
| 204 | return; |
| 205 | } |
| 206 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 207 | SkScalar x = X(start); |
| 208 | SkScalar y = fYStrategy.tileY(Y(start)); |
| 209 | Span yAdjustedSpan{{x, y}, length, count}; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 210 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 211 | if (!fXStrategy.maybeProcessSpan(yAdjustedSpan, fNext)) { |
| 212 | span_fallback(span, this); |
| 213 | } |
herb | 3eb4895 | 2016-02-19 14:39:47 -0800 | [diff] [blame] | 214 | } |
| 215 | |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 216 | private: |
| 217 | Next* const fNext; |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 218 | XStrategy fXStrategy; |
| 219 | YStrategy fYStrategy; |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 220 | }; |
| 221 | |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 222 | template <typename XStrategy, typename Next> |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 223 | void choose_tiler_ymode( |
| 224 | SkShader::TileMode yMode, SkFilterQuality filterQuality, SkISize dimensions, |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 225 | Next* next, |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 226 | SkLinearBitmapPipeline::TileStage* tileStage) { |
| 227 | switch (yMode) { |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 228 | case SkShader::kClamp_TileMode: { |
| 229 | using Tiler = CombinedTileStage<XStrategy, YClampStrategy, Next>; |
| 230 | tileStage->initStage<Tiler>(next, dimensions); |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 231 | break; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 232 | } |
| 233 | case SkShader::kRepeat_TileMode: { |
| 234 | using Tiler = CombinedTileStage<XStrategy, YRepeatStrategy, Next>; |
| 235 | tileStage->initStage<Tiler>(next, dimensions); |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 236 | break; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 237 | } |
| 238 | case SkShader::kMirror_TileMode: { |
| 239 | using Tiler = CombinedTileStage<XStrategy, YMirrorStrategy, Next>; |
| 240 | tileStage->initStage<Tiler>(next, dimensions); |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 241 | break; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 242 | } |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 243 | } |
| 244 | }; |
| 245 | |
| 246 | static SkLinearBitmapPipeline::PointProcessorInterface* choose_tiler( |
herb | 308d988 | 2016-03-29 14:48:44 -0700 | [diff] [blame] | 247 | SkLinearBitmapPipeline::SampleProcessorInterface* next, |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 248 | SkISize dimensions, |
| 249 | SkShader::TileMode xMode, |
| 250 | SkShader::TileMode yMode, |
| 251 | SkFilterQuality filterQuality, |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 252 | SkScalar dx, |
| 253 | SkLinearBitmapPipeline::TileStage* tileStage) |
| 254 | { |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 255 | switch (xMode) { |
| 256 | case SkShader::kClamp_TileMode: |
| 257 | choose_tiler_ymode<XClampStrategy>(yMode, filterQuality, dimensions, next, tileStage); |
| 258 | break; |
| 259 | case SkShader::kRepeat_TileMode: |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 260 | if (dx == 1.0f && filterQuality == kNone_SkFilterQuality) { |
| 261 | choose_tiler_ymode<XRepeatUnitScaleStrategy>( |
| 262 | yMode, kNone_SkFilterQuality, dimensions, next, tileStage); |
| 263 | } else { |
| 264 | choose_tiler_ymode<XRepeatStrategy>( |
| 265 | yMode, filterQuality, dimensions, next, tileStage); |
| 266 | } |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 267 | break; |
| 268 | case SkShader::kMirror_TileMode: |
| 269 | choose_tiler_ymode<XMirrorStrategy>(yMode, filterQuality, dimensions, next, tileStage); |
| 270 | break; |
herb | 7dd5db4 | 2016-03-01 13:54:08 -0800 | [diff] [blame] | 271 | } |
| 272 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 273 | return tileStage->get(); |
| 274 | } |
| 275 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 276 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 277 | // Specialized Samplers |
| 278 | |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 279 | // RGBA8888UnitRepeatSrc - A sampler that takes advantage of the fact the the src and destination |
| 280 | // are the same format and do not need in transformations in pixel space. Therefore, there is no |
| 281 | // need to convert them to HiFi pixel format. |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 282 | class RGBA8888UnitRepeatSrc final : public SkLinearBitmapPipeline::SampleProcessorInterface, |
| 283 | public SkLinearBitmapPipeline::DestinationInterface { |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 284 | public: |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 285 | RGBA8888UnitRepeatSrc(const uint32_t* src, int32_t width) |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 286 | : fSrc{src}, fWidth{width} { } |
| 287 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 288 | void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override { |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 289 | SkASSERT(fDest + n <= fEnd); |
| 290 | // At this point xs and ys should be >= 0, so trunc is the same as floor. |
| 291 | Sk4i iXs = SkNx_cast<int>(xs); |
| 292 | Sk4i iYs = SkNx_cast<int>(ys); |
| 293 | |
| 294 | if (n >= 1) *fDest++ = *this->pixelAddress(iXs[0], iYs[0]); |
| 295 | if (n >= 2) *fDest++ = *this->pixelAddress(iXs[1], iYs[1]); |
| 296 | if (n >= 3) *fDest++ = *this->pixelAddress(iXs[2], iYs[2]); |
| 297 | } |
| 298 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 299 | void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override { |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 300 | SkASSERT(fDest + 4 <= fEnd); |
| 301 | Sk4i iXs = SkNx_cast<int>(xs); |
| 302 | Sk4i iYs = SkNx_cast<int>(ys); |
| 303 | *fDest++ = *this->pixelAddress(iXs[0], iYs[0]); |
| 304 | *fDest++ = *this->pixelAddress(iXs[1], iYs[1]); |
| 305 | *fDest++ = *this->pixelAddress(iXs[2], iYs[2]); |
| 306 | *fDest++ = *this->pixelAddress(iXs[3], iYs[3]); |
| 307 | } |
| 308 | |
| 309 | void pointSpan(Span span) override { |
| 310 | SkASSERT(fDest + span.count() <= fEnd); |
herb | 5d4943c | 2016-04-26 08:15:27 -0700 | [diff] [blame] | 311 | if (span.length() != 0.0f) { |
| 312 | int32_t x = SkScalarTruncToInt(span.startX()); |
| 313 | int32_t y = SkScalarTruncToInt(span.startY()); |
| 314 | const uint32_t* src = this->pixelAddress(x, y); |
| 315 | memmove(fDest, src, span.count() * sizeof(uint32_t)); |
| 316 | fDest += span.count(); |
| 317 | } |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void repeatSpan(Span span, int32_t repeatCount) override { |
| 321 | SkASSERT(fDest + span.count() * repeatCount <= fEnd); |
| 322 | |
herb | 5d4943c | 2016-04-26 08:15:27 -0700 | [diff] [blame] | 323 | int32_t x = SkScalarTruncToInt(span.startX()); |
| 324 | int32_t y = SkScalarTruncToInt(span.startY()); |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 325 | const uint32_t* src = this->pixelAddress(x, y); |
| 326 | uint32_t* dest = fDest; |
| 327 | while (repeatCount --> 0) { |
| 328 | memmove(dest, src, span.count() * sizeof(uint32_t)); |
| 329 | dest += span.count(); |
| 330 | } |
| 331 | fDest = dest; |
| 332 | } |
| 333 | |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 334 | void setDestination(void* dst, int count) override { |
| 335 | fDest = static_cast<uint32_t*>(dst); |
| 336 | fEnd = fDest + count; |
| 337 | } |
| 338 | |
| 339 | private: |
| 340 | const uint32_t* pixelAddress(int32_t x, int32_t y) { |
| 341 | return &fSrc[fWidth * y + x]; |
| 342 | } |
| 343 | const uint32_t* const fSrc; |
| 344 | const int32_t fWidth; |
| 345 | uint32_t* fDest; |
| 346 | uint32_t* fEnd; |
| 347 | }; |
| 348 | |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 349 | // RGBA8888UnitRepeatSrc - A sampler that takes advantage of the fact the the src and destination |
| 350 | // are the same format and do not need in transformations in pixel space. Therefore, there is no |
| 351 | // need to convert them to HiFi pixel format. |
| 352 | class RGBA8888UnitRepeatSrcOver final : public SkLinearBitmapPipeline::SampleProcessorInterface, |
| 353 | public SkLinearBitmapPipeline::DestinationInterface { |
| 354 | public: |
| 355 | RGBA8888UnitRepeatSrcOver(const uint32_t* src, int32_t width) |
| 356 | : fSrc{src}, fWidth{width} { } |
| 357 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 358 | void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override { |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 359 | SkASSERT(fDest + n <= fEnd); |
| 360 | // At this point xs and ys should be >= 0, so trunc is the same as floor. |
| 361 | Sk4i iXs = SkNx_cast<int>(xs); |
| 362 | Sk4i iYs = SkNx_cast<int>(ys); |
| 363 | |
| 364 | if (n >= 1) blendPixelAt(iXs[0], iYs[0]); |
| 365 | if (n >= 2) blendPixelAt(iXs[1], iYs[1]); |
| 366 | if (n >= 3) blendPixelAt(iXs[2], iYs[2]); |
| 367 | } |
| 368 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 369 | void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override { |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 370 | SkASSERT(fDest + 4 <= fEnd); |
| 371 | Sk4i iXs = SkNx_cast<int>(xs); |
| 372 | Sk4i iYs = SkNx_cast<int>(ys); |
| 373 | blendPixelAt(iXs[0], iYs[0]); |
| 374 | blendPixelAt(iXs[1], iYs[1]); |
| 375 | blendPixelAt(iXs[2], iYs[2]); |
| 376 | blendPixelAt(iXs[3], iYs[3]); |
| 377 | } |
| 378 | |
| 379 | void pointSpan(Span span) override { |
| 380 | if (span.length() != 0.0f) { |
| 381 | this->repeatSpan(span, 1); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void repeatSpan(Span span, int32_t repeatCount) override { |
| 386 | SkASSERT(fDest + span.count() * repeatCount <= fEnd); |
| 387 | SkASSERT(span.count() > 0); |
| 388 | SkASSERT(repeatCount > 0); |
| 389 | |
| 390 | int32_t x = (int32_t)span.startX(); |
| 391 | int32_t y = (int32_t)span.startY(); |
| 392 | const uint32_t* beginSpan = this->pixelAddress(x, y); |
| 393 | |
| 394 | SkOpts::srcover_srgb_srgb(fDest, beginSpan, span.count() * repeatCount, span.count()); |
| 395 | |
| 396 | fDest += span.count() * repeatCount; |
| 397 | |
| 398 | SkASSERT(fDest <= fEnd); |
| 399 | } |
| 400 | |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 401 | void setDestination(void* dst, int count) override { |
| 402 | SkASSERT(count > 0); |
| 403 | fDest = static_cast<uint32_t*>(dst); |
| 404 | fEnd = fDest + count; |
| 405 | } |
| 406 | |
| 407 | private: |
| 408 | const uint32_t* pixelAddress(int32_t x, int32_t y) { |
| 409 | return &fSrc[fWidth * y + x]; |
| 410 | } |
| 411 | |
| 412 | void blendPixelAt(int32_t x, int32_t y) { |
| 413 | const uint32_t* src = this->pixelAddress(x, y); |
| 414 | SkOpts::srcover_srgb_srgb(fDest, src, 1, 1); |
| 415 | fDest += 1; |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 416 | } |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 417 | |
| 418 | const uint32_t* const fSrc; |
| 419 | const int32_t fWidth; |
| 420 | uint32_t* fDest; |
| 421 | uint32_t* fEnd; |
| 422 | }; |
| 423 | |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 424 | using Blender = SkLinearBitmapPipeline::BlendProcessorInterface; |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 425 | |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 426 | template <SkColorType colorType> |
| 427 | static SkLinearBitmapPipeline::PixelAccessorInterface* choose_specific_accessor( |
| 428 | const SkPixmap& srcPixmap, SkLinearBitmapPipeline::Accessor* accessor) |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 429 | { |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 430 | if (srcPixmap.info().gammaCloseToSRGB()) { |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 431 | using PA = PixelAccessor<colorType, kSRGB_SkGammaType>; |
| 432 | accessor->init<PA>(srcPixmap); |
| 433 | return accessor->get(); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 434 | } else { |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 435 | using PA = PixelAccessor<colorType, kLinear_SkGammaType>; |
| 436 | accessor->init<PA>(srcPixmap); |
| 437 | return accessor->get(); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 441 | static SkLinearBitmapPipeline::PixelAccessorInterface* choose_pixel_accessor( |
herb | ed54504 | 2016-02-18 13:55:02 -0800 | [diff] [blame] | 442 | const SkPixmap& srcPixmap, |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 443 | const SkColor A8TintColor, |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 444 | SkLinearBitmapPipeline::Accessor* accessor) |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 445 | { |
herb | ed54504 | 2016-02-18 13:55:02 -0800 | [diff] [blame] | 446 | const SkImageInfo& imageInfo = srcPixmap.info(); |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 447 | |
| 448 | SkLinearBitmapPipeline::PixelAccessorInterface* pixelAccessor = nullptr; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 449 | switch (imageInfo.colorType()) { |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 450 | case kAlpha_8_SkColorType: { |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 451 | using PA = PixelAccessor<kAlpha_8_SkColorType, kLinear_SkGammaType>; |
| 452 | accessor->init<PA>(srcPixmap, A8TintColor); |
| 453 | pixelAccessor = accessor->get(); |
herb | fd5a260 | 2016-03-01 07:01:23 -0800 | [diff] [blame] | 454 | } |
| 455 | break; |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 456 | case kARGB_4444_SkColorType: |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 457 | pixelAccessor = choose_specific_accessor<kARGB_4444_SkColorType>(srcPixmap, accessor); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 458 | break; |
| 459 | case kRGB_565_SkColorType: |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 460 | pixelAccessor = choose_specific_accessor<kRGB_565_SkColorType>(srcPixmap, accessor); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 461 | break; |
| 462 | case kRGBA_8888_SkColorType: |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 463 | pixelAccessor = choose_specific_accessor<kRGBA_8888_SkColorType>(srcPixmap, accessor); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 464 | break; |
herb | fd5a260 | 2016-03-01 07:01:23 -0800 | [diff] [blame] | 465 | case kBGRA_8888_SkColorType: |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 466 | pixelAccessor = choose_specific_accessor<kBGRA_8888_SkColorType>(srcPixmap, accessor); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 467 | break; |
herb | 222f8ff | 2016-03-23 15:14:23 -0700 | [diff] [blame] | 468 | case kIndex_8_SkColorType: |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 469 | pixelAccessor = choose_specific_accessor<kIndex_8_SkColorType>(srcPixmap, accessor); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 470 | break; |
| 471 | case kGray_8_SkColorType: |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 472 | pixelAccessor = choose_specific_accessor<kGray_8_SkColorType>(srcPixmap, accessor); |
herb | 222f8ff | 2016-03-23 15:14:23 -0700 | [diff] [blame] | 473 | break; |
herb | 15332a8 | 2016-05-12 11:37:00 -0700 | [diff] [blame] | 474 | case kRGBA_F16_SkColorType: { |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 475 | using PA = PixelAccessor<kRGBA_F16_SkColorType, kLinear_SkGammaType>; |
| 476 | accessor->init<PA>(srcPixmap); |
| 477 | pixelAccessor = accessor->get(); |
herb | 15332a8 | 2016-05-12 11:37:00 -0700 | [diff] [blame] | 478 | } |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 479 | break; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 480 | default: |
| 481 | SkFAIL("Not implemented. Unsupported src"); |
| 482 | break; |
| 483 | } |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 484 | |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 485 | return pixelAccessor; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 486 | } |
| 487 | |
herb | 308d988 | 2016-03-29 14:48:44 -0700 | [diff] [blame] | 488 | SkLinearBitmapPipeline::SampleProcessorInterface* choose_pixel_sampler( |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 489 | Blender* next, |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 490 | SkFilterQuality filterQuality, |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 491 | SkShader::TileMode xTile, SkShader::TileMode yTile, |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 492 | const SkPixmap& srcPixmap, |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 493 | const SkColor A8TintColor, |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 494 | SkLinearBitmapPipeline::SampleStage* sampleStage, |
| 495 | SkLinearBitmapPipeline::Accessor* accessor) { |
| 496 | const SkImageInfo& imageInfo = srcPixmap.info(); |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 497 | SkISize dimensions = imageInfo.dimensions(); |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 498 | |
| 499 | // Special case samplers with fully expanded templates |
| 500 | if (imageInfo.gammaCloseToSRGB()) { |
| 501 | if (filterQuality == kNone_SkFilterQuality) { |
| 502 | switch (imageInfo.colorType()) { |
| 503 | case kN32_SkColorType: { |
| 504 | using S = |
| 505 | NearestNeighborSampler< |
| 506 | PixelAccessor<kN32_SkColorType, kSRGB_SkGammaType>, Blender>; |
| 507 | sampleStage->initStage<S>(next, srcPixmap); |
| 508 | return sampleStage->get(); |
| 509 | } |
| 510 | case kIndex_8_SkColorType: { |
| 511 | using S = |
| 512 | NearestNeighborSampler< |
| 513 | PixelAccessor<kIndex_8_SkColorType, kSRGB_SkGammaType>, Blender>; |
| 514 | sampleStage->initStage<S>(next, srcPixmap); |
| 515 | return sampleStage->get(); |
| 516 | } |
| 517 | default: |
| 518 | break; |
| 519 | } |
| 520 | } else { |
| 521 | switch (imageInfo.colorType()) { |
| 522 | case kN32_SkColorType: { |
| 523 | using S = |
| 524 | BilerpSampler< |
| 525 | PixelAccessor<kN32_SkColorType, kSRGB_SkGammaType>, Blender>; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 526 | sampleStage->initStage<S>(next, dimensions, xTile, yTile, srcPixmap); |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 527 | return sampleStage->get(); |
| 528 | } |
| 529 | case kIndex_8_SkColorType: { |
| 530 | using S = |
| 531 | BilerpSampler< |
| 532 | PixelAccessor<kIndex_8_SkColorType, kSRGB_SkGammaType>, Blender>; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 533 | sampleStage->initStage<S>(next, dimensions, xTile, yTile, srcPixmap); |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 534 | return sampleStage->get(); |
| 535 | } |
| 536 | default: |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 542 | auto pixelAccessor = choose_pixel_accessor(srcPixmap, A8TintColor, accessor); |
herb | 00dd453 | 2016-07-11 10:33:37 -0700 | [diff] [blame] | 543 | // General cases. |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 544 | if (filterQuality == kNone_SkFilterQuality) { |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 545 | using S = NearestNeighborSampler<PixelAccessorShim, Blender>; |
| 546 | sampleStage->initStage<S>(next, pixelAccessor); |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 547 | } else { |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 548 | using S = BilerpSampler<PixelAccessorShim, Blender>; |
| 549 | sampleStage->initStage<S>(next, dimensions, xTile, yTile, pixelAccessor); |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 550 | } |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 551 | return sampleStage->get(); |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 552 | } |
| 553 | |
herb | 490d30f | 2016-03-04 10:39:14 -0800 | [diff] [blame] | 554 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 555 | // Pixel Blender Stage |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 556 | template <SkAlphaType alphaType> |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 557 | class SrcFPPixel final : public SkLinearBitmapPipeline::BlendProcessorInterface { |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 558 | public: |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 559 | SrcFPPixel(float postAlpha) : fPostAlpha{postAlpha} { } |
| 560 | SrcFPPixel(const SrcFPPixel& Blender) : fPostAlpha(Blender.fPostAlpha) {} |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 561 | void SK_VECTORCALL blendPixel(Sk4f pixel) override { |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 562 | SkASSERT(fDst + 1 <= fEnd ); |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 563 | this->srcPixel(fDst, pixel, 0); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 564 | fDst += 1; |
| 565 | } |
| 566 | |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 567 | void SK_VECTORCALL blend4Pixels(Sk4f p0, Sk4f p1, Sk4f p2, Sk4f p3) override { |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 568 | SkASSERT(fDst + 4 <= fEnd); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 569 | SkPM4f* dst = fDst; |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 570 | this->srcPixel(dst, p0, 0); |
| 571 | this->srcPixel(dst, p1, 1); |
| 572 | this->srcPixel(dst, p2, 2); |
| 573 | this->srcPixel(dst, p3, 3); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 574 | fDst += 4; |
| 575 | } |
| 576 | |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 577 | void setDestination(void* dst, int count) override { |
| 578 | fDst = static_cast<SkPM4f*>(dst); |
| 579 | fEnd = fDst + count; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | private: |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 583 | void SK_VECTORCALL srcPixel(SkPM4f* dst, Sk4f pixel, int index) { |
| 584 | check_pixel(pixel); |
| 585 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 586 | Sk4f newPixel = pixel; |
| 587 | if (alphaType == kUnpremul_SkAlphaType) { |
| 588 | newPixel = Premultiply(pixel); |
| 589 | } |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 590 | newPixel = newPixel * fPostAlpha; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 591 | newPixel.store(dst + index); |
| 592 | } |
mtklein | e5fb9c8 | 2016-07-07 08:12:09 -0700 | [diff] [blame] | 593 | static Sk4f SK_VECTORCALL Premultiply(Sk4f pixel) { |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 594 | float alpha = pixel[3]; |
| 595 | return pixel * Sk4f{alpha, alpha, alpha, 1.0f}; |
| 596 | } |
| 597 | |
| 598 | SkPM4f* fDst; |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 599 | SkPM4f* fEnd; |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 600 | Sk4f fPostAlpha; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 601 | }; |
| 602 | |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 603 | static SkLinearBitmapPipeline::BlendProcessorInterface* choose_blender_for_shading( |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 604 | SkAlphaType alphaType, |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 605 | float postAlpha, |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 606 | SkLinearBitmapPipeline::BlenderStage* blenderStage) { |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 607 | if (alphaType == kUnpremul_SkAlphaType) { |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 608 | blenderStage->initSink<SrcFPPixel<kUnpremul_SkAlphaType>>(postAlpha); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 609 | } else { |
| 610 | // kOpaque_SkAlphaType is treated the same as kPremul_SkAlphaType |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 611 | blenderStage->initSink<SrcFPPixel<kPremul_SkAlphaType>>(postAlpha); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 612 | } |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 613 | return blenderStage->get(); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 614 | } |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 615 | |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 616 | } // namespace |
| 617 | |
herb | 490d30f | 2016-03-04 10:39:14 -0800 | [diff] [blame] | 618 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 619 | // SkLinearBitmapPipeline |
herb | a856e25 | 2016-02-22 14:48:21 -0800 | [diff] [blame] | 620 | SkLinearBitmapPipeline::~SkLinearBitmapPipeline() {} |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 621 | |
| 622 | SkLinearBitmapPipeline::SkLinearBitmapPipeline( |
| 623 | const SkMatrix& inverse, |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 624 | SkFilterQuality filterQuality, |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 625 | SkShader::TileMode xTile, SkShader::TileMode yTile, |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 626 | SkColor paintColor, |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 627 | const SkPixmap& srcPixmap) |
| 628 | { |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 629 | SkISize dimensions = srcPixmap.info().dimensions(); |
herb | ed54504 | 2016-02-18 13:55:02 -0800 | [diff] [blame] | 630 | const SkImageInfo& srcImageInfo = srcPixmap.info(); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 631 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 632 | SkMatrix adjustedInverse = inverse; |
| 633 | if (filterQuality == kNone_SkFilterQuality) { |
| 634 | if (inverse.getScaleX() >= 0.0f) { |
| 635 | adjustedInverse.setTranslateX( |
| 636 | nextafterf(inverse.getTranslateX(), std::floor(inverse.getTranslateX()))); |
| 637 | } |
| 638 | if (inverse.getScaleY() >= 0.0f) { |
| 639 | adjustedInverse.setTranslateY( |
| 640 | nextafterf(inverse.getTranslateY(), std::floor(inverse.getTranslateY()))); |
| 641 | } |
| 642 | } |
| 643 | |
herb | 303c979 | 2016-04-07 07:56:12 -0700 | [diff] [blame] | 644 | SkScalar dx = adjustedInverse.getScaleX(); |
| 645 | |
herb | 222f8ff | 2016-03-23 15:14:23 -0700 | [diff] [blame] | 646 | // If it is an index 8 color type, the sampler converts to unpremul for better fidelity. |
| 647 | SkAlphaType alphaType = srcImageInfo.alphaType(); |
| 648 | if (srcPixmap.colorType() == kIndex_8_SkColorType) { |
| 649 | alphaType = kUnpremul_SkAlphaType; |
| 650 | } |
| 651 | |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 652 | float postAlpha = SkColorGetA(paintColor) * (1.0f / 255.0f); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 653 | // As the stages are built, the chooser function may skip a stage. For example, with the |
| 654 | // identity matrix, the matrix stage is skipped, and the tilerStage is the first stage. |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 655 | auto blenderStage = choose_blender_for_shading(alphaType, postAlpha, &fBlenderStage); |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 656 | auto samplerStage = choose_pixel_sampler( |
herb | 86a6c6d | 2016-07-22 14:06:27 -0700 | [diff] [blame] | 657 | blenderStage, filterQuality, xTile, yTile, |
| 658 | srcPixmap, paintColor, &fSampleStage, &fAccessor); |
herb | 9e0efe5 | 2016-04-08 13:25:28 -0700 | [diff] [blame] | 659 | auto tilerStage = choose_tiler(samplerStage, dimensions, xTile, yTile, |
| 660 | filterQuality, dx, &fTileStage); |
| 661 | fFirstStage = choose_matrix(tilerStage, adjustedInverse, &fMatrixStage); |
| 662 | fLastStage = blenderStage; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 663 | } |
| 664 | |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 665 | bool SkLinearBitmapPipeline::ClonePipelineForBlitting( |
herb | 57a69dc | 2016-05-19 14:19:23 -0700 | [diff] [blame] | 666 | SkEmbeddableLinearPipeline* pipelineStorage, |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 667 | const SkLinearBitmapPipeline& pipeline, |
| 668 | SkMatrix::TypeMask matrixMask, |
| 669 | SkShader::TileMode xTileMode, |
| 670 | SkShader::TileMode yTileMode, |
| 671 | SkFilterQuality filterQuality, |
| 672 | const SkPixmap& srcPixmap, |
| 673 | float finalAlpha, |
| 674 | SkXfermode::Mode xferMode, |
| 675 | const SkImageInfo& dstInfo) |
| 676 | { |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 677 | if (xferMode == SkXfermode::kSrcOver_Mode |
| 678 | && srcPixmap.info().alphaType() == kOpaque_SkAlphaType) { |
| 679 | xferMode = SkXfermode::kSrc_Mode; |
| 680 | } |
| 681 | |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 682 | if (matrixMask & ~SkMatrix::kTranslate_Mask ) { return false; } |
| 683 | if (filterQuality != SkFilterQuality::kNone_SkFilterQuality) { return false; } |
| 684 | if (finalAlpha != 1.0f) { return false; } |
| 685 | if (srcPixmap.info().colorType() != kRGBA_8888_SkColorType |
| 686 | || dstInfo.colorType() != kRGBA_8888_SkColorType) { return false; } |
| 687 | |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 688 | if (!srcPixmap.info().gammaCloseToSRGB() || !dstInfo.gammaCloseToSRGB()) { |
| 689 | return false; |
| 690 | } |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 691 | |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 692 | if (xferMode != SkXfermode::kSrc_Mode && xferMode != SkXfermode::kSrcOver_Mode) { |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 693 | return false; |
| 694 | } |
| 695 | |
herb | 57a69dc | 2016-05-19 14:19:23 -0700 | [diff] [blame] | 696 | pipelineStorage->init(pipeline, srcPixmap, xferMode, dstInfo); |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 697 | |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | SkLinearBitmapPipeline::SkLinearBitmapPipeline( |
| 702 | const SkLinearBitmapPipeline& pipeline, |
| 703 | const SkPixmap& srcPixmap, |
| 704 | SkXfermode::Mode mode, |
| 705 | const SkImageInfo& dstInfo) |
| 706 | { |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 707 | SkASSERT(mode == SkXfermode::kSrc_Mode || mode == SkXfermode::kSrcOver_Mode); |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 708 | SkASSERT(srcPixmap.info().colorType() == dstInfo.colorType() |
| 709 | && srcPixmap.info().colorType() == kRGBA_8888_SkColorType); |
| 710 | |
herb | 871a048 | 2016-05-03 08:11:52 -0700 | [diff] [blame] | 711 | if (mode == SkXfermode::kSrc_Mode) { |
| 712 | fSampleStage.initSink<RGBA8888UnitRepeatSrc>( |
| 713 | srcPixmap.writable_addr32(0, 0), srcPixmap.rowBytes() / 4); |
| 714 | fLastStage = fSampleStage.getInterface<DestinationInterface, RGBA8888UnitRepeatSrc>(); |
| 715 | } else { |
| 716 | fSampleStage.initSink<RGBA8888UnitRepeatSrcOver>( |
| 717 | srcPixmap.writable_addr32(0, 0), srcPixmap.rowBytes() / 4); |
| 718 | fLastStage = fSampleStage.getInterface<DestinationInterface, RGBA8888UnitRepeatSrcOver>(); |
| 719 | } |
| 720 | |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 721 | auto sampleStage = fSampleStage.get(); |
| 722 | auto tilerStage = pipeline.fTileStage.cloneStageTo(sampleStage, &fTileStage); |
| 723 | tilerStage = (tilerStage != nullptr) ? tilerStage : sampleStage; |
| 724 | auto matrixStage = pipeline.fMatrixStage.cloneStageTo(tilerStage, &fMatrixStage); |
| 725 | matrixStage = (matrixStage != nullptr) ? matrixStage : tilerStage; |
| 726 | fFirstStage = matrixStage; |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 727 | } |
| 728 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 729 | void SkLinearBitmapPipeline::shadeSpan4f(int x, int y, SkPM4f* dst, int count) { |
herb | 3eb4895 | 2016-02-19 14:39:47 -0800 | [diff] [blame] | 730 | SkASSERT(count > 0); |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 731 | this->blitSpan(x, y, dst, count); |
| 732 | } |
| 733 | |
| 734 | void SkLinearBitmapPipeline::blitSpan(int x, int y, void* dst, int count) { |
| 735 | SkASSERT(count > 0); |
herb | 6a5d713 | 2016-04-08 06:49:59 -0700 | [diff] [blame] | 736 | fLastStage->setDestination(dst, count); |
| 737 | |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 738 | // The count and length arguments start out in a precise relation in order to keep the |
| 739 | // math correct through the different stages. Count is the number of pixel to produce. |
| 740 | // Since the code samples at pixel centers, length is the distance from the center of the |
| 741 | // first pixel to the center of the last pixel. This implies that length is count-1. |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 742 | fFirstStage->pointSpan(Span{{x + 0.5f, y + 0.5f}, count - 1.0f, count}); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 743 | } |