| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using Android.Content;
- using Android.Graphics;
- using Android.OS;
- using Android.Util;
- using Android.Views;
- using Avalonia.Platform;
- namespace Avalonia.Android
- {
- public abstract class InvalidationAwareSurfaceView : SurfaceView, ISurfaceHolderCallback, IPlatformHandle
- {
- bool _invalidateQueued;
- readonly object _lock = new object();
- private readonly Handler _handler;
-
- public InvalidationAwareSurfaceView(Context context) : base(context)
- {
- Holder.AddCallback(this);
- _handler = new Handler(context.MainLooper);
- }
- public override void Invalidate()
- {
- lock (_lock)
- {
- if(_invalidateQueued)
- return;
- _handler.Post(() =>
- {
- if (Holder.Surface?.IsValid != true)
- return;
- try
- {
- DoDraw();
- }
- catch (Exception e)
- {
- Log.WriteLine(LogPriority.Error, "Avalonia", e.ToString());
- }
- });
- }
- }
- public override void Invalidate(global::Android.Graphics.Rect dirty)
- {
- Invalidate();
- }
- public override void Invalidate(int l, int t, int r, int b)
- {
- Invalidate();
- }
- public void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
- {
- Log.Info("AVALONIA", "Surface Changed");
- DoDraw();
- }
- public void SurfaceCreated(ISurfaceHolder holder)
- {
- Log.Info("AVALONIA", "Surface Created");
- DoDraw();
- }
- public void SurfaceDestroyed(ISurfaceHolder holder)
- {
- Log.Info("AVALONIA", "Surface Destroyed");
-
- }
- protected void DoDraw()
- {
- lock (_lock)
- {
- _invalidateQueued = false;
- }
- Draw();
- }
- protected abstract void Draw();
- public string HandleDescriptor => "SurfaceView";
- }
- }
|