blob: cedb23341e1d47ca8bbcd2812aef48409e45143f [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
Chris Craik117bdbc2015-02-05 10:12:38 -080017#include "renderstate/Stencil.h"
18
19#include "Caches.h"
Romain Guyb7b93e02013-08-01 15:29:25 -070020#include "Debug.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080021#include "Extensions.h"
Romain Guy0baaac52012-08-31 20:31:01 -070022#include "Properties.h"
Romain Guy0baaac52012-08-31 20:31:01 -070023
Romain Guy3bbacf22013-02-06 16:51:04 -080024#include <GLES2/gl2ext.h>
25
Romain Guy0baaac52012-08-31 20:31:01 -070026namespace android {
27namespace uirenderer {
28
Romain Guy3bbacf22013-02-06 16:51:04 -080029#if DEBUG_STENCIL
30#define STENCIL_WRITE_VALUE 0xff
31#define STENCIL_MASK_VALUE 0xff
32#else
33#define STENCIL_WRITE_VALUE 0x1
34#define STENCIL_MASK_VALUE 0x1
35#endif
36
Rob Tsuk487a92c2015-01-06 13:22:54 -080037Stencil::Stencil()
38 : mState(kDisabled) {
Romain Guy0baaac52012-08-31 20:31:01 -070039}
40
John Reck23b797a2014-01-03 18:08:34 -080041uint8_t Stencil::getStencilSize() {
Romain Guy0baaac52012-08-31 20:31:01 -070042 return STENCIL_BUFFER_SIZE;
43}
44
Romain Guy3bbacf22013-02-06 16:51:04 -080045GLenum Stencil::getSmallestStencilFormat() {
46#if !DEBUG_STENCIL
Chris Craik117bdbc2015-02-05 10:12:38 -080047 const Extensions& extensions = Caches::getInstance().extensions();
Romain Guy3bbacf22013-02-06 16:51:04 -080048 if (extensions.has1BitStencil()) {
49 return GL_STENCIL_INDEX1_OES;
50 } else if (extensions.has4BitStencil()) {
51 return GL_STENCIL_INDEX4_OES;
52 }
53#endif
54 return GL_STENCIL_INDEX8;
55}
56
Romain Guy0baaac52012-08-31 20:31:01 -070057void Stencil::clear() {
58 glClearStencil(0);
59 glClear(GL_STENCIL_BUFFER_BIT);
60}
61
Rob Tsuk487a92c2015-01-06 13:22:54 -080062void Stencil::enableTest(int incrementThreshold) {
Romain Guy0baaac52012-08-31 20:31:01 -070063 if (mState != kTest) {
64 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080065 if (incrementThreshold > 0) {
66 glStencilFunc(GL_EQUAL, incrementThreshold, 0xff);
67 } else {
68 glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
69 }
Romain Guy0baaac52012-08-31 20:31:01 -070070 // We only want to test, let's keep everything
71 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Romain Guyf7e52d92012-09-21 15:06:52 -070072 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080073 glStencilMask(0);
Romain Guy0baaac52012-08-31 20:31:01 -070074 mState = kTest;
75 }
76}
77
Rob Tsuk487a92c2015-01-06 13:22:54 -080078void Stencil::enableWrite(int incrementThreshold) {
Romain Guy0baaac52012-08-31 20:31:01 -070079 if (mState != kWrite) {
80 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080081 if (incrementThreshold > 0) {
82 glStencilFunc(GL_ALWAYS, 1, 0xff);
83 // The test always passes so the first two values are meaningless
84 glStencilOp(GL_INCR, GL_INCR, GL_INCR);
85 } else {
86 glStencilFunc(GL_ALWAYS, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
87 // The test always passes so the first two values are meaningless
88 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
89 }
Romain Guyf7e52d92012-09-21 15:06:52 -070090 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080091 glStencilMask(0xff);
Romain Guy0baaac52012-08-31 20:31:01 -070092 mState = kWrite;
93 }
94}
95
Romain Guy7c450aa2012-09-21 19:15:00 -070096void Stencil::enableDebugTest(GLint value, bool greater) {
97 enable();
98 glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff);
99 // We only want to test, let's keep everything
100 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
101 mState = kTest;
102}
103
104void Stencil::enableDebugWrite() {
105 if (mState != kWrite) {
106 enable();
107 glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
108 // The test always passes so the first two values are meaningless
109 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
110 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
111 mState = kWrite;
112 }
113}
114
Romain Guy0baaac52012-08-31 20:31:01 -0700115void Stencil::enable() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700116 if (mState == kDisabled) {
Romain Guy0baaac52012-08-31 20:31:01 -0700117 glEnable(GL_STENCIL_TEST);
118 }
119}
120
121void Stencil::disable() {
122 if (mState != kDisabled) {
123 glDisable(GL_STENCIL_TEST);
124 mState = kDisabled;
125 }
126}
127
Chris Craik117bdbc2015-02-05 10:12:38 -0800128void Stencil::dump() {
129 ALOGD("Stencil: state %d", mState);
130}
131
Romain Guy0baaac52012-08-31 20:31:01 -0700132}; // namespace uirenderer
133}; // namespace android