mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.47k stars 538 forks source link

[BUG] Alpha8 GPU image draws incorrectly #2239

Open mgood7123 opened 2 years ago

mgood7123 commented 2 years ago

Description

Alpha8 GPU color does not match Alpha8 CPU color

Code

```cs string tmp_string; SKImageInfo IMAGE_INFO = new(1, 1, SKColorType.Alpha8); var color = new SKColor(25, 0, 0, 50); using SKBitmap bitmap = new(IMAGE_INFO); using SKCanvas canvas_ = new(bitmap); canvas_.Clear(color); var BITMAP_BYTES = bitmap.Bytes; Console.WriteLine("BITMAP BYTES LENGTH: " + BITMAP_BYTES.Length); tmp_string = "BITMAP BYTES CONTENT: "; for (int i = 0; i < BITMAP_BYTES.Length; i++) { tmp_string += BITMAP_BYTES[i]; if ((i + 1) != BITMAP_BYTES.Length) { tmp_string += ", "; } } Console.WriteLine(tmp_string); var bitmap_surface = SKSurface.Create(IMAGE_INFO); using SKCanvas canvas_surface = bitmap_surface.Canvas; canvas_surface.Clear(color); using var bitmap_p = bitmap_surface.PeekPixels(); var BITMAP_SURFACE_BYTES = bitmap_p.GetPixelSpan(); Console.WriteLine("BITMAP SURFACE BYTES LENGTH: " + BITMAP_SURFACE_BYTES.Length); tmp_string = "BITMAP SURFACE BYTES CONTENT: "; for (int i = 0; i < BITMAP_SURFACE_BYTES.Length; i++) { tmp_string += BITMAP_SURFACE_BYTES[i]; if ((i + 1) != BITMAP_SURFACE_BYTES.Length) { tmp_string += ", "; } } Console.WriteLine(tmp_string); using var SURFACE_CPU = SKSurface.Create(IMAGE_INFO); SURFACE_CPU.Canvas.Clear(color); SURFACE_CPU.Canvas.Flush(); SURFACE_CPU.Flush(true, true); // submit, synchronous using var PIXMAP_CPU = SURFACE_CPU.PeekPixels(); using var IMAGE_CPU = SKImage.FromPixels(PIXMAP_CPU); using var BITMAP_CPU = SKBitmap.FromImage(IMAGE_CPU); var CPU_BYTES = BITMAP_CPU.Bytes; using var SURFACE_GPU = SKSurface.Create(context, false, IMAGE_INFO); SURFACE_GPU.Canvas.Clear(color); SURFACE_GPU.Canvas.Flush(); SURFACE_GPU.Flush(true, true); // submit, synchronous using var IMAGE_GPU = SURFACE_GPU.Snapshot(); using var BITMAP_GPU = SKBitmap.FromImage(IMAGE_GPU); var GPU_BYTES = BITMAP_GPU.Bytes; Console.WriteLine("CPU BYTES LENGTH: " + CPU_BYTES.Length); tmp_string = "CPU BYTES CONTENT: "; for (int i = 0; i < CPU_BYTES.Length; i++) { tmp_string += CPU_BYTES[i]; if ((i + 1) != CPU_BYTES.Length) { tmp_string += ", "; } } Console.WriteLine(tmp_string); Console.WriteLine("GPU BYTES LENGTH: " + GPU_BYTES.Length); tmp_string = "GPU BYTES CONTENT: "; for (int i = 0; i < GPU_BYTES.Length; i++) { tmp_string += GPU_BYTES[i]; if ((i + 1) != GPU_BYTES.Length) { tmp_string += ", "; } } Console.WriteLine(tmp_string); SKImage raster = IMAGE_GPU; if (IMAGE_GPU.IsTextureBacked) { raster = IMAGE_GPU.ToRasterImage(); } var pixmap = new SKPixmap(); if (!raster.PeekPixels(pixmap)) { pixmap.Dispose(); pixmap = null; } if (pixmap != null) { IntPtr pixels = pixmap.GetPixels(); using SKData pixelData = SKData.Create(pixels, pixmap.BytesSize); Console.WriteLine("GPU PIXEL SPAN LENGTH: " + pixelData.Span.Length); tmp_string = "GPU PIXEL SPAN CONTENT: "; for (int i = 0; i < pixelData.Span.Length; i++) { tmp_string += pixelData.Span[i]; if ((i + 1) != pixelData.Span.Length) { tmp_string += ", "; } } Console.WriteLine(tmp_string); pixmap.Dispose(); } ```

Expected Behavior

BITMAP BYTES LENGTH: 1
BITMAP BYTES CONTENT: 50
BITMAP SURFACE BYTES LENGTH: 1
BITMAP SURFACE BYTES CONTENT: 50
CPU BYTES LENGTH: 4
CPU BYTES CONTENT: 0, 0, 0, 50
GPU BYTES LENGTH: 4
GPU BYTES CONTENT: 0, 0, 0, 50
GPU PIXEL SPAN LENGTH: 1
GPU PIXEL SPAN CONTENT: 50

Actual Behavior

BITMAP BYTES LENGTH: 1
BITMAP BYTES CONTENT: 50
BITMAP SURFACE BYTES LENGTH: 1
BITMAP SURFACE BYTES CONTENT: 50
CPU BYTES LENGTH: 4
CPU BYTES CONTENT: 0, 0, 0, 50
GPU BYTES LENGTH: 4
GPU BYTES CONTENT: 0, 0, 0, 5
GPU PIXEL SPAN LENGTH: 1
GPU PIXEL SPAN CONTENT: 5

Basic Information

``` Microsoft Visual Studio Community 2022 Version 17.3.2 VisualStudio.17.Release/17.3.2+32819.101 Microsoft .NET Framework Version 4.8.09037 Installed Version: Community Visual C++ 2022 00482-90000-00000-AA910 Microsoft Visual C++ 2022 ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 17.3.376.3011 ASP.NET and Web Tools ASP.NET Web Frameworks and Tools 2012 17.3.376.3011 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 17.3.376.3011 Azure App Service Tools v3.0.0 Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio C# Tools 4.3.0-3.22412.4+c97184bafab9a34d61e85f1c1ef34f25283ce9ba C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Cookiecutter 17.0.22179.3 Provides tools for finding, instantiating and customizing templates in cookiecutter format. Dotfuscator Community Edition 6.5.0+76d4669002 PreEmptive Protection - Dotfuscator CE Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. IncrediBuild Build Acceleration 1.6.0.2 IncrediBuild effectively reduces compilation and development times by up to 90%. Linux Core Dump Debugging 1.0.9.32708 Enables debugging of Linux core dumps. Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.3.20 (3f4cb00) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 6.3.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Office Developer Tools for Visual Studio 17.0.32616.00 Microsoft Office Developer Tools for Visual Studio Python - Django support 17.0.22179.3 Provides templates and integration for the Django web framework. Python - Profiling support 17.0.22179.3 Profiling support for Python projects. Python with Pylance 17.0.22179.3 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers. Razor (ASP.NET Core) 17.0.0.2232702+e1d654e792aa2fe6646a6935bcca80ff0aff4387 Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 17.0.62207.04100 Microsoft SQL Server Data Tools Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees. Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory. Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory. ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript Tools 17.0.10701.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.3.0-3.22412.4+c97184bafab9a34d61e85f1c1ef34f25283ce9ba Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual C++ for Cross Platform Mobile Development (Android) 17.0.32708.82 Visual C++ for Cross Platform Mobile Development (Android) Visual C++ for Linux Development 1.0.9.32708 Visual C++ for Linux Development Visual F# Tools 17.1.0-beta.22363.4+1b94f89d4d1f41f20f9be73c76f4b229d4e49078 Microsoft Visual F# Tools Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio. VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager. Xamarin 17.3.0.296 (d17-3@a0ff24b) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.3.0.208 (remotes/origin/d17-3@e4f67afef) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.2.28 (9b3d244) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 13.0.0.0 (d17-3/030cd63) Xamarin.Android Reference Assemblies and MSBuild support. Mono: dffa5ab Java.Interop: xamarin/java.interop/d17-3@7716ae53 SQLite: xamarin/sqlite/3.38.5@df4deab Xamarin.Android Tools: xamarin/xamarin-android-tools/main@14076a6 Xamarin.iOS and Xamarin.Mac SDK 15.12.0.2 (87f98a75e) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Reproduction Link

Skia TestBed.zip

mgood7123 commented 2 years ago

if i test this in pure native i get SOMETHING ELSE

i expect TWO FIVE FIVE like in https://fiddle.skia.org/c/feaaefb4c89602123b712bd2688c723f

    SkImageInfo info = SkImageInfo::MakeA8(1, 1);
    auto gpuSurface(SkSurface::MakeRenderTarget(AsGrRecordingContext(context), SkBudgeted::kNo, info, 0, kBottomLeft_GrSurfaceOrigin, nullptr, false));
    auto surfaceCanvas = gpuSurface->getCanvas();
    SkColor color;
    color = SkColorSetA(color, 255);
    surfaceCanvas->drawColor(color);
    auto image_(gpuSurface->makeImageSnapshot());
    auto raster = image_->makeRasterImage();
    SkPixmap pixmap;
    raster->peekPixels(&pixmap);
    const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr());
    SkDebugf(pixels == nullptr ? "nullptr\n" : pixels[0] == 255 ? "TWO FIVE FIVE\n" : "SOMETHING ELSE\n");