blob: 61dd8c3200a4c22c90238325e06337a954560277 [file] [log] [blame]
Chris Craik65fe5ee2015-01-26 18:06:29 -08001/*
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 */
Chris Craik96a5c4c2015-01-27 15:46:35 -080016#include "renderstate/Scissor.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080017
Chris Craike4db79d2015-12-22 16:32:23 -080018#include "Rect.h"
19
Chris Craik117bdbc2015-02-05 10:12:38 -080020#include <utils/Log.h>
21
Chris Craik65fe5ee2015-01-26 18:06:29 -080022namespace 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
Chris Craike4db79d2015-12-22 16:32:23 -080076void 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
Chris Craik65fe5ee2015-01-26 18:06:29 -080096void 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
Chris Craik117bdbc2015-02-05 10:12:38 -0800106void Scissor::dump() {
107 ALOGD("Scissor: enabled %d, %d %d %d %d",
108 mEnabled, mScissorX, mScissorY, mScissorWidth, mScissorHeight);
109}
110
Chris Craik65fe5ee2015-01-26 18:06:29 -0800111} /* namespace uirenderer */
112} /* namespace android */
113