1 /*
2  * Copyright (C) 2011 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 #include "rsContext.h"
18 #include "rsScriptC.h"
19 #include "rsMatrix4x4.h"
20 #include "rsMatrix3x3.h"
21 #include "rsMatrix2x2.h"
22 #include "rsMesh.h"
23 #include "rsgApiStructs.h"
24 
25 #include "utils/Timers.h"
26 #include "driver/rsdVertexArray.h"
27 #include "driver/rsdShaderCache.h"
28 #include "driver/rsdCore.h"
29 
30 #define GL_GLEXT_PROTOTYPES
31 
32 #include <GLES/gl.h>
33 #include <GLES/glext.h>
34 #include <GLES2/gl2.h>
35 #include <GLES2/gl2ext.h>
36 
37 #include <time.h>
38 
39 using namespace android;
40 using namespace android::renderscript;
41 
42 namespace android {
43 namespace renderscript {
44 
45 //////////////////////////////////////////////////////////////////////////////
46 // Context
47 //////////////////////////////////////////////////////////////////////////////
48 
rsrBindTexture(Context * rsc,ProgramFragment * pf,uint32_t slot,Allocation * a)49 void rsrBindTexture(Context *rsc, ProgramFragment *pf, uint32_t slot, Allocation *a) {
50     CHECK_OBJ_OR_NULL(a);
51     CHECK_OBJ(pf);
52     pf->bindTexture(rsc, slot, a);
53 }
54 
rsrBindConstant(Context * rsc,ProgramFragment * pf,uint32_t slot,Allocation * a)55 void rsrBindConstant(Context *rsc, ProgramFragment *pf, uint32_t slot, Allocation *a) {
56     CHECK_OBJ_OR_NULL(a);
57     CHECK_OBJ(pf);
58     pf->bindAllocation(rsc, a, slot);
59 }
60 
rsrBindConstant(Context * rsc,ProgramVertex * pv,uint32_t slot,Allocation * a)61 void rsrBindConstant(Context *rsc, ProgramVertex *pv, uint32_t slot, Allocation *a) {
62     CHECK_OBJ_OR_NULL(a);
63     CHECK_OBJ(pv);
64     pv->bindAllocation(rsc, a, slot);
65 }
66 
rsrBindSampler(Context * rsc,ProgramFragment * pf,uint32_t slot,Sampler * s)67 void rsrBindSampler(Context *rsc, ProgramFragment *pf, uint32_t slot, Sampler *s) {
68     CHECK_OBJ_OR_NULL(vs);
69     CHECK_OBJ(vpf);
70     pf->bindSampler(rsc, slot, s);
71 }
72 
rsrBindProgramStore(Context * rsc,ProgramStore * ps)73 void rsrBindProgramStore(Context *rsc, ProgramStore *ps) {
74     CHECK_OBJ_OR_NULL(ps);
75     rsc->setProgramStore(ps);
76 }
77 
rsrBindProgramFragment(Context * rsc,ProgramFragment * pf)78 void rsrBindProgramFragment(Context *rsc, ProgramFragment *pf) {
79     CHECK_OBJ_OR_NULL(pf);
80     rsc->setProgramFragment(pf);
81 }
82 
rsrBindProgramVertex(Context * rsc,ProgramVertex * pv)83 void rsrBindProgramVertex(Context *rsc, ProgramVertex *pv) {
84     CHECK_OBJ_OR_NULL(pv);
85     rsc->setProgramVertex(pv);
86 }
87 
rsrBindProgramRaster(Context * rsc,ProgramRaster * pr)88 void rsrBindProgramRaster(Context *rsc, ProgramRaster *pr) {
89     CHECK_OBJ_OR_NULL(pr);
90     rsc->setProgramRaster(pr);
91 }
92 
rsrBindFrameBufferObjectColorTarget(Context * rsc,Allocation * a,uint32_t slot)93 void rsrBindFrameBufferObjectColorTarget(Context *rsc, Allocation *a, uint32_t slot) {
94     CHECK_OBJ(va);
95     rsc->mFBOCache.bindColorTarget(rsc, a, slot);
96     rsc->mStateVertex.updateSize(rsc);
97 }
98 
rsrBindFrameBufferObjectDepthTarget(Context * rsc,Allocation * a)99 void rsrBindFrameBufferObjectDepthTarget(Context *rsc, Allocation *a) {
100     CHECK_OBJ(va);
101     rsc->mFBOCache.bindDepthTarget(rsc, a);
102     rsc->mStateVertex.updateSize(rsc);
103 }
104 
rsrClearFrameBufferObjectColorTarget(Context * rsc,uint32_t slot)105 void rsrClearFrameBufferObjectColorTarget(Context *rsc, uint32_t slot) {
106     rsc->mFBOCache.bindColorTarget(rsc, nullptr, slot);
107     rsc->mStateVertex.updateSize(rsc);
108 }
109 
rsrClearFrameBufferObjectDepthTarget(Context * rsc)110 void rsrClearFrameBufferObjectDepthTarget(Context *rsc) {
111     rsc->mFBOCache.bindDepthTarget(rsc, nullptr);
112     rsc->mStateVertex.updateSize(rsc);
113 }
114 
rsrClearFrameBufferObjectTargets(Context * rsc)115 void rsrClearFrameBufferObjectTargets(Context *rsc) {
116     rsc->mFBOCache.resetAll(rsc);
117     rsc->mStateVertex.updateSize(rsc);
118 }
119 
120 //////////////////////////////////////////////////////////////////////////////
121 // VP
122 //////////////////////////////////////////////////////////////////////////////
123 
rsrVpLoadProjectionMatrix(Context * rsc,const rsc_Matrix * m)124 void rsrVpLoadProjectionMatrix(Context *rsc, const rsc_Matrix *m) {
125     rsc->getProgramVertex()->setProjectionMatrix(rsc, m);
126 }
127 
rsrVpLoadModelMatrix(Context * rsc,const rsc_Matrix * m)128 void rsrVpLoadModelMatrix(Context *rsc, const rsc_Matrix *m) {
129     rsc->getProgramVertex()->setModelviewMatrix(rsc, m);
130 }
131 
rsrVpLoadTextureMatrix(Context * rsc,const rsc_Matrix * m)132 void rsrVpLoadTextureMatrix(Context *rsc, const rsc_Matrix *m) {
133     rsc->getProgramVertex()->setTextureMatrix(rsc, m);
134 }
135 
rsrPfConstantColor(Context * rsc,ProgramFragment * pf,float r,float g,float b,float a)136 void rsrPfConstantColor(Context *rsc, ProgramFragment *pf,
137                         float r, float g, float b, float a) {
138     CHECK_OBJ(pf);
139     pf->setConstantColor(rsc, r, g, b, a);
140 }
141 
rsrVpGetProjectionMatrix(Context * rsc,rsc_Matrix * m)142 void rsrVpGetProjectionMatrix(Context *rsc, rsc_Matrix *m) {
143     rsc->getProgramVertex()->getProjectionMatrix(rsc, m);
144 }
145 
146 //////////////////////////////////////////////////////////////////////////////
147 // Drawing
148 //////////////////////////////////////////////////////////////////////////////
149 
150 
rsrDrawPath(Context * rsc,Path * sm)151 void rsrDrawPath(Context *rsc, Path *sm) {
152     // Never shipped in a working state
153 }
154 
rsrDrawMesh(Context * rsc,Mesh * sm)155 void rsrDrawMesh(Context *rsc, Mesh *sm) {
156     CHECK_OBJ(sm);
157     if (!rsc->setupCheck()) {
158         return;
159     }
160     sm->render(rsc);
161 }
162 
rsrDrawMeshPrimitive(Context * rsc,Mesh * sm,uint32_t primIndex)163 void rsrDrawMeshPrimitive(Context *rsc, Mesh *sm, uint32_t primIndex) {
164     CHECK_OBJ(sm);
165     if (!rsc->setupCheck()) {
166         return;
167     }
168     sm->renderPrimitive(rsc, primIndex);
169 }
170 
rsrDrawMeshPrimitiveRange(Context * rsc,Mesh * sm,uint32_t primIndex,uint32_t start,uint32_t len)171 void rsrDrawMeshPrimitiveRange(Context *rsc, Mesh *sm, uint32_t primIndex,
172                                uint32_t start, uint32_t len) {
173     CHECK_OBJ(sm);
174     if (!rsc->setupCheck()) {
175         return;
176     }
177     sm->renderPrimitiveRange(rsc, primIndex, start, len);
178 }
179 
rsrMeshComputeBoundingBox(Context * rsc,Mesh * sm,float * minX,float * minY,float * minZ,float * maxX,float * maxY,float * maxZ)180 void rsrMeshComputeBoundingBox(Context *rsc, Mesh *sm,
181                                float *minX, float *minY, float *minZ,
182                                float *maxX, float *maxY, float *maxZ) {
183     CHECK_OBJ(sm);
184     sm->computeBBox(rsc);
185     *minX = sm->mBBoxMin[0];
186     *minY = sm->mBBoxMin[1];
187     *minZ = sm->mBBoxMin[2];
188     *maxX = sm->mBBoxMax[0];
189     *maxY = sm->mBBoxMax[1];
190     *maxZ = sm->mBBoxMax[2];
191 }
192 
193 
194 //////////////////////////////////////////////////////////////////////////////
195 //
196 //////////////////////////////////////////////////////////////////////////////
197 
198 
rsrColor(Context * rsc,float r,float g,float b,float a)199 void rsrColor(Context *rsc, float r, float g, float b, float a) {
200     ProgramFragment *pf = rsc->getProgramFragment();
201     pf->setConstantColor(rsc, r, g, b, a);
202 }
203 
rsrPrepareClear(Context * rsc)204 void rsrPrepareClear(Context *rsc) {
205     rsc->mFBOCache.setup(rsc);
206     rsc->setupProgramStore();
207 }
208 
rsrGetWidth(Context * rsc)209 uint32_t rsrGetWidth(Context *rsc) {
210     return rsc->getWidth();
211 }
212 
rsrGetHeight(Context * rsc)213 uint32_t rsrGetHeight(Context *rsc) {
214     return rsc->getHeight();
215 }
216 
rsrDrawTextAlloc(Context * rsc,Allocation * a,int x,int y)217 void rsrDrawTextAlloc(Context *rsc, Allocation *a, int x, int y) {
218     const char *text = (const char *)rsc->mHal.funcs.allocation.lock1D(rsc, a);
219     size_t allocSize = a->getType()->getPackedSizeBytes();
220     rsc->mStateFont.renderText(text, allocSize, x, y);
221     rsc->mHal.funcs.allocation.unlock1D(rsc, a);
222 }
223 
rsrDrawText(Context * rsc,const char * text,int x,int y)224 void rsrDrawText(Context *rsc, const char *text, int x, int y) {
225     size_t textLen = strlen(text);
226     rsc->mStateFont.renderText(text, textLen, x, y);
227 }
228 
SetMetrics(Font::Rect * metrics,int32_t * left,int32_t * right,int32_t * top,int32_t * bottom)229 static void SetMetrics(Font::Rect *metrics,
230                        int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
231     if (left) {
232         *left = metrics->left;
233     }
234     if (right) {
235         *right = metrics->right;
236     }
237     if (top) {
238         *top = metrics->top;
239     }
240     if (bottom) {
241         *bottom = metrics->bottom;
242     }
243 }
244 
rsrMeasureTextAlloc(Context * rsc,Allocation * a,int32_t * left,int32_t * right,int32_t * top,int32_t * bottom)245 void rsrMeasureTextAlloc(Context *rsc, Allocation *a,
246                          int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
247     CHECK_OBJ(a);
248     const char *text = (const char *)rsc->mHal.funcs.allocation.lock1D(rsc, a);
249     size_t textLen = a->getType()->getPackedSizeBytes();
250     Font::Rect metrics;
251     rsc->mStateFont.measureText(text, textLen, &metrics);
252     SetMetrics(&metrics, left, right, top, bottom);
253     rsc->mHal.funcs.allocation.unlock1D(rsc, a);
254 }
255 
rsrMeasureText(Context * rsc,const char * text,int32_t * left,int32_t * right,int32_t * top,int32_t * bottom)256 void rsrMeasureText(Context *rsc, const char *text,
257                     int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
258     size_t textLen = strlen(text);
259     Font::Rect metrics;
260     rsc->mStateFont.measureText(text, textLen, &metrics);
261     SetMetrics(&metrics, left, right, top, bottom);
262 }
263 
rsrBindFont(Context * rsc,Font * font)264 void rsrBindFont(Context *rsc, Font *font) {
265     CHECK_OBJ(font);
266     rsi_ContextBindFont(rsc, font);
267 }
268 
rsrFontColor(Context * rsc,float r,float g,float b,float a)269 void rsrFontColor(Context *rsc, float r, float g, float b, float a) {
270     rsc->mStateFont.setFontColor(r, g, b, a);
271 }
272 
273 }
274 }
275