blob: 8ee6e5a4532471f350654b1627ec399c2df8c1e2 [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 */
16
17#include "rsContext.h"
18
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
20#include <GLES/glext.h>
21
Jason Samsd19f10d2009-05-22 14:03:28 -070022using namespace android;
23using namespace android::renderscript;
24
Jason Samsa9e7a052009-09-25 14:51:22 -070025Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070026{
Jason Sams61f08d62009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070029 mPtr = NULL;
30
31 mCpuWrite = false;
32 mCpuRead = false;
33 mGpuWrite = false;
34 mGpuRead = false;
35
36 mReadWriteRatio = 0;
37 mUpdateSize = 0;
38
39 mIsTexture = false;
40 mTextureID = 0;
41
42 mIsVertexBuffer = false;
43 mBufferID = 0;
44
45 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070046 rsAssert(type);
Jason Samsd19f10d2009-05-22 14:03:28 -070047 mPtr = malloc(mType->getSizeBytes());
48 if (!mPtr) {
49 LOGE("Allocation::Allocation, alloc failure");
50 }
Jason Samsd19f10d2009-05-22 14:03:28 -070051}
52
53Allocation::~Allocation()
54{
55}
56
57void Allocation::setCpuWritable(bool)
58{
59}
60
61void Allocation::setGpuWritable(bool)
62{
63}
64
65void Allocation::setCpuReadable(bool)
66{
67}
68
69void Allocation::setGpuReadable(bool)
70{
71}
72
73bool Allocation::fixAllocation()
74{
75 return false;
76}
77
78void Allocation::uploadToTexture(uint32_t lodOffset)
79{
80 //rsAssert(!mTextureId);
81 rsAssert(lodOffset < mType->getLODCount());
82
Jason Samse2ae85f2009-06-03 16:04:54 -070083 GLenum type = mType->getElement()->getGLType();
84 GLenum format = mType->getElement()->getGLFormat();
85
86 if (!type || !format) {
87 return;
88 }
89
Jason Samsd19f10d2009-05-22 14:03:28 -070090 if (!mTextureID) {
91 glGenTextures(1, &mTextureID);
92 }
93 glBindTexture(GL_TEXTURE_2D, mTextureID);
94
Jason Samsa9e7a052009-09-25 14:51:22 -070095 Adapter2D adapt(getContext(), this);
Jason Samsd19f10d2009-05-22 14:03:28 -070096 for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) {
97 adapt.setLOD(lod+lodOffset);
98
99 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -0700100 glTexImage2D(GL_TEXTURE_2D, lod, format,
101 adapt.getDimX(), adapt.getDimY(),
102 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700103 }
104}
105
106void Allocation::uploadToBufferObject()
107{
108 rsAssert(!mType->getDimY());
109 rsAssert(!mType->getDimZ());
110
Jason Samsd19f10d2009-05-22 14:03:28 -0700111 if (!mBufferID) {
112 glGenBuffers(1, &mBufferID);
113 }
114 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
115 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
116 glBindBuffer(GL_ARRAY_BUFFER, 0);
117}
118
Jason Sams1bada8c2009-08-09 17:01:55 -0700119
Jason Sams07ae4062009-08-27 20:23:34 -0700120void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700121{
Jason Sams07ae4062009-08-27 20:23:34 -0700122 uint32_t size = mType->getSizeBytes();
123 if (size != sizeBytes) {
124 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
125 return;
126 }
127 memcpy(mPtr, data, size);
Jason Samsd19f10d2009-05-22 14:03:28 -0700128}
129
Jason Sams40a29e82009-08-10 14:55:26 -0700130void Allocation::read(void *data)
131{
132 memcpy(data, mPtr, mType->getSizeBytes());
133}
134
Jason Sams07ae4062009-08-27 20:23:34 -0700135void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700136{
137 uint32_t eSize = mType->getElementSizeBytes();
138 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
139 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700140 uint32_t size = count * eSize;
141
142 if (size != sizeBytes) {
143 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
144 return;
145 }
146 memcpy(ptr, data, size);
Jason Samsd19f10d2009-05-22 14:03:28 -0700147}
148
Jason Samse2ae85f2009-06-03 16:04:54 -0700149void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700150 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700151{
152 uint32_t eSize = mType->getElementSizeBytes();
153 uint32_t lineSize = eSize * w;
154 uint32_t destW = mType->getDimX();
155
156 const uint8_t *src = static_cast<const uint8_t *>(data);
157 uint8_t *dst = static_cast<uint8_t *>(mPtr);
158 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700159
160 if ((lineSize * eSize * h) != sizeBytes) {
161 rsAssert(!"Allocation::subData called with mismatched size");
162 return;
163 }
164
Jason Samsd19f10d2009-05-22 14:03:28 -0700165 for (uint32_t line=yoff; line < (yoff+h); line++) {
166 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
167 memcpy(dst, src, lineSize);
168 src += lineSize;
169 dst += destW * eSize;
170 }
171}
172
173void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700174 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700175{
176}
177
178
179
180/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700181//
Jason Samsd19f10d2009-05-22 14:03:28 -0700182
183
184namespace android {
185namespace renderscript {
186
187RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
188{
189 const Type * type = static_cast<const Type *>(vtype);
190
Jason Samsa9e7a052009-09-25 14:51:22 -0700191 Allocation * alloc = new Allocation(rsc, type);
Jason Sams07ae4062009-08-27 20:23:34 -0700192 alloc->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700193 return alloc;
194}
195
Jason Samsd19f10d2009-05-22 14:03:28 -0700196RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
197{
Jason Samsa9e7a052009-09-25 14:51:22 -0700198 Type * type = new Type(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700199 type->setDimX(count);
200 type->setElement(static_cast<Element *>(e));
201 type->compute();
202 return rsi_AllocationCreateTyped(rsc, type);
203}
204
205void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
206{
207 Allocation *alloc = static_cast<Allocation *>(va);
208 alloc->uploadToTexture(baseMipLevel);
209}
210
211void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
212{
213 Allocation *alloc = static_cast<Allocation *>(va);
214 alloc->uploadToBufferObject();
215}
216
Jason Samse2ae85f2009-06-03 16:04:54 -0700217static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700218{
219 uint32_t w = out.getDimX();
220 uint32_t h = out.getDimY();
221
Jason Sams6f5c61c2009-07-28 17:20:11 -0700222 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700223 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
224 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
225 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
226
Jason Sams6f5c61c2009-07-28 17:20:11 -0700227 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700228 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
229 oPtr ++;
230 i1 += 2;
231 i2 += 2;
232 }
233 }
234}
235
236static void mip8888(const Adapter2D &out, const Adapter2D &in)
237{
238 uint32_t w = out.getDimX();
239 uint32_t h = out.getDimY();
240
Jason Sams6f5c61c2009-07-28 17:20:11 -0700241 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700242 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
243 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
244 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
245
Jason Sams6f5c61c2009-07-28 17:20:11 -0700246 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700247 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700248 oPtr ++;
249 i1 += 2;
250 i2 += 2;
251 }
252 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700253}
254
Jason Sams6f5c61c2009-07-28 17:20:11 -0700255static void mip(const Adapter2D &out, const Adapter2D &in)
256{
257 switch(out.getBaseType()->getElement()->getSizeBits()) {
258 case 32:
259 mip8888(out, in);
260 break;
261 case 16:
262 mip565(out, in);
263 break;
264
265 }
266
267}
Jason Samsd19f10d2009-05-22 14:03:28 -0700268
Jason Samsfe08d992009-05-27 14:45:32 -0700269typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
270
271static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
272{
273 memcpy(dst, src, count * 2);
274}
275static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
276{
277 memcpy(dst, src, count);
278}
279static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
280{
281 memcpy(dst, src, count * 4);
282}
283
284
285static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
286{
287 uint16_t *d = static_cast<uint16_t *>(dst);
288 const uint8_t *s = static_cast<const uint8_t *>(src);
289
290 while(count--) {
291 *d = rs888to565(s[0], s[1], s[2]);
292 d++;
293 s+= 3;
294 }
295}
296
297static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
298{
299 uint16_t *d = static_cast<uint16_t *>(dst);
300 const uint8_t *s = static_cast<const uint8_t *>(src);
301
302 while(count--) {
303 *d = rs888to565(s[0], s[1], s[2]);
304 d++;
305 s+= 4;
306 }
307}
308
Jason Samsea84a7c2009-09-04 14:42:41 -0700309static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Samsfe08d992009-05-27 14:45:32 -0700310{
Jason Samsea84a7c2009-09-04 14:42:41 -0700311 GLenum srcGLType = src->getGLType();
312 GLenum srcGLFmt = src->getGLFormat();
313 GLenum dstGLType = dst->getGLType();
314 GLenum dstGLFmt = dst->getGLFormat();
315
316 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
317 switch(dst->getSizeBytes()) {
318 case 4:
319 return elementConverter_cpy_32;
320 case 2:
321 return elementConverter_cpy_16;
322 case 1:
323 return elementConverter_cpy_8;
324 }
Jason Samsfe08d992009-05-27 14:45:32 -0700325 }
326
Jason Samsea84a7c2009-09-04 14:42:41 -0700327 if (srcGLType == GL_UNSIGNED_BYTE &&
328 srcGLFmt == GL_RGB &&
329 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
330 dstGLType == GL_RGB) {
331
Jason Samsfe08d992009-05-27 14:45:32 -0700332 return elementConverter_888_to_565;
333 }
334
Jason Samsea84a7c2009-09-04 14:42:41 -0700335 if (srcGLType == GL_UNSIGNED_BYTE &&
336 srcGLFmt == GL_RGBA &&
337 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
338 dstGLType == GL_RGB) {
339
Jason Samsfe08d992009-05-27 14:45:32 -0700340 return elementConverter_8888_to_565;
341 }
342
Jason Samsea84a7c2009-09-04 14:42:41 -0700343 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700344 return 0;
345}
346
347
Jason Samsea84a7c2009-09-04 14:42:41 -0700348RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Samsfe08d992009-05-27 14:45:32 -0700349{
Jason Samsea84a7c2009-09-04 14:42:41 -0700350 const Element *src = static_cast<const Element *>(_src);
351 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700352 rsAssert(!(w & (w-1)));
353 rsAssert(!(h & (h-1)));
354
355 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsea84a7c2009-09-04 14:42:41 -0700356 rsi_TypeBegin(rsc, _dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700357 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
358 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
359 if (genMips) {
360 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
361 }
362 RsType type = rsi_TypeCreate(rsc);
363
364 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
365 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
366 if (texAlloc == NULL) {
367 LOGE("Memory allocation failure");
368 return NULL;
369 }
Jason Sams07ae4062009-08-27 20:23:34 -0700370 texAlloc->incUserRef();
Jason Samsfe08d992009-05-27 14:45:32 -0700371
Jason Samsea84a7c2009-09-04 14:42:41 -0700372 ElementConverter_t cvt = pickConverter(dst, src);
Jason Samsfe08d992009-05-27 14:45:32 -0700373 cvt(texAlloc->getPtr(), data, w * h);
374
375 if (genMips) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700376 Adapter2D adapt(rsc, texAlloc);
377 Adapter2D adapt2(rsc, texAlloc);
Jason Samsfe08d992009-05-27 14:45:32 -0700378 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
379 adapt.setLOD(lod);
380 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700381 mip(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700382 }
383 }
384
385 return texAlloc;
386}
387
Jason Samsea84a7c2009-09-04 14:42:41 -0700388RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Samsb0ec1b42009-07-28 12:02:16 -0700389{
Jason Samsea84a7c2009-09-04 14:42:41 -0700390 const Element *srcE = static_cast<const Element *>(_src);
391 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700392 uint32_t w2 = rsHigherPow2(w);
393 uint32_t h2 = rsHigherPow2(h);
394
395 if ((w2 == w) && (h2 == h)) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700396 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700397 }
398
Jason Samsea84a7c2009-09-04 14:42:41 -0700399 uint32_t bpp = srcE->getSizeBytes();
Jason Samsb0ec1b42009-07-28 12:02:16 -0700400 size_t size = w2 * h2 * bpp;
401 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
402 memset(tmp, 0, size);
403
404 const uint8_t * src = static_cast<const uint8_t *>(data);
405 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700406 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Jason Samsb0ec1b42009-07-28 12:02:16 -0700407 memcpy(&ydst[(w2 - w) >> 1], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700408 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700409 }
410
Jason Samsea84a7c2009-09-04 14:42:41 -0700411 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700412 free(tmp);
413 return ret;
414
415
416
417
418}
419
Jason Sams07ae4062009-08-27 20:23:34 -0700420void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700421{
422 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700423 a->data(data, sizeBytes);
Jason Sams9bee51c2009-08-05 13:57:03 -0700424 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700425}
426
Jason Sams07ae4062009-08-27 20:23:34 -0700427void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700428{
429 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700430 a->subData(xoff, count, data, sizeBytes);
Jason Sams9bee51c2009-08-05 13:57:03 -0700431 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700432}
433
Jason Sams07ae4062009-08-27 20:23:34 -0700434void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700435{
436 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700437 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Sams9bee51c2009-08-05 13:57:03 -0700438 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700439}
440
Jason Sams40a29e82009-08-10 14:55:26 -0700441void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
442{
443 Allocation *a = static_cast<Allocation *>(va);
444 a->read(data);
445}
446
Jason Samsd19f10d2009-05-22 14:03:28 -0700447
448}
449}