1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_RS_BUILD_FOR_HOST
#include "rsContext.h"
#include <GLES/gl.h>
#include <GLES2/gl2.h>
#include <GLES/glext.h>
#else
#include "rsContextHostStub.h"
#include <OpenGL/gl.h>
#include <OpenGl/glext.h>
#endif
#include "utils/StopWatch.h"
using namespace android;
using namespace android::renderscript;
Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages) : ObjectBase(rsc) {
init(rsc, type);
mUsageFlags = usages;
allocScriptMemory();
if (mType->getElement()->getHasReferences()) {
memset(mPtr, 0, mType->getSizeBytes());
}
if (!mPtr) {
LOGE("Allocation::Allocation, alloc failure");
}
}
void Allocation::init(Context *rsc, const Type *type) {
mPtr = NULL;
mCpuWrite = false;
mCpuRead = false;
mGpuWrite = false;
mGpuRead = false;
mReadWriteRatio = 0;
mUpdateSize = 0;
mUsageFlags = 0;
mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
mTextureID = 0;
mBufferID = 0;
mUploadDefered = false;
mUserBitmapCallback = NULL;
mUserBitmapCallbackData = NULL;
mType.set(type);
rsAssert(type);
mPtr = NULL;
}
Allocation::~Allocation() {
if (mUserBitmapCallback != NULL) {
mUserBitmapCallback(mUserBitmapCallbackData);
mPtr = NULL;
}
freeScriptMemory();
if (mBufferID) {
// Causes a SW crash....
//LOGV(" mBufferID %i", mBufferID);
//glDeleteBuffers(1, &mBufferID);
//mBufferID = 0;
}
if (mTextureID) {
glDeleteTextures(1, &mTextureID);
mTextureID = 0;
}
}
void Allocation::setCpuWritable(bool) {
}
void Allocation::setGpuWritable(bool) {
}
void Allocation::setCpuReadable(bool) {
}
void Allocation::setGpuReadable(bool) {
}
bool Allocation::fixAllocation() {
return false;
}
void Allocation::deferedUploadToTexture(const Context *rsc) {
mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
mUploadDefered = true;
}
uint32_t Allocation::getGLTarget() const {
if (getIsTexture()) {
if (mType->getDimFaces()) {
return GL_TEXTURE_CUBE_MAP;
} else {
return GL_TEXTURE_2D;
}
}
if (getIsBufferObject()) {
return GL_ARRAY_BUFFER;
}
return 0;
}
void Allocation::allocScriptMemory() {
rsAssert(!mPtr);
mPtr = malloc(mType->getSizeBytes());
}
void Allocation::freeScriptMemory() {
rsAssert(!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT));
if (mPtr) {
free(mPtr);
mPtr = NULL;
}
}
void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
if (getIsTexture()) {
uploadToTexture(rsc);
}
if (getIsBufferObject()) {
uploadToBufferObject(rsc);
}
mUploadDefered = false;
}
void Allocation::uploadToTexture(const Context *rsc) {
mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
GLenum type = mType->getElement()->getComponent().getGLType();
GLenum format = mType->getElement()->getComponent().getGLFormat();
if (!type || !format) {
return;
}
if (!mPtr) {
return;
}
bool isFirstUpload = false;
if (!mTextureID) {
glGenTextures(1, &mTextureID);
if (!mTextureID) {
// This should not happen, however, its likely the cause of the
// white sqare bug.
// Force a crash to 1: restart the app, 2: make sure we get a bugreport.
LOGE("Upload to texture failed to gen mTextureID");
rsc->dumpDebug();
mUploadDefered = true;
return;
}
isFirstUpload = true;
}
GLenum target = (GLenum)getGLTarget();
if (target == GL_TEXTURE_2D) {
upload2DTexture(isFirstUpload, mPtr);
} else if (target == GL_TEXTURE_CUBE_MAP) {
uploadCubeTexture(isFirstUpload);
}
if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
#ifndef ANDROID_RS_BUILD_FOR_HOST
glGenerateMipmap(target);
#endif //ANDROID_RS_BUILD_FOR_HOST
}
if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
freeScriptMemory();
}
rsc->checkError("Allocation::uploadToTexture");
}
void Allocation::upload2DTexture(bool isFirstUpload, const void *ptr) {
GLenum type = mType->getElement()->getComponent().getGLType();
GLenum format = mType->getElement()->getComponent().getGLFormat();
GLenum target = (GLenum)getGLTarget();
glBindTexture(target, mTextureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
const uint8_t *p = (const uint8_t *)ptr;
p += mType->getLODOffset(lod);
if (isFirstUpload) {
glTexImage2D(GL_TEXTURE_2D, lod, format,
mType->getLODDimX(lod), mType->getLODDimY(lod),
0, format, type, p);
} else {
glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
mType->getLODDimX(lod), mType->getLODDimY(lod),
format, type, p);
}
}
}
void Allocation::uploadCubeTexture(bool isFirstUpload) {
GLenum type = mType->getElement()->getComponent().getGLType();
GLenum format = mType->getElement()->getComponent().getGLFormat();
GLenum target = (GLenum)getGLTarget();
glBindTexture(target, mTextureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum faceOrder[] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
Adapter2D adapt(getContext(), this);
for (uint32_t face = 0; face < 6; face ++) {
adapt.setFace(face);
for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
adapt.setLOD(lod);
uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
if (isFirstUpload) {
glTexImage2D(faceOrder[face], lod, format,
adapt.getDimX(), adapt.getDimY(),
0, format, type, ptr);
} else {
glTexSubImage2D(faceOrder[face], lod, 0, 0,
adapt.getDimX(), adapt.getDimY(),
format, type, ptr);
}
}
}
}
void Allocation::deferedUploadToBufferObject(const Context *rsc) {
mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
mUploadDefered = true;
}
void Allocation::uploadToBufferObject(const Context *rsc) {
rsAssert(!mType->getDimY());
rsAssert(!mType->getDimZ());
mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
if (!mBufferID) {
glGenBuffers(1, &mBufferID);
}
if (!mBufferID) {
LOGE("Upload to buffer object failed");
mUploadDefered = true;
return;
}
GLenum target = (GLenum)getGLTarget();
glBindBuffer(target, mBufferID);
glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
glBindBuffer(target, 0);
rsc->checkError("Allocation::uploadToBufferObject");
}
void Allocation::uploadCheck(Context *rsc) {
if (mUploadDefered) {
syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
}
}
void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) {
uint32_t size = mType->getSizeBytes();
if (size != sizeBytes) {
LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
return;
}
if (mType->getElement()->getHasReferences()) {
incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
}
memcpy(mPtr, data, size);
sendDirty();
mUploadDefered = true;
}
void Allocation::read(void *data) {
memcpy(data, mPtr, mType->getSizeBytes());
}
void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
uint32_t eSize = mType->getElementSizeBytes();
uint8_t * ptr = static_cast<uint8_t *>(mPtr);
ptr += eSize * xoff;
uint32_t size = count * eSize;
if (size != sizeBytes) {
LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
mType->dumpLOGV("type info");
return;
}
if (mType->getElement()->getHasReferences()) {
incRefs(data, count);
decRefs(ptr, count);
}
memcpy(ptr, data, size);
sendDirty();
mUploadDefered = true;
}
void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
uint32_t eSize = mType->getElementSizeBytes();
uint32_t lineSize = eSize * w;
uint32_t destW = mType->getDimX();
const uint8_t *src = static_cast<const uint8_t *>(data);
uint8_t *dst = static_cast<uint8_t *>(mPtr);
dst += eSize * (xoff + yoff * destW);
if ((lineSize * eSize * h) != sizeBytes) {
rsAssert(!"Allocation::subData called with mismatched size");
return;
}
for (uint32_t line=yoff; line < (yoff+h); line++) {
if (mType->getElement()->getHasReferences()) {
incRefs(src, w);
decRefs(dst, w);
}
memcpy(dst, src, lineSize);
src += lineSize;
dst += destW * eSize;
}
sendDirty();
mUploadDefered = true;
}
void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
}
void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
uint32_t cIdx, uint32_t sizeBytes) {
uint32_t eSize = mType->getElementSizeBytes();
uint8_t * ptr = static_cast<uint8_t *>(mPtr);
ptr += eSize * x;
if (cIdx >= mType->getElement()->getFieldCount()) {
LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
return;
}
if (x >= mType->getDimX()) {
LOGE("Error Allocation::subElementData X offset %i out of range.", x);
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
return;
}
const Element * e = mType->getElement()->getField(cIdx);
ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
if (sizeBytes != e->getSizeBytes()) {
LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
return;
}
if (e->getHasReferences()) {
e->incRefs(data);
e->decRefs(ptr);
}
memcpy(ptr, data, sizeBytes);
sendDirty();
mUploadDefered = true;
}
void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
const void *data, uint32_t cIdx, uint32_t sizeBytes) {
uint32_t eSize = mType->getElementSizeBytes();
uint8_t * ptr = static_cast<uint8_t *>(mPtr);
ptr += eSize * (x + y * mType->getDimX());
if (x >= mType->getDimX()) {
LOGE("Error Allocation::subElementData X offset %i out of range.", x);
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
return;
}
if (y >= mType->getDimY()) {
LOGE("Error Allocation::subElementData X offset %i out of range.", x);
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
return;
}
if (cIdx >= mType->getElement()->getFieldCount()) {
LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
return;
}
const Element * e = mType->getElement()->getField(cIdx);
ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
if (sizeBytes != e->getSizeBytes()) {
LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
return;
}
if (e->getHasReferences()) {
e->incRefs(data);
e->decRefs(ptr);
}
memcpy(ptr, data, sizeBytes);
sendDirty();
mUploadDefered = true;
}
void Allocation::addProgramToDirty(const Program *p) {
mToDirtyList.push(p);
}
void Allocation::removeProgramToDirty(const Program *p) {
for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
if (mToDirtyList[ct] == p) {
mToDirtyList.removeAt(ct);
return;
}
}
rsAssert(0);
}
void Allocation::dumpLOGV(const char *prefix) const {
ObjectBase::dumpLOGV(prefix);
String8 s(prefix);
s.append(" type ");
if (mType.get()) {
mType->dumpLOGV(s.string());
}
LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
}
void Allocation::serialize(OStream *stream) const {
// Need to identify ourselves
stream->addU32((uint32_t)getClassId());
String8 name(getName());
stream->addString(&name);
// First thing we need to serialize is the type object since it will be needed
// to initialize the class
mType->serialize(stream);
uint32_t dataSize = mType->getSizeBytes();
// Write how much data we are storing
stream->addU32(dataSize);
// Now write the data
stream->addByteArray(mPtr, dataSize);
}
Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
// First make sure we are reading the correct object
RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
LOGE("allocation loading skipped due to invalid class id\n");
return NULL;
}
String8 name;
stream->loadString(&name);
Type *type = Type::createFromStream(rsc, stream);
if (!type) {
return NULL;
}
type->compute();
// Number of bytes we wrote out for this allocation
uint32_t dataSize = stream->loadU32();
if (dataSize != type->getSizeBytes()) {
LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
ObjectBase::checkDelete(type);
return NULL;
}
Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
alloc->setName(name.string(), name.size());
// Read in all of our allocation data
alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
stream->reset(stream->getPos() + dataSize);
return alloc;
}
void Allocation::sendDirty() const {
for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
mToDirtyList[ct]->forceDirty();
}
}
void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
const uint8_t *p = static_cast<const uint8_t *>(ptr);
const Element *e = mType->getElement();
uint32_t stride = e->getSizeBytes();
p += stride * startOff;
while (ct > 0) {
e->incRefs(p);
ct --;
p += stride;
}
}
void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
const uint8_t *p = static_cast<const uint8_t *>(ptr);
const Element *e = mType->getElement();
uint32_t stride = e->getSizeBytes();
p += stride * startOff;
while (ct > 0) {
e->decRefs(p);
ct --;
p += stride;
}
}
void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
}
void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Type *t = mType->cloneAndResize1D(rsc, dimX);
uint32_t oldDimX = mType->getDimX();
if (dimX == oldDimX) {
return;
}
if (dimX < oldDimX) {
decRefs(mPtr, oldDimX - dimX, dimX);
}
mPtr = realloc(mPtr, t->getSizeBytes());
if (dimX > oldDimX) {
const Element *e = mType->getElement();
uint32_t stride = e->getSizeBytes();
memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
}
mType.set(t);
}
void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
LOGE("not implemented");
}
/////////////////
//
namespace android {
namespace renderscript {
void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Allocation *alloc = static_cast<Allocation *>(va);
alloc->deferedUploadToTexture(rsc);
}
void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Allocation *alloc = static_cast<Allocation *>(va);
alloc->deferedUploadToBufferObject(rsc);
}
static void mip565(const Adapter2D &out, const Adapter2D &in) {
uint32_t w = out.getDimX();
uint32_t h = out.getDimY();
for (uint32_t y=0; y < h; y++) {
uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
for (uint32_t x=0; x < w; x++) {
*oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
oPtr ++;
i1 += 2;
i2 += 2;
}
}
}
static void mip8888(const Adapter2D &out, const Adapter2D &in) {
uint32_t w = out.getDimX();
uint32_t h = out.getDimY();
for (uint32_t y=0; y < h; y++) {
uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
for (uint32_t x=0; x < w; x++) {
*oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
oPtr ++;
i1 += 2;
i2 += 2;
}
}
}
static void mip8(const Adapter2D &out, const Adapter2D &in) {
uint32_t w = out.getDimX();
uint32_t h = out.getDimY();
for (uint32_t y=0; y < h; y++) {
uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
for (uint32_t x=0; x < w; x++) {
*oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
oPtr ++;
i1 += 2;
i2 += 2;
}
}
}
static void mip(const Adapter2D &out, const Adapter2D &in) {
switch (out.getBaseType()->getElement()->getSizeBits()) {
case 32:
mip8888(out, in);
break;
case 16:
mip565(out, in);
break;
case 8:
mip8(out, in);
break;
}
}
#ifndef ANDROID_RS_BUILD_FOR_HOST
void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
Allocation *a = static_cast<Allocation *>(va);
a->syncAll(rsc, src);
}
void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
Allocation *texAlloc = static_cast<Allocation *>(va);
const Type * t = texAlloc->getType();
uint32_t w = t->getDimX();
uint32_t h = t->getDimY();
bool genMips = t->getDimLOD();
size_t s = w * h * t->getElementSizeBytes();
if (s != dataLen) {
rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
return;
}
if (texAlloc->getIsScript()) {
memcpy(texAlloc->getPtr(), data, s);
if (genMips) {
Adapter2D adapt(rsc, texAlloc);
Adapter2D adapt2(rsc, texAlloc);
for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
adapt.setLOD(lod);
adapt2.setLOD(lod + 1);
mip(adapt2, adapt);
}
}
} else {
texAlloc->upload2DTexture(false, data);
}
}
void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
Allocation *texAlloc = static_cast<Allocation *>(va);
const Type * t = texAlloc->getType();
size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
if (s != dataLen) {
rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
return;
}
memcpy(data, texAlloc->getPtr(), s);
}
void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {
Allocation *a = static_cast<Allocation *>(va);
a->data(rsc, data, sizeBytes);
}
void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Allocation *a = static_cast<Allocation *>(va);
a->subData(rsc, xoff, count, data, sizeBytes);
}
void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes) {
Allocation *a = static_cast<Allocation *>(va);
a->subElementData(rsc, x, y, data, eoff, sizeBytes);
}
void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes) {
Allocation *a = static_cast<Allocation *>(va);
a->subElementData(rsc, x, data, eoff, sizeBytes);
}
void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Allocation *a = static_cast<Allocation *>(va);
a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
}
void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Allocation *a = static_cast<Allocation *>(va);
a->read(data);
}
void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Allocation *a = static_cast<Allocation *>(va);
a->resize1D(rsc, dimX);
}
void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Allocation *a = static_cast<Allocation *>(va);
a->resize2D(rsc, dimX, dimY);
}
#endif //ANDROID_RS_BUILD_FOR_HOST
}
}
const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Allocation *a = static_cast<Allocation *>(va);
a->getType()->incUserRef();
return a->getType();
}
RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
RsAllocationMipmapControl mips,
uint32_t usages) {
Context *rsc = static_cast<Context *>(con);
Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages);
alloc->incUserRef();
return alloc;
}
RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
RsAllocationMipmapControl mips,
const void *data, uint32_t usages) {
Context *rsc = static_cast<Context *>(con);
Type *t = static_cast<Type *>(vtype);
RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
if (texAlloc == NULL) {
LOGE("Memory allocation failure");
return NULL;
}
memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Adapter2D adapt(rsc, texAlloc);
Adapter2D adapt2(rsc, texAlloc);
for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
adapt.setLOD(lod);
adapt2.setLOD(lod + 1);
mip(adapt2, adapt);
}
}
texAlloc->deferedUploadToTexture(rsc);
return texAlloc;
}
RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
RsAllocationMipmapControl mips,
const void *data, uint32_t usages) {
Context *rsc = static_cast<Context *>(con);
Type *t = static_cast<Type *>(vtype);
// Cubemap allocation's faces should be Width by Width each.
// Source data should have 6 * Width by Width pixels
// Error checking is done in the java layer
RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
if (texAlloc == NULL) {
LOGE("Memory allocation failure");
return NULL;
}
uint8_t *sourcePtr = (uint8_t*)data;
for (uint32_t face = 0; face < 6; face ++) {
Adapter2D faceAdapter(rsc, texAlloc);
faceAdapter.setFace(face);
size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes();
memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize);
// Move the data pointer to the next cube face
sourcePtr += cpySize;
if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Adapter2D adapt(rsc, texAlloc);
Adapter2D adapt2(rsc, texAlloc);
adapt.setFace(face);
adapt2.setFace(face);
for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
adapt.setLOD(lod);
adapt2.setLOD(lod + 1);
mip(adapt2, adapt);
}
}
}
texAlloc->deferedUploadToTexture(rsc);
return texAlloc;
}
|