From 2b1b444b970061be41f587d000d8d36d07297109 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Mon, 12 Jan 2015 13:19:56 -0800 Subject: [PATCH 1/3] Move Skia C and Rust interface to Skia This allows using Skia independently of Azure. --- Cargo.toml | 2 +- makefile.cargo | 2 +- src/azure-c.cpp | 62 +------------------------------------------------ src/azure-c.h | 25 ++------------------ src/azure.rs | 35 +++------------------------- src/azure_hl.rs | 45 +++++++++++++++++------------------ src/lib.rs | 10 ++++---- 7 files changed, 33 insertions(+), 148 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d91124d..b28d0a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ build = "make -f makefile.cargo" git = "https://github.com/servo/rust-geom" -[dependencies.skia-sys] +[dependencies.skia] git = "https://github.com/servo/skia" branch = "upstream-2014-06-16" diff --git a/makefile.cargo b/makefile.cargo index ef6ab32..89ec1b6 100644 --- a/makefile.cargo +++ b/makefile.cargo @@ -59,7 +59,7 @@ CXXFLAGS += \ $(NULL) endif -SKIA_OUTDIR = $(shell find $(OUT_DIR)/.. -name 'skia-sys-*' -type d) +SKIA_OUTDIR = $(shell find $(OUT_DIR)/.. -name 'skia-*' -type d) FREETYPE_OUTDIR = $(shell find $(OUT_DIR)/.. -name 'freetype-sys-*' -type d) CXXFLAGS += \ diff --git a/src/azure-c.cpp b/src/azure-c.cpp index c68827f..61be59c 100644 --- a/src/azure-c.cpp +++ b/src/azure-c.cpp @@ -170,61 +170,6 @@ AzCreateColorPattern(AzColor *aColor) { return gfxColorPattern; } -extern "C" AzSkiaSharedGLContextRef -AzCreateSkiaSharedGLContext(AzGLNativeContextRef aNativeContext, AzIntSize *aSize) { - GrGLNativeContext* nativeContext = reinterpret_cast(aNativeContext); - SkNativeSharedGLContext *sharedGLContext = new SkNativeSharedGLContext(*nativeContext); - if (sharedGLContext == NULL) { - return NULL; - } - if (!sharedGLContext->init(aSize->width, aSize->height)) { - return NULL; - } - return sharedGLContext; -} - -extern "C" void -AzRetainSkiaSharedGLContext(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - sharedGLContext->ref(); -} - -extern "C" void -AzReleaseSkiaSharedGLContext(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - sharedGLContext->unref(); -} - -extern "C" unsigned int -AzSkiaSharedGLContextGetFBOID(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - return sharedGLContext->getFBOID(); -} - -extern "C" AzSkiaGrGLSharedSurfaceRef -AzSkiaSharedGLContextStealSurface(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - return reinterpret_cast(sharedGLContext->stealSurface()); -} - -extern "C" AzSkiaGrContextRef -AzSkiaSharedGLContextGetGrContext(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - return sharedGLContext->getGrContext(); -} - -extern "C" void -AzSkiaSharedGLContextMakeCurrent(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - sharedGLContext->makeCurrent(); -} - -extern "C" void -AzSkiaSharedGLContextFlush(AzSkiaSharedGLContextRef aGLContext) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - sharedGLContext->flush(); -} - extern "C" AzDrawTargetRef AzCreateDrawTarget(AzBackendType aBackend, AzIntSize *aSize, AzSurfaceFormat aFormat) { gfx::BackendType backendType = static_cast(aBackend); @@ -255,7 +200,7 @@ AzCreateDrawTargetForData(AzBackendType aBackend, unsigned char *aData, AzIntSiz } extern "C" AzDrawTargetRef -AzCreateSkiaDrawTargetForFBO(AzSkiaSharedGLContextRef aGLContext, AzIntSize *aSize, AzSurfaceFormat aFormat) { +AzCreateSkiaDrawTargetForFBO(SkiaSkNativeSharedGLContextRef aGLContext, AzIntSize *aSize, AzSurfaceFormat aFormat) { SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); GrContext *grContext = sharedGLContext->getGrContext(); gfx::IntSize *size = reinterpret_cast(aSize); @@ -623,11 +568,6 @@ AzDestroyFontOptions(AzFontOptions* aOptions) { #endif } -extern "C" AzGLContext -AzSkiaGetCurrentGLContext() { - return SkNativeSharedGLContext::GetCurrent(); -} - // FIXME: Needs to take a FillRule extern "C" AzPathBuilderRef AzCreatePathBuilder(AzDrawTargetRef aDrawTarget) { diff --git a/src/azure-c.h b/src/azure-c.h index 36e9661..979e504 100644 --- a/src/azure-c.h +++ b/src/azure-c.h @@ -5,9 +5,7 @@ #ifndef MOZILLA_GFX_AZURE_C_H_ #define MOZILLA_GFX_AZURE_C_H_ -#include "GrContext.h" -#include "SkNativeSharedGLContext.h" - +#include "skia-c.h" #include #include @@ -22,11 +20,6 @@ void AzSanityCheck(); // FIXME: This stuff is copy pasted from the azure headers typedef void* AzGradientStopsRef; -typedef void* AzSkiaSharedGLContextRef; -typedef void* AzSkiaSharedGrGLContextRef; -typedef void* AzSkiaGrContextRef; -typedef void* AzSkiaGrGLSharedSurfaceRef; -typedef void* AzSkiaGrGLSharedContextRef; typedef void* AzDrawTargetRef; typedef void* AzPatternRef; typedef void* AzColorPatternRef; @@ -343,20 +336,8 @@ typedef struct _AzNativeFont { void *mFont; } AzNativeFont; -typedef GrGLSharedContext AzGLContext; -typedef GrGLNativeContext* AzGLNativeContextRef; - AzColorPatternRef AzCreateColorPattern(AzColor *aColor); -AzSkiaSharedGLContextRef AzCreateSkiaSharedGLContext(AzGLNativeContextRef aNativeContext, - AzIntSize *aSize); -void AzRetainSkiaSharedGLContext(AzSkiaSharedGLContextRef aGLContext); -void AzReleaseSkiaSharedGLContext(AzSkiaSharedGLContextRef aGLContext); -unsigned int AzSkiaSharedGLContextGetFBOID(AzSkiaSharedGLContextRef aGLContext); -AzSkiaGrContextRef AzSkiaSharedGLContextGetGrContext(AzSkiaSharedGLContextRef aGLContext); -void AzSkiaSharedGLContextMakeCurrent(AzSkiaSharedGLContextRef aGLContext); -void AzSkiaSharedGLContextFlush(AzSkiaSharedGLContextRef aGLContext); - AzDrawTargetRef AzCreateDrawTarget(AzBackendType aBackend, AzIntSize *aSize, AzSurfaceFormat aFormat); @@ -365,7 +346,7 @@ AzDrawTargetRef AzCreateDrawTargetForData(AzBackendType aBackend, AzIntSize *aSize, int32_t aStride, AzSurfaceFormat aFormat); -AzDrawTargetRef AzCreateSkiaDrawTragetForFBO(AzSkiaSharedGLContextRef aGLContext, +AzDrawTargetRef AzCreateSkiaDrawTragetForFBO(SkiaSkNativeSharedGLContextRef aGLContext, AzIntSize *aSize, AzSurfaceFormat aFormat); @@ -491,8 +472,6 @@ AzFontOptions* AzCreateFontOptionsForName(char *aName, AzFontStyle aStyle); AzFontOptions* AzCreateFontOptionsForData(uint8_t *aFontData, uint32_t aFontDataSize); void AzDestroyFontOptions(AzFontOptions* aOptions); -AzGLContext AzSkiaGetCurrentGLContext(); - #ifdef __cplusplus } #endif diff --git a/src/azure.rs b/src/azure.rs index f2187c6..6a48e85 100644 --- a/src/azure.rs +++ b/src/azure.rs @@ -10,6 +10,8 @@ use libc::*; +use skia::SkiaSkNativeSharedGLContextRef; + pub type AzFontOptions = *mut c_void; pub type AzFloat = c_float; @@ -393,14 +395,6 @@ pub type AzNativeFont = struct__AzNativeFont; pub type AzGradientStopsRef = *mut c_void; -pub type AzSkiaSharedGLContextRef = *mut c_void; - -pub type AzSkiaSharedGrGLContextRef = *mut c_void; - -pub type AzSkiaGrContextRef = *mut c_void; - -pub type AzSkiaGrGLSharedSurfaceRef = *mut c_void; - pub type AzDrawTargetRef = *mut c_void; pub type AzPatternRef = *mut c_void; @@ -422,12 +416,8 @@ pub type AzDrawSurfaceOptionsRef = *mut AzDrawSurfaceOptions; pub type AzGLContext = *mut c_void; -pub type AzSkiaGrGLSharedContextRef = *mut c_void; - pub type AzGLContextMetadataRef = *mut c_void; -pub type AzGLNativeContextRef = *mut c_void; - pub type AzPathRef = *mut c_void; pub type AzPathBuilderRef = *mut c_void; @@ -445,28 +435,11 @@ pub fn AzCreateColorPattern(aColor: *mut AzColor) -> AzColorPatternRef; pub fn AzReleasePattern(aPattern: AzPatternRef); -pub fn AzCreateSkiaSharedGLContext(aNativeContext: AzGLNativeContextRef, aSize: *mut AzIntSize) - -> AzSkiaSharedGLContextRef; - -pub fn AzRetainSkiaSharedGLContext(aGLContext: AzSkiaSharedGLContextRef); - -pub fn AzReleaseSkiaSharedGLContext(aGLContext: AzSkiaSharedGLContextRef); - -pub fn AzSkiaSharedGLContextGetFBOID(aGLContext: AzSkiaSharedGLContextRef) -> c_uint; - -pub fn AzSkiaSharedGLContextStealSurface(aGLContext: AzSkiaSharedGLContextRef) -> AzSkiaGrGLSharedSurfaceRef; - -pub fn AzSkiaSharedGLContextGetGrContext(aGLContext: AzSkiaSharedGLContextRef) -> AzSkiaGrContextRef; - -pub fn AzSkiaSharedGLContextMakeCurrent(aGLContext: AzSkiaSharedGLContextRef); - -pub fn AzSkiaSharedGLContextFlush(aGLContext: AzSkiaSharedGLContextRef); - pub fn AzCreateDrawTarget(aBackend: AzBackendType, aSize: *mut AzIntSize, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; pub fn AzCreateDrawTargetForData(aBackend: AzBackendType, aData: *mut c_uchar, aSize: *mut AzIntSize, aStride: i32, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; -pub fn AzCreateSkiaDrawTargetForFBO(aGLContext: AzSkiaSharedGLContextRef, aSize: *mut AzIntSize, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; +pub fn AzCreateSkiaDrawTargetForFBO(aGLContext: SkiaSkNativeSharedGLContextRef, aSize: *mut AzIntSize, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; pub fn AzRetainDrawTarget(aTarget: AzDrawTargetRef); @@ -571,8 +544,6 @@ pub fn AzCreateFontOptionsForName(aName: *const c_char, aStyle: enum_AzFontStyle pub fn AzDestroyFontOptions(aOptions: *mut AzFontOptions); -pub fn AzSkiaGetCurrentGLContext() -> AzGLContext; - pub fn AzCreatePathBuilder(aDrawTarget: AzDrawTargetRef) -> AzPathBuilderRef; pub fn AzReleasePathBuilder(aPathBuilder: AzPathBuilderRef); diff --git a/src/azure_hl.rs b/src/azure_hl.rs index 9e77767..7fc9349 100644 --- a/src/azure_hl.rs +++ b/src/azure_hl.rs @@ -26,12 +26,10 @@ use azure::{AzCompositionOp}; use azure::{struct__AzColor, struct__AzGlyphBuffer}; use azure::{struct__AzDrawOptions, struct__AzDrawSurfaceOptions, struct__AzIntSize}; use azure::{struct__AzPoint, struct__AzRect, struct__AzStrokeOptions, struct__AzMatrix5x4}; -use azure::{AzGLContext, AzSkiaSharedGLContextRef}; use azure::{AzCreateColorPattern, AzCreateDrawTarget, AzCreateDrawTargetForData}; use azure::{AzDataSourceSurfaceGetData, AzDataSourceSurfaceGetStride}; use azure::{AzDrawTargetClearRect}; -use azure::{AzDrawTargetCreateSourceSurfaceFromData, AzCreateSkiaSharedGLContext}; -use azure::{AzReleaseSkiaSharedGLContext, AzRetainSkiaSharedGLContext}; +use azure::{AzDrawTargetCreateSourceSurfaceFromData}; use azure::{AzDrawTargetDrawSurface, AzDrawTargetFillRect, AzDrawTargetFlush}; use azure::{AzDrawTargetGetSize, AzDrawTargetGetSnapshot, AzDrawTargetSetTransform}; use azure::{AzDrawTargetStrokeLine, AzDrawTargetStrokeRect, AzDrawTargetFillGlyphs}; @@ -39,13 +37,11 @@ use azure::{AzDrawTargetCreateGradientStops, AzDrawTargetGetFormat}; use azure::{AzReleaseDrawTarget, AzReleasePattern, AzReleaseGradientStops}; use azure::{AzReleaseSourceSurface, AzRetainDrawTarget}; use azure::{AzSourceSurfaceGetDataSurface, AzSourceSurfaceGetFormat}; -use azure::{AzSourceSurfaceGetSize, AzCreateSkiaDrawTargetForFBO, AzSkiaGetCurrentGLContext}; -use azure::{AzSkiaSharedGLContextMakeCurrent, AzSkiaSharedGLContextStealSurface}; -use azure::{AzSkiaSharedGLContextFlush, AzSkiaGrGLSharedSurfaceRef}; +use azure::{AzSourceSurfaceGetSize, AzCreateSkiaDrawTargetForFBO}; use azure::{AzCreatePathBuilder, AzPathBuilderRef, AzPathBuilderMoveTo, AzPathBuilderLineTo}; use azure::{AzPathBuilderArc, AzPathBuilderFinish, AzReleasePathBuilder}; use azure::{AzDrawTargetFill, AzPathRef, AzReleasePath, AzDrawTargetPushClip, AzDrawTargetPopClip}; -use azure::{AzGLNativeContextRef, AzLinearGradientPatternRef, AzMatrix, AzPatternRef}; +use azure::{AzLinearGradientPatternRef, AzMatrix, AzPatternRef}; use azure::{AzCreateLinearGradientPattern, AzDrawTargetPushClipRect}; use azure::{AzDrawTargetDrawSurfaceWithShadow, AzDrawTargetCreateShadowDrawTarget}; use azure::{AzDrawTargetCreateSimilarDrawTarget, AzDrawTargetGetTransform}; @@ -55,16 +51,21 @@ use azure::{AzFilterNodeSetMatrix5x4Attribute, AzFilterNodeSetFilterNodeInput}; use azure::{AzFilterNodeSetFloatArrayAttribute, AzFilterNodeSetBoolAttribute}; use azure::{AzDrawTargetDrawFilter, AzFilterNodeRef, AzFilterType}; -use std::sync::Arc; use geom::matrix2d::Matrix2D; use geom::point::Point2D; use geom::rect::Rect; use geom::size::Size2D; use libc::types::common::c99::{uint8_t, uint16_t}; use libc::size_t; +use skia::SkiaGrGLNativeContextRef; +use skia::{SkiaSkNativeSharedGLContextRef, SkiaSkNativeSharedGLContextCreate}; +use skia::{SkiaSkNativeSharedGLContextRelease, SkiaSkNativeSharedGLContextRetain}; +use skia::{SkiaSkNativeSharedGLContextMakeCurrent, SkiaSkNativeSharedGLContextStealSurface}; +use skia::{SkiaSkNativeSharedGLContextFlush, SkiaGrGLSharedSurfaceRef}; use std::mem; use std::ptr; use std::slice; +use std::sync::Arc; pub trait AsAzureRect { fn as_azure_rect(&self) -> AzRect; @@ -352,7 +353,7 @@ impl BackendType { pub struct DrawTarget { pub azure_draw_target: AzDrawTargetRef, pub data: Option>>, - pub skia_context: Option + pub skia_context: Option } impl Drop for DrawTarget { @@ -361,7 +362,7 @@ impl Drop for DrawTarget { AzReleaseDrawTarget(self.azure_draw_target); match self.skia_context { None => {} - Some(ctx_ref) => { AzReleaseSkiaSharedGLContext(ctx_ref); } + Some(ctx_ref) => { SkiaSkNativeSharedGLContextRelease(ctx_ref); } } } } @@ -377,7 +378,7 @@ impl PartialEq for DrawTarget { /// Contains the GL resources that Skia was holding onto that may be safely extracted. At the /// moment this consists simply of the native surface. pub struct StolenGLResources { - pub surface: AzSkiaGrGLSharedSurfaceRef, + pub surface: SkiaGrGLSharedSurfaceRef, } impl DrawTarget { @@ -423,13 +424,15 @@ impl DrawTarget { } pub fn new_with_fbo(backend: BackendType, - native_graphics_context: AzGLNativeContextRef, + native_graphics_context: SkiaGrGLNativeContextRef, size: Size2D, format: SurfaceFormat) -> DrawTarget { assert!(backend == BackendType::Skia); + let native_surface = native_graphics_context as SkiaGrGLSharedSurfaceRef; let skia_context = unsafe { - AzCreateSkiaSharedGLContext(native_graphics_context, - &mut size.as_azure_int_size()) + SkiaSkNativeSharedGLContextCreate(native_graphics_context, + size.width, + size.height) }; let azure_draw_target = unsafe { AzCreateSkiaDrawTargetForFBO(skia_context, @@ -450,7 +453,7 @@ impl DrawTarget { unsafe { AzRetainDrawTarget(self.azure_draw_target); match self.skia_context { - Some(ctx) => AzRetainSkiaSharedGLContext(ctx), + Some(ctx) => SkiaSkNativeSharedGLContextRetain(ctx), None => (), } } @@ -469,7 +472,7 @@ impl DrawTarget { None => {} Some(ctx) => { unsafe { - AzSkiaSharedGLContextMakeCurrent(ctx); + SkiaSkNativeSharedGLContextMakeCurrent(ctx); } } } @@ -479,7 +482,7 @@ impl DrawTarget { pub fn steal_gl_resources(self) -> Option { self.skia_context.map(|ctx| { StolenGLResources { - surface: unsafe { AzSkiaSharedGLContextStealSurface(ctx) }, + surface: unsafe { SkiaSkNativeSharedGLContextStealSurface(ctx) }, } }) } @@ -509,7 +512,7 @@ impl DrawTarget { AzDrawTargetFlush(self.azure_draw_target); match self.skia_context { None => {} - Some(ctx) => { AzSkiaSharedGLContextFlush(ctx); } + Some(ctx) => { SkiaSkNativeSharedGLContextFlush(ctx); } } } } @@ -1019,12 +1022,6 @@ impl<'a> PatternRef<'a> { } } -pub fn current_gl_context() -> AzGLContext { - unsafe { - AzSkiaGetCurrentGLContext() - } -} - pub struct FilterNode { pub azure_filter_node: AzFilterNodeRef, } diff --git a/src/lib.rs b/src/lib.rs index a995dc3..0b6fc54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ extern crate libc; extern crate geom; +extern crate skia; #[cfg(target_os = "linux")] extern crate xlib; #[cfg(target_os = "android")] @@ -56,21 +57,18 @@ pub use azure::{AzFontOptions, AzFloat, enum_AzSurfaceType, AZ_SURFACE_DATA, AzDrawOptions, struct__AzStrokeOptions, AzStrokeOptions, struct__AzDrawSurfaceOptions, AzDrawSurfaceOptions, struct__AzGlyph, AzGlyph, struct__AzGlyphBuffer, AzGlyphBuffer, struct__AzNativeFont, AzNativeFont, AzGradientStopsRef, - AzSkiaSharedGLContextRef, AzSkiaSharedGrGLContextRef, AzSkiaGrContextRef, AzSkiaGrGLSharedSurfaceRef, AzDrawTargetRef, AzPatternRef, AzColorPatternRef, AzScaledFontRef, AzGlyphRenderingOptionsRef, AzSourceSurfaceRef, AzDataSourceSurfaceRef, AzDrawSurfaceOptionsRef, - AzGLContext, AzSkiaGrGLSharedContextRef, AzGLContextMetadataRef, AzGLNativeContextRef, + AzGLContext, AzGLContextMetadataRef, AzPathRef, AzPathBuilderRef, AzSanityCheck, AzCreateColorPattern, - AzCreateSkiaSharedGLContext, AzRetainSkiaSharedGLContext, AzReleaseSkiaSharedGLContext, - AzSkiaSharedGLContextGetFBOID, AzSkiaSharedGLContextStealSurface, AzSkiaSharedGLContextGetGrContext, AzSkiaSharedGLContextMakeCurrent, - AzSkiaSharedGLContextFlush, AzCreateDrawTarget, AzCreateDrawTargetForData, AzCreateSkiaDrawTargetForFBO, + AzCreateDrawTarget, AzCreateDrawTargetForData, AzCreateSkiaDrawTargetForFBO, AzRetainDrawTarget, AzReleaseDrawTarget, AzDrawTargetGetSize, AzDrawTargetFlush, AzDrawTargetClearRect, AzDrawTargetFillRect, AzDrawTargetStrokeRect, AzDrawTargetStrokeLine, AzDrawTargetFill, AzDrawTargetPushClip, AzDrawTargetPopClip, AzDrawTargetFillGlyphs, AzDrawTargetDrawSurface, AzDrawTargetGetSnapshot, AzDrawTargetCreateSourceSurfaceFromData, AzReleaseSourceSurface, AzSourceSurfaceGetSize, AzSourceSurfaceGetFormat, AzSourceSurfaceGetDataSurface, AzDataSourceSurfaceGetData, AzDataSourceSurfaceGetStride, AzCreateScaledFontForNativeFont, AzReleaseScaledFont, AzDrawTargetSetTransform, - AzCreateFontOptionsForName, AzDestroyFontOptions, AzSkiaGetCurrentGLContext, AzCreatePathBuilder, + AzCreateFontOptionsForName, AzDestroyFontOptions, AzCreatePathBuilder, AzReleasePathBuilder, AzPathBuilderMoveTo, AzPathBuilderLineTo, AzPathBuilderFinish, AzReleasePath}; pub mod azure_hl; From c4201e8ba90a692a2a9a694f6a4f6a4ab3f02192 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Tue, 13 Jan 2015 18:26:33 -0800 Subject: [PATCH 2/3] Expose AzCreateDrawTargetSkiaWithGrContextAndFBO as it is in Azure Instead of reaching into the private Skia API to create Skia draw targets, use the Azure API and the newly exposed Skia rust API. This is part of the transition of teasing out the Skia-specific parts of rust-azure. --- src/azure-c.cpp | 7 +++---- src/azure-c.h | 8 +++++--- src/azure.rs | 5 ++--- src/azure_hl.rs | 29 +++++++++++++++++++++-------- src/lib.rs | 2 +- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/azure-c.cpp b/src/azure-c.cpp index 61be59c..9f2a42f 100644 --- a/src/azure-c.cpp +++ b/src/azure-c.cpp @@ -200,13 +200,12 @@ AzCreateDrawTargetForData(AzBackendType aBackend, unsigned char *aData, AzIntSiz } extern "C" AzDrawTargetRef -AzCreateSkiaDrawTargetForFBO(SkiaSkNativeSharedGLContextRef aGLContext, AzIntSize *aSize, AzSurfaceFormat aFormat) { - SkNativeSharedGLContext *sharedGLContext = static_cast(aGLContext); - GrContext *grContext = sharedGLContext->getGrContext(); +AzCreateDrawTargetSkiaWithGrContextAndFBO(SkiaGrContextRef aGrContext, unsigned int aFBOID, AzIntSize *aSize, AzSurfaceFormat aFormat) { + GrContext *grContext = reinterpret_cast(aGrContext); gfx::IntSize *size = reinterpret_cast(aSize); gfx::SurfaceFormat surfaceFormat = static_cast(aFormat); RefPtr target = gfx::Factory::CreateDrawTargetSkiaWithGrContextAndFBO(grContext, - sharedGLContext->getFBOID(), + aFBOID, *size, surfaceFormat); if (target != NULL) { diff --git a/src/azure-c.h b/src/azure-c.h index 979e504..9f732cd 100644 --- a/src/azure-c.h +++ b/src/azure-c.h @@ -346,9 +346,11 @@ AzDrawTargetRef AzCreateDrawTargetForData(AzBackendType aBackend, AzIntSize *aSize, int32_t aStride, AzSurfaceFormat aFormat); -AzDrawTargetRef AzCreateSkiaDrawTragetForFBO(SkiaSkNativeSharedGLContextRef aGLContext, - AzIntSize *aSize, - AzSurfaceFormat aFormat); +AzDrawTargetRef +AzCreateDrawTargetSkiaWithGrContextAndFBO(SkiaGrContextRef aGrContext, + unsigned int aFBOID, + AzIntSize *aSize, + AzSurfaceFormat aFormat); void AzRetainDrawTarget(AzDrawTargetRef aTarget); diff --git a/src/azure.rs b/src/azure.rs index 6a48e85..4aa9da7 100644 --- a/src/azure.rs +++ b/src/azure.rs @@ -9,8 +9,7 @@ #![allow(non_snake_case)] use libc::*; - -use skia::SkiaSkNativeSharedGLContextRef; +use skia::SkiaGrContextRef; pub type AzFontOptions = *mut c_void; pub type AzFloat = c_float; @@ -439,7 +438,7 @@ pub fn AzCreateDrawTarget(aBackend: AzBackendType, aSize: *mut AzIntSize, aForma pub fn AzCreateDrawTargetForData(aBackend: AzBackendType, aData: *mut c_uchar, aSize: *mut AzIntSize, aStride: i32, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; -pub fn AzCreateSkiaDrawTargetForFBO(aGLContext: SkiaSkNativeSharedGLContextRef, aSize: *mut AzIntSize, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; +pub fn AzCreateDrawTargetSkiaWithGrContextAndFBO(aGrContext: SkiaGrContextRef, aFBOID: u32, aSize: *mut AzIntSize, aFormat: AzSurfaceFormat) -> AzDrawTargetRef; pub fn AzRetainDrawTarget(aTarget: AzDrawTargetRef); diff --git a/src/azure_hl.rs b/src/azure_hl.rs index 7fc9349..871debd 100644 --- a/src/azure_hl.rs +++ b/src/azure_hl.rs @@ -37,7 +37,7 @@ use azure::{AzDrawTargetCreateGradientStops, AzDrawTargetGetFormat}; use azure::{AzReleaseDrawTarget, AzReleasePattern, AzReleaseGradientStops}; use azure::{AzReleaseSourceSurface, AzRetainDrawTarget}; use azure::{AzSourceSurfaceGetDataSurface, AzSourceSurfaceGetFormat}; -use azure::{AzSourceSurfaceGetSize, AzCreateSkiaDrawTargetForFBO}; +use azure::{AzSourceSurfaceGetSize, AzCreateDrawTargetSkiaWithGrContextAndFBO}; use azure::{AzCreatePathBuilder, AzPathBuilderRef, AzPathBuilderMoveTo, AzPathBuilderLineTo}; use azure::{AzPathBuilderArc, AzPathBuilderFinish, AzReleasePathBuilder}; use azure::{AzDrawTargetFill, AzPathRef, AzReleasePath, AzDrawTargetPushClip, AzDrawTargetPopClip}; @@ -57,8 +57,9 @@ use geom::rect::Rect; use geom::size::Size2D; use libc::types::common::c99::{uint8_t, uint16_t}; use libc::size_t; -use skia::SkiaGrGLNativeContextRef; +use skia::{SkiaGrGLNativeContextRef, SkiaGrContextRef}; use skia::{SkiaSkNativeSharedGLContextRef, SkiaSkNativeSharedGLContextCreate}; +use skia::{SkiaSkNativeSharedGLContextGetFBOID, SkiaSkNativeSharedGLContextGetGrContext}; use skia::{SkiaSkNativeSharedGLContextRelease, SkiaSkNativeSharedGLContextRetain}; use skia::{SkiaSkNativeSharedGLContextMakeCurrent, SkiaSkNativeSharedGLContextStealSurface}; use skia::{SkiaSkNativeSharedGLContextFlush, SkiaGrGLSharedSurfaceRef}; @@ -430,14 +431,26 @@ impl DrawTarget { assert!(backend == BackendType::Skia); let native_surface = native_graphics_context as SkiaGrGLSharedSurfaceRef; let skia_context = unsafe { - SkiaSkNativeSharedGLContextCreate(native_graphics_context, - size.width, - size.height) + SkiaSkNativeSharedGLContextCreate(native_graphics_context, size.width, size.height) }; + + if skia_context.is_null() { + panic!("null skia shared gl context"); + } + + let gr_context = unsafe { + SkiaSkNativeSharedGLContextGetGrContext(skia_context) + }; + + let fbo_id = unsafe { + SkiaSkNativeSharedGLContextGetFBOID(skia_context) + }; + let azure_draw_target = unsafe { - AzCreateSkiaDrawTargetForFBO(skia_context, - &mut size.as_azure_int_size(), - format.as_azure_surface_format()) + AzCreateDrawTargetSkiaWithGrContextAndFBO(gr_context, + fbo_id, + &mut size.as_azure_int_size(), + format.as_azure_surface_format()) }; if azure_draw_target.is_null() { panic!("null azure draw target"); diff --git a/src/lib.rs b/src/lib.rs index 0b6fc54..c12fae8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,7 @@ pub use azure::{AzFontOptions, AzFloat, enum_AzSurfaceType, AZ_SURFACE_DATA, AzGlyphRenderingOptionsRef, AzSourceSurfaceRef, AzDataSourceSurfaceRef, AzDrawSurfaceOptionsRef, AzGLContext, AzGLContextMetadataRef, AzPathRef, AzPathBuilderRef, AzSanityCheck, AzCreateColorPattern, - AzCreateDrawTarget, AzCreateDrawTargetForData, AzCreateSkiaDrawTargetForFBO, + AzCreateDrawTarget, AzCreateDrawTargetForData, AzCreateDrawTargetSkiaWithGrContextAndFBO, AzRetainDrawTarget, AzReleaseDrawTarget, AzDrawTargetGetSize, AzDrawTargetFlush, AzDrawTargetClearRect, AzDrawTargetFillRect, AzDrawTargetStrokeRect, AzDrawTargetStrokeLine, AzDrawTargetFill, AzDrawTargetPushClip, AzDrawTargetPopClip, AzDrawTargetFillGlyphs, From f9864ef524b9fa3e8493957d0302c868085f97e1 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Thu, 15 Jan 2015 08:24:05 -0800 Subject: [PATCH 3/3] Split out the backing of DrawTarget to DrawTargetBacking This will make it easier to generically consume DrawTargets. If DrawTarget ever has support for other surface types, it will be much easier to generate shared surfaces from them. This allows hiding the GL Skia implementation detail behind another layer of abstraction. --- src/azure_hl.rs | 112 +++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/src/azure_hl.rs b/src/azure_hl.rs index 871debd..49b0617 100644 --- a/src/azure_hl.rs +++ b/src/azure_hl.rs @@ -353,18 +353,13 @@ impl BackendType { pub struct DrawTarget { pub azure_draw_target: AzDrawTargetRef, - pub data: Option>>, - pub skia_context: Option + pub backing: DrawTargetBacking, } impl Drop for DrawTarget { fn drop(&mut self) { unsafe { AzReleaseDrawTarget(self.azure_draw_target); - match self.skia_context { - None => {} - Some(ctx_ref) => { SkiaSkNativeSharedGLContextRelease(ctx_ref); } - } } } } @@ -376,12 +371,6 @@ impl PartialEq for DrawTarget { } } -/// Contains the GL resources that Skia was holding onto that may be safely extracted. At the -/// moment this consists simply of the native surface. -pub struct StolenGLResources { - pub surface: SkiaGrGLSharedSurfaceRef, -} - impl DrawTarget { pub fn new(backend: BackendType, size: Size2D, format: SurfaceFormat) -> DrawTarget { @@ -395,8 +384,7 @@ impl DrawTarget { } DrawTarget { azure_draw_target: azure_draw_target, - data: None, - skia_context: None + backing: DrawTargetBacking::Empty, } } @@ -419,8 +407,7 @@ impl DrawTarget { } DrawTarget { azure_draw_target: azure_draw_target, - data: Some(Arc::new(data)), - skia_context: None + backing: DrawTargetBacking::Data(Arc::new(data)), } } @@ -457,47 +444,29 @@ impl DrawTarget { } DrawTarget { azure_draw_target: azure_draw_target, - data: None, - skia_context: Some(skia_context) + backing: DrawTargetBacking::SkiaContext(skia_context) } } pub fn clone(&self) -> DrawTarget { unsafe { AzRetainDrawTarget(self.azure_draw_target); - match self.skia_context { - Some(ctx) => SkiaSkNativeSharedGLContextRetain(ctx), - None => (), - } } DrawTarget { azure_draw_target: self.azure_draw_target, - data: match self.data { - None => None, - Some(ref arc) => Some(arc.clone()) - }, - skia_context: self.skia_context + backing: self.backing.clone(), } } pub fn make_current(&self) { - match self.skia_context { - None => {} - Some(ctx) => { - unsafe { - SkiaSkNativeSharedGLContextMakeCurrent(ctx); - } - } - } + self.backing.make_current(); } - /// Consumes this draw target and returns the underlying native surface and GL context, if they exist. - pub fn steal_gl_resources(self) -> Option { - self.skia_context.map(|ctx| { - StolenGLResources { - surface: unsafe { SkiaSkNativeSharedGLContextStealSurface(ctx) }, - } - }) + /// Consumes this draw target and returns the underlying draw target backing. + pub fn steal_draw_target_backing(mut self) -> DrawTargetBacking { + let mut backing = DrawTargetBacking::Empty; + mem::swap(&mut backing, &mut self.backing); + backing } pub fn get_size(&self) -> AzIntSize { @@ -523,11 +492,8 @@ impl DrawTarget { pub fn flush(&self) { unsafe { AzDrawTargetFlush(self.azure_draw_target); - match self.skia_context { - None => {} - Some(ctx) => { SkiaSkNativeSharedGLContextFlush(ctx); } - } } + self.backing.flush(); } pub fn clear_rect(&self, rect: &Rect) { @@ -676,8 +642,7 @@ impl DrawTarget { format.as_azure_surface_format()); DrawTarget { azure_draw_target: new_draw_target, - data: None, - skia_context: None, + backing: DrawTargetBacking::Empty, } } } @@ -695,8 +660,7 @@ impl DrawTarget { sigma); DrawTarget { azure_draw_target: new_draw_target, - data: None, - skia_context: None, + backing: DrawTargetBacking::Empty, } } } @@ -777,6 +741,54 @@ pub fn clone_mutable_draw_target(draw_target: &mut DrawTarget) -> DrawTarget { return draw_target.clone(); } +pub enum DrawTargetBacking { + Empty, // The backing is completely owned by the DrawTarget. + Data(Arc>), + SkiaContext(SkiaSkNativeSharedGLContextRef), +} + +impl Drop for DrawTargetBacking { + fn drop(&mut self) { + match *self { + DrawTargetBacking::Empty | DrawTargetBacking::Data(_) => { } + DrawTargetBacking::SkiaContext(skia_context) => unsafe { + SkiaSkNativeSharedGLContextRelease(skia_context); + } + } + } +} + +impl DrawTargetBacking { + pub fn clone(&self) -> DrawTargetBacking { + match *self { + DrawTargetBacking::Empty => DrawTargetBacking::Empty, + DrawTargetBacking::Data(ref arc_data) => DrawTargetBacking::Data(arc_data.clone()), + DrawTargetBacking::SkiaContext(skia_context) => { + unsafe { SkiaSkNativeSharedGLContextRetain(skia_context); } + DrawTargetBacking::SkiaContext(skia_context) + } + } + } + + pub fn make_current(&self) { + match *self { + DrawTargetBacking::Empty | DrawTargetBacking::Data(_) => { } + DrawTargetBacking::SkiaContext(skia_context) => unsafe { + SkiaSkNativeSharedGLContextMakeCurrent(skia_context); + } + } + } + + pub fn flush(&self) { + match *self { + DrawTargetBacking::Empty | DrawTargetBacking::Data(_) => { }, + DrawTargetBacking::SkiaContext(skia_context) => unsafe { + SkiaSkNativeSharedGLContextFlush(skia_context); + } + } + } +} + pub struct SourceSurface { pub azure_source_surface: AzSourceSurfaceRef, }