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