OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkPaintPriv.h" | 8 #include "SkPaintPriv.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkColorFilter.h" | 11 #include "SkColorFilter.h" |
12 #include "SkImage.h" | 12 #include "SkImage.h" |
13 #include "SkPaint.h" | 13 #include "SkPaint.h" |
14 #include "SkShader.h" | 14 #include "SkShader.h" |
15 | 15 |
16 bool isPaintOpaque(const SkPaint* paint, SkPaintBitmapOpacity contentType) { | 16 enum ShaderOverrideOpacity { |
| 17 kNone_ShaderOverrideOpacity, //!< there is no overriding shader (bitm
ap or image) |
| 18 kOpaque_ShaderOverrideOpacity, //!< the overriding shader is opaque |
| 19 kNotOpaque_ShaderOverrideOpacity, //!< the overriding shader may not be op
aque |
| 20 }; |
| 21 |
| 22 static bool changes_alpha(const SkPaint& paint) { |
| 23 SkColorFilter* cf = paint.getColorFilter(); |
| 24 return cf && !(cf->getFlags() & SkColorFilter::kAlphaUnchanged_Flag); |
| 25 } |
| 26 |
| 27 static bool overwrites(const SkPaint* paint, ShaderOverrideOpacity overrideOpaci
ty) { |
17 if (!paint) { | 28 if (!paint) { |
18 return contentType != kUnknown_SkPaintBitmapOpacity; | 29 // No paint means we default to SRC_OVER, so we overwrite iff our shader
-override |
| 30 // is opaque, or we don't have one. |
| 31 return overrideOpacity != kNotOpaque_ShaderOverrideOpacity; |
19 } | 32 } |
| 33 |
20 SkXfermode::SrcColorOpacity opacityType = SkXfermode::kUnknown_SrcColorOpaci
ty; | 34 SkXfermode::SrcColorOpacity opacityType = SkXfermode::kUnknown_SrcColorOpaci
ty; |
21 | 35 |
22 if (!paint->getColorFilter() || | 36 if (!changes_alpha(*paint)) { |
23 ((paint->getColorFilter()->getFlags() & | 37 const unsigned paintAlpha = paint->getAlpha(); |
24 SkColorFilter::kAlphaUnchanged_Flag) != 0)) { | 38 if (0xff == paintAlpha && overrideOpacity != kNotOpaque_ShaderOverrideOp
acity && |
25 if (0xff == paint->getAlpha() && | 39 (!paint->getShader() || paint->getShader()->isOpaque())) |
26 contentType != kUnknown_SkPaintBitmapOpacity && | 40 { |
27 (!paint->getShader() || paint->getShader()->isOpaque())) { | |
28 opacityType = SkXfermode::kOpaque_SrcColorOpacity; | 41 opacityType = SkXfermode::kOpaque_SrcColorOpacity; |
29 } else if (0 == paint->getColor() && | 42 } else if (0 == paintAlpha) { |
30 contentType == kNoBitmap_SkPaintBitmapOpacity && | 43 if (overrideOpacity == kNone_ShaderOverrideOpacity && !paint->getSha
der()) { |
31 !paint->getShader()) { | 44 opacityType = SkXfermode::kTransparentBlack_SrcColorOpacity; |
32 opacityType = SkXfermode::kTransparentBlack_SrcColorOpacity; | 45 } else { |
33 } else if (0 == paint->getAlpha()) { | 46 opacityType = SkXfermode::kTransparentAlpha_SrcColorOpacity; |
34 opacityType = SkXfermode::kTransparentAlpha_SrcColorOpacity; | 47 } |
35 } | 48 } |
36 } | 49 } |
37 | 50 |
38 return SkXfermode::IsOpaque(paint->getXfermode(), opacityType); | 51 return SkXfermode::IsOpaque(paint->getXfermode(), opacityType); |
39 } | 52 } |
40 | 53 |
41 bool isPaintOpaque(const SkPaint* paint, const SkBitmap* bmpReplacesShader) { | 54 bool SkPaintPriv::Overwrites(const SkPaint& paint) { |
42 SkPaintBitmapOpacity contentType; | 55 return overwrites(&paint, kNone_ShaderOverrideOpacity); |
43 | |
44 if(!bmpReplacesShader) | |
45 contentType = kNoBitmap_SkPaintBitmapOpacity; | |
46 else if(bmpReplacesShader->isOpaque()) | |
47 contentType = kOpaque_SkPaintBitmapOpacity; | |
48 else | |
49 contentType = kUnknown_SkPaintBitmapOpacity; | |
50 | |
51 return isPaintOpaque(paint, contentType); | |
52 } | 56 } |
53 | 57 |
54 bool isPaintOpaque(const SkPaint* paint, const SkImage* image) { | 58 bool SkPaintPriv::Overwrites(const SkBitmap& bitmap, const SkPaint* paint) { |
55 return isPaintOpaque(paint, image->isOpaque() ? | 59 return overwrites(paint, bitmap.isOpaque() ? kOpaque_ShaderOverrideOpacity |
56 kOpaque_SkPaintBitmapOpacity : kUnknown_SkPaintBitmapOp
acity); | 60 : kNotOpaque_ShaderOverrideOpacit
y); |
57 } | 61 } |
| 62 |
| 63 bool SkPaintPriv::Overwrites(const SkImage* image, const SkPaint* paint) { |
| 64 return overwrites(paint, image->isOpaque() ? kOpaque_ShaderOverrideOpacity |
| 65 : kNotOpaque_ShaderOverrideOpacit
y); |
| 66 } |
OLD | NEW |