blob: f1798de0f7b4afc84d1d7e62e4864e617dfc3149 [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>
Jason Samsc2908e62010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <GLES/glext.h>
22
Jason Samsd19f10d2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
Jason Samsa9e7a052009-09-25 14:51:22 -070026Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070027{
Jason Sams61f08d62009-09-25 16:37:33 -070028 mAllocFile = __FILE__;
29 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070030 mPtr = NULL;
31
32 mCpuWrite = false;
33 mCpuRead = false;
34 mGpuWrite = false;
35 mGpuRead = false;
36
37 mReadWriteRatio = 0;
38 mUpdateSize = 0;
39
40 mIsTexture = false;
41 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070042 mIsVertexBuffer = false;
43 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080044 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070045
46 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070047 rsAssert(type);
Jason Samsd19f10d2009-05-22 14:03:28 -070048 mPtr = malloc(mType->getSizeBytes());
49 if (!mPtr) {
50 LOGE("Allocation::Allocation, alloc failure");
51 }
Jason Samsd19f10d2009-05-22 14:03:28 -070052}
53
54Allocation::~Allocation()
55{
Jason Samsb7a6c432009-11-02 14:25:10 -080056 free(mPtr);
57 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080058
59 if (mBufferID) {
60 // Causes a SW crash....
61 //LOGV(" mBufferID %i", mBufferID);
62 //glDeleteBuffers(1, &mBufferID);
63 //mBufferID = 0;
64 }
65 if (mTextureID) {
66 glDeleteTextures(1, &mTextureID);
67 mTextureID = 0;
68 }
Jason Samsd19f10d2009-05-22 14:03:28 -070069}
70
71void Allocation::setCpuWritable(bool)
72{
73}
74
75void Allocation::setGpuWritable(bool)
76{
77}
78
79void Allocation::setCpuReadable(bool)
80{
81}
82
83void Allocation::setGpuReadable(bool)
84{
85}
86
87bool Allocation::fixAllocation()
88{
89 return false;
90}
91
Jason Samsc2908e62010-02-23 17:44:28 -080092void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Sams3b7d39b2009-12-14 12:57:40 -080093{
94 rsAssert(lodOffset < mType->getLODCount());
95 mIsTexture = true;
96 mTextureLOD = lodOffset;
97 mUploadDefered = true;
Jason Samsc2908e62010-02-23 17:44:28 -080098 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -080099}
100
101void Allocation::uploadToTexture(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700102{
103 //rsAssert(!mTextureId);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800104
105 mIsTexture = true;
106 if (!rsc->checkDriver()) {
107 mUploadDefered = true;
108 return;
109 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700110
Jason Sams718cd1f2009-12-23 14:35:29 -0800111 GLenum type = mType->getElement()->getComponent().getGLType();
112 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700113
114 if (!type || !format) {
115 return;
116 }
117
Jason Samsd19f10d2009-05-22 14:03:28 -0700118 if (!mTextureID) {
119 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800120
121 if (!mTextureID) {
122 // This should not happen, however, its likely the cause of the
123 // white sqare bug.
124 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
125 LOGE("Upload to texture failed to gen mTextureID");
126 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800127 mUploadDefered = true;
128 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800129 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700130 }
131 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800132 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700133
Jason Samsa9e7a052009-09-25 14:51:22 -0700134 Adapter2D adapt(getContext(), this);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800135 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
136 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700137
138 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -0700139 glTexImage2D(GL_TEXTURE_2D, lod, format,
140 adapt.getDimX(), adapt.getDimY(),
141 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700142 }
Jason Samsc2908e62010-02-23 17:44:28 -0800143 if (mTextureGenMipmap) {
144 glGenerateMipmap(GL_TEXTURE_2D);
145 }
146
Jason Samsd19f10d2009-05-22 14:03:28 -0700147}
148
Jason Sams3b7d39b2009-12-14 12:57:40 -0800149void Allocation::deferedUploadToBufferObject(const Context *rsc)
150{
151 mIsVertexBuffer = true;
152 mUploadDefered = true;
153}
154
155void Allocation::uploadToBufferObject(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700156{
157 rsAssert(!mType->getDimY());
158 rsAssert(!mType->getDimZ());
159
Jason Sams3b7d39b2009-12-14 12:57:40 -0800160 mIsVertexBuffer = true;
161 if (!rsc->checkDriver()) {
162 mUploadDefered = true;
163 return;
164 }
165
Jason Samsd19f10d2009-05-22 14:03:28 -0700166 if (!mBufferID) {
167 glGenBuffers(1, &mBufferID);
168 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800169 if (!mBufferID) {
170 LOGE("Upload to buffer object failed");
171 mUploadDefered = true;
172 return;
173 }
174
Jason Samsd19f10d2009-05-22 14:03:28 -0700175 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
176 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
177 glBindBuffer(GL_ARRAY_BUFFER, 0);
178}
179
Jason Sams3b7d39b2009-12-14 12:57:40 -0800180void Allocation::uploadCheck(const Context *rsc)
181{
182 if (mUploadDefered) {
183 mUploadDefered = false;
184 if (mIsVertexBuffer) {
185 uploadToBufferObject(rsc);
186 }
187 if (mIsTexture) {
188 uploadToTexture(rsc);
189 }
190 }
191}
192
Jason Sams1bada8c2009-08-09 17:01:55 -0700193
Jason Sams07ae4062009-08-27 20:23:34 -0700194void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700195{
Jason Sams07ae4062009-08-27 20:23:34 -0700196 uint32_t size = mType->getSizeBytes();
197 if (size != sizeBytes) {
198 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
199 return;
200 }
201 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700202 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800203 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700204}
205
Jason Sams40a29e82009-08-10 14:55:26 -0700206void Allocation::read(void *data)
207{
208 memcpy(data, mPtr, mType->getSizeBytes());
209}
210
Jason Sams07ae4062009-08-27 20:23:34 -0700211void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700212{
213 uint32_t eSize = mType->getElementSizeBytes();
214 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
215 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700216 uint32_t size = count * eSize;
217
218 if (size != sizeBytes) {
219 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700220 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700221 return;
222 }
223 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700224 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800225 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700226}
227
Jason Samse2ae85f2009-06-03 16:04:54 -0700228void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700229 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700230{
231 uint32_t eSize = mType->getElementSizeBytes();
232 uint32_t lineSize = eSize * w;
233 uint32_t destW = mType->getDimX();
234
235 const uint8_t *src = static_cast<const uint8_t *>(data);
236 uint8_t *dst = static_cast<uint8_t *>(mPtr);
237 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700238
239 if ((lineSize * eSize * h) != sizeBytes) {
240 rsAssert(!"Allocation::subData called with mismatched size");
241 return;
242 }
243
Jason Samsd19f10d2009-05-22 14:03:28 -0700244 for (uint32_t line=yoff; line < (yoff+h); line++) {
245 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
246 memcpy(dst, src, lineSize);
247 src += lineSize;
248 dst += destW * eSize;
249 }
Jason Sams83f1c632009-10-26 15:19:28 -0700250 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800251 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700252}
253
254void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700255 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700256{
257}
258
Jason Sams83f1c632009-10-26 15:19:28 -0700259void Allocation::addProgramToDirty(const Program *p)
260{
261 mToDirtyList.add(p);
262}
Jason Samsd19f10d2009-05-22 14:03:28 -0700263
Jason Sams83f1c632009-10-26 15:19:28 -0700264void Allocation::removeProgramToDirty(const Program *p)
265{
266 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
267 if (mToDirtyList[ct] == p) {
268 mToDirtyList.removeAt(ct);
269 return;
270 }
271 }
272 rsAssert(0);
273}
274
Jason Sams715333b2009-11-17 17:26:46 -0800275void Allocation::dumpLOGV(const char *prefix) const
276{
277 ObjectBase::dumpLOGV(prefix);
278
279 String8 s(prefix);
280 s.append(" type ");
281 if (mType.get()) {
282 mType->dumpLOGV(s.string());
283 }
284
285 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
286 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
287
Jason Sams361765362009-11-23 15:27:33 -0800288 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Sams715333b2009-11-17 17:26:46 -0800289 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
290
Jason Sams715333b2009-11-17 17:26:46 -0800291}
Jason Samsd19f10d2009-05-22 14:03:28 -0700292
Jason Sams83f1c632009-10-26 15:19:28 -0700293void Allocation::sendDirty() const
294{
295 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
296 mToDirtyList[ct]->forceDirty();
297 }
298}
Jason Samsd19f10d2009-05-22 14:03:28 -0700299
300/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700301//
Jason Samsd19f10d2009-05-22 14:03:28 -0700302
303
304namespace android {
305namespace renderscript {
306
307RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
308{
309 const Type * type = static_cast<const Type *>(vtype);
310
Jason Samsa9e7a052009-09-25 14:51:22 -0700311 Allocation * alloc = new Allocation(rsc, type);
Jason Sams07ae4062009-08-27 20:23:34 -0700312 alloc->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700313 return alloc;
314}
315
Jason Samsd19f10d2009-05-22 14:03:28 -0700316RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
317{
Jason Samsa9e7a052009-09-25 14:51:22 -0700318 Type * type = new Type(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700319 type->setDimX(count);
320 type->setElement(static_cast<Element *>(e));
321 type->compute();
322 return rsi_AllocationCreateTyped(rsc, type);
323}
324
Jason Samsc2908e62010-02-23 17:44:28 -0800325void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Samsd19f10d2009-05-22 14:03:28 -0700326{
327 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsc2908e62010-02-23 17:44:28 -0800328 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700329}
330
331void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
332{
333 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800334 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700335}
336
Jason Samse2ae85f2009-06-03 16:04:54 -0700337static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700338{
339 uint32_t w = out.getDimX();
340 uint32_t h = out.getDimY();
341
Jason Sams6f5c61c2009-07-28 17:20:11 -0700342 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700343 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
344 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
345 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
346
Jason Sams6f5c61c2009-07-28 17:20:11 -0700347 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700348 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
349 oPtr ++;
350 i1 += 2;
351 i2 += 2;
352 }
353 }
354}
355
356static void mip8888(const Adapter2D &out, const Adapter2D &in)
357{
358 uint32_t w = out.getDimX();
359 uint32_t h = out.getDimY();
360
Jason Sams6f5c61c2009-07-28 17:20:11 -0700361 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700362 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
363 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
364 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
365
Jason Sams6f5c61c2009-07-28 17:20:11 -0700366 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700367 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700368 oPtr ++;
369 i1 += 2;
370 i2 += 2;
371 }
372 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700373}
374
Jason Samse20e3b42010-01-19 17:53:54 -0800375static void mip8(const Adapter2D &out, const Adapter2D &in)
376{
377 uint32_t w = out.getDimX();
378 uint32_t h = out.getDimY();
379
380 for (uint32_t y=0; y < h; y++) {
381 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
382 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
383 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
384
385 for (uint32_t x=0; x < w; x++) {
386 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
387 oPtr ++;
388 i1 += 2;
389 i2 += 2;
390 }
391 }
392}
393
Jason Sams6f5c61c2009-07-28 17:20:11 -0700394static void mip(const Adapter2D &out, const Adapter2D &in)
395{
396 switch(out.getBaseType()->getElement()->getSizeBits()) {
397 case 32:
398 mip8888(out, in);
399 break;
400 case 16:
401 mip565(out, in);
402 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800403 case 8:
404 mip8(out, in);
405 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700406
407 }
408
409}
Jason Samsd19f10d2009-05-22 14:03:28 -0700410
Jason Samsfe08d992009-05-27 14:45:32 -0700411typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
412
413static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
414{
415 memcpy(dst, src, count * 2);
416}
417static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
418{
419 memcpy(dst, src, count);
420}
421static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
422{
423 memcpy(dst, src, count * 4);
424}
425
426
427static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
428{
429 uint16_t *d = static_cast<uint16_t *>(dst);
430 const uint8_t *s = static_cast<const uint8_t *>(src);
431
432 while(count--) {
433 *d = rs888to565(s[0], s[1], s[2]);
434 d++;
435 s+= 3;
436 }
437}
438
439static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
440{
441 uint16_t *d = static_cast<uint16_t *>(dst);
442 const uint8_t *s = static_cast<const uint8_t *>(src);
443
444 while(count--) {
445 *d = rs888to565(s[0], s[1], s[2]);
446 d++;
447 s+= 4;
448 }
449}
450
Jason Samsea84a7c2009-09-04 14:42:41 -0700451static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Samsfe08d992009-05-27 14:45:32 -0700452{
Jason Sams718cd1f2009-12-23 14:35:29 -0800453 GLenum srcGLType = src->getComponent().getGLType();
454 GLenum srcGLFmt = src->getComponent().getGLFormat();
455 GLenum dstGLType = dst->getComponent().getGLType();
456 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsea84a7c2009-09-04 14:42:41 -0700457
458 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
459 switch(dst->getSizeBytes()) {
460 case 4:
461 return elementConverter_cpy_32;
462 case 2:
463 return elementConverter_cpy_16;
464 case 1:
465 return elementConverter_cpy_8;
466 }
Jason Samsfe08d992009-05-27 14:45:32 -0700467 }
468
Jason Samsea84a7c2009-09-04 14:42:41 -0700469 if (srcGLType == GL_UNSIGNED_BYTE &&
470 srcGLFmt == GL_RGB &&
471 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
472 dstGLType == GL_RGB) {
473
Jason Samsfe08d992009-05-27 14:45:32 -0700474 return elementConverter_888_to_565;
475 }
476
Jason Samsea84a7c2009-09-04 14:42:41 -0700477 if (srcGLType == GL_UNSIGNED_BYTE &&
478 srcGLFmt == GL_RGBA &&
479 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
480 dstGLType == GL_RGB) {
481
Jason Samsfe08d992009-05-27 14:45:32 -0700482 return elementConverter_8888_to_565;
483 }
484
Jason Samsea84a7c2009-09-04 14:42:41 -0700485 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700486 return 0;
487}
488
489
Jason Samsea84a7c2009-09-04 14:42:41 -0700490RsAllocation 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 -0700491{
Jason Samsea84a7c2009-09-04 14:42:41 -0700492 const Element *src = static_cast<const Element *>(_src);
493 const Element *dst = static_cast<const Element *>(_dst);
Jason Sams74e02ef2010-01-06 15:10:29 -0800494
495 // Check for pow2 on pre es 2.0 versions.
496 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
Jason Samsb0ec1b42009-07-28 12:02:16 -0700497
498 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsea84a7c2009-09-04 14:42:41 -0700499 rsi_TypeBegin(rsc, _dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700500 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
501 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
502 if (genMips) {
503 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
504 }
505 RsType type = rsi_TypeCreate(rsc);
506
507 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
508 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
509 if (texAlloc == NULL) {
510 LOGE("Memory allocation failure");
511 return NULL;
512 }
Jason Samsfe08d992009-05-27 14:45:32 -0700513
Jason Samsea84a7c2009-09-04 14:42:41 -0700514 ElementConverter_t cvt = pickConverter(dst, src);
Jason Samsfe08d992009-05-27 14:45:32 -0700515 cvt(texAlloc->getPtr(), data, w * h);
516
517 if (genMips) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700518 Adapter2D adapt(rsc, texAlloc);
519 Adapter2D adapt2(rsc, texAlloc);
Jason Samsfe08d992009-05-27 14:45:32 -0700520 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
521 adapt.setLOD(lod);
522 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700523 mip(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700524 }
525 }
526
527 return texAlloc;
528}
529
Jason Samsea84a7c2009-09-04 14:42:41 -0700530RsAllocation 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 -0700531{
Jason Samsea84a7c2009-09-04 14:42:41 -0700532 const Element *srcE = static_cast<const Element *>(_src);
533 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700534 uint32_t w2 = rsHigherPow2(w);
535 uint32_t h2 = rsHigherPow2(h);
536
537 if ((w2 == w) && (h2 == h)) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700538 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700539 }
540
Jason Samsea84a7c2009-09-04 14:42:41 -0700541 uint32_t bpp = srcE->getSizeBytes();
Jason Samsb0ec1b42009-07-28 12:02:16 -0700542 size_t size = w2 * h2 * bpp;
543 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
544 memset(tmp, 0, size);
545
546 const uint8_t * src = static_cast<const uint8_t *>(data);
547 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700548 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen911458a2009-10-28 15:10:56 -0700549 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700550 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700551 }
552
Jason Samsea84a7c2009-09-04 14:42:41 -0700553 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700554 free(tmp);
555 return ret;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700556}
557
Jason Sams07ae4062009-08-27 20:23:34 -0700558void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700559{
560 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700561 a->data(data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700562}
563
Jason Sams07ae4062009-08-27 20:23:34 -0700564void 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 -0700565{
566 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700567 a->subData(xoff, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700568}
569
Jason Sams07ae4062009-08-27 20:23:34 -0700570void 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 -0700571{
572 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700573 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700574}
575
Jason Sams40a29e82009-08-10 14:55:26 -0700576void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
577{
578 Allocation *a = static_cast<Allocation *>(va);
579 a->read(data);
580}
581
Jason Samsd19f10d2009-05-22 14:03:28 -0700582
583}
584}