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