blob: 0c3d0eef5cdc6c36522f1d4a02ca826fa7437545 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
2**
3** Copyright 2009, 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#include <stdlib.h>
19#include <stdio.h>
20
21#include "context.h"
22#include "fp.h"
23#include "state.h"
24#include "matrix.h"
25#include "vertex.h"
26#include "light.h"
27#include "primitives.h"
28#include "texture.h"
29#include "BufferObjectManager.h"
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030#include "TextureObjectManager.h"
Mathias Agopian0a3139a2009-06-10 16:01:54 -070031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <hardware/gralloc.h>
33#include <hardware/copybit.h>
Mathias Agopian0a3139a2009-06-10 16:01:54 -070034#include <private/ui/android_natives_priv.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035
Mathias Agopian54ba51d2009-10-26 20:12:37 -070036#include <ui/GraphicBuffer.h>
37#include <ui/Region.h>
38#include <ui/Rect.h>
39
Mathias Agopian6d2cad22009-06-17 21:18:56 -070040
41#define DEBUG_COPYBIT true
42
Mathias Agopian076b1cc2009-04-10 14:24:30 -070043// ----------------------------------------------------------------------------
44
45namespace android {
46
Mathias Agopian0a3139a2009-06-10 16:01:54 -070047static void textureToCopyBitImage(
Mathias Agopian6dbedd72009-10-16 16:17:58 -070048 const GGLSurface* surface, int32_t opFormat,
49 buffer_handle_t buffer, copybit_image_t* img)
Mathias Agopian0a3139a2009-06-10 16:01:54 -070050{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051 img->w = surface->stride;
52 img->h = surface->height;
Mathias Agopian6dbedd72009-10-16 16:17:58 -070053 img->format = opFormat;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054 img->base = surface->data;
Mathias Agopian5911aa92009-06-24 16:55:59 -070055 img->handle = (native_handle_t *)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056}
57
58struct clipRectRegion : public copybit_region_t {
Mathias Agopiancf251b92009-06-16 18:08:29 -070059 clipRectRegion(ogles_context_t* c)
60 {
61 scissor_t const* scissor = &c->rasterizer.state.scissor;
62 r.l = scissor->left;
63 r.t = scissor->top;
64 r.r = scissor->right;
65 r.b = scissor->bottom;
66 next = iterate;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070067 }
68private:
69 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
Mathias Agopiancf251b92009-06-16 18:08:29 -070070 *rect = static_cast<clipRectRegion const*>(self)->r;
71 const_cast<copybit_region_t *>(self)->next = iterate_done;
72 return 1;
73 }
74 static int iterate_done(copybit_region_t const *, copybit_rect_t*) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 return 0;
76 }
Mathias Agopiancf251b92009-06-16 18:08:29 -070077 copybit_rect_t r;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078};
79
80static bool supportedCopybitsFormat(int format) {
81 switch (format) {
82 case COPYBIT_FORMAT_RGBA_8888:
Mathias Agopiandfbec0e2009-08-07 20:55:14 -070083 case COPYBIT_FORMAT_RGBX_8888:
84 case COPYBIT_FORMAT_RGB_888:
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 case COPYBIT_FORMAT_RGB_565:
86 case COPYBIT_FORMAT_BGRA_8888:
87 case COPYBIT_FORMAT_RGBA_5551:
88 case COPYBIT_FORMAT_RGBA_4444:
89 case COPYBIT_FORMAT_YCbCr_422_SP:
90 case COPYBIT_FORMAT_YCbCr_420_SP:
91 return true;
92 default:
93 return false;
94 }
95}
96
97static bool hasAlpha(int format) {
98 switch (format) {
99 case COPYBIT_FORMAT_RGBA_8888:
100 case COPYBIT_FORMAT_BGRA_8888:
101 case COPYBIT_FORMAT_RGBA_5551:
102 case COPYBIT_FORMAT_RGBA_4444:
103 return true;
104 default:
105 return false;
106 }
107}
108
109static inline int fixedToByte(GGLfixed val) {
110 return (val - (val >> 8)) >> 8;
111}
112
113/**
114 * Performs a quick check of the rendering state. If this function returns
115 * false we cannot use the copybit driver.
116 */
117
118static bool checkContext(ogles_context_t* c) {
119
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700120 // By convention copybitQuickCheckContext() has already returned true.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 // avoid checking the same information again.
122
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700123 if (c->copybits.blitEngine == NULL) {
124 LOGD_IF(DEBUG_COPYBIT, "no copybit hal");
125 return false;
126 }
127
128 if (c->rasterizer.state.enables
129 & (GGL_ENABLE_DEPTH_TEST|GGL_ENABLE_FOG)) {
130 LOGD_IF(DEBUG_COPYBIT, "depth test and/or fog");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 return false;
132 }
133
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700134 // Note: The drawSurfaceBuffer is only set for destination
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135 // surfaces types that are supported by the hardware and
136 // do not have an alpha channel. So we don't have to re-check that here.
137
138 static const int tmu = 0;
139 texture_unit_t& u(c->textures.tmu[tmu]);
140 EGLTextureObject* textureObject = u.texture;
141
142 if (!supportedCopybitsFormat(textureObject->surface.format)) {
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700143 LOGD_IF(DEBUG_COPYBIT, "texture format not supported");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700144 return false;
145 }
146 return true;
147}
148
149
150static bool copybit(GLint x, GLint y,
151 GLint w, GLint h,
152 EGLTextureObject* textureObject,
153 const GLint* crop_rect,
154 int transform,
155 ogles_context_t* c)
156{
157 // We assume checkContext has already been called and has already
158 // returned true.
159
160 const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
161
162 y = cbSurface.height - (y + h);
163
164 const GLint Ucr = crop_rect[0];
165 const GLint Vcr = crop_rect[1];
166 const GLint Wcr = crop_rect[2];
167 const GLint Hcr = crop_rect[3];
168
Mathias Agopian295eff22009-06-29 16:36:49 -0700169 GLint screen_w = w;
170 GLint screen_h = h;
171 int32_t dsdx = Wcr << 16; // dsdx = ((Wcr/screen_w)/Wt)*Wt
172 int32_t dtdy = Hcr << 16; // dtdy = -((Hcr/screen_h)/Ht)*Ht
Mathias Agopiane7829b82009-06-23 18:31:06 -0700173 if (transform & COPYBIT_TRANSFORM_ROT_90) {
Mathias Agopian295eff22009-06-29 16:36:49 -0700174 swap(screen_w, screen_h);
Mathias Agopiane7829b82009-06-23 18:31:06 -0700175 }
Mathias Agopian295eff22009-06-29 16:36:49 -0700176 if (dsdx!=screen_w || dtdy!=screen_h) {
177 // in most cases the divide is not needed
178 dsdx /= screen_w;
179 dtdy /= screen_h;
180 }
181 dtdy = -dtdy; // see equation of dtdy above
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700183 // copybit doesn't say anything about filtering, so we can't
184 // discriminate. On msm7k, copybit will always filter.
185 // the code below handles min/mag filters, we keep it as a reference.
186
187#ifdef MIN_MAG_FILTER
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700188 int32_t texelArea = gglMulx(dtdy, dsdx);
189 if (texelArea < FIXED_ONE && textureObject->mag_filter != GL_LINEAR) {
190 // Non-linear filtering on a texture enlargement.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700191 LOGD_IF(DEBUG_COPYBIT, "mag filter is not GL_LINEAR");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 return false;
193 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 if (texelArea > FIXED_ONE && textureObject->min_filter != GL_LINEAR) {
195 // Non-linear filtering on an texture shrink.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700196 LOGD_IF(DEBUG_COPYBIT, "min filter is not GL_LINEAR");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 return false;
198 }
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700199#endif
200
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 const uint32_t enables = c->rasterizer.state.enables;
202 int planeAlpha = 255;
203 static const int tmu = 0;
204 texture_t& tev(c->rasterizer.state.texture[tmu]);
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700205 int32_t opFormat = textureObject->surface.format;
206 const bool srcTextureHasAlpha = hasAlpha(opFormat);
Mathias Agopiane7829b82009-06-23 18:31:06 -0700207 if (!srcTextureHasAlpha) {
208 planeAlpha = fixedToByte(c->currentColorClamped.a);
209 }
210
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700211 const bool cbHasAlpha = hasAlpha(cbSurface.format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700212 bool blending = false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 if ((enables & GGL_ENABLE_BLENDING)
214 && !(c->rasterizer.state.blend.src == GL_ONE
215 && c->rasterizer.state.blend.dst == GL_ZERO)) {
216 // Blending is OK if it is
217 // the exact kind of blending that the copybits hardware supports.
218 // Note: The hardware only supports
219 // GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA,
220 // But the surface flinger uses GL_ONE / GL_ONE_MINUS_SRC_ALPHA.
221 // We substitute GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA in that case,
222 // because the performance is worth it, even if the results are
223 // not correct.
224 if (!((c->rasterizer.state.blend.src == GL_SRC_ALPHA
225 || c->rasterizer.state.blend.src == GL_ONE)
226 && c->rasterizer.state.blend.dst == GL_ONE_MINUS_SRC_ALPHA
227 && c->rasterizer.state.blend.alpha_separate == 0)) {
228 // Incompatible blend mode.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700229 LOGD_IF(DEBUG_COPYBIT, "incompatible blend mode");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230 return false;
231 }
232 blending = true;
233 } else {
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700234 if (cbHasAlpha) {
235 // NOTE: the result will be slightly wrong in this case because
236 // the destination alpha channel will be set to 1.0 instead of
237 // the iterated alpha value. *shrug*.
238 }
239 // disable plane blending and src blending for supported formats
240 planeAlpha = 255;
241 if (opFormat == COPYBIT_FORMAT_RGBA_8888) {
242 opFormat = COPYBIT_FORMAT_RGBX_8888;
243 } else {
244 if (srcTextureHasAlpha) {
245 LOGD_IF(DEBUG_COPYBIT, "texture format requires blending");
246 return false;
247 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248 }
249 }
250
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700251 switch (tev.env) {
252 case GGL_REPLACE:
253 break;
254 case GGL_MODULATE:
255 // only cases allowed is:
256 // RGB source, color={1,1,1,a} -> can be done with GL_REPLACE
257 // RGBA source, color={1,1,1,1} -> can be done with GL_REPLACE
258 if (blending) {
259 if (c->currentColorClamped.r == c->currentColorClamped.a &&
260 c->currentColorClamped.g == c->currentColorClamped.a &&
261 c->currentColorClamped.b == c->currentColorClamped.a) {
262 // TODO: Need to emulate: RGBA source, color={a,a,a,a} / premult
263 // and RGBA source, color={1,1,1,a} / regular-blending
264 // (both are equivalent)
265 }
266 }
267 LOGD_IF(DEBUG_COPYBIT, "GGL_MODULATE");
268 return false;
269 default:
270 // Incompatible texture environment.
271 LOGD_IF(DEBUG_COPYBIT, "incompatible texture environment");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 return false;
273 }
274
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 copybit_device_t* copybit = c->copybits.blitEngine;
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700276 copybit_image_t src;
277 buffer_handle_t source_hnd = textureObject->buffer->handle;
278 textureToCopyBitImage(&textureObject->surface, opFormat, source_hnd, &src);
279 copybit_rect_t srect = { Ucr, Vcr + Hcr, Ucr + Wcr, Vcr };
280
281 /*
282 * Below we perform extra passes needed to emulate things the h/w
283 * cannot do.
284 */
285
286 const GLfixed minScaleInv = gglDivQ(0x10000, c->copybits.minScale, 16);
287 const GLfixed maxScaleInv = gglDivQ(0x10000, c->copybits.maxScale, 16);
288
289 sp<GraphicBuffer> tempBitmap;
290
291 if (dsdx < maxScaleInv || dsdx > minScaleInv ||
292 dtdy < maxScaleInv || dtdy > minScaleInv)
293 {
294 // The requested scale is out of the range the hardware
295 // can support.
296 LOGD_IF(DEBUG_COPYBIT,
297 "scale out of range dsdx=%08x (Wcr=%d / w=%d), "
298 "dtdy=%08x (Hcr=%d / h=%d), Ucr=%d, Vcr=%d",
299 dsdx, Wcr, w, dtdy, Hcr, h, Ucr, Vcr);
300
301 int32_t xscale=0x10000, yscale=0x10000;
302 if (dsdx > minScaleInv) xscale = c->copybits.minScale;
303 else if (dsdx < maxScaleInv) xscale = c->copybits.maxScale;
304 if (dtdy > minScaleInv) yscale = c->copybits.minScale;
305 else if (dtdy < maxScaleInv) yscale = c->copybits.maxScale;
306 dsdx = gglMulx(dsdx, xscale);
307 dtdy = gglMulx(dtdy, yscale);
308
309 /* we handle only one step of resizing below. Handling an arbitrary
310 * number is relatively easy (replace "if" above by "while"), but requires
311 * two intermediate buffers and so far we never had the need.
312 */
313
314 if (dsdx < maxScaleInv || dsdx > minScaleInv ||
315 dtdy < maxScaleInv || dtdy > minScaleInv) {
316 LOGD_IF(DEBUG_COPYBIT,
317 "scale out of range dsdx=%08x (Wcr=%d / w=%d), "
318 "dtdy=%08x (Hcr=%d / h=%d), Ucr=%d, Vcr=%d",
319 dsdx, Wcr, w, dtdy, Hcr, h, Ucr, Vcr);
320 return false;
321 }
322
323 const int tmp_w = gglMulx(srect.r - srect.l, xscale, 16);
324 const int tmp_h = gglMulx(srect.b - srect.t, yscale, 16);
325
326 LOGD_IF(DEBUG_COPYBIT,
327 "xscale=%08x, yscale=%08x, dsdx=%08x, dtdy=%08x, tmp_w=%d, tmp_h=%d",
328 xscale, yscale, dsdx, dtdy, tmp_w, tmp_h);
329
330 tempBitmap = new GraphicBuffer(
331 tmp_w, tmp_h, src.format,
332 GraphicBuffer::USAGE_HW_2D);
333
334 status_t err = tempBitmap->initCheck();
335 if (err == NO_ERROR) {
336 copybit_image_t tmp_dst;
337 copybit_rect_t tmp_rect;
338 tmp_dst.w = tmp_w;
339 tmp_dst.h = tmp_h;
340 tmp_dst.format = src.format;
341 tmp_dst.handle = (native_handle_t*)tempBitmap->getNativeBuffer()->handle;
342 tmp_rect.l = 0;
343 tmp_rect.t = 0;
344 tmp_rect.r = tmp_dst.w;
345 tmp_rect.b = tmp_dst.h;
346 region_iterator tmp_it(Region(Rect(tmp_rect.r, tmp_rect.b)));
347 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
348 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
349 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
350 err = copybit->stretch(copybit,
351 &tmp_dst, &src, &tmp_rect, &srect, &tmp_it);
352 src = tmp_dst;
353 srect = tmp_rect;
354 }
355 }
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700356
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700357 copybit_image_t dst;
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700358 buffer_handle_t target_hnd = c->copybits.drawSurfaceBuffer;
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700359 textureToCopyBitImage(&cbSurface, cbSurface.format, target_hnd, &dst);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700360 copybit_rect_t drect = {x, y, x+w, y+h};
361
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700362 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, transform);
363 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, planeAlpha);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700364 copybit->set_parameter(copybit, COPYBIT_DITHER,
365 (enables & GGL_ENABLE_DITHER) ? COPYBIT_ENABLE : COPYBIT_DISABLE);
366
367 clipRectRegion it(c);
Mathias Agopian295eff22009-06-29 16:36:49 -0700368 status_t err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it);
Mathias Agopianf13901e2009-07-15 18:53:32 -0700369 if (err != NO_ERROR) {
370 c->textures.tmu[0].texture->try_copybit = false;
371 }
Mathias Agopian295eff22009-06-29 16:36:49 -0700372 return err == NO_ERROR ? true : false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700373}
374
375/*
376 * Try to draw a triangle fan with copybit, return false if we fail.
377 */
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700378bool drawTriangleFanWithCopybit_impl(ogles_context_t* c, GLint first, GLsizei count)
379{
380 if (!checkContext(c)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700381 return false;
382 }
383
Mathias Agopian7272add2009-06-18 19:31:07 -0700384 // FIXME: we should handle culling here
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700385 c->arrays.compileElements(c, c->vc.vBuffer, 0, 4);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700386
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700387 // we detect if we're dealing with a rectangle, by comparing the
388 // rectangles {v0,v2} and {v1,v3} which should be identical.
389
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700390 // NOTE: we should check that the rectangle is window aligned, however
391 // if we do that, the optimization won't be taken in a lot of cases.
392 // Since this code is intended to be used with SurfaceFlinger only,
393 // so it's okay...
394
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700395 const vec4_t& v0 = c->vc.vBuffer[0].window;
396 const vec4_t& v1 = c->vc.vBuffer[1].window;
397 const vec4_t& v2 = c->vc.vBuffer[2].window;
398 const vec4_t& v3 = c->vc.vBuffer[3].window;
399 int l = min(v0.x, v2.x);
400 int b = min(v0.y, v2.y);
401 int r = max(v0.x, v2.x);
402 int t = max(v0.y, v2.y);
403 if ((l != min(v1.x, v3.x)) || (b != min(v1.y, v3.y)) ||
404 (r != max(v1.x, v3.x)) || (t != max(v1.y, v3.y))) {
405 LOGD_IF(DEBUG_COPYBIT, "geometry not a rectangle");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700406 return false;
407 }
Mathias Agopian7272add2009-06-18 19:31:07 -0700408
409 // fetch and transform texture coordinates
410 // NOTE: maybe it would be better to have a "compileElementsAll" method
411 // that would ensure all vertex data are fetched and transformed
412 const transform_t& tr = c->transforms.texture[0].transform;
413 for (size_t i=0 ; i<4 ; i++) {
414 const GLubyte* tp = c->arrays.texture[0].element(i);
415 vertex_t* const v = &c->vc.vBuffer[i];
416 c->arrays.texture[0].fetch(c, v->texture[0].v, tp);
417 // FIXME: we should bail if q!=1
418 c->arrays.tex_transform[0](&tr, &v->texture[0], &v->texture[0]);
419 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700420
421 const vec4_t& t0 = c->vc.vBuffer[0].texture[0];
422 const vec4_t& t1 = c->vc.vBuffer[1].texture[0];
423 const vec4_t& t2 = c->vc.vBuffer[2].texture[0];
424 const vec4_t& t3 = c->vc.vBuffer[3].texture[0];
425 int txl = min(t0.x, t2.x);
426 int txb = min(t0.y, t2.y);
427 int txr = max(t0.x, t2.x);
428 int txt = max(t0.y, t2.y);
429 if ((txl != min(t1.x, t3.x)) || (txb != min(t1.y, t3.y)) ||
430 (txr != max(t1.x, t3.x)) || (txt != max(t1.y, t3.y))) {
431 LOGD_IF(DEBUG_COPYBIT, "texcoord not a rectangle");
432 return false;
433 }
434 if ((txl != 0) || (txb != 0) ||
435 (txr != FIXED_ONE) || (txt != FIXED_ONE)) {
436 // we could probably handle this case, if we wanted to
Mathias Agopian7272add2009-06-18 19:31:07 -0700437 LOGD_IF(DEBUG_COPYBIT, "texture is cropped: %08x,%08x,%08x,%08x",
438 txl, txb, txr, txt);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700439 return false;
440 }
441
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700442 // at this point, we know we are dealing with a rectangle, so we
443 // only need to consider 3 vertices for computing the jacobians
444
445 const int dx01 = v1.x - v0.x;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700446 const int dx02 = v2.x - v0.x;
Mathias Agopian295eff22009-06-29 16:36:49 -0700447 const int dy01 = v1.y - v0.y;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700448 const int dy02 = v2.y - v0.y;
449 const int ds01 = t1.S - t0.S;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700450 const int ds02 = t2.S - t0.S;
Mathias Agopian295eff22009-06-29 16:36:49 -0700451 const int dt01 = t1.T - t0.T;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700452 const int dt02 = t2.T - t0.T;
453 const int area = dx01*dy02 - dy01*dx02;
454 int dsdx, dsdy, dtdx, dtdy;
455 if (area >= 0) {
456 dsdx = ds01*dy02 - ds02*dy01;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700457 dtdx = dt01*dy02 - dt02*dy01;
Mathias Agopian295eff22009-06-29 16:36:49 -0700458 dsdy = ds02*dx01 - ds01*dx02;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700459 dtdy = dt02*dx01 - dt01*dx02;
460 } else {
461 dsdx = ds02*dy01 - ds01*dy02;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700462 dtdx = dt02*dy01 - dt01*dy02;
Mathias Agopian295eff22009-06-29 16:36:49 -0700463 dsdy = ds01*dx02 - ds02*dx01;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700464 dtdy = dt01*dx02 - dt02*dx01;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700465 }
466
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700467 // here we rely on the fact that we know the transform is
468 // a rigid-body transform AND that it can only rotate in 90 degrees
469 // increments
470
471 int transform = 0;
472 if (dsdx == 0) {
473 // 90 deg rotation case
474 // [ 0 dtdx ]
475 // [ dsdx 0 ]
476 transform |= COPYBIT_TRANSFORM_ROT_90;
477 // FIXME: not sure if FLIP_H and FLIP_V shouldn't be inverted
478 if (dtdx > 0)
479 transform |= COPYBIT_TRANSFORM_FLIP_H;
480 if (dsdy < 0)
481 transform |= COPYBIT_TRANSFORM_FLIP_V;
482 } else {
483 // [ dsdx 0 ]
484 // [ 0 dtdy ]
485 if (dsdx < 0)
486 transform |= COPYBIT_TRANSFORM_FLIP_H;
487 if (dtdy < 0)
488 transform |= COPYBIT_TRANSFORM_FLIP_V;
489 }
490
491 //LOGD("l=%d, b=%d, w=%d, h=%d, tr=%d", x, y, w, h, transform);
492 //LOGD("A=%f\tB=%f\nC=%f\tD=%f",
493 // dsdx/65536.0, dtdx/65536.0, dsdy/65536.0, dtdy/65536.0);
494
495 int x = l >> 4;
496 int y = b >> 4;
497 int w = (r-l) >> 4;
498 int h = (t-b) >> 4;
499 texture_unit_t& u(c->textures.tmu[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700500 EGLTextureObject* textureObject = u.texture;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700501 GLint tWidth = textureObject->surface.width;
502 GLint tHeight = textureObject->surface.height;
503 GLint crop_rect[4] = {0, tHeight, tWidth, -tHeight};
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700504 const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
505 y = cbSurface.height - (y + h);
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700506 return copybit(x, y, w, h, textureObject, crop_rect, transform, c);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700507}
508
509/*
510 * Try to drawTexiOESWithCopybit, return false if we fail.
511 */
512
513bool drawTexiOESWithCopybit_impl(GLint x, GLint y, GLint z,
514 GLint w, GLint h, ogles_context_t* c)
515{
516 // quickly process empty rects
517 if ((w|h) <= 0) {
518 return true;
519 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700520 if (!checkContext(c)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521 return false;
522 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700523 texture_unit_t& u(c->textures.tmu[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700524 EGLTextureObject* textureObject = u.texture;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700525 return copybit(x, y, w, h, textureObject, textureObject->crop_rect, 0, c);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700526}
527
528} // namespace android
529