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