rsAdapter.cpp revision 326e0ddf89e8df2837752fbfd7a014814b32082c
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 20using namespace android; 21using namespace android::renderscript; 22 23 24Adapter1D::Adapter1D() 25{ 26 reset(); 27} 28 29Adapter1D::Adapter1D(Allocation *a) 30{ 31 reset(); 32 setAllocation(a); 33} 34 35void Adapter1D::reset() 36{ 37 mY = 0; 38 mZ = 0; 39 mLOD = 0; 40 mFace = 0; 41} 42 43void * Adapter1D::getElement(uint32_t x) 44{ 45 rsAssert(mAllocation.get()); 46 rsAssert(mAllocation->getPtr()); 47 rsAssert(mAllocation->getType()); 48 uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr()); 49 ptr += mAllocation->getType()->getLODOffset(mLOD, x, mY); 50 return ptr; 51} 52 53void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data) 54{ 55 if (mAllocation.get() && mAllocation.get()->getType()) { 56 void *ptr = getElement(xoff); 57 count *= mAllocation.get()->getType()->getElementSizeBytes(); 58 memcpy(ptr, data, count); 59 } 60} 61 62void Adapter1D::data(const void *data) 63{ 64 memcpy(getElement(0), 65 data, 66 mAllocation.get()->getType()->getSizeBytes()); 67} 68 69namespace android { 70namespace renderscript { 71 72RsAdapter1D rsi_Adapter1DCreate(Context *rsc) 73{ 74 return new Adapter1D(); 75} 76 77void rsi_Adapter1DDestroy(Context *rsc, RsAdapter1D va) 78{ 79 Adapter1D * a = static_cast<Adapter1D *>(va); 80 a->decRef(); 81} 82 83void 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 90void 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 115void 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 121void 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 132Adapter2D::Adapter2D() 133{ 134 reset(); 135} 136 137Adapter2D::Adapter2D(Allocation *a) 138{ 139 reset(); 140 setAllocation(a); 141} 142 143void Adapter2D::reset() 144{ 145 mZ = 0; 146 mLOD = 0; 147 mFace = 0; 148} 149 150void * Adapter2D::getElement(uint32_t x, uint32_t y) const 151{ 152 rsAssert(mAllocation.get()); 153 rsAssert(mAllocation->getPtr()); 154 rsAssert(mAllocation->getType()); 155 uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr()); 156 ptr += mAllocation->getType()->getLODOffset(mLOD, x, y); 157 return ptr; 158} 159 160void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) 161{ 162 rsAssert(mAllocation.get()); 163 rsAssert(mAllocation->getPtr()); 164 rsAssert(mAllocation->getType()); 165 166 uint32_t eSize = mAllocation.get()->getType()->getElementSizeBytes(); 167 uint32_t lineSize = eSize * w; 168 uint32_t destW = getDimX(); 169 170 const uint8_t *src = static_cast<const uint8_t *>(data); 171 for (uint32_t line=yoff; line < (yoff+h); line++) { 172 memcpy(getElement(xoff, line), src, lineSize); 173 src += lineSize; 174 } 175} 176 177void Adapter2D::data(const void *data) 178{ 179 memcpy(getElement(0,0), 180 data, 181 mAllocation.get()->getType()->getSizeBytes()); 182} 183 184 185 186namespace android { 187namespace renderscript { 188 189RsAdapter2D rsi_Adapter2DCreate(Context *rsc) 190{ 191 return new Adapter2D(); 192} 193 194void rsi_Adapter2DDestroy(Context *rsc, RsAdapter2D va) 195{ 196 Adapter2D * a = static_cast<Adapter2D *>(va); 197 a->decRef(); 198} 199 200void 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 207void 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 232void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data) 233{ 234 Adapter2D * a = static_cast<Adapter2D *>(va); 235 a->data(data); 236} 237 238void 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