blob: bb4be485d6ba2961e13d1f8c208a14e5311b977b [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/sgl/SkColorFilter.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "SkColorFilter.h"
19#include "SkShader.h"
20
21void SkColorFilter::filterSpan16(const uint16_t s[], int count, uint16_t d[])
22{
23 SkASSERT(this->getFlags() & SkColorFilter::kHasFilter16_Flag);
24 SkASSERT(!"missing implementation of SkColorFilter::filterSpan16");
25
26 if (d != s)
27 memcpy(d, s, count * sizeof(uint16_t));
28}
29
30//////////////////////////////////////////////////////////////////////////////
31
32SkFilterShader::SkFilterShader(SkShader* shader, SkColorFilter* filter)
33{
34 fShader = shader; shader->ref();
35 fFilter = filter; filter->ref();
36}
37
38SkFilterShader::SkFilterShader(SkFlattenableReadBuffer& buffer) :
39 INHERITED(buffer)
40{
41 fShader = static_cast<SkShader*>(buffer.readFlattenable());
42 fFilter = static_cast<SkColorFilter*>(buffer.readFlattenable());
43}
44
45SkFilterShader::~SkFilterShader()
46{
47 fFilter->unref();
48 fShader->unref();
49}
50
51void SkFilterShader::beginSession()
52{
53 this->INHERITED::beginSession();
54 fShader->beginSession();
55}
56
57void SkFilterShader::endSession()
58{
59 fShader->endSession();
60 this->INHERITED::endSession();
61}
62
63void SkFilterShader::flatten(SkFlattenableWriteBuffer& buffer)
64{
65 this->INHERITED::flatten(buffer);
66 buffer.writeFlattenable(fShader);
67 buffer.writeFlattenable(fFilter);
68}
69
70uint32_t SkFilterShader::getFlags()
71{
72 uint32_t shaderF = fShader->getFlags();
73 uint32_t filterF = fFilter->getFlags();
74
75 // if the filter doesn't support 16bit, clear the matching bit in the shader
76 if (!(filterF & SkColorFilter::kHasFilter16_Flag))
77 shaderF &= ~SkShader::kHasSpan16_Flag;
78
79 // if the filter might change alpha, clear the opaque flag in the shader
80 if (!(filterF & SkColorFilter::kAlphaUnchanged_Flag))
81 shaderF &= ~(SkShader::kOpaqueAlpha_Flag | SkShader::kHasSpan16_Flag);
82
83 return shaderF;
84}
85
86bool SkFilterShader::setContext(const SkBitmap& device,
87 const SkPaint& paint,
88 const SkMatrix& matrix)
89{
90 return this->INHERITED::setContext(device, paint, matrix) &&
91 fShader->setContext(device, paint, matrix);
92}
93
94void SkFilterShader::shadeSpan(int x, int y, SkPMColor result[], int count)
95{
96 fShader->shadeSpan(x, y, result, count);
97 fFilter->filterSpan(result, count, result);
98}
99
100void SkFilterShader::shadeSpan16(int x, int y, uint16_t result[], int count)
101{
102 SkASSERT(fShader->getFlags() & SkShader::kHasSpan16_Flag);
103 SkASSERT(fFilter->getFlags() & SkColorFilter::kHasFilter16_Flag);
104
105 fShader->shadeSpan16(x, y, result, count);
106 fFilter->filterSpan16(result, count, result);
107}
108