blob: ba2e6f2670e32dbfd605c326a64dddb6a97f372a [file] [log] [blame]
Romain Guy0baaac52012-08-31 20:31:01 -07001/*
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
Romain Guy3bbacf22013-02-06 16:51:04 -080017#include "Extensions.h"
Romain Guy0baaac52012-08-31 20:31:01 -070018#include "Properties.h"
19#include "Stencil.h"
20
Romain Guy3bbacf22013-02-06 16:51:04 -080021#include <GLES2/gl2ext.h>
22
Romain Guy0baaac52012-08-31 20:31:01 -070023namespace android {
24namespace uirenderer {
25
Romain Guy3bbacf22013-02-06 16:51:04 -080026#if DEBUG_STENCIL
27#define STENCIL_WRITE_VALUE 0xff
28#define STENCIL_MASK_VALUE 0xff
29#else
30#define STENCIL_WRITE_VALUE 0x1
31#define STENCIL_MASK_VALUE 0x1
32#endif
33
Romain Guy0baaac52012-08-31 20:31:01 -070034Stencil::Stencil(): mState(kDisabled) {
35}
36
37uint32_t Stencil::getStencilSize() {
38 return STENCIL_BUFFER_SIZE;
39}
40
Romain Guy3bbacf22013-02-06 16:51:04 -080041GLenum Stencil::getSmallestStencilFormat() {
42#if !DEBUG_STENCIL
43 const Extensions& extensions = Extensions::getInstance();
44 if (extensions.has1BitStencil()) {
45 return GL_STENCIL_INDEX1_OES;
46 } else if (extensions.has4BitStencil()) {
47 return GL_STENCIL_INDEX4_OES;
48 }
49#endif
50 return GL_STENCIL_INDEX8;
51}
52
Romain Guy0baaac52012-08-31 20:31:01 -070053void Stencil::clear() {
54 glClearStencil(0);
55 glClear(GL_STENCIL_BUFFER_BIT);
56}
57
58void Stencil::enableTest() {
59 if (mState != kTest) {
60 enable();
Romain Guy3bbacf22013-02-06 16:51:04 -080061 glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
Romain Guy0baaac52012-08-31 20:31:01 -070062 // We only want to test, let's keep everything
63 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Romain Guyf7e52d92012-09-21 15:06:52 -070064 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Romain Guy0baaac52012-08-31 20:31:01 -070065 mState = kTest;
66 }
67}
68
69void Stencil::enableWrite() {
70 if (mState != kWrite) {
71 enable();
Romain Guy3bbacf22013-02-06 16:51:04 -080072 glStencilFunc(GL_ALWAYS, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
Romain Guy0baaac52012-08-31 20:31:01 -070073 // The test always passes so the first two values are meaningless
74 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
Romain Guyf7e52d92012-09-21 15:06:52 -070075 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
Romain Guy0baaac52012-08-31 20:31:01 -070076 mState = kWrite;
77 }
78}
79
Romain Guy7c450aa2012-09-21 19:15:00 -070080void Stencil::enableDebugTest(GLint value, bool greater) {
81 enable();
82 glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff);
83 // We only want to test, let's keep everything
84 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
85 mState = kTest;
86}
87
88void Stencil::enableDebugWrite() {
89 if (mState != kWrite) {
90 enable();
91 glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
92 // The test always passes so the first two values are meaningless
93 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
94 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
95 mState = kWrite;
96 }
97}
98
Romain Guy0baaac52012-08-31 20:31:01 -070099void Stencil::enable() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700100 if (mState == kDisabled) {
Romain Guy0baaac52012-08-31 20:31:01 -0700101 glEnable(GL_STENCIL_TEST);
102 }
103}
104
105void Stencil::disable() {
106 if (mState != kDisabled) {
107 glDisable(GL_STENCIL_TEST);
108 mState = kDisabled;
109 }
110}
111
112}; // namespace uirenderer
113}; // namespace android