Skip to content

Commit

Permalink
Move SkiaImage cleanup to skia thread
Browse files Browse the repository at this point in the history
  • Loading branch information
queezle42 committed May 15, 2024
1 parent 4869a3d commit 4bdaa9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
31 changes: 20 additions & 11 deletions quasar-wayland-skia/src/Quasar/Wayland/Skia.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ import Quasar.Wayland.Utils.Resources
C.context (CPP.cppCtx <> C.fptrCtx <> mempty {
C.ctxTypesTable = Map.fromList [
(C.TypeName "GrDirectContext", [t|GrDirectContext|]),
(C.TypeName "SkSurface", [t|SkSurface|])
(C.TypeName "SkSurface", [t|SkSurface|]),
(C.TypeName "SkImage", [t|SkImage|])
]
})

Expand All @@ -82,7 +83,7 @@ class Typeable s => IsSkiaBackend s where
newSkiaBackendTexture :: Skia s -> Int32 -> Int32 -> SkiaIO (Ptr SkSurface, SkiaTextureStorage s)
destroySkiaTextureStorage :: Skia s -> SkiaSurfaceState s -> SkiaIO ()
exportSkiaSurfaceDmabuf :: SkiaSurfaceState s -> SkiaIO Dmabuf
skiaImportDmabuf :: Skia s -> Dmabuf -> SkiaIO SkiaImage
skiaImportDmabuf :: Skia s -> Dmabuf -> SkiaIO (Ptr SkImage)

data Skia s = Skia {
disposer :: Disposer,
Expand Down Expand Up @@ -167,13 +168,8 @@ readSkiaSurfaceStateIO (SkiaSurface var) =
Nothing -> undefined
Just surfaceState -> pure surfaceState

data SkiaImage = SkiaImage {
skImage :: Ptr SkImage,
disposer :: Disposer
}

instance Disposable SkiaImage where
getDisposer surface = surface.disposer
newtype SkiaImage = SkiaImage (DisposableVar (Ptr SkImage))
deriving Disposable

--newtype ManagedSkiaSurface = ManagedSkiaSurface {
-- skSurface :: ForeignPtr SkSurface
Expand Down Expand Up @@ -416,8 +412,21 @@ importDmabuf skia frameRelease rc = do
atomicallyC (tryReadRc rc) >>= \case
Nothing -> undefined
Just (ExternalDmabuf _ (Borrowed _ dmabuf)) -> do
readonlySurface <- skiaImportDmabuf skia dmabuf
liftIO $ newRcIO (SkiaRenderedFrameImportedDmabuf frameRelease readonlySurface rc)
skImage <- skiaImportDmabuf skia dmabuf
skiaImage <- liftIO $ newSkiaImage skia skImage

liftIO $ newRcIO (SkiaRenderedFrameImportedDmabuf frameRelease skiaImage rc)


newSkiaImage :: Skia s -> Ptr SkImage -> IO SkiaImage
newSkiaImage skia skImage =
SkiaImage <$> newFnDisposableVarIO skia.exceptionSink destroyImage skImage
where
destroyImage :: Ptr SkImage -> IO ()
destroyImage ptr = runSkiaIO skia.thread $ liftIO do
[CPPU.throwBlock|void {
delete $(SkImage* ptr);
}|]

skiaDmabufGlobal :: IsSkiaBackend s => Skia s -> Global
skiaDmabufGlobal skia =
Expand Down
12 changes: 2 additions & 10 deletions quasar-wayland-skia/src/Quasar/Wayland/Skia/GL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ destroySkiaGLTexture skia surfaceState = liftIO do
glDeleteTexture surfaceState.storage.glTexture


skiaGLImportDmabuf :: Skia GL -> Dmabuf -> SkiaIO SkiaImage
skiaGLImportDmabuf :: Skia GL -> Dmabuf -> SkiaIO (Ptr SkImage)
skiaGLImportDmabuf skia dmabuf = liftIO do
let grDirectContext = skia.grDirectContext

Expand Down Expand Up @@ -443,12 +443,4 @@ skiaGLImportDmabuf skia dmabuf = liftIO do
when (skImage == nullPtr) do
throwIO $ userError "Failed to create skia image from external texture"

disposer <- atomicallyC do
newUnmanagedIODisposer [CPPU.throwBlock|void {
delete $(SkImage* skImage);
}|] skia.exceptionSink

pure SkiaImage {
skImage,
disposer
}
pure skImage

0 comments on commit 4bdaa9a

Please sign in to comment.