Skip to content

Commit 863860f

Browse files
authored
Add image from an opengl texture (#74)
1 parent 90e6bc1 commit 863860f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

platform/cc/Image.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@
55
#include "SkImage.h"
66
#include "SkShader.h"
77
#include "interop.hh"
8+
#include "GrBackendSurface.h"
9+
#include "GrDirectContext.h"
10+
#include "include/gpu/GrBackendSurface.h"
11+
#include "include/gpu/ganesh/SkImageGanesh.h"
12+
#include "include/gpu/gl/GrGLTypes.h"
13+
14+
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nAdoptGLTextureFrom
15+
(JNIEnv* env, jclass jclass, jlong contextPtr, jint textureId, jint target, jint width, jint height, jint format, jint surfaceOrigin, jint colorType) {
16+
17+
GrDirectContext* context = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(contextPtr));
18+
19+
GrGLTextureInfo textureInfo;
20+
textureInfo.fID = static_cast<GrGLuint>(textureId);
21+
textureInfo.fTarget = static_cast<GrGLenum>(target);
22+
textureInfo.fFormat = static_cast<GrGLenum>(format);
23+
24+
GrBackendTexture backendTexture = GrBackendTexture(
25+
width, height,
26+
skgpu::Mipmapped::kYes,
27+
textureInfo
28+
);
29+
30+
sk_sp<SkImage> image = SkImages::AdoptTextureFrom(
31+
context,
32+
backendTexture,
33+
static_cast<GrSurfaceOrigin>(surfaceOrigin),
34+
static_cast<SkColorType>(colorType)
35+
);
36+
37+
return reinterpret_cast<jlong>(image.release());
38+
}
839

940
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterFromBytes
1041
(JNIEnv* env, jclass jclass, jint width, jint height, jint colorType, jint alphaType, jlong colorSpacePtr, jbyteArray bytesArr, jlong rowBytes) {

shared/java/Image.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,43 @@ public static Image makeRaster(ImageInfo imageInfo, byte[] bytes, long rowBytes)
2323
return makeRasterFromBytes(imageInfo, bytes, rowBytes);
2424
}
2525

26+
/**
27+
* <p>Creates Image from an OpenGL texture.</p>
28+
*
29+
* <p>Image is returned if the texture is valid. Valid texture parameters include:</p>
30+
* <ul>
31+
* <li>textureId is a valid OpenGL texture ID;</li>
32+
* <li>width and height are greater than zero;</li>
33+
* <li>ColorType and AlphaType are valid, and ColorType is not ColorType.UNKNOWN;</li>
34+
* <li>colorSpace is a valid SkColorSpace;</li>
35+
* </ul>
36+
*
37+
* @param context GrDirectContext
38+
* @param textureId OpenGL texture ID
39+
* @param width width of the texture
40+
* @param height height of the texture
41+
* @param colorType color type of the texture
42+
* @return Image
43+
*/
44+
public static Image adoptGLTextureFrom(DirectContext context, int textureId, int target, int width, int height, int format, SurfaceOrigin surfaceOrigin, ColorType colorType) {
45+
try {
46+
Stats.onNativeCall();
47+
long ptr = _nAdoptGLTextureFrom(Native.getPtr(context),
48+
textureId,
49+
target,
50+
width,
51+
height,
52+
format,
53+
surfaceOrigin.ordinal(),
54+
colorType.ordinal());
55+
if (ptr == 0)
56+
throw new RuntimeException("Failed to adoptGLTextureFrom " + textureId + " " + width + "x" + height);
57+
return new Image(ptr);
58+
} finally {
59+
ReferenceUtil.reachabilityFence(context);
60+
}
61+
}
62+
2663
/**
2764
* <p>Creates Image from pixels.</p>
2865
*
@@ -382,6 +419,7 @@ public boolean scalePixels(@NotNull Pixmap dst, SamplingMode samplingMode, boole
382419
}
383420
}
384421

422+
@ApiStatus.Internal public static native long _nAdoptGLTextureFrom(long contextPtr, int textureId, int target, int width, int height, int format, int surfaceOrigin, int colorType);
385423
@ApiStatus.Internal public static native long _nMakeRasterFromBytes(int width, int height, int colorType, int alphaType, long colorSpacePtr, byte[] pixels, long rowBytes);
386424
@ApiStatus.Internal public static native long _nMakeRasterFromData(int width, int height, int colorType, int alphaType, long colorSpacePtr, long dataPtr, long rowBytes);
387425
@ApiStatus.Internal public static native long _nMakeRasterFromBitmap(long bitmapPtr);

0 commit comments

Comments
 (0)