blob: f59442196af1984dc2066b10b0717643796c1944 [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
John Reck23b797a2014-01-03 18:08:34 -080037uint8_t Stencil::getStencilSize() {
Romain Guy0baaac52012-08-31 20:31:01 -070038 return STENCIL_BUFFER_SIZE;
39}
40
Chris Craike1450132015-04-28 17:55:50 -070041/**
42 * This method will return either GL_STENCIL_INDEX4_OES if supported,
43 * GL_STENCIL_INDEX8 if not.
44 *
45 * Layers can't use a single bit stencil because multi-rect ClipArea needs a high enough
46 * stencil resolution to represent the summation of multiple intersecting rect geometries.
47 */
48GLenum Stencil::getLayerStencilFormat() {
Romain Guy3bbacf22013-02-06 16:51:04 -080049#if !DEBUG_STENCIL
John Reck8dc02f92017-07-17 09:55:02 -070050 const Extensions& extensions = DeviceInfo::get()->extensions();
Chris Craike1450132015-04-28 17:55:50 -070051 if (extensions.has4BitStencil()) {
Romain Guy3bbacf22013-02-06 16:51:04 -080052 return GL_STENCIL_INDEX4_OES;
53 }
54#endif
55 return GL_STENCIL_INDEX8;
56}
57
Romain Guy0baaac52012-08-31 20:31:01 -070058void Stencil::clear() {
Chris Craikfa51a0e2015-07-30 11:05:16 -070059 glStencilMask(0xff);
Romain Guy0baaac52012-08-31 20:31:01 -070060 glClearStencil(0);
61 glClear(GL_STENCIL_BUFFER_BIT);
Chris Craikfa51a0e2015-07-30 11:05:16 -070062
Chris Craikb9ce116d2015-08-20 15:14:06 -070063 if (mState == StencilState::Test) {
Chris Craikfa51a0e2015-07-30 11:05:16 -070064 // reset to test state, with immutable stencil
65 glStencilMask(0);
66 }
Romain Guy0baaac52012-08-31 20:31:01 -070067}
68
Rob Tsuk487a92c2015-01-06 13:22:54 -080069void Stencil::enableTest(int incrementThreshold) {
Chris Craikb9ce116d2015-08-20 15:14:06 -070070 if (mState != StencilState::Test) {
Romain Guy0baaac52012-08-31 20:31:01 -070071 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080072 if (incrementThreshold > 0) {
73 glStencilFunc(GL_EQUAL, incrementThreshold, 0xff);
74 } else {
75 glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
76 }
Romain Guy0baaac52012-08-31 20:31:01 -070077 // We only want to test, let's keep everything
78 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Romain Guyf7e52d92012-09-21 15:06:52 -070079 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080080 glStencilMask(0);
Chris Craikb9ce116d2015-08-20 15:14:06 -070081 mState = StencilState::Test;
Romain Guy0baaac52012-08-31 20:31:01 -070082 }
83}
84
Rob Tsuk487a92c2015-01-06 13:22:54 -080085void Stencil::enableWrite(int incrementThreshold) {
Chris Craikb9ce116d2015-08-20 15:14:06 -070086 if (mState != StencilState::Write) {
Romain Guy0baaac52012-08-31 20:31:01 -070087 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080088 if (incrementThreshold > 0) {
89 glStencilFunc(GL_ALWAYS, 1, 0xff);
90 // The test always passes so the first two values are meaningless
91 glStencilOp(GL_INCR, GL_INCR, GL_INCR);
92 } else {
93 glStencilFunc(GL_ALWAYS, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
94 // The test always passes so the first two values are meaningless
95 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
96 }
Romain Guyf7e52d92012-09-21 15:06:52 -070097 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080098 glStencilMask(0xff);
Chris Craikb9ce116d2015-08-20 15:14:06 -070099 mState = StencilState::Write;
Romain Guy0baaac52012-08-31 20:31:01 -0700100 }
101}
102
Romain Guy7c450aa2012-09-21 19:15:00 -0700103void Stencil::enableDebugTest(GLint value, bool greater) {
104 enable();
105 glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff);
106 // We only want to test, let's keep everything
107 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700108 mState = StencilState::Test;
Chris Craikfa51a0e2015-07-30 11:05:16 -0700109 glStencilMask(0);
Romain Guy7c450aa2012-09-21 19:15:00 -0700110}
111
112void Stencil::enableDebugWrite() {
Chris Craikfa51a0e2015-07-30 11:05:16 -0700113 enable();
114 glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
115 // The test always passes so the first two values are meaningless
116 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
117 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700118 mState = StencilState::Write;
Chris Craikfa51a0e2015-07-30 11:05:16 -0700119 glStencilMask(0xff);
Romain Guy7c450aa2012-09-21 19:15:00 -0700120}
121
Romain Guy0baaac52012-08-31 20:31:01 -0700122void Stencil::enable() {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700123 if (mState == StencilState::Disabled) {
Romain Guy0baaac52012-08-31 20:31:01 -0700124 glEnable(GL_STENCIL_TEST);
125 }
126}
127
128void Stencil::disable() {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700129 if (mState != StencilState::Disabled) {
Romain Guy0baaac52012-08-31 20:31:01 -0700130 glDisable(GL_STENCIL_TEST);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700131 mState = StencilState::Disabled;
Romain Guy0baaac52012-08-31 20:31:01 -0700132 }
133}
134
Chris Craik117bdbc2015-02-05 10:12:38 -0800135void Stencil::dump() {
136 ALOGD("Stencil: state %d", mState);
137}
138
Romain Guy0baaac52012-08-31 20:31:01 -0700139}; // namespace uirenderer
140}; // namespace android