have raster-image return itself as a texture
authorreed <reed@google.com>
Wed, 16 Sep 2015 19:53:29 +0000 (12:53 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 16 Sep 2015 19:53:29 +0000 (12:53 -0700)
This tickles skbug.com/4351, but we don't know how to fix that yet, so we think this CL is ok.

BUG=skia:

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

src/image/SkImage_Base.h
src/image/SkImage_Raster.cpp

index d31626764723bbf707aacc4f9e82f6235d612878..737b30d1db6e3736dcede3736d2bb23bc0bf09d0 100644 (file)
@@ -56,7 +56,10 @@ public:
 
     // return a read-only copy of the pixels. We promise to not modify them,
     // but only inspect them (or encode them).
-    virtual bool getROPixels(SkBitmap*) const { return false; }
+    virtual bool getROPixels(SkBitmap*) const = 0;
+
+    // Caller must call unref when they are done.
+    virtual GrTexture* asTextureRef(GrContext*, SkImageUsageType) const = 0;
 
     virtual SkShader* onNewShader(SkShader::TileMode,
                                   SkShader::TileMode,
@@ -71,9 +74,6 @@ public:
 
     virtual bool onIsLazyGenerated() const { return false; }
 
-    // Caller must call unref when they are done.
-    virtual GrTexture* asTextureRef(GrContext*, SkImageUsageType) const { return nullptr; }
-
 private:
     const SkSurfaceProps fProps;
 
index 042107e3e3fcb5ad51eb13d17c84e78e85d61cce..67498f0e112cf4a5e2f001258906fc6c323316a1 100644 (file)
 #include "SkCanvas.h"
 #include "SkColorTable.h"
 #include "SkData.h"
-#include "SkImageGeneratorPriv.h"
 #include "SkImagePriv.h"
 #include "SkPixelRef.h"
 #include "SkSurface.h"
 
+#if SK_SUPPORT_GPU
+#include "GrContext.h"
+#include "SkGr.h"
+#include "SkGrPriv.h"
+#endif
+
 class SkImage_Raster : public SkImage_Base {
 public:
     static bool ValidArgs(const Info& info, size_t rowBytes, SkColorTable* ctable,
@@ -67,6 +72,7 @@ public:
     const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override;
     SkData* onRefEncoded() const override;
     bool getROPixels(SkBitmap*) const override;
+    GrTexture* asTextureRef(GrContext*, SkImageUsageType) const override;
 
     // exposed for SkSurface_Raster via SkNewImageFromPixelRef
     SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& pixelRefOrigin, size_t rowBytes,
@@ -180,6 +186,32 @@ bool SkImage_Raster::getROPixels(SkBitmap* dst) const {
     return true;
 }
 
+GrTexture* SkImage_Raster::asTextureRef(GrContext* ctx, SkImageUsageType usage) const {
+#if SK_SUPPORT_GPU
+    if (!ctx) {
+        return nullptr;
+    }
+
+    // textures (at least the texture-key) only support 16bit dimensions, so abort early
+    // if we're too big.
+    if (fBitmap.width() > 0xFFFF || fBitmap.height() > 0xFFFF) {
+        return nullptr;
+    }
+
+    GrUniqueKey key;
+    GrMakeKeyFromImageID(&key, fBitmap.getGenerationID(),
+                         SkIRect::MakeWH(fBitmap.width(), fBitmap.height()),
+                         *ctx->caps(), usage);
+
+    if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key)) {
+        return tex;
+    }
+    return GrRefCachedBitmapTexture(ctx, fBitmap, usage);
+#endif
+    
+    return nullptr;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, size_t rowBytes,