Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | #include "TextureObjectManager.h" |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <hardware/gralloc.h> |
| 33 | #include <hardware/copybit.h> |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 34 | #include <private/ui/android_natives_priv.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 36 | #include <ui/GraphicBuffer.h> |
| 37 | #include <ui/Region.h> |
| 38 | #include <ui/Rect.h> |
| 39 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 40 | |
| 41 | #define DEBUG_COPYBIT true |
| 42 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | namespace android { |
| 46 | |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 47 | static void textureToCopyBitImage( |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 48 | const GGLSurface* surface, int32_t opFormat, |
| 49 | buffer_handle_t buffer, copybit_image_t* img) |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 50 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 51 | img->w = surface->stride; |
| 52 | img->h = surface->height; |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 53 | img->format = opFormat; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 54 | img->base = surface->data; |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 55 | img->handle = (native_handle_t *)buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | struct clipRectRegion : public copybit_region_t { |
Mathias Agopian | cf251b9 | 2009-06-16 18:08:29 -0700 | [diff] [blame] | 59 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 67 | } |
| 68 | private: |
| 69 | static int iterate(copybit_region_t const * self, copybit_rect_t* rect) { |
Mathias Agopian | cf251b9 | 2009-06-16 18:08:29 -0700 | [diff] [blame] | 70 | *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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Mathias Agopian | cf251b9 | 2009-06-16 18:08:29 -0700 | [diff] [blame] | 77 | copybit_rect_t r; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static bool supportedCopybitsFormat(int format) { |
| 81 | switch (format) { |
| 82 | case COPYBIT_FORMAT_RGBA_8888: |
Mathias Agopian | dfbec0e | 2009-08-07 20:55:14 -0700 | [diff] [blame] | 83 | case COPYBIT_FORMAT_RGBX_8888: |
| 84 | case COPYBIT_FORMAT_RGB_888: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 85 | 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 | |
| 97 | static 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 | |
| 109 | static 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 | |
| 118 | static bool checkContext(ogles_context_t* c) { |
| 119 | |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 120 | // By convention copybitQuickCheckContext() has already returned true. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 121 | // avoid checking the same information again. |
| 122 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 123 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 134 | // Note: The drawSurfaceBuffer is only set for destination |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 135 | // 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 143 | LOGD_IF(DEBUG_COPYBIT, "texture format not supported"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static 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 Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 169 | 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 Agopian | e7829b8 | 2009-06-23 18:31:06 -0700 | [diff] [blame] | 173 | if (transform & COPYBIT_TRANSFORM_ROT_90) { |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 174 | swap(screen_w, screen_h); |
Mathias Agopian | e7829b8 | 2009-06-23 18:31:06 -0700 | [diff] [blame] | 175 | } |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 176 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 182 | |
Mathias Agopian | aa6e88b | 2009-06-17 21:58:18 -0700 | [diff] [blame] | 183 | // 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 188 | 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 191 | LOGD_IF(DEBUG_COPYBIT, "mag filter is not GL_LINEAR"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 192 | return false; |
| 193 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 194 | if (texelArea > FIXED_ONE && textureObject->min_filter != GL_LINEAR) { |
| 195 | // Non-linear filtering on an texture shrink. |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 196 | LOGD_IF(DEBUG_COPYBIT, "min filter is not GL_LINEAR"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
Mathias Agopian | aa6e88b | 2009-06-17 21:58:18 -0700 | [diff] [blame] | 199 | #endif |
| 200 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 201 | 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 Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 205 | int32_t opFormat = textureObject->surface.format; |
| 206 | const bool srcTextureHasAlpha = hasAlpha(opFormat); |
Mathias Agopian | e7829b8 | 2009-06-23 18:31:06 -0700 | [diff] [blame] | 207 | if (!srcTextureHasAlpha) { |
| 208 | planeAlpha = fixedToByte(c->currentColorClamped.a); |
| 209 | } |
| 210 | |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 211 | const bool cbHasAlpha = hasAlpha(cbSurface.format); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 212 | bool blending = false; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 213 | 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 229 | LOGD_IF(DEBUG_COPYBIT, "incompatible blend mode"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | blending = true; |
| 233 | } else { |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 234 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 251 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 275 | copybit_device_t* copybit = c->copybits.blitEngine; |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 276 | 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 Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 356 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 357 | copybit_image_t dst; |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 358 | buffer_handle_t target_hnd = c->copybits.drawSurfaceBuffer; |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 359 | textureToCopyBitImage(&cbSurface, cbSurface.format, target_hnd, &dst); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 360 | copybit_rect_t drect = {x, y, x+w, y+h}; |
| 361 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 362 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, transform); |
| 363 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, planeAlpha); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 364 | copybit->set_parameter(copybit, COPYBIT_DITHER, |
| 365 | (enables & GGL_ENABLE_DITHER) ? COPYBIT_ENABLE : COPYBIT_DISABLE); |
| 366 | |
| 367 | clipRectRegion it(c); |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 368 | status_t err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it); |
Mathias Agopian | f13901e | 2009-07-15 18:53:32 -0700 | [diff] [blame] | 369 | if (err != NO_ERROR) { |
| 370 | c->textures.tmu[0].texture->try_copybit = false; |
| 371 | } |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 372 | return err == NO_ERROR ? true : false; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Try to draw a triangle fan with copybit, return false if we fail. |
| 377 | */ |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 378 | bool drawTriangleFanWithCopybit_impl(ogles_context_t* c, GLint first, GLsizei count) |
| 379 | { |
| 380 | if (!checkContext(c)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
Mathias Agopian | 7272add | 2009-06-18 19:31:07 -0700 | [diff] [blame] | 384 | // FIXME: we should handle culling here |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 385 | c->arrays.compileElements(c, c->vc.vBuffer, 0, 4); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 386 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 387 | // 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 Agopian | aa6e88b | 2009-06-17 21:58:18 -0700 | [diff] [blame] | 390 | // 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 395 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 406 | return false; |
| 407 | } |
Mathias Agopian | 7272add | 2009-06-18 19:31:07 -0700 | [diff] [blame] | 408 | |
| 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 420 | |
| 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 Agopian | 7272add | 2009-06-18 19:31:07 -0700 | [diff] [blame] | 437 | LOGD_IF(DEBUG_COPYBIT, "texture is cropped: %08x,%08x,%08x,%08x", |
| 438 | txl, txb, txr, txt); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 442 | // 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 446 | const int dx02 = v2.x - v0.x; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 447 | const int dy01 = v1.y - v0.y; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 448 | const int dy02 = v2.y - v0.y; |
| 449 | const int ds01 = t1.S - t0.S; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 450 | const int ds02 = t2.S - t0.S; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 451 | const int dt01 = t1.T - t0.T; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 452 | 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 457 | dtdx = dt01*dy02 - dt02*dy01; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 458 | dsdy = ds02*dx01 - ds01*dx02; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 459 | dtdy = dt02*dx01 - dt01*dx02; |
| 460 | } else { |
| 461 | dsdx = ds02*dy01 - ds01*dy02; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 462 | dtdx = dt02*dy01 - dt01*dy02; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 463 | dsdy = ds01*dx02 - ds02*dx01; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 464 | dtdy = dt01*dx02 - dt02*dx01; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 467 | // 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 500 | EGLTextureObject* textureObject = u.texture; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 501 | GLint tWidth = textureObject->surface.width; |
| 502 | GLint tHeight = textureObject->surface.height; |
| 503 | GLint crop_rect[4] = {0, tHeight, tWidth, -tHeight}; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 504 | const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s; |
| 505 | y = cbSurface.height - (y + h); |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 506 | return copybit(x, y, w, h, textureObject, crop_rect, transform, c); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Try to drawTexiOESWithCopybit, return false if we fail. |
| 511 | */ |
| 512 | |
| 513 | bool 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 520 | if (!checkContext(c)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 521 | return false; |
| 522 | } |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 523 | texture_unit_t& u(c->textures.tmu[0]); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 524 | EGLTextureObject* textureObject = u.texture; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 525 | return copybit(x, y, w, h, textureObject, textureObject->crop_rect, 0, c); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | } // namespace android |
| 529 | |