Stencil.cpp revision 0baaac5e9adf3ee280ae1239e2e58754a9d2b099
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_LESS, 0x0, 0x1);
41        // We only want to test, let's keep everything
42        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
43        mState = kTest;
44    }
45}
46
47void Stencil::enableWrite() {
48    if (mState != kWrite) {
49        enable();
50        glStencilFunc(GL_ALWAYS, 0x1, 0x1);
51        // The test always passes so the first two values are meaningless
52        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
53        mState = kWrite;
54    }
55}
56
57void Stencil::enable() {
58    if (!mState == kDisabled) {
59        glEnable(GL_STENCIL_TEST);
60    }
61}
62
63void Stencil::disable() {
64    if (mState != kDisabled) {
65        glDisable(GL_STENCIL_TEST);
66        mState = kDisabled;
67    }
68}
69
70}; // namespace uirenderer
71}; // namespace android
72