blob: 38cec64e3753ef310406fd55e3234d41f9d4102a [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{
Jason Samsb7a6c432009-11-02 14:25:10 -080055 free(mPtr);
56 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080057
58 if (mBufferID) {
59 // Causes a SW crash....
60 //LOGV(" mBufferID %i", mBufferID);
61 //glDeleteBuffers(1, &mBufferID);
62 //mBufferID = 0;
63 }
64 if (mTextureID) {
65 glDeleteTextures(1, &mTextureID);
66 mTextureID = 0;
67 }
Jason Samsd19f10d2009-05-22 14:03:28 -070068}
69
70void Allocation::setCpuWritable(bool)
71{
72}
73
74void Allocation::setGpuWritable(bool)
75{
76}
77
78void Allocation::setCpuReadable(bool)
79{
80}
81
82void Allocation::setGpuReadable(bool)
83{
84}
85
86bool Allocation::fixAllocation()
87{
88 return false;
89}
90
91void Allocation::uploadToTexture(uint32_t lodOffset)
92{
93 //rsAssert(!mTextureId);
94 rsAssert(lodOffset < mType->getLODCount());
95
Jason Samse2ae85f2009-06-03 16:04:54 -070096 GLenum type = mType->getElement()->getGLType();
97 GLenum format = mType->getElement()->getGLFormat();
98
99 if (!type || !format) {
100 return;
101 }
102
Jason Samsd19f10d2009-05-22 14:03:28 -0700103 if (!mTextureID) {
104 glGenTextures(1, &mTextureID);
105 }
106 glBindTexture(GL_TEXTURE_2D, mTextureID);
107
Jason Samsa9e7a052009-09-25 14:51:22 -0700108 Adapter2D adapt(getContext(), this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700109 for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) {
110 adapt.setLOD(lod+lodOffset);
111
112 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -0700113 glTexImage2D(GL_TEXTURE_2D, lod, format,
114 adapt.getDimX(), adapt.getDimY(),
115 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700116 }
117}
118
119void Allocation::uploadToBufferObject()
120{
121 rsAssert(!mType->getDimY());
122 rsAssert(!mType->getDimZ());
123
Jason Samsd19f10d2009-05-22 14:03:28 -0700124 if (!mBufferID) {
125 glGenBuffers(1, &mBufferID);
126 }
127 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
128 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
129 glBindBuffer(GL_ARRAY_BUFFER, 0);
130}
131
Jason Sams1bada8c2009-08-09 17:01:55 -0700132
Jason Sams07ae4062009-08-27 20:23:34 -0700133void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700134{
Jason Sams07ae4062009-08-27 20:23:34 -0700135 uint32_t size = mType->getSizeBytes();
136 if (size != sizeBytes) {
137 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
138 return;
139 }
140 memcpy(mPtr, data, size);
Jason Samsd19f10d2009-05-22 14:03:28 -0700141}
142
Jason Sams40a29e82009-08-10 14:55:26 -0700143void Allocation::read(void *data)
144{
145 memcpy(data, mPtr, mType->getSizeBytes());
146}
147
Jason Sams07ae4062009-08-27 20:23:34 -0700148void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700149{
150 uint32_t eSize = mType->getElementSizeBytes();
151 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
152 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700153 uint32_t size = count * eSize;
154
155 if (size != sizeBytes) {
156 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700157 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700158 return;
159 }
160 memcpy(ptr, data, size);
Jason Samsd19f10d2009-05-22 14:03:28 -0700161}
162
Jason Samse2ae85f2009-06-03 16:04:54 -0700163void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700164 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700165{
166 uint32_t eSize = mType->getElementSizeBytes();
167 uint32_t lineSize = eSize * w;
168 uint32_t destW = mType->getDimX();
169
170 const uint8_t *src = static_cast<const uint8_t *>(data);
171 uint8_t *dst = static_cast<uint8_t *>(mPtr);
172 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700173
174 if ((lineSize * eSize * h) != sizeBytes) {
175 rsAssert(!"Allocation::subData called with mismatched size");
176 return;
177 }
178
Jason Samsd19f10d2009-05-22 14:03:28 -0700179 for (uint32_t line=yoff; line < (yoff+h); line++) {
180 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
181 memcpy(dst, src, lineSize);
182 src += lineSize;
183 dst += destW * eSize;
184 }
185}
186
187void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700188 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700189{
190}
191
192
193
194/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700195//
Jason Samsd19f10d2009-05-22 14:03:28 -0700196
197
198namespace android {
199namespace renderscript {
200
201RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
202{
203 const Type * type = static_cast<const Type *>(vtype);
204
Jason Samsa9e7a052009-09-25 14:51:22 -0700205 Allocation * alloc = new Allocation(rsc, type);
Jason Sams07ae4062009-08-27 20:23:34 -0700206 alloc->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700207 return alloc;
208}
209
Jason Samsd19f10d2009-05-22 14:03:28 -0700210RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
211{
Jason Samsa9e7a052009-09-25 14:51:22 -0700212 Type * type = new Type(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700213 type->setDimX(count);
214 type->setElement(static_cast<Element *>(e));
215 type->compute();
216 return rsi_AllocationCreateTyped(rsc, type);
217}
218
219void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
220{
221 Allocation *alloc = static_cast<Allocation *>(va);
222 alloc->uploadToTexture(baseMipLevel);
223}
224
225void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
226{
227 Allocation *alloc = static_cast<Allocation *>(va);
228 alloc->uploadToBufferObject();
229}
230
Jason Samse2ae85f2009-06-03 16:04:54 -0700231static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700232{
233 uint32_t w = out.getDimX();
234 uint32_t h = out.getDimY();
235
Jason Sams6f5c61c2009-07-28 17:20:11 -0700236 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700237 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
238 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
239 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
240
Jason Sams6f5c61c2009-07-28 17:20:11 -0700241 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700242 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
243 oPtr ++;
244 i1 += 2;
245 i2 += 2;
246 }
247 }
248}
249
250static void mip8888(const Adapter2D &out, const Adapter2D &in)
251{
252 uint32_t w = out.getDimX();
253 uint32_t h = out.getDimY();
254
Jason Sams6f5c61c2009-07-28 17:20:11 -0700255 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700256 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
257 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
258 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
259
Jason Sams6f5c61c2009-07-28 17:20:11 -0700260 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700261 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700262 oPtr ++;
263 i1 += 2;
264 i2 += 2;
265 }
266 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700267}
268
Jason Sams6f5c61c2009-07-28 17:20:11 -0700269static void mip(const Adapter2D &out, const Adapter2D &in)
270{
271 switch(out.getBaseType()->getElement()->getSizeBits()) {
272 case 32:
273 mip8888(out, in);
274 break;
275 case 16:
276 mip565(out, in);
277 break;
278
279 }
280
281}
Jason Samsd19f10d2009-05-22 14:03:28 -0700282
Jason Samsfe08d992009-05-27 14:45:32 -0700283typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
284
285static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
286{
287 memcpy(dst, src, count * 2);
288}
289static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
290{
291 memcpy(dst, src, count);
292}
293static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
294{
295 memcpy(dst, src, count * 4);
296}
297
298
299static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
300{
301 uint16_t *d = static_cast<uint16_t *>(dst);
302 const uint8_t *s = static_cast<const uint8_t *>(src);
303
304 while(count--) {
305 *d = rs888to565(s[0], s[1], s[2]);
306 d++;
307 s+= 3;
308 }
309}
310
311static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
312{
313 uint16_t *d = static_cast<uint16_t *>(dst);
314 const uint8_t *s = static_cast<const uint8_t *>(src);
315
316 while(count--) {
317 *d = rs888to565(s[0], s[1], s[2]);
318 d++;
319 s+= 4;
320 }
321}
322
Jason Samsea84a7c2009-09-04 14:42:41 -0700323static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Samsfe08d992009-05-27 14:45:32 -0700324{
Jason Samsea84a7c2009-09-04 14:42:41 -0700325 GLenum srcGLType = src->getGLType();
326 GLenum srcGLFmt = src->getGLFormat();
327 GLenum dstGLType = dst->getGLType();
328 GLenum dstGLFmt = dst->getGLFormat();
329
330 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
331 switch(dst->getSizeBytes()) {
332 case 4:
333 return elementConverter_cpy_32;
334 case 2:
335 return elementConverter_cpy_16;
336 case 1:
337 return elementConverter_cpy_8;
338 }
Jason Samsfe08d992009-05-27 14:45:32 -0700339 }
340
Jason Samsea84a7c2009-09-04 14:42:41 -0700341 if (srcGLType == GL_UNSIGNED_BYTE &&
342 srcGLFmt == GL_RGB &&
343 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
344 dstGLType == GL_RGB) {
345
Jason Samsfe08d992009-05-27 14:45:32 -0700346 return elementConverter_888_to_565;
347 }
348
Jason Samsea84a7c2009-09-04 14:42:41 -0700349 if (srcGLType == GL_UNSIGNED_BYTE &&
350 srcGLFmt == GL_RGBA &&
351 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
352 dstGLType == GL_RGB) {
353
Jason Samsfe08d992009-05-27 14:45:32 -0700354 return elementConverter_8888_to_565;
355 }
356
Jason Samsea84a7c2009-09-04 14:42:41 -0700357 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700358 return 0;
359}
360
361
Jason Samsea84a7c2009-09-04 14:42:41 -0700362RsAllocation 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 -0700363{
Jason Samsea84a7c2009-09-04 14:42:41 -0700364 const Element *src = static_cast<const Element *>(_src);
365 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700366 rsAssert(!(w & (w-1)));
367 rsAssert(!(h & (h-1)));
368
369 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsea84a7c2009-09-04 14:42:41 -0700370 rsi_TypeBegin(rsc, _dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700371 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
372 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
373 if (genMips) {
374 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
375 }
376 RsType type = rsi_TypeCreate(rsc);
377
378 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
379 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
380 if (texAlloc == NULL) {
381 LOGE("Memory allocation failure");
382 return NULL;
383 }
Jason Sams07ae4062009-08-27 20:23:34 -0700384 texAlloc->incUserRef();
Jason Samsfe08d992009-05-27 14:45:32 -0700385
Jason Samsea84a7c2009-09-04 14:42:41 -0700386 ElementConverter_t cvt = pickConverter(dst, src);
Jason Samsfe08d992009-05-27 14:45:32 -0700387 cvt(texAlloc->getPtr(), data, w * h);
388
389 if (genMips) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700390 Adapter2D adapt(rsc, texAlloc);
391 Adapter2D adapt2(rsc, texAlloc);
Jason Samsfe08d992009-05-27 14:45:32 -0700392 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
393 adapt.setLOD(lod);
394 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700395 mip(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700396 }
397 }
398
399 return texAlloc;
400}
401
Jason Samsea84a7c2009-09-04 14:42:41 -0700402RsAllocation 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 -0700403{
Jason Samsea84a7c2009-09-04 14:42:41 -0700404 const Element *srcE = static_cast<const Element *>(_src);
405 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700406 uint32_t w2 = rsHigherPow2(w);
407 uint32_t h2 = rsHigherPow2(h);
408
409 if ((w2 == w) && (h2 == h)) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700410 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700411 }
412
Jason Samsea84a7c2009-09-04 14:42:41 -0700413 uint32_t bpp = srcE->getSizeBytes();
Jason Samsb0ec1b42009-07-28 12:02:16 -0700414 size_t size = w2 * h2 * bpp;
415 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
416 memset(tmp, 0, size);
417
418 const uint8_t * src = static_cast<const uint8_t *>(data);
419 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700420 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen911458a2009-10-28 15:10:56 -0700421 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700422 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700423 }
424
Jason Samsea84a7c2009-09-04 14:42:41 -0700425 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700426 free(tmp);
427 return ret;
428
429
430
431
432}
433
Jason Sams07ae4062009-08-27 20:23:34 -0700434void rsi_AllocationData(Context *rsc, RsAllocation va, 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->data(data, sizeBytes);
Jason Sams9bee51c2009-08-05 13:57:03 -0700438 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700439}
440
Jason Sams07ae4062009-08-27 20:23:34 -0700441void 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 -0700442{
443 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700444 a->subData(xoff, count, data, sizeBytes);
Jason Sams9bee51c2009-08-05 13:57:03 -0700445 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700446}
447
Jason Sams07ae4062009-08-27 20:23:34 -0700448void 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 -0700449{
450 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700451 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Sams9bee51c2009-08-05 13:57:03 -0700452 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700453}
454
Jason Sams40a29e82009-08-10 14:55:26 -0700455void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
456{
457 Allocation *a = static_cast<Allocation *>(va);
458 a->read(data);
459}
460
Jason Samsd19f10d2009-05-22 14:03:28 -0700461
462}
463}