1/* libs/pixelflinger/raster.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
20#include <string.h>
21
22#include "raster.h"
23#include "trap.h"
24
25namespace android {
26
27static void ggl_rasterPos2x(void* con, GGLfixed x, GGLfixed y);
28static void ggl_rasterPos2i(void* con, GGLint x, GGLint y);
29static void ggl_copyPixels(void* con, GGLint xs, GGLint ys,
30        GGLsizei width, GGLsizei height, GGLenum type);
31
32void ggl_init_raster(context_t* c)
33{
34    GGLContext& procs = *(GGLContext*)c;
35    GGL_INIT_PROC(procs, copyPixels);
36    GGL_INIT_PROC(procs, rasterPos2x);
37    GGL_INIT_PROC(procs, rasterPos2i);
38}
39
40void ggl_rasterPos2x(void* con, GGLfixed x, GGLfixed y)
41{
42    GGL_CONTEXT(c, con);
43    // raster pos should be processed just like glVertex
44    c->state.raster.x = x;
45    c->state.raster.y = y;
46}
47
48void ggl_rasterPos2i(void* con, GGLint x, GGLint y)
49{
50    ggl_rasterPos2x(con, gglIntToFixed(x), gglIntToFixed(y));
51}
52
53void ggl_copyPixels(void* con, GGLint xs, GGLint ys,
54        GGLsizei width, GGLsizei height, GGLenum /*type*/)
55{
56    GGL_CONTEXT(c, con);
57
58    // color-buffer
59    surface_t* cb = &(c->state.buffers.color);
60
61    // undefined behaviour if we try to copy from outside the surface
62    if (uint32_t(xs) > cb->width)
63        return;
64    if (uint32_t(ys) > cb->height)
65        return;
66    if (uint32_t(xs + width) > cb->width)
67        return;
68    if (uint32_t(ys + height) > cb->height)
69        return;
70
71    // copy to current raster position
72    GGLint xd = gglFixedToIntRound(c->state.raster.x);
73    GGLint yd = gglFixedToIntRound(c->state.raster.y);
74
75    // clip to scissor
76    if (xd < GGLint(c->state.scissor.left)) {
77        GGLint offset = GGLint(c->state.scissor.left) - xd;
78        xd = GGLint(c->state.scissor.left);
79        xs += offset;
80        width -= offset;
81    }
82    if (yd < GGLint(c->state.scissor.top)) {
83        GGLint offset = GGLint(c->state.scissor.top) - yd;
84        yd = GGLint(c->state.scissor.top);
85        ys += offset;
86        height -= offset;
87    }
88    if ((xd + width) > GGLint(c->state.scissor.right)) {
89        width = GGLint(c->state.scissor.right) - xd;
90    }
91    if ((yd + height) > GGLint(c->state.scissor.bottom)) {
92        height = GGLint(c->state.scissor.bottom) - yd;
93    }
94
95    if (width<=0 || height<=0) {
96        return; // nothing to copy
97    }
98
99    if (xs==xd && ys==yd) {
100        // nothing to do, but be careful, this might not be true when we support
101        // gglPixelTransfer, gglPixelMap and gglPixelZoom
102        return;
103    }
104
105    const GGLFormat* fp = &(c->formats[cb->format]);
106    uint8_t* src = reinterpret_cast<uint8_t*>(cb->data)
107            + (xs + (cb->stride * ys)) * fp->size;
108    uint8_t* dst = reinterpret_cast<uint8_t*>(cb->data)
109            + (xd + (cb->stride * yd)) * fp->size;
110    const size_t bpr = cb->stride * fp->size;
111    const size_t rowsize = width * fp->size;
112    size_t yc = height;
113
114    if (ys < yd) {
115        // bottom to top
116        src += height * bpr;
117        dst += height * bpr;
118        do {
119            dst -= bpr;
120            src -= bpr;
121            memcpy(dst, src, rowsize);
122        } while (--yc);
123    } else {
124        if (ys == yd) {
125            // might be right to left
126            do {
127                memmove(dst, src, rowsize);
128                dst += bpr;
129                src += bpr;
130            } while (--yc);
131        } else {
132            // top to bottom
133            do {
134                memcpy(dst, src, rowsize);
135                dst += bpr;
136                src += bpr;
137            } while (--yc);
138        }
139    }
140}
141
142}; // namespace android
143
144using namespace android;
145
146GGLint gglBitBlit(GGLContext* con, int tmu, GGLint crop[4], GGLint where[4])
147{
148    GGL_CONTEXT(c, (void*)con);
149
150     GGLint x = where[0];
151     GGLint y = where[1];
152     GGLint w = where[2];
153     GGLint h = where[3];
154
155    // exclsively enable this tmu
156    const GGLSurface& cbSurface = c->state.buffers.color.s;
157    c->procs.activeTexture(c, tmu);
158    c->procs.disable(c, GGL_W_LERP);
159
160    uint32_t tmus = 1UL<<tmu;
161    if (c->state.enabled_tmu != tmus) {
162        c->activeTMU->enable = 1;
163        c->state.enabled_tmu = tmus;
164        c->state.enables |= GGL_ENABLE_TMUS;
165        ggl_state_changed(c, GGL_TMU_STATE);
166    }
167
168    const GGLint Wcr = crop[2];
169    const GGLint Hcr = crop[3];
170    if ((w == Wcr) && (h == Hcr)) {
171        c->procs.texGeni(c, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
172        c->procs.texGeni(c, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
173        const GGLint Ucr = crop[0];
174        const GGLint Vcr = crop[1];
175        const GGLint s0  = Ucr - x;
176        const GGLint t0  = Vcr - y;
177        c->procs.texCoord2i(c, s0, t0);
178        c->procs.recti(c, x, y, x+w, y+h);
179    } else {
180        int32_t texcoords[8];
181        x = gglIntToFixed(x);
182        y = gglIntToFixed(y);
183
184        // we CLAMP here, which works with premultiplied (s,t)
185        c->procs.texParameteri(c, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_S, GGL_CLAMP);
186        c->procs.texParameteri(c, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP);
187        c->procs.texGeni(c, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
188        c->procs.texGeni(c, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
189
190        const GGLint Ucr = crop[0] << 16;
191        const GGLint Vcr = crop[1] << 16;
192        const GGLint Wcr = crop[2] << 16;
193        const GGLint Hcr = crop[3] << 16;
194
195        // computes texture coordinates (pre-multiplied)
196        int32_t dsdx = Wcr / w;   // dsdx = ((Wcr/w)/Wt)*Wt
197        int32_t dtdy = Hcr / h;   // dtdy = ((Hcr/h)/Ht)*Ht
198        int32_t s0   = Ucr - gglMulx(dsdx, x); // s0 = Ucr - x * dsdx
199        int32_t t0   = Vcr - gglMulx(dtdy, y); // t0 = Vcr - y * dtdy
200        texcoords[0] = s0;
201        texcoords[1] = dsdx;
202        texcoords[2] = 0;
203        texcoords[3] = t0;
204        texcoords[4] = 0;
205        texcoords[5] = dtdy;
206        texcoords[6] = 0;
207        texcoords[7] = 0;
208        c->procs.texCoordGradScale8xv(c, tmu, texcoords);
209        c->procs.recti(c,
210                gglFixedToIntRound(x),
211                gglFixedToIntRound(y),
212                gglFixedToIntRound(x)+w,
213                gglFixedToIntRound(y)+h);
214    }
215    return 0;
216}
217
218