blob: 3de5b2ba5547438890ceaf9fd91bddf8df5e9373 [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 Agopiandf2d9292009-10-28 21:00:29 -070077public:
Mathias Agopiancf251b92009-06-16 18:08:29 -070078 copybit_rect_t r;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079};
80
81static bool supportedCopybitsFormat(int format) {
82 switch (format) {
83 case COPYBIT_FORMAT_RGBA_8888:
Mathias Agopiandfbec0e2009-08-07 20:55:14 -070084 case COPYBIT_FORMAT_RGBX_8888:
85 case COPYBIT_FORMAT_RGB_888:
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 case COPYBIT_FORMAT_RGB_565:
87 case COPYBIT_FORMAT_BGRA_8888:
88 case COPYBIT_FORMAT_RGBA_5551:
89 case COPYBIT_FORMAT_RGBA_4444:
90 case COPYBIT_FORMAT_YCbCr_422_SP:
91 case COPYBIT_FORMAT_YCbCr_420_SP:
92 return true;
93 default:
94 return false;
95 }
96}
97
98static bool hasAlpha(int format) {
99 switch (format) {
100 case COPYBIT_FORMAT_RGBA_8888:
101 case COPYBIT_FORMAT_BGRA_8888:
102 case COPYBIT_FORMAT_RGBA_5551:
103 case COPYBIT_FORMAT_RGBA_4444:
104 return true;
105 default:
106 return false;
107 }
108}
109
110static inline int fixedToByte(GGLfixed val) {
111 return (val - (val >> 8)) >> 8;
112}
113
114/**
115 * Performs a quick check of the rendering state. If this function returns
116 * false we cannot use the copybit driver.
117 */
118
119static bool checkContext(ogles_context_t* c) {
120
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700121 // By convention copybitQuickCheckContext() has already returned true.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 // avoid checking the same information again.
123
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700124 if (c->copybits.blitEngine == NULL) {
125 LOGD_IF(DEBUG_COPYBIT, "no copybit hal");
126 return false;
127 }
128
129 if (c->rasterizer.state.enables
130 & (GGL_ENABLE_DEPTH_TEST|GGL_ENABLE_FOG)) {
131 LOGD_IF(DEBUG_COPYBIT, "depth test and/or fog");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 return false;
133 }
134
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700135 // Note: The drawSurfaceBuffer is only set for destination
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700136 // surfaces types that are supported by the hardware and
137 // do not have an alpha channel. So we don't have to re-check that here.
138
139 static const int tmu = 0;
140 texture_unit_t& u(c->textures.tmu[tmu]);
141 EGLTextureObject* textureObject = u.texture;
142
143 if (!supportedCopybitsFormat(textureObject->surface.format)) {
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700144 LOGD_IF(DEBUG_COPYBIT, "texture format not supported");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145 return false;
146 }
147 return true;
148}
149
150
151static bool copybit(GLint x, GLint y,
152 GLint w, GLint h,
153 EGLTextureObject* textureObject,
154 const GLint* crop_rect,
155 int transform,
156 ogles_context_t* c)
157{
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700158 status_t err = NO_ERROR;
159
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160 // We assume checkContext has already been called and has already
161 // returned true.
162
163 const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
164
165 y = cbSurface.height - (y + h);
166
167 const GLint Ucr = crop_rect[0];
168 const GLint Vcr = crop_rect[1];
169 const GLint Wcr = crop_rect[2];
170 const GLint Hcr = crop_rect[3];
171
Mathias Agopian295eff22009-06-29 16:36:49 -0700172 GLint screen_w = w;
173 GLint screen_h = h;
174 int32_t dsdx = Wcr << 16; // dsdx = ((Wcr/screen_w)/Wt)*Wt
175 int32_t dtdy = Hcr << 16; // dtdy = -((Hcr/screen_h)/Ht)*Ht
Mathias Agopiane7829b82009-06-23 18:31:06 -0700176 if (transform & COPYBIT_TRANSFORM_ROT_90) {
Mathias Agopian295eff22009-06-29 16:36:49 -0700177 swap(screen_w, screen_h);
Mathias Agopiane7829b82009-06-23 18:31:06 -0700178 }
Mathias Agopian295eff22009-06-29 16:36:49 -0700179 if (dsdx!=screen_w || dtdy!=screen_h) {
180 // in most cases the divide is not needed
181 dsdx /= screen_w;
182 dtdy /= screen_h;
183 }
184 dtdy = -dtdy; // see equation of dtdy above
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700185
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700186 // copybit doesn't say anything about filtering, so we can't
187 // discriminate. On msm7k, copybit will always filter.
188 // the code below handles min/mag filters, we keep it as a reference.
189
190#ifdef MIN_MAG_FILTER
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700191 int32_t texelArea = gglMulx(dtdy, dsdx);
192 if (texelArea < FIXED_ONE && textureObject->mag_filter != GL_LINEAR) {
193 // Non-linear filtering on a texture enlargement.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700194 LOGD_IF(DEBUG_COPYBIT, "mag filter is not GL_LINEAR");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700195 return false;
196 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 if (texelArea > FIXED_ONE && textureObject->min_filter != GL_LINEAR) {
198 // Non-linear filtering on an texture shrink.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700199 LOGD_IF(DEBUG_COPYBIT, "min filter is not GL_LINEAR");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 return false;
201 }
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700202#endif
203
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 const uint32_t enables = c->rasterizer.state.enables;
205 int planeAlpha = 255;
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700206 bool alphaPlaneWorkaround = false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207 static const int tmu = 0;
208 texture_t& tev(c->rasterizer.state.texture[tmu]);
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700209 int32_t opFormat = textureObject->surface.format;
210 const bool srcTextureHasAlpha = hasAlpha(opFormat);
Mathias Agopiane7829b82009-06-23 18:31:06 -0700211 if (!srcTextureHasAlpha) {
212 planeAlpha = fixedToByte(c->currentColorClamped.a);
213 }
214
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700215 const bool cbHasAlpha = hasAlpha(cbSurface.format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216 bool blending = false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 if ((enables & GGL_ENABLE_BLENDING)
218 && !(c->rasterizer.state.blend.src == GL_ONE
219 && c->rasterizer.state.blend.dst == GL_ZERO)) {
220 // Blending is OK if it is
221 // the exact kind of blending that the copybits hardware supports.
222 // Note: The hardware only supports
223 // GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA,
224 // But the surface flinger uses GL_ONE / GL_ONE_MINUS_SRC_ALPHA.
225 // We substitute GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA in that case,
226 // because the performance is worth it, even if the results are
227 // not correct.
228 if (!((c->rasterizer.state.blend.src == GL_SRC_ALPHA
229 || c->rasterizer.state.blend.src == GL_ONE)
230 && c->rasterizer.state.blend.dst == GL_ONE_MINUS_SRC_ALPHA
231 && c->rasterizer.state.blend.alpha_separate == 0)) {
232 // Incompatible blend mode.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700233 LOGD_IF(DEBUG_COPYBIT, "incompatible blend mode");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 return false;
235 }
236 blending = true;
237 } else {
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700238 if (cbHasAlpha) {
239 // NOTE: the result will be slightly wrong in this case because
240 // the destination alpha channel will be set to 1.0 instead of
241 // the iterated alpha value. *shrug*.
242 }
243 // disable plane blending and src blending for supported formats
244 planeAlpha = 255;
245 if (opFormat == COPYBIT_FORMAT_RGBA_8888) {
246 opFormat = COPYBIT_FORMAT_RGBX_8888;
247 } else {
248 if (srcTextureHasAlpha) {
249 LOGD_IF(DEBUG_COPYBIT, "texture format requires blending");
250 return false;
251 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 }
253 }
254
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700255 switch (tev.env) {
256 case GGL_REPLACE:
257 break;
258 case GGL_MODULATE:
259 // only cases allowed is:
260 // RGB source, color={1,1,1,a} -> can be done with GL_REPLACE
261 // RGBA source, color={1,1,1,1} -> can be done with GL_REPLACE
262 if (blending) {
263 if (c->currentColorClamped.r == c->currentColorClamped.a &&
264 c->currentColorClamped.g == c->currentColorClamped.a &&
265 c->currentColorClamped.b == c->currentColorClamped.a) {
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700266 // TODO: RGBA source, color={1,1,1,a} / regular-blending
267 // is equivalent
268 alphaPlaneWorkaround = true;
269 break;
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700270 }
271 }
272 LOGD_IF(DEBUG_COPYBIT, "GGL_MODULATE");
273 return false;
274 default:
275 // Incompatible texture environment.
276 LOGD_IF(DEBUG_COPYBIT, "incompatible texture environment");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277 return false;
278 }
279
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 copybit_device_t* copybit = c->copybits.blitEngine;
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700281 copybit_image_t src;
282 buffer_handle_t source_hnd = textureObject->buffer->handle;
283 textureToCopyBitImage(&textureObject->surface, opFormat, source_hnd, &src);
284 copybit_rect_t srect = { Ucr, Vcr + Hcr, Ucr + Wcr, Vcr };
285
286 /*
287 * Below we perform extra passes needed to emulate things the h/w
288 * cannot do.
289 */
290
291 const GLfixed minScaleInv = gglDivQ(0x10000, c->copybits.minScale, 16);
292 const GLfixed maxScaleInv = gglDivQ(0x10000, c->copybits.maxScale, 16);
293
294 sp<GraphicBuffer> tempBitmap;
295
296 if (dsdx < maxScaleInv || dsdx > minScaleInv ||
297 dtdy < maxScaleInv || dtdy > minScaleInv)
298 {
299 // The requested scale is out of the range the hardware
300 // can support.
301 LOGD_IF(DEBUG_COPYBIT,
302 "scale out of range dsdx=%08x (Wcr=%d / w=%d), "
303 "dtdy=%08x (Hcr=%d / h=%d), Ucr=%d, Vcr=%d",
304 dsdx, Wcr, w, dtdy, Hcr, h, Ucr, Vcr);
305
306 int32_t xscale=0x10000, yscale=0x10000;
307 if (dsdx > minScaleInv) xscale = c->copybits.minScale;
308 else if (dsdx < maxScaleInv) xscale = c->copybits.maxScale;
309 if (dtdy > minScaleInv) yscale = c->copybits.minScale;
310 else if (dtdy < maxScaleInv) yscale = c->copybits.maxScale;
311 dsdx = gglMulx(dsdx, xscale);
312 dtdy = gglMulx(dtdy, yscale);
313
314 /* we handle only one step of resizing below. Handling an arbitrary
315 * number is relatively easy (replace "if" above by "while"), but requires
316 * two intermediate buffers and so far we never had the need.
317 */
318
319 if (dsdx < maxScaleInv || dsdx > minScaleInv ||
320 dtdy < maxScaleInv || dtdy > minScaleInv) {
321 LOGD_IF(DEBUG_COPYBIT,
322 "scale out of range dsdx=%08x (Wcr=%d / w=%d), "
323 "dtdy=%08x (Hcr=%d / h=%d), Ucr=%d, Vcr=%d",
324 dsdx, Wcr, w, dtdy, Hcr, h, Ucr, Vcr);
325 return false;
326 }
327
328 const int tmp_w = gglMulx(srect.r - srect.l, xscale, 16);
329 const int tmp_h = gglMulx(srect.b - srect.t, yscale, 16);
330
331 LOGD_IF(DEBUG_COPYBIT,
332 "xscale=%08x, yscale=%08x, dsdx=%08x, dtdy=%08x, tmp_w=%d, tmp_h=%d",
333 xscale, yscale, dsdx, dtdy, tmp_w, tmp_h);
334
335 tempBitmap = new GraphicBuffer(
336 tmp_w, tmp_h, src.format,
337 GraphicBuffer::USAGE_HW_2D);
338
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700339 err = tempBitmap->initCheck();
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700340 if (err == NO_ERROR) {
341 copybit_image_t tmp_dst;
342 copybit_rect_t tmp_rect;
343 tmp_dst.w = tmp_w;
344 tmp_dst.h = tmp_h;
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700345 tmp_dst.format = tempBitmap->format;
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700346 tmp_dst.handle = (native_handle_t*)tempBitmap->getNativeBuffer()->handle;
347 tmp_rect.l = 0;
348 tmp_rect.t = 0;
349 tmp_rect.r = tmp_dst.w;
350 tmp_rect.b = tmp_dst.h;
351 region_iterator tmp_it(Region(Rect(tmp_rect.r, tmp_rect.b)));
352 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
353 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
354 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
355 err = copybit->stretch(copybit,
356 &tmp_dst, &src, &tmp_rect, &srect, &tmp_it);
357 src = tmp_dst;
358 srect = tmp_rect;
359 }
360 }
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700361
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700362 copybit_image_t dst;
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700363 buffer_handle_t target_hnd = c->copybits.drawSurfaceBuffer;
Mathias Agopian6dbedd72009-10-16 16:17:58 -0700364 textureToCopyBitImage(&cbSurface, cbSurface.format, target_hnd, &dst);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700365 copybit_rect_t drect = {x, y, x+w, y+h};
366
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700367
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700368 /* and now the alpha-plane hack. This handles the "Fade" case of a
369 * texture with an alpha channel.
370 */
371 if (alphaPlaneWorkaround) {
372 sp<GraphicBuffer> tempCb = new GraphicBuffer(
373 w, h, COPYBIT_FORMAT_RGB_565,
374 GraphicBuffer::USAGE_HW_2D);
375
376 err = tempCb->initCheck();
377
378 copybit_image_t tmpCbImg;
379 copybit_rect_t tmpCbRect;
380 tmpCbImg.w = w;
381 tmpCbImg.h = h;
382 tmpCbImg.format = tempCb->format;
383 tmpCbImg.handle = (native_handle_t*)tempCb->getNativeBuffer()->handle;
384 tmpCbRect.l = 0;
385 tmpCbRect.t = 0;
386 tmpCbRect.r = w;
387 tmpCbRect.b = h;
388
389 if (!err) {
390 // first make a copy of the destination buffer
391 region_iterator tmp_it(Region(Rect(w, h)));
392 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
393 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
394 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
395 err = copybit->stretch(copybit,
396 &tmpCbImg, &dst, &tmpCbRect, &drect, &tmp_it);
397 }
398 if (!err) {
399 // then proceed as usual, but without the alpha plane
400 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, transform);
401 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
402 copybit->set_parameter(copybit, COPYBIT_DITHER,
403 (enables & GGL_ENABLE_DITHER) ?
404 COPYBIT_ENABLE : COPYBIT_DISABLE);
405 clipRectRegion it(c);
406 err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it);
407 }
408 if (!err) {
409 // finally copy back the destination on top with 1-alphaplane
410 int invPlaneAlpha = 0xFF - fixedToByte(c->currentColorClamped.a);
411 clipRectRegion it(c);
412 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
413 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, invPlaneAlpha);
414 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
415 err = copybit->stretch(copybit,
416 &dst, &tmpCbImg, &drect, &tmpCbRect, &it);
417 }
418 } else {
419 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, transform);
420 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, planeAlpha);
421 copybit->set_parameter(copybit, COPYBIT_DITHER,
422 (enables & GGL_ENABLE_DITHER) ?
423 COPYBIT_ENABLE : COPYBIT_DISABLE);
424 clipRectRegion it(c);
Mathias Agopiandf2d9292009-10-28 21:00:29 -0700425
Iliyan Malchevd95c3222009-10-30 18:29:08 -0700426 LOGD_IF(0,
427 "dst={%d, %d, %d, %p, %p}, "
Mathias Agopiandf2d9292009-10-28 21:00:29 -0700428 "src={%d, %d, %d, %p, %p}, "
429 "drect={%d,%d,%d,%d}, "
430 "srect={%d,%d,%d,%d}, "
431 "it={%d,%d,%d,%d}, " ,
432 dst.w, dst.h, dst.format, dst.base, dst.handle,
433 src.w, src.h, src.format, src.base, src.handle,
434 drect.l, drect.t, drect.r, drect.b,
435 srect.l, srect.t, srect.r, srect.b,
436 it.r.l, it.r.t, it.r.r, it.r.b
437 );
438
Mathias Agopian2ad8ec52009-10-27 23:33:48 -0700439 err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it);
440 }
Mathias Agopianf13901e2009-07-15 18:53:32 -0700441 if (err != NO_ERROR) {
442 c->textures.tmu[0].texture->try_copybit = false;
443 }
Mathias Agopian295eff22009-06-29 16:36:49 -0700444 return err == NO_ERROR ? true : false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700445}
446
447/*
448 * Try to draw a triangle fan with copybit, return false if we fail.
449 */
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700450bool drawTriangleFanWithCopybit_impl(ogles_context_t* c, GLint first, GLsizei count)
451{
452 if (!checkContext(c)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700453 return false;
454 }
455
Mathias Agopian7272add2009-06-18 19:31:07 -0700456 // FIXME: we should handle culling here
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700457 c->arrays.compileElements(c, c->vc.vBuffer, 0, 4);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700458
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700459 // we detect if we're dealing with a rectangle, by comparing the
460 // rectangles {v0,v2} and {v1,v3} which should be identical.
461
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700462 // NOTE: we should check that the rectangle is window aligned, however
463 // if we do that, the optimization won't be taken in a lot of cases.
464 // Since this code is intended to be used with SurfaceFlinger only,
465 // so it's okay...
466
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700467 const vec4_t& v0 = c->vc.vBuffer[0].window;
468 const vec4_t& v1 = c->vc.vBuffer[1].window;
469 const vec4_t& v2 = c->vc.vBuffer[2].window;
470 const vec4_t& v3 = c->vc.vBuffer[3].window;
471 int l = min(v0.x, v2.x);
472 int b = min(v0.y, v2.y);
473 int r = max(v0.x, v2.x);
474 int t = max(v0.y, v2.y);
475 if ((l != min(v1.x, v3.x)) || (b != min(v1.y, v3.y)) ||
476 (r != max(v1.x, v3.x)) || (t != max(v1.y, v3.y))) {
477 LOGD_IF(DEBUG_COPYBIT, "geometry not a rectangle");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700478 return false;
479 }
Mathias Agopian7272add2009-06-18 19:31:07 -0700480
481 // fetch and transform texture coordinates
482 // NOTE: maybe it would be better to have a "compileElementsAll" method
483 // that would ensure all vertex data are fetched and transformed
484 const transform_t& tr = c->transforms.texture[0].transform;
485 for (size_t i=0 ; i<4 ; i++) {
486 const GLubyte* tp = c->arrays.texture[0].element(i);
487 vertex_t* const v = &c->vc.vBuffer[i];
488 c->arrays.texture[0].fetch(c, v->texture[0].v, tp);
489 // FIXME: we should bail if q!=1
490 c->arrays.tex_transform[0](&tr, &v->texture[0], &v->texture[0]);
491 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700492
493 const vec4_t& t0 = c->vc.vBuffer[0].texture[0];
494 const vec4_t& t1 = c->vc.vBuffer[1].texture[0];
495 const vec4_t& t2 = c->vc.vBuffer[2].texture[0];
496 const vec4_t& t3 = c->vc.vBuffer[3].texture[0];
497 int txl = min(t0.x, t2.x);
498 int txb = min(t0.y, t2.y);
499 int txr = max(t0.x, t2.x);
500 int txt = max(t0.y, t2.y);
501 if ((txl != min(t1.x, t3.x)) || (txb != min(t1.y, t3.y)) ||
502 (txr != max(t1.x, t3.x)) || (txt != max(t1.y, t3.y))) {
503 LOGD_IF(DEBUG_COPYBIT, "texcoord not a rectangle");
504 return false;
505 }
506 if ((txl != 0) || (txb != 0) ||
507 (txr != FIXED_ONE) || (txt != FIXED_ONE)) {
508 // we could probably handle this case, if we wanted to
Mathias Agopian7272add2009-06-18 19:31:07 -0700509 LOGD_IF(DEBUG_COPYBIT, "texture is cropped: %08x,%08x,%08x,%08x",
510 txl, txb, txr, txt);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511 return false;
512 }
513
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700514 // at this point, we know we are dealing with a rectangle, so we
515 // only need to consider 3 vertices for computing the jacobians
516
517 const int dx01 = v1.x - v0.x;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700518 const int dx02 = v2.x - v0.x;
Mathias Agopian295eff22009-06-29 16:36:49 -0700519 const int dy01 = v1.y - v0.y;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700520 const int dy02 = v2.y - v0.y;
521 const int ds01 = t1.S - t0.S;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700522 const int ds02 = t2.S - t0.S;
Mathias Agopian295eff22009-06-29 16:36:49 -0700523 const int dt01 = t1.T - t0.T;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700524 const int dt02 = t2.T - t0.T;
525 const int area = dx01*dy02 - dy01*dx02;
526 int dsdx, dsdy, dtdx, dtdy;
527 if (area >= 0) {
528 dsdx = ds01*dy02 - ds02*dy01;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700529 dtdx = dt01*dy02 - dt02*dy01;
Mathias Agopian295eff22009-06-29 16:36:49 -0700530 dsdy = ds02*dx01 - ds01*dx02;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700531 dtdy = dt02*dx01 - dt01*dx02;
532 } else {
533 dsdx = ds02*dy01 - ds01*dy02;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700534 dtdx = dt02*dy01 - dt01*dy02;
Mathias Agopian295eff22009-06-29 16:36:49 -0700535 dsdy = ds01*dx02 - ds02*dx01;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700536 dtdy = dt01*dx02 - dt02*dx01;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 }
538
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700539 // here we rely on the fact that we know the transform is
540 // a rigid-body transform AND that it can only rotate in 90 degrees
541 // increments
542
543 int transform = 0;
544 if (dsdx == 0) {
545 // 90 deg rotation case
546 // [ 0 dtdx ]
547 // [ dsdx 0 ]
548 transform |= COPYBIT_TRANSFORM_ROT_90;
549 // FIXME: not sure if FLIP_H and FLIP_V shouldn't be inverted
550 if (dtdx > 0)
551 transform |= COPYBIT_TRANSFORM_FLIP_H;
552 if (dsdy < 0)
553 transform |= COPYBIT_TRANSFORM_FLIP_V;
554 } else {
555 // [ dsdx 0 ]
556 // [ 0 dtdy ]
557 if (dsdx < 0)
558 transform |= COPYBIT_TRANSFORM_FLIP_H;
559 if (dtdy < 0)
560 transform |= COPYBIT_TRANSFORM_FLIP_V;
561 }
562
563 //LOGD("l=%d, b=%d, w=%d, h=%d, tr=%d", x, y, w, h, transform);
564 //LOGD("A=%f\tB=%f\nC=%f\tD=%f",
565 // dsdx/65536.0, dtdx/65536.0, dsdy/65536.0, dtdy/65536.0);
566
567 int x = l >> 4;
568 int y = b >> 4;
569 int w = (r-l) >> 4;
570 int h = (t-b) >> 4;
571 texture_unit_t& u(c->textures.tmu[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572 EGLTextureObject* textureObject = u.texture;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700573 GLint tWidth = textureObject->surface.width;
574 GLint tHeight = textureObject->surface.height;
575 GLint crop_rect[4] = {0, tHeight, tWidth, -tHeight};
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700576 const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
577 y = cbSurface.height - (y + h);
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700578 return copybit(x, y, w, h, textureObject, crop_rect, transform, c);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579}
580
581/*
582 * Try to drawTexiOESWithCopybit, return false if we fail.
583 */
584
585bool drawTexiOESWithCopybit_impl(GLint x, GLint y, GLint z,
586 GLint w, GLint h, ogles_context_t* c)
587{
588 // quickly process empty rects
589 if ((w|h) <= 0) {
590 return true;
591 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700592 if (!checkContext(c)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593 return false;
594 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700595 texture_unit_t& u(c->textures.tmu[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700596 EGLTextureObject* textureObject = u.texture;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700597 return copybit(x, y, w, h, textureObject, textureObject->crop_rect, 0, c);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700598}
599
600} // namespace android
601