sk_sp<SkImage> SkiaRenderer::renderFrame(OpenGLContext& glContext, OpenGLTexture& texture) {
// 1. Activate the OpenGL context w/ 1x1 pbuffer surface (eglMakeCurrent)
glContext.use();
// 2. Initialize Skia
if (_skiaContext == nullptr) {
_skiaContext = GrDirectContext::MakeGL();
}
// TODO: Do I need that?
_skiaContext->resetContext();
_skiaContext->resetGLTextureBindings();
// 3. Create the offscreen Skia Surface
if (_offscreenSurface == nullptr) {
GrBackendTexture skiaTex = _skiaContext->createBackendTexture(texture.width,
texture.height,
SkColorType::kN32_SkColorType,
GrMipMapped::kNo,
GrRenderable::kYes);
GrGLTextureInfo info;
skiaTex.getGLTextureInfo(&info);
_offscreenSurfaceTextureId = info.fID;
__android_log_print(ANDROID_LOG_INFO, TAG, "Created Texture %i!", info.fID);
_offscreenSurface = getSkiaSurface(info.fID, texture.width, texture.height);
}
sk_sp<SkImage> frame = getSkiaTexture(texture);
SkCanvas* canvas = _offscreenSurface->getCanvas();
//canvas->clear(SkColors::kCyan);
auto duration = std::chrono::system_clock::now().time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
canvas->drawImage(frame, 0, 0);
// TODO: Run Skia Frame Processor
SkRect rect = SkRect::MakeXYWH(150, 250, millis % 2000 / 10, millis % 2000 / 10);
SkPaint paint;
paint.setColor(SkColors::kRed);
canvas->drawRect(rect, paint);
_offscreenSurface->flushAndSubmit();
// TODO: Do I need eglSwapBuffer for the 1x1 pbuffer?
glContext.flush();
return _offscreenSurface->makeImageSnapshot();
}
void SkiaRenderer::renderTextureToOutputSurface(OpenGLContext& glContext, sk_sp<SkImage> image, EGLSurface outputSurface) {
// 1. Activate the OpenGL context for the given output surface (eglMakeCurrent)
glContext.use(outputSurface);
// 2. Initialize Skia
if (_skiaContext == nullptr) {
_skiaContext = GrDirectContext::MakeGL();
}
// TODO: use this later kRenderTarget_GrGLBackendState | kTextureBinding_GrGLBackendState
_skiaContext->resetContext();
_skiaContext->resetGLTextureBindings();
// 3. Wrap the target output surface (FBO0 on this glContext)
sk_sp<SkSurface> surface = getSkiaSurface(0, image->width(), image->height());
SkCanvas* canvas = surface->getCanvas();
//canvas->clear(SkColors::kCyan);
canvas->drawImage(image, 0, 0);
// TODO: Remove this
auto duration = std::chrono::system_clock::now().time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
SkRect rect = SkRect::MakeXYWH(150, 250, millis % 3000 / 10, millis % 3000 / 10);
SkPaint paint;
paint.setColor(SkColors::kGreen);
canvas->drawRect(rect, paint);
// This does eglSwapBuffers()
_skiaContext->flushAndSubmit();
// does eglSwapBuffers
glContext.flush();
}