Commit 120338fa authored by Peng Huang's avatar Peng Huang Committed by Commit Bot

Fix a crash in WrappedSkImage and canvas issues in SharedImageAHB

wrapped_sk_image()->GetSkSurface() may return nullptr if the deive is
lost, so we need to check the result before using it to avoid crash.

This CL also improves SharedImageAHB to recreate SkSurface, if surface
properties are changed. And it also resets the canvas when write access
is done.

Bug: None
Change-Id: Ic9a7a966ac7063a74f650c1b04c2ee6ba475d918
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2145467
Auto-Submit: Peng Huang <penghuang@chromium.org>
Reviewed-by: default avatarJonathan Backer <backer@chromium.org>
Commit-Queue: Peng Huang <penghuang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759251}
parent 037de0e2
......@@ -41,6 +41,7 @@
#include "gpu/vulkan/vulkan_image.h"
#include "gpu/vulkan/vulkan_implementation.h"
#include "gpu/vulkan/vulkan_util.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPromiseImageTexture.h"
#include "third_party/skia/include/gpu/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
......@@ -306,16 +307,31 @@ class SharedImageRepresentationSkiaVkAHB
if (!BeginAccess(false /* readonly */, begin_semaphores, end_semaphores))
return nullptr;
if (!surface_) {
auto* gr_context = context_state_->gr_context();
if (gr_context->abandoned()) {
LOG(ERROR) << "GrContext is abandoned.";
return nullptr;
}
if (!surface_ || final_msaa_count != surface_msaa_count_ ||
surface_props != surface_->props()) {
SkColorType sk_color_type = viz::ResourceFormatToClosestSkColorType(
/*gpu_compositing=*/true, format());
surface_ = SkSurface::MakeFromBackendTextureAsRenderTarget(
context_state_->gr_context(), promise_texture_->backendTexture(),
gr_context, promise_texture_->backendTexture(),
kTopLeft_GrSurfaceOrigin, final_msaa_count, sk_color_type,
color_space().ToSkColorSpace(), &surface_props);
if (!surface_) {
LOG(ERROR) << "MakeFromBackendTextureAsRenderTarget() failed.";
return nullptr;
}
surface_msaa_count_ = final_msaa_count;
}
DCHECK(surface_);
int count = surface_->getCanvas()->save();
DCHECK_EQ(count, 1);
ALLOW_UNUSED_LOCAL(count);
return surface_;
}
......@@ -325,7 +341,7 @@ class SharedImageRepresentationSkiaVkAHB
surface.reset();
DCHECK(surface_->unique());
surface_->getCanvas()->restoreToCount(1);
EndAccess(false /* readonly */);
}
......@@ -451,6 +467,7 @@ class SharedImageRepresentationSkiaVkAHB
std::unique_ptr<VulkanImage> vulkan_image_;
sk_sp<SkPromiseImageTexture> promise_texture_;
RepresentationAccessMode mode_ = RepresentationAccessMode::kNone;
int surface_msaa_count_ = 0;
sk_sp<SkSurface> surface_;
scoped_refptr<SharedContextState> context_state_;
VkSemaphore end_access_semaphore_ = VK_NULL_HANDLE;
......
......@@ -87,13 +87,16 @@ class WrappedSkImage : public ClearTrackingSharedImageBacking {
DCHECK(context_state_->IsCurrent(nullptr));
if (!surface_ || final_msaa_count != surface_msaa_count_ ||
surface_props != *surface_props_) {
surface_msaa_count_ = final_msaa_count;
surface_props_.emplace(surface_props);
surface_props != surface_->props()) {
surface_ = SkSurface::MakeFromBackendTexture(
context_state_->gr_context(), backend_texture_,
kTopLeft_GrSurfaceOrigin, surface_msaa_count_, GetSkColorType(),
color_space().ToSkColorSpace(), &surface_props_.value());
color_space().ToSkColorSpace(), &surface_props);
if (!surface_) {
LOG(ERROR) << "MakeFromBackendTexture() failed.";
return nullptr;
}
surface_msaa_count_ = final_msaa_count;
}
return surface_;
}
......@@ -231,7 +234,6 @@ class WrappedSkImage : public ClearTrackingSharedImageBacking {
sk_sp<SkPromiseImageTexture> promise_texture_;
sk_sp<SkSurface> surface_;
int surface_msaa_count_ = 0;
base::Optional<SkSurfaceProps> surface_props_;
uint64_t tracing_id_ = 0;
......@@ -254,6 +256,8 @@ class WrappedSkImageRepresentation : public SharedImageRepresentationSkia {
std::vector<GrBackendSemaphore>* end_semaphores) override {
auto surface =
wrapped_sk_image()->GetSkSurface(final_msaa_count, surface_props);
if (!surface)
return nullptr;
int save_count = surface->getCanvas()->save();
ALLOW_UNUSED_LOCAL(save_count);
DCHECK_EQ(1, save_count);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment