Add color space xform to GrAlphaThresholdFragmentProcessor
authorBrian Osman <brianosman@google.com>
Mon, 3 Oct 2016 17:15:12 +0000 (13:15 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Mon, 3 Oct 2016 17:56:31 +0000 (17:56 +0000)
Fix imagealphathreshold_surface GM to test gamut conversion.

BUG=skia:

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2867

Change-Id: Id9aaebe72d1dadc613ef1a8d17d066b51049300f
Reviewed-on: https://skia-review.googlesource.com/2867
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>

gm/imagealphathreshold.cpp
src/effects/GrAlphaThresholdFragmentProcessor.cpp
src/effects/GrAlphaThresholdFragmentProcessor.h
src/effects/SkAlphaThresholdFilter.cpp

index 97524ce..1e90f76 100644 (file)
@@ -104,7 +104,7 @@ protected:
     }
 
     void onDraw(SkCanvas* canvas) override {
-        SkImageInfo info = SkImageInfo::MakeN32(WIDTH, HEIGHT, kOpaque_SkAlphaType);
+        SkImageInfo info = SkImageInfo::MakeS32(WIDTH, HEIGHT, kOpaque_SkAlphaType);
         auto surface(canvas->makeSurface(info));
         if (nullptr == surface) {
             surface = SkSurface::MakeRaster(info);
index 3559694..a35dc6d 100644 (file)
 #include "GrTextureAccess.h"
 #include "SkRefCnt.h"
 
+#include "glsl/GrGLSLColorSpaceXformHelper.h"
 #include "glsl/GrGLSLFragmentProcessor.h"
 #include "glsl/GrGLSLFragmentShaderBuilder.h"
 #include "glsl/GrGLSLUniformHandler.h"
 
-sk_sp<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::Make(GrTexture* texture,
-                                                                   GrTexture* maskTexture,
-                                                                   float innerThreshold,
-                                                                   float outerThreshold,
-                                                                   const SkIRect& bounds) {
+sk_sp<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::Make(
+                                                           GrTexture* texture,
+                                                           sk_sp<GrColorSpaceXform> colorSpaceXform,
+                                                           GrTexture* maskTexture,
+                                                           float innerThreshold,
+                                                           float outerThreshold,
+                                                           const SkIRect& bounds) {
     return sk_sp<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(
-                                                    texture, maskTexture,
-                                                    innerThreshold, outerThreshold,
-                                                    bounds));
+                                                                texture, std::move(colorSpaceXform),
+                                                                maskTexture,
+                                                                innerThreshold, outerThreshold,
+                                                                bounds));
 }
 
 static SkMatrix make_div_and_translate_matrix(GrTexture* texture, int x, int y) {
@@ -34,17 +38,20 @@ static SkMatrix make_div_and_translate_matrix(GrTexture* texture, int x, int y)
     return matrix;
 }
 
-GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(GrTexture* texture,
-                                                                     GrTexture* maskTexture,
-                                                                     float innerThreshold,
-                                                                     float outerThreshold,
-                                                                     const SkIRect& bounds)
+GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(
+                                                           GrTexture* texture,
+                                                           sk_sp<GrColorSpaceXform> colorSpaceXform,
+                                                           GrTexture* maskTexture,
+                                                           float innerThreshold,
+                                                           float outerThreshold,
+                                                           const SkIRect& bounds)
     : fInnerThreshold(innerThreshold)
     , fOuterThreshold(outerThreshold)
     , fImageCoordTransform(kLocal_GrCoordSet,
                            GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture,
                            GrTextureParams::kNone_FilterMode)
     , fImageTextureAccess(texture)
+    , fColorSpaceXform(std::move(colorSpaceXform))
     , fMaskCoordTransform(kLocal_GrCoordSet,
                           make_div_and_translate_matrix(maskTexture, -bounds.x(), -bounds.y()),
                           maskTexture,
@@ -79,13 +86,20 @@ class GrGLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor {
 public:
     void emitCode(EmitArgs&) override;
 
+    static inline void GenKey(const GrProcessor& effect, const GrGLSLCaps&,
+                              GrProcessorKeyBuilder* b) {
+        const GrAlphaThresholdFragmentProcessor& atfp =
+            effect.cast<GrAlphaThresholdFragmentProcessor>();
+        b->add32(GrColorSpaceXform::XformKey(atfp.colorSpaceXform()));
+    }
+
 protected:
     void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
 
 private:
     GrGLSLProgramDataManager::UniformHandle fInnerThresholdVar;
     GrGLSLProgramDataManager::UniformHandle fOuterThresholdVar;
-
+    GrGLSLProgramDataManager::UniformHandle fColorSpaceXformVar;
     typedef GrGLSLFragmentProcessor INHERITED;
 };
 
@@ -98,6 +112,11 @@ void GrGLAlphaThresholdFragmentProcessor::emitCode(EmitArgs& args) {
                                                     kFloat_GrSLType, kDefault_GrSLPrecision,
                                                     "outer_threshold");
 
+    const GrAlphaThresholdFragmentProcessor& atfp =
+        args.fFp.cast<GrAlphaThresholdFragmentProcessor>();
+    GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, atfp.colorSpaceXform(),
+                                                 &fColorSpaceXformVar);
+
     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
     SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
     SkString maskCoords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]);
@@ -105,7 +124,8 @@ void GrGLAlphaThresholdFragmentProcessor::emitCode(EmitArgs& args) {
     fragBuilder->codeAppendf("vec2 coord = %s;", coords2D.c_str());
     fragBuilder->codeAppendf("vec2 mask_coord = %s;", maskCoords2D.c_str());
     fragBuilder->codeAppend("vec4 input_color = ");
-    fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord");
+    fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord", kVec2f_GrSLType,
+                                     &colorSpaceHelper);
     fragBuilder->codeAppend(";");
     fragBuilder->codeAppend("vec4 mask_color = ");
     fragBuilder->appendTextureLookup(args.fTexSamplers[1], "mask_coord");
@@ -139,6 +159,9 @@ void GrGLAlphaThresholdFragmentProcessor::onSetData(const GrGLSLProgramDataManag
     const GrAlphaThresholdFragmentProcessor& atfp = proc.cast<GrAlphaThresholdFragmentProcessor>();
     pdman.set1f(fInnerThresholdVar, atfp.innerThreshold());
     pdman.set1f(fOuterThresholdVar, atfp.outerThreshold());
+    if (SkToBool(atfp.colorSpaceXform())) {
+        pdman.setSkMatrix44(fColorSpaceXformVar, atfp.colorSpaceXform()->srcToDst());
+    }
 }
 
 /////////////////////////////////////////////////////////////////////
@@ -157,7 +180,7 @@ sk_sp<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(GrProce
     uint32_t x = d->fRandom->nextULessThan(kMaxWidth - width);
     uint32_t y = d->fRandom->nextULessThan(kMaxHeight - height);
     SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
-    return GrAlphaThresholdFragmentProcessor::Make(bmpTex, maskTex,
+    return GrAlphaThresholdFragmentProcessor::Make(bmpTex, nullptr, maskTex,
                                                    innerThresh, outerThresh,
                                                    bounds);
 }
index 5160291..c5b8d4e 100644 (file)
@@ -12,6 +12,7 @@
 
 #if SK_SUPPORT_GPU
 
+#include "GrColorSpaceXform.h"
 #include "GrCoordTransform.h"
 #include "GrFragmentProcessor.h"
 #include "GrProcessorUnitTest.h"
@@ -20,6 +21,7 @@ class GrAlphaThresholdFragmentProcessor : public GrFragmentProcessor {
 
 public:
     static sk_sp<GrFragmentProcessor> Make(GrTexture* texture,
+                                           sk_sp<GrColorSpaceXform> colorSpaceXform,
                                            GrTexture* maskTexture,
                                            float innerThreshold,
                                            float outerThreshold,
@@ -30,8 +32,11 @@ public:
     float innerThreshold() const { return fInnerThreshold; }
     float outerThreshold() const { return fOuterThreshold; }
 
+    GrColorSpaceXform* colorSpaceXform() const { return fColorSpaceXform.get(); }
+
 private:
     GrAlphaThresholdFragmentProcessor(GrTexture* texture,
+                                      sk_sp<GrColorSpaceXform> colorSpaceXform,
                                       GrTexture* maskTexture,
                                       float innerThreshold,
                                       float outerThreshold,
@@ -51,6 +56,8 @@ private:
     float fOuterThreshold;
     GrCoordTransform fImageCoordTransform;
     GrTextureAccess  fImageTextureAccess;
+    // Color space transform is for the image (not the mask)
+    sk_sp<GrColorSpaceXform> fColorSpaceXform;
     GrCoordTransform fMaskCoordTransform;
     GrTextureAccess  fMaskTextureAccess;
 
index 47a2d3d..bbae2e1 100644 (file)
@@ -166,18 +166,21 @@ sk_sp<SkSpecialImage> SkAlphaThresholdFilterImpl::onFilterImage(SkSpecialImage*
             return nullptr;
         }
 
-        // SRGBTODO: handle sRGB here
+        const OutputProperties& outProps = ctx.outputProperties();
+        sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(input->getColorSpace(),
+                                                                           outProps.colorSpace());
         sk_sp<GrFragmentProcessor> fp(GrAlphaThresholdFragmentProcessor::Make(
-                                                                   inputTexture.get(),
-                                                                   maskTexture.get(),
-                                                                   fInnerThreshold,
-                                                                   fOuterThreshold,
-                                                                   bounds));
+                                                                         inputTexture.get(),
+                                                                         std::move(colorSpaceXform),
+                                                                         maskTexture.get(),
+                                                                         fInnerThreshold,
+                                                                         fOuterThreshold,
+                                                                         bounds));
         if (!fp) {
             return nullptr;
         }
 
-        return DrawWithFP(context, std::move(fp), bounds, ctx.outputProperties());
+        return DrawWithFP(context, std::move(fp), bounds, outProps);
     }
 #endif