croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 1 | /* libs/graphics/sgl/SkShader.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, Google Inc. |
| 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 | |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 18 | #include "SkShader.h" |
| 19 | #include "SkPaint.h" |
| 20 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 21 | SkShader::SkShader() : fLocalMatrix(NULL) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
| 25 | SkShader::~SkShader() |
| 26 | { |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 27 | sk_free(fLocalMatrix); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void SkShader::setLocalMatrix(const SkMatrix& matrix) |
| 31 | { |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 32 | if (matrix.isIdentity()) |
| 33 | { |
| 34 | if (fLocalMatrix) |
| 35 | { |
| 36 | sk_free(fLocalMatrix); |
| 37 | fLocalMatrix = NULL; |
| 38 | } |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | if (fLocalMatrix == NULL) |
| 43 | fLocalMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix)); |
| 44 | *fLocalMatrix = matrix; |
| 45 | } |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool SkShader::setContext(const SkBitmap& device, |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 49 | const SkPaint& paint, |
| 50 | const SkMatrix& matrix) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 51 | { |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 52 | const SkMatrix* m = &matrix; |
| 53 | SkMatrix total; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 54 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 55 | fDeviceConfig = SkToU8(device.getConfig()); |
| 56 | fPaintAlpha = paint.getAlpha(); |
| 57 | if (fLocalMatrix) |
| 58 | { |
| 59 | total.setConcat(matrix, *fLocalMatrix); |
| 60 | m = &total; |
| 61 | } |
| 62 | if (m->invert(&fTotalInverse)) |
| 63 | { |
| 64 | fInverseMapPtProc = fTotalInverse.getMapPtProc(); |
| 65 | fTotalInverseClass = (uint8_t)SkShader::ComputeMatrixClass(fTotalInverse); |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | #include "SkColorPriv.h" |
| 72 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 73 | void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 74 | { |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 75 | SkASSERT(span16); |
| 76 | SkASSERT(count > 0); |
| 77 | SkASSERT(this->canCallShadeSpan16()); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 78 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 79 | // basically, if we get here, the subclass screwed up |
| 80 | SkASSERT(!"kHasSpan16 flag is set, but shadeSpan16() not implemented"); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 81 | } |
| 82 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 83 | #define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space |
| 84 | #define kTempColorCount (kTempColorQuadCount << 2) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 85 | |
| 86 | #ifdef SK_CPU_BENDIAN |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 87 | #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 88 | #else |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 89 | #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 92 | void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 93 | { |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 94 | SkASSERT(count > 0); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 95 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 96 | SkPMColor colors[kTempColorCount]; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 97 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 98 | while ((count -= kTempColorCount) >= 0) |
| 99 | { |
| 100 | this->shadeSpan(x, y, colors, kTempColorCount); |
| 101 | x += kTempColorCount; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 102 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 103 | const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT); |
| 104 | int quads = kTempColorQuadCount; |
| 105 | do { |
| 106 | U8CPU a0 = srcA[0]; |
| 107 | U8CPU a1 = srcA[4]; |
| 108 | U8CPU a2 = srcA[8]; |
| 109 | U8CPU a3 = srcA[12]; |
| 110 | srcA += 4*4; |
| 111 | *alpha++ = SkToU8(a0); |
| 112 | *alpha++ = SkToU8(a1); |
| 113 | *alpha++ = SkToU8(a2); |
| 114 | *alpha++ = SkToU8(a3); |
| 115 | } while (--quads != 0); |
| 116 | } |
| 117 | SkASSERT(count < 0); |
| 118 | SkASSERT(count + kTempColorCount >= 0); |
| 119 | if (count += kTempColorCount) |
| 120 | { |
| 121 | this->shadeSpan(x, y, colors, count); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 122 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 123 | const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT); |
| 124 | do { |
| 125 | *alpha++ = *srcA; |
| 126 | srcA += 4; |
| 127 | } while (--count != 0); |
| 128 | } |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 129 | #if 0 |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 130 | do { |
| 131 | int n = count; |
| 132 | if (n > kTempColorCount) |
| 133 | n = kTempColorCount; |
| 134 | SkASSERT(n > 0); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 135 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 136 | this->shadeSpan(x, y, colors, n); |
| 137 | x += n; |
| 138 | count -= n; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 139 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 140 | const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT); |
| 141 | do { |
| 142 | *alpha++ = *srcA; |
| 143 | srcA += 4; |
| 144 | } while (--n != 0); |
| 145 | } while (count > 0); |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 146 | #endif |
| 147 | } |
| 148 | |
| 149 | SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) |
| 150 | { |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 151 | MatrixClass mc = kLinear_MatrixClass; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 152 | |
croachrose | 48ede9b | 2006-11-08 19:24:48 +0000 | [diff] [blame^] | 153 | if (mat.getType() & SkMatrix::kPerspective_Mask) |
| 154 | { |
| 155 | if (mat.fixedStepInX(0, NULL, NULL)) |
| 156 | mc = kFixedStepInX_MatrixClass; |
| 157 | else |
| 158 | mc = kPerspective_MatrixClass; |
| 159 | } |
| 160 | return mc; |
croachrose | 0f87cd8 | 2006-09-20 15:47:42 +0000 | [diff] [blame] | 161 | } |
| 162 | |