blob: 319cfe4ba0d031d18bdbbb40720feef8c55831e0 [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
Chris Craike1450132015-04-28 17:55:50 -070045/**
46 * This method will return either GL_STENCIL_INDEX4_OES if supported,
47 * GL_STENCIL_INDEX8 if not.
48 *
49 * Layers can't use a single bit stencil because multi-rect ClipArea needs a high enough
50 * stencil resolution to represent the summation of multiple intersecting rect geometries.
51 */
52GLenum Stencil::getLayerStencilFormat() {
Romain Guy3bbacf22013-02-06 16:51:04 -080053#if !DEBUG_STENCIL
Chris Craik117bdbc2015-02-05 10:12:38 -080054 const Extensions& extensions = Caches::getInstance().extensions();
Chris Craike1450132015-04-28 17:55:50 -070055 if (extensions.has4BitStencil()) {
Romain Guy3bbacf22013-02-06 16:51:04 -080056 return GL_STENCIL_INDEX4_OES;
57 }
58#endif
59 return GL_STENCIL_INDEX8;
60}
61
Romain Guy0baaac52012-08-31 20:31:01 -070062void Stencil::clear() {
Chris Craikfa51a0e2015-07-30 11:05:16 -070063 glStencilMask(0xff);
Romain Guy0baaac52012-08-31 20:31:01 -070064 glClearStencil(0);
65 glClear(GL_STENCIL_BUFFER_BIT);
Chris Craikfa51a0e2015-07-30 11:05:16 -070066
67 if (mState == kTest) {
68 // reset to test state, with immutable stencil
69 glStencilMask(0);
70 }
Romain Guy0baaac52012-08-31 20:31:01 -070071}
72
Rob Tsuk487a92c2015-01-06 13:22:54 -080073void Stencil::enableTest(int incrementThreshold) {
Romain Guy0baaac52012-08-31 20:31:01 -070074 if (mState != kTest) {
75 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080076 if (incrementThreshold > 0) {
77 glStencilFunc(GL_EQUAL, incrementThreshold, 0xff);
78 } else {
79 glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
80 }
Romain Guy0baaac52012-08-31 20:31:01 -070081 // We only want to test, let's keep everything
82 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Romain Guyf7e52d92012-09-21 15:06:52 -070083 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Rob Tsuk487a92c2015-01-06 13:22:54 -080084 glStencilMask(0);
Romain Guy0baaac52012-08-31 20:31:01 -070085 mState = kTest;
86 }
87}
88
Rob Tsuk487a92c2015-01-06 13:22:54 -080089void Stencil::enableWrite(int incrementThreshold) {
Romain Guy0baaac52012-08-31 20:31:01 -070090 if (mState != kWrite) {
91 enable();
Rob Tsuk487a92c2015-01-06 13:22:54 -080092 if (incrementThreshold > 0) {
93 glStencilFunc(GL_ALWAYS, 1, 0xff);
94 // The test always passes so the first two values are meaningless
95 glStencilOp(GL_INCR, GL_INCR, GL_INCR);
96 } else {
97 glStencilFunc(GL_ALWAYS, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE);
98 // The test always passes so the first two values are meaningless
99 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
100 }
Romain Guyf7e52d92012-09-21 15:06:52 -0700101 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800102 glStencilMask(0xff);
Romain Guy0baaac52012-08-31 20:31:01 -0700103 mState = kWrite;
104 }
105}
106
Romain Guy7c450aa2012-09-21 19:15:00 -0700107void Stencil::enableDebugTest(GLint value, bool greater) {
108 enable();
109 glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff);
110 // We only want to test, let's keep everything
111 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
112 mState = kTest;
Chris Craikfa51a0e2015-07-30 11:05:16 -0700113 glStencilMask(0);
Romain Guy7c450aa2012-09-21 19:15:00 -0700114}
115
116void Stencil::enableDebugWrite() {
Chris Craikfa51a0e2015-07-30 11:05:16 -0700117 enable();
118 glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
119 // The test always passes so the first two values are meaningless
120 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
121 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
122 mState = kWrite;
123 glStencilMask(0xff);
Romain Guy7c450aa2012-09-21 19:15:00 -0700124}
125
Romain Guy0baaac52012-08-31 20:31:01 -0700126void Stencil::enable() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700127 if (mState == kDisabled) {
Romain Guy0baaac52012-08-31 20:31:01 -0700128 glEnable(GL_STENCIL_TEST);
129 }
130}
131
132void Stencil::disable() {
133 if (mState != kDisabled) {
134 glDisable(GL_STENCIL_TEST);
135 mState = kDisabled;
136 }
137}
138
Chris Craik117bdbc2015-02-05 10:12:38 -0800139void Stencil::dump() {
140 ALOGD("Stencil: state %d", mState);
141}
142
Romain Guy0baaac52012-08-31 20:31:01 -0700143}; // namespace uirenderer
144}; // namespace android