Commit b30dc0c1 authored by Peng Huang's avatar Peng Huang Committed by Commit Bot

SkiaRenderer: fix crashes when context is lost

When context is lost, the SharedContextState::gr_context_ could be
nullptr, we should check it is not nullptr, before using it to
avoid crashes.

Bug: 1103726
Change-Id: I50a84d3cd2ffff05095ce682f80ddf31d5304d66
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2295683Reviewed-by: default avatarVasiliy Telezhnikov <vasilyt@chromium.org>
Commit-Queue: Peng Huang <penghuang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#787918}
parent 919fb801
...@@ -272,16 +272,19 @@ SkCanvas* SkiaOutputSurfaceImpl::BeginPaintCurrentFrame() { ...@@ -272,16 +272,19 @@ SkCanvas* SkiaOutputSurfaceImpl::BeginPaintCurrentFrame() {
DCHECK(!overdraw_surface_recorder_); DCHECK(!overdraw_surface_recorder_);
DCHECK(debug_settings_->show_overdraw_feedback); DCHECK(debug_settings_->show_overdraw_feedback);
nway_canvas_.emplace(characterization_.width(), characterization_.height());
nway_canvas_->addCanvas(current_paint_->recorder()->getCanvas());
SkSurfaceCharacterization characterization = CreateSkSurfaceCharacterization( SkSurfaceCharacterization characterization = CreateSkSurfaceCharacterization(
gfx::Size(characterization_.width(), characterization_.height()), gfx::Size(characterization_.width(), characterization_.height()),
BGRA_8888, false /* mipmap */, characterization_.refColorSpace(), BGRA_8888, false /* mipmap */, characterization_.refColorSpace(),
false /* is_root_render_pass */); false /* is_root_render_pass */);
overdraw_surface_recorder_.emplace(characterization); if (characterization.isValid()) {
overdraw_canvas_.emplace((overdraw_surface_recorder_->getCanvas())); overdraw_surface_recorder_.emplace(characterization);
overdraw_canvas_.emplace((overdraw_surface_recorder_->getCanvas()));
nway_canvas_->addCanvas(&overdraw_canvas_.value());
}
nway_canvas_.emplace(characterization_.width(), characterization_.height());
nway_canvas_->addCanvas(current_paint_->recorder()->getCanvas());
nway_canvas_->addCanvas(&overdraw_canvas_.value());
return &nway_canvas_.value(); return &nway_canvas_.value();
} }
...@@ -476,10 +479,13 @@ SkCanvas* SkiaOutputSurfaceImpl::BeginPaintRenderPass( ...@@ -476,10 +479,13 @@ SkCanvas* SkiaOutputSurfaceImpl::BeginPaintRenderPass(
DCHECK(!current_paint_); DCHECK(!current_paint_);
DCHECK(resource_sync_tokens_.empty()); DCHECK(resource_sync_tokens_.empty());
SkSurfaceCharacterization c = CreateSkSurfaceCharacterization( SkSurfaceCharacterization characterization = CreateSkSurfaceCharacterization(
surface_size, format, mipmap, std::move(color_space), surface_size, format, mipmap, std::move(color_space),
false /* is_root_render_pass */); false /* is_root_render_pass */);
current_paint_.emplace(c, id); if (!characterization.isValid())
return nullptr;
current_paint_.emplace(characterization, id);
return current_paint_->recorder()->getCanvas(); return current_paint_->recorder()->getCanvas();
} }
...@@ -727,7 +733,6 @@ bool SkiaOutputSurfaceImpl::Initialize() { ...@@ -727,7 +733,6 @@ bool SkiaOutputSurfaceImpl::Initialize() {
damage_of_buffers_.resize(capabilities_.number_of_buffers); damage_of_buffers_.resize(capabilities_.number_of_buffers);
} }
} }
return result; return result;
} }
...@@ -759,6 +764,7 @@ void SkiaOutputSurfaceImpl::InitializeOnGpuThread( ...@@ -759,6 +764,7 @@ void SkiaOutputSurfaceImpl::InitializeOnGpuThread(
} else { } else {
capabilities_ = impl_on_gpu_->capabilities(); capabilities_ = impl_on_gpu_->capabilities();
is_displayed_as_overlay_ = impl_on_gpu_->IsDisplayedAsOverlay(); is_displayed_as_overlay_ = impl_on_gpu_->IsDisplayedAsOverlay();
gr_context_thread_safe_ = impl_on_gpu_->GetGrContextThreadSafeProxy();
*result = true; *result = true;
} }
} }
...@@ -770,7 +776,9 @@ SkiaOutputSurfaceImpl::CreateSkSurfaceCharacterization( ...@@ -770,7 +776,9 @@ SkiaOutputSurfaceImpl::CreateSkSurfaceCharacterization(
bool mipmap, bool mipmap,
sk_sp<SkColorSpace> color_space, sk_sp<SkColorSpace> color_space,
bool is_root_render_pass) { bool is_root_render_pass) {
auto gr_context_thread_safe = impl_on_gpu_->GetGrContextThreadSafeProxy(); if (!gr_context_thread_safe_)
return SkSurfaceCharacterization();
auto cache_max_resource_bytes = impl_on_gpu_->max_resource_cache_bytes(); auto cache_max_resource_bytes = impl_on_gpu_->max_resource_cache_bytes();
// LegacyFontHost will get LCD text and skia figures out what type to use. // LegacyFontHost will get LCD text and skia figures out what type to use.
SkSurfaceProps surface_props(0 /*flags */, SkSurfaceProps surface_props(0 /*flags */,
...@@ -795,7 +803,7 @@ SkiaOutputSurfaceImpl::CreateSkSurfaceCharacterization( ...@@ -795,7 +803,7 @@ SkiaOutputSurfaceImpl::CreateSkSurfaceCharacterization(
DCHECK((capabilities_.uses_default_gl_framebuffer && DCHECK((capabilities_.uses_default_gl_framebuffer &&
dependency_->gr_context_type() == gpu::GrContextType::kGL) || dependency_->gr_context_type() == gpu::GrContextType::kGL) ||
!capabilities_.uses_default_gl_framebuffer); !capabilities_.uses_default_gl_framebuffer);
auto characterization = gr_context_thread_safe->createCharacterization( auto characterization = gr_context_thread_safe_->createCharacterization(
cache_max_resource_bytes, image_info, backend_format, cache_max_resource_bytes, image_info, backend_format,
0 /* sampleCount */, surface_origin, surface_props, mipmap, 0 /* sampleCount */, surface_origin, surface_props, mipmap,
capabilities_.uses_default_gl_framebuffer, false /* isTextureable */, capabilities_.uses_default_gl_framebuffer, false /* isTextureable */,
...@@ -825,14 +833,14 @@ SkiaOutputSurfaceImpl::CreateSkSurfaceCharacterization( ...@@ -825,14 +833,14 @@ SkiaOutputSurfaceImpl::CreateSkSurfaceCharacterization(
auto color_type = auto color_type =
ResourceFormatToClosestSkColorType(true /* gpu_compositing */, format); ResourceFormatToClosestSkColorType(true /* gpu_compositing */, format);
auto backend_format = gr_context_thread_safe->defaultBackendFormat( auto backend_format = gr_context_thread_safe_->defaultBackendFormat(
color_type, GrRenderable::kYes); color_type, GrRenderable::kYes);
DCHECK(backend_format.isValid()); DCHECK(backend_format.isValid());
auto image_info = auto image_info =
SkImageInfo::Make(surface_size.width(), surface_size.height(), color_type, SkImageInfo::Make(surface_size.width(), surface_size.height(), color_type,
kPremul_SkAlphaType, std::move(color_space)); kPremul_SkAlphaType, std::move(color_space));
auto characterization = gr_context_thread_safe->createCharacterization( auto characterization = gr_context_thread_safe_->createCharacterization(
cache_max_resource_bytes, image_info, backend_format, 0 /* sampleCount */, cache_max_resource_bytes, image_info, backend_format, 0 /* sampleCount */,
kTopLeft_GrSurfaceOrigin, surface_props, mipmap, kTopLeft_GrSurfaceOrigin, surface_props, mipmap,
false /* willUseGLFBO0 */, true /* isTextureable */, false /* willUseGLFBO0 */, true /* isTextureable */,
...@@ -1037,6 +1045,7 @@ void SkiaOutputSurfaceImpl::PrepareYUVATextureIndices( ...@@ -1037,6 +1045,7 @@ void SkiaOutputSurfaceImpl::PrepareYUVATextureIndices(
void SkiaOutputSurfaceImpl::ContextLost() { void SkiaOutputSurfaceImpl::ContextLost() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
gr_context_thread_safe_.reset();
for (auto& observer : observers_) for (auto& observer : observers_)
observer.OnContextLost(); observer.OnContextLost();
} }
......
...@@ -268,6 +268,8 @@ class VIZ_SERVICE_EXPORT SkiaOutputSurfaceImpl : public SkiaOutputSurface { ...@@ -268,6 +268,8 @@ class VIZ_SERVICE_EXPORT SkiaOutputSurfaceImpl : public SkiaOutputSurface {
// |impl_on_gpu| is created and destroyed on the GPU thread. // |impl_on_gpu| is created and destroyed on the GPU thread.
std::unique_ptr<SkiaOutputSurfaceImplOnGpu> impl_on_gpu_; std::unique_ptr<SkiaOutputSurfaceImplOnGpu> impl_on_gpu_;
sk_sp<GrContextThreadSafeProxy> gr_context_thread_safe_;
bool has_set_draw_rectangle_for_frame_ = false; bool has_set_draw_rectangle_for_frame_ = false;
base::Optional<gfx::Rect> draw_rectangle_; base::Optional<gfx::Rect> draw_rectangle_;
......
...@@ -1379,7 +1379,7 @@ void SkiaOutputSurfaceImplOnGpu::EndAccessImages( ...@@ -1379,7 +1379,7 @@ void SkiaOutputSurfaceImplOnGpu::EndAccessImages(
sk_sp<GrContextThreadSafeProxy> sk_sp<GrContextThreadSafeProxy>
SkiaOutputSurfaceImplOnGpu::GetGrContextThreadSafeProxy() { SkiaOutputSurfaceImplOnGpu::GetGrContextThreadSafeProxy() {
return gr_context()->threadSafeProxy(); return gr_context() ? gr_context()->threadSafeProxy() : nullptr;
} }
void SkiaOutputSurfaceImplOnGpu::ReleaseImageContexts( void SkiaOutputSurfaceImplOnGpu::ReleaseImageContexts(
......
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