/**
* Creates new <code>GraphicBuffer</code> instance. This method will return null
* if the buffer cannot be created.
*
* @param width The width in pixels of the buffer
* @param height The height in pixels of the buffer
* @param format The format of each pixel as specified in {@link PixelFormat}
* @param usage Hint indicating how the buffer will be used
*
* @return A <code>GraphicBuffer</code> instance or null
*/
public static GraphicBuffer create(int width, int height, int format, int usage) {
long nativeObject = nCreateGraphicBuffer(width, height, format, usage);
if (nativeObject != 0) {
return new GraphicBuffer(width, height, format, usage, nativeObject);
}
return null;
}
静态方法 create()
中先调用 nCreateGraphicBuffer()
方法创建 GraphicBuffer native,然后创建一个 GraphicBuffer 对象返回。
static jlong android_graphics_GraphicBuffer_create(JNIEnv* env, jobject clazz,
jint width, jint height, jint format, jint usage) {
sp<GraphicBuffer> buffer = new GraphicBuffer(
uint32_t(width), uint32_t(height), PixelFormat(format), uint32_t(usage),
std::string("android_graphics_GraphicBuffer_create pid [") +
std::to_string(getpid()) +"]");
status_t error = buffer->initCheck();
if (error < 0) {
ALOGW_IF(kDebugGraphicBuffer, "createGraphicBuffer() failed in GraphicBuffer.create()");
return NULL;
}
GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
return reinterpret_cast<jlong>(wrapper);
}
nCreateGraphicBuffer()
方法绑定的是 android_graphics_GraphicBuffer_create()
函数,它调用 GraphicBuffer 构造函数创建一个 native 对象。如果创建成功,用 GraphicBufferWrapper 包装之后返回地址。
GraphicBuffer 构造函数中会调用 GraphicBuffer::initWithSize()。