Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 16 | |
Alex Sakhartchouk | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 17 | #include "rsContext.h" |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 18 | #include "rs_hal.h" |
| 19 | |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 20 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 24 | Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages, |
| 25 | RsAllocationMipmapControl mc) |
| 26 | : ObjectBase(rsc) { |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 27 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 28 | memset(&mHal, 0, sizeof(mHal)); |
| 29 | mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 30 | mHal.state.usageFlags = usages; |
| 31 | mHal.state.mipmapControl = mc; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 32 | |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 33 | setType(type); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 34 | updateCache(); |
| 35 | } |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 36 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 37 | Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages, |
| 38 | RsAllocationMipmapControl mc) { |
| 39 | Allocation *a = new Allocation(rsc, type, usages, mc); |
| 40 | |
| 41 | if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) { |
| 42 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure"); |
| 43 | delete a; |
| 44 | return NULL; |
| 45 | } |
| 46 | return a; |
| 47 | } |
| 48 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 49 | void Allocation::updateCache() { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 50 | const Type *type = mHal.state.type; |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 51 | mHal.state.dimensionX = type->getDimX(); |
| 52 | mHal.state.dimensionY = type->getDimY(); |
| 53 | mHal.state.dimensionZ = type->getDimZ(); |
| 54 | mHal.state.hasFaces = type->getDimFaces(); |
| 55 | mHal.state.hasMipmaps = type->getDimLOD(); |
| 56 | mHal.state.elementSizeBytes = type->getElementSizeBytes(); |
| 57 | mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 60 | Allocation::~Allocation() { |
Jason Sams | 777ec26 | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 61 | freeChildrenUnlocked(); |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 62 | mRSC->mHal.funcs.allocation.destroy(mRSC, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 65 | void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) { |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 66 | rsc->mHal.funcs.allocation.syncAll(rsc, this, src); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 69 | void Allocation::read(void *data) { |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 70 | memcpy(data, getPtr(), mHal.state.type->getSizeBytes()); |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 73 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, |
| 74 | uint32_t count, const void *data, uint32_t sizeBytes) { |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 75 | const uint32_t eSize = mHal.state.type->getElementSizeBytes(); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 76 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 77 | if ((count * eSize) != sizeBytes) { |
| 78 | LOGE("Allocation::subData called with mismatched size expected %i, got %i", |
| 79 | (count * eSize), sizeBytes); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 80 | mHal.state.type->dumpLOGV("type info"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 83 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 84 | rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes); |
| 85 | sendDirty(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 88 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 89 | uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 90 | const uint32_t eSize = mHal.state.elementSizeBytes; |
| 91 | const uint32_t lineSize = eSize * w; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 92 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 93 | //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 94 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 95 | if ((lineSize * h) != sizeBytes) { |
| 96 | LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 97 | rsAssert(!"Allocation::subData called with mismatched size"); |
| 98 | return; |
| 99 | } |
| 100 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 101 | rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes); |
| 102 | sendDirty(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 105 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 106 | uint32_t lod, RsAllocationCubemapFace face, |
| 107 | uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 110 | void Allocation::elementData(Context *rsc, uint32_t x, const void *data, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 111 | uint32_t cIdx, uint32_t sizeBytes) { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 112 | uint32_t eSize = mHal.state.elementSizeBytes; |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 113 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 114 | if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 115 | LOGE("Error Allocation::subElementData component %i out of range.", cIdx); |
| 116 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
| 117 | return; |
| 118 | } |
| 119 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 120 | if (x >= mHal.state.dimensionX) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 121 | LOGE("Error Allocation::subElementData X offset %i out of range.", x); |
| 122 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 123 | return; |
| 124 | } |
| 125 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 126 | const Element * e = mHal.state.type->getElement()->getField(cIdx); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 127 | if (sizeBytes != e->getSizeBytes()) { |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 128 | LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes()); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 129 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 130 | return; |
| 131 | } |
| 132 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 133 | rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes); |
| 134 | sendDirty(rsc); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 137 | void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | const void *data, uint32_t cIdx, uint32_t sizeBytes) { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 139 | uint32_t eSize = mHal.state.elementSizeBytes; |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 140 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 141 | if (x >= mHal.state.dimensionX) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 142 | LOGE("Error Allocation::subElementData X offset %i out of range.", x); |
| 143 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 144 | return; |
| 145 | } |
| 146 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 147 | if (y >= mHal.state.dimensionY) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 148 | LOGE("Error Allocation::subElementData X offset %i out of range.", x); |
| 149 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 150 | return; |
| 151 | } |
| 152 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 153 | if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 154 | LOGE("Error Allocation::subElementData component %i out of range.", cIdx); |
| 155 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
| 156 | return; |
| 157 | } |
| 158 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 159 | const Element * e = mHal.state.type->getElement()->getField(cIdx); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 160 | |
| 161 | if (sizeBytes != e->getSizeBytes()) { |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 162 | LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes()); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 163 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 164 | return; |
| 165 | } |
| 166 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 167 | rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes); |
| 168 | sendDirty(rsc); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 171 | void Allocation::addProgramToDirty(const Program *p) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 172 | mToDirtyList.push(p); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 173 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 174 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 175 | void Allocation::removeProgramToDirty(const Program *p) { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 176 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 177 | if (mToDirtyList[ct] == p) { |
| 178 | mToDirtyList.removeAt(ct); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | rsAssert(0); |
| 183 | } |
| 184 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 185 | void Allocation::dumpLOGV(const char *prefix) const { |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 186 | ObjectBase::dumpLOGV(prefix); |
| 187 | |
| 188 | String8 s(prefix); |
| 189 | s.append(" type "); |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 190 | if (mHal.state.type) { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 191 | mHal.state.type->dumpLOGV(s.string()); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 194 | ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x", |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 195 | prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 196 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 197 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 198 | void Allocation::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 199 | // Need to identify ourselves |
| 200 | stream->addU32((uint32_t)getClassId()); |
| 201 | |
| 202 | String8 name(getName()); |
| 203 | stream->addString(&name); |
| 204 | |
| 205 | // First thing we need to serialize is the type object since it will be needed |
| 206 | // to initialize the class |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 207 | mHal.state.type->serialize(stream); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 208 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 209 | uint32_t dataSize = mHal.state.type->getSizeBytes(); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 210 | // Write how much data we are storing |
| 211 | stream->addU32(dataSize); |
| 212 | // Now write the data |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 213 | stream->addByteArray(getPtr(), dataSize); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 216 | Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 217 | // First make sure we are reading the correct object |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 218 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 219 | if (classID != RS_A3D_CLASS_ID_ALLOCATION) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 220 | LOGE("allocation loading skipped due to invalid class id\n"); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | String8 name; |
| 225 | stream->loadString(&name); |
| 226 | |
| 227 | Type *type = Type::createFromStream(rsc, stream); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 228 | if (!type) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
| 231 | type->compute(); |
| 232 | |
| 233 | // Number of bytes we wrote out for this allocation |
| 234 | uint32_t dataSize = stream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 235 | if (dataSize != type->getSizeBytes()) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 236 | LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 237 | ObjectBase::checkDelete(type); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 238 | return NULL; |
| 239 | } |
| 240 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 241 | Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 242 | alloc->setName(name.string(), name.size()); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 243 | type->decUserRef(); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 244 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 245 | uint32_t count = dataSize / type->getElementSizeBytes(); |
| 246 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 247 | // Read in all of our allocation data |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 248 | alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); |
Alex Sakhartchouk | 2ce0e3f | 2010-08-11 10:30:44 -0700 | [diff] [blame] | 249 | stream->reset(stream->getPos() + dataSize); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 250 | |
| 251 | return alloc; |
| 252 | } |
| 253 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 254 | void Allocation::sendDirty(const Context *rsc) const { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 255 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 256 | mToDirtyList[ct]->forceDirty(); |
| 257 | } |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 258 | mRSC->mHal.funcs.allocation.markDirty(rsc, this); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 259 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 260 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 261 | void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 262 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 263 | const Element *e = mHal.state.type->getElement(); |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 264 | uint32_t stride = e->getSizeBytes(); |
| 265 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 266 | p += stride * startOff; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 267 | while (ct > 0) { |
| 268 | e->incRefs(p); |
| 269 | ct --; |
| 270 | p += stride; |
| 271 | } |
| 272 | } |
| 273 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 274 | void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { |
Alex Sakhartchouk | 065fa8d | 2011-08-12 11:30:30 -0700 | [diff] [blame] | 275 | if (!mHal.state.hasReferences || !getIsScript()) { |
| 276 | return; |
| 277 | } |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 278 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 279 | const Element *e = mHal.state.type->getElement(); |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 280 | uint32_t stride = e->getSizeBytes(); |
| 281 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 282 | p += stride * startOff; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 283 | while (ct > 0) { |
| 284 | e->decRefs(p); |
| 285 | ct --; |
| 286 | p += stride; |
| 287 | } |
| 288 | } |
| 289 | |
Jason Sams | 777ec26 | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 290 | void Allocation::freeChildrenUnlocked () { |
| 291 | decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0); |
| 292 | } |
| 293 | |
| 294 | bool Allocation::freeChildren() { |
| 295 | if (mHal.state.hasReferences) { |
| 296 | incSysRef(); |
| 297 | freeChildrenUnlocked(); |
| 298 | return decSysRef(); |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 303 | void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 306 | void Allocation::resize1D(Context *rsc, uint32_t dimX) { |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 307 | uint32_t oldDimX = mHal.state.dimensionX; |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 308 | if (dimX == oldDimX) { |
| 309 | return; |
| 310 | } |
| 311 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 312 | ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 313 | if (dimX < oldDimX) { |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 314 | decRefs(getPtr(), oldDimX - dimX, dimX); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 315 | } |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 316 | rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences); |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 317 | setType(t.get()); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 318 | updateCache(); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 321 | void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 322 | LOGE("not implemented"); |
| 323 | } |
| 324 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 325 | ///////////////// |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 326 | // |
Stephen Hines | 60f9a62 | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 327 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 328 | namespace android { |
| 329 | namespace renderscript { |
| 330 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 331 | static void AllocationGenerateScriptMips(RsContext con, RsAllocation va); |
| 332 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 333 | static void mip565(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 334 | uint32_t w = out.getDimX(); |
| 335 | uint32_t h = out.getDimY(); |
| 336 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 337 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 338 | uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); |
| 339 | const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); |
| 340 | const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); |
| 341 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 342 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 343 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 344 | oPtr ++; |
| 345 | i1 += 2; |
| 346 | i2 += 2; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 351 | static void mip8888(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 352 | uint32_t w = out.getDimX(); |
| 353 | uint32_t h = out.getDimY(); |
| 354 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 355 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 356 | uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); |
| 357 | const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); |
| 358 | const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); |
| 359 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 360 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 361 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 362 | oPtr ++; |
| 363 | i1 += 2; |
| 364 | i2 += 2; |
| 365 | } |
| 366 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 369 | static void mip8(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | e20e3b4 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 370 | uint32_t w = out.getDimX(); |
| 371 | uint32_t h = out.getDimY(); |
| 372 | |
| 373 | for (uint32_t y=0; y < h; y++) { |
| 374 | uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); |
| 375 | const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); |
| 376 | const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); |
| 377 | |
| 378 | for (uint32_t x=0; x < w; x++) { |
| 379 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 380 | oPtr ++; |
| 381 | i1 += 2; |
| 382 | i2 += 2; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 387 | static void mip(const Adapter2D &out, const Adapter2D &in) { |
| 388 | switch (out.getBaseType()->getElement()->getSizeBits()) { |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 389 | case 32: |
| 390 | mip8888(out, in); |
| 391 | break; |
| 392 | case 16: |
| 393 | mip565(out, in); |
| 394 | break; |
Jason Sams | e20e3b4 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 395 | case 8: |
| 396 | mip8(out, in); |
| 397 | break; |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 398 | } |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 399 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 400 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 401 | void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { |
| 402 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 403 | a->sendDirty(rsc); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 404 | a->syncAll(rsc, src); |
| 405 | } |
| 406 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 407 | void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 408 | Allocation *texAlloc = static_cast<Allocation *>(va); |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 409 | AllocationGenerateScriptMips(rsc, texAlloc); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) { |
| 413 | Allocation *texAlloc = static_cast<Allocation *>(va); |
| 414 | const Type * t = texAlloc->getType(); |
| 415 | |
| 416 | size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes(); |
| 417 | if (s != dataLen) { |
| 418 | rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size"); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | memcpy(data, texAlloc->getPtr(), s); |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 425 | void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 426 | uint32_t count, const void *data, size_t sizeBytes) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 427 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 428 | a->data(rsc, xoff, lod, count, data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 431 | void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face, |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 432 | const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 433 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 434 | a->elementData(rsc, x, y, data, eoff, sizeBytes); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 437 | void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod, |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 438 | const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 439 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 440 | a->elementData(rsc, x, data, eoff, sizeBytes); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 443 | void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 444 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 445 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 446 | a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 449 | void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) { |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 450 | Allocation *a = static_cast<Allocation *>(va); |
| 451 | a->read(data); |
| 452 | } |
| 453 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 454 | void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 455 | Allocation *a = static_cast<Allocation *>(va); |
| 456 | a->resize1D(rsc, dimX); |
| 457 | } |
| 458 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 459 | void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 460 | Allocation *a = static_cast<Allocation *>(va); |
| 461 | a->resize2D(rsc, dimX, dimY); |
| 462 | } |
| 463 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 464 | static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) { |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 465 | Context *rsc = static_cast<Context *>(con); |
| 466 | Allocation *texAlloc = static_cast<Allocation *>(va); |
| 467 | uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1; |
| 468 | for (uint32_t face = 0; face < numFaces; face ++) { |
| 469 | Adapter2D adapt(rsc, texAlloc); |
| 470 | Adapter2D adapt2(rsc, texAlloc); |
| 471 | adapt.setFace(face); |
| 472 | adapt2.setFace(face); |
| 473 | for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 474 | adapt.setLOD(lod); |
| 475 | adapt2.setLOD(lod + 1); |
| 476 | mip(adapt2, adapt); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 481 | RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, |
| 482 | RsAllocationMipmapControl mips, |
| 483 | uint32_t usages) { |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 484 | Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips); |
| 485 | if (!alloc) { |
| 486 | return NULL; |
| 487 | } |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 488 | alloc->incUserRef(); |
| 489 | return alloc; |
| 490 | } |
| 491 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 492 | RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, |
| 493 | RsAllocationMipmapControl mips, |
| 494 | const void *data, size_t data_length, uint32_t usages) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 495 | Type *t = static_cast<Type *>(vtype); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 496 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 497 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 498 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 499 | if (texAlloc == NULL) { |
| 500 | LOGE("Memory allocation failure"); |
| 501 | return NULL; |
| 502 | } |
| 503 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 504 | memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes()); |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 505 | if (mips == RS_ALLOCATION_MIPMAP_FULL) { |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 506 | AllocationGenerateScriptMips(rsc, texAlloc); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 509 | texAlloc->sendDirty(rsc); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 510 | return texAlloc; |
| 511 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 512 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 513 | RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, |
| 514 | RsAllocationMipmapControl mips, |
| 515 | const void *data, size_t data_length, uint32_t usages) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 516 | Type *t = static_cast<Type *>(vtype); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 517 | |
| 518 | // Cubemap allocation's faces should be Width by Width each. |
| 519 | // Source data should have 6 * Width by Width pixels |
| 520 | // Error checking is done in the java layer |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 521 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 522 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 523 | if (texAlloc == NULL) { |
| 524 | LOGE("Memory allocation failure"); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 528 | uint32_t faceSize = t->getDimX(); |
| 529 | uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes(); |
| 530 | uint32_t copySize = faceSize * t->getElementSizeBytes(); |
| 531 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 532 | uint8_t *sourcePtr = (uint8_t*)data; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 533 | for (uint32_t face = 0; face < 6; face ++) { |
| 534 | Adapter2D faceAdapter(rsc, texAlloc); |
| 535 | faceAdapter.setFace(face); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 536 | |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 537 | for (uint32_t dI = 0; dI < faceSize; dI ++) { |
| 538 | memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize); |
| 539 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 540 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 541 | // Move the data pointer to the next cube face |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 542 | sourcePtr += copySize; |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 543 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 544 | |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 545 | if (mips == RS_ALLOCATION_MIPMAP_FULL) { |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 546 | AllocationGenerateScriptMips(rsc, texAlloc); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 549 | texAlloc->sendDirty(rsc); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 550 | return texAlloc; |
| 551 | } |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 552 | |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 553 | void rsi_AllocationCopy2DRange(Context *rsc, |
| 554 | RsAllocation dstAlloc, |
| 555 | uint32_t dstXoff, uint32_t dstYoff, |
| 556 | uint32_t dstMip, uint32_t dstFace, |
| 557 | uint32_t width, uint32_t height, |
| 558 | RsAllocation srcAlloc, |
| 559 | uint32_t srcXoff, uint32_t srcYoff, |
| 560 | uint32_t srcMip, uint32_t srcFace) { |
| 561 | Allocation *dst = static_cast<Allocation *>(dstAlloc); |
| 562 | Allocation *src= static_cast<Allocation *>(srcAlloc); |
| 563 | rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip, |
| 564 | (RsAllocationCubemapFace)dstFace, |
| 565 | width, height, |
| 566 | src, srcXoff, srcYoff,srcMip, |
| 567 | (RsAllocationCubemapFace)srcFace); |
| 568 | } |
| 569 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | const void * rsaAllocationGetType(RsContext con, RsAllocation va) { |
| 574 | Allocation *a = static_cast<Allocation *>(va); |
| 575 | a->getType()->incUserRef(); |
| 576 | |
| 577 | return a->getType(); |
| 578 | } |