From 51be21eb5aededc4f47fe969d0b77d6c272ea64e Mon Sep 17 00:00:00 2001 From: Robert Phillips Date: Tue, 22 Mar 2022 11:43:52 -0400 Subject: [PATCH] [graphite] Add makeTextureImage call to graphitestart GM Bug: skia:12701 Change-Id: I8cca9d8ea20ed585d0021e8ea929dc136d699883 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/523423 Reviewed-by: Jim Van Verth Commit-Queue: Robert Phillips --- gm/graphitestart.cpp | 47 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/gm/graphitestart.cpp b/gm/graphitestart.cpp index e1b3a831ff..8465dc85e7 100644 --- a/gm/graphitestart.cpp +++ b/gm/graphitestart.cpp @@ -11,10 +11,43 @@ #include "include/core/SkImage.h" #include "include/core/SkPaint.h" #include "include/effects/SkGradientShader.h" +#include "include/gpu/GrRecordingContext.h" #include "tools/Resources.h" namespace { +class MetaContext { +public: + MetaContext(SkCanvas* canvas) { +#ifdef SK_GRAPHITE_ENABLED + fRecorder = canvas->recorder(); +#endif + fContext = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext() + : nullptr; + } + + sk_sp makeTextureImage(sk_sp orig) const { +#ifdef SK_GRAPHITE_ENABLED + if (fRecorder) { + return orig->makeTextureImage(fRecorder); + } +#endif + + if (fContext) { + return orig->makeTextureImage(fContext); + } + + return orig; + } + +private: +#ifdef SK_GRAPHITE_ENABLED + skgpu::Recorder* fRecorder; +#endif + + GrDirectContext* fContext; +}; + sk_sp create_gradient_shader(SkRect r) { // TODO: it seems like only the x-component of sk_FragCoord is making it to the shader! SkPoint pts[2] = { {r.fLeft, r.fTop}, {r.fRight, r.fTop} }; @@ -25,7 +58,7 @@ sk_sp create_gradient_shader(SkRect r) { SkTileMode::kClamp); } -sk_sp create_image_shader() { +sk_sp create_image_shader(const MetaContext& context) { SkImageInfo ii = SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType); SkBitmap bitmap; @@ -42,16 +75,16 @@ sk_sp create_image_shader() { bitmap.setImmutable(); sk_sp img = SkImage::MakeFromBitmap(bitmap); - // TODO: we'll need a 'makeTextureImage' call here + img = context.makeTextureImage(std::move(img)); return img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions()); } -sk_sp create_blend_shader(SkBlendMode bm) { +sk_sp create_blend_shader(const MetaContext& context, SkBlendMode bm) { constexpr SkColor4f kTransYellow = {1.0f, 1.0f, 0.0f, 0.5f}; sk_sp dst = SkShaders::Color(kTransYellow, nullptr); - return SkShaders::Blend(bm, std::move(dst), create_image_shader()); + return SkShaders::Blend(bm, std::move(dst), create_image_shader(context)); } void draw_blend_mode_swatches(SkCanvas* canvas, SkRect clipRect) { @@ -119,6 +152,8 @@ protected: void onDraw(SkCanvas* canvas) override { + MetaContext context(canvas); + // UL corner { SkPaint p; @@ -137,14 +172,14 @@ protected: // LL corner { SkPaint p; - p.setShader(create_image_shader()); + p.setShader(create_image_shader(context)); canvas->drawRect({2, 129, 127, 255}, p); } // LR corner { SkPaint p; - p.setShader(create_blend_shader(SkBlendMode::kModulate)); + p.setShader(create_blend_shader(context, SkBlendMode::kModulate)); canvas->drawRect({129, 129, 255, 255}, p); }