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 "Rect.h"
19
20#include <utils/Log.h>
21
22namespace android {
23namespace uirenderer {
24
25Scissor::Scissor()
26    : mEnabled(false)
27    , mScissorX(0)
28    , mScissorY(0)
29    , mScissorWidth(0)
30    , mScissorHeight(0) {
31}
32
33bool Scissor::setEnabled(bool enabled) {
34    if (mEnabled != enabled) {
35        if (enabled) {
36            glEnable(GL_SCISSOR_TEST);
37        } else {
38            glDisable(GL_SCISSOR_TEST);
39        }
40        mEnabled = enabled;
41        return true;
42    }
43    return false;
44}
45
46bool Scissor::set(GLint x, GLint y, GLint width, GLint height) {
47    if (mEnabled && (x != mScissorX || y != mScissorY
48            || width != mScissorWidth || height != mScissorHeight)) {
49
50        if (x < 0) {
51            width += x;
52            x = 0;
53        }
54        if (y < 0) {
55            height += y;
56            y = 0;
57        }
58        if (width < 0) {
59            width = 0;
60        }
61        if (height < 0) {
62            height = 0;
63        }
64        glScissor(x, y, width, height);
65
66        mScissorX = x;
67        mScissorY = y;
68        mScissorWidth = width;
69        mScissorHeight = height;
70
71        return true;
72    }
73    return false;
74}
75
76void Scissor::set(int viewportHeight, const Rect& clip) {
77    // transform to Y-flipped GL space, and prevent negatives
78    GLint x = std::max(0, (int)clip.left);
79    GLint y = std::max(0, viewportHeight - (int)clip.bottom);
80    GLint width = std::max(0, ((int)clip.right) - x);
81    GLint height = std::max(0, (viewportHeight - (int)clip.top) - y);
82
83    if (x != mScissorX
84            || y != mScissorY
85            || width != mScissorWidth
86            || height != mScissorHeight) {
87        glScissor(x, y, width, height);
88
89        mScissorX = x;
90        mScissorY = y;
91        mScissorWidth = width;
92        mScissorHeight = height;
93    }
94}
95
96void Scissor::reset() {
97    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
98}
99
100void Scissor::invalidate() {
101    mEnabled = glIsEnabled(GL_SCISSOR_TEST);
102    setEnabled(true);
103    reset();
104}
105
106void Scissor::dump() {
107    ALOGD("Scissor: enabled %d, %d %d %d %d",
108            mEnabled, mScissorX, mScissorY, mScissorWidth, mScissorHeight);
109}
110
111} /* namespace uirenderer */
112} /* namespace android */
113
114