1/*
2 * Copyright (C) 2011-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/** @file rs_graphics.rsh
18 *  \brief RenderScript graphics API
19 *
20 *  A set of graphics functions used by RenderScript.
21 *
22 */
23#ifndef __RS_GRAPHICS_RSH__
24#define __RS_GRAPHICS_RSH__
25
26#ifdef __LP64__
27//#error "RenderScript graphics is deprecated and not supported in 64bit mode."
28#else
29
30#include "rs_mesh.rsh"
31#include "rs_program.rsh"
32
33#if (defined(RS_VERSION) && (RS_VERSION >= 14))
34/**
35 * Set the color target used for all subsequent rendering calls
36 * @param colorTarget
37 * @param slot
38 */
39extern void __attribute__((overloadable))
40    rsgBindColorTarget(rs_allocation colorTarget, uint slot);
41
42/**
43 * Clear the previously set color target
44 * @param slot
45 */
46extern void __attribute__((overloadable))
47    rsgClearColorTarget(uint slot);
48
49/**
50 * Set the depth target used for all subsequent rendering calls
51 * @param depthTarget
52 */
53extern void __attribute__((overloadable))
54    rsgBindDepthTarget(rs_allocation depthTarget);
55
56/**
57 * Clear the previously set depth target
58 */
59extern void __attribute__((overloadable))
60    rsgClearDepthTarget(void);
61
62/**
63 * Clear all color and depth targets and resume rendering into
64 * the framebuffer
65 */
66extern void __attribute__((overloadable))
67    rsgClearAllRenderTargets(void);
68
69/**
70 * Force RenderScript to finish all rendering commands
71 */
72extern uint __attribute__((overloadable))
73    rsgFinish(void);
74
75#endif //defined(RS_VERSION) && (RS_VERSION >= 14)
76
77/**
78 * Bind a new ProgramFragment to the rendering context.
79 *
80 * @param pf
81 */
82extern void __attribute__((overloadable))
83    rsgBindProgramFragment(rs_program_fragment pf);
84
85/**
86 * Bind a new ProgramStore to the rendering context.
87 *
88 * @param ps
89 */
90extern void __attribute__((overloadable))
91    rsgBindProgramStore(rs_program_store ps);
92
93/**
94 * Bind a new ProgramVertex to the rendering context.
95 *
96 * @param pv
97 */
98extern void __attribute__((overloadable))
99    rsgBindProgramVertex(rs_program_vertex pv);
100
101/**
102 * Bind a new ProgramRaster to the rendering context.
103 *
104 * @param pr
105 */
106extern void __attribute__((overloadable))
107    rsgBindProgramRaster(rs_program_raster pr);
108
109/**
110 * Bind a new Sampler object to a ProgramFragment.  The sampler will
111 * operate on the texture bound at the matching slot.
112 *
113 * @param slot
114 */
115extern void __attribute__((overloadable))
116    rsgBindSampler(rs_program_fragment, uint slot, rs_sampler);
117
118/**
119 * Bind a new Allocation object to a ProgramFragment.  The
120 * Allocation must be a valid texture for the Program.  The sampling
121 * of the texture will be controled by the Sampler bound at the
122 * matching slot.
123 *
124 * @param slot
125 */
126extern void __attribute__((overloadable))
127    rsgBindTexture(rs_program_fragment, uint slot, rs_allocation);
128
129/**
130 * Load the projection matrix for a currently bound fixed function
131 * vertex program. Calling this function with a custom vertex shader
132 * would result in an error.
133 * @param proj projection matrix
134 */
135extern void __attribute__((overloadable))
136    rsgProgramVertexLoadProjectionMatrix(const rs_matrix4x4 *proj);
137/**
138 * Load the model matrix for a currently bound fixed function
139 * vertex program. Calling this function with a custom vertex shader
140 * would result in an error.
141 * @param model model matrix
142 */
143extern void __attribute__((overloadable))
144    rsgProgramVertexLoadModelMatrix(const rs_matrix4x4 *model);
145/**
146 * Load the texture matrix for a currently bound fixed function
147 * vertex program. Calling this function with a custom vertex shader
148 * would result in an error.
149 * @param tex texture matrix
150 */
151extern void __attribute__((overloadable))
152    rsgProgramVertexLoadTextureMatrix(const rs_matrix4x4 *tex);
153/**
154 * Get the projection matrix for a currently bound fixed function
155 * vertex program. Calling this function with a custom vertex shader
156 * would result in an error.
157 * @param proj matrix to store the current projection matrix into
158 */
159extern void __attribute__((overloadable))
160    rsgProgramVertexGetProjectionMatrix(rs_matrix4x4 *proj);
161
162/**
163 * Set the constant color for a fixed function emulation program.
164 *
165 * @param pf
166 * @param r
167 * @param g
168 * @param b
169 * @param a
170 */
171extern void __attribute__((overloadable))
172    rsgProgramFragmentConstantColor(rs_program_fragment pf, float r, float g, float b, float a);
173
174/**
175 * Bind a new Allocation object to a ProgramFragment.  The
176 * Allocation must be a valid constant input for the Program.
177 *
178 * @param ps program object
179 * @param slot index of the constant buffer on the program
180 * @param c constants to bind
181 */
182extern void __attribute__((overloadable))
183    rsgBindConstant(rs_program_fragment ps, uint slot, rs_allocation c);
184
185/**
186 * Bind a new Allocation object to a ProgramVertex.  The
187 * Allocation must be a valid constant input for the Program.
188 *
189 * @param pv program object
190 * @param slot index of the constant buffer on the program
191 * @param c constants to bind
192 */
193extern void __attribute__((overloadable))
194    rsgBindConstant(rs_program_vertex pv, uint slot, rs_allocation c);
195
196/**
197 * Get the width of the current rendering surface.
198 *
199 * @return uint
200 */
201extern uint __attribute__((overloadable))
202    rsgGetWidth(void);
203
204/**
205 * Get the height of the current rendering surface.
206 *
207 * @return uint
208 */
209extern uint __attribute__((overloadable))
210    rsgGetHeight(void);
211
212
213/**
214 * Sync the contents of an allocation from its SCRIPT memory space to its HW
215 * memory spaces.
216 *
217 * @param alloc
218 */
219extern void __attribute__((overloadable))
220    rsgAllocationSyncAll(rs_allocation alloc);
221
222#if (defined(RS_VERSION) && (RS_VERSION >= 14))
223
224/**
225 * Sync the contents of an allocation from memory space
226 * specified by source.
227 *
228 * @param alloc
229 * @param source
230 */
231extern void __attribute__((overloadable))
232    rsgAllocationSyncAll(rs_allocation alloc,
233                         rs_allocation_usage_type source);
234
235#endif //defined(RS_VERSION) && (RS_VERSION >= 14)
236
237/**
238 * Low performance utility function for drawing a simple rectangle.  Not
239 * intended for drawing large quantities of geometry.
240 *
241 * @param x1
242 * @param y1
243 * @param x2
244 * @param y2
245 * @param z
246 */
247extern void __attribute__((overloadable))
248    rsgDrawRect(float x1, float y1, float x2, float y2, float z);
249
250/**
251 * Low performance utility function for drawing a simple quad.  Not intended for
252 * drawing large quantities of geometry.
253 *
254 * @param x1
255 * @param y1
256 * @param z1
257 * @param x2
258 * @param y2
259 * @param z2
260 * @param x3
261 * @param y3
262 * @param z3
263 * @param x4
264 * @param y4
265 * @param z4
266 */
267extern void __attribute__((overloadable))
268    rsgDrawQuad(float x1, float y1, float z1,
269                float x2, float y2, float z2,
270                float x3, float y3, float z3,
271                float x4, float y4, float z4);
272
273
274/**
275 * Low performance utility function for drawing a textured quad.  Not intended
276 * for drawing large quantities of geometry.
277 *
278 * @param x1
279 * @param y1
280 * @param z1
281 * @param u1
282 * @param v1
283 * @param x2
284 * @param y2
285 * @param z2
286 * @param u2
287 * @param v2
288 * @param x3
289 * @param y3
290 * @param z3
291 * @param u3
292 * @param v3
293 * @param x4
294 * @param y4
295 * @param z4
296 * @param u4
297 * @param v4
298 */
299extern void __attribute__((overloadable))
300    rsgDrawQuadTexCoords(float x1, float y1, float z1, float u1, float v1,
301                         float x2, float y2, float z2, float u2, float v2,
302                         float x3, float y3, float z3, float u3, float v3,
303                         float x4, float y4, float z4, float u4, float v4);
304
305
306/**
307 * Low performance function for drawing rectangles in screenspace.  This
308 * function uses the default passthough ProgramVertex.  Any bound ProgramVertex
309 * is ignored.  This function has considerable overhead and should not be used
310 * for drawing in shipping applications.
311 *
312 * @param x
313 * @param y
314 * @param z
315 * @param w
316 * @param h
317 */
318extern void __attribute__((overloadable))
319    rsgDrawSpriteScreenspace(float x, float y, float z, float w, float h);
320
321extern void __attribute__((overloadable))
322    rsgDrawPath(rs_path p);
323
324/**
325 * Draw a mesh using the current context state.  The whole mesh is
326 * rendered.
327 *
328 * @param ism
329 */
330extern void __attribute__((overloadable))
331    rsgDrawMesh(rs_mesh ism);
332/**
333 * Draw part of a mesh using the current context state.
334 * @param ism mesh object to render
335 * @param primitiveIndex for meshes that contain multiple primitive groups
336 *        this parameter specifies the index of the group to draw.
337 */
338extern void __attribute__((overloadable))
339    rsgDrawMesh(rs_mesh ism, uint primitiveIndex);
340/**
341 * Draw specified index range of part of a mesh using the current context state.
342 * @param ism mesh object to render
343 * @param primitiveIndex for meshes that contain multiple primitive groups
344 *        this parameter specifies the index of the group to draw.
345 * @param start starting index in the range
346 * @param len number of indices to draw
347 */
348extern void __attribute__((overloadable))
349    rsgDrawMesh(rs_mesh ism, uint primitiveIndex, uint start, uint len);
350
351/**
352 * Clears the rendering surface to the specified color.
353 *
354 * @param r
355 * @param g
356 * @param b
357 * @param a
358 */
359extern void __attribute__((overloadable))
360    rsgClearColor(float r, float g, float b, float a);
361
362/**
363 * Clears the depth suface to the specified value.
364 */
365extern void __attribute__((overloadable))
366    rsgClearDepth(float value);
367/**
368 * Draws text given a string and location
369 */
370extern void __attribute__((overloadable))
371    rsgDrawText(const char *, int x, int y);
372/**
373 * \overload
374 */
375extern void __attribute__((overloadable))
376    rsgDrawText(rs_allocation, int x, int y);
377/**
378 * Binds the font object to be used for all subsequent font rendering calls
379 * @param font object to bind
380 */
381extern void __attribute__((overloadable))
382    rsgBindFont(rs_font font);
383/**
384 * Sets the font color for all subsequent rendering calls
385 * @param r red component
386 * @param g green component
387 * @param b blue component
388 * @param a alpha component
389 */
390extern void __attribute__((overloadable))
391    rsgFontColor(float r, float g, float b, float a);
392/**
393 * Returns the bounding box of the text relative to (0, 0)
394 * Any of left, right, top, bottom could be NULL
395 */
396extern void __attribute__((overloadable))
397    rsgMeasureText(const char *, int *left, int *right, int *top, int *bottom);
398/**
399 * \overload
400 */
401extern void __attribute__((overloadable))
402    rsgMeasureText(rs_allocation, int *left, int *right, int *top, int *bottom);
403/**
404 * Computes an axis aligned bounding box of a mesh object
405 */
406extern void __attribute__((overloadable))
407    rsgMeshComputeBoundingBox(rs_mesh mesh, float *minX, float *minY, float *minZ,
408                                                float *maxX, float *maxY, float *maxZ);
409/**
410 * \overload
411 */
412__inline__ static void __attribute__((overloadable, always_inline))
413rsgMeshComputeBoundingBox(rs_mesh mesh, float3 *bBoxMin, float3 *bBoxMax) {
414    float x1, y1, z1, x2, y2, z2;
415    rsgMeshComputeBoundingBox(mesh, &x1, &y1, &z1, &x2, &y2, &z2);
416    bBoxMin->x = x1;
417    bBoxMin->y = y1;
418    bBoxMin->z = z1;
419    bBoxMax->x = x2;
420    bBoxMax->y = y2;
421    bBoxMax->z = z2;
422}
423
424#endif //__LP64__
425#endif
426
427