1/*
2 * Copyright (C) 2012 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
17#include <GLES2/gl2.h>
18
19#include "Properties.h"
20#include "Stencil.h"
21
22namespace android {
23namespace uirenderer {
24
25Stencil::Stencil(): mState(kDisabled) {
26}
27
28uint32_t Stencil::getStencilSize() {
29    return STENCIL_BUFFER_SIZE;
30}
31
32void Stencil::clear() {
33    glClearStencil(0);
34    glClear(GL_STENCIL_BUFFER_BIT);
35}
36
37void Stencil::enableTest() {
38    if (mState != kTest) {
39        enable();
40        glStencilFunc(GL_EQUAL, 0x1, 0x1);
41        // We only want to test, let's keep everything
42        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
43        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
44        mState = kTest;
45    }
46}
47
48void Stencil::enableWrite() {
49    if (mState != kWrite) {
50        enable();
51        glStencilFunc(GL_ALWAYS, 0x1, 0x1);
52        // The test always passes so the first two values are meaningless
53        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
54        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
55        mState = kWrite;
56    }
57}
58
59void Stencil::enableDebugTest(GLint value, bool greater) {
60    enable();
61    glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff);
62    // We only want to test, let's keep everything
63    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
64    mState = kTest;
65}
66
67void Stencil::enableDebugWrite() {
68    if (mState != kWrite) {
69        enable();
70        glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
71        // The test always passes so the first two values are meaningless
72        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
73        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
74        mState = kWrite;
75    }
76}
77
78void Stencil::enable() {
79    if (mState == kDisabled) {
80        glEnable(GL_STENCIL_TEST);
81    }
82}
83
84void Stencil::disable() {
85    if (mState != kDisabled) {
86        glDisable(GL_STENCIL_TEST);
87        mState = kDisabled;
88    }
89}
90
91}; // namespace uirenderer
92}; // namespace android
93