How to make sure makeImage is GPU backend?

67 views
Skip to first unread message

程洋

unread,
Jun 3, 2024, 9:09:44 PMJun 3
to skia-discuss
I wrote a simple lanczosScale shader and run it. `scaleBuild.makeImage` takes 120ms to run. I expect it less than 5 ms. Where am I wrong?

Here is my code
--------------------------------------------------------------------------------
----------------------Initialization GL Context-------------------
eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint majorVersion;
EGLint minorVersion;
eglInitialize(eglDisplay, &majorVersion, &minorVersion);
SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API));

EGLint numConfigs = 0;
EGLint eglSampleCnt = 1;
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_STENCIL_SIZE, 8,
EGL_SAMPLE_BUFFERS, eglSampleCnt ? 1 : 0,
EGL_SAMPLES, eglSampleCnt,
EGL_NONE
};
EGLConfig surfaceConfig;
SkAssertResult(eglChooseConfig(eglDisplay, configAttribs, &surfaceConfig, 1, &numConfigs));
SkASSERT(numConfigs > 0);
static const EGLint kEGLContextAttribsForOpenGLES[] = {EGL_CONTEXT_CLIENT_VERSION, 3,EGL_NONE};
eglContext = eglCreateContext(eglDisplay, surfaceConfig, nullptr, kEGLContextAttribsForOpenGLES);

EGLint pbufferAttribs[] = {
EGL_WIDTH, 2048,
EGL_HEIGHT, 2048,
EGL_LARGEST_PBUFFER, EGL_TRUE,
EGL_NONE,
};
eglSurface = eglCreatePbufferSurface(eglDisplay, surfaceConfig, pbufferAttribs);
SkASSERT(EGL_NO_SURFACE != eglSurface);

eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
directContext = GrDirectContext::MakeGL();
--------------------------End Initialization Context-----------------
--------------------------Run Shader---------------------

float originalWH[2] = {
std::ceil(float(inputImage->imageInfo().width())),
std::ceil(float(inputImage->imageInfo().height()))
};

SkImageInfo scaledInfoH = SkImageInfo::Make(int(originalWH[0] * scale), int(originalWH[1]), kRGBA_F16_SkColorType, kPremul_SkAlphaType, nullptr);

SkSamplingOptions linear(SkFilterMode::kLinear, SkMipmapMode::kNone);

SkRuntimeShaderBuilder scaleBuild(lanzcosShader.at("scaleH"));
scaleBuild.child("child") = inputImage->makeShader(SkTileMode::kDecal, SkTileMode::kDecal, linear, nullptr);
scaleBuild.uniform("scale") = 1.0f / scale;
---------------This line takes 120 ms to run------------------
sk_sp<SkImage> tmpImage(scaleBuild.makeImage(context, nullptr, scaledInfoH, false));
---------------This line takes 120 ms to run------------------

John Stiles

unread,
Jun 3, 2024, 9:11:59 PMJun 3
to skia-d...@googlegroups.com
The shader is being run on the CPU. You should draw to a GPU surface. 
Note that makeImage has been removed from Skia in more recent builds. 

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/37620317-d3ff-400c-804c-eb733df1c59dn%40googlegroups.com.

程洋

unread,
Jun 4, 2024, 2:15:47 AMJun 4
to skia-discuss

directContext GrDirectContext::MakeGL();

But I used OpenGL context. Why it is on CPU? How to make it run on GPU?

程洋

unread,
Jun 4, 2024, 10:11:17 AMJun 4
to skia-discuss
I generate SkImage like this. It seems to be a CPU backend memory.
I noticed that if SkImage->IsTextureBackend() is true, then it will be very fast.

Is there any way I can turn this SkImage to TextureBackend before I call shader?

sk_sp<SkData> imageData = SkData::MakeWithCopy(buffer, fileSize);
// 释放缓冲区
// delete[] buffer;

sk_sp<SkImage> inputImage = SkImage::MakeFromEncoded(imageData);


Brian Osman

unread,
Jun 4, 2024, 10:24:04 AMJun 4
to skia-d...@googlegroups.com
Yes, at that version of Skia, I think you can do something like: `sk_sp<SkImage> textureImage = inputImage->makeTextureImage(directContext);` to get a texture-backed version of the image.

程洋

unread,
Jun 5, 2024, 6:29:33 AMJun 5
to skia-discuss
That works. Thank you
Reply all
Reply to author
Forward
0 new messages