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 | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 77 | public: |
Mathias Agopian | cf251b9 | 2009-06-16 18:08:29 -0700 | [diff] [blame] | 78 | copybit_rect_t r; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static bool supportedCopybitsFormat(int format) { |
| 82 | switch (format) { |
| 83 | case COPYBIT_FORMAT_RGBA_8888: |
Mathias Agopian | dfbec0e | 2009-08-07 20:55:14 -0700 | [diff] [blame] | 84 | case COPYBIT_FORMAT_RGBX_8888: |
| 85 | case COPYBIT_FORMAT_RGB_888: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 86 | 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 | |
| 98 | static 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 | |
| 110 | static 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 | |
| 119 | static bool checkContext(ogles_context_t* c) { |
| 120 | |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 121 | // By convention copybitQuickCheckContext() has already returned true. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 122 | // avoid checking the same information again. |
| 123 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 124 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 135 | // Note: The drawSurfaceBuffer is only set for destination |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 136 | // 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 144 | LOGD_IF(DEBUG_COPYBIT, "texture format not supported"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | static 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 Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 158 | status_t err = NO_ERROR; |
| 159 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 160 | // 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 Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 172 | 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 Agopian | e7829b8 | 2009-06-23 18:31:06 -0700 | [diff] [blame] | 176 | if (transform & COPYBIT_TRANSFORM_ROT_90) { |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 177 | swap(screen_w, screen_h); |
Mathias Agopian | e7829b8 | 2009-06-23 18:31:06 -0700 | [diff] [blame] | 178 | } |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 179 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 185 | |
Mathias Agopian | aa6e88b | 2009-06-17 21:58:18 -0700 | [diff] [blame] | 186 | // 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 191 | 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 194 | LOGD_IF(DEBUG_COPYBIT, "mag filter is not GL_LINEAR"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 197 | if (texelArea > FIXED_ONE && textureObject->min_filter != GL_LINEAR) { |
| 198 | // Non-linear filtering on an texture shrink. |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 199 | LOGD_IF(DEBUG_COPYBIT, "min filter is not GL_LINEAR"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
Mathias Agopian | aa6e88b | 2009-06-17 21:58:18 -0700 | [diff] [blame] | 202 | #endif |
| 203 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 204 | const uint32_t enables = c->rasterizer.state.enables; |
| 205 | int planeAlpha = 255; |
Mathias Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 206 | bool alphaPlaneWorkaround = false; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 207 | static const int tmu = 0; |
| 208 | texture_t& tev(c->rasterizer.state.texture[tmu]); |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 209 | int32_t opFormat = textureObject->surface.format; |
| 210 | const bool srcTextureHasAlpha = hasAlpha(opFormat); |
Mathias Agopian | e7829b8 | 2009-06-23 18:31:06 -0700 | [diff] [blame] | 211 | if (!srcTextureHasAlpha) { |
| 212 | planeAlpha = fixedToByte(c->currentColorClamped.a); |
| 213 | } |
| 214 | |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 215 | const bool cbHasAlpha = hasAlpha(cbSurface.format); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 216 | bool blending = false; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 217 | 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 233 | LOGD_IF(DEBUG_COPYBIT, "incompatible blend mode"); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | blending = true; |
| 237 | } else { |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 238 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 255 | 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 Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 266 | // TODO: RGBA source, color={1,1,1,a} / regular-blending |
| 267 | // is equivalent |
| 268 | alphaPlaneWorkaround = true; |
| 269 | break; |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 270 | } |
| 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 280 | copybit_device_t* copybit = c->copybits.blitEngine; |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 281 | 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 Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 339 | err = tempBitmap->initCheck(); |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 340 | 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 Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 345 | tmp_dst.format = tempBitmap->format; |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 346 | 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 Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 361 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 362 | copybit_image_t dst; |
Mathias Agopian | 0a3139a | 2009-06-10 16:01:54 -0700 | [diff] [blame] | 363 | buffer_handle_t target_hnd = c->copybits.drawSurfaceBuffer; |
Mathias Agopian | 6dbedd7 | 2009-10-16 16:17:58 -0700 | [diff] [blame] | 364 | textureToCopyBitImage(&cbSurface, cbSurface.format, target_hnd, &dst); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 365 | copybit_rect_t drect = {x, y, x+w, y+h}; |
| 366 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 367 | |
Mathias Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 368 | /* 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 Agopian | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 425 | |
Iliyan Malchev | d95c322 | 2009-10-30 18:29:08 -0700 | [diff] [blame] | 426 | LOGD_IF(0, |
| 427 | "dst={%d, %d, %d, %p, %p}, " |
Mathias Agopian | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 428 | "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 Agopian | 2ad8ec5 | 2009-10-27 23:33:48 -0700 | [diff] [blame] | 439 | err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it); |
| 440 | } |
Mathias Agopian | f13901e | 2009-07-15 18:53:32 -0700 | [diff] [blame] | 441 | if (err != NO_ERROR) { |
| 442 | c->textures.tmu[0].texture->try_copybit = false; |
| 443 | } |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 444 | return err == NO_ERROR ? true : false; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Try to draw a triangle fan with copybit, return false if we fail. |
| 449 | */ |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 450 | bool drawTriangleFanWithCopybit_impl(ogles_context_t* c, GLint first, GLsizei count) |
| 451 | { |
| 452 | if (!checkContext(c)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | |
Mathias Agopian | 7272add | 2009-06-18 19:31:07 -0700 | [diff] [blame] | 456 | // FIXME: we should handle culling here |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 457 | c->arrays.compileElements(c, c->vc.vBuffer, 0, 4); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 458 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 459 | // 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 Agopian | aa6e88b | 2009-06-17 21:58:18 -0700 | [diff] [blame] | 462 | // 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 467 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 478 | return false; |
| 479 | } |
Mathias Agopian | 7272add | 2009-06-18 19:31:07 -0700 | [diff] [blame] | 480 | |
| 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 492 | |
| 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 Agopian | 7272add | 2009-06-18 19:31:07 -0700 | [diff] [blame] | 509 | LOGD_IF(DEBUG_COPYBIT, "texture is cropped: %08x,%08x,%08x,%08x", |
| 510 | txl, txb, txr, txt); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 514 | // 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 518 | const int dx02 = v2.x - v0.x; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 519 | const int dy01 = v1.y - v0.y; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 520 | const int dy02 = v2.y - v0.y; |
| 521 | const int ds01 = t1.S - t0.S; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 522 | const int ds02 = t2.S - t0.S; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 523 | const int dt01 = t1.T - t0.T; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 524 | 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 529 | dtdx = dt01*dy02 - dt02*dy01; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 530 | dsdy = ds02*dx01 - ds01*dx02; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 531 | dtdy = dt02*dx01 - dt01*dx02; |
| 532 | } else { |
| 533 | dsdx = ds02*dy01 - ds01*dy02; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 534 | dtdx = dt02*dy01 - dt01*dy02; |
Mathias Agopian | 295eff2 | 2009-06-29 16:36:49 -0700 | [diff] [blame] | 535 | dsdy = ds01*dx02 - ds02*dx01; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 536 | dtdy = dt01*dx02 - dt02*dx01; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 539 | // 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 572 | EGLTextureObject* textureObject = u.texture; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 573 | GLint tWidth = textureObject->surface.width; |
| 574 | GLint tHeight = textureObject->surface.height; |
| 575 | GLint crop_rect[4] = {0, tHeight, tWidth, -tHeight}; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 576 | const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s; |
| 577 | y = cbSurface.height - (y + h); |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 578 | return copybit(x, y, w, h, textureObject, crop_rect, transform, c); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Try to drawTexiOESWithCopybit, return false if we fail. |
| 583 | */ |
| 584 | |
| 585 | bool 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 Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 592 | if (!checkContext(c)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 593 | return false; |
| 594 | } |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 595 | texture_unit_t& u(c->textures.tmu[0]); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 596 | EGLTextureObject* textureObject = u.texture; |
Mathias Agopian | 6d2cad2 | 2009-06-17 21:18:56 -0700 | [diff] [blame] | 597 | return copybit(x, y, w, h, textureObject, textureObject->crop_rect, 0, c); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | } // namespace android |
| 601 | |