1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkPaintPriv.h"
9 
10 #include "SkBitmap.h"
11 #include "SkColorFilter.h"
12 #include "SkImage.h"
13 #include "SkPaint.h"
14 #include "SkShader.h"
15 
isPaintOpaque(const SkPaint * paint,SkPaintBitmapOpacity contentType)16 bool isPaintOpaque(const SkPaint* paint, SkPaintBitmapOpacity contentType) {
17     if (!paint) {
18         return contentType != kUnknown_SkPaintBitmapOpacity;
19     }
20     SkXfermode::SrcColorOpacity opacityType = SkXfermode::kUnknown_SrcColorOpacity;
21 
22     if (!paint->getColorFilter() ||
23         ((paint->getColorFilter()->getFlags() &
24           SkColorFilter::kAlphaUnchanged_Flag) != 0)) {
25         if (0xff == paint->getAlpha() &&
26             contentType != kUnknown_SkPaintBitmapOpacity &&
27             (!paint->getShader() || paint->getShader()->isOpaque())) {
28             opacityType = SkXfermode::kOpaque_SrcColorOpacity;
29         } else if (0 == paint->getColor() &&
30                    contentType == kNoBitmap_SkPaintBitmapOpacity &&
31                    !paint->getShader()) {
32             opacityType = SkXfermode::kTransparentBlack_SrcColorOpacity;
33         } else if (0 == paint->getAlpha()) {
34             opacityType = SkXfermode::kTransparentAlpha_SrcColorOpacity;
35         }
36     }
37 
38     return SkXfermode::IsOpaque(paint->getXfermode(), opacityType);
39 }
40 
isPaintOpaque(const SkPaint * paint,const SkBitmap * bmpReplacesShader)41 bool isPaintOpaque(const SkPaint* paint, const SkBitmap* bmpReplacesShader) {
42     SkPaintBitmapOpacity contentType;
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 }
53 
isPaintOpaque(const SkPaint * paint,const SkImage * image)54 bool isPaintOpaque(const SkPaint* paint, const SkImage* image) {
55     return isPaintOpaque(paint, image->isOpaque() ?
56                          kOpaque_SkPaintBitmapOpacity : kUnknown_SkPaintBitmapOpacity);
57 }
58