tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkFilterShader.h" |
| 9 | |
| 10 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
| 14 | #include "SkString.h" |
| 15 | |
| 16 | SkFilterShader::SkFilterShader(SkShader* shader, SkColorFilter* filter) { |
| 17 | fShader = shader; |
| 18 | shader->ref(); |
| 19 | |
| 20 | fFilter = filter; |
| 21 | filter->ref(); |
| 22 | } |
| 23 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 24 | SkFilterShader::SkFilterShader(SkReadBuffer& buffer) |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 25 | : INHERITED(buffer) { |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 26 | fShader = buffer.readShader(); |
| 27 | fFilter = buffer.readColorFilter(); |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | SkFilterShader::~SkFilterShader() { |
| 31 | fFilter->unref(); |
| 32 | fShader->unref(); |
| 33 | } |
| 34 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 35 | void SkFilterShader::flatten(SkWriteBuffer& buffer) const { |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 36 | this->INHERITED::flatten(buffer); |
| 37 | buffer.writeFlattenable(fShader); |
| 38 | buffer.writeFlattenable(fFilter); |
| 39 | } |
| 40 | |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 41 | uint32_t SkFilterShader::FilterShaderContext::getFlags() const { |
| 42 | const SkFilterShader& filterShader = static_cast<const SkFilterShader&>(fShader); |
| 43 | |
| 44 | uint32_t shaderF = fShaderContext->getFlags(); |
| 45 | uint32_t filterF = filterShader.fFilter->getFlags(); |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 46 | |
| 47 | // if the filter doesn't support 16bit, clear the matching bit in the shader |
| 48 | if (!(filterF & SkColorFilter::kHasFilter16_Flag)) { |
| 49 | shaderF &= ~SkShader::kHasSpan16_Flag; |
| 50 | } |
| 51 | // if the filter might change alpha, clear the opaque flag in the shader |
| 52 | if (!(filterF & SkColorFilter::kAlphaUnchanged_Flag)) { |
| 53 | shaderF &= ~(SkShader::kOpaqueAlpha_Flag | SkShader::kHasSpan16_Flag); |
| 54 | } |
| 55 | return shaderF; |
| 56 | } |
| 57 | |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 58 | SkShader::Context* SkFilterShader::createContext(const SkBitmap& device, |
| 59 | const SkPaint& paint, |
| 60 | const SkMatrix& matrix, |
| 61 | void* storage) const { |
| 62 | if (!this->validContext(device, paint, matrix)) { |
| 63 | return NULL; |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 64 | } |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 65 | |
| 66 | char* shaderContextStorage = (char*)storage + sizeof(FilterShaderContext); |
| 67 | SkShader::Context* shaderContext = fShader->createContext(device, paint, matrix, |
| 68 | shaderContextStorage); |
| 69 | SkASSERT(shaderContext); |
| 70 | |
| 71 | return SkNEW_PLACEMENT_ARGS(storage, FilterShaderContext, |
| 72 | (*this, shaderContext, device, paint, matrix)); |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 73 | } |
| 74 | |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 75 | size_t SkFilterShader::contextSize() const { |
| 76 | return sizeof(FilterShaderContext) + fShader->contextSize(); |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 79 | bool SkFilterShader::validContext(const SkBitmap& device, |
| 80 | const SkPaint& paint, |
| 81 | const SkMatrix& matrix, |
| 82 | SkMatrix* totalInverse) const { |
| 83 | return this->INHERITED::validContext(device, paint, matrix, totalInverse) && |
| 84 | fShader->validContext(device, paint, matrix); |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 85 | } |
| 86 | |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 87 | SkFilterShader::FilterShaderContext::FilterShaderContext(const SkFilterShader& filterShader, |
| 88 | SkShader::Context* shaderContext, |
| 89 | const SkBitmap& device, |
| 90 | const SkPaint& paint, |
| 91 | const SkMatrix& matrix) |
| 92 | : INHERITED(filterShader, device, paint, matrix) |
| 93 | , fShaderContext(shaderContext) {} |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 95 | SkFilterShader::FilterShaderContext::~FilterShaderContext() { |
| 96 | fShaderContext->~Context(); |
| 97 | } |
| 98 | |
| 99 | void SkFilterShader::FilterShaderContext::shadeSpan(int x, int y, SkPMColor result[], int count) { |
| 100 | const SkFilterShader& filterShader = static_cast<const SkFilterShader&>(fShader); |
| 101 | |
| 102 | fShaderContext->shadeSpan(x, y, result, count); |
| 103 | filterShader.fFilter->filterSpan(result, count, result); |
| 104 | } |
| 105 | |
| 106 | void SkFilterShader::FilterShaderContext::shadeSpan16(int x, int y, uint16_t result[], int count) { |
| 107 | const SkFilterShader& filterShader = static_cast<const SkFilterShader&>(fShader); |
| 108 | |
| 109 | SkASSERT(fShaderContext->getFlags() & SkShader::kHasSpan16_Flag); |
| 110 | SkASSERT(filterShader.fFilter->getFlags() & SkColorFilter::kHasFilter16_Flag); |
| 111 | |
| 112 | fShaderContext->shadeSpan16(x, y, result, count); |
| 113 | filterShader.fFilter->filterSpan16(result, count, result); |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 114 | } |
| 115 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 116 | #ifndef SK_IGNORE_TO_STRING |
tfarina@chromium.org | 883fe7f | 2013-07-16 13:05:27 +0000 | [diff] [blame] | 117 | void SkFilterShader::toString(SkString* str) const { |
| 118 | str->append("SkFilterShader: ("); |
| 119 | |
| 120 | str->append("Shader: "); |
| 121 | fShader->toString(str); |
| 122 | str->append(" Filter: "); |
| 123 | // TODO: add "fFilter->toString(str);" once SkColorFilter::toString is added |
| 124 | |
| 125 | this->INHERITED::toString(str); |
| 126 | |
| 127 | str->append(")"); |
| 128 | } |
| 129 | #endif |