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