1/* 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 17#include "renderstate/Stencil.h" 18 19#include "Caches.h" 20#include "Debug.h" 21#include "Extensions.h" 22#include "Properties.h" 23 24#include <GLES2/gl2ext.h> 25 26namespace android { 27namespace uirenderer { 28 29#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 37uint8_t Stencil::getStencilSize() { 38 return STENCIL_BUFFER_SIZE; 39} 40 41/** 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() { 49#if !DEBUG_STENCIL 50 const Extensions& extensions = DeviceInfo::get()->extensions(); 51 if (extensions.has4BitStencil()) { 52 return GL_STENCIL_INDEX4_OES; 53 } 54#endif 55 return GL_STENCIL_INDEX8; 56} 57 58void Stencil::clear() { 59 glStencilMask(0xff); 60 glClearStencil(0); 61 glClear(GL_STENCIL_BUFFER_BIT); 62 63 if (mState == StencilState::Test) { 64 // reset to test state, with immutable stencil 65 glStencilMask(0); 66 } 67} 68 69void Stencil::enableTest(int incrementThreshold) { 70 if (mState != StencilState::Test) { 71 enable(); 72 if (incrementThreshold > 0) { 73 glStencilFunc(GL_EQUAL, incrementThreshold, 0xff); 74 } else { 75 glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE); 76 } 77 // We only want to test, let's keep everything 78 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 79 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 80 glStencilMask(0); 81 mState = StencilState::Test; 82 } 83} 84 85void Stencil::enableWrite(int incrementThreshold) { 86 if (mState != StencilState::Write) { 87 enable(); 88 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 } 97 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 98 glStencilMask(0xff); 99 mState = StencilState::Write; 100 } 101} 102 103void 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); 108 mState = StencilState::Test; 109 glStencilMask(0); 110} 111 112void Stencil::enableDebugWrite() { 113 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); 118 mState = StencilState::Write; 119 glStencilMask(0xff); 120} 121 122void Stencil::enable() { 123 if (mState == StencilState::Disabled) { 124 glEnable(GL_STENCIL_TEST); 125 } 126} 127 128void Stencil::disable() { 129 if (mState != StencilState::Disabled) { 130 glDisable(GL_STENCIL_TEST); 131 mState = StencilState::Disabled; 132 } 133} 134 135void Stencil::dump() { 136 ALOGD("Stencil: state %d", mState); 137} 138 139}; // namespace uirenderer 140}; // namespace android 141