1
/* libs/graphics/sgl/SkShader.cpp
3
** Copyright 2006, The Android Open Source Project
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
9
** http://www.apache.org/licenses/LICENSE-2.0
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.
21
SkShader::SkShader() : fLocalMatrix(NULL) {
22
SkDEBUGCODE(fInSession = false;)
25
SkShader::SkShader(SkFlattenableReadBuffer& buffer)
26
: INHERITED(buffer), fLocalMatrix(NULL) {
27
if (buffer.readBool()) {
29
buffer.read(&matrix, sizeof(matrix));
30
setLocalMatrix(matrix);
32
SkDEBUGCODE(fInSession = false;)
35
SkShader::~SkShader() {
36
SkASSERT(!fInSession);
37
sk_free(fLocalMatrix);
40
void SkShader::beginSession() {
41
SkASSERT(!fInSession);
42
SkDEBUGCODE(fInSession = true;)
45
void SkShader::endSession() {
47
SkDEBUGCODE(fInSession = false;)
50
void SkShader::flatten(SkFlattenableWriteBuffer& buffer) {
51
this->INHERITED::flatten(buffer);
52
buffer.writeBool(fLocalMatrix != NULL);
54
buffer.writeMul4(fLocalMatrix, sizeof(SkMatrix));
58
bool SkShader::getLocalMatrix(SkMatrix* localM) const {
61
*localM = *fLocalMatrix;
72
void SkShader::setLocalMatrix(const SkMatrix& localM) {
73
if (localM.isIdentity()) {
74
this->resetLocalMatrix();
76
if (fLocalMatrix == NULL) {
77
fLocalMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix));
79
*fLocalMatrix = localM;
83
void SkShader::resetLocalMatrix() {
85
sk_free(fLocalMatrix);
90
bool SkShader::setContext(const SkBitmap& device,
92
const SkMatrix& matrix) {
93
const SkMatrix* m = &matrix;
96
fDeviceConfig = SkToU8(device.getConfig());
97
fPaintAlpha = paint.getAlpha();
99
total.setConcat(matrix, *fLocalMatrix);
102
if (m->invert(&fTotalInverse)) {
103
fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
109
#include "SkColorPriv.h"
111
void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
114
SkASSERT(this->canCallShadeSpan16());
116
// basically, if we get here, the subclass screwed up
117
SkASSERT(!"kHasSpan16 flag is set, but shadeSpan16() not implemented");
120
#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
121
#define kTempColorCount (kTempColorQuadCount << 2)
123
#ifdef SK_CPU_BENDIAN
124
#define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
126
#define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
129
void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
132
SkPMColor colors[kTempColorCount];
134
while ((count -= kTempColorCount) >= 0) {
135
this->shadeSpan(x, y, colors, kTempColorCount);
136
x += kTempColorCount;
138
const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
139
int quads = kTempColorQuadCount;
146
*alpha++ = SkToU8(a0);
147
*alpha++ = SkToU8(a1);
148
*alpha++ = SkToU8(a2);
149
*alpha++ = SkToU8(a3);
150
} while (--quads != 0);
153
SkASSERT(count + kTempColorCount >= 0);
154
if (count += kTempColorCount) {
155
this->shadeSpan(x, y, colors, count);
157
const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
161
} while (--count != 0);
166
if (n > kTempColorCount)
170
this->shadeSpan(x, y, colors, n);
174
const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
183
SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
184
MatrixClass mc = kLinear_MatrixClass;
186
if (mat.getType() & SkMatrix::kPerspective_Mask) {
187
if (mat.fixedStepInX(0, NULL, NULL)) {
188
mc = kFixedStepInX_MatrixClass;
190
mc = kPerspective_MatrixClass;
196
//////////////////////////////////////////////////////////////////////////////
198
bool SkShader::asABitmap(SkBitmap*, SkMatrix*, TileMode*) {
202
SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
203
TileMode tmx, TileMode tmy) {
204
return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
207
//////////////////////////////////////////////////////////////////////////////
209
#include "SkColorShader.h"
212
SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
213
fInheritColor = b.readU8();
217
fColor = b.readU32();
220
void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) {
221
this->INHERITED::flatten(buffer);
222
buffer.write8(fInheritColor);
226
buffer.write32(fColor);
229
uint32_t SkColorShader::getFlags() {
230
return (SkGetPackedA32(fPMColor) == 255 ? kOpaqueAlpha_Flag : 0) |
234
uint8_t SkColorShader::getSpan16Alpha() const {
235
return SkGetPackedA32(fPMColor);
238
bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
239
const SkMatrix& matrix) {
240
if (!this->INHERITED::setContext(device, paint, matrix)) {
248
c = paint.getColor();
252
a = SkAlphaMul(SkColorGetA(c), SkAlpha255To256(paint.getAlpha()));
255
unsigned r = SkColorGetR(c);
256
unsigned g = SkColorGetG(c);
257
unsigned b = SkColorGetB(c);
259
// we want this before we apply any alpha
260
fColor16 = SkPack888ToRGB16(r, g, b);
263
a = SkAlpha255To256(a);
264
r = SkAlphaMul(r, a);
265
g = SkAlphaMul(g, a);
266
b = SkAlphaMul(b, a);
268
fPMColor = SkPackARGB32(a, r, g, b);
273
void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
274
sk_memset32(span, fPMColor, count);
277
void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
278
sk_memset16(span, fColor16, count);
281
void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
282
memset(alpha, SkGetPackedA32(fPMColor), count);