1 /*
2  * Copyright (C) 2015 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 #include "renderstate/Scissor.h"
17 
18 #include <utils/Log.h>
19 
20 namespace android {
21 namespace uirenderer {
22 
Scissor()23 Scissor::Scissor()
24     : mEnabled(false)
25     , mScissorX(0)
26     , mScissorY(0)
27     , mScissorWidth(0)
28     , mScissorHeight(0) {
29 }
30 
setEnabled(bool enabled)31 bool Scissor::setEnabled(bool enabled) {
32     if (mEnabled != enabled) {
33         if (enabled) {
34             glEnable(GL_SCISSOR_TEST);
35         } else {
36             glDisable(GL_SCISSOR_TEST);
37         }
38         mEnabled = enabled;
39         return true;
40     }
41     return false;
42 }
43 
set(GLint x,GLint y,GLint width,GLint height)44 bool Scissor::set(GLint x, GLint y, GLint width, GLint height) {
45     if (mEnabled && (x != mScissorX || y != mScissorY
46             || width != mScissorWidth || height != mScissorHeight)) {
47 
48         if (x < 0) {
49             width += x;
50             x = 0;
51         }
52         if (y < 0) {
53             height += y;
54             y = 0;
55         }
56         if (width < 0) {
57             width = 0;
58         }
59         if (height < 0) {
60             height = 0;
61         }
62         glScissor(x, y, width, height);
63 
64         mScissorX = x;
65         mScissorY = y;
66         mScissorWidth = width;
67         mScissorHeight = height;
68 
69         return true;
70     }
71     return false;
72 }
73 
reset()74 void Scissor::reset() {
75     mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
76 }
77 
invalidate()78 void Scissor::invalidate() {
79     mEnabled = glIsEnabled(GL_SCISSOR_TEST);
80     setEnabled(true);
81     reset();
82 }
83 
dump()84 void Scissor::dump() {
85     ALOGD("Scissor: enabled %d, %d %d %d %d",
86             mEnabled, mScissorX, mScissorY, mScissorWidth, mScissorHeight);
87 }
88 
89 } /* namespace uirenderer */
90 } /* namespace android */
91 
92