Add SkSpecialImage::makeTextureImage entry point
authorrobertphillips <robertphillips@google.com>
Fri, 18 Mar 2016 15:14:27 +0000 (08:14 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 18 Mar 2016 15:14:27 +0000 (08:14 -0700)
This is calved off of: https://codereview.chromium.org/1785643003 (Switch SkBlurImageFilter over to new onFilterImage interface)

GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1813813002

Committed: https://skia.googlesource.com/skia/+/05849018c85403a34b88819db1c4bcf713b70a2b

Review URL: https://codereview.chromium.org/1813813002

src/core/SkImageFilter.cpp
src/core/SkSpecialImage.cpp
src/core/SkSpecialImage.h
tests/SpecialImageTest.cpp

index 1a4c876d61c5ed75346e308a495a2cf73af9b609..4ae839c3d441f33a8e6f23b13e346e6f603bfebd 100644 (file)
@@ -596,7 +596,18 @@ SkSpecialImage* SkImageFilter::filterInput(int index,
         return SkRef(src);
     }
 
-    return input->filterImage(src, this->mapContext(ctx), offset);
+    SkAutoTUnref<SkSpecialImage> result(input->filterImage(src, this->mapContext(ctx), offset));
+
+#if SK_SUPPORT_GPU
+    if (src->peekTexture() && result && !result->peekTexture()) {
+        // Keep the result on the GPU - this is still required for some
+        // image filters that don't support GPU in all cases
+        GrContext* context = src->peekTexture()->getContext();
+        return result->makeTextureImage(src->internal_getProxy(), context).release();
+    }
+#endif
+
+    return result.release();
 }
 
 #if SK_SUPPORT_GPU
index e90c655a5824567bb64035da8643567ea77f73e1..991e16351586674ad01272215071f043c72d8408 100644 (file)
@@ -4,10 +4,16 @@
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file
  */
+#include "SkSpecialImage.h"
+
+#if SK_SUPPORT_GPU
+#include "GrTexture.h"
+#include "GrTextureParams.h"
+#include "SkGr.h"
+#endif
 
 #include "SkCanvas.h"
 #include "SkImage_Base.h"
-#include "SkSpecialImage.h"
 #include "SkSpecialSurface.h"
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -42,6 +48,38 @@ static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) {
     return static_cast<const SkSpecialImage_Base*>(image);
 }
 
+sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(SkImageFilter::Proxy* proxy,
+                                                       GrContext* context) {
+#if SK_SUPPORT_GPU
+    if (!context) {
+        return nullptr;
+    }
+    if (GrTexture* peek = as_SIB(this)->peekTexture()) {
+        return peek->getContext() == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr;
+    }
+
+    SkBitmap bmp;
+    if (!this->internal_getBM(&bmp)) {
+        return nullptr;
+    }
+
+    SkAutoTUnref<GrTexture> resultTex(
+        GrRefCachedBitmapTexture(context, bmp, GrTextureParams::ClampNoFilter()));
+    if (!resultTex) {
+        return nullptr;
+    }
+
+    SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
+
+    return SkSpecialImage::MakeFromGpu(proxy,
+                                       SkIRect::MakeWH(resultTex->width(), resultTex->height()),
+                                       this->uniqueID(),
+                                       resultTex, at);
+#else
+    return nullptr;
+#endif
+}
+
 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
     return as_SIB(this)->onDraw(canvas, x, y, paint);
 }
index 1a01a5abc7a8a285955df101aa67ac759ab73775..47ad6ccab7972f7de6e2d698fc5f978d910963bc 100644 (file)
 // remove this when internal_getProxy goes away (see skbug.com/4965)
 #include "SkImageFilter.h"
 
+#include "SkImageInfo.h" // for SkAlphaType
+
+class GrContext;
 class GrTexture;
 class SkBitmap;
 class SkCanvas;
 class SkImage;
 struct SkImageInfo;
 class SkPaint;
+class SkPixmap;
 class SkSpecialSurface;
 
 enum {
@@ -50,6 +54,13 @@ public:
     virtual bool isOpaque() const { return false; }
     virtual size_t getSize() const = 0;
 
+    /**
+     *  Ensures that a special image is backed by a texture (when GrContext is non-null). If no
+     *  transformation is required, the returned image may be the same as this special image.
+     *  If this special image is from a different GrContext, this will fail.
+     */
+    sk_sp<SkSpecialImage> makeTextureImage(SkImageFilter::Proxy*, GrContext*);
+
     /**
      *  Draw this SpecialImage into the canvas.
      */
index 233ce2cee18338a4e2fcc6157028ffb1de5ee640..3240fbd3a72fee36054f4f43a7358738e0133c51 100644 (file)
@@ -174,6 +174,81 @@ DEF_TEST(SpecialImage_Pixmap, reporter) {
 
 
 #if SK_SUPPORT_GPU
+
+static void test_texture_backed(skiatest::Reporter* reporter,
+                                const sk_sp<SkSpecialImage>& orig,
+                                const sk_sp<SkSpecialImage>& gpuBacked) {
+    REPORTER_ASSERT(reporter, gpuBacked);
+    REPORTER_ASSERT(reporter, gpuBacked->peekTexture());
+    REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
+    REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
+                              gpuBacked->subset().height() == orig->subset().height());
+}
+
+// Test out the SkSpecialImage::makeTextureImage entry point
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, context) {
+    SkBitmap bm = create_bm();
+
+    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
+
+    {
+        // raster
+        sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
+                                                                        nullptr,
+                                                                        SkIRect::MakeWH(kFullSize,
+                                                                                        kFullSize),
+                                                                        bm));
+
+        {
+            sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context));
+            test_texture_backed(reporter, rasterImage, fromRaster);
+        }
+
+        {
+            sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
+
+            sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context));
+            test_texture_backed(reporter, subRasterImage, fromSubRaster);
+        }
+    }
+
+    {
+        // gpu
+        GrSurfaceDesc desc;
+        desc.fConfig = kSkia8888_GrPixelConfig;
+        desc.fFlags = kNone_GrSurfaceFlags;
+        desc.fWidth = kFullSize;
+        desc.fHeight = kFullSize;
+
+        SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
+                                                                                  SkBudgeted::kNo,
+                                                                                  bm.getPixels(),
+                                                                                  0));
+        if (!texture) {
+            return;
+        }
+
+        sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
+                                                                nullptr,
+                                                                SkIRect::MakeWH(kFullSize,
+                                                                                kFullSize),
+                                                                kNeedNewImageUniqueID_SpecialImage,
+                                                                texture));
+
+        {
+            sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context));
+            test_texture_backed(reporter, gpuImage, fromGPU);
+        }
+
+        {
+            sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
+
+            sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context));
+            test_texture_backed(reporter, subGPUImage, fromSubGPU);
+        }
+    }
+}
+
 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
     SkBitmap bm = create_bm();
 
@@ -183,7 +258,8 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
     desc.fWidth  = kFullSize;
     desc.fHeight = kFullSize;
 
-    SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, SkBudgeted::kNo,
+    SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
+                                                                              SkBudgeted::kNo,
                                                                               bm.getPixels(), 0));
     if (!texture) {
         return;