blob: 41811ae01a7275823d9d182fc14eea2e53f6b13b [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001
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"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080019#include "rsAdapter.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070020
21using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024Adapter1D::Adapter1D(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070025 reset();
26}
27
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080028Adapter1D::Adapter1D(Context *rsc, Allocation *a) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070029 reset();
30 setAllocation(a);
31}
32
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033void Adapter1D::reset() {
Jason Sams326e0dd2009-05-22 14:03:28 -070034 mY = 0;
35 mZ = 0;
36 mLOD = 0;
37 mFace = 0;
38}
39
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080040void * Adapter1D::getElement(uint32_t x) {
Jason Sams326e0dd2009-05-22 14:03:28 -070041 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -070050 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057void Adapter1D::data(const void *data) {
Jason Samsd3c8de22009-08-03 16:03:08 -070058 memcpy(getElement(0),
59 data,
Jason Sams326e0dd2009-05-22 14:03:28 -070060 mAllocation.get()->getType()->getSizeBytes());
61}
62
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080063void Adapter1D::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070064}
65
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080066Adapter1D *Adapter1D::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070067 return NULL;
68}
69
Jason Sams326e0dd2009-05-22 14:03:28 -070070namespace android {
71namespace renderscript {
72
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080073RsAdapter1D rsi_Adapter1DCreate(Context *rsc) {
Jason Samse514b452009-09-25 14:51:22 -070074 Adapter1D *a = new Adapter1D(rsc);
Jason Sams9397e302009-08-27 20:23:34 -070075 a->incUserRef();
Jason Samsd3c8de22009-08-03 16:03:08 -070076 return a;
Jason Sams326e0dd2009-05-22 14:03:28 -070077}
78
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080079void rsi_Adapter1DBindAllocation(Context *rsc, RsAdapter1D va, RsAllocation valloc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070080 Adapter1D * a = static_cast<Adapter1D *>(va);
81 Allocation * alloc = static_cast<Allocation *>(valloc);
82 a->setAllocation(alloc);
83}
84
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085void rsi_Adapter1DSetConstraint(Context *rsc, RsAdapter1D va, RsDimension dim, uint32_t value) {
Jason Sams326e0dd2009-05-22 14:03:28 -070086 Adapter1D * a = static_cast<Adapter1D *>(va);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087 switch (dim) {
Jason Sams326e0dd2009-05-22 14:03:28 -070088 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800109void rsi_Adapter1DSubData(Context *rsc, RsAdapter1D va, uint32_t xoff, uint32_t count, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700110 Adapter1D * a = static_cast<Adapter1D *>(va);
111 a->subData(xoff, count, data);
112}
113
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800114void rsi_Adapter1DData(Context *rsc, RsAdapter1D va, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700115 Adapter1D * a = static_cast<Adapter1D *>(va);
116 a->data(data);
117}
118
119}
120}
121
122//////////////////////////
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124Adapter2D::Adapter2D(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 reset();
126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128Adapter2D::Adapter2D(Context *rsc, Allocation *a) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 reset();
130 setAllocation(a);
131}
132
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800133void Adapter2D::reset() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700134 mZ = 0;
135 mLOD = 0;
136 mFace = 0;
137}
138
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139void * Adapter2D::getElement(uint32_t x, uint32_t y) const {
Jason Sams326e0dd2009-05-22 14:03:28 -0700140 rsAssert(mAllocation.get());
141 rsAssert(mAllocation->getPtr());
142 rsAssert(mAllocation->getType());
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800143 if (mFace != 0 && !mAllocation->getType()->getDimFaces()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000144 ALOGE("Adapter wants cubemap face, but allocation has none");
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800145 return NULL;
146 }
147
Jason Sams326e0dd2009-05-22 14:03:28 -0700148 uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr());
149 ptr += mAllocation->getType()->getLODOffset(mLOD, x, y);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800150
151 if (mFace != 0) {
152 uint32_t totalSizeBytes = mAllocation->getType()->getSizeBytes();
153 uint32_t faceOffset = totalSizeBytes / 6;
154 ptr += faceOffset * mFace;
155 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 return ptr;
157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700160 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;
Jason Sams326e0dd2009-05-22 14:03:28 -0700166
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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800174void Adapter2D::data(const void *data) {
Jason Samsd3c8de22009-08-03 16:03:08 -0700175 memcpy(getElement(0,0),
176 data,
Jason Sams326e0dd2009-05-22 14:03:28 -0700177 mAllocation.get()->getType()->getSizeBytes());
178}
179
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800180void Adapter2D::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700181}
182
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800183Adapter2D *Adapter2D::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700184 return NULL;
185}
Jason Sams326e0dd2009-05-22 14:03:28 -0700186
187
188namespace android {
189namespace renderscript {
190
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800191RsAdapter2D rsi_Adapter2DCreate(Context *rsc) {
Jason Samse514b452009-09-25 14:51:22 -0700192 Adapter2D *a = new Adapter2D(rsc);
Jason Sams9397e302009-08-27 20:23:34 -0700193 a->incUserRef();
Jason Samsd3c8de22009-08-03 16:03:08 -0700194 return a;
Jason Sams326e0dd2009-05-22 14:03:28 -0700195}
196
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800197void rsi_Adapter2DBindAllocation(Context *rsc, RsAdapter2D va, RsAllocation valloc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700198 Adapter2D * a = static_cast<Adapter2D *>(va);
199 Allocation * alloc = static_cast<Allocation *>(valloc);
200 a->setAllocation(alloc);
201}
202
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800203void rsi_Adapter2DSetConstraint(Context *rsc, RsAdapter2D va, RsDimension dim, uint32_t value) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700204 Adapter2D * a = static_cast<Adapter2D *>(va);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800205 switch (dim) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700206 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700228 Adapter2D * a = static_cast<Adapter2D *>(va);
229 a->data(data);
230}
231
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800232void rsi_Adapter2DSubData(Context *rsc, RsAdapter2D va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700233 Adapter2D * a = static_cast<Adapter2D *>(va);
234 a->subData(xoff, yoff, w, h, data);
235}
236
237}
238}