From f2de1b8b4dcf2d3223ac26224fa74880f1c215e3 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Fri, 16 Jul 2021 10:12:24 -0400 Subject: [PATCH] Fix crash when drawing a GPU-backed surface onto raster. We now null-check the result of recordingContext() before using it. Thanks cqjjjzr2@gmail.com for the initial discovery and suggested fix. Change-Id: Iafcf306f8cd9deb7147ef9c5c71a21907eedc01f Bug: skia:12214 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/429098 Commit-Queue: John Stiles Auto-Submit: John Stiles Reviewed-by: Robert Phillips --- gn/tests.gni | 1 + src/image/SkSurface_Gpu.cpp | 2 +- tests/Skbug12214.cpp | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/Skbug12214.cpp diff --git a/gn/tests.gni b/gn/tests.gni index 58434def0e..24de8b8b27 100644 --- a/gn/tests.gni +++ b/gn/tests.gni @@ -271,6 +271,7 @@ tests_sources = [ "$_tests/SkUTFTest.cpp", "$_tests/SkVMTest.cpp", "$_tests/SkVxTest.cpp", + "$_tests/Skbug12214.cpp", "$_tests/Skbug5221.cpp", "$_tests/Skbug6389.cpp", "$_tests/Skbug6653.cpp", diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp index 3211ccb0a0..104032f73d 100644 --- a/src/image/SkSurface_Gpu.cpp +++ b/src/image/SkSurface_Gpu.cpp @@ -275,7 +275,7 @@ void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, // onDraw) since that may not always perform the copy-on-write optimization. auto tryDraw = [&] { auto surfaceContext = fDevice->recordingContext(); - auto canvasContext = canvas->recordingContext()->asDirectContext(); + auto canvasContext = GrAsDirectContext(canvas->recordingContext()); if (!canvasContext) { return false; } diff --git a/tests/Skbug12214.cpp b/tests/Skbug12214.cpp new file mode 100644 index 0000000000..aac4f016cd --- /dev/null +++ b/tests/Skbug12214.cpp @@ -0,0 +1,23 @@ +/* + * Copyright 2021 Google LLC + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "tests/Test.h" + +#include "include/core/SkCanvas.h" +#include "include/core/SkSurface.h" +#include "include/gpu/GrDirectContext.h" + +DEF_GPUTEST_FOR_ALL_CONTEXTS(skbug12214, r, contextInfo) { + auto imageInfo = SkImageInfo::Make(/*width=*/32, /*height=*/32, kRGBA_8888_SkColorType, + kPremul_SkAlphaType); + sk_sp surface1 = SkSurface::MakeRenderTarget(contextInfo.directContext(), + SkBudgeted::kNo, imageInfo); + sk_sp surface2 = SkSurface::MakeRaster(imageInfo); + + // The test succeeds if this draw does not crash. (See skia:12214) + surface1->draw(surface2->getCanvas(), /*x=*/0, /*y=*/0); +}