| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkMergeImageFilter.h" | 8 #include "SkMergeImageFilter.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkValidationUtils.h" | 13 #include "SkValidationUtils.h" |
| 14 | 14 |
| 15 /////////////////////////////////////////////////////////////////////////////// | 15 /////////////////////////////////////////////////////////////////////////////// |
| 16 | 16 |
| 17 void SkMergeImageFilter::initAllocModes() { | 17 void SkMergeImageFilter::initAllocModes() { |
| 18 int inputCount = countInputs(); | 18 int inputCount = this->countInputs(); |
| 19 if (inputCount) { | 19 if (inputCount) { |
| 20 size_t size = sizeof(uint8_t) * inputCount; | 20 size_t size = sizeof(uint8_t) * inputCount; |
| 21 if (size <= sizeof(fStorage)) { | 21 if (size <= sizeof(fStorage)) { |
| 22 fModes = SkTCast<uint8_t*>(fStorage); | 22 fModes = SkTCast<uint8_t*>(fStorage); |
| 23 } else { | 23 } else { |
| 24 fModes = SkTCast<uint8_t*>(sk_malloc_throw(size)); | 24 fModes = SkTCast<uint8_t*>(sk_malloc_throw(size)); |
| 25 } | 25 } |
| 26 } else { | 26 } else { |
| 27 fModes = nullptr; | 27 fModes = nullptr; |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 void SkMergeImageFilter::initModes(const SkXfermode::Mode modes[]) { | 31 void SkMergeImageFilter::initModes(const SkXfermode::Mode modes[]) { |
| 32 if (modes) { | 32 if (modes) { |
| 33 this->initAllocModes(); | 33 this->initAllocModes(); |
| 34 int inputCount = countInputs(); | 34 int inputCount = this->countInputs(); |
| 35 for (int i = 0; i < inputCount; ++i) { | 35 for (int i = 0; i < inputCount; ++i) { |
| 36 fModes[i] = SkToU8(modes[i]); | 36 fModes[i] = SkToU8(modes[i]); |
| 37 } | 37 } |
| 38 } else { | 38 } else { |
| 39 fModes = nullptr; | 39 fModes = nullptr; |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 SkMergeImageFilter::SkMergeImageFilter(SkImageFilter* filters[], int count, | 43 SkMergeImageFilter::SkMergeImageFilter(SkImageFilter* filters[], int count, |
| 44 const SkXfermode::Mode modes[], | 44 const SkXfermode::Mode modes[], |
| 45 const CropRect* cropRect) | 45 const CropRect* cropRect) |
| 46 : INHERITED(count, filters, cropRect) { | 46 : INHERITED(count, filters, cropRect) { |
| 47 SkASSERT(count >= 0); | 47 SkASSERT(count >= 0); |
| 48 this->initModes(modes); | 48 this->initModes(modes); |
| 49 } | 49 } |
| 50 | 50 |
| 51 SkMergeImageFilter::~SkMergeImageFilter() { | 51 SkMergeImageFilter::~SkMergeImageFilter() { |
| 52 | 52 |
| 53 if (fModes != SkTCast<uint8_t*>(fStorage)) { | 53 if (fModes != SkTCast<uint8_t*>(fStorage)) { |
| 54 sk_free(fModes); | 54 sk_free(fModes); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 bool SkMergeImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, | 58 bool SkMergeImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, |
| 59 const Context& ctx, | 59 const Context& ctx, |
| 60 SkBitmap* result, SkIPoint* offset) const
{ | 60 SkBitmap* result, SkIPoint* offset) const
{ |
| 61 if (countInputs() < 1) { | 61 int inputCount = this->countInputs(); |
| 62 if (inputCount < 1) { |
| 62 return false; | 63 return false; |
| 63 } | 64 } |
| 64 | 65 |
| 65 SkIRect bounds; | 66 SkIRect bounds; |
| 66 if (!this->applyCropRect(ctx, src, SkIPoint::Make(0, 0), &bounds)) { | 67 |
| 68 SkAutoTDeleteArray<SkBitmap> inputs(new SkBitmap[inputCount]); |
| 69 SkAutoTDeleteArray<SkIPoint> offsets(new SkIPoint[inputCount]); |
| 70 bool didProduceResult = false; |
| 71 |
| 72 // Filter all of the inputs. |
| 73 for (int i = 0; i < inputCount; ++i) { |
| 74 inputs[i] = src; |
| 75 offsets[i].setZero(); |
| 76 if (!this->filterInput(i, proxy, src, ctx, &inputs[i], &offsets[i])) { |
| 77 inputs[i].reset(); |
| 78 continue; |
| 79 } |
| 80 SkIRect srcBounds; |
| 81 inputs[i].getBounds(&srcBounds); |
| 82 srcBounds.offset(offsets[i]); |
| 83 if (!didProduceResult) { |
| 84 bounds = srcBounds; |
| 85 didProduceResult = true; |
| 86 } else { |
| 87 bounds.join(srcBounds); |
| 88 } |
| 89 } |
| 90 if (!didProduceResult) { |
| 91 return false; |
| 92 } |
| 93 |
| 94 // Apply the crop rect to the union of the inputs' bounds. |
| 95 if (!this->getCropRect().applyTo(bounds, ctx, &bounds)) { |
| 67 return false; | 96 return false; |
| 68 } | 97 } |
| 69 | 98 |
| 70 const int x0 = bounds.left(); | 99 const int x0 = bounds.left(); |
| 71 const int y0 = bounds.top(); | 100 const int y0 = bounds.top(); |
| 72 | 101 |
| 102 // Allocate the destination buffer. |
| 73 SkAutoTUnref<SkBaseDevice> dst(proxy->createDevice(bounds.width(), bounds.he
ight())); | 103 SkAutoTUnref<SkBaseDevice> dst(proxy->createDevice(bounds.width(), bounds.he
ight())); |
| 74 if (nullptr == dst) { | 104 if (nullptr == dst) { |
| 75 return false; | 105 return false; |
| 76 } | 106 } |
| 77 SkCanvas canvas(dst); | 107 SkCanvas canvas(dst); |
| 78 SkPaint paint; | |
| 79 | 108 |
| 80 bool didProduceResult = false; | 109 // Composite all of the filter inputs. |
| 81 int inputCount = countInputs(); | |
| 82 for (int i = 0; i < inputCount; ++i) { | 110 for (int i = 0; i < inputCount; ++i) { |
| 83 SkBitmap tmp; | 111 SkPaint paint; |
| 84 SkBitmap input = src; | |
| 85 SkIPoint pos = SkIPoint::Make(0, 0); | |
| 86 if (!this->filterInput(i, proxy, src, ctx, &input, &pos)) { | |
| 87 continue; | |
| 88 } | |
| 89 if (fModes) { | 112 if (fModes) { |
| 90 paint.setXfermodeMode((SkXfermode::Mode)fModes[i]); | 113 paint.setXfermodeMode((SkXfermode::Mode)fModes[i]); |
| 91 } else { | |
| 92 paint.setXfermode(nullptr); | |
| 93 } | 114 } |
| 94 canvas.drawBitmap(input, SkIntToScalar(pos.x() - x0), SkIntToScalar(pos.
y() - y0), &paint); | 115 canvas.drawBitmap(inputs[i], SkIntToScalar(offsets[i].x() - x0), |
| 95 didProduceResult = true; | 116 SkIntToScalar(offsets[i].y() - y0), &paint)
; |
| 96 } | 117 } |
| 97 | 118 |
| 98 if (!didProduceResult) | |
| 99 return false; | |
| 100 | |
| 101 offset->fX = bounds.left(); | 119 offset->fX = bounds.left(); |
| 102 offset->fY = bounds.top(); | 120 offset->fY = bounds.top(); |
| 103 *result = dst->accessBitmap(false); | 121 *result = dst->accessBitmap(false); |
| 104 return true; | 122 return true; |
| 105 } | 123 } |
| 106 | 124 |
| 107 SkFlattenable* SkMergeImageFilter::CreateProc(SkReadBuffer& buffer) { | 125 SkFlattenable* SkMergeImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 108 Common common; | 126 Common common; |
| 109 if (!common.unflatten(buffer, -1)) { | 127 if (!common.unflatten(buffer, -1)) { |
| 110 return nullptr; | 128 return nullptr; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 127 } | 145 } |
| 128 return Create(common.inputs(), count, modes.get(), &common.cropRect()); | 146 return Create(common.inputs(), count, modes.get(), &common.cropRect()); |
| 129 } | 147 } |
| 130 return Create(common.inputs(), count, nullptr, &common.cropRect()); | 148 return Create(common.inputs(), count, nullptr, &common.cropRect()); |
| 131 } | 149 } |
| 132 | 150 |
| 133 void SkMergeImageFilter::flatten(SkWriteBuffer& buffer) const { | 151 void SkMergeImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 134 this->INHERITED::flatten(buffer); | 152 this->INHERITED::flatten(buffer); |
| 135 buffer.writeBool(fModes != nullptr); | 153 buffer.writeBool(fModes != nullptr); |
| 136 if (fModes) { | 154 if (fModes) { |
| 137 buffer.writeByteArray(fModes, countInputs() * sizeof(fModes[0])); | 155 buffer.writeByteArray(fModes, this->countInputs() * sizeof(fModes[0])); |
| 138 } | 156 } |
| 139 } | 157 } |
| 140 | 158 |
| 141 #ifndef SK_IGNORE_TO_STRING | 159 #ifndef SK_IGNORE_TO_STRING |
| 142 void SkMergeImageFilter::toString(SkString* str) const { | 160 void SkMergeImageFilter::toString(SkString* str) const { |
| 143 str->appendf("SkMergeImageFilter: ("); | 161 str->appendf("SkMergeImageFilter: ("); |
| 144 | 162 |
| 145 for (int i = 0; i < this->countInputs(); ++i) { | 163 for (int i = 0; i < this->countInputs(); ++i) { |
| 146 SkImageFilter* filter = this->getInput(i); | 164 SkImageFilter* filter = this->getInput(i); |
| 147 str->appendf("%d: (", i); | 165 str->appendf("%d: (", i); |
| 148 filter->toString(str); | 166 filter->toString(str); |
| 149 str->appendf(")"); | 167 str->appendf(")"); |
| 150 } | 168 } |
| 151 | 169 |
| 152 str->append(")"); | 170 str->append(")"); |
| 153 } | 171 } |
| 154 #endif | 172 #endif |
| OLD | NEW |