Skip to content

Commit c107b10

Browse files
Jason SamsAndroid (Google) Code Review
authored andcommitted
Merge "Beging IO stream out from allocation to surface texture."
2 parents 8181201 + 163766c commit c107b10

File tree

10 files changed

+301
-18
lines changed

10 files changed

+301
-18
lines changed

graphics/java/android/renderscript/Allocation.java

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class Allocation extends BaseObj {
102102
public static final int USAGE_SCRIPT = 0x0001;
103103

104104
/**
105-
* GRAPHICS_TEXTURE The allcation will be used as a texture
105+
* GRAPHICS_TEXTURE The allocation will be used as a texture
106106
* source by one or more graphics programs.
107107
*
108108
*/
@@ -124,34 +124,34 @@ public class Allocation extends BaseObj {
124124
public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
125125

126126
/**
127-
* USAGE_GRAPHICS_RENDER_TARGET The allcation will be used as a
127+
* USAGE_GRAPHICS_RENDER_TARGET The allocation will be used as a
128128
* target for offscreen rendering
129129
*
130130
*/
131131
public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
132132

133133
/**
134-
* USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT The allocation will be
135-
* used with a SurfaceTexture object. This usage will cause the
136-
* allocation to be created read only.
134+
* USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE The allocation
135+
* will be used as a SurfaceTexture graphics consumer. This
136+
* usage may only be used with USAGE_GRAPHICS_TEXTURE.
137137
*
138138
* @hide
139139
*/
140140
public static final int USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE = 0x0020;
141141

142142
/**
143-
* USAGE_IO_INPUT The allocation will be
144-
* used with a SurfaceTexture object. This usage will cause the
145-
* allocation to be created read only.
143+
* USAGE_IO_INPUT The allocation will be used as SurfaceTexture
144+
* consumer. This usage will cause the allocation to be created
145+
* read only.
146146
*
147147
* @hide
148148
*/
149149
public static final int USAGE_IO_INPUT = 0x0040;
150150

151151
/**
152-
* USAGE_IO_OUTPUT The allocation will be
153-
* used with a SurfaceTexture object. This usage will cause the
154-
* allocation to be created write only.
152+
* USAGE_IO_OUTPUT The allocation will be used as a
153+
* SurfaceTexture producer. The dimensions and format of the
154+
* SurfaceTexture will be forced to those of the allocation.
155155
*
156156
* @hide
157157
*/
@@ -323,6 +323,37 @@ public void syncAll(int srcLocation) {
323323
mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
324324
}
325325

326+
/**
327+
* Send a buffer to the output stream. The contents of the
328+
* Allocation will be undefined after this operation.
329+
*
330+
* @hide
331+
*
332+
*/
333+
public void ioSendOutput() {
334+
if ((mUsage & USAGE_IO_OUTPUT) == 0) {
335+
throw new RSIllegalArgumentException(
336+
"Can only send buffer if IO_OUTPUT usage specified.");
337+
}
338+
mRS.validate();
339+
mRS.nAllocationIoSend(getID());
340+
}
341+
342+
/**
343+
* Receive the latest input into the Allocation.
344+
*
345+
* @hide
346+
*
347+
*/
348+
public void ioGetInput() {
349+
if ((mUsage & USAGE_IO_INPUT) == 0) {
350+
throw new RSIllegalArgumentException(
351+
"Can only send buffer if IO_OUTPUT usage specified.");
352+
}
353+
mRS.validate();
354+
mRS.nAllocationIoReceive(getID());
355+
}
356+
326357
public void copyFrom(BaseObj[] d) {
327358
mRS.validate();
328359
validateIsObject();
@@ -887,17 +918,37 @@ public synchronized void resize(int dimX) {
887918
updateCacheInfo(mType);
888919
}
889920

890-
/*
921+
/**
922+
* Resize a 2D allocation. The contents of the allocation are
923+
* preserved. If new elements are allocated objects are created
924+
* with null contents and the new region is otherwise undefined.
925+
*
926+
* If the new region is smaller the references of any objects
927+
* outside the new region will be released.
928+
*
929+
* A new type will be created with the new dimension.
930+
*
931+
* @hide
932+
* @param dimX The new size of the allocation.
933+
* @param dimY The new size of the allocation.
934+
*/
891935
public void resize(int dimX, int dimY) {
892-
if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
893-
throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
936+
if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
937+
throw new RSInvalidStateException(
938+
"Resize only support for 2D allocations at this time.");
894939
}
895940
if (mType.getY() == 0) {
896-
throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
941+
throw new RSInvalidStateException(
942+
"Resize only support for 2D allocations at this time.");
897943
}
898944
mRS.nAllocationResize2D(getID(), dimX, dimY);
945+
mRS.finish(); // Necessary because resize is fifoed and update is async.
946+
947+
int typeID = mRS.nAllocationGetType(getID());
948+
mType = new Type(typeID, mRS);
949+
mType.updateFromNative();
950+
updateCacheInfo(mType);
899951
}
900-
*/
901952

902953

903954

@@ -1090,6 +1141,18 @@ public SurfaceTexture getSurfaceTexture() {
10901141

10911142
}
10921143

1144+
/**
1145+
* @hide
1146+
*/
1147+
public void setSurfaceTexture(SurfaceTexture sur) {
1148+
if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1149+
throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1150+
}
1151+
1152+
mRS.validate();
1153+
mRS.nAllocationSetSurfaceTexture(getID(), sur);
1154+
}
1155+
10931156

10941157
/**
10951158
* Creates a non-mipmapped renderscript allocation to use as a

graphics/java/android/renderscript/RenderScript.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ synchronized int nAllocationGetSurfaceTextureID(int alloc) {
274274
validate();
275275
return rsnAllocationGetSurfaceTextureID(mContext, alloc);
276276
}
277+
native void rsnAllocationSetSurfaceTexture(int con, int alloc, SurfaceTexture sur);
278+
synchronized void nAllocationSetSurfaceTexture(int alloc, SurfaceTexture sur) {
279+
validate();
280+
rsnAllocationSetSurfaceTexture(mContext, alloc, sur);
281+
}
282+
native void rsnAllocationIoSend(int con, int alloc);
283+
synchronized void nAllocationIoSend(int alloc) {
284+
validate();
285+
rsnAllocationIoSend(mContext, alloc);
286+
}
287+
native void rsnAllocationIoReceive(int con, int alloc);
288+
synchronized void nAllocationIoReceive(int alloc) {
289+
validate();
290+
rsnAllocationIoReceive(mContext, alloc);
291+
}
292+
277293

278294
native void rsnAllocationGenerateMipmaps(int con, int alloc);
279295
synchronized void nAllocationGenerateMipmaps(int alloc) {

graphics/jni/android_renderscript_RenderScript.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,37 @@ nAllocationGetSurfaceTextureID(JNIEnv *_env, jobject _this, RsContext con, jint
450450
return rsAllocationGetSurfaceTextureID(con, (RsAllocation)a);
451451
}
452452

453+
static void
454+
nAllocationSetSurfaceTexture(JNIEnv *_env, jobject _this, RsContext con,
455+
RsAllocation alloc, jobject sur)
456+
{
457+
LOG_API("nAllocationSetSurfaceTexture, con(%p), alloc(%p), surface(%p)",
458+
con, alloc, (Surface *)sur);
459+
460+
sp<ANativeWindow> window;
461+
if (sur != 0) {
462+
sp<SurfaceTexture> st = SurfaceTexture_getSurfaceTexture(_env, sur);
463+
window = new SurfaceTextureClient(st);
464+
}
465+
466+
rsAllocationSetSurface(con, alloc, window.get());
467+
}
468+
469+
static void
470+
nAllocationIoSend(JNIEnv *_env, jobject _this, RsContext con, RsAllocation alloc)
471+
{
472+
LOG_API("nAllocationIoSend, con(%p), alloc(%p)", con, alloc);
473+
rsAllocationIoSend(con, alloc);
474+
}
475+
476+
static void
477+
nAllocationIoReceive(JNIEnv *_env, jobject _this, RsContext con, RsAllocation alloc)
478+
{
479+
LOG_API("nAllocationIoReceive, con(%p), alloc(%p)", con, alloc);
480+
rsAllocationIoReceive(con, alloc);
481+
}
482+
483+
453484
static void
454485
nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, RsContext con, jint alloc)
455486
{
@@ -1277,6 +1308,9 @@ static JNINativeMethod methods[] = {
12771308

12781309
{"rsnAllocationSyncAll", "(III)V", (void*)nAllocationSyncAll },
12791310
{"rsnAllocationGetSurfaceTextureID", "(II)I", (void*)nAllocationGetSurfaceTextureID },
1311+
{"rsnAllocationSetSurfaceTexture", "(IILandroid/graphics/SurfaceTexture;)V", (void*)nAllocationSetSurfaceTexture },
1312+
{"rsnAllocationIoSend", "(II)V", (void*)nAllocationIoSend },
1313+
{"rsnAllocationIoReceive", "(II)V", (void*)nAllocationIoReceive },
12801314
{"rsnAllocationData1D", "(IIIII[II)V", (void*)nAllocationData1D_i },
12811315
{"rsnAllocationData1D", "(IIIII[SI)V", (void*)nAllocationData1D_s },
12821316
{"rsnAllocationData1D", "(IIIII[BI)V", (void*)nAllocationData1D_b },

libs/rs/driver/rsdAllocation.cpp

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
#include "rsAllocation.h"
2525

26+
#include "system/window.h"
27+
#include "hardware/gralloc.h"
28+
#include "ui/Rect.h"
29+
#include "ui/GraphicBufferMapper.h"
30+
2631
#include <GLES/gl.h>
2732
#include <GLES2/gl2.h>
2833
#include <GLES/glext.h>
@@ -220,7 +225,8 @@ bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
220225
}
221226

222227
void * ptr = alloc->mHal.state.usrPtr;
223-
if (!ptr) {
228+
if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
229+
} else {
224230
ptr = malloc(alloc->mHal.state.type->getSizeBytes());
225231
if (!ptr) {
226232
free(drv);
@@ -248,7 +254,7 @@ bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
248254
alloc->mHal.drvState.mallocPtr = ptr;
249255
drv->mallocPtr = (uint8_t *)ptr;
250256
alloc->mHal.drv = drv;
251-
if (forceZero) {
257+
if (forceZero && ptr) {
252258
memset(ptr, 0, alloc->mHal.state.type->getSizeBytes());
253259
}
254260

@@ -386,6 +392,93 @@ int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *al
386392
return drv->textureID;
387393
}
388394

395+
static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
396+
DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
397+
398+
int32_t r = nw->dequeueBuffer(nw, &drv->wndBuffer);
399+
if (r) {
400+
rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
401+
return false;
402+
}
403+
404+
// This lock is implicitly released by the queue buffer in IoSend
405+
r = nw->lockBuffer(nw, drv->wndBuffer);
406+
if (r) {
407+
rsc->setError(RS_ERROR_DRIVER, "Error locking next IO output buffer.");
408+
return false;
409+
}
410+
411+
// Must lock the whole surface
412+
GraphicBufferMapper &mapper = GraphicBufferMapper::get();
413+
Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
414+
415+
void *dst = NULL;
416+
mapper.lock(drv->wndBuffer->handle,
417+
GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
418+
bounds, &dst);
419+
alloc->mHal.drvState.mallocPtr = dst;
420+
return true;
421+
}
422+
423+
void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
424+
DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
425+
426+
//ALOGE("rsdAllocationSetSurfaceTexture %p %p", alloc, nw);
427+
428+
// Cleanup old surface if there is one.
429+
if (alloc->mHal.state.wndSurface) {
430+
ANativeWindow *old = alloc->mHal.state.wndSurface;
431+
GraphicBufferMapper &mapper = GraphicBufferMapper::get();
432+
mapper.unlock(drv->wndBuffer->handle);
433+
old->queueBuffer(old, drv->wndBuffer);
434+
}
435+
436+
if (nw != NULL) {
437+
int32_t r;
438+
r = native_window_set_usage(nw, GRALLOC_USAGE_SW_READ_RARELY |
439+
GRALLOC_USAGE_SW_WRITE_OFTEN);
440+
if (r) {
441+
rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
442+
return;
443+
}
444+
445+
r = native_window_set_buffers_dimensions(nw, alloc->mHal.state.dimensionX,
446+
alloc->mHal.state.dimensionY);
447+
if (r) {
448+
rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
449+
return;
450+
}
451+
452+
r = native_window_set_buffer_count(nw, 3);
453+
if (r) {
454+
rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
455+
return;
456+
}
457+
458+
IoGetBuffer(rsc, alloc, nw);
459+
}
460+
}
461+
462+
void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
463+
DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
464+
ANativeWindow *nw = alloc->mHal.state.wndSurface;
465+
466+
GraphicBufferMapper &mapper = GraphicBufferMapper::get();
467+
mapper.unlock(drv->wndBuffer->handle);
468+
int32_t r = nw->queueBuffer(nw, drv->wndBuffer);
469+
if (r) {
470+
rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
471+
return;
472+
}
473+
474+
IoGetBuffer(rsc, alloc, nw);
475+
}
476+
477+
void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
478+
ALOGE("not implemented");
479+
}
480+
481+
389482
void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
390483
uint32_t xoff, uint32_t lod, uint32_t count,
391484
const void *data, uint32_t sizeBytes) {

libs/rs/driver/rsdAllocation.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <GLES2/gl2.h>
2525

2626
class RsdFrameBufferObj;
27+
struct ANativeWindowBuffer;
2728

2829
struct DrvAllocation {
2930
// Is this a legal structure to be used as a texture source.
@@ -47,6 +48,7 @@ struct DrvAllocation {
4748
bool uploadDeferred;
4849

4950
RsdFrameBufferObj * readBackFBO;
51+
ANativeWindowBuffer *wndBuffer;
5052
};
5153

5254
GLenum rsdTypeToGLType(RsDataType t);
@@ -69,6 +71,12 @@ void rsdAllocationMarkDirty(const android::renderscript::Context *rsc,
6971
const android::renderscript::Allocation *alloc);
7072
int32_t rsdAllocationInitSurfaceTexture(const android::renderscript::Context *rsc,
7173
const android::renderscript::Allocation *alloc);
74+
void rsdAllocationSetSurfaceTexture(const android::renderscript::Context *rsc,
75+
android::renderscript::Allocation *alloc, ANativeWindow *nw);
76+
void rsdAllocationIoSend(const android::renderscript::Context *rsc,
77+
android::renderscript::Allocation *alloc);
78+
void rsdAllocationIoReceive(const android::renderscript::Context *rsc,
79+
android::renderscript::Allocation *alloc);
7280

7381
void rsdAllocationData1D(const android::renderscript::Context *rsc,
7482
const android::renderscript::Allocation *alloc,

libs/rs/driver/rsdCore.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ static RsdHalFunctions FunctionTable = {
7474
rsdAllocationSyncAll,
7575
rsdAllocationMarkDirty,
7676
rsdAllocationInitSurfaceTexture,
77+
rsdAllocationSetSurfaceTexture,
78+
rsdAllocationIoSend,
79+
rsdAllocationIoReceive,
7780
rsdAllocationData1D,
7881
rsdAllocationData2D,
7982
rsdAllocationData3D,

0 commit comments

Comments
 (0)