OpenGLTexture SkiaRenderer::renderFrame(OpenGLContext& glContext, OpenGLTexture& texture) {
// 1. Activate the OpenGL context (eglMakeCurrent)
glContext.use();
// 2. Initialize Skia
if (_skiaContext == nullptr) {
GrContextOptions options;
// TODO: Set this to true or not? idk
options.fDisableGpuYUVConversion = false;
_skiaContext = GrDirectContext::MakeGL(options);
}
// TODO: use this later kRenderTarget_GrGLBackendState | kTextureBinding_GrGLBackendState
_skiaContext->resetContext();
// 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;
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
_offscreenSurface = SkSurfaces::WrapBackendTexture(_skiaContext.get(),
skiaTex,
kBottomLeft_GrSurfaceOrigin,
0,
SkColorType::kN32_SkColorType,
nullptr,
&props,
// TODO: Delete texture!
nullptr);
}
GrGLTextureInfo textureInfo {
// OpenGL will automatically convert YUV -> RGB because it's an EXTERNAL texture
.fTarget = texture.target,
.fID = texture.id,
.fFormat = GR_GL_RGBA8,
.fProtected = skgpu::Protected::kNo,
};
GrBackendTexture skiaTexture(texture.width,
texture.height,
GrMipMapped::kNo,
textureInfo);
sk_sp<SkImage> frame = SkImages::BorrowTextureFrom(_skiaContext.get(),
skiaTexture,
kBottomLeft_GrSurfaceOrigin,
kN32_SkColorType,
kOpaque_SkAlphaType,
nullptr,
nullptr);
SkCanvas* canvas = _offscreenSurface->getCanvas();
canvas->clear(SkColors::kCyan);
canvas->drawImage(frame, 0, 0);
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);
_offscreenSurface->flushAndSubmit();
OpenGLTexture result {
.id = _offscreenSurfaceTextureId,
.target = GL_TEXTURE_2D,
.width = texture.width,
.height = texture.height,
};
return result;
}