blob: acbed1480acfbea74921293d400d7890c580ae98 [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 Guyb7b93e02013-08-01 15:29:25 -070017#include "Debug.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080018#include "Extensions.h"
Romain Guy0baaac52012-08-31 20:31:01 -070019#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#include "renderstate/Stencil.h"
Romain Guy0baaac52012-08-31 20:31:01 -070021
Romain Guy3bbacf22013-02-06 16:51:04 -080022#include <GLES2/gl2ext.h>
23
Romain Guy0baaac52012-08-31 20:31:01 -070024namespace android {
25namespace uirenderer {
26
Romain Guy3bbacf22013-02-06 16:51:04 -080027#if DEBUG_STENCIL
28#define STENCIL_WRITE_VALUE 0xff
29#define STENCIL_MASK_VALUE 0xff
30#else
31#define STENCIL_WRITE_VALUE 0x1
32#define STENCIL_MASK_VALUE 0x1
33#endif
34
Rob Tsuk487a92c2015-01-06 13:22:54 -080035Stencil::Stencil()
36 : mState(kDisabled) {
Romain Guy0baaac52012-08-31 20:31:01 -070037}
38
John Reck23b797a2014-01-03 18:08:34 -080039uint8_t Stencil::getStencilSize() {
Romain Guy0baaac52012-08-31 20:31:01 -070040 return STENCIL_BUFFER_SIZE;
41}
42
Romain Guy3bbacf22013-02-06 16:51:04 -080043GLenum Stencil::getSmallestStencilFormat() {
44#if !DEBUG_STENCIL
45 const Extensions& extensions = Extensions::getInstance();
46 if (extensions.has1BitStencil()) {
47 return GL_STENCIL_INDEX1_OES;
48 } else if (extensions.has4BitStencil()) {
49 return GL_STENCIL_INDEX4_OES;
50 }
51#endif
52 return GL_STENCIL_INDEX8;
53}
54
Romain Guy0baaac52012-08-31 20:31:01 -070055void Stencil::clear() {
56 glClearStencil(0);
57 glClear(GL_STENCIL_BUFFER_BIT);
58}
59
Rob Tsuk487a92c2015-01-06 13:22:54 -080060void Stencil::enableTest(int incrementThreshold) {
Romain Guy0baaac52012-08-31 20:31:01 -070061 if (mState != kTest) {
62 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080063 if (incrementThreshold > 0) {
64 glStencilFunc(GL_EQUAL, incrementThreshold, 0xff);
65 } else {
66 glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
67 }
Romain Guy0baaac52012-08-31 20:31:01 -070068 // We only want to test, let's keep everything
69 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Romain Guyf7e52d92012-09-21 15:06:52 -070070 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080071 glStencilMask(0);
Romain Guy0baaac52012-08-31 20:31:01 -070072 mState = kTest;
73 }
74}
75
Rob Tsuk487a92c2015-01-06 13:22:54 -080076void Stencil::enableWrite(int incrementThreshold) {
Romain Guy0baaac52012-08-31 20:31:01 -070077 if (mState != kWrite) {
78 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080079 if (incrementThreshold > 0) {
80 glStencilFunc(GL_ALWAYS, 1, 0xff);
81 // The test always passes so the first two values are meaningless
82 glStencilOp(GL_INCR, GL_INCR, GL_INCR);
83 } else {
84 glStencilFunc(GL_ALWAYS, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
85 // The test always passes so the first two values are meaningless
86 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
87 }
Romain Guyf7e52d92012-09-21 15:06:52 -070088 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080089 glStencilMask(0xff);
Romain Guy0baaac52012-08-31 20:31:01 -070090 mState = kWrite;
91 }
92}
93
Romain Guy7c450aa2012-09-21 19:15:00 -070094void Stencil::enableDebugTest(GLint value, bool greater) {
95 enable();
96 glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff);
97 // We only want to test, let's keep everything
98 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
99 mState = kTest;
100}
101
102void Stencil::enableDebugWrite() {
103 if (mState != kWrite) {
104 enable();
105 glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
106 // The test always passes so the first two values are meaningless
107 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
108 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
109 mState = kWrite;
110 }
111}
112
Romain Guy0baaac52012-08-31 20:31:01 -0700113void Stencil::enable() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700114 if (mState == kDisabled) {
Romain Guy0baaac52012-08-31 20:31:01 -0700115 glEnable(GL_STENCIL_TEST);
116 }
117}
118
119void Stencil::disable() {
120 if (mState != kDisabled) {
121 glDisable(GL_STENCIL_TEST);
122 mState = kDisabled;
123 }
124}
125
126}; // namespace uirenderer
127}; // namespace android