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