1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Keith Whitwell <keithw@vmware.com>
31   */
32 
33 
34 #include "main/macros.h"
35 #include "main/framebuffer.h"
36 #include "st_context.h"
37 #include "pipe/p_context.h"
38 #include "st_atom.h"
39 #include "st_util.h"
40 
41 
42 /**
43  * Scissor depends on the scissor box, and the framebuffer dimensions.
44  */
45 void
st_update_scissor(struct st_context * st)46 st_update_scissor( struct st_context *st )
47 {
48    struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
49    const struct gl_context *ctx = st->ctx;
50    const struct gl_framebuffer *fb = ctx->DrawBuffer;
51    const unsigned int fb_width = _mesa_geometric_width(fb);
52    const unsigned int fb_height = _mesa_geometric_height(fb);
53    GLint miny, maxy;
54    unsigned i;
55    bool changed = false;
56 
57    if (!ctx->Scissor.EnableFlags)
58       return;
59 
60    for (i = 0 ; i < st->state.num_viewports; i++) {
61       scissor[i].minx = 0;
62       scissor[i].miny = 0;
63       scissor[i].maxx = fb_width;
64       scissor[i].maxy = fb_height;
65 
66       if (ctx->Scissor.EnableFlags & (1 << i)) {
67          /* need to be careful here with xmax or ymax < 0 */
68          GLint xmax = MAX2(0, ctx->Scissor.ScissorArray[i].X + ctx->Scissor.ScissorArray[i].Width);
69          GLint ymax = MAX2(0, ctx->Scissor.ScissorArray[i].Y + ctx->Scissor.ScissorArray[i].Height);
70 
71          if (ctx->Scissor.ScissorArray[i].X > (GLint)scissor[i].minx)
72             scissor[i].minx = ctx->Scissor.ScissorArray[i].X;
73          if (ctx->Scissor.ScissorArray[i].Y > (GLint)scissor[i].miny)
74             scissor[i].miny = ctx->Scissor.ScissorArray[i].Y;
75 
76          if (xmax < (GLint) scissor[i].maxx)
77             scissor[i].maxx = xmax;
78          if (ymax < (GLint) scissor[i].maxy)
79             scissor[i].maxy = ymax;
80 
81          /* check for null space */
82          if (scissor[i].minx >= scissor[i].maxx || scissor[i].miny >= scissor[i].maxy)
83             scissor[i].minx = scissor[i].miny = scissor[i].maxx = scissor[i].maxy = 0;
84       }
85 
86       /* Now invert Y if needed.
87        * Gallium drivers use the convention Y=0=top for surfaces.
88        */
89       if (st->state.fb_orientation == Y_0_TOP) {
90          miny = fb->Height - scissor[i].maxy;
91          maxy = fb->Height - scissor[i].miny;
92          scissor[i].miny = miny;
93          scissor[i].maxy = maxy;
94       }
95 
96       if (memcmp(&scissor[i], &st->state.scissor[i], sizeof(scissor[0])) != 0) {
97          /* state has changed */
98          st->state.scissor[i] = scissor[i];  /* struct copy */
99          changed = true;
100       }
101    }
102 
103    if (changed) {
104       struct pipe_context *pipe = st->pipe;
105 
106       pipe->set_scissor_states(pipe, 0, st->state.num_viewports, scissor);
107    }
108 }
109 
110 void
st_update_window_rectangles(struct st_context * st)111 st_update_window_rectangles(struct st_context *st)
112 {
113    struct pipe_scissor_state new_rects[PIPE_MAX_WINDOW_RECTANGLES];
114    const struct gl_context *ctx = st->ctx;
115    const struct gl_scissor_attrib *scissor = &ctx->Scissor;
116    unsigned i;
117    bool changed = false;
118    unsigned num_rects = scissor->NumWindowRects;
119    bool include = scissor->WindowRectMode == GL_INCLUSIVE_EXT;
120 
121    if (ctx->DrawBuffer == ctx->WinSysDrawBuffer) {
122       num_rects = 0;
123       include = false;
124    }
125    for (i = 0; i < num_rects; i++) {
126       const struct gl_scissor_rect *rect = &scissor->WindowRects[i];
127       new_rects[i].minx = MAX2(rect->X, 0);
128       new_rects[i].miny = MAX2(rect->Y, 0);
129       new_rects[i].maxx = MAX2(rect->X + rect->Width, 0);
130       new_rects[i].maxy = MAX2(rect->Y + rect->Height, 0);
131    }
132    if (num_rects > 0 && memcmp(new_rects, st->state.window_rects.rects,
133                                num_rects * sizeof(struct pipe_scissor_state))) {
134       memcpy(st->state.window_rects.rects, new_rects,
135              num_rects * sizeof(struct pipe_scissor_state));
136       changed = true;
137    }
138    if (st->state.window_rects.num != num_rects) {
139       st->state.window_rects.num = num_rects;
140       changed = true;
141    }
142    if (st->state.window_rects.include != include) {
143       st->state.window_rects.include = include;
144       changed = true;
145    }
146    if (changed)
147       st->pipe->set_window_rectangles(
148             st->pipe, include, num_rects, new_rects);
149 }
150