blob: e95c2c8ab14da6b61f5fc059195d2aa80702da09 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* 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,
Ashok Bhat3078b132014-02-17 15:15:46 +000054 GGLsizei width, GGLsizei height, GGLenum /*type*/)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055{
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
Jim Huang6e1a9432010-08-10 02:56:47 +0800146GGLint gglBitBlit(GGLContext* con, int tmu, GGLint crop[4], GGLint where[4])
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147{
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 c->procs.activeTexture(c, tmu);
157 c->procs.disable(c, GGL_W_LERP);
158
159 uint32_t tmus = 1UL<<tmu;
160 if (c->state.enabled_tmu != tmus) {
161 c->activeTMU->enable = 1;
162 c->state.enabled_tmu = tmus;
163 c->state.enables |= GGL_ENABLE_TMUS;
164 ggl_state_changed(c, GGL_TMU_STATE);
165 }
166
167 const GGLint Wcr = crop[2];
168 const GGLint Hcr = crop[3];
169 if ((w == Wcr) && (h == Hcr)) {
170 c->procs.texGeni(c, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
171 c->procs.texGeni(c, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
172 const GGLint Ucr = crop[0];
173 const GGLint Vcr = crop[1];
174 const GGLint s0 = Ucr - x;
175 const GGLint t0 = Vcr - y;
176 c->procs.texCoord2i(c, s0, t0);
177 c->procs.recti(c, x, y, x+w, y+h);
178 } else {
179 int32_t texcoords[8];
180 x = gglIntToFixed(x);
181 y = gglIntToFixed(y);
182
183 // we CLAMP here, which works with premultiplied (s,t)
184 c->procs.texParameteri(c, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_S, GGL_CLAMP);
185 c->procs.texParameteri(c, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP);
186 c->procs.texGeni(c, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
187 c->procs.texGeni(c, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
188
189 const GGLint Ucr = crop[0] << 16;
190 const GGLint Vcr = crop[1] << 16;
191 const GGLint Wcr = crop[2] << 16;
192 const GGLint Hcr = crop[3] << 16;
193
194 // computes texture coordinates (pre-multiplied)
195 int32_t dsdx = Wcr / w; // dsdx = ((Wcr/w)/Wt)*Wt
196 int32_t dtdy = Hcr / h; // dtdy = ((Hcr/h)/Ht)*Ht
197 int32_t s0 = Ucr - gglMulx(dsdx, x); // s0 = Ucr - x * dsdx
198 int32_t t0 = Vcr - gglMulx(dtdy, y); // t0 = Vcr - y * dtdy
199 texcoords[0] = s0;
200 texcoords[1] = dsdx;
201 texcoords[2] = 0;
202 texcoords[3] = t0;
203 texcoords[4] = 0;
204 texcoords[5] = dtdy;
205 texcoords[6] = 0;
206 texcoords[7] = 0;
207 c->procs.texCoordGradScale8xv(c, tmu, texcoords);
208 c->procs.recti(c,
209 gglFixedToIntRound(x),
210 gglFixedToIntRound(y),
211 gglFixedToIntRound(x)+w,
212 gglFixedToIntRound(y)+h);
213 }
214 return 0;
215}
216