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 | |
Romain Guy | b7b93e0 | 2013-08-01 15:29:25 -0700 | [diff] [blame] | 17 | #include "Debug.h" |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 18 | #include "Extensions.h" |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 19 | #include "Properties.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 20 | #include "renderstate/Stencil.h" |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 21 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 22 | #include <GLES2/gl2ext.h> |
| 23 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 27 | #if DEBUG_STENCIL |
| 28 | #define STENCIL_WRITE_VALUE 0xff |
| 29 | #define STENCIL_MASK_VALUE 0xff |
| 30 | #else |
| 31 | #define STENCIL_WRITE_VALUE 0x1 |
| 32 | #define STENCIL_MASK_VALUE 0x1 |
| 33 | #endif |
| 34 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 35 | Stencil::Stencil() |
| 36 | : mState(kDisabled) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 37 | } |
| 38 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 39 | uint8_t Stencil::getStencilSize() { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 40 | return STENCIL_BUFFER_SIZE; |
| 41 | } |
| 42 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 43 | GLenum Stencil::getSmallestStencilFormat() { |
| 44 | #if !DEBUG_STENCIL |
| 45 | const Extensions& extensions = Extensions::getInstance(); |
| 46 | if (extensions.has1BitStencil()) { |
| 47 | return GL_STENCIL_INDEX1_OES; |
| 48 | } else if (extensions.has4BitStencil()) { |
| 49 | return GL_STENCIL_INDEX4_OES; |
| 50 | } |
| 51 | #endif |
| 52 | return GL_STENCIL_INDEX8; |
| 53 | } |
| 54 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 55 | void Stencil::clear() { |
| 56 | glClearStencil(0); |
| 57 | glClear(GL_STENCIL_BUFFER_BIT); |
| 58 | } |
| 59 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 60 | void Stencil::enableTest(int incrementThreshold) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 61 | if (mState != kTest) { |
| 62 | enable(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 63 | if (incrementThreshold > 0) { |
| 64 | glStencilFunc(GL_EQUAL, incrementThreshold, 0xff); |
| 65 | } else { |
| 66 | glStencilFunc(GL_EQUAL, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE); |
| 67 | } |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 68 | // We only want to test, let's keep everything |
| 69 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
Romain Guy | f7e52d9 | 2012-09-21 15:06:52 -0700 | [diff] [blame] | 70 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 71 | glStencilMask(0); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 72 | mState = kTest; |
| 73 | } |
| 74 | } |
| 75 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 76 | void Stencil::enableWrite(int incrementThreshold) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 77 | if (mState != kWrite) { |
| 78 | enable(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 79 | if (incrementThreshold > 0) { |
| 80 | glStencilFunc(GL_ALWAYS, 1, 0xff); |
| 81 | // The test always passes so the first two values are meaningless |
| 82 | glStencilOp(GL_INCR, GL_INCR, GL_INCR); |
| 83 | } else { |
| 84 | glStencilFunc(GL_ALWAYS, STENCIL_WRITE_VALUE, STENCIL_MASK_VALUE); |
| 85 | // The test always passes so the first two values are meaningless |
| 86 | glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); |
| 87 | } |
Romain Guy | f7e52d9 | 2012-09-21 15:06:52 -0700 | [diff] [blame] | 88 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 89 | glStencilMask(0xff); |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 90 | mState = kWrite; |
| 91 | } |
| 92 | } |
| 93 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 94 | void Stencil::enableDebugTest(GLint value, bool greater) { |
| 95 | enable(); |
| 96 | glStencilFunc(greater ? GL_LESS : GL_EQUAL, value, 0xffffffff); |
| 97 | // We only want to test, let's keep everything |
| 98 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 99 | mState = kTest; |
| 100 | } |
| 101 | |
| 102 | void Stencil::enableDebugWrite() { |
| 103 | if (mState != kWrite) { |
| 104 | enable(); |
| 105 | glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff); |
| 106 | // The test always passes so the first two values are meaningless |
| 107 | glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); |
| 108 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 109 | mState = kWrite; |
| 110 | } |
| 111 | } |
| 112 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 113 | void Stencil::enable() { |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 114 | if (mState == kDisabled) { |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 115 | glEnable(GL_STENCIL_TEST); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void Stencil::disable() { |
| 120 | if (mState != kDisabled) { |
| 121 | glDisable(GL_STENCIL_TEST); |
| 122 | mState = kDisabled; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | }; // namespace uirenderer |
| 127 | }; // namespace android |