blob: 0356e4d377f67c6ab8eaf10d84a53f268829e797 [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 */
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -070017#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>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070022#else
23#include "rsContextHostStub.h"
24
25#include <OpenGL/gl.h>
26#include <OpenGl/glext.h>
27#endif
Jason Sams4b962e52009-06-22 17:15:15 -070028
Jason Samsd19f10d2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
Jason Samsa9e7a052009-09-25 14:51:22 -070032Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070033{
Jason Sams8a64743f2010-03-01 15:31:04 -080034 init(rsc, type);
35
36 mPtr = malloc(mType->getSizeBytes());
Jason Samsee734982010-08-12 12:44:02 -070037 if (mType->getElement()->getHasReferences()) {
38 memset(mPtr, 0, mType->getSizeBytes());
39 }
Jason Sams8a64743f2010-03-01 15:31:04 -080040 if (!mPtr) {
41 LOGE("Allocation::Allocation, alloc failure");
42 }
43}
44
45Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
46 void *callbackData, RsBitmapCallback_t callback)
47: ObjectBase(rsc)
48{
49 init(rsc, type);
50
51 mPtr = bmp;
52 mUserBitmapCallback = callback;
53 mUserBitmapCallbackData = callbackData;
54}
55
56void Allocation::init(Context *rsc, const Type *type)
57{
Jason Sams61f08d62009-09-25 16:37:33 -070058 mAllocFile = __FILE__;
59 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070060 mPtr = NULL;
61
62 mCpuWrite = false;
63 mCpuRead = false;
64 mGpuWrite = false;
65 mGpuRead = false;
66
67 mReadWriteRatio = 0;
68 mUpdateSize = 0;
69
70 mIsTexture = false;
71 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070072 mIsVertexBuffer = false;
73 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080074 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070075
Jason Sams8a64743f2010-03-01 15:31:04 -080076 mUserBitmapCallback = NULL;
77 mUserBitmapCallbackData = NULL;
78
Jason Samsd19f10d2009-05-22 14:03:28 -070079 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070080 rsAssert(type);
Jason Sams8a64743f2010-03-01 15:31:04 -080081
82 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070083}
84
85Allocation::~Allocation()
86{
Jason Sams8a64743f2010-03-01 15:31:04 -080087 if (mUserBitmapCallback != NULL) {
88 mUserBitmapCallback(mUserBitmapCallbackData);
89 } else {
90 free(mPtr);
91 }
Jason Samsb7a6c432009-11-02 14:25:10 -080092 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080093
94 if (mBufferID) {
95 // Causes a SW crash....
96 //LOGV(" mBufferID %i", mBufferID);
97 //glDeleteBuffers(1, &mBufferID);
98 //mBufferID = 0;
99 }
100 if (mTextureID) {
101 glDeleteTextures(1, &mTextureID);
102 mTextureID = 0;
103 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700104}
105
106void Allocation::setCpuWritable(bool)
107{
108}
109
110void Allocation::setGpuWritable(bool)
111{
112}
113
114void Allocation::setCpuReadable(bool)
115{
116}
117
118void Allocation::setGpuReadable(bool)
119{
120}
121
122bool Allocation::fixAllocation()
123{
124 return false;
125}
126
Jason Samsc2908e62010-02-23 17:44:28 -0800127void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Sams3b7d39b2009-12-14 12:57:40 -0800128{
129 rsAssert(lodOffset < mType->getLODCount());
130 mIsTexture = true;
131 mTextureLOD = lodOffset;
132 mUploadDefered = true;
Jason Samsc2908e62010-02-23 17:44:28 -0800133 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800134}
135
136void Allocation::uploadToTexture(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700137{
138 //rsAssert(!mTextureId);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800139
140 mIsTexture = true;
141 if (!rsc->checkDriver()) {
142 mUploadDefered = true;
143 return;
144 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700145
Jason Sams718cd1f2009-12-23 14:35:29 -0800146 GLenum type = mType->getElement()->getComponent().getGLType();
147 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700148
149 if (!type || !format) {
150 return;
151 }
152
Jason Samsd19f10d2009-05-22 14:03:28 -0700153 if (!mTextureID) {
154 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800155
156 if (!mTextureID) {
157 // This should not happen, however, its likely the cause of the
158 // white sqare bug.
159 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
160 LOGE("Upload to texture failed to gen mTextureID");
161 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800162 mUploadDefered = true;
163 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800164 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700165 }
166 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800167 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700168
Jason Samsa9e7a052009-09-25 14:51:22 -0700169 Adapter2D adapt(getContext(), this);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800170 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
171 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700172
173 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -0700174 glTexImage2D(GL_TEXTURE_2D, lod, format,
175 adapt.getDimX(), adapt.getDimY(),
176 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700177 }
Jason Samsc2908e62010-02-23 17:44:28 -0800178 if (mTextureGenMipmap) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700179#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsc2908e62010-02-23 17:44:28 -0800180 glGenerateMipmap(GL_TEXTURE_2D);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700181#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsc2908e62010-02-23 17:44:28 -0800182 }
183
Jason Samsf4687002010-03-10 17:30:41 -0800184 rsc->checkError("Allocation::uploadToTexture");
Jason Samsd19f10d2009-05-22 14:03:28 -0700185}
186
Jason Sams3b7d39b2009-12-14 12:57:40 -0800187void Allocation::deferedUploadToBufferObject(const Context *rsc)
188{
189 mIsVertexBuffer = true;
190 mUploadDefered = true;
191}
192
193void Allocation::uploadToBufferObject(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700194{
195 rsAssert(!mType->getDimY());
196 rsAssert(!mType->getDimZ());
197
Jason Sams3b7d39b2009-12-14 12:57:40 -0800198 mIsVertexBuffer = true;
199 if (!rsc->checkDriver()) {
200 mUploadDefered = true;
201 return;
202 }
203
Jason Samsd19f10d2009-05-22 14:03:28 -0700204 if (!mBufferID) {
205 glGenBuffers(1, &mBufferID);
206 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800207 if (!mBufferID) {
208 LOGE("Upload to buffer object failed");
209 mUploadDefered = true;
210 return;
211 }
212
Jason Samsd19f10d2009-05-22 14:03:28 -0700213 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
214 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
215 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800216 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700217}
218
Jason Sams3b7d39b2009-12-14 12:57:40 -0800219void Allocation::uploadCheck(const Context *rsc)
220{
221 if (mUploadDefered) {
222 mUploadDefered = false;
223 if (mIsVertexBuffer) {
224 uploadToBufferObject(rsc);
225 }
226 if (mIsTexture) {
227 uploadToTexture(rsc);
228 }
229 }
230}
231
Jason Sams1bada8c2009-08-09 17:01:55 -0700232
Jason Sams49bdaf02010-08-31 13:50:42 -0700233void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700234{
Jason Sams07ae4062009-08-27 20:23:34 -0700235 uint32_t size = mType->getSizeBytes();
236 if (size != sizeBytes) {
237 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
238 return;
239 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700240
241 if (mType->getElement()->getHasReferences()) {
242 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
243 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
244 }
245
Jason Sams07ae4062009-08-27 20:23:34 -0700246 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700247 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800248 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700249}
250
Jason Sams40a29e82009-08-10 14:55:26 -0700251void Allocation::read(void *data)
252{
253 memcpy(data, mPtr, mType->getSizeBytes());
254}
255
Jason Sams49bdaf02010-08-31 13:50:42 -0700256void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700257{
258 uint32_t eSize = mType->getElementSizeBytes();
259 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
260 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700261 uint32_t size = count * eSize;
262
263 if (size != sizeBytes) {
264 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700265 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700266 return;
267 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700268
269 if (mType->getElement()->getHasReferences()) {
270 incRefs(data, count);
271 decRefs(ptr, count);
272 }
273
Jason Sams07ae4062009-08-27 20:23:34 -0700274 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700275 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800276 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700277}
278
Jason Sams49bdaf02010-08-31 13:50:42 -0700279void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700280 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700281{
282 uint32_t eSize = mType->getElementSizeBytes();
283 uint32_t lineSize = eSize * w;
284 uint32_t destW = mType->getDimX();
285
286 const uint8_t *src = static_cast<const uint8_t *>(data);
287 uint8_t *dst = static_cast<uint8_t *>(mPtr);
288 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700289
290 if ((lineSize * eSize * h) != sizeBytes) {
291 rsAssert(!"Allocation::subData called with mismatched size");
292 return;
293 }
294
Jason Samsd19f10d2009-05-22 14:03:28 -0700295 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700296 if (mType->getElement()->getHasReferences()) {
297 incRefs(src, w);
298 decRefs(dst, w);
299 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700300 memcpy(dst, src, lineSize);
301 src += lineSize;
302 dst += destW * eSize;
303 }
Jason Sams83f1c632009-10-26 15:19:28 -0700304 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800305 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700306}
307
Jason Sams49bdaf02010-08-31 13:50:42 -0700308void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700309 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700310{
311}
312
Jason Sams49bdaf02010-08-31 13:50:42 -0700313void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
314 uint32_t cIdx, uint32_t sizeBytes)
315{
316 uint32_t eSize = mType->getElementSizeBytes();
317 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
318 ptr += eSize * x;
319
320 if (cIdx >= mType->getElement()->getFieldCount()) {
321 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
322 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
323 return;
324 }
325
326 if (x >= mType->getDimX()) {
327 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
328 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
329 return;
330 }
331
332 const Element * e = mType->getElement()->getField(cIdx);
333 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
334
335 if (sizeBytes != e->getSizeBytes()) {
336 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
337 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
338 return;
339 }
340
341 if (e->getHasReferences()) {
342 e->incRefs(data);
343 e->decRefs(ptr);
344 }
345
346 memcpy(ptr, data, sizeBytes);
347 sendDirty();
348 mUploadDefered = true;
349}
350
351void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
352 const void *data, uint32_t cIdx, uint32_t sizeBytes)
353{
354 uint32_t eSize = mType->getElementSizeBytes();
355 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
356 ptr += eSize * (x + y * mType->getDimX());
357
358 if (x >= mType->getDimX()) {
359 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
360 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
361 return;
362 }
363
364 if (y >= mType->getDimY()) {
365 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
366 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
367 return;
368 }
369
370 if (cIdx >= mType->getElement()->getFieldCount()) {
371 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
372 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
373 return;
374 }
375
376 const Element * e = mType->getElement()->getField(cIdx);
377 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
378
379 if (sizeBytes != e->getSizeBytes()) {
380 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
381 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
382 return;
383 }
384
385 if (e->getHasReferences()) {
386 e->incRefs(data);
387 e->decRefs(ptr);
388 }
389
390 memcpy(ptr, data, sizeBytes);
391 sendDirty();
392 mUploadDefered = true;
393}
394
Jason Sams83f1c632009-10-26 15:19:28 -0700395void Allocation::addProgramToDirty(const Program *p)
396{
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700397 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700398}
Jason Samsd19f10d2009-05-22 14:03:28 -0700399
Jason Sams83f1c632009-10-26 15:19:28 -0700400void Allocation::removeProgramToDirty(const Program *p)
401{
402 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
403 if (mToDirtyList[ct] == p) {
404 mToDirtyList.removeAt(ct);
405 return;
406 }
407 }
408 rsAssert(0);
409}
410
Jason Sams715333b2009-11-17 17:26:46 -0800411void Allocation::dumpLOGV(const char *prefix) const
412{
413 ObjectBase::dumpLOGV(prefix);
414
415 String8 s(prefix);
416 s.append(" type ");
417 if (mType.get()) {
418 mType->dumpLOGV(s.string());
419 }
420
421 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
422 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
423
Jason Sams361765362009-11-23 15:27:33 -0800424 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Sams715333b2009-11-17 17:26:46 -0800425 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
426
Jason Sams715333b2009-11-17 17:26:46 -0800427}
Jason Samsd19f10d2009-05-22 14:03:28 -0700428
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700429void Allocation::serialize(OStream *stream) const
430{
431 // Need to identify ourselves
432 stream->addU32((uint32_t)getClassId());
433
434 String8 name(getName());
435 stream->addString(&name);
436
437 // First thing we need to serialize is the type object since it will be needed
438 // to initialize the class
439 mType->serialize(stream);
440
441 uint32_t dataSize = mType->getSizeBytes();
442 // Write how much data we are storing
443 stream->addU32(dataSize);
444 // Now write the data
445 stream->addByteArray(mPtr, dataSize);
446}
447
448Allocation *Allocation::createFromStream(Context *rsc, IStream *stream)
449{
450 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700451 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
452 if(classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700453 LOGE("allocation loading skipped due to invalid class id\n");
454 return NULL;
455 }
456
457 String8 name;
458 stream->loadString(&name);
459
460 Type *type = Type::createFromStream(rsc, stream);
461 if(!type) {
462 return NULL;
463 }
464 type->compute();
465
466 // Number of bytes we wrote out for this allocation
467 uint32_t dataSize = stream->loadU32();
468 if(dataSize != type->getSizeBytes()) {
469 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
470 delete type;
471 return NULL;
472 }
473
474 Allocation *alloc = new Allocation(rsc, type);
475 alloc->setName(name.string(), name.size());
476
477 // Read in all of our allocation data
Jason Sams49bdaf02010-08-31 13:50:42 -0700478 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700479 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700480
481 return alloc;
482}
483
Jason Sams83f1c632009-10-26 15:19:28 -0700484void Allocation::sendDirty() const
485{
486 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
487 mToDirtyList[ct]->forceDirty();
488 }
489}
Jason Samsd19f10d2009-05-22 14:03:28 -0700490
Jason Samsb28ca96f2010-08-09 18:13:33 -0700491void Allocation::incRefs(const void *ptr, size_t ct) const
492{
493 const uint8_t *p = static_cast<const uint8_t *>(ptr);
494 const Element *e = mType->getElement();
495 uint32_t stride = e->getSizeBytes();
496
497 while (ct > 0) {
498 e->incRefs(p);
499 ct --;
500 p += stride;
501 }
502}
503
504void Allocation::decRefs(const void *ptr, size_t ct) const
505{
506 const uint8_t *p = static_cast<const uint8_t *>(ptr);
507 const Element *e = mType->getElement();
508 uint32_t stride = e->getSizeBytes();
509
510 while (ct > 0) {
511 e->decRefs(p);
512 ct --;
513 p += stride;
514 }
515}
516
Jason Samsd19f10d2009-05-22 14:03:28 -0700517/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700518//
Jason Samsd19f10d2009-05-22 14:03:28 -0700519
520
521namespace android {
522namespace renderscript {
523
524RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
525{
526 const Type * type = static_cast<const Type *>(vtype);
527
Jason Samsa9e7a052009-09-25 14:51:22 -0700528 Allocation * alloc = new Allocation(rsc, type);
Jason Sams07ae4062009-08-27 20:23:34 -0700529 alloc->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700530 return alloc;
531}
532
Jason Samsd19f10d2009-05-22 14:03:28 -0700533RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
534{
Jason Samsa9e7a052009-09-25 14:51:22 -0700535 Type * type = new Type(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700536 type->setDimX(count);
537 type->setElement(static_cast<Element *>(e));
538 type->compute();
539 return rsi_AllocationCreateTyped(rsc, type);
540}
541
Jason Samsc2908e62010-02-23 17:44:28 -0800542void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Samsd19f10d2009-05-22 14:03:28 -0700543{
544 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsc2908e62010-02-23 17:44:28 -0800545 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700546}
547
548void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
549{
550 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800551 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700552}
553
Jason Samse2ae85f2009-06-03 16:04:54 -0700554static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700555{
556 uint32_t w = out.getDimX();
557 uint32_t h = out.getDimY();
558
Jason Sams6f5c61c2009-07-28 17:20:11 -0700559 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700560 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
561 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
562 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
563
Jason Sams6f5c61c2009-07-28 17:20:11 -0700564 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700565 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
566 oPtr ++;
567 i1 += 2;
568 i2 += 2;
569 }
570 }
571}
572
573static void mip8888(const Adapter2D &out, const Adapter2D &in)
574{
575 uint32_t w = out.getDimX();
576 uint32_t h = out.getDimY();
577
Jason Sams6f5c61c2009-07-28 17:20:11 -0700578 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700579 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
580 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
581 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
582
Jason Sams6f5c61c2009-07-28 17:20:11 -0700583 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700584 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700585 oPtr ++;
586 i1 += 2;
587 i2 += 2;
588 }
589 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700590}
591
Jason Samse20e3b42010-01-19 17:53:54 -0800592static void mip8(const Adapter2D &out, const Adapter2D &in)
593{
594 uint32_t w = out.getDimX();
595 uint32_t h = out.getDimY();
596
597 for (uint32_t y=0; y < h; y++) {
598 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
599 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
600 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
601
602 for (uint32_t x=0; x < w; x++) {
603 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
604 oPtr ++;
605 i1 += 2;
606 i2 += 2;
607 }
608 }
609}
610
Jason Sams6f5c61c2009-07-28 17:20:11 -0700611static void mip(const Adapter2D &out, const Adapter2D &in)
612{
613 switch(out.getBaseType()->getElement()->getSizeBits()) {
614 case 32:
615 mip8888(out, in);
616 break;
617 case 16:
618 mip565(out, in);
619 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800620 case 8:
621 mip8(out, in);
622 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700623
624 }
625
626}
Jason Samsd19f10d2009-05-22 14:03:28 -0700627
Jason Samsfe08d992009-05-27 14:45:32 -0700628typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
629
630static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
631{
632 memcpy(dst, src, count * 2);
633}
634static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
635{
636 memcpy(dst, src, count);
637}
638static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
639{
640 memcpy(dst, src, count * 4);
641}
642
643
644static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
645{
646 uint16_t *d = static_cast<uint16_t *>(dst);
647 const uint8_t *s = static_cast<const uint8_t *>(src);
648
649 while(count--) {
650 *d = rs888to565(s[0], s[1], s[2]);
651 d++;
652 s+= 3;
653 }
654}
655
656static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
657{
658 uint16_t *d = static_cast<uint16_t *>(dst);
659 const uint8_t *s = static_cast<const uint8_t *>(src);
660
661 while(count--) {
662 *d = rs888to565(s[0], s[1], s[2]);
663 d++;
664 s+= 4;
665 }
666}
667
Jason Samsea84a7c2009-09-04 14:42:41 -0700668static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Samsfe08d992009-05-27 14:45:32 -0700669{
Jason Sams718cd1f2009-12-23 14:35:29 -0800670 GLenum srcGLType = src->getComponent().getGLType();
671 GLenum srcGLFmt = src->getComponent().getGLFormat();
672 GLenum dstGLType = dst->getComponent().getGLType();
673 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsea84a7c2009-09-04 14:42:41 -0700674
675 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
676 switch(dst->getSizeBytes()) {
677 case 4:
678 return elementConverter_cpy_32;
679 case 2:
680 return elementConverter_cpy_16;
681 case 1:
682 return elementConverter_cpy_8;
683 }
Jason Samsfe08d992009-05-27 14:45:32 -0700684 }
685
Jason Samsea84a7c2009-09-04 14:42:41 -0700686 if (srcGLType == GL_UNSIGNED_BYTE &&
687 srcGLFmt == GL_RGB &&
688 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams0ebd5692010-06-22 17:45:34 -0700689 dstGLFmt == GL_RGB) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700690
Jason Samsfe08d992009-05-27 14:45:32 -0700691 return elementConverter_888_to_565;
692 }
693
Jason Samsea84a7c2009-09-04 14:42:41 -0700694 if (srcGLType == GL_UNSIGNED_BYTE &&
695 srcGLFmt == GL_RGBA &&
696 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams0ebd5692010-06-22 17:45:34 -0700697 dstGLFmt == GL_RGB) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700698
Jason Samsfe08d992009-05-27 14:45:32 -0700699 return elementConverter_8888_to_565;
700 }
701
Jason Samsea84a7c2009-09-04 14:42:41 -0700702 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams0ebd5692010-06-22 17:45:34 -0700703 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt);
704 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt);
705 src->dumpLOGV("SRC ");
706 dst->dumpLOGV("DST ");
Jason Samsfe08d992009-05-27 14:45:32 -0700707 return 0;
708}
709
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700710#ifndef ANDROID_RS_BUILD_FOR_HOST
711
Jason Sams8a64743f2010-03-01 15:31:04 -0800712RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
713 void *bmp, void *callbackData, RsBitmapCallback_t callback)
714{
715 const Type * type = static_cast<const Type *>(vtype);
716 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
717 alloc->incUserRef();
718 return alloc;
719}
Jason Samsfe08d992009-05-27 14:45:32 -0700720
Jason Samsea84a7c2009-09-04 14:42:41 -0700721RsAllocation 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 -0700722{
Jason Samsea84a7c2009-09-04 14:42:41 -0700723 const Element *src = static_cast<const Element *>(_src);
724 const Element *dst = static_cast<const Element *>(_dst);
Jason Sams74e02ef2010-01-06 15:10:29 -0800725
Jason Samsd081fff2010-09-16 18:18:29 -0700726 //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips);
Jason Samsea84a7c2009-09-04 14:42:41 -0700727 rsi_TypeBegin(rsc, _dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700728 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
729 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
730 if (genMips) {
731 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
732 }
733 RsType type = rsi_TypeCreate(rsc);
734
735 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
736 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
737 if (texAlloc == NULL) {
738 LOGE("Memory allocation failure");
739 return NULL;
740 }
Jason Samsfe08d992009-05-27 14:45:32 -0700741
Jason Samsea84a7c2009-09-04 14:42:41 -0700742 ElementConverter_t cvt = pickConverter(dst, src);
Jason Sams49bdaf02010-08-31 13:50:42 -0700743 if (cvt) {
744 cvt(texAlloc->getPtr(), data, w * h);
745 if (genMips) {
746 Adapter2D adapt(rsc, texAlloc);
747 Adapter2D adapt2(rsc, texAlloc);
748 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
749 adapt.setLOD(lod);
750 adapt2.setLOD(lod + 1);
751 mip(adapt2, adapt);
752 }
Jason Samsfe08d992009-05-27 14:45:32 -0700753 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700754 } else {
755 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
Jason Samsfe08d992009-05-27 14:45:32 -0700756 }
757
758 return texAlloc;
759}
760
Jason Samsea84a7c2009-09-04 14:42:41 -0700761RsAllocation 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 -0700762{
Jason Samsea84a7c2009-09-04 14:42:41 -0700763 const Element *srcE = static_cast<const Element *>(_src);
764 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700765 uint32_t w2 = rsHigherPow2(w);
766 uint32_t h2 = rsHigherPow2(h);
767
768 if ((w2 == w) && (h2 == h)) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700769 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700770 }
771
Jason Samsea84a7c2009-09-04 14:42:41 -0700772 uint32_t bpp = srcE->getSizeBytes();
Jason Samsb0ec1b42009-07-28 12:02:16 -0700773 size_t size = w2 * h2 * bpp;
774 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
775 memset(tmp, 0, size);
776
777 const uint8_t * src = static_cast<const uint8_t *>(data);
778 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700779 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen911458a2009-10-28 15:10:56 -0700780 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700781 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700782 }
783
Jason Samsea84a7c2009-09-04 14:42:41 -0700784 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700785 free(tmp);
786 return ret;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700787}
788
Jason Sams07ae4062009-08-27 20:23:34 -0700789void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700790{
791 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700792 a->data(rsc, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700793}
794
Jason Sams07ae4062009-08-27 20:23:34 -0700795void 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 -0700796{
797 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700798 a->subData(rsc, xoff, count, data, sizeBytes);
799}
800
801void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes)
802{
803 Allocation *a = static_cast<Allocation *>(va);
804 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
805}
806
807void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes)
808{
809 Allocation *a = static_cast<Allocation *>(va);
810 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700811}
812
Jason Sams07ae4062009-08-27 20:23:34 -0700813void 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 -0700814{
815 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700816 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700817}
818
Jason Sams40a29e82009-08-10 14:55:26 -0700819void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
820{
821 Allocation *a = static_cast<Allocation *>(va);
822 a->read(data);
823}
824
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700825const void* rsi_AllocationGetType(Context *rsc, RsAllocation va)
826{
827 Allocation *a = static_cast<Allocation *>(va);
828 a->getType()->incUserRef();
829
830 return a->getType();
831}
832
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700833#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -0700834
835}
836}