summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/grass/grass.rs
blob: b03f19c9352a2ba1c5af1b78ad35585e8e8d0efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma version(1)

#pragma rs java_package_name(com.android.wallpaper.grass)

#include "rs_graphics.rsh"

#define RSID_BLADES_BUFFER 2

#define TESSELATION 0.5f
#define HALF_TESSELATION 0.25f
#define MAX_BEND 0.09f
#define SECONDS_IN_DAY 86400.0f
#define PI 3.1415926f
#define HALF_PI 1.570796326f
#define REAL_TIME 1

int gBladesCount;
int gIndexCount;
int gWidth;
int gHeight;
float gXOffset;
float gDawn;
float gMorning;
float gAfternoon;
float gDusk;
int gIsPreview;
rs_program_vertex gPVBackground;
rs_program_fragment gPFBackground;
rs_program_fragment gPFGrass;
rs_program_store gPSBackground;
rs_allocation gTNight;
rs_allocation gTSunset;
rs_allocation gTSunrise;
rs_allocation gTSky;
rs_allocation gTAa;
rs_mesh gBladesMesh;


typedef struct Blade {
    float angle;
    int size;
    float xPos;
    float yPos;
    float offset;
    float scale;
    float lengthX;
    float lengthY;
    float hardness;
    float h;
    float s;
    float b;
    float turbulencex;
} Blade_t;
Blade_t *Blades;

typedef struct RS_PACKED Vertex {
    uchar4 color;
    float2 position;
    float2 texture0;
} __attribute__((packed,aligned(4))) Vertex_t;
Vertex_t *Verticies;

#define B 0x100
#define BM 0xff
#define N 0x1000

static int p[B + B + 2];
static float g3[B + B + 2][3];
static float g2[B + B + 2][2];
static float g1[B + B + 2];

static float noise_sCurve(float t)
{
    return t * t * (3.0f - 2.0f * t);
}

static void normalizef2(float v[])
{
    float s = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
    v[0] = v[0] / s;
    v[1] = v[1] / s;
}

static void normalizef3(float v[])
{
    float s = (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
    v[0] = v[0] / s;
    v[1] = v[1] / s;
    v[2] = v[2] / s;
}

void init()
{
    int i, j, k;

    for (i = 0; i < B; i++) {
        p[i] = i;

        g1[i] = (float)(rsRand(B * 2) - B) / B;

        for (j = 0; j < 2; j++)
            g2[i][j] = (float)(rsRand(B * 2) - B) / B;
        normalizef2(g2[i]);

        for (j = 0; j < 3; j++)
            g3[i][j] = (float)(rsRand(B * 2) - B) / B;
        normalizef3(g3[i]);
    }

    for (i = B-1; i >= 0; i--) {
        k = p[i];
        p[i] = p[j = rsRand(B)];
        p[j] = k;
    }

    for (i = 0; i < B + 2; i++) {
        p[B + i] = p[i];
        g1[B + i] = g1[i];
        for (j = 0; j < 2; j++)
            g2[B + i][j] = g2[i][j];
        for (j = 0; j < 3; j++)
            g3[B + i][j] = g3[i][j];
    }
}

static float noisef(float x)
{
    int bx0, bx1;
    float rx0, rx1, sx, t, u, v;

    t = x + N;
    bx0 = ((int)t) & BM;
    bx1 = (bx0+1) & BM;
    rx0 = t - (int)t;
    rx1 = rx0 - 1.0f;

    sx = noise_sCurve(rx0);

    u = rx0 * g1[p[bx0]];
    v = rx1 * g1[p[bx1]];
    return 2.3f * mix(u, v, sx);
}

static float noisef2(float x, float y)
{
    int bx0, bx1, by0, by1, b00, b10, b01, b11;
    float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
    float *q;
    int i, j;

    t = x + N;
    bx0 = ((int)t) & BM;
    bx1 = (bx0+1) & BM;
    rx0 = t - (int)t;
    rx1 = rx0 - 1.0f;

    t = y + N;
    by0 = ((int)t) & BM;
    by1 = (by0+1) & BM;
    ry0 = t - (int)t;
    ry1 = ry0 - 1.0f;

    i = p[bx0];
    j = p[bx1];

    b00 = p[i + by0];
    b10 = p[j + by0];
    b01 = p[i + by1];
    b11 = p[j + by1];

    sx = noise_sCurve(rx0);
    sy = noise_sCurve(ry0);

    q = g2[b00]; u = rx0 * q[0] + ry0 * q[1];
    q = g2[b10]; v = rx1 * q[0] + ry0 * q[1];
    a = mix(u, v, sx);

    q = g2[b01]; u = rx0 * q[0] + ry1 * q[1];
    q = g2[b11]; v = rx1 * q[0] + ry1 * q[1];
    b = mix(u, v, sx);

    return 1.5f * mix(a, b, sy);
}

static float turbulencef2(float x, float y, float octaves)
{
    float t = 0.0f;

    for (float f = 1.0f; f <= octaves; f *= 2)
        t += fabs(noisef2(f * x, f * y)) / f;
    return t;
}

void updateBlades()
{
    Blade_t *bladeStruct = Blades;
    for (int i = 0; i < gBladesCount; i ++) {
        float xpos = rsRand(-gWidth, gWidth);
        bladeStruct->xPos = xpos;
        bladeStruct->turbulencex = xpos * 0.006f;
        bladeStruct->yPos = gHeight;
        bladeStruct++;
    }
}

static float time(int isPreview) {
    if (REAL_TIME && !isPreview) {
        rs_time_t currentTime = rsTime(0);
        rs_tm localTime;
        rsLocaltime(&localTime, &currentTime);
        return (localTime.tm_hour * 3600.0f +
                localTime.tm_min * 60.0f +
                localTime.tm_sec) / SECONDS_IN_DAY;
    }
    float t = rsUptimeMillis() / 30000.0f;
    return t - (int) t;
}

static void alpha(float a) {
    rsgProgramFragmentConstantColor(gPFBackground, 1.0f, 1.0f, 1.0f, a);
}

static float normf(float start, float stop, float value) {
    return (value - start) / (stop - start);
}

static void drawNight(int width, int height) {
    rsgBindTexture(gPFBackground, 0, gTNight);
    rsgDrawQuadTexCoords(
            0.0f, -32.0f, 0.0f,
            0.0f, 1.0f,
            0.0f, height, 0.0f,
            0.0f, 0.0f,
            width, height, 0.0f,
            2.0f, 0.0f,
            width, -32.0f, 0.0f,
            2.0f, 1.0f);
}

static void drawSunrise(int width, int height) {
    rsgBindTexture(gPFBackground, 0, gTSunrise);
    rsgDrawRect(0.0f, 0.0f, width, height, 0.0f);
}

static void drawNoon(int width, int height) {
    rsgBindTexture(gPFBackground, 0, gTSky);
    rsgDrawRect(0.0f, 0.0f, width, height, 0.0f);
}

static void drawSunset(int width, int height) {
    rsgBindTexture(gPFBackground, 0, gTSunset);
    rsgDrawRect(0.0f, 0.0f, width, height, 0.0f);
}


static uchar4 hsbToRgb(float h, float s, float b)
{
    float red = 0.0f;
    float green = 0.0f;
    float blue = 0.0f;

    float x = h;
    float y = s;
    float z = b;

    float hf = (x - (int) x) * 6.0f;
    int ihf = (int) hf;
    float f = hf - ihf;
    float pv = z * (1.0f - y);
    float qv = z * (1.0f - y * f);
    float tv = z * (1.0f - y * (1.0f - f));

    switch (ihf) {
        case 0:         // Red is the dominant color
            red = z;
            green = tv;
            blue = pv;
            break;
        case 1:         // Green is the dominant color
            red = qv;
            green = z;
            blue = pv;
            break;
        case 2:
            red = pv;
            green = z;
            blue = tv;
            break;
        case 3:         // Blue is the dominant color
            red = pv;
            green = qv;
            blue = z;
            break;
        case 4:
            red = tv;
            green = pv;
            blue = z;
            break;
        case 5:         // Red is the dominant color
            red = z;
            green = pv;
            blue = qv;
            break;
    }

    return rsPackColorTo8888(red, green, blue);
}

static int drawBlade(Blade_t *bladeStruct, Vertex_t *v,
        float brightness, float xOffset, float now) {

    float scale = bladeStruct->scale;
    float angle = bladeStruct->angle;
    float xpos = bladeStruct->xPos + xOffset;
    int size = bladeStruct->size;

    uchar4 color = hsbToRgb(bladeStruct->h, bladeStruct->s,
                            mix(0.f, bladeStruct->b, brightness));

    float newAngle = (turbulencef2(bladeStruct->turbulencex, now, 4.0f) - 0.5f) * 0.5f;
    angle = clamp(angle + (newAngle + bladeStruct->offset - angle) * 0.15f, -MAX_BEND, MAX_BEND);

    float currentAngle = HALF_PI;

    float bottomX = xpos;
    float bottomY = bladeStruct->yPos;

    float d = angle * bladeStruct->hardness;


    float si = size * scale;
    float bottomLeft = bottomX - si;
    float bottomRight = bottomX + si;
    float bottom = bottomY + HALF_TESSELATION;

    v[0].color = color;                          // V1.ABGR
    v[0].position.x = bottomLeft;                    // V1.X
    v[0].position.y = bottom;                        // V1.Y
    v[0].texture0.x = 0.f;                           // V1.s
    v[0].texture0.y = 0.f;                           // V1.t
                                                    //
    v[1].color = color;                          // V2.ABGR
    v[1].position.x = bottomRight;                   // V2.X
    v[1].position.y = bottom;                        // V2.Y
    v[1].texture0.x = 1.f;                           // V2.s
    v[1].texture0.y = 0.f;                           // V2.t
    v += 2;

    for ( ; size > 0; size -= 1) {
        float topX = bottomX - cos(currentAngle) * bladeStruct->lengthX;
        float topY = bottomY - sin(currentAngle) * bladeStruct->lengthY;

        si = (float)size * scale;
        float spi = si - scale;

        float topLeft = topX - spi;
        float topRight = topX + spi;

        v[0].color = color;                          // V1.ABGR
        v[0].position.x = topLeft;                       // V1.X
        v[0].position.y = topY;                          // V1.Y
        v[0].texture0.x = 0.f;                           // V1.s
        v[0].texture0.y = 0.f;                           // V1.t

        v[1].color = color;                          // V2.ABGR
        v[1].position.x = topRight;                      // V2.X
        v[1].position.y = topY;                          // V2.Y
        v[1].texture0.x = 1.f;                           // V2.s
        v[1].texture0.y = 0.f;                           // V2.t

        v += 2;
        bottomX = topX;
        bottomY = topY;
        currentAngle += d;
    }

    bladeStruct->angle = angle;

    // 2 vertices per triangle, 5 properties per vertex (RGBA, X, Y, S, T)
    return bladeStruct->size * 2 + 2;
}

static void drawBlades(float brightness, float xOffset) {
    // For anti-aliasing
    rsgBindTexture(gPFGrass, 0, gTAa);

    Blade_t *bladeStruct = Blades;
    Vertex_t *vtx = Verticies;
    float now = rsUptimeMillis() * 0.00004f;

    for (int i = 0; i < gBladesCount; i += 1) {
        int offset = drawBlade(bladeStruct, vtx, brightness, xOffset, now);
        vtx += offset;
        bladeStruct ++;
    }

    rsgDrawMesh(gBladesMesh, 0, 0, gIndexCount);
}

int root(void) {
    float x = mix((float)gWidth, 0.f, gXOffset);

    float now = time(gIsPreview);

    rsgBindProgramVertex(gPVBackground);
    rsgBindProgramFragment(gPFBackground);
    rsgBindProgramStore(gPSBackground);
    alpha(1.0f);

    float newB = 1.0f;
    if (now >= 0.0f && now < gDawn) {                    // Draw night
        drawNight(gWidth, gHeight);
        newB = 0.0f;
    } else if (now >= gDawn && now <= gMorning) {         // Draw sunrise
        float half = gDawn + (gMorning - gDawn) * 0.5f;
        if (now <= half) {                              // Draw night->sunrise
            drawNight(gWidth, gHeight);
            newB = normf(gDawn, half, now);
            alpha(newB);
            drawSunrise(gWidth, gHeight);
        } else {                                        // Draw sunrise->day
            drawSunrise(gWidth, gHeight);
            alpha(normf(half, gMorning, now));
            drawNoon(gWidth, gHeight);
        }
    } else if (now > gMorning && now < gAfternoon) {      // Draw day
        drawNoon(gWidth, gHeight);
    } else if (now >= gAfternoon && now <= gDusk) {       // Draw sunset
        float half = gAfternoon + (gDusk - gAfternoon) * 0.5f;
        if (now <= half) {                              // Draw day->sunset
            drawNoon(gWidth, gHeight);
            newB = normf(gAfternoon, half, now);
            alpha(newB);
            newB = 1.0f - newB;
            drawSunset(gWidth, gHeight);
        } else {                                        // Draw sunset->night
            drawSunset(gWidth, gHeight);
            alpha(normf(half, gDusk, now));
            drawNight(gWidth, gHeight);
            newB = 0.0f;
        }
    } else if (now > gDusk) {                            // Draw night
        drawNight(gWidth, gHeight);
        newB = 0.0f;
    }

    rsgBindProgramFragment(gPFGrass);
    drawBlades(newB, x);

    return 50;
}