Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 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 <GLES2/gl2.h> |
| 18 | |
| 19 | #include "Properties.h" |
| 20 | #include "Stencil.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | Stencil::Stencil(): mState(kDisabled) { |
| 26 | } |
| 27 | |
| 28 | uint32_t Stencil::getStencilSize() { |
| 29 | return STENCIL_BUFFER_SIZE; |
| 30 | } |
| 31 | |
| 32 | void Stencil::clear() { |
| 33 | glClearStencil(0); |
| 34 | glClear(GL_STENCIL_BUFFER_BIT); |
| 35 | } |
| 36 | |
| 37 | void Stencil::enableTest() { |
| 38 | if (mState != kTest) { |
| 39 | enable(); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 40 | glStencilFunc(GL_EQUAL, 0x1, 0x1); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 41 | // We only want to test, let's keep everything |
| 42 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
Romain Guy | f7e52d9 | 2012-09-21 15:06:52 -0700 | [diff] [blame] | 43 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 44 | mState = kTest; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void Stencil::enableWrite() { |
| 49 | if (mState != kWrite) { |
| 50 | enable(); |
| 51 | glStencilFunc(GL_ALWAYS, 0x1, 0x1); |
| 52 | // The test always passes so the first two values are meaningless |
| 53 | glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); |
Romain Guy | f7e52d9 | 2012-09-21 15:06:52 -0700 | [diff] [blame] | 54 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 55 | mState = kWrite; |
| 56 | } |
| 57 | } |
| 58 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 59 | void Stencil::enableDebugTest(GLint value, bool greater) { |
| 60 | enable(); |
| 61 | glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff); |
| 62 | // We only want to test, let's keep everything |
| 63 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 64 | mState = kTest; |
| 65 | } |
| 66 | |
| 67 | void Stencil::enableDebugWrite() { |
| 68 | if (mState != kWrite) { |
| 69 | enable(); |
| 70 | glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff); |
| 71 | // The test always passes so the first two values are meaningless |
| 72 | glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); |
| 73 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 74 | mState = kWrite; |
| 75 | } |
| 76 | } |
| 77 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 78 | void Stencil::enable() { |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 79 | if (mState == kDisabled) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 80 | glEnable(GL_STENCIL_TEST); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void Stencil::disable() { |
| 85 | if (mState != kDisabled) { |
| 86 | glDisable(GL_STENCIL_TEST); |
| 87 | mState = kDisabled; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | }; // namespace uirenderer |
| 92 | }; // namespace android |