blob: cde8f8a68af87a741459767071c4f4454ddfecd2 [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#include "gralloc_priv.h"
36
Mathias Agopian6d2cad22009-06-17 21:18:56 -070037
38#define DEBUG_COPYBIT true
39
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040// ----------------------------------------------------------------------------
41
42namespace android {
43
Mathias Agopian0a3139a2009-06-10 16:01:54 -070044static void textureToCopyBitImage(
45 const GGLSurface* surface, buffer_handle_t buffer, copybit_image_t* img)
46{
47 // we know private_handle_t is good here
48 private_handle_t* hnd = (private_handle_t*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049 img->w = surface->stride;
50 img->h = surface->height;
51 img->format = surface->format;
Mathias Agopian0a3139a2009-06-10 16:01:54 -070052 img->offset = hnd->offset;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053 img->base = surface->data;
Mathias Agopian0a3139a2009-06-10 16:01:54 -070054 img->fd = hnd->fd;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055}
56
57struct clipRectRegion : public copybit_region_t {
Mathias Agopiancf251b92009-06-16 18:08:29 -070058 clipRectRegion(ogles_context_t* c)
59 {
60 scissor_t const* scissor = &c->rasterizer.state.scissor;
61 r.l = scissor->left;
62 r.t = scissor->top;
63 r.r = scissor->right;
64 r.b = scissor->bottom;
65 next = iterate;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066 }
67private:
68 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
Mathias Agopiancf251b92009-06-16 18:08:29 -070069 *rect = static_cast<clipRectRegion const*>(self)->r;
70 const_cast<copybit_region_t *>(self)->next = iterate_done;
71 return 1;
72 }
73 static int iterate_done(copybit_region_t const *, copybit_rect_t*) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 return 0;
75 }
Mathias Agopiancf251b92009-06-16 18:08:29 -070076 copybit_rect_t r;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077};
78
79static bool supportedCopybitsFormat(int format) {
80 switch (format) {
81 case COPYBIT_FORMAT_RGBA_8888:
82 case COPYBIT_FORMAT_RGB_565:
83 case COPYBIT_FORMAT_BGRA_8888:
84 case COPYBIT_FORMAT_RGBA_5551:
85 case COPYBIT_FORMAT_RGBA_4444:
86 case COPYBIT_FORMAT_YCbCr_422_SP:
87 case COPYBIT_FORMAT_YCbCr_420_SP:
88 return true;
89 default:
90 return false;
91 }
92}
93
94static bool hasAlpha(int format) {
95 switch (format) {
96 case COPYBIT_FORMAT_RGBA_8888:
97 case COPYBIT_FORMAT_BGRA_8888:
98 case COPYBIT_FORMAT_RGBA_5551:
99 case COPYBIT_FORMAT_RGBA_4444:
100 return true;
101 default:
102 return false;
103 }
104}
105
106static inline int fixedToByte(GGLfixed val) {
107 return (val - (val >> 8)) >> 8;
108}
109
110/**
111 * Performs a quick check of the rendering state. If this function returns
112 * false we cannot use the copybit driver.
113 */
114
115static bool checkContext(ogles_context_t* c) {
116
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700117 // By convention copybitQuickCheckContext() has already returned true.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 // avoid checking the same information again.
119
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700120 if (c->copybits.blitEngine == NULL) {
121 LOGD_IF(DEBUG_COPYBIT, "no copybit hal");
122 return false;
123 }
124
125 if (c->rasterizer.state.enables
126 & (GGL_ENABLE_DEPTH_TEST|GGL_ENABLE_FOG)) {
127 LOGD_IF(DEBUG_COPYBIT, "depth test and/or fog");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700128 return false;
129 }
130
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700131 // Note: The drawSurfaceBuffer is only set for destination
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 // surfaces types that are supported by the hardware and
133 // do not have an alpha channel. So we don't have to re-check that here.
134
135 static const int tmu = 0;
136 texture_unit_t& u(c->textures.tmu[tmu]);
137 EGLTextureObject* textureObject = u.texture;
138
139 if (!supportedCopybitsFormat(textureObject->surface.format)) {
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700140 LOGD_IF(DEBUG_COPYBIT, "texture format not supported");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 return false;
142 }
143 return true;
144}
145
146
147static bool copybit(GLint x, GLint y,
148 GLint w, GLint h,
149 EGLTextureObject* textureObject,
150 const GLint* crop_rect,
151 int transform,
152 ogles_context_t* c)
153{
154 // We assume checkContext has already been called and has already
155 // returned true.
156
157 const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
158
159 y = cbSurface.height - (y + h);
160
161 const GLint Ucr = crop_rect[0];
162 const GLint Vcr = crop_rect[1];
163 const GLint Wcr = crop_rect[2];
164 const GLint Hcr = crop_rect[3];
165
166 int32_t dsdx = (Wcr << 16) / w; // dsdx = ((Wcr/w)/Wt)*Wt
167 int32_t dtdy = ((-Hcr) << 16) / h; // dtdy = -((Hcr/h)/Ht)*Ht
168
169 if (dsdx < c->copybits.minScale || dsdx > c->copybits.maxScale
170 || dtdy < c->copybits.minScale || dtdy > c->copybits.maxScale) {
171 // The requested scale is out of the range the hardware
172 // can support.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700173 LOGD_IF(DEBUG_COPYBIT,
174 "scale out of range dsdx=%08x, dtdy=%08x", dsdx, dtdy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175 return false;
176 }
177
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700178 // copybit doesn't say anything about filtering, so we can't
179 // discriminate. On msm7k, copybit will always filter.
180 // the code below handles min/mag filters, we keep it as a reference.
181
182#ifdef MIN_MAG_FILTER
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 int32_t texelArea = gglMulx(dtdy, dsdx);
184 if (texelArea < FIXED_ONE && textureObject->mag_filter != GL_LINEAR) {
185 // Non-linear filtering on a texture enlargement.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700186 LOGD_IF(DEBUG_COPYBIT, "mag filter is not GL_LINEAR");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 return false;
188 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700189 if (texelArea > FIXED_ONE && textureObject->min_filter != GL_LINEAR) {
190 // Non-linear filtering on an texture shrink.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700191 LOGD_IF(DEBUG_COPYBIT, "min filter is not GL_LINEAR");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 return false;
193 }
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700194#endif
195
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 const uint32_t enables = c->rasterizer.state.enables;
197 int planeAlpha = 255;
198 static const int tmu = 0;
199 texture_t& tev(c->rasterizer.state.texture[tmu]);
200 bool srcTextureHasAlpha = hasAlpha(textureObject->surface.format);
201 switch (tev.env) {
202
203 case GGL_REPLACE:
204 if (!srcTextureHasAlpha) {
205 planeAlpha = fixedToByte(c->currentColorClamped.a);
206 }
207 break;
208
209 case GGL_MODULATE:
210 if (! (c->currentColorClamped.r == FIXED_ONE
211 && c->currentColorClamped.g == FIXED_ONE
212 && c->currentColorClamped.b == FIXED_ONE)) {
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700213 LOGD_IF(DEBUG_COPYBIT, "MODULATE and non white color");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 return false;
215 }
216 planeAlpha = fixedToByte(c->currentColorClamped.a);
217 break;
218
219 default:
220 // Incompatible texture environment.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700221 LOGD_IF(DEBUG_COPYBIT, "incompatible texture environment");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222 return false;
223 }
224
225 bool blending = false;
226
227 if ((enables & GGL_ENABLE_BLENDING)
228 && !(c->rasterizer.state.blend.src == GL_ONE
229 && c->rasterizer.state.blend.dst == GL_ZERO)) {
230 // Blending is OK if it is
231 // the exact kind of blending that the copybits hardware supports.
232 // Note: The hardware only supports
233 // GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA,
234 // But the surface flinger uses GL_ONE / GL_ONE_MINUS_SRC_ALPHA.
235 // We substitute GL_SRC_ALPHA / GL_ONE_MINUS_SRC_ALPHA in that case,
236 // because the performance is worth it, even if the results are
237 // not correct.
238 if (!((c->rasterizer.state.blend.src == GL_SRC_ALPHA
239 || c->rasterizer.state.blend.src == GL_ONE)
240 && c->rasterizer.state.blend.dst == GL_ONE_MINUS_SRC_ALPHA
241 && c->rasterizer.state.blend.alpha_separate == 0)) {
242 // Incompatible blend mode.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700243 LOGD_IF(DEBUG_COPYBIT, "incompatible blend mode");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244 return false;
245 }
246 blending = true;
247 } else {
248 // No blending is OK if we are not using alpha.
249 if (srcTextureHasAlpha || planeAlpha != 255) {
250 // Incompatible alpha
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700251 LOGD_IF(DEBUG_COPYBIT, "incompatible alpha");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 return false;
253 }
254 }
255
256 if (srcTextureHasAlpha && planeAlpha != 255) {
257 // Can't do two types of alpha at once.
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700258 LOGD_IF(DEBUG_COPYBIT, "src alpha and plane alpha");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700259 return false;
260 }
261
262 // LOGW("calling copybits");
263
264 copybit_device_t* copybit = c->copybits.blitEngine;
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700265
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700266 copybit_image_t dst;
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700267 buffer_handle_t target_hnd = c->copybits.drawSurfaceBuffer;
268 textureToCopyBitImage(&cbSurface, target_hnd, &dst);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700269 copybit_rect_t drect = {x, y, x+w, y+h};
270
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700271 // we know private_handle_t is good here
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 copybit_image_t src;
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700273 buffer_handle_t source_hnd = textureObject->buffer->handle;
274 textureToCopyBitImage(&textureObject->surface, source_hnd, &src);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 copybit_rect_t srect = { Ucr, Vcr + Hcr, Ucr + Wcr, Vcr };
276
277 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, transform);
278 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, planeAlpha);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 copybit->set_parameter(copybit, COPYBIT_DITHER,
280 (enables & GGL_ENABLE_DITHER) ? COPYBIT_ENABLE : COPYBIT_DISABLE);
281
282 clipRectRegion it(c);
283 copybit->stretch(copybit, &dst, &src, &drect, &srect, &it);
284 return true;
285}
286
287/*
288 * Try to draw a triangle fan with copybit, return false if we fail.
289 */
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700290bool drawTriangleFanWithCopybit_impl(ogles_context_t* c, GLint first, GLsizei count)
291{
292 if (!checkContext(c)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 return false;
294 }
295
Mathias Agopian7272add2009-06-18 19:31:07 -0700296 // FIXME: we should handle culling here
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 c->arrays.compileElements(c, c->vc.vBuffer, 0, 4);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700299 // we detect if we're dealing with a rectangle, by comparing the
300 // rectangles {v0,v2} and {v1,v3} which should be identical.
301
Mathias Agopianaa6e88b2009-06-17 21:58:18 -0700302 // NOTE: we should check that the rectangle is window aligned, however
303 // if we do that, the optimization won't be taken in a lot of cases.
304 // Since this code is intended to be used with SurfaceFlinger only,
305 // so it's okay...
306
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700307 const vec4_t& v0 = c->vc.vBuffer[0].window;
308 const vec4_t& v1 = c->vc.vBuffer[1].window;
309 const vec4_t& v2 = c->vc.vBuffer[2].window;
310 const vec4_t& v3 = c->vc.vBuffer[3].window;
311 int l = min(v0.x, v2.x);
312 int b = min(v0.y, v2.y);
313 int r = max(v0.x, v2.x);
314 int t = max(v0.y, v2.y);
315 if ((l != min(v1.x, v3.x)) || (b != min(v1.y, v3.y)) ||
316 (r != max(v1.x, v3.x)) || (t != max(v1.y, v3.y))) {
317 LOGD_IF(DEBUG_COPYBIT, "geometry not a rectangle");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318 return false;
319 }
Mathias Agopian7272add2009-06-18 19:31:07 -0700320
321 // fetch and transform texture coordinates
322 // NOTE: maybe it would be better to have a "compileElementsAll" method
323 // that would ensure all vertex data are fetched and transformed
324 const transform_t& tr = c->transforms.texture[0].transform;
325 for (size_t i=0 ; i<4 ; i++) {
326 const GLubyte* tp = c->arrays.texture[0].element(i);
327 vertex_t* const v = &c->vc.vBuffer[i];
328 c->arrays.texture[0].fetch(c, v->texture[0].v, tp);
329 // FIXME: we should bail if q!=1
330 c->arrays.tex_transform[0](&tr, &v->texture[0], &v->texture[0]);
331 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700332
333 const vec4_t& t0 = c->vc.vBuffer[0].texture[0];
334 const vec4_t& t1 = c->vc.vBuffer[1].texture[0];
335 const vec4_t& t2 = c->vc.vBuffer[2].texture[0];
336 const vec4_t& t3 = c->vc.vBuffer[3].texture[0];
337 int txl = min(t0.x, t2.x);
338 int txb = min(t0.y, t2.y);
339 int txr = max(t0.x, t2.x);
340 int txt = max(t0.y, t2.y);
341 if ((txl != min(t1.x, t3.x)) || (txb != min(t1.y, t3.y)) ||
342 (txr != max(t1.x, t3.x)) || (txt != max(t1.y, t3.y))) {
343 LOGD_IF(DEBUG_COPYBIT, "texcoord not a rectangle");
344 return false;
345 }
346 if ((txl != 0) || (txb != 0) ||
347 (txr != FIXED_ONE) || (txt != FIXED_ONE)) {
348 // we could probably handle this case, if we wanted to
Mathias Agopian7272add2009-06-18 19:31:07 -0700349 LOGD_IF(DEBUG_COPYBIT, "texture is cropped: %08x,%08x,%08x,%08x",
350 txl, txb, txr, txt);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700351 return false;
352 }
353
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700354 // at this point, we know we are dealing with a rectangle, so we
355 // only need to consider 3 vertices for computing the jacobians
356
357 const int dx01 = v1.x - v0.x;
358 const int dy01 = v1.y - v0.y;
359 const int dx02 = v2.x - v0.x;
360 const int dy02 = v2.y - v0.y;
361 const int ds01 = t1.S - t0.S;
362 const int dt01 = t1.T - t0.T;
363 const int ds02 = t2.S - t0.S;
364 const int dt02 = t2.T - t0.T;
365 const int area = dx01*dy02 - dy01*dx02;
366 int dsdx, dsdy, dtdx, dtdy;
367 if (area >= 0) {
368 dsdx = ds01*dy02 - ds02*dy01;
369 dsdy = ds02*dx01 - ds01*dx02;
370 dtdx = dt01*dy02 - dt02*dy01;
371 dtdy = dt02*dx01 - dt01*dx02;
372 } else {
373 dsdx = ds02*dy01 - ds01*dy02;
374 dsdy = ds01*dx02 - ds02*dx01;
375 dtdx = dt02*dy01 - dt01*dy02;
376 dtdy = dt01*dx02 - dt02*dx01;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700377 }
378
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700379 // here we rely on the fact that we know the transform is
380 // a rigid-body transform AND that it can only rotate in 90 degrees
381 // increments
382
383 int transform = 0;
384 if (dsdx == 0) {
385 // 90 deg rotation case
386 // [ 0 dtdx ]
387 // [ dsdx 0 ]
388 transform |= COPYBIT_TRANSFORM_ROT_90;
389 // FIXME: not sure if FLIP_H and FLIP_V shouldn't be inverted
390 if (dtdx > 0)
391 transform |= COPYBIT_TRANSFORM_FLIP_H;
392 if (dsdy < 0)
393 transform |= COPYBIT_TRANSFORM_FLIP_V;
394 } else {
395 // [ dsdx 0 ]
396 // [ 0 dtdy ]
397 if (dsdx < 0)
398 transform |= COPYBIT_TRANSFORM_FLIP_H;
399 if (dtdy < 0)
400 transform |= COPYBIT_TRANSFORM_FLIP_V;
401 }
402
403 //LOGD("l=%d, b=%d, w=%d, h=%d, tr=%d", x, y, w, h, transform);
404 //LOGD("A=%f\tB=%f\nC=%f\tD=%f",
405 // dsdx/65536.0, dtdx/65536.0, dsdy/65536.0, dtdy/65536.0);
406
407 int x = l >> 4;
408 int y = b >> 4;
409 int w = (r-l) >> 4;
410 int h = (t-b) >> 4;
411 texture_unit_t& u(c->textures.tmu[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700412 EGLTextureObject* textureObject = u.texture;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700413 GLint tWidth = textureObject->surface.width;
414 GLint tHeight = textureObject->surface.height;
415 GLint crop_rect[4] = {0, tHeight, tWidth, -tHeight};
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700416 const GGLSurface& cbSurface = c->rasterizer.state.buffers.color.s;
417 y = cbSurface.height - (y + h);
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700418 return copybit(x, y, w, h, textureObject, crop_rect, transform, c);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700419}
420
421/*
422 * Try to drawTexiOESWithCopybit, return false if we fail.
423 */
424
425bool drawTexiOESWithCopybit_impl(GLint x, GLint y, GLint z,
426 GLint w, GLint h, ogles_context_t* c)
427{
428 // quickly process empty rects
429 if ((w|h) <= 0) {
430 return true;
431 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700432 if (!checkContext(c)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433 return false;
434 }
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700435 texture_unit_t& u(c->textures.tmu[0]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700436 EGLTextureObject* textureObject = u.texture;
Mathias Agopian6d2cad22009-06-17 21:18:56 -0700437 return copybit(x, y, w, h, textureObject, textureObject->crop_rect, 0, c);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700438}
439
440} // namespace android
441