Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image from an opengl texture #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions platform/cc/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
#include "SkShader.h"
#include "interop.hh"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nAdoptTextureFrom
(JNIEnv* env, jclass jclass, jlong contextPtr, jint textureId, jint target, jint width, jint height, jint format, jint surfaceOrigin, jint colorType) {

GrDirectContext* context = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(contextPtr));

GrGLTextureInfo textureInfo;
textureInfo.fID = static_cast<GrGLuint>(textureId);
textureInfo.fTarget = static_cast<GrGLenum>(target);
textureInfo.fFormat = static_cast<GrGLenum>(format);

GrBackendTexture backendTexture = GrBackendTexture(
width, height,
skgpu::Mipmapped::kYes,
textureInfo
);

sk_sp<SkImage> image = SkImages::AdoptTextureFrom(
context,
backendTexture,
static_cast<GrSurfaceOrigin>(surfaceOrigin),
static_cast<SkColorType>(colorType)
);

return reinterpret_cast<jlong>(image.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterFromBytes
(JNIEnv* env, jclass jclass, jint width, jint height, jint colorType, jint alphaType, jlong colorSpacePtr, jbyteArray bytesArr, jlong rowBytes) {
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(static_cast<uintptr_t>(colorSpacePtr));
Expand Down
38 changes: 38 additions & 0 deletions shared/java/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,43 @@ public static Image makeRaster(ImageInfo imageInfo, byte[] bytes, long rowBytes)
return makeRasterFromBytes(imageInfo, bytes, rowBytes);
}

/**
* <p>Creates Image from an OpenGL texture.</p>
*
* <p>Image is returned if the texture is valid. Valid texture parameters include:</p>
* <ul>
* <li>textureId is a valid OpenGL texture ID;</li>
* <li>width and height are greater than zero;</li>
* <li>ColorType and AlphaType are valid, and ColorType is not ColorType.UNKNOWN;</li>
* <li>colorSpace is a valid SkColorSpace;</li>
* </ul>
*
* @param context GrDirectContext
* @param textureId OpenGL texture ID
* @param width width of the texture
* @param height height of the texture
* @param colorType color type of the texture
* @return Image
*/
public static Image adoptTextureFrom(DirectContext context, int textureId, int target, int width, int height, int format, SurfaceOrigin surfaceOrigin, ColorType colorType) {
try {
Stats.onNativeCall();
long ptr = _nAdoptTextureFrom(Native.getPtr(context),
textureId,
target,
width,
height,
format,
surfaceOrigin.ordinal(),
colorType.ordinal());
if (ptr == 0)
throw new RuntimeException("Failed to adoptTextureFrom " + textureId + " " + width + "x" + height);
return new Image(ptr);
} finally {
ReferenceUtil.reachabilityFence(context);
}
}

/**
* <p>Creates Image from pixels.</p>
*
Expand Down Expand Up @@ -382,6 +419,7 @@ public boolean scalePixels(@NotNull Pixmap dst, SamplingMode samplingMode, boole
}
}

@ApiStatus.Internal public static native long _nAdoptTextureFrom(long contextPtr, int textureId, int target, int width, int height, int format, int surfaceOrigin, int colorType);
@ApiStatus.Internal public static native long _nMakeRasterFromBytes(int width, int height, int colorType, int alphaType, long colorSpacePtr, byte[] pixels, long rowBytes);
@ApiStatus.Internal public static native long _nMakeRasterFromData(int width, int height, int colorType, int alphaType, long colorSpacePtr, long dataPtr, long rowBytes);
@ApiStatus.Internal public static native long _nMakeRasterFromBitmap(long bitmapPtr);
Expand Down