| 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 "SkColorFilterImageFilter.h" | 8 #include "SkColorFilterImageFilter.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 bool matrix_needs_clamping(SkScalar matrix[20]) { | 50 bool matrix_needs_clamping(SkScalar matrix[20]) { |
| 51 return component_needs_clamping(matrix) | 51 return component_needs_clamping(matrix) |
| 52 || component_needs_clamping(matrix+5) | 52 || component_needs_clamping(matrix+5) |
| 53 || component_needs_clamping(matrix+10) | 53 || component_needs_clamping(matrix+10) |
| 54 || component_needs_clamping(matrix+15); | 54 || component_needs_clamping(matrix+15); |
| 55 } | 55 } |
| 56 | 56 |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 SkColorFilterImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf, | 59 SkColorFilterImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf, |
| 60 SkImageFilter* input) { | 60 SkImageFilter* input, const SkIRect* cropRect) { |
| 61 SkASSERT(cf); | 61 SkASSERT(cf); |
| 62 SkScalar colorMatrix[20], inputMatrix[20]; | 62 SkScalar colorMatrix[20], inputMatrix[20]; |
| 63 SkColorFilter* inputColorFilter; | 63 SkColorFilter* inputColorFilter; |
| 64 if (input && cf->asColorMatrix(colorMatrix) | 64 if (input && cf->asColorMatrix(colorMatrix) |
| 65 && input->asColorFilter(&inputColorFilter) | 65 && input->asColorFilter(&inputColorFilter) |
| 66 && (NULL != inputColorFilter)) { | 66 && (NULL != inputColorFilter)) { |
| 67 SkAutoUnref autoUnref(inputColorFilter); | 67 SkAutoUnref autoUnref(inputColorFilter); |
| 68 if (inputColorFilter->asColorMatrix(inputMatrix) && !matrix_needs_clampi
ng(inputMatrix)) { | 68 if (inputColorFilter->asColorMatrix(inputMatrix) && !matrix_needs_clampi
ng(inputMatrix)) { |
| 69 SkScalar combinedMatrix[20]; | 69 SkScalar combinedMatrix[20]; |
| 70 mult_color_matrix(inputMatrix, colorMatrix, combinedMatrix); | 70 mult_color_matrix(inputMatrix, colorMatrix, combinedMatrix); |
| 71 SkAutoTUnref<SkColorFilter> newCF(SkNEW_ARGS(SkColorMatrixFilter, (c
ombinedMatrix))); | 71 SkAutoTUnref<SkColorFilter> newCF(SkNEW_ARGS(SkColorMatrixFilter, (c
ombinedMatrix))); |
| 72 return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(
0))); | 72 return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(
0), cropRect)); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 return SkNEW_ARGS(SkColorFilterImageFilter, (cf, input)); | 75 return SkNEW_ARGS(SkColorFilterImageFilter, (cf, input, cropRect)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, SkImageFil
ter* input) : INHERITED(input), fColorFilter(cf) { | 78 SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, |
| 79 SkImageFilter* input, const SkIRect* cropRect) |
| 80 : INHERITED(input, cropRect), fColorFilter(cf) { |
| 79 SkASSERT(cf); | 81 SkASSERT(cf); |
| 80 SkSafeRef(cf); | 82 SkSafeRef(cf); |
| 81 } | 83 } |
| 82 | 84 |
| 83 SkColorFilterImageFilter::SkColorFilterImageFilter(SkFlattenableReadBuffer& buff
er) : INHERITED(buffer) { | 85 SkColorFilterImageFilter::SkColorFilterImageFilter(SkFlattenableReadBuffer& buff
er) : INHERITED(buffer) { |
| 84 fColorFilter = buffer.readFlattenableT<SkColorFilter>(); | 86 fColorFilter = buffer.readFlattenableT<SkColorFilter>(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void SkColorFilterImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { | 89 void SkColorFilterImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 88 this->INHERITED::flatten(buffer); | 90 this->INHERITED::flatten(buffer); |
| 89 | 91 |
| 90 buffer.writeFlattenable(fColorFilter); | 92 buffer.writeFlattenable(fColorFilter); |
| 91 } | 93 } |
| 92 | 94 |
| 93 SkColorFilterImageFilter::~SkColorFilterImageFilter() { | 95 SkColorFilterImageFilter::~SkColorFilterImageFilter() { |
| 94 SkSafeUnref(fColorFilter); | 96 SkSafeUnref(fColorFilter); |
| 95 } | 97 } |
| 96 | 98 |
| 97 bool SkColorFilterImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& sourc
e, | 99 bool SkColorFilterImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& sourc
e, |
| 98 const SkMatrix& matrix, | 100 const SkMatrix& matrix, |
| 99 SkBitmap* result, | 101 SkBitmap* result, |
| 100 SkIPoint* loc) { | 102 SkIPoint* loc) { |
| 101 SkBitmap src = source; | 103 SkBitmap src = source; |
| 102 if (getInput(0) && !getInput(0)->filterImage(proxy, source, matrix, &src, lo
c)) { | 104 if (getInput(0) && !getInput(0)->filterImage(proxy, source, matrix, &src, lo
c)) { |
| 103 return false; | 105 return false; |
| 104 } | 106 } |
| 105 | 107 |
| 106 SkAutoTUnref<SkDevice> device(proxy->createDevice(src.width(), src.height())
); | 108 SkIRect bounds; |
| 109 src.getBounds(&bounds); |
| 110 if (!this->applyCropRect(&bounds)) { |
| 111 return false; |
| 112 } |
| 113 |
| 114 SkAutoTUnref<SkDevice> device(proxy->createDevice(bounds.width(), bounds.hei
ght())); |
| 107 SkCanvas canvas(device.get()); | 115 SkCanvas canvas(device.get()); |
| 108 SkPaint paint; | 116 SkPaint paint; |
| 109 | 117 |
| 110 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 118 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 111 paint.setColorFilter(fColorFilter); | 119 paint.setColorFilter(fColorFilter); |
| 112 canvas.drawSprite(src, 0, 0, &paint); | 120 canvas.drawSprite(src, -bounds.fLeft, -bounds.fTop, &paint); |
| 113 | 121 |
| 114 *result = device.get()->accessBitmap(false); | 122 *result = device.get()->accessBitmap(false); |
| 123 loc->fX += bounds.fLeft; |
| 124 loc->fY += bounds.fTop; |
| 115 return true; | 125 return true; |
| 116 } | 126 } |
| 117 | 127 |
| 118 bool SkColorFilterImageFilter::asColorFilter(SkColorFilter** filter) const { | 128 bool SkColorFilterImageFilter::asColorFilter(SkColorFilter** filter) const { |
| 119 if (filter) { | 129 if (cropRect().isLargest()) { |
| 120 *filter = fColorFilter; | 130 if (filter) { |
| 121 fColorFilter->ref(); | 131 *filter = fColorFilter; |
| 132 fColorFilter->ref(); |
| 133 } |
| 134 return true; |
| 122 } | 135 } |
| 123 return true; | 136 return false; |
| 124 } | 137 } |
| OLD | NEW |