165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik/*
265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * Copyright (C) 2015 The Android Open Source Project
365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik *
465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * you may not use this file except in compliance with the License.
665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * You may obtain a copy of the License at
765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik *
865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik *
1065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * Unless required by applicable law or agreed to in writing, software
1165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
1265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * See the License for the specific language governing permissions and
1465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik * limitations under the License.
1565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik */
1696a5c4c7bab6718524de7253da8309143ab48befChris Craik#include "renderstate/Scissor.h"
1765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
18e4db79de127cfe961195f52907af8451026eaa20Chris Craik#include "Rect.h"
19e4db79de127cfe961195f52907af8451026eaa20Chris Craik
20117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik#include <utils/Log.h>
21117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
2265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craiknamespace android {
2365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craiknamespace uirenderer {
2465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
2565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris CraikScissor::Scissor()
2665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    : mEnabled(false)
2765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    , mScissorX(0)
2865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    , mScissorY(0)
2965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    , mScissorWidth(0)
3065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    , mScissorHeight(0) {
3165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik}
3265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
3365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craikbool Scissor::setEnabled(bool enabled) {
3465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    if (mEnabled != enabled) {
3565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        if (enabled) {
3665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            glEnable(GL_SCISSOR_TEST);
3765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        } else {
3865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            glDisable(GL_SCISSOR_TEST);
3965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        }
4065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        mEnabled = enabled;
4165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        return true;
4265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    }
4365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    return false;
4465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik}
4565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
4665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craikbool Scissor::set(GLint x, GLint y, GLint width, GLint height) {
4765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    if (mEnabled && (x != mScissorX || y != mScissorY
4865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            || width != mScissorWidth || height != mScissorHeight)) {
4965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
5065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        if (x < 0) {
5165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            width += x;
5265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            x = 0;
5365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        }
5465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        if (y < 0) {
5565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            height += y;
5665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            y = 0;
5765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        }
5865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        if (width < 0) {
5965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            width = 0;
6065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        }
6165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        if (height < 0) {
6265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            height = 0;
6365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        }
6465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        glScissor(x, y, width, height);
6565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
6665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        mScissorX = x;
6765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        mScissorY = y;
6865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        mScissorWidth = width;
6965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        mScissorHeight = height;
7065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
7165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik        return true;
7265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    }
7365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    return false;
7465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik}
7565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
76e4db79de127cfe961195f52907af8451026eaa20Chris Craikvoid Scissor::set(int viewportHeight, const Rect& clip) {
77e4db79de127cfe961195f52907af8451026eaa20Chris Craik    // transform to Y-flipped GL space, and prevent negatives
78e4db79de127cfe961195f52907af8451026eaa20Chris Craik    GLint x = std::max(0, (int)clip.left);
79e4db79de127cfe961195f52907af8451026eaa20Chris Craik    GLint y = std::max(0, viewportHeight - (int)clip.bottom);
80e4db79de127cfe961195f52907af8451026eaa20Chris Craik    GLint width = std::max(0, ((int)clip.right) - x);
81e4db79de127cfe961195f52907af8451026eaa20Chris Craik    GLint height = std::max(0, (viewportHeight - (int)clip.top) - y);
82e4db79de127cfe961195f52907af8451026eaa20Chris Craik
83e4db79de127cfe961195f52907af8451026eaa20Chris Craik    if (x != mScissorX
84e4db79de127cfe961195f52907af8451026eaa20Chris Craik            || y != mScissorY
85e4db79de127cfe961195f52907af8451026eaa20Chris Craik            || width != mScissorWidth
86e4db79de127cfe961195f52907af8451026eaa20Chris Craik            || height != mScissorHeight) {
87e4db79de127cfe961195f52907af8451026eaa20Chris Craik        glScissor(x, y, width, height);
88e4db79de127cfe961195f52907af8451026eaa20Chris Craik
89e4db79de127cfe961195f52907af8451026eaa20Chris Craik        mScissorX = x;
90e4db79de127cfe961195f52907af8451026eaa20Chris Craik        mScissorY = y;
91e4db79de127cfe961195f52907af8451026eaa20Chris Craik        mScissorWidth = width;
92e4db79de127cfe961195f52907af8451026eaa20Chris Craik        mScissorHeight = height;
93e4db79de127cfe961195f52907af8451026eaa20Chris Craik    }
94e4db79de127cfe961195f52907af8451026eaa20Chris Craik}
95e4db79de127cfe961195f52907af8451026eaa20Chris Craik
9665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craikvoid Scissor::reset() {
9765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
9865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik}
9965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
10065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craikvoid Scissor::invalidate() {
10165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    mEnabled = glIsEnabled(GL_SCISSOR_TEST);
10265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    setEnabled(true);
10365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    reset();
10465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik}
10565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
106117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craikvoid Scissor::dump() {
107117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    ALOGD("Scissor: enabled %d, %d %d %d %d",
108117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik            mEnabled, mScissorX, mScissorY, mScissorWidth, mScissorHeight);
109117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik}
110117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
11165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik} /* namespace uirenderer */
11265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik} /* namespace android */
11365fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
114