blob: 6e8ca705110ea826cf59379ab76e33aaa09db45d [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"
19
20using namespace android;
21using namespace android::renderscript;
22
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080023Adapter1D::Adapter1D(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070024 reset();
25}
26
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080027Adapter1D::Adapter1D(Context *rsc, Allocation *a) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070028 reset();
29 setAllocation(a);
30}
31
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080032void Adapter1D::reset() {
Jason Sams326e0dd2009-05-22 14:03:28 -070033 mY = 0;
34 mZ = 0;
35 mLOD = 0;
36 mFace = 0;
37}
38
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080039void * Adapter1D::getElement(uint32_t x) {
Jason Sams326e0dd2009-05-22 14:03:28 -070040 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080048void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -070049 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080056void Adapter1D::data(const void *data) {
Jason Samsd3c8de22009-08-03 16:03:08 -070057 memcpy(getElement(0),
58 data,
Jason Sams326e0dd2009-05-22 14:03:28 -070059 mAllocation.get()->getType()->getSizeBytes());
60}
61
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062void Adapter1D::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070063}
64
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065Adapter1D *Adapter1D::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070066 return NULL;
67}
68
Jason Sams326e0dd2009-05-22 14:03:28 -070069namespace android {
70namespace renderscript {
71
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080072RsAdapter1D rsi_Adapter1DCreate(Context *rsc) {
Jason Samse514b452009-09-25 14:51:22 -070073 Adapter1D *a = new Adapter1D(rsc);
Jason Sams9397e302009-08-27 20:23:34 -070074 a->incUserRef();
Jason Samsd3c8de22009-08-03 16:03:08 -070075 return a;
Jason Sams326e0dd2009-05-22 14:03:28 -070076}
77
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080078void rsi_Adapter1DBindAllocation(Context *rsc, RsAdapter1D va, RsAllocation valloc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070079 Adapter1D * a = static_cast<Adapter1D *>(va);
80 Allocation * alloc = static_cast<Allocation *>(valloc);
81 a->setAllocation(alloc);
82}
83
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080084void rsi_Adapter1DSetConstraint(Context *rsc, RsAdapter1D va, RsDimension dim, uint32_t value) {
Jason Sams326e0dd2009-05-22 14:03:28 -070085 Adapter1D * a = static_cast<Adapter1D *>(va);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086 switch (dim) {
Jason Sams326e0dd2009-05-22 14:03:28 -070087 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800108void rsi_Adapter1DSubData(Context *rsc, RsAdapter1D va, uint32_t xoff, uint32_t count, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700109 Adapter1D * a = static_cast<Adapter1D *>(va);
110 a->subData(xoff, count, data);
111}
112
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800113void rsi_Adapter1DData(Context *rsc, RsAdapter1D va, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700114 Adapter1D * a = static_cast<Adapter1D *>(va);
115 a->data(data);
116}
117
118}
119}
120
121//////////////////////////
122
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800123Adapter2D::Adapter2D(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700124 reset();
125}
126
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800127Adapter2D::Adapter2D(Context *rsc, Allocation *a) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 reset();
129 setAllocation(a);
130}
131
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800132void Adapter2D::reset() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700133 mZ = 0;
134 mLOD = 0;
135 mFace = 0;
136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void * Adapter2D::getElement(uint32_t x, uint32_t y) const {
Jason Sams326e0dd2009-05-22 14:03:28 -0700139 rsAssert(mAllocation.get());
140 rsAssert(mAllocation->getPtr());
141 rsAssert(mAllocation->getType());
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800142 if (mFace != 0 && !mAllocation->getType()->getDimFaces()) {
143 LOGE("Adapter wants cubemap face, but allocation has none");
144 return NULL;
145 }
146
Jason Sams326e0dd2009-05-22 14:03:28 -0700147 uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr());
148 ptr += mAllocation->getType()->getLODOffset(mLOD, x, y);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800149
150 if (mFace != 0) {
151 uint32_t totalSizeBytes = mAllocation->getType()->getSizeBytes();
152 uint32_t faceOffset = totalSizeBytes / 6;
153 ptr += faceOffset * mFace;
154 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700155 return ptr;
156}
157
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800158void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 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;
Jason Sams326e0dd2009-05-22 14:03:28 -0700165
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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800173void Adapter2D::data(const void *data) {
Jason Samsd3c8de22009-08-03 16:03:08 -0700174 memcpy(getElement(0,0),
175 data,
Jason Sams326e0dd2009-05-22 14:03:28 -0700176 mAllocation.get()->getType()->getSizeBytes());
177}
178
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800179void Adapter2D::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700180}
181
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800182Adapter2D *Adapter2D::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700183 return NULL;
184}
Jason Sams326e0dd2009-05-22 14:03:28 -0700185
186
187namespace android {
188namespace renderscript {
189
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800190RsAdapter2D rsi_Adapter2DCreate(Context *rsc) {
Jason Samse514b452009-09-25 14:51:22 -0700191 Adapter2D *a = new Adapter2D(rsc);
Jason Sams9397e302009-08-27 20:23:34 -0700192 a->incUserRef();
Jason Samsd3c8de22009-08-03 16:03:08 -0700193 return a;
Jason Sams326e0dd2009-05-22 14:03:28 -0700194}
195
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800196void rsi_Adapter2DBindAllocation(Context *rsc, RsAdapter2D va, RsAllocation valloc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700197 Adapter2D * a = static_cast<Adapter2D *>(va);
198 Allocation * alloc = static_cast<Allocation *>(valloc);
199 a->setAllocation(alloc);
200}
201
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800202void rsi_Adapter2DSetConstraint(Context *rsc, RsAdapter2D va, RsDimension dim, uint32_t value) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700203 Adapter2D * a = static_cast<Adapter2D *>(va);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800204 switch (dim) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700205 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800226void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700227 Adapter2D * a = static_cast<Adapter2D *>(va);
228 a->data(data);
229}
230
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800231void 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 -0700232 Adapter2D * a = static_cast<Adapter2D *>(va);
233 a->subData(xoff, yoff, w, h, data);
234}
235
236}
237}