diff --git a/engine/src/flutter/flow/raster_cache.cc b/engine/src/flutter/flow/raster_cache.cc index 716a85534a..2b31577d3d 100644 --- a/engine/src/flutter/flow/raster_cache.cc +++ b/engine/src/flutter/flow/raster_cache.cc @@ -11,6 +11,7 @@ #include "flutter/glue/trace_event.h" #include "lib/fxl/logging.h" #include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkColorSpaceXformCanvas.h" #include "third_party/skia/include/core/SkImage.h" #include "third_party/skia/include/core/SkPicture.h" #include "third_party/skia/include/core/SkSurface.h" @@ -95,9 +96,8 @@ RasterCacheResult RasterizePicture(SkPicture* picture, std::fabs(logical_rect.height() * metrics_scale_y * scale.y())); const SkImageInfo image_info = SkImageInfo::MakeN32Premul( - std::ceil(physical_rect.width()), // physical width - std::ceil(physical_rect.height()), // physical height - sk_ref_sp(dst_color_space) // colorspace + std::ceil(physical_rect.width()), // physical width + std::ceil(physical_rect.height()) // physical height ); sk_sp surface = @@ -110,6 +110,14 @@ RasterCacheResult RasterizePicture(SkPicture* picture, } SkCanvas* canvas = surface->getCanvas(); + std::unique_ptr xformCanvas; + if (dst_color_space) { + xformCanvas = SkCreateColorSpaceXformCanvas(surface->getCanvas(), + sk_ref_sp(dst_color_space)); + if (xformCanvas) { + canvas = xformCanvas.get(); + } + } canvas->clear(SK_ColorTRANSPARENT); canvas->scale(std::abs(scale.x() * metrics_scale_x), diff --git a/engine/src/flutter/lib/ui/painting/codec.cc b/engine/src/flutter/lib/ui/painting/codec.cc index 7b06786ce6..fde62f49c7 100644 --- a/engine/src/flutter/lib/ui/painting/codec.cc +++ b/engine/src/flutter/lib/ui/painting/codec.cc @@ -55,8 +55,8 @@ sk_sp DecodeImage(sk_sp buffer, size_t trace_id) { GrContext* context = ResourceContext::Get(); if (context) { - // This acts as a flag to indicate that we want a color space aware decode. - sk_sp dstColorSpace = SkColorSpace::MakeSRGB(); + // This indicates that we do not want a "linear blending" decode. + sk_sp dstColorSpace = nullptr; return SkImage::MakeCrossContextFromEncoded(context, std::move(buffer), false, dstColorSpace.get()); } else { @@ -241,8 +241,8 @@ sk_sp MultiFrameCodec::GetNextFrameImage() { if (context) { SkPixmap pixmap(bitmap.info(), bitmap.pixelRef()->pixels(), bitmap.pixelRef()->rowBytes()); - // This acts as a flag to indicate that we want a color space aware decode. - sk_sp dstColorSpace = SkColorSpace::MakeSRGB(); + // This indicates that we do not want a "linear blending" decode. + sk_sp dstColorSpace = nullptr; return SkImage::MakeCrossContextFromPixmap(context, pixmap, false, dstColorSpace.get()); } else { diff --git a/engine/src/flutter/lib/ui/painting/image_decoding.cc b/engine/src/flutter/lib/ui/painting/image_decoding.cc index fdcff961b2..b909e60d8e 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoding.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoding.cc @@ -35,8 +35,8 @@ sk_sp DecodeImage(sk_sp buffer, size_t trace_id) { GrContext* context = ResourceContext::Get(); if (context) { - // This acts as a flag to indicate that we want a color space aware decode. - sk_sp dstColorSpace = SkColorSpace::MakeSRGB(); + // This indicates that we do not want a "linear blending" decode. + sk_sp dstColorSpace = nullptr; return SkImage::MakeCrossContextFromEncoded(context, std::move(buffer), false, dstColorSpace.get()); } else { diff --git a/engine/src/flutter/shell/common/platform_view.cc b/engine/src/flutter/shell/common/platform_view.cc index eaa293634e..6d3d3f61e2 100644 --- a/engine/src/flutter/shell/common/platform_view.cc +++ b/engine/src/flutter/shell/common/platform_view.cc @@ -189,7 +189,6 @@ void PlatformView::SetupResourceContextOnIOThreadPerform( // other threads correctly, so the textures end up blank. For now, suppress // that feature, which will cause texture uploads to do CPU YUV conversion. options.fDisableGpuYUVConversion = true; - options.fRequireDecodeDisableForSRGB = false; blink::ResourceContext::Set(GrContext::Create( GrBackend::kOpenGL_GrBackend, diff --git a/engine/src/flutter/shell/common/surface.cc b/engine/src/flutter/shell/common/surface.cc index a9153ebab0..01d288a5e5 100644 --- a/engine/src/flutter/shell/common/surface.cc +++ b/engine/src/flutter/shell/common/surface.cc @@ -4,6 +4,7 @@ #include "flutter/shell/common/surface.h" #include "lib/fxl/logging.h" +#include "third_party/skia/include/core/SkColorSpaceXformCanvas.h" #include "third_party/skia/include/core/SkSurface.h" namespace shell { @@ -12,6 +13,10 @@ SurfaceFrame::SurfaceFrame(sk_sp surface, SubmitCallback submit_callback) : submitted_(false), surface_(surface), submit_callback_(submit_callback) { FXL_DCHECK(submit_callback_); + if (surface_) { + xform_canvas_ = SkCreateColorSpaceXformCanvas(surface_->getCanvas(), + SkColorSpace::MakeSRGB()); + } } SurfaceFrame::~SurfaceFrame() { @@ -32,6 +37,9 @@ bool SurfaceFrame::Submit() { } SkCanvas* SurfaceFrame::SkiaCanvas() { + if (xform_canvas_) { + return xform_canvas_.get(); + } return surface_ != nullptr ? surface_->getCanvas() : nullptr; } diff --git a/engine/src/flutter/shell/common/surface.h b/engine/src/flutter/shell/common/surface.h index 4d9b33b733..906480237f 100644 --- a/engine/src/flutter/shell/common/surface.h +++ b/engine/src/flutter/shell/common/surface.h @@ -33,6 +33,7 @@ class SurfaceFrame { private: bool submitted_; sk_sp surface_; + std::unique_ptr xform_canvas_; SubmitCallback submit_callback_; bool PerformSubmit(); diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc index 66409e8a8d..92cfd25f19 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc @@ -33,11 +33,8 @@ GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate) auto backend_context = reinterpret_cast(GrGLCreateNativeInterface()); - GrContextOptions options; - options.fRequireDecodeDisableForSRGB = false; - - auto context = sk_sp( - GrContext::Create(kOpenGL_GrBackend, backend_context, options)); + auto context = + sk_sp(GrContext::Create(kOpenGL_GrBackend, backend_context)); if (context == nullptr) { FXL_LOG(ERROR) << "Failed to setup Skia Gr context."; @@ -65,7 +62,6 @@ GPUSurfaceGL::~GPUSurfaceGL() { } onscreen_surface_ = nullptr; - offscreen_surface_ = nullptr; context_->releaseResourcesAndAbandonContext(); context_ = nullptr; @@ -76,7 +72,7 @@ bool GPUSurfaceGL::IsValid() { return valid_; } -static GrPixelConfig FirstSupportedNonSRGBConfig(GrContext* context) { +static GrPixelConfig FirstSupportedConfig(GrContext* context) { #define RETURN_IF_RENDERABLE(x) \ if (context->caps()->isConfigRenderable((x), false)) { \ return (x); \ @@ -90,15 +86,12 @@ static GrPixelConfig FirstSupportedNonSRGBConfig(GrContext* context) { static sk_sp WrapOnscreenSurface(GrContext* context, const SkISize& size, - intptr_t fbo, - bool supports_srgb) { + intptr_t fbo) { const GrGLFramebufferInfo framebuffer_info = { .fFBOID = static_cast(fbo), }; - const GrPixelConfig pixel_config = supports_srgb - ? kSRGBA_8888_GrPixelConfig - : FirstSupportedNonSRGBConfig(context); + const GrPixelConfig pixel_config = FirstSupportedConfig(context); GrBackendRenderTarget render_target(size.fWidth, // width size.fHeight, // height @@ -108,8 +101,7 @@ static sk_sp WrapOnscreenSurface(GrContext* context, framebuffer_info // framebuffer info ); - sk_sp colorspace = - supports_srgb ? SkColorSpace::MakeSRGB() : nullptr; + sk_sp colorspace = nullptr; SkSurfaceProps surface_props( SkSurfaceProps::InitType::kLegacyFontHost_InitType); @@ -123,52 +115,29 @@ static sk_sp WrapOnscreenSurface(GrContext* context, ); } -static sk_sp CreateOffscreenSurface(GrContext* context, - const SkISize& size) { - const SkImageInfo image_info = - SkImageInfo::MakeS32(size.fWidth, size.fHeight, kOpaque_SkAlphaType); - - const SkSurfaceProps surface_props( - SkSurfaceProps::InitType::kLegacyFontHost_InitType); - - return SkSurface::MakeRenderTarget( - context, // context - SkBudgeted::kNo, // budgeted - image_info, // image info - 0, // sample count - kBottomLeft_GrSurfaceOrigin, // surface origin - &surface_props // surface props - ); -} - bool GPUSurfaceGL::CreateOrUpdateSurfaces(const SkISize& size) { if (onscreen_surface_ != nullptr && size == SkISize::Make(onscreen_surface_->width(), onscreen_surface_->height())) { - // We know that if there is an offscreen surface, it will be sized to be - // equal to the size of the onscreen surface. And the onscreen surface size - // appears unchanged. So bail. + // Surface size appears unchanged. So bail. return true; } // We need to do some updates. TRACE_EVENT0("flutter", "UpdateSurfacesSize"); - // Either way, we need to get rid of previous surfaces. + // Either way, we need to get rid of previous surface. onscreen_surface_ = nullptr; - offscreen_surface_ = nullptr; if (size.isEmpty()) { FXL_LOG(ERROR) << "Cannot create surfaces of empty size."; return false; } - sk_sp onscreen_surface, offscreen_surface; + sk_sp onscreen_surface; - const bool surface_supports_srgb = delegate_->SurfaceSupportsSRGB(); - - onscreen_surface = WrapOnscreenSurface( - context_.get(), size, delegate_->GLContextFBO(), surface_supports_srgb); + onscreen_surface = + WrapOnscreenSurface(context_.get(), size, delegate_->GLContextFBO()); if (onscreen_surface == nullptr) { // If the onscreen surface could not be wrapped. There is absolutely no @@ -177,27 +146,7 @@ bool GPUSurfaceGL::CreateOrUpdateSurfaces(const SkISize& size) { return false; } - if (!surface_supports_srgb) { - offscreen_surface = CreateOffscreenSurface(context_.get(), size); - if (offscreen_surface == nullptr) { - // If the offscreen surface was needed but could not be wrapped. Render to - // the onscreen surface directly but warn the user that color correctness - // is not available. - static bool warned_once = false; - if (!warned_once) { - warned_once = true; - FXL_LOG(ERROR) << "WARNING: Could not create offscreen surface. This " - "device or emulator does not support " - "color correct rendering. Fallbacks are in effect. " - "Colors on this device will differ from those " - "displayed on most other devices. This warning will " - "only be logged once."; - } - } - } - onscreen_surface_ = std::move(onscreen_surface); - offscreen_surface_ = std::move(offscreen_surface); return true; } @@ -234,23 +183,6 @@ bool GPUSurfaceGL::PresentSurface(SkCanvas* canvas) { return false; } - if (offscreen_surface_ != nullptr) { - // Because the surface did not support sRGB, we rendered to an offscreen - // surface. Now we must ensure that the texture is copied onscreen. - TRACE_EVENT0("flutter", "CopyTextureOnscreen"); - SkPaint paint; - const GrCaps* caps = context_->caps(); - if (caps->srgbSupport() && !caps->srgbDecodeDisableSupport()) { - paint.setColorFilter(SkColorFilter::MakeLinearToSRGBGamma()); - } - onscreen_surface_->getCanvas()->drawImage( - offscreen_surface_->makeImageSnapshot(), // image - 0, // left - 0, // top - &paint // paint - ); - } - { TRACE_EVENT0("flutter", "SkCanvas::Flush"); onscreen_surface_->getCanvas()->flush(); @@ -266,7 +198,7 @@ sk_sp GPUSurfaceGL::AcquireRenderSurface(const SkISize& size) { return nullptr; } - return offscreen_surface_ != nullptr ? offscreen_surface_ : onscreen_surface_; + return onscreen_surface_; } GrContext* GPUSurfaceGL::GetContext() { diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.h b/engine/src/flutter/shell/gpu/gpu_surface_gl.h index 3938a0845a..8a29f09cfc 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.h @@ -22,8 +22,6 @@ class GPUSurfaceGLDelegate { virtual bool GLContextPresent() = 0; virtual intptr_t GLContextFBO() const = 0; - - virtual bool SurfaceSupportsSRGB() const = 0; }; class GPUSurfaceGL : public Surface { @@ -42,7 +40,6 @@ class GPUSurfaceGL : public Surface { GPUSurfaceGLDelegate* delegate_; sk_sp context_; sk_sp onscreen_surface_; - sk_sp offscreen_surface_; bool valid_ = false; fml::WeakPtrFactory weak_factory_; diff --git a/engine/src/flutter/shell/platform/android/android_context_gl.cc b/engine/src/flutter/shell/platform/android/android_context_gl.cc index 45b5ee9a8d..f54629f864 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl.cc +++ b/engine/src/flutter/shell/platform/android/android_context_gl.cc @@ -6,14 +6,6 @@ #include #include -#ifndef EGL_GL_COLORSPACE_KHR -#define EGL_GL_COLORSPACE_KHR 0x309D -#endif - -#ifndef EGL_GL_COLORSPACE_SRGB_KHR -#define EGL_GL_COLORSPACE_SRGB_KHR 0x3089 -#endif - namespace shell { template @@ -128,14 +120,7 @@ bool AndroidContextGL::CreateWindowSurface( window_ = std::move(window); EGLDisplay display = environment_->Display(); - const EGLint srgb_attribs[] = {EGL_GL_COLORSPACE_KHR, - EGL_GL_COLORSPACE_SRGB_KHR, EGL_NONE}; - const EGLint default_attribs[] = {EGL_NONE}; - - const EGLint* attribs = default_attribs; - if (srgb_support_) { - attribs = srgb_attribs; - } + const EGLint attribs[] = {EGL_NONE}; surface_ = eglCreateWindowSurface( display, config_, @@ -150,19 +135,7 @@ bool AndroidContextGL::CreatePBufferSurface() { EGLDisplay display = environment_->Display(); - const EGLint srgb_attribs[] = {EGL_WIDTH, - 1, - EGL_HEIGHT, - 1, - EGL_GL_COLORSPACE_KHR, - EGL_GL_COLORSPACE_SRGB_KHR, - EGL_NONE}; - const EGLint default_attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; - - const EGLint* attribs = default_attribs; - if (srgb_support_) { - attribs = srgb_attribs; - } + const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; surface_ = eglCreatePbufferSurface(display, config_, attribs); return surface_ != EGL_NO_SURFACE; @@ -206,12 +179,6 @@ AndroidContextGL::AndroidContextGL(fxl::RefPtr env, return; } - // On its own, this is not enough to guarantee that we will render in - // sRGB mode. We also need to query GL using the GrContext. - - const char* exts = eglQueryString(environment_->Display(), EGL_EXTENSIONS); - srgb_support_ = strstr(exts, "EGL_KHR_gl_colorspace"); - if (!this->CreatePBufferSurface()) { FXL_LOG(ERROR) << "Could not create the EGL surface."; LogLastEGLError(); @@ -302,8 +269,4 @@ bool AndroidContextGL::Resize(const SkISize& size) { return true; } -bool AndroidContextGL::SupportsSRGB() const { - return srgb_support_; -} - } // namespace shell diff --git a/engine/src/flutter/shell/platform/android/android_context_gl.h b/engine/src/flutter/shell/platform/android/android_context_gl.h index 89f02f52cf..207f621c66 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl.h +++ b/engine/src/flutter/shell/platform/android/android_context_gl.h @@ -35,15 +35,12 @@ class AndroidContextGL : public fxl::RefCountedThreadSafe { bool Resize(const SkISize& size); - bool SupportsSRGB() const; - private: fxl::RefPtr environment_; fxl::RefPtr window_; EGLConfig config_; EGLSurface surface_; EGLContext context_; - bool srgb_support_; bool valid_; AndroidContextGL(fxl::RefPtr env, diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl.cc b/engine/src/flutter/shell/platform/android/android_surface_gl.cc index ebaa9c730a..f30ac1272b 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.cc @@ -55,10 +55,6 @@ AndroidSurfaceGL::AndroidSurfaceGL( AndroidSurfaceGL::~AndroidSurfaceGL() = default; -bool AndroidSurfaceGL::SurfaceSupportsSRGB() const { - return offscreen_context_->SupportsSRGB(); -} - bool AndroidSurfaceGL::IsOffscreenContextValid() const { return offscreen_context_ && offscreen_context_->IsValid(); } diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl.h b/engine/src/flutter/shell/platform/android/android_surface_gl.h index 671c665564..08aee081c8 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.h +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.h @@ -47,8 +47,6 @@ class AndroidSurfaceGL : public GPUSurfaceGLDelegate, public AndroidSurface { intptr_t GLContextFBO() const override; - bool SurfaceSupportsSRGB() const override; - private: fxl::RefPtr onscreen_context_; fxl::RefPtr offscreen_context_; diff --git a/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.h b/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.h index bf0bd3a955..737f04cd77 100644 --- a/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.h +++ b/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.h @@ -33,8 +33,6 @@ class PlatformViewMac : public PlatformView, public GPUSurfaceGLDelegate { intptr_t GLContextFBO() const override; - bool SurfaceSupportsSRGB() const override; - VsyncWaiter* GetVsyncWaiter() override; bool ResourceContextMakeCurrent() override; diff --git a/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.mm b/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.mm index 0faf1a2ce1..ce6ed8d544 100644 --- a/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.mm +++ b/engine/src/flutter/shell/platform/darwin/desktop/platform_view_mac.mm @@ -77,10 +77,6 @@ intptr_t PlatformViewMac::GLContextFBO() const { return 0; } -bool PlatformViewMac::SurfaceSupportsSRGB() const { - return false; -} - bool PlatformViewMac::GLContextMakeCurrent() { TRACE_EVENT0("flutter", "PlatformViewMac::GLContextMakeCurrent"); if (!IsValid()) { diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_gl_context.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_gl_context.mm index dc018a0eb5..b536294e00 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_gl_context.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_gl_context.mm @@ -122,7 +122,7 @@ IOSGLContext::IOSGLContext(PlatformView::SurfaceConfig config, CAEAGLLayer* laye } } - NSString* drawableColorFormat = kEAGLColorFormatSRGBA8; + NSString* drawableColorFormat = kEAGLColorFormatRGBA8; layer_.get().drawableProperties = @{ kEAGLDrawablePropertyColorFormat : drawableColorFormat, kEAGLDrawablePropertyRetainedBacking : @(NO), diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h index 98f476600b..48b8a67018 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h @@ -36,8 +36,6 @@ class IOSSurfaceGL : public IOSSurface, public GPUSurfaceGLDelegate { intptr_t GLContextFBO() const override; - bool SurfaceSupportsSRGB() const override; - private: IOSGLContext context_; diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm index 93aa3c9ddf..d152521c3f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm @@ -36,10 +36,6 @@ intptr_t IOSSurfaceGL::GLContextFBO() const { return IsValid() ? context_.framebuffer() : GL_NONE; } -bool IOSSurfaceGL::SurfaceSupportsSRGB() const { - return true; -} - bool IOSSurfaceGL::GLContextMakeCurrent() { return IsValid() ? context_.MakeCurrent() : false; } diff --git a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc index 9790eb5cbd..d69525b0ba 100644 --- a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc @@ -31,10 +31,6 @@ intptr_t PlatformViewEmbedder::GLContextFBO() const { return dispatch_table_.gl_fbo_callback(); } -bool PlatformViewEmbedder::SurfaceSupportsSRGB() const { - return true; -} - void PlatformViewEmbedder::Attach() { CreateEngine(); NotifyCreated(std::make_unique(this)); diff --git a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h index 1cf780d536..75eea0e299 100644 --- a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h +++ b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h @@ -36,9 +36,6 @@ class PlatformViewEmbedder : public PlatformView, public GPUSurfaceGLDelegate { // |shell::GPUSurfaceGLDelegate| intptr_t GLContextFBO() const override; - // |shell::GPUSurfaceGLDelegate| - bool SurfaceSupportsSRGB() const override; - // |shell::PlatformView| void Attach() override;