2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkFilterShader.h"
10 #include "SkColorFilter.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
16 SkFilterShader::SkFilterShader(SkShader* shader, SkColorFilter* filter) {
24 SkFilterShader::SkFilterShader(SkReadBuffer& buffer)
26 fShader = buffer.readShader();
27 fFilter = buffer.readColorFilter();
30 SkFilterShader::~SkFilterShader() {
35 void SkFilterShader::flatten(SkWriteBuffer& buffer) const {
36 this->INHERITED::flatten(buffer);
37 buffer.writeFlattenable(fShader);
38 buffer.writeFlattenable(fFilter);
41 uint32_t SkFilterShader::getFlags() {
42 uint32_t shaderF = fShader->getFlags();
43 uint32_t filterF = fFilter->getFlags();
45 // if the filter doesn't support 16bit, clear the matching bit in the shader
46 if (!(filterF & SkColorFilter::kHasFilter16_Flag)) {
47 shaderF &= ~SkShader::kHasSpan16_Flag;
49 // if the filter might change alpha, clear the opaque flag in the shader
50 if (!(filterF & SkColorFilter::kAlphaUnchanged_Flag)) {
51 shaderF &= ~(SkShader::kOpaqueAlpha_Flag | SkShader::kHasSpan16_Flag);
56 bool SkFilterShader::setContext(const SkBitmap& device,
58 const SkMatrix& matrix) {
59 // we need to keep the setContext/endContext calls balanced. If we return
60 // false, our endContext() will not be called.
62 if (!this->INHERITED::setContext(device, paint, matrix)) {
65 if (!fShader->setContext(device, paint, matrix)) {
66 this->INHERITED::endContext();
72 void SkFilterShader::endContext() {
73 fShader->endContext();
74 this->INHERITED::endContext();
77 void SkFilterShader::shadeSpan(int x, int y, SkPMColor result[], int count) {
78 fShader->shadeSpan(x, y, result, count);
79 fFilter->filterSpan(result, count, result);
82 void SkFilterShader::shadeSpan16(int x, int y, uint16_t result[], int count) {
83 SkASSERT(fShader->getFlags() & SkShader::kHasSpan16_Flag);
84 SkASSERT(fFilter->getFlags() & SkColorFilter::kHasFilter16_Flag);
86 fShader->shadeSpan16(x, y, result, count);
87 fFilter->filterSpan16(result, count, result);
91 void SkFilterShader::toString(SkString* str) const {
92 str->append("SkFilterShader: (");
94 str->append("Shader: ");
95 fShader->toString(str);
96 str->append(" Filter: ");
97 // TODO: add "fFilter->toString(str);" once SkColorFilter::toString is added
99 this->INHERITED::toString(str);