blob: 5e0959a18b962e8613e6c812610286576952bd7e [file] [log] [blame]
Jason Sams326e0dd2009-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 Sams1aa5a4e2009-06-22 17:15:15 -070019#include <GLES/gl.h>
20#include <GLES/glext.h>
21
Jason Sams326e0dd2009-05-22 14:03:28 -070022using namespace android;
23using namespace android::renderscript;
24
Jason Samse514b452009-09-25 14:51:22 -070025Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070026{
Jason Samsf2649a92009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Sams326e0dd2009-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 Samse5ffb872009-08-09 17:01:55 -070046 rsAssert(type);
Jason Sams326e0dd2009-05-22 14:03:28 -070047 mPtr = malloc(mType->getSizeBytes());
48 if (!mPtr) {
49 LOGE("Allocation::Allocation, alloc failure");
50 }
Jason Sams326e0dd2009-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 Sams565ac362009-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 Sams326e0dd2009-05-22 14:03:28 -070090 if (!mTextureID) {
91 glGenTextures(1, &mTextureID);
92 }
93 glBindTexture(GL_TEXTURE_2D, mTextureID);
94
Jason Samse514b452009-09-25 14:51:22 -070095 Adapter2D adapt(getContext(), this);
Jason Sams326e0dd2009-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 Sams565ac362009-06-03 16:04:54 -0700100 glTexImage2D(GL_TEXTURE_2D, lod, format,
101 adapt.getDimX(), adapt.getDimY(),
102 0, format, type, ptr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700103 }
104}
105
106void Allocation::uploadToBufferObject()
107{
108 rsAssert(!mType->getDimY());
109 rsAssert(!mType->getDimZ());
110
Jason Sams326e0dd2009-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 Samse5ffb872009-08-09 17:01:55 -0700119
Jason Sams9397e302009-08-27 20:23:34 -0700120void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700121{
Jason Sams9397e302009-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 Sams5c3e3bc2009-10-26 15:19:28 -0700128 sendDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700129}
130
Jason Samse579df42009-08-10 14:55:26 -0700131void Allocation::read(void *data)
132{
133 memcpy(data, mPtr, mType->getSizeBytes());
134}
135
Jason Sams9397e302009-08-27 20:23:34 -0700136void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700137{
138 uint32_t eSize = mType->getElementSizeBytes();
139 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
140 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700141 uint32_t size = count * eSize;
142
143 if (size != sizeBytes) {
144 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700145 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700146 return;
147 }
148 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700149 sendDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700150}
151
Jason Sams565ac362009-06-03 16:04:54 -0700152void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams9397e302009-08-27 20:23:34 -0700153 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700154{
155 uint32_t eSize = mType->getElementSizeBytes();
156 uint32_t lineSize = eSize * w;
157 uint32_t destW = mType->getDimX();
158
159 const uint8_t *src = static_cast<const uint8_t *>(data);
160 uint8_t *dst = static_cast<uint8_t *>(mPtr);
161 dst += eSize * (xoff + yoff * destW);
Jason Sams9397e302009-08-27 20:23:34 -0700162
163 if ((lineSize * eSize * h) != sizeBytes) {
164 rsAssert(!"Allocation::subData called with mismatched size");
165 return;
166 }
167
Jason Sams326e0dd2009-05-22 14:03:28 -0700168 for (uint32_t line=yoff; line < (yoff+h); line++) {
169 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
170 memcpy(dst, src, lineSize);
171 src += lineSize;
172 dst += destW * eSize;
173 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700174 sendDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700175}
176
177void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams9397e302009-08-27 20:23:34 -0700178 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700179{
180}
181
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700182void Allocation::addProgramToDirty(const Program *p)
183{
184 mToDirtyList.add(p);
185}
Jason Sams326e0dd2009-05-22 14:03:28 -0700186
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700187void Allocation::removeProgramToDirty(const Program *p)
188{
189 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
190 if (mToDirtyList[ct] == p) {
191 mToDirtyList.removeAt(ct);
192 return;
193 }
194 }
195 rsAssert(0);
196}
197
198void Allocation::sendDirty() const
199{
200 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
201 mToDirtyList[ct]->forceDirty();
202 }
203}
Jason Sams326e0dd2009-05-22 14:03:28 -0700204
205/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700206//
Jason Sams326e0dd2009-05-22 14:03:28 -0700207
208
209namespace android {
210namespace renderscript {
211
212RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
213{
214 const Type * type = static_cast<const Type *>(vtype);
215
Jason Samse514b452009-09-25 14:51:22 -0700216 Allocation * alloc = new Allocation(rsc, type);
Jason Sams9397e302009-08-27 20:23:34 -0700217 alloc->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700218 return alloc;
219}
220
Jason Sams326e0dd2009-05-22 14:03:28 -0700221RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
222{
Jason Samse514b452009-09-25 14:51:22 -0700223 Type * type = new Type(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700224 type->setDimX(count);
225 type->setElement(static_cast<Element *>(e));
226 type->compute();
227 return rsi_AllocationCreateTyped(rsc, type);
228}
229
230void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
231{
232 Allocation *alloc = static_cast<Allocation *>(va);
233 alloc->uploadToTexture(baseMipLevel);
234}
235
236void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
237{
238 Allocation *alloc = static_cast<Allocation *>(va);
239 alloc->uploadToBufferObject();
240}
241
Jason Sams565ac362009-06-03 16:04:54 -0700242static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Sams326e0dd2009-05-22 14:03:28 -0700243{
244 uint32_t w = out.getDimX();
245 uint32_t h = out.getDimY();
246
Jason Samse9f5c532009-07-28 17:20:11 -0700247 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700248 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
249 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
250 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
251
Jason Samse9f5c532009-07-28 17:20:11 -0700252 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700253 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
254 oPtr ++;
255 i1 += 2;
256 i2 += 2;
257 }
258 }
259}
260
261static void mip8888(const Adapter2D &out, const Adapter2D &in)
262{
263 uint32_t w = out.getDimX();
264 uint32_t h = out.getDimY();
265
Jason Samse9f5c532009-07-28 17:20:11 -0700266 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700267 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
268 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
269 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
270
Jason Samse9f5c532009-07-28 17:20:11 -0700271 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700272 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700273 oPtr ++;
274 i1 += 2;
275 i2 += 2;
276 }
277 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700278}
279
Jason Samse9f5c532009-07-28 17:20:11 -0700280static void mip(const Adapter2D &out, const Adapter2D &in)
281{
282 switch(out.getBaseType()->getElement()->getSizeBits()) {
283 case 32:
284 mip8888(out, in);
285 break;
286 case 16:
287 mip565(out, in);
288 break;
289
290 }
291
292}
Jason Sams326e0dd2009-05-22 14:03:28 -0700293
Jason Sams6678e9b2009-05-27 14:45:32 -0700294typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
295
296static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
297{
298 memcpy(dst, src, count * 2);
299}
300static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
301{
302 memcpy(dst, src, count);
303}
304static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
305{
306 memcpy(dst, src, count * 4);
307}
308
309
310static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
311{
312 uint16_t *d = static_cast<uint16_t *>(dst);
313 const uint8_t *s = static_cast<const uint8_t *>(src);
314
315 while(count--) {
316 *d = rs888to565(s[0], s[1], s[2]);
317 d++;
318 s+= 3;
319 }
320}
321
322static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
323{
324 uint16_t *d = static_cast<uint16_t *>(dst);
325 const uint8_t *s = static_cast<const uint8_t *>(src);
326
327 while(count--) {
328 *d = rs888to565(s[0], s[1], s[2]);
329 d++;
330 s+= 4;
331 }
332}
333
Jason Samsa57c0a72009-09-04 14:42:41 -0700334static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Sams6678e9b2009-05-27 14:45:32 -0700335{
Jason Samsa57c0a72009-09-04 14:42:41 -0700336 GLenum srcGLType = src->getGLType();
337 GLenum srcGLFmt = src->getGLFormat();
338 GLenum dstGLType = dst->getGLType();
339 GLenum dstGLFmt = dst->getGLFormat();
340
341 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
342 switch(dst->getSizeBytes()) {
343 case 4:
344 return elementConverter_cpy_32;
345 case 2:
346 return elementConverter_cpy_16;
347 case 1:
348 return elementConverter_cpy_8;
349 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700350 }
351
Jason Samsa57c0a72009-09-04 14:42:41 -0700352 if (srcGLType == GL_UNSIGNED_BYTE &&
353 srcGLFmt == GL_RGB &&
354 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
355 dstGLType == GL_RGB) {
356
Jason Sams6678e9b2009-05-27 14:45:32 -0700357 return elementConverter_888_to_565;
358 }
359
Jason Samsa57c0a72009-09-04 14:42:41 -0700360 if (srcGLType == GL_UNSIGNED_BYTE &&
361 srcGLFmt == GL_RGBA &&
362 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
363 dstGLType == GL_RGB) {
364
Jason Sams6678e9b2009-05-27 14:45:32 -0700365 return elementConverter_8888_to_565;
366 }
367
Jason Samsa57c0a72009-09-04 14:42:41 -0700368 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700369 return 0;
370}
371
372
Jason Samsa57c0a72009-09-04 14:42:41 -0700373RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Sams6678e9b2009-05-27 14:45:32 -0700374{
Jason Samsa57c0a72009-09-04 14:42:41 -0700375 const Element *src = static_cast<const Element *>(_src);
376 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsc9d43db2009-07-28 12:02:16 -0700377 rsAssert(!(w & (w-1)));
378 rsAssert(!(h & (h-1)));
379
380 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsa57c0a72009-09-04 14:42:41 -0700381 rsi_TypeBegin(rsc, _dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700382 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
383 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
384 if (genMips) {
385 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
386 }
387 RsType type = rsi_TypeCreate(rsc);
388
389 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
390 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
391 if (texAlloc == NULL) {
392 LOGE("Memory allocation failure");
393 return NULL;
394 }
Jason Sams9397e302009-08-27 20:23:34 -0700395 texAlloc->incUserRef();
Jason Sams6678e9b2009-05-27 14:45:32 -0700396
Jason Samsa57c0a72009-09-04 14:42:41 -0700397 ElementConverter_t cvt = pickConverter(dst, src);
Jason Sams6678e9b2009-05-27 14:45:32 -0700398 cvt(texAlloc->getPtr(), data, w * h);
399
400 if (genMips) {
Jason Samse514b452009-09-25 14:51:22 -0700401 Adapter2D adapt(rsc, texAlloc);
402 Adapter2D adapt2(rsc, texAlloc);
Jason Sams6678e9b2009-05-27 14:45:32 -0700403 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
404 adapt.setLOD(lod);
405 adapt2.setLOD(lod + 1);
Jason Samse9f5c532009-07-28 17:20:11 -0700406 mip(adapt2, adapt);
Jason Sams6678e9b2009-05-27 14:45:32 -0700407 }
408 }
409
410 return texAlloc;
411}
412
Jason Samsa57c0a72009-09-04 14:42:41 -0700413RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Samsc9d43db2009-07-28 12:02:16 -0700414{
Jason Samsa57c0a72009-09-04 14:42:41 -0700415 const Element *srcE = static_cast<const Element *>(_src);
416 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsc9d43db2009-07-28 12:02:16 -0700417 uint32_t w2 = rsHigherPow2(w);
418 uint32_t h2 = rsHigherPow2(h);
419
420 if ((w2 == w) && (h2 == h)) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700421 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsc9d43db2009-07-28 12:02:16 -0700422 }
423
Jason Samsa57c0a72009-09-04 14:42:41 -0700424 uint32_t bpp = srcE->getSizeBytes();
Jason Samsc9d43db2009-07-28 12:02:16 -0700425 size_t size = w2 * h2 * bpp;
426 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
427 memset(tmp, 0, size);
428
429 const uint8_t * src = static_cast<const uint8_t *>(data);
430 for (uint32_t y = 0; y < h; y++) {
Jason Sams50253db2009-07-29 20:55:44 -0700431 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Jason Samsc9d43db2009-07-28 12:02:16 -0700432 memcpy(&ydst[(w2 - w) >> 1], src, w * bpp);
Jason Sams50253db2009-07-29 20:55:44 -0700433 src += w * bpp;
Jason Samsc9d43db2009-07-28 12:02:16 -0700434 }
435
Jason Samsa57c0a72009-09-04 14:42:41 -0700436 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsc9d43db2009-07-28 12:02:16 -0700437 free(tmp);
438 return ret;
Jason Samsc9d43db2009-07-28 12:02:16 -0700439}
440
Jason Sams9397e302009-08-27 20:23:34 -0700441void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700442{
443 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700444 a->data(data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700445}
446
Jason Sams9397e302009-08-27 20:23:34 -0700447void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700448{
449 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700450 a->subData(xoff, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700451}
452
Jason Sams9397e302009-08-27 20:23:34 -0700453void 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 Sams326e0dd2009-05-22 14:03:28 -0700454{
455 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700456 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700457}
458
Jason Samse579df42009-08-10 14:55:26 -0700459void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
460{
461 Allocation *a = static_cast<Allocation *>(va);
462 a->read(data);
463}
464
Jason Sams326e0dd2009-05-22 14:03:28 -0700465
466}
467}