1/* libs/pixelflinger/picker.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#include <stdio.h>
20
21#include "buffer.h"
22#include "scanline.h"
23#include "picker.h"
24
25namespace android {
26
27// ----------------------------------------------------------------------------
28
29void ggl_init_picker(context_t* /*c*/)
30{
31}
32
33void ggl_pick(context_t* c)
34{
35    if (ggl_likely(!c->dirty))
36        return;
37
38    // compute needs, see if they changed...
39    const uint32_t enables = c->state.enables;
40    needs_t new_needs(c->state.needs);
41
42    if (c->dirty & GGL_CB_STATE) {
43        new_needs.n &= ~GGL_NEEDS_CB_FORMAT_MASK;
44        new_needs.n |= GGL_BUILD_NEEDS(c->state.buffers.color.format, CB_FORMAT);
45        if (enables & GGL_ENABLE_BLENDING)
46            c->dirty |= GGL_PIXEL_PIPELINE_STATE;
47    }
48
49    if (c->dirty & GGL_PIXEL_PIPELINE_STATE) {
50        uint32_t n = GGL_BUILD_NEEDS(c->state.buffers.color.format, CB_FORMAT);
51        uint32_t p = 0;
52        if (enables & GGL_ENABLE_BLENDING) {
53            uint32_t src = c->state.blend.src;
54            uint32_t dst = c->state.blend.dst;
55            uint32_t src_alpha = c->state.blend.src_alpha;
56            uint32_t dst_alpha = c->state.blend.dst_alpha;
57            const GGLFormat& cbf = c->formats[ c->state.buffers.color.format ];
58            if (!cbf.c[GGLFormat::ALPHA].h) {
59                if ((src == GGL_ONE_MINUS_DST_ALPHA) ||
60                    (src == GGL_DST_ALPHA)) {
61                    src = GGL_ONE;
62                }
63                if ((src_alpha == GGL_ONE_MINUS_DST_ALPHA) ||
64                    (src_alpha == GGL_DST_ALPHA)) {
65                    src_alpha = GGL_ONE;
66                }
67                if ((dst == GGL_ONE_MINUS_DST_ALPHA) ||
68                    (dst == GGL_DST_ALPHA)) {
69                    dst = GGL_ONE;
70                }
71                if ((dst_alpha == GGL_ONE_MINUS_DST_ALPHA) ||
72                    (dst_alpha == GGL_DST_ALPHA)) {
73                    dst_alpha = GGL_ONE;
74                }
75            }
76
77            src       = ggl_blendfactor_to_needs(src);
78            dst       = ggl_blendfactor_to_needs(dst);
79            src_alpha = ggl_blendfactor_to_needs(src_alpha);
80            dst_alpha = ggl_blendfactor_to_needs(dst_alpha);
81
82            n |= GGL_BUILD_NEEDS( src, BLEND_SRC );
83            n |= GGL_BUILD_NEEDS( dst, BLEND_DST );
84            if (c->state.blend.alpha_separate) {
85                n |= GGL_BUILD_NEEDS( src_alpha, BLEND_SRCA );
86                n |= GGL_BUILD_NEEDS( dst_alpha, BLEND_DSTA );
87            } else {
88                n |= GGL_BUILD_NEEDS( src, BLEND_SRCA );
89                n |= GGL_BUILD_NEEDS( dst, BLEND_DSTA );
90            }
91        } else {
92            n |= GGL_BUILD_NEEDS( GGL_ONE,  BLEND_SRC );
93            n |= GGL_BUILD_NEEDS( GGL_ZERO, BLEND_DST );
94            n |= GGL_BUILD_NEEDS( GGL_ONE,  BLEND_SRCA );
95            n |= GGL_BUILD_NEEDS( GGL_ZERO, BLEND_DSTA );
96        }
97
98
99        n |= GGL_BUILD_NEEDS(c->state.mask.color^0xF,               MASK_ARGB);
100        n |= GGL_BUILD_NEEDS((enables & GGL_ENABLE_SMOOTH)  ?1:0,   SHADE);
101        if (enables & GGL_ENABLE_TMUS) {
102            n |= GGL_BUILD_NEEDS((enables & GGL_ENABLE_W)       ?1:0,   W);
103        }
104        p |= GGL_BUILD_NEEDS((enables & GGL_ENABLE_DITHER)  ?1:0,   P_DITHER);
105        p |= GGL_BUILD_NEEDS((enables & GGL_ENABLE_AA)      ?1:0,   P_AA);
106        p |= GGL_BUILD_NEEDS((enables & GGL_ENABLE_FOG)     ?1:0,   P_FOG);
107
108        if (enables & GGL_ENABLE_LOGIC_OP) {
109            n |= GGL_BUILD_NEEDS(c->state.logic_op.opcode, LOGIC_OP);
110        } else {
111            n |= GGL_BUILD_NEEDS(GGL_COPY, LOGIC_OP);
112        }
113
114        if (enables & GGL_ENABLE_ALPHA_TEST) {
115            p |= GGL_BUILD_NEEDS(c->state.alpha_test.func, P_ALPHA_TEST);
116        } else {
117            p |= GGL_BUILD_NEEDS(GGL_ALWAYS, P_ALPHA_TEST);
118        }
119
120        if (enables & GGL_ENABLE_DEPTH_TEST) {
121            p |= GGL_BUILD_NEEDS(c->state.depth_test.func, P_DEPTH_TEST);
122            p |= GGL_BUILD_NEEDS(c->state.mask.depth&1, P_MASK_Z);
123        } else {
124            p |= GGL_BUILD_NEEDS(GGL_ALWAYS, P_DEPTH_TEST);
125            // writing to the z-buffer is always disabled if depth-test
126            // is disabled.
127        }
128        new_needs.n = n;
129        new_needs.p = p;
130    }
131
132    if (c->dirty & GGL_TMU_STATE) {
133        int idx = 0;
134        for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; ++i) {
135            const texture_t& tx = c->state.texture[i];
136            if (tx.enable) {
137                uint32_t t = 0;
138                t |= GGL_BUILD_NEEDS(tx.surface.format, T_FORMAT);
139                t |= GGL_BUILD_NEEDS(ggl_env_to_needs(tx.env), T_ENV);
140                t |= GGL_BUILD_NEEDS(0, T_POT);       // XXX: not used yet
141                if (tx.s_coord==GGL_ONE_TO_ONE && tx.t_coord==GGL_ONE_TO_ONE) {
142                    // we encode 1-to-1 into the wrap mode
143                    t |= GGL_BUILD_NEEDS(GGL_NEEDS_WRAP_11, T_S_WRAP);
144                    t |= GGL_BUILD_NEEDS(GGL_NEEDS_WRAP_11, T_T_WRAP);
145                } else {
146                    t |= GGL_BUILD_NEEDS(ggl_wrap_to_needs(tx.s_wrap), T_S_WRAP);
147                    t |= GGL_BUILD_NEEDS(ggl_wrap_to_needs(tx.t_wrap), T_T_WRAP);
148                }
149                if (tx.mag_filter == GGL_LINEAR) {
150                    t |= GGL_BUILD_NEEDS(1, T_LINEAR);
151                }
152                if (tx.min_filter == GGL_LINEAR) {
153                    t |= GGL_BUILD_NEEDS(1, T_LINEAR);
154                }
155                new_needs.t[idx++] = t;
156            } else {
157                new_needs.t[i] = 0;
158            }
159        }
160    }
161
162    if (new_needs != c->state.needs) {
163        c->state.needs = new_needs;
164        ggl_pick_texture(c);
165        ggl_pick_cb(c);
166        ggl_pick_scanline(c);
167    }
168    c->dirty = 0;
169}
170
171// ----------------------------------------------------------------------------
172}; // namespace android
173
174