Skip to content

Commit e07694b

Browse files
author
Jason Sams
committed
Validate context when using RS objects.
BUG=6035422 Change-Id: I8586be0085b36767289e1f634111c0ff076cec3c
1 parent 991c873 commit e07694b

16 files changed

+94
-87
lines changed

graphics/java/android/renderscript/Allocation.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public enum MipmapControl {
184184

185185
private int getIDSafe() {
186186
if (mAdaptedAllocation != null) {
187-
return mAdaptedAllocation.getID();
187+
return mAdaptedAllocation.getID(mRS);
188188
}
189-
return getID();
189+
return getID(mRS);
190190
}
191191

192192

@@ -321,7 +321,7 @@ private void validateIsObject() {
321321
@Override
322322
void updateFromNative() {
323323
super.updateFromNative();
324-
int typeID = mRS.nAllocationGetType(getID());
324+
int typeID = mRS.nAllocationGetType(getID(mRS));
325325
if(typeID != 0) {
326326
mType = new Type(typeID, mRS);
327327
mType.updateFromNative();
@@ -371,7 +371,7 @@ public void ioSend() {
371371
"Can only send buffer if IO_OUTPUT usage specified.");
372372
}
373373
mRS.validate();
374-
mRS.nAllocationIoSend(getID());
374+
mRS.nAllocationIoSend(getID(mRS));
375375
}
376376

377377
/**
@@ -394,7 +394,7 @@ public void ioReceive() {
394394
"Can only receive if IO_INPUT usage specified.");
395395
}
396396
mRS.validate();
397-
mRS.nAllocationIoReceive(getID());
397+
mRS.nAllocationIoReceive(getID(mRS));
398398
}
399399

400400
/**
@@ -411,7 +411,7 @@ public void copyFrom(BaseObj[] d) {
411411
}
412412
int i[] = new int[d.length];
413413
for (int ct=0; ct < d.length; ct++) {
414-
i[ct] = d[ct].getID();
414+
i[ct] = d[ct].getID(mRS);
415415
}
416416
copy1DRangeFromUnchecked(0, mCurrentCount, i);
417417
}
@@ -571,7 +571,7 @@ public void copyFrom(Bitmap b) {
571571
mRS.validate();
572572
validateBitmapSize(b);
573573
validateBitmapFormat(b);
574-
mRS.nAllocationCopyFromBitmap(getID(), b);
574+
mRS.nAllocationCopyFromBitmap(getID(mRS), b);
575575
}
576576

577577
/**
@@ -652,7 +652,7 @@ private void data1DChecks(int off, int count, int len, int dataSize) {
652652
* followup sync will be required.
653653
*/
654654
public void generateMipmaps() {
655-
mRS.nAllocationGenerateMipmaps(getID());
655+
mRS.nAllocationGenerateMipmaps(getID(mRS));
656656
}
657657

658658
/**
@@ -780,7 +780,7 @@ public void copy1DRangeFrom(int off, int count, float[] d) {
780780
public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
781781
mRS.nAllocationData2D(getIDSafe(), off, 0,
782782
mSelectedLOD, mSelectedFace.mID,
783-
count, 1, data.getID(), dataOff, 0,
783+
count, 1, data.getID(mRS), dataOff, 0,
784784
data.mSelectedLOD, data.mSelectedFace.mID);
785785
}
786786

@@ -857,7 +857,7 @@ public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
857857
validate2DRange(xoff, yoff, w, h);
858858
mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
859859
mSelectedLOD, mSelectedFace.mID,
860-
w, h, data.getID(), dataXoff, dataYoff,
860+
w, h, data.getID(mRS), dataXoff, dataYoff,
861861
data.mSelectedLOD, data.mSelectedFace.mID);
862862
}
863863

@@ -888,7 +888,7 @@ public void copyTo(Bitmap b) {
888888
mRS.validate();
889889
validateBitmapFormat(b);
890890
validateBitmapSize(b);
891-
mRS.nAllocationCopyToBitmap(getID(), b);
891+
mRS.nAllocationCopyToBitmap(getID(mRS), b);
892892
}
893893

894894
/**
@@ -901,7 +901,7 @@ public void copyTo(Bitmap b) {
901901
public void copyTo(byte[] d) {
902902
validateIsInt8();
903903
mRS.validate();
904-
mRS.nAllocationRead(getID(), d);
904+
mRS.nAllocationRead(getID(mRS), d);
905905
}
906906

907907
/**
@@ -914,7 +914,7 @@ public void copyTo(byte[] d) {
914914
public void copyTo(short[] d) {
915915
validateIsInt16();
916916
mRS.validate();
917-
mRS.nAllocationRead(getID(), d);
917+
mRS.nAllocationRead(getID(mRS), d);
918918
}
919919

920920
/**
@@ -927,7 +927,7 @@ public void copyTo(short[] d) {
927927
public void copyTo(int[] d) {
928928
validateIsInt32();
929929
mRS.validate();
930-
mRS.nAllocationRead(getID(), d);
930+
mRS.nAllocationRead(getID(mRS), d);
931931
}
932932

933933
/**
@@ -940,7 +940,7 @@ public void copyTo(int[] d) {
940940
public void copyTo(float[] d) {
941941
validateIsFloat32();
942942
mRS.validate();
943-
mRS.nAllocationRead(getID(), d);
943+
mRS.nAllocationRead(getID(mRS), d);
944944
}
945945

946946
/**
@@ -959,10 +959,10 @@ public synchronized void resize(int dimX) {
959959
if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
960960
throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
961961
}
962-
mRS.nAllocationResize1D(getID(), dimX);
962+
mRS.nAllocationResize1D(getID(mRS), dimX);
963963
mRS.finish(); // Necessary because resize is fifoed and update is async.
964964

965-
int typeID = mRS.nAllocationGetType(getID());
965+
int typeID = mRS.nAllocationGetType(getID(mRS));
966966
mType = new Type(typeID, mRS);
967967
mType.updateFromNative();
968968
updateCacheInfo(mType);
@@ -991,10 +991,10 @@ public void resize(int dimX, int dimY) {
991991
throw new RSInvalidStateException(
992992
"Resize only support for 2D allocations at this time.");
993993
}
994-
mRS.nAllocationResize2D(getID(), dimX, dimY);
994+
mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
995995
mRS.finish(); // Necessary because resize is fifoed and update is async.
996996

997-
int typeID = mRS.nAllocationGetType(getID());
997+
int typeID = mRS.nAllocationGetType(getID(mRS));
998998
mType = new Type(typeID, mRS);
999999
mType.updateFromNative();
10001000
updateCacheInfo(mType);
@@ -1019,10 +1019,10 @@ public void resize(int dimX, int dimY) {
10191019
*/
10201020
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
10211021
rs.validate();
1022-
if (type.getID() == 0) {
1022+
if (type.getID(rs) == 0) {
10231023
throw new RSInvalidStateException("Bad Type");
10241024
}
1025-
int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage, 0);
1025+
int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
10261026
if (id == 0) {
10271027
throw new RSRuntimeException("Allocation creation failed.");
10281028
}
@@ -1043,10 +1043,10 @@ static public Allocation createTyped(RenderScript rs, Type type, MipmapControl m
10431043
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips,
10441044
int usage, int pointer) {
10451045
rs.validate();
1046-
if (type.getID() == 0) {
1046+
if (type.getID(rs) == 0) {
10471047
throw new RSInvalidStateException("Bad Type");
10481048
}
1049-
int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage, pointer);
1049+
int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, pointer);
10501050
if (id == 0) {
10511051
throw new RSRuntimeException("Allocation creation failed.");
10521052
}
@@ -1101,7 +1101,7 @@ static public Allocation createSized(RenderScript rs, Element e,
11011101
b.setX(count);
11021102
Type t = b.create();
11031103

1104-
int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage, 0);
1104+
int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
11051105
if (id == 0) {
11061106
throw new RSRuntimeException("Allocation creation failed.");
11071107
}
@@ -1168,7 +1168,7 @@ static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
11681168
rs.validate();
11691169
Type t = typeFromBitmap(rs, b, mips);
11701170

1171-
int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
1171+
int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
11721172
if (id == 0) {
11731173
throw new RSRuntimeException("Load failed.");
11741174
}
@@ -1186,9 +1186,9 @@ public SurfaceTexture getSurfaceTexture() {
11861186
throw new RSInvalidStateException("Allocation is not a surface texture.");
11871187
}
11881188

1189-
int id = mRS.nAllocationGetSurfaceTextureID(getID());
1189+
int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
11901190
SurfaceTexture st = new SurfaceTexture(id);
1191-
mRS.nAllocationGetSurfaceTextureID2(getID(), st);
1191+
mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
11921192

11931193
return st;
11941194
}
@@ -1211,7 +1211,7 @@ public void setSurface(Surface sur) {
12111211
throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
12121212
}
12131213

1214-
mRS.nAllocationSetSurface(getID(), sur);
1214+
mRS.nAllocationSetSurface(getID(mRS), sur);
12151215
}
12161216

12171217
/**
@@ -1224,7 +1224,7 @@ public void setSurfaceTexture(SurfaceTexture st) {
12241224
}
12251225

12261226
Surface s = new Surface(st);
1227-
mRS.nAllocationSetSurface(getID(), s);
1227+
mRS.nAllocationSetSurface(getID(mRS), s);
12281228
}
12291229

12301230
/**
@@ -1283,7 +1283,7 @@ static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
12831283
tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
12841284
Type t = tb.create();
12851285

1286-
int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
1286+
int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
12871287
if(id == 0) {
12881288
throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
12891289
}

graphics/java/android/renderscript/AllocationAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AllocationAdapter extends Allocation {
3030
mAdaptedAllocation = alloc;
3131
}
3232

33-
int getID() {
33+
int getID(RenderScript rs) {
3434
throw new RSInvalidStateException(
3535
"This operation is not supported with adapters at this time.");
3636
}

graphics/java/android/renderscript/BaseObj.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,22 @@ void setID(int id) {
4343
* Lookup the native object ID for this object. Primarily used by the
4444
* generated reflected code.
4545
*
46+
* @param rs Context to verify against internal context for
47+
* match.
4648
*
4749
* @return int
4850
*/
49-
int getID() {
51+
int getID(RenderScript rs) {
52+
mRS.validate();
5053
if (mDestroyed) {
5154
throw new RSInvalidStateException("using a destroyed object.");
5255
}
5356
if (mID == 0) {
5457
throw new RSRuntimeException("Internal error: Object id 0.");
5558
}
59+
if ((rs != null) && (rs != mRS)) {
60+
throw new RSInvalidStateException("using object with mismatched context.");
61+
}
5662
return mID;
5763
}
5864

@@ -138,7 +144,7 @@ synchronized public void destroy() {
138144
*/
139145
void updateFromNative() {
140146
mRS.validate();
141-
mName = mRS.nGetName(getID());
147+
mName = mRS.nGetName(getID(mRS));
142148
}
143149

144150
/**

graphics/java/android/renderscript/Element.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ void updateFromNative() {
778778

779779
// we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
780780
int[] dataBuffer = new int[5];
781-
mRS.nElementGetNativeData(getID(), dataBuffer);
781+
mRS.nElementGetNativeData(getID(mRS), dataBuffer);
782782

783783
mNormalized = dataBuffer[2] == 1 ? true : false;
784784
mVectorSize = dataBuffer[3];
@@ -803,7 +803,7 @@ void updateFromNative() {
803803
mOffsetInBytes = new int[numSubElements];
804804

805805
int[] subElementIds = new int[numSubElements];
806-
mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
806+
mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
807807
for(int i = 0; i < numSubElements; i ++) {
808808
mElements[i] = new Element(subElementIds[i], mRS);
809809
mElements[i].updateFromNative();
@@ -1062,7 +1062,7 @@ public Element create() {
10621062

10631063
int[] ids = new int[ein.length];
10641064
for (int ct = 0; ct < ein.length; ct++ ) {
1065-
ids[ct] = ein[ct].getID();
1065+
ids[ct] = ein[ct].getID(mRS);
10661066
}
10671067
int id = mRS.nElementCreate2(ids, sin, asin);
10681068
return new Element(id, mRS, ein, sin, asin);

graphics/java/android/renderscript/FieldPacker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void addF64(double v) {
143143

144144
public void addObj(BaseObj obj) {
145145
if (obj != null) {
146-
addI32(obj.getID());
146+
addI32(obj.getID(null));
147147
} else {
148148
addI32(0);
149149
}

graphics/java/android/renderscript/FileA3D.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
165165
}
166166

167167
private void initEntries() {
168-
int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
168+
int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
169169
if(numFileEntries <= 0) {
170170
return;
171171
}
@@ -174,10 +174,10 @@ private void initEntries() {
174174
int[] ids = new int[numFileEntries];
175175
String[] names = new String[numFileEntries];
176176

177-
mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);
177+
mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
178178

179179
for(int i = 0; i < numFileEntries; i ++) {
180-
mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], EntryType.toEntryType(ids[i]));
180+
mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
181181
}
182182
}
183183

graphics/java/android/renderscript/Mesh.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ public Primitive getPrimitive(int slot) {
137137
@Override
138138
void updateFromNative() {
139139
super.updateFromNative();
140-
int vtxCount = mRS.nMeshGetVertexBufferCount(getID());
141-
int idxCount = mRS.nMeshGetIndexCount(getID());
140+
int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS));
141+
int idxCount = mRS.nMeshGetIndexCount(getID(mRS));
142142

143143
int[] vtxIDs = new int[vtxCount];
144144
int[] idxIDs = new int[idxCount];
145145
int[] primitives = new int[idxCount];
146146

147-
mRS.nMeshGetVertices(getID(), vtxIDs, vtxCount);
148-
mRS.nMeshGetIndices(getID(), idxIDs, primitives, idxCount);
147+
mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount);
148+
mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount);
149149

150150
mVertexBuffers = new Allocation[vtxCount];
151151
mIndexBuffers = new Allocation[idxCount];
@@ -343,7 +343,7 @@ public Mesh create() {
343343
alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
344344
}
345345
vertexBuffers[ct] = alloc;
346-
vtx[ct] = alloc.getID();
346+
vtx[ct] = alloc.getID(mRS);
347347
}
348348

349349
for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
@@ -354,7 +354,7 @@ public Mesh create() {
354354
} else if(entry.e != null) {
355355
alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
356356
}
357-
int allocID = (alloc == null) ? 0 : alloc.getID();
357+
int allocID = (alloc == null) ? 0 : alloc.getID(mRS);
358358
indexBuffers[ct] = alloc;
359359
primitives[ct] = entry.prim;
360360

@@ -483,12 +483,12 @@ public Mesh create() {
483483
for(int ct = 0; ct < mVertexTypeCount; ct ++) {
484484
Entry entry = mVertexTypes[ct];
485485
vertexBuffers[ct] = entry.a;
486-
vtx[ct] = entry.a.getID();
486+
vtx[ct] = entry.a.getID(mRS);
487487
}
488488

489489
for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
490490
Entry entry = (Entry)mIndexTypes.elementAt(ct);
491-
int allocID = (entry.a == null) ? 0 : entry.a.getID();
491+
int allocID = (entry.a == null) ? 0 : entry.a.getID(mRS);
492492
indexBuffers[ct] = entry.a;
493493
primitives[ct] = entry.prim;
494494

graphics/java/android/renderscript/Path.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void updateFromNative() {
6767

6868

6969
public static Path createStaticPath(RenderScript rs, Primitive p, float quality, Allocation vtx) {
70-
int id = rs.nPathCreate(p.mID, false, vtx.getID(), 0, quality);
70+
int id = rs.nPathCreate(p.mID, false, vtx.getID(rs), 0, quality);
7171
Path newPath = new Path(id, rs, p, null, null, quality);
7272
return newPath;
7373
}

0 commit comments

Comments
 (0)