epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 7 | |
| 8 | #include "SkBitmapProcShader.h" |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 9 | |
| 10 | #include "SkArenaAlloc.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 11 | #include "SkBitmapProcState.h" |
reed | 013e9e3 | 2015-09-15 14:46:27 -0700 | [diff] [blame] | 12 | #include "SkBitmapProvider.h" |
Mike Reed | 43e498e | 2017-06-08 20:26:41 -0400 | [diff] [blame] | 13 | #include "SkPM4fPriv.h" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 14 | #include "SkXfermodePriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 16 | static bool only_scale_and_translate(const SkMatrix& matrix) { |
| 17 | unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; |
| 18 | return (matrix.getType() & ~mask) == 0; |
| 19 | } |
| 20 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 21 | class BitmapProcInfoContext : public SkShaderBase::Context { |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 22 | public: |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 23 | // The info has been allocated elsewhere, but we are responsible for calling its destructor. |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 24 | BitmapProcInfoContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec, |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 25 | SkBitmapProcInfo* info) |
| 26 | : INHERITED(shader, rec) |
| 27 | , fInfo(info) |
| 28 | { |
| 29 | fFlags = 0; |
| 30 | if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 31 | fFlags |= SkShaderBase::kOpaqueAlpha_Flag; |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 35 | fFlags |= SkShaderBase::kConstInY32_Flag; |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 39 | uint32_t getFlags() const override { return fFlags; } |
| 40 | |
| 41 | private: |
| 42 | SkBitmapProcInfo* fInfo; |
| 43 | uint32_t fFlags; |
| 44 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 45 | typedef SkShaderBase::Context INHERITED; |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | class BitmapProcShaderContext : public BitmapProcInfoContext { |
| 51 | public: |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 52 | BitmapProcShaderContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec, |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 53 | SkBitmapProcState* state) |
| 54 | : INHERITED(shader, rec, state) |
| 55 | , fState(state) |
| 56 | {} |
| 57 | |
| 58 | void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { |
| 59 | const SkBitmapProcState& state = *fState; |
| 60 | if (state.getShaderProc32()) { |
| 61 | state.getShaderProc32()(&state, x, y, dstC, count); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | const int BUF_MAX = 128; |
| 66 | uint32_t buffer[BUF_MAX]; |
| 67 | SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); |
| 68 | SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32(); |
| 69 | const int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX); |
| 70 | |
| 71 | SkASSERT(state.fPixmap.addr()); |
| 72 | |
| 73 | for (;;) { |
| 74 | int n = SkTMin(count, max); |
| 75 | SkASSERT(n > 0 && n < BUF_MAX*2); |
| 76 | mproc(state, buffer, n, x, y); |
| 77 | sproc(state, buffer, n, dstC); |
| 78 | |
| 79 | if ((count -= n) == 0) { |
| 80 | break; |
| 81 | } |
| 82 | SkASSERT(count > 0); |
| 83 | x += n; |
| 84 | dstC += n; |
| 85 | } |
| 86 | } |
| 87 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 88 | private: |
| 89 | SkBitmapProcState* fState; |
| 90 | |
| 91 | typedef BitmapProcInfoContext INHERITED; |
| 92 | }; |
| 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame] | 95 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 96 | SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext( |
| 97 | const SkShaderBase& shader, TileMode tmx, TileMode tmy, |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 98 | const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc) |
| 99 | { |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 100 | SkMatrix totalInverse; |
| 101 | // Do this first, so we know the matrix can be inverted. |
Florin Malita | 26368c3 | 2017-05-08 13:03:24 -0400 | [diff] [blame] | 102 | if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) { |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 103 | return nullptr; |
| 104 | } |
| 105 | |
Mike Klein | 0fddb2d | 2017-07-20 08:59:19 -0400 | [diff] [blame] | 106 | SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy); |
| 107 | if (!state->setup(totalInverse, *rec.fPaint)) { |
| 108 | return nullptr; |
Mike Reed | fb49909 | 2017-06-26 13:53:32 +0000 | [diff] [blame] | 109 | } |
Mike Klein | 0fddb2d | 2017-07-20 08:59:19 -0400 | [diff] [blame] | 110 | return alloc->make<BitmapProcShaderContext>(shader, rec, state); |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 111 | } |