blob: b8ddb0b7cac2ecac49a6088a52f5ba89f7bfb149 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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 Samsd19f10d2009-05-22 14:03:28 -070016
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080017#include "rsContext.h"
18#ifndef ANDROID_RS_SERIALIZE
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Samsc2908e62010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080022#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070023
Jason Samsd19f10d2009-05-22 14:03:28 -070024using namespace android;
25using namespace android::renderscript;
26
Alex Sakhartchouk08571962010-12-15 09:59:58 -080027Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
28 RsAllocationMipmapControl mc)
29 : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080030 init(rsc, type);
31
Jason Samse4a06c52011-03-16 16:29:28 -070032 mHal.state.usageFlags = usages;
33 mHal.state.mipmapControl = mc;
Jason Sams5476b452010-12-08 16:14:36 -080034
Jason Sams5e0035a2010-12-13 17:11:21 -080035 allocScriptMemory();
Jason Samse4a06c52011-03-16 16:29:28 -070036 if (mHal.state.type->getElement()->getHasReferences()) {
37 memset(mHal.state.mallocPtr, 0, mHal.state.type->getSizeBytes());
Jason Samsee734982010-08-12 12:44:02 -070038 }
Jason Samse4a06c52011-03-16 16:29:28 -070039 if (!mHal.state.mallocPtr) {
Jason Sams8a64743f2010-03-01 15:31:04 -080040 LOGE("Allocation::Allocation, alloc failure");
41 }
42}
43
Jason Sams8a64743f2010-03-01 15:31:04 -080044
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080045void Allocation::init(Context *rsc, const Type *type) {
Jason Samse4a06c52011-03-16 16:29:28 -070046 memset(&mHal, 0, sizeof(mHal));
47 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsd19f10d2009-05-22 14:03:28 -070048
49 mCpuWrite = false;
50 mCpuRead = false;
51 mGpuWrite = false;
52 mGpuRead = false;
53
54 mReadWriteRatio = 0;
55 mUpdateSize = 0;
56
Jason Samsd19f10d2009-05-22 14:03:28 -070057 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070058 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080059 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070060
Jason Sams8a64743f2010-03-01 15:31:04 -080061 mUserBitmapCallback = NULL;
62 mUserBitmapCallbackData = NULL;
63
Jason Samse4a06c52011-03-16 16:29:28 -070064 mHal.state.type.set(type);
65 updateCache();
66}
Jason Sams8a64743f2010-03-01 15:31:04 -080067
Jason Samse4a06c52011-03-16 16:29:28 -070068void Allocation::updateCache() {
69 const Type *type = mHal.state.type.get();
70 mHal.state.dimensionX = type->getDimX();
71 mHal.state.dimensionY = type->getDimY();
72 mHal.state.dimensionZ = type->getDimZ();
73 mHal.state.hasFaces = type->getDimFaces();
74 mHal.state.hasMipmaps = type->getDimLOD();
75 mHal.state.elementSizeBytes = type->getElementSizeBytes();
76 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
Jason Samsd19f10d2009-05-22 14:03:28 -070077}
78
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080079Allocation::~Allocation() {
Jason Sams8a64743f2010-03-01 15:31:04 -080080 if (mUserBitmapCallback != NULL) {
81 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Samse4a06c52011-03-16 16:29:28 -070082 mHal.state.mallocPtr = NULL;
Jason Sams8a64743f2010-03-01 15:31:04 -080083 }
Jason Sams5e0035a2010-12-13 17:11:21 -080084 freeScriptMemory();
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080085#ifndef ANDROID_RS_SERIALIZE
Jason Sams9d5e03d2009-11-03 11:25:42 -080086 if (mBufferID) {
87 // Causes a SW crash....
88 //LOGV(" mBufferID %i", mBufferID);
89 //glDeleteBuffers(1, &mBufferID);
90 //mBufferID = 0;
91 }
92 if (mTextureID) {
93 glDeleteTextures(1, &mTextureID);
94 mTextureID = 0;
95 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080096#endif //ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -070097}
98
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080099void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700100}
101
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800102void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700103}
104
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800105void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700106}
107
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800108void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700109}
110
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800111bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700112 return false;
113}
114
Jason Sams5e0035a2010-12-13 17:11:21 -0800115void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samse4a06c52011-03-16 16:29:28 -0700116 mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800117 mUploadDefered = true;
118}
119
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800120uint32_t Allocation::getGLTarget() const {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800121#ifndef ANDROID_RS_SERIALIZE
Jason Samsd4b23b52010-12-13 15:32:35 -0800122 if (getIsTexture()) {
Jason Samse4a06c52011-03-16 16:29:28 -0700123 if (mHal.state.type->getDimFaces()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800124 return GL_TEXTURE_CUBE_MAP;
125 } else {
126 return GL_TEXTURE_2D;
127 }
128 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800129 if (getIsBufferObject()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800130 return GL_ARRAY_BUFFER;
131 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800132#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800133 return 0;
134}
135
Jason Sams5e0035a2010-12-13 17:11:21 -0800136void Allocation::allocScriptMemory() {
Jason Samse4a06c52011-03-16 16:29:28 -0700137 rsAssert(!mHal.state.mallocPtr);
138 mHal.state.mallocPtr = malloc(mHal.state.type->getSizeBytes());
Jason Sams5e0035a2010-12-13 17:11:21 -0800139}
140
141void Allocation::freeScriptMemory() {
Jason Samse4a06c52011-03-16 16:29:28 -0700142 if (mHal.state.mallocPtr) {
143 free(mHal.state.mallocPtr);
144 mHal.state.mallocPtr = NULL;
Jason Sams5e0035a2010-12-13 17:11:21 -0800145 }
146}
147
148
Jason Sams5476b452010-12-08 16:14:36 -0800149void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
150 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
151
Jason Samsd4b23b52010-12-13 15:32:35 -0800152 if (getIsTexture()) {
Jason Sams5476b452010-12-08 16:14:36 -0800153 uploadToTexture(rsc);
154 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800155 if (getIsBufferObject()) {
Jason Sams5476b452010-12-08 16:14:36 -0800156 uploadToBufferObject(rsc);
157 }
158
159 mUploadDefered = false;
160}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800161
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800162void Allocation::uploadToTexture(const Context *rsc) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800163#ifndef ANDROID_RS_SERIALIZE
Jason Samse4a06c52011-03-16 16:29:28 -0700164 mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
165 GLenum type = mHal.state.type->getElement()->getComponent().getGLType();
166 GLenum format = mHal.state.type->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700167
168 if (!type || !format) {
169 return;
170 }
171
Jason Samse4a06c52011-03-16 16:29:28 -0700172 if (!mHal.state.mallocPtr) {
Jason Sams5e0035a2010-12-13 17:11:21 -0800173 return;
174 }
175
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700176 bool isFirstUpload = false;
177
Jason Samsd19f10d2009-05-22 14:03:28 -0700178 if (!mTextureID) {
179 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800180
181 if (!mTextureID) {
182 // This should not happen, however, its likely the cause of the
183 // white sqare bug.
184 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
185 LOGE("Upload to texture failed to gen mTextureID");
186 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800187 mUploadDefered = true;
188 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800189 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700190 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700191 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800192
Jason Samsef70a202011-01-13 17:38:18 -0800193 upload2DTexture(isFirstUpload);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800194
Jason Samse4a06c52011-03-16 16:29:28 -0700195 if (!(mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
Jason Sams5e0035a2010-12-13 17:11:21 -0800196 freeScriptMemory();
197 }
198
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800199 rsc->checkError("Allocation::uploadToTexture");
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800200#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800201}
202
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800203#ifndef ANDROID_RS_SERIALIZE
Jason Samsef70a202011-01-13 17:38:18 -0800204const static GLenum gFaceOrder[] = {
205 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
206 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
207 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
208 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
209 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
210 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
211};
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800212#endif //ANDROID_RS_SERIALIZE
Jason Samsef70a202011-01-13 17:38:18 -0800213
Jason Samsfb9f82c2011-01-12 14:53:25 -0800214void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
215 uint32_t lod, RsAllocationCubemapFace face,
216 uint32_t w, uint32_t h) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800217#ifndef ANDROID_RS_SERIALIZE
Jason Samse4a06c52011-03-16 16:29:28 -0700218 GLenum type = mHal.state.type->getElement()->getComponent().getGLType();
219 GLenum format = mHal.state.type->getElement()->getComponent().getGLFormat();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800220 GLenum target = (GLenum)getGLTarget();
Jason Sams300406a2011-01-16 14:54:28 -0800221 rsAssert(mTextureID);
Jason Samsfb9f82c2011-01-12 14:53:25 -0800222 glBindTexture(target, mTextureID);
223 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsef70a202011-01-13 17:38:18 -0800224 GLenum t = GL_TEXTURE_2D;
Jason Samse4a06c52011-03-16 16:29:28 -0700225 if (mHal.state.hasFaces) {
Jason Samsef70a202011-01-13 17:38:18 -0800226 t = gFaceOrder[face];
227 }
228 glTexSubImage2D(t, lod, xoff, yoff, w, h, format, type, ptr);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800229#endif //ANDROID_RS_SERIALIZE
Jason Samsfb9f82c2011-01-12 14:53:25 -0800230}
231
Jason Samsef70a202011-01-13 17:38:18 -0800232void Allocation::upload2DTexture(bool isFirstUpload) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800233#ifndef ANDROID_RS_SERIALIZE
Jason Samse4a06c52011-03-16 16:29:28 -0700234 GLenum type = mHal.state.type->getElement()->getComponent().getGLType();
235 GLenum format = mHal.state.type->getElement()->getComponent().getGLFormat();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800236
Jason Sams5e0035a2010-12-13 17:11:21 -0800237 GLenum target = (GLenum)getGLTarget();
238 glBindTexture(target, mTextureID);
239 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700240
Jason Samsef70a202011-01-13 17:38:18 -0800241 uint32_t faceCount = 1;
Jason Samse4a06c52011-03-16 16:29:28 -0700242 if (mHal.state.hasFaces) {
Jason Samsef70a202011-01-13 17:38:18 -0800243 faceCount = 6;
Jason Samsd19f10d2009-05-22 14:03:28 -0700244 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800245
Jason Samsef70a202011-01-13 17:38:18 -0800246 for (uint32_t face = 0; face < faceCount; face ++) {
Jason Samse4a06c52011-03-16 16:29:28 -0700247 for (uint32_t lod = 0; lod < mHal.state.type->getLODCount(); lod++) {
248 const uint8_t *p = (const uint8_t *)mHal.state.mallocPtr;
249 p += mHal.state.type->getLODFaceOffset(lod, (RsAllocationCubemapFace)face, 0, 0);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800250
Jason Samsef70a202011-01-13 17:38:18 -0800251 GLenum t = GL_TEXTURE_2D;
Jason Samse4a06c52011-03-16 16:29:28 -0700252 if (mHal.state.hasFaces) {
Jason Samsef70a202011-01-13 17:38:18 -0800253 t = gFaceOrder[face];
254 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800255
256 if (isFirstUpload) {
Jason Samsef70a202011-01-13 17:38:18 -0800257 glTexImage2D(t, lod, format,
Jason Samse4a06c52011-03-16 16:29:28 -0700258 mHal.state.type->getLODDimX(lod), mHal.state.type->getLODDimY(lod),
Jason Samsef70a202011-01-13 17:38:18 -0800259 0, format, type, p);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800260 } else {
Jason Samsef70a202011-01-13 17:38:18 -0800261 glTexSubImage2D(t, lod, 0, 0,
Jason Samse4a06c52011-03-16 16:29:28 -0700262 mHal.state.type->getLODDimX(lod), mHal.state.type->getLODDimY(lod),
Jason Samsef70a202011-01-13 17:38:18 -0800263 format, type, p);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800264 }
265 }
266 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800267
Jason Samse4a06c52011-03-16 16:29:28 -0700268 if (mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800269 glGenerateMipmap(target);
Jason Sams6d8eb262010-12-15 01:41:00 -0800270 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800271#endif //ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700272}
273
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800274void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samse4a06c52011-03-16 16:29:28 -0700275 mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800276 mUploadDefered = true;
277}
278
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800279void Allocation::uploadToBufferObject(const Context *rsc) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800280#ifndef ANDROID_RS_SERIALIZE
Jason Samse4a06c52011-03-16 16:29:28 -0700281 rsAssert(!mHal.state.type->getDimY());
282 rsAssert(!mHal.state.type->getDimZ());
Jason Samsd19f10d2009-05-22 14:03:28 -0700283
Jason Samse4a06c52011-03-16 16:29:28 -0700284 mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800285
Jason Samsd19f10d2009-05-22 14:03:28 -0700286 if (!mBufferID) {
287 glGenBuffers(1, &mBufferID);
288 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800289 if (!mBufferID) {
290 LOGE("Upload to buffer object failed");
291 mUploadDefered = true;
292 return;
293 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800294 GLenum target = (GLenum)getGLTarget();
295 glBindBuffer(target, mBufferID);
Jason Samse4a06c52011-03-16 16:29:28 -0700296 glBufferData(target, mHal.state.type->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800297 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800298 rsc->checkError("Allocation::uploadToBufferObject");
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800299#endif //ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700300}
301
Jason Sams5476b452010-12-08 16:14:36 -0800302void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800303 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800304 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800305 }
306}
307
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800308void Allocation::read(void *data) {
Jason Samse4a06c52011-03-16 16:29:28 -0700309 memcpy(data, mHal.state.mallocPtr, mHal.state.type->getSizeBytes());
Jason Sams40a29e82009-08-10 14:55:26 -0700310}
311
Jason Sams49a05d72010-12-29 14:31:29 -0800312void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
313 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700314 uint32_t eSize = mHal.state.type->getElementSizeBytes();
315 uint8_t * ptr = static_cast<uint8_t *>(mHal.state.mallocPtr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700316 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700317 uint32_t size = count * eSize;
318
319 if (size != sizeBytes) {
320 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse4a06c52011-03-16 16:29:28 -0700321 mHal.state.type->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700322 return;
323 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700324
Jason Samse4a06c52011-03-16 16:29:28 -0700325 if (mHal.state.hasReferences) {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700326 incRefs(data, count);
327 decRefs(ptr, count);
328 }
329
Jason Sams07ae4062009-08-27 20:23:34 -0700330 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700331 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800332 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700333}
334
Jason Sams49a05d72010-12-29 14:31:29 -0800335void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800336 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700337 uint32_t eSize = mHal.state.elementSizeBytes;
Jason Samsd19f10d2009-05-22 14:03:28 -0700338 uint32_t lineSize = eSize * w;
Jason Samse4a06c52011-03-16 16:29:28 -0700339 uint32_t destW = mHal.state.dimensionX;
Jason Samsd19f10d2009-05-22 14:03:28 -0700340
Jason Samsf7086092011-01-12 13:28:37 -0800341 //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -0700342
Jason Samsf7086092011-01-12 13:28:37 -0800343 if ((lineSize * h) != sizeBytes) {
344 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -0700345 rsAssert(!"Allocation::subData called with mismatched size");
346 return;
347 }
348
Jason Samse4a06c52011-03-16 16:29:28 -0700349 if (mHal.state.mallocPtr) {
Jason Samsf7086092011-01-12 13:28:37 -0800350 const uint8_t *src = static_cast<const uint8_t *>(data);
Jason Samse4a06c52011-03-16 16:29:28 -0700351 uint8_t *dst = static_cast<uint8_t *>(mHal.state.mallocPtr);
352 dst += mHal.state.type->getLODFaceOffset(lod, face, xoff, yoff);
Jason Samsf7086092011-01-12 13:28:37 -0800353
354 //LOGE(" %p %p %i ", dst, src, eSize);
355 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samse4a06c52011-03-16 16:29:28 -0700356 if (mHal.state.hasReferences) {
Jason Samsf7086092011-01-12 13:28:37 -0800357 incRefs(src, w);
358 decRefs(dst, w);
359 }
360 memcpy(dst, src, lineSize);
361 src += lineSize;
362 dst += destW * eSize;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700363 }
Jason Samsf7086092011-01-12 13:28:37 -0800364 sendDirty();
365 mUploadDefered = true;
366 } else {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800367 update2DTexture(data, xoff, yoff, lod, face, w, h);
Jason Samsd19f10d2009-05-22 14:03:28 -0700368 }
369}
370
Jason Samsfb9f82c2011-01-12 14:53:25 -0800371void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
372 uint32_t lod, RsAllocationCubemapFace face,
373 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700374}
375
Jason Sams49a05d72010-12-29 14:31:29 -0800376void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800377 uint32_t cIdx, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700378 uint32_t eSize = mHal.state.elementSizeBytes;
379 uint8_t * ptr = static_cast<uint8_t *>(mHal.state.mallocPtr);
Jason Sams49bdaf02010-08-31 13:50:42 -0700380 ptr += eSize * x;
381
Jason Samse4a06c52011-03-16 16:29:28 -0700382 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700383 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
384 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
385 return;
386 }
387
Jason Samse4a06c52011-03-16 16:29:28 -0700388 if (x >= mHal.state.dimensionX) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700389 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
390 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
391 return;
392 }
393
Jason Samse4a06c52011-03-16 16:29:28 -0700394 const Element * e = mHal.state.type->getElement()->getField(cIdx);
395 ptr += mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700396
397 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800398 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700399 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
400 return;
401 }
402
403 if (e->getHasReferences()) {
404 e->incRefs(data);
405 e->decRefs(ptr);
406 }
407
408 memcpy(ptr, data, sizeBytes);
409 sendDirty();
410 mUploadDefered = true;
411}
412
Jason Sams49a05d72010-12-29 14:31:29 -0800413void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800414 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Samse4a06c52011-03-16 16:29:28 -0700415 uint32_t eSize = mHal.state.elementSizeBytes;
416 uint8_t * ptr = static_cast<uint8_t *>(mHal.state.mallocPtr);
417 ptr += eSize * (x + y * mHal.state.dimensionX);
Jason Sams49bdaf02010-08-31 13:50:42 -0700418
Jason Samse4a06c52011-03-16 16:29:28 -0700419 if (x >= mHal.state.dimensionX) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700420 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
421 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
422 return;
423 }
424
Jason Samse4a06c52011-03-16 16:29:28 -0700425 if (y >= mHal.state.dimensionY) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700426 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
427 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
428 return;
429 }
430
Jason Samse4a06c52011-03-16 16:29:28 -0700431 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700432 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
433 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
434 return;
435 }
436
Jason Samse4a06c52011-03-16 16:29:28 -0700437 const Element * e = mHal.state.type->getElement()->getField(cIdx);
438 ptr += mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -0700439
440 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800441 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700442 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
443 return;
444 }
445
446 if (e->getHasReferences()) {
447 e->incRefs(data);
448 e->decRefs(ptr);
449 }
450
451 memcpy(ptr, data, sizeBytes);
452 sendDirty();
453 mUploadDefered = true;
454}
455
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800456void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800457#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700458 mToDirtyList.push(p);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800459#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700460}
Jason Samsd19f10d2009-05-22 14:03:28 -0700461
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800462void Allocation::removeProgramToDirty(const Program *p) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800463#ifndef ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700464 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
465 if (mToDirtyList[ct] == p) {
466 mToDirtyList.removeAt(ct);
467 return;
468 }
469 }
470 rsAssert(0);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800471#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700472}
473
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800474void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800475 ObjectBase::dumpLOGV(prefix);
476
477 String8 s(prefix);
478 s.append(" type ");
Jason Samse4a06c52011-03-16 16:29:28 -0700479 if (mHal.state.type.get()) {
480 mHal.state.type->dumpLOGV(s.string());
Jason Sams715333b2009-11-17 17:26:46 -0800481 }
482
483 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
Jason Samse4a06c52011-03-16 16:29:28 -0700484 prefix, mHal.state.mallocPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
Jason Sams715333b2009-11-17 17:26:46 -0800485
Jason Samsd4b23b52010-12-13 15:32:35 -0800486 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
Jason Samse4a06c52011-03-16 16:29:28 -0700487 prefix, mHal.state.usageFlags, mHal.state.mipmapControl, mTextureID, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800488}
Jason Samsd19f10d2009-05-22 14:03:28 -0700489
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800490void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700491 // Need to identify ourselves
492 stream->addU32((uint32_t)getClassId());
493
494 String8 name(getName());
495 stream->addString(&name);
496
497 // First thing we need to serialize is the type object since it will be needed
498 // to initialize the class
Jason Samse4a06c52011-03-16 16:29:28 -0700499 mHal.state.type->serialize(stream);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700500
Jason Samse4a06c52011-03-16 16:29:28 -0700501 uint32_t dataSize = mHal.state.type->getSizeBytes();
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700502 // Write how much data we are storing
503 stream->addU32(dataSize);
504 // Now write the data
Jason Samse4a06c52011-03-16 16:29:28 -0700505 stream->addByteArray(mHal.state.mallocPtr, dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700506}
507
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800508Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700509 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700510 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800511 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700512 LOGE("allocation loading skipped due to invalid class id\n");
513 return NULL;
514 }
515
516 String8 name;
517 stream->loadString(&name);
518
519 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800520 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700521 return NULL;
522 }
523 type->compute();
524
525 // Number of bytes we wrote out for this allocation
526 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800527 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700528 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700529 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700530 return NULL;
531 }
532
Jason Samsd4b23b52010-12-13 15:32:35 -0800533 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700534 alloc->setName(name.string(), name.size());
535
Jason Sams49a05d72010-12-29 14:31:29 -0800536 uint32_t count = dataSize / type->getElementSizeBytes();
537
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700538 // Read in all of our allocation data
Jason Sams49a05d72010-12-29 14:31:29 -0800539 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700540 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700541
542 return alloc;
543}
544
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800545void Allocation::sendDirty() const {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800546#ifndef ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700547 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
548 mToDirtyList[ct]->forceDirty();
549 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800550#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700551}
Jason Samsd19f10d2009-05-22 14:03:28 -0700552
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800553void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700554 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700555 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700556 uint32_t stride = e->getSizeBytes();
557
Jason Sams5edc6082010-10-05 13:32:49 -0700558 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700559 while (ct > 0) {
560 e->incRefs(p);
561 ct --;
562 p += stride;
563 }
564}
565
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800566void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700567 const uint8_t *p = static_cast<const uint8_t *>(ptr);
Jason Samse4a06c52011-03-16 16:29:28 -0700568 const Element *e = mHal.state.type->getElement();
Jason Samsb28ca96f2010-08-09 18:13:33 -0700569 uint32_t stride = e->getSizeBytes();
570
Jason Sams5edc6082010-10-05 13:32:49 -0700571 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700572 while (ct > 0) {
573 e->decRefs(p);
574 ct --;
575 p += stride;
576 }
577}
578
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800579void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700580}
581
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800582void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Samse4a06c52011-03-16 16:29:28 -0700583 Type *t = mHal.state.type->cloneAndResize1D(rsc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700584
Jason Samse4a06c52011-03-16 16:29:28 -0700585 uint32_t oldDimX = mHal.state.dimensionX;
Jason Sams5edc6082010-10-05 13:32:49 -0700586 if (dimX == oldDimX) {
587 return;
588 }
589
590 if (dimX < oldDimX) {
Jason Samse4a06c52011-03-16 16:29:28 -0700591 decRefs(mHal.state.mallocPtr, oldDimX - dimX, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -0700592 }
Jason Samse4a06c52011-03-16 16:29:28 -0700593 mHal.state.mallocPtr = realloc(mHal.state.mallocPtr, t->getSizeBytes());
Jason Sams5edc6082010-10-05 13:32:49 -0700594
595 if (dimX > oldDimX) {
Jason Samse4a06c52011-03-16 16:29:28 -0700596 const Element *e = mHal.state.type->getElement();
Jason Sams5edc6082010-10-05 13:32:49 -0700597 uint32_t stride = e->getSizeBytes();
Jason Samse4a06c52011-03-16 16:29:28 -0700598 memset(((uint8_t *)mHal.state.mallocPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
Jason Sams5edc6082010-10-05 13:32:49 -0700599 }
Jason Samse4a06c52011-03-16 16:29:28 -0700600
601 mHal.state.type.set(t);
602 updateCache();
Jason Sams5edc6082010-10-05 13:32:49 -0700603}
604
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800605void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700606 LOGE("not implemented");
607}
608
Jason Samsd19f10d2009-05-22 14:03:28 -0700609/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700610//
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800611#ifndef ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700612
Stephen Hines60f9a622011-03-01 17:34:59 -0800613static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
614
Jason Samsd19f10d2009-05-22 14:03:28 -0700615namespace android {
616namespace renderscript {
617
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800618void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700619 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams5e0035a2010-12-13 17:11:21 -0800620 alloc->deferedUploadToTexture(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700621}
622
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800623void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700624 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800625 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700626}
627
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800628static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700629 uint32_t w = out.getDimX();
630 uint32_t h = out.getDimY();
631
Jason Sams6f5c61c2009-07-28 17:20:11 -0700632 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700633 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
634 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
635 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
636
Jason Sams6f5c61c2009-07-28 17:20:11 -0700637 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700638 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
639 oPtr ++;
640 i1 += 2;
641 i2 += 2;
642 }
643 }
644}
645
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800646static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700647 uint32_t w = out.getDimX();
648 uint32_t h = out.getDimY();
649
Jason Sams6f5c61c2009-07-28 17:20:11 -0700650 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700651 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
652 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
653 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
654
Jason Sams6f5c61c2009-07-28 17:20:11 -0700655 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700656 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700657 oPtr ++;
658 i1 += 2;
659 i2 += 2;
660 }
661 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700662}
663
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800664static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800665 uint32_t w = out.getDimX();
666 uint32_t h = out.getDimY();
667
668 for (uint32_t y=0; y < h; y++) {
669 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
670 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
671 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
672
673 for (uint32_t x=0; x < w; x++) {
674 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
675 oPtr ++;
676 i1 += 2;
677 i2 += 2;
678 }
679 }
680}
681
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800682static void mip(const Adapter2D &out, const Adapter2D &in) {
683 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700684 case 32:
685 mip8888(out, in);
686 break;
687 case 16:
688 mip565(out, in);
689 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800690 case 8:
691 mip8(out, in);
692 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700693 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700694}
Jason Samsd19f10d2009-05-22 14:03:28 -0700695
Jason Sams5476b452010-12-08 16:14:36 -0800696void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
697 Allocation *a = static_cast<Allocation *>(va);
698 a->syncAll(rsc, src);
Jason Sams62f258f2011-01-28 15:49:07 -0800699 a->sendDirty();
Jason Sams5476b452010-12-08 16:14:36 -0800700}
701
Jason Samsf7086092011-01-12 13:28:37 -0800702void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700703 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsf7086092011-01-12 13:28:37 -0800704 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams4ef66502010-12-10 16:03:15 -0800705}
706
707void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
708 Allocation *texAlloc = static_cast<Allocation *>(va);
709 const Type * t = texAlloc->getType();
710
711 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
712 if (s != dataLen) {
713 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
714 return;
715 }
716
717 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700718}
719
Jason Sams49a05d72010-12-29 14:31:29 -0800720void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
721 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700722 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800723 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700724}
725
Jason Sams49a05d72010-12-29 14:31:29 -0800726void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
727 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700728 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800729 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700730}
731
Jason Sams49a05d72010-12-29 14:31:29 -0800732void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
733 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700734 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800735 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700736}
737
Jason Sams49a05d72010-12-29 14:31:29 -0800738void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
739 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700740 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800741 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700742}
743
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800744void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700745 Allocation *a = static_cast<Allocation *>(va);
746 a->read(data);
747}
748
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800749void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700750 Allocation *a = static_cast<Allocation *>(va);
751 a->resize1D(rsc, dimX);
752}
753
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800754void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700755 Allocation *a = static_cast<Allocation *>(va);
756 a->resize2D(rsc, dimX, dimY);
757}
758
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700759}
760}
761
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800762static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
763 Context *rsc = static_cast<Context *>(con);
764 Allocation *texAlloc = static_cast<Allocation *>(va);
765 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
766 for (uint32_t face = 0; face < numFaces; face ++) {
767 Adapter2D adapt(rsc, texAlloc);
768 Adapter2D adapt2(rsc, texAlloc);
769 adapt.setFace(face);
770 adapt2.setFace(face);
771 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
772 adapt.setLOD(lod);
773 adapt2.setLOD(lod + 1);
774 mip(adapt2, adapt);
775 }
776 }
777}
778
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800779const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700780 Allocation *a = static_cast<Allocation *>(va);
781 a->getType()->incUserRef();
782
783 return a->getType();
784}
785
Jason Sams5476b452010-12-08 16:14:36 -0800786RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800787 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800788 uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700789 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800790 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Sams31a7e422010-10-26 13:09:17 -0700791 alloc->incUserRef();
792 return alloc;
793}
794
Jason Sams5476b452010-12-08 16:14:36 -0800795
796RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800797 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800798 const void *data, uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700799 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800800 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700801
Jason Sams5476b452010-12-08 16:14:36 -0800802 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700803 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
804 if (texAlloc == NULL) {
805 LOGE("Memory allocation failure");
806 return NULL;
807 }
808
Jason Sams5476b452010-12-08 16:14:36 -0800809 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800810 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800811 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams31a7e422010-10-26 13:09:17 -0700812 }
813
Jason Sams5e0035a2010-12-13 17:11:21 -0800814 texAlloc->deferedUploadToTexture(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700815 return texAlloc;
816}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800817
Jason Sams5476b452010-12-08 16:14:36 -0800818RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800819 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800820 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800821 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800822 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800823
824 // Cubemap allocation's faces should be Width by Width each.
825 // Source data should have 6 * Width by Width pixels
826 // Error checking is done in the java layer
Jason Sams5476b452010-12-08 16:14:36 -0800827 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800828 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
829 if (texAlloc == NULL) {
830 LOGE("Memory allocation failure");
831 return NULL;
832 }
833
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800834 uint32_t faceSize = t->getDimX();
835 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
836 uint32_t copySize = faceSize * t->getElementSizeBytes();
837
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800838 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800839 for (uint32_t face = 0; face < 6; face ++) {
840 Adapter2D faceAdapter(rsc, texAlloc);
841 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800842
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800843 for (uint32_t dI = 0; dI < faceSize; dI ++) {
844 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
845 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800846
Jason Sams5476b452010-12-08 16:14:36 -0800847 // Move the data pointer to the next cube face
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800848 sourcePtr += copySize;
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800849 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800850
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800851 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
852 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800853 }
854
Jason Sams5e0035a2010-12-13 17:11:21 -0800855 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800856 return texAlloc;
857}
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800858
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800859#endif //ANDROID_RS_SERIALIZE