epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 9 | #include "SkReadBuffer.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame^] | 10 | #include "SkRefCnt.h" |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame] | 11 | #include "SkString.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame^] | 12 | #include "SkTDArray.h" |
| 13 | #include "SkUnPreMultiply.h" |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 14 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame^] | 16 | class GrFragmentProcessor; |
| 17 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 18 | bool SkColorFilter::asColorMode(SkColor* color, SkXfermode::Mode* mode) const { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 19 | return false; |
| 20 | } |
| 21 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 22 | bool SkColorFilter::asColorMatrix(SkScalar matrix[20]) const { |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 23 | return false; |
| 24 | } |
| 25 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 26 | bool SkColorFilter::asComponentTable(SkBitmap*) const { |
reed@google.com | 7191840 | 2012-01-05 17:24:35 +0000 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 30 | SkColor SkColorFilter::filterColor(SkColor c) const { |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 31 | SkPMColor dst, src = SkPreMultiplyColor(c); |
| 32 | this->filterSpan(&src, 1, &dst); |
| 33 | return SkUnPreMultiply::PMColorToColor(dst); |
| 34 | } |
| 35 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 37 | |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 38 | /* |
| 39 | * Since colorfilters may be used on the GPU backend, and in that case we may string together |
| 40 | * many GrFragmentProcessors, we might exceed some internal instruction/resource limit. |
| 41 | * |
| 42 | * Since we don't yet know *what* those limits might be when we construct the final shader, |
| 43 | * we just set an arbitrary limit during construction. If later we find smarter ways to know what |
| 44 | * the limnits are, we can change this constant (or remove it). |
| 45 | */ |
| 46 | #define SK_MAX_COMPOSE_COLORFILTER_COUNT 4 |
| 47 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 48 | class SkComposeColorFilter : public SkColorFilter { |
| 49 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | uint32_t getFlags() const override { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 51 | // Can only claim alphaunchanged and 16bit support if both our proxys do. |
| 52 | return fOuter->getFlags() & fInner->getFlags(); |
| 53 | } |
| 54 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | void filterSpan(const SkPMColor shader[], int count, SkPMColor result[]) const override { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 56 | fInner->filterSpan(shader, count, result); |
| 57 | fOuter->filterSpan(result, count, result); |
| 58 | } |
| 59 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 60 | #ifndef SK_IGNORE_TO_STRING |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void toString(SkString* str) const override { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 62 | SkString outerS, innerS; |
| 63 | fOuter->toString(&outerS); |
| 64 | fInner->toString(&innerS); |
| 65 | str->appendf("SkComposeColorFilter: outer(%s) inner(%s)", outerS.c_str(), innerS.c_str()); |
| 66 | } |
| 67 | #endif |
| 68 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 69 | #if SK_SUPPORT_GPU |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 70 | bool asFragmentProcessors(GrContext* context, GrProcessorDataManager* procDataManager, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | SkTDArray<GrFragmentProcessor*>* array) const override { |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 72 | bool hasFrags = fInner->asFragmentProcessors(context, procDataManager, array); |
| 73 | hasFrags |= fOuter->asFragmentProcessors(context, procDataManager, array); |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 74 | return hasFrags; |
| 75 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 76 | #endif |
| 77 | |
| 78 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposeColorFilter) |
| 79 | |
| 80 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 81 | void flatten(SkWriteBuffer& buffer) const override { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 82 | buffer.writeFlattenable(fOuter); |
| 83 | buffer.writeFlattenable(fInner); |
| 84 | } |
| 85 | |
| 86 | private: |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 87 | SkComposeColorFilter(SkColorFilter* outer, SkColorFilter* inner, int composedFilterCount) |
| 88 | : fOuter(SkRef(outer)) |
| 89 | , fInner(SkRef(inner)) |
| 90 | , fComposedFilterCount(composedFilterCount) |
| 91 | { |
| 92 | SkASSERT(composedFilterCount >= 2); |
| 93 | SkASSERT(composedFilterCount <= SK_MAX_COMPOSE_COLORFILTER_COUNT); |
| 94 | } |
| 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | int privateComposedFilterCount() const override { |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 97 | return fComposedFilterCount; |
| 98 | } |
| 99 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 100 | SkAutoTUnref<SkColorFilter> fOuter; |
| 101 | SkAutoTUnref<SkColorFilter> fInner; |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 102 | const int fComposedFilterCount; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 103 | |
| 104 | friend class SkColorFilter; |
| 105 | |
| 106 | typedef SkColorFilter INHERITED; |
| 107 | }; |
| 108 | |
| 109 | SkFlattenable* SkComposeColorFilter::CreateProc(SkReadBuffer& buffer) { |
| 110 | SkAutoTUnref<SkColorFilter> outer(buffer.readColorFilter()); |
| 111 | SkAutoTUnref<SkColorFilter> inner(buffer.readColorFilter()); |
| 112 | return CreateComposeFilter(outer, inner); |
| 113 | } |
| 114 | |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 115 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 116 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 117 | SkColorFilter* SkColorFilter::CreateComposeFilter(SkColorFilter* outer, SkColorFilter* inner) { |
| 118 | if (!outer) { |
| 119 | return SkSafeRef(inner); |
| 120 | } |
| 121 | if (!inner) { |
| 122 | return SkSafeRef(outer); |
| 123 | } |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 124 | |
| 125 | // Give the subclass a shot at a more optimal composition... |
| 126 | SkColorFilter* composition = outer->newComposed(inner); |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 127 | if (composition) { |
| 128 | return composition; |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 129 | } |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 130 | |
| 131 | int count = inner->privateComposedFilterCount() + outer->privateComposedFilterCount(); |
| 132 | if (count > SK_MAX_COMPOSE_COLORFILTER_COUNT) { |
| 133 | return NULL; |
| 134 | } |
| 135 | return SkNEW_ARGS(SkComposeColorFilter, (outer, inner, count)); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkColorFilter) |
| 139 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkComposeColorFilter) |
| 140 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 141 | |