1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <renderstate/Blend.h>
17 #include "Program.h"
18
19 #include "ShadowTessellator.h"
20
21 namespace android {
22 namespace uirenderer {
23
24 /**
25 * Structure mapping Skia xfermodes to OpenGL blending factors.
26 */
27 struct Blender {
28 SkBlendMode mode;
29 GLenum src;
30 GLenum dst;
31 };
32
33 // assumptions made by lookup tables in either this file or ProgramCache
34 static_assert(0 == static_cast<int>(SkBlendMode::kClear), "SkBlendMode enums have changed");
35 static_assert(1 == static_cast<int>(SkBlendMode::kSrc), "SkBlendMode enums have changed");
36 static_assert(2 == static_cast<int>(SkBlendMode::kDst), "SkBlendMode enums have changed");
37 static_assert(3 == static_cast<int>(SkBlendMode::kSrcOver), "SkBlendMode enums have changed");
38 static_assert(4 == static_cast<int>(SkBlendMode::kDstOver), "SkBlendMode enums have changed");
39 static_assert(5 == static_cast<int>(SkBlendMode::kSrcIn), "SkBlendMode enums have changed");
40 static_assert(6 == static_cast<int>(SkBlendMode::kDstIn), "SkBlendMode enums have changed");
41 static_assert(7 == static_cast<int>(SkBlendMode::kSrcOut), "SkBlendMode enums have changed");
42 static_assert(8 == static_cast<int>(SkBlendMode::kDstOut), "SkBlendMode enums have changed");
43 static_assert(9 == static_cast<int>(SkBlendMode::kSrcATop), "SkBlendMode enums have changed");
44 static_assert(10 == static_cast<int>(SkBlendMode::kDstATop), "SkBlendMode enums have changed");
45 static_assert(11 == static_cast<int>(SkBlendMode::kXor), "SkBlendMode enums have changed");
46 static_assert(12 == static_cast<int>(SkBlendMode::kPlus), "SkBlendMode enums have changed");
47 static_assert(13 == static_cast<int>(SkBlendMode::kModulate), "SkBlendMode enums have changed");
48 static_assert(14 == static_cast<int>(SkBlendMode::kScreen), "SkBlendMode enums have changed");
49 static_assert(15 == static_cast<int>(SkBlendMode::kOverlay), "SkBlendMode enums have changed");
50 static_assert(16 == static_cast<int>(SkBlendMode::kDarken), "SkBlendMode enums have changed");
51 static_assert(17 == static_cast<int>(SkBlendMode::kLighten), "SkBlendMode enums have changed");
52
53 // In this array, the index of each Blender equals the value of the first
54 // entry. For instance, gBlends[1] == gBlends[SkBlendMode::kSrc]
55 const Blender kBlends[] = {
56 { SkBlendMode::kClear, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
57 { SkBlendMode::kSrc, GL_ONE, GL_ZERO },
58 { SkBlendMode::kDst, GL_ZERO, GL_ONE },
59 { SkBlendMode::kSrcOver, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
60 { SkBlendMode::kDstOver, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
61 { SkBlendMode::kSrcIn, GL_DST_ALPHA, GL_ZERO },
62 { SkBlendMode::kDstIn, GL_ZERO, GL_SRC_ALPHA },
63 { SkBlendMode::kSrcOut, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
64 { SkBlendMode::kDstOut, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
65 { SkBlendMode::kSrcATop, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
66 { SkBlendMode::kDstATop, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
67 { SkBlendMode::kXor, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
68 { SkBlendMode::kPlus, GL_ONE, GL_ONE },
69 { SkBlendMode::kModulate, GL_ZERO, GL_SRC_COLOR },
70 { SkBlendMode::kScreen, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
71 };
72
73 // This array contains the swapped version of each SkBlendMode. For instance
74 // this array's SrcOver blending mode is actually DstOver. You can refer to
75 // createLayer() for more information on the purpose of this array.
76 const Blender kBlendsSwap[] = {
77 { SkBlendMode::kClear, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkBlendMode::kSrc, GL_ZERO, GL_ONE },
79 { SkBlendMode::kDst, GL_ONE, GL_ZERO },
80 { SkBlendMode::kSrcOver, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
81 { SkBlendMode::kDstOver, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
82 { SkBlendMode::kSrcIn, GL_ZERO, GL_SRC_ALPHA },
83 { SkBlendMode::kDstIn, GL_DST_ALPHA, GL_ZERO },
84 { SkBlendMode::kSrcOut, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
85 { SkBlendMode::kDstOut, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
86 { SkBlendMode::kSrcATop, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
87 { SkBlendMode::kDstATop, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
88 { SkBlendMode::kXor, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
89 { SkBlendMode::kPlus, GL_ONE, GL_ONE },
90 { SkBlendMode::kModulate, GL_DST_COLOR, GL_ZERO },
91 { SkBlendMode::kScreen, GL_ONE_MINUS_DST_COLOR, GL_ONE }
92 };
93
Blend()94 Blend::Blend()
95 : mEnabled(false)
96 , mSrcMode(GL_ZERO)
97 , mDstMode(GL_ZERO) {
98 // gl blending off by default
99 }
100
invalidate()101 void Blend::invalidate() {
102 syncEnabled();
103 mSrcMode = mDstMode = GL_ZERO;
104 }
105
syncEnabled()106 void Blend::syncEnabled() {
107 if (mEnabled) {
108 glEnable(GL_BLEND);
109 } else {
110 glDisable(GL_BLEND);
111 }
112 }
113
getFactors(SkBlendMode mode,ModeOrderSwap modeUsage,GLenum * outSrc,GLenum * outDst)114 void Blend::getFactors(SkBlendMode mode, ModeOrderSwap modeUsage, GLenum* outSrc, GLenum* outDst) {
115 int index = static_cast<int>(mode);
116 *outSrc = (modeUsage == ModeOrderSwap::Swap) ? kBlendsSwap[index].src : kBlends[index].src;
117 *outDst = (modeUsage == ModeOrderSwap::Swap) ? kBlendsSwap[index].dst : kBlends[index].dst;
118 }
119
setFactors(GLenum srcMode,GLenum dstMode)120 void Blend::setFactors(GLenum srcMode, GLenum dstMode) {
121 if ((srcMode == GL_ZERO || srcMode == GL_ONE) && dstMode == GL_ZERO) {
122 // disable blending
123 if (mEnabled) {
124 glDisable(GL_BLEND);
125 mEnabled = false;
126 }
127 } else {
128 // enable blending
129 if (!mEnabled) {
130 glEnable(GL_BLEND);
131 mEnabled = true;
132 }
133
134 if (srcMode != mSrcMode || dstMode != mDstMode) {
135 glBlendFunc(srcMode, dstMode);
136 mSrcMode = srcMode;
137 mDstMode = dstMode;
138 }
139 }
140 }
141
dump()142 void Blend::dump() {
143 ALOGD("Blend: enabled %d, func src %d, dst %d", mEnabled, mSrcMode, mDstMode);
144 }
145
146 } /* namespace uirenderer */
147 } /* namespace android */
148
149