epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkTransparentShader.h" |
| 11 | #include "SkColorPriv.h" |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
| 14 | bool SkTransparentShader::setContext(const SkBitmap& device, |
| 15 | const SkPaint& paint, |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 16 | const SkMatrix& matrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | fDevice = &device; |
| 18 | fAlpha = paint.getAlpha(); |
| 19 | |
| 20 | return this->INHERITED::setContext(device, paint, matrix); |
| 21 | } |
| 22 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 23 | uint32_t SkTransparentShader::getFlags() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | uint32_t flags = this->INHERITED::getFlags(); |
| 25 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 26 | switch (fDevice->config()) { |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 27 | case SkBitmap::kRGB_565_Config: |
| 28 | flags |= kHasSpan16_Flag; |
| 29 | if (fAlpha == 255) |
| 30 | flags |= kOpaqueAlpha_Flag; |
| 31 | break; |
| 32 | case SkBitmap::kARGB_8888_Config: |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 33 | if (fAlpha == 255 && fDevice->isOpaque()) |
| 34 | flags |= kOpaqueAlpha_Flag; |
| 35 | break; |
| 36 | default: |
| 37 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | return flags; |
| 40 | } |
| 41 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 42 | void SkTransparentShader::shadeSpan(int x, int y, SkPMColor span[], int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | unsigned scale = SkAlpha255To256(fAlpha); |
| 44 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 45 | switch (fDevice->config()) { |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 46 | case SkBitmap::kARGB_8888_Config: |
| 47 | if (scale == 256) { |
senorblanco@chromium.org | e4c98ff | 2011-05-20 19:06:10 +0000 | [diff] [blame] | 48 | SkPMColor* src = fDevice->getAddr32(x, y); |
| 49 | if (src != span) { |
| 50 | memcpy(span, src, count * sizeof(SkPMColor)); |
| 51 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 52 | } else { |
| 53 | const SkPMColor* src = fDevice->getAddr32(x, y); |
| 54 | for (int i = count - 1; i >= 0; --i) { |
| 55 | span[i] = SkAlphaMulQ(src[i], scale); |
| 56 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 58 | break; |
| 59 | case SkBitmap::kRGB_565_Config: { |
| 60 | const uint16_t* src = fDevice->getAddr16(x, y); |
| 61 | if (scale == 256) { |
| 62 | for (int i = count - 1; i >= 0; --i) { |
| 63 | span[i] = SkPixel16ToPixel32(src[i]); |
| 64 | } |
| 65 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | unsigned alpha = fAlpha; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 67 | for (int i = count - 1; i >= 0; --i) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | uint16_t c = src[i]; |
| 69 | unsigned r = SkPacked16ToR32(c); |
| 70 | unsigned g = SkPacked16ToG32(c); |
| 71 | unsigned b = SkPacked16ToB32(c); |
| 72 | |
| 73 | span[i] = SkPackARGB32( alpha, |
| 74 | SkAlphaMul(r, scale), |
| 75 | SkAlphaMul(g, scale), |
| 76 | SkAlphaMul(b, scale)); |
| 77 | } |
| 78 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 79 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 81 | case SkBitmap::kIndex8_Config: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 82 | SkDEBUGFAIL("index8 not supported as a destination device"); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 83 | break; |
| 84 | case SkBitmap::kA8_Config: { |
| 85 | const uint8_t* src = fDevice->getAddr8(x, y); |
| 86 | if (scale == 256) { |
| 87 | for (int i = count - 1; i >= 0; --i) { |
| 88 | span[i] = SkPackARGB32(src[i], 0, 0, 0); |
| 89 | } |
| 90 | } else { |
| 91 | for (int i = count - 1; i >= 0; --i) { |
| 92 | span[i] = SkPackARGB32(SkAlphaMul(src[i], scale), 0, 0, 0); |
| 93 | } |
| 94 | } |
| 95 | break; |
| 96 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 97 | default: // to avoid warnings |
| 98 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 102 | void SkTransparentShader::shadeSpan16(int x, int y, uint16_t span[], int count) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 103 | SkASSERT(fDevice->config() == SkBitmap::kRGB_565_Config); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | |
senorblanco@chromium.org | e4c98ff | 2011-05-20 19:06:10 +0000 | [diff] [blame] | 105 | uint16_t* src = fDevice->getAddr16(x, y); |
| 106 | if (src != span) { |
| 107 | memcpy(span, src, count << 1); |
| 108 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | } |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 110 | |
| 111 | #ifdef SK_DEVELOPER |
| 112 | void SkTransparentShader::toString(SkString* str) const { |
| 113 | str->append("SkTransparentShader: ("); |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 114 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 115 | this->INHERITED::toString(str); |
| 116 | |
| 117 | str->append(")"); |
| 118 | } |
| 119 | #endif |