summaryrefslogtreecommitdiffstats
path: root/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/FullscreenBlur.java
blob: 8c6b5fca003bb64229b9e308c1caf08ca918b0b0 (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
/*
 * Copyright (C) 2011 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.
 */


package com.android.scenegraph;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.renderscript.*;
import android.renderscript.Allocation.MipmapControl;
import android.renderscript.Element.Builder;
import android.renderscript.Font.Style;
import android.renderscript.Program.TextureType;
import android.renderscript.ProgramStore.DepthFunc;
import android.util.Log;

import java.util.ArrayList;

class FullscreenBlur {

    static Allocation sRenderTargetBlur0Color;
    static Allocation sRenderTargetBlur0Depth;
    static Allocation sRenderTargetBlur1Color;
    static Allocation sRenderTargetBlur1Depth;
    static Allocation sRenderTargetBlur2Color;
    static Allocation sRenderTargetBlur2Depth;

    static ProgramFragment mPF_BlurH;
    static ProgramFragment mPF_BlurV;
    static ProgramFragment mPF_SelectColor;
    static ProgramFragment mPF_Texture;
    static ScriptField_FBlurOffsets_s mFsBlurHConst;
    static ScriptField_FBlurOffsets_s mFsBlurVConst;
    static ProgramVertex mPV_Paint;
    static ProgramVertex mPV_Blur;

    // This is only used when full screen blur is enabled
    // Basically, it's the offscreen render targets
    static void createRenderTargets(RenderScriptGL rs, int w, int h) {
        Type.Builder b = new Type.Builder(rs, Element.RGBA_8888(rs));
        b.setX(w/8).setY(h/8);
        Type renderType = b.create();
        sRenderTargetBlur0Color = Allocation.createTyped(rs, renderType,
                                                         Allocation.USAGE_GRAPHICS_TEXTURE |
                                                         Allocation.USAGE_GRAPHICS_RENDER_TARGET);
        sRenderTargetBlur1Color = Allocation.createTyped(rs, renderType,
                                                         Allocation.USAGE_GRAPHICS_TEXTURE |
                                                         Allocation.USAGE_GRAPHICS_RENDER_TARGET);
        sRenderTargetBlur2Color = Allocation.createTyped(rs, renderType,
                                                         Allocation.USAGE_GRAPHICS_TEXTURE |
                                                         Allocation.USAGE_GRAPHICS_RENDER_TARGET);

        b = new Type.Builder(rs,
                             Element.createPixel(rs, Element.DataType.UNSIGNED_16,
                                                 Element.DataKind.PIXEL_DEPTH));
        b.setX(w/8).setY(h/8);
        sRenderTargetBlur0Depth = Allocation.createTyped(rs,
                                                         b.create(),
                                                         Allocation.USAGE_GRAPHICS_RENDER_TARGET);

        sRenderTargetBlur1Depth = Allocation.createTyped(rs,
                                                         b.create(),
                                                         Allocation.USAGE_GRAPHICS_RENDER_TARGET);
        sRenderTargetBlur2Depth = Allocation.createTyped(rs,
                                                         b.create(),
                                                         Allocation.USAGE_GRAPHICS_RENDER_TARGET);
    }

    static void addBlurPasses(Scene scene, RenderScriptGL rs, SceneManager sceneManager) {
        ArrayList<RenderableBase> allDraw = scene.getRenderables();
        int numDraw = allDraw.size();

        RenderState drawTex = new RenderState(mPV_Blur, mPF_Texture,
                                            BLEND_ADD_DEPTH_NONE(rs),
                                            ProgramRaster.CULL_NONE(rs));

        RenderState selectCol = new RenderState(mPV_Blur, mPF_SelectColor,
                                            ProgramStore.BLEND_NONE_DEPTH_NONE(rs),
                                            ProgramRaster.CULL_NONE(rs));

        RenderState hBlur = new RenderState(mPV_Blur, mPF_BlurH,
                                            ProgramStore.BLEND_NONE_DEPTH_NONE(rs),
                                            ProgramRaster.CULL_NONE(rs));

        RenderState vBlur = new RenderState(mPV_Blur, mPF_BlurV,
                                            ProgramStore.BLEND_NONE_DEPTH_NONE(rs),
                                            ProgramRaster.CULL_NONE(rs));

        RenderPass blurSourcePass = new RenderPass();
        blurSourcePass.setColorTarget(sRenderTargetBlur0Color);
        blurSourcePass.setDepthTarget(sRenderTargetBlur0Depth);
        blurSourcePass.setClearColor(new Float4(1.0f, 1.0f, 1.0f, 1.0f));
        blurSourcePass.setShouldClearColor(true);
        blurSourcePass.setClearDepth(1.0f);
        blurSourcePass.setShouldClearDepth(true);
        blurSourcePass.setCamera(scene.getCameras().get(1));
        for (int i = 0; i < numDraw; i ++) {
            blurSourcePass.appendRenderable((Renderable)allDraw.get(i));
        }
        scene.appendRenderPass(blurSourcePass);

        RenderPass selectColorPass = new RenderPass();
        selectColorPass.setColorTarget(sRenderTargetBlur2Color);
        selectColorPass.setDepthTarget(sRenderTargetBlur2Depth);
        selectColorPass.setShouldClearColor(false);
        selectColorPass.setShouldClearDepth(false);
        selectColorPass.setCamera(scene.getCameras().get(1));
        // Make blur shape
        Renderable quad = sceneManager.getRenderableQuad("ScreenAlignedQuadS", selectCol);
        quad.updateTextures(rs, sRenderTargetBlur0Color, 0);
        selectColorPass.appendRenderable(quad);
        scene.appendRenderPass(selectColorPass);

        RenderPass horizontalBlurPass = new RenderPass();
        horizontalBlurPass.setColorTarget(sRenderTargetBlur1Color);
        horizontalBlurPass.setDepthTarget(sRenderTargetBlur1Depth);
        horizontalBlurPass.setShouldClearColor(false);
        horizontalBlurPass.setShouldClearDepth(false);
        horizontalBlurPass.setCamera(scene.getCameras().get(1));
        // Make blur shape
        quad = sceneManager.getRenderableQuad("ScreenAlignedQuadH", hBlur);
        quad.updateTextures(rs, sRenderTargetBlur2Color, 0);
        horizontalBlurPass.appendRenderable(quad);
        scene.appendRenderPass(horizontalBlurPass);

        RenderPass verticalBlurPass = new RenderPass();
        verticalBlurPass.setColorTarget(sRenderTargetBlur2Color);
        verticalBlurPass.setDepthTarget(sRenderTargetBlur2Depth);
        verticalBlurPass.setShouldClearColor(false);
        verticalBlurPass.setShouldClearDepth(false);
        verticalBlurPass.setCamera(scene.getCameras().get(1));
        // Make blur shape
        quad = sceneManager.getRenderableQuad("ScreenAlignedQuadV", vBlur);
        quad.updateTextures(rs, sRenderTargetBlur1Color, 0);
        verticalBlurPass.appendRenderable(quad);
        scene.appendRenderPass(verticalBlurPass);

    }

    static void addCompositePass(Scene scene, RenderScriptGL rs, SceneManager sceneManager) {
        RenderState drawTex = new RenderState(mPV_Blur, mPF_Texture,
                                            BLEND_ADD_DEPTH_NONE(rs),
                                            ProgramRaster.CULL_NONE(rs));

        RenderPass compositePass = new RenderPass();
        compositePass.setClearColor(new Float4(1.0f, 1.0f, 1.0f, 0.0f));
        compositePass.setShouldClearColor(false);
        compositePass.setClearDepth(1.0f);
        compositePass.setShouldClearDepth(false);
        compositePass.setCamera(scene.getCameras().get(1));
        Renderable quad = sceneManager.getRenderableQuad("ScreenAlignedQuad", drawTex);
        quad.updateTextures(rs, sRenderTargetBlur2Color, 0);
        compositePass.appendRenderable(quad);

        scene.appendRenderPass(compositePass);
    }

    private static ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) {
        ProgramStore.Builder builder = new ProgramStore.Builder(rs);
        builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
        builder.setBlendFunc(ProgramStore.BlendSrcFunc.ONE, ProgramStore.BlendDstFunc.ONE);
        builder.setDitherEnabled(false);
        builder.setDepthMaskEnabled(false);
        return builder.create();
    }

    static void initShaders(Resources res, RenderScript rs,
                            ScriptField_VShaderParams_s vsConst,
                            ScriptField_FShaderParams_s fsConst) {
        ProgramVertex.Builder vb = new ProgramVertex.Builder(rs);
        vb.addConstant(vsConst.getAllocation().getType());
        vb.addInput(ScriptField_VertexShaderInputs_s.createElement(rs));
        vb.setShader(res, R.raw.blur_vertex);
        mPV_Blur = vb.create();
        mPV_Blur.bindConstants(vsConst.getAllocation(), 0);

        ProgramFragment.Builder fb = new ProgramFragment.Builder(rs);
        fb.addConstant(fsConst.getAllocation().getType());
        fb.setShader(res, R.raw.texture);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_Texture = fb.create();
        mPF_Texture.bindConstants(fsConst.getAllocation(), 0);
        mPF_Texture.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(rs), 0);

        mFsBlurHConst = new ScriptField_FBlurOffsets_s(rs, 1);
        float xAdvance = 1.0f / (float)sRenderTargetBlur0Color.getType().getX();
        ScriptField_FBlurOffsets_s.Item item = new ScriptField_FBlurOffsets_s.Item();
        item.blurOffset0 = - xAdvance * 2.5f;
        item.blurOffset1 = - xAdvance * 0.5f;
        item.blurOffset2 =   xAdvance * 1.5f;
        item.blurOffset3 =   xAdvance * 3.5f;
        mFsBlurHConst.set(item, 0, true);

        fb = new ProgramFragment.Builder(rs);
        fb.addConstant(mFsBlurHConst.getAllocation().getType());
        fb.setShader(res, R.raw.blur_h);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_BlurH = fb.create();
        mPF_BlurH.bindConstants(mFsBlurHConst.getAllocation(), 0);
        mPF_BlurH.bindTexture(sRenderTargetBlur0Color, 0);
        mPF_BlurH.bindSampler(Sampler.CLAMP_LINEAR(rs), 0);

        mFsBlurVConst = new ScriptField_FBlurOffsets_s(rs, 1);
        float yAdvance = 1.0f / (float)sRenderTargetBlur0Color.getType().getY();
        item.blurOffset0 = - yAdvance * 2.5f;
        item.blurOffset1 = - yAdvance * 0.5f;
        item.blurOffset2 =   yAdvance * 1.5f;
        item.blurOffset3 =   yAdvance * 3.5f;
        mFsBlurVConst.set(item, 0, true);

        fb = new ProgramFragment.Builder(rs);
        fb.addConstant(mFsBlurVConst.getAllocation().getType());
        fb.setShader(res, R.raw.blur_v);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_BlurV = fb.create();
        mPF_BlurV.bindConstants(mFsBlurVConst.getAllocation(), 0);
        mPF_BlurV.bindTexture(sRenderTargetBlur1Color, 0);
        mPF_BlurV.bindSampler(Sampler.CLAMP_LINEAR(rs), 0);

        fb = new ProgramFragment.Builder(rs);
        //fb.addConstant(mFsBlurVConst.getAllocation().getType());
        fb.setShader(res, R.raw.select_color);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_SelectColor = fb.create();
        //mPF_SelectColor.bindConstants(mFsBlurVConst.getAllocation(), 0);
        //mPF_SelectColor.bindTexture(sRenderTargetBlur1Color, 0);
        mPF_SelectColor.bindSampler(Sampler.CLAMP_LINEAR(rs), 0);
    }

}