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