1
2 /*
3 * Copyright (C) 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 "rsContext.h"
19
20 using namespace android;
21 using namespace android::renderscript;
22
23
Adapter1D(Context * rsc)24 Adapter1D::Adapter1D(Context *rsc) : ObjectBase(rsc)
25 {
26 mAllocFile = __FILE__;
27 mAllocLine = __LINE__;
28 reset();
29 }
30
Adapter1D(Context * rsc,Allocation * a)31 Adapter1D::Adapter1D(Context *rsc, Allocation *a) : ObjectBase(rsc)
32 {
33 mAllocFile = __FILE__;
34 mAllocLine = __LINE__;
35 reset();
36 setAllocation(a);
37 }
38
reset()39 void Adapter1D::reset()
40 {
41 mY = 0;
42 mZ = 0;
43 mLOD = 0;
44 mFace = 0;
45 }
46
getElement(uint32_t x)47 void * Adapter1D::getElement(uint32_t x)
48 {
49 rsAssert(mAllocation.get());
50 rsAssert(mAllocation->getPtr());
51 rsAssert(mAllocation->getType());
52 uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr());
53 ptr += mAllocation->getType()->getLODOffset(mLOD, x, mY);
54 return ptr;
55 }
56
subData(uint32_t xoff,uint32_t count,const void * data)57 void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data)
58 {
59 if (mAllocation.get() && mAllocation.get()->getType()) {
60 void *ptr = getElement(xoff);
61 count *= mAllocation.get()->getType()->getElementSizeBytes();
62 memcpy(ptr, data, count);
63 }
64 }
65
data(const void * data)66 void Adapter1D::data(const void *data)
67 {
68 memcpy(getElement(0),
69 data,
70 mAllocation.get()->getType()->getSizeBytes());
71 }
72
73 namespace android {
74 namespace renderscript {
75
rsi_Adapter1DCreate(Context * rsc)76 RsAdapter1D rsi_Adapter1DCreate(Context *rsc)
77 {
78 Adapter1D *a = new Adapter1D(rsc);
79 a->incUserRef();
80 return a;
81 }
82
rsi_Adapter1DBindAllocation(Context * rsc,RsAdapter1D va,RsAllocation valloc)83 void rsi_Adapter1DBindAllocation(Context *rsc, RsAdapter1D va, RsAllocation valloc)
84 {
85 Adapter1D * a = static_cast<Adapter1D *>(va);
86 Allocation * alloc = static_cast<Allocation *>(valloc);
87 a->setAllocation(alloc);
88 }
89
rsi_Adapter1DSetConstraint(Context * rsc,RsAdapter1D va,RsDimension dim,uint32_t value)90 void rsi_Adapter1DSetConstraint(Context *rsc, RsAdapter1D va, RsDimension dim, uint32_t value)
91 {
92 Adapter1D * a = static_cast<Adapter1D *>(va);
93 switch(dim) {
94 case RS_DIMENSION_X:
95 rsAssert(!"Cannot contrain X in an 1D adapter");
96 return;
97 case RS_DIMENSION_Y:
98 a->setY(value);
99 break;
100 case RS_DIMENSION_Z:
101 a->setZ(value);
102 break;
103 case RS_DIMENSION_LOD:
104 a->setLOD(value);
105 break;
106 case RS_DIMENSION_FACE:
107 a->setFace(value);
108 break;
109 default:
110 rsAssert(!"Unimplemented constraint");
111 return;
112 }
113 }
114
rsi_Adapter1DSubData(Context * rsc,RsAdapter1D va,uint32_t xoff,uint32_t count,const void * data)115 void rsi_Adapter1DSubData(Context *rsc, RsAdapter1D va, uint32_t xoff, uint32_t count, const void *data)
116 {
117 Adapter1D * a = static_cast<Adapter1D *>(va);
118 a->subData(xoff, count, data);
119 }
120
rsi_Adapter1DData(Context * rsc,RsAdapter1D va,const void * data)121 void rsi_Adapter1DData(Context *rsc, RsAdapter1D va, const void *data)
122 {
123 Adapter1D * a = static_cast<Adapter1D *>(va);
124 a->data(data);
125 }
126
127 }
128 }
129
130 //////////////////////////
131
Adapter2D(Context * rsc)132 Adapter2D::Adapter2D(Context *rsc) : ObjectBase(rsc)
133 {
134 mAllocFile = __FILE__;
135 mAllocLine = __LINE__;
136 reset();
137 }
138
Adapter2D(Context * rsc,Allocation * a)139 Adapter2D::Adapter2D(Context *rsc, Allocation *a) : ObjectBase(rsc)
140 {
141 mAllocFile = __FILE__;
142 mAllocLine = __LINE__;
143 reset();
144 setAllocation(a);
145 }
146
reset()147 void Adapter2D::reset()
148 {
149 mZ = 0;
150 mLOD = 0;
151 mFace = 0;
152 }
153
getElement(uint32_t x,uint32_t y) const154 void * Adapter2D::getElement(uint32_t x, uint32_t y) const
155 {
156 rsAssert(mAllocation.get());
157 rsAssert(mAllocation->getPtr());
158 rsAssert(mAllocation->getType());
159 uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr());
160 ptr += mAllocation->getType()->getLODOffset(mLOD, x, y);
161 return ptr;
162 }
163
subData(uint32_t xoff,uint32_t yoff,uint32_t w,uint32_t h,const void * data)164 void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
165 {
166 rsAssert(mAllocation.get());
167 rsAssert(mAllocation->getPtr());
168 rsAssert(mAllocation->getType());
169
170 uint32_t eSize = mAllocation.get()->getType()->getElementSizeBytes();
171 uint32_t lineSize = eSize * w;
172 uint32_t destW = getDimX();
173
174 const uint8_t *src = static_cast<const uint8_t *>(data);
175 for (uint32_t line=yoff; line < (yoff+h); line++) {
176 memcpy(getElement(xoff, line), src, lineSize);
177 src += lineSize;
178 }
179 }
180
data(const void * data)181 void Adapter2D::data(const void *data)
182 {
183 memcpy(getElement(0,0),
184 data,
185 mAllocation.get()->getType()->getSizeBytes());
186 }
187
188
189
190 namespace android {
191 namespace renderscript {
192
rsi_Adapter2DCreate(Context * rsc)193 RsAdapter2D rsi_Adapter2DCreate(Context *rsc)
194 {
195 Adapter2D *a = new Adapter2D(rsc);
196 a->incUserRef();
197 return a;
198 }
199
rsi_Adapter2DBindAllocation(Context * rsc,RsAdapter2D va,RsAllocation valloc)200 void rsi_Adapter2DBindAllocation(Context *rsc, RsAdapter2D va, RsAllocation valloc)
201 {
202 Adapter2D * a = static_cast<Adapter2D *>(va);
203 Allocation * alloc = static_cast<Allocation *>(valloc);
204 a->setAllocation(alloc);
205 }
206
rsi_Adapter2DSetConstraint(Context * rsc,RsAdapter2D va,RsDimension dim,uint32_t value)207 void rsi_Adapter2DSetConstraint(Context *rsc, RsAdapter2D va, RsDimension dim, uint32_t value)
208 {
209 Adapter2D * a = static_cast<Adapter2D *>(va);
210 switch(dim) {
211 case RS_DIMENSION_X:
212 rsAssert(!"Cannot contrain X in an 2D adapter");
213 return;
214 case RS_DIMENSION_Y:
215 rsAssert(!"Cannot contrain Y in an 2D adapter");
216 break;
217 case RS_DIMENSION_Z:
218 a->setZ(value);
219 break;
220 case RS_DIMENSION_LOD:
221 a->setLOD(value);
222 break;
223 case RS_DIMENSION_FACE:
224 a->setFace(value);
225 break;
226 default:
227 rsAssert(!"Unimplemented constraint");
228 return;
229 }
230 }
231
rsi_Adapter2DData(Context * rsc,RsAdapter2D va,const void * data)232 void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data)
233 {
234 Adapter2D * a = static_cast<Adapter2D *>(va);
235 a->data(data);
236 }
237
rsi_Adapter2DSubData(Context * rsc,RsAdapter2D va,uint32_t xoff,uint32_t yoff,uint32_t w,uint32_t h,const void * data)238 void rsi_Adapter2DSubData(Context *rsc, RsAdapter2D va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
239 {
240 Adapter2D * a = static_cast<Adapter2D *>(va);
241 a->subData(xoff, yoff, w, h, data);
242 }
243
244 }
245 }
246