new image from backend desc

BUG=485243

Review URL: https://codereview.chromium.org/1121813002
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index eee93bb..13287c8 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1731,29 +1731,14 @@
                                filter, ctx, result, offset);
 }
 
-static SkImageInfo make_info(GrTexture* tex, int w, int h, bool isOpaque) {
-    const GrPixelConfig config = tex->config();
-    SkColorType ct;
-    SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
-    if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
-        ct = kUnknown_SkColorType;
-    }
-    return SkImageInfo::Make(w, h, ct, at);
-}
-
 static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
     GrTexture* tex = image->getTexture();
     if (tex) {
-        // TODO: handle the GrTexture directly, and skip GrPixelRef
-        const SkImageInfo info = make_info(tex, image->width(), image->height(), image->isOpaque());
-        bm->setInfo(info);
-        bm->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, tex)))->unref();
+        GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
+        return true;
     } else {
-        if (!as_IB(image)->getROPixels(bm)) {
-            return false;
-        }
+        return as_IB(image)->getROPixels(bm);
     }
-    return true;
 }
 
 void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 5df8c53..2fc0996 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -12,6 +12,7 @@
 #include "SkConfig8888.h"
 #include "SkData.h"
 #include "SkErrorInternals.h"
+#include "SkGrPixelRef.h"
 #include "SkMessageBus.h"
 #include "SkPixelRef.h"
 #include "SkResourceCache.h"
@@ -737,3 +738,25 @@
     // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
     return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
 }
+
+SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
+#ifdef SK_DEBUG
+    const GrSurfaceDesc& desc = tex->desc();
+    SkASSERT(w <= desc.fWidth);
+    SkASSERT(h <= desc.fHeight);
+#endif
+    const GrPixelConfig config = tex->config();
+    SkColorType ct;
+    SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
+    if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
+        ct = kUnknown_SkColorType;
+    }
+    return SkImageInfo::Make(w, h, ct, at);
+}
+
+
+void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
+    const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
+    dst->setInfo(info);
+    dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
+}
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 494e210..4efbc33 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -169,4 +169,16 @@
     return surface->newImageSnapshot();
 }
 
+//////////////////////////////////////////////////////////////////////////////////////
 
+#if !SK_SUPPORT_GPU
+
+SkImage* SkImage::NewFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType) {
+    return NULL;
+}
+
+SkImage* SkImage::NewFromTextureCopy(GrContext*, const GrBackendTextureDesc&, SkAlphaType) {
+    return NULL;
+}
+
+#endif
diff --git a/src/image/SkImagePriv.h b/src/image/SkImagePriv.h
index 1b2ae4f..3a5c59a 100644
--- a/src/image/SkImagePriv.h
+++ b/src/image/SkImagePriv.h
@@ -38,11 +38,6 @@
 // in which case the surface may need to perform a copy-on-write.
 extern const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* rasterImage);
 
-// Given an image created with NewTexture, return its GrTexture. This
-// may be called to see if the surface and the image share the same GrTexture,
-// in which case the surface may need to perform a copy-on-write.
-extern GrTexture* SkTextureImageGetTexture(SkImage* textureImage);
-
 // When a texture is shared by a surface and an image its budgeted status is that of the
 // surface. This function is used when the surface makes a new texture for itself in order
 // for the orphaned image to determine whether the original texture counts against the
@@ -54,7 +49,4 @@
 // surface needs to perform a copy-on-write
 extern void SkTextureImageSetTexture(SkImage* image, GrTexture* texture);
 
-extern SkImage* SkNewImageFromBitmapTexture(const SkBitmap&, int sampleCountForNewSurfaces,
-                                            SkSurface::Budgeted);
-
 #endif
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index eec0d19..06ecda1 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -8,57 +8,154 @@
 #include "SkImage_Gpu.h"
 #include "SkCanvas.h"
 #include "GrContext.h"
+#include "SkGpuDevice.h"
 
-SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap, int sampleCountForNewSurfaces,
-                         SkSurface::Budgeted budgeted)
-    : INHERITED(bitmap.width(), bitmap.height(), NULL)
-    , fBitmap(bitmap)
+SkImage_Gpu::SkImage_Gpu(int w, int h, SkAlphaType at, GrTexture* tex,
+                         int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted)
+    : INHERITED(w, h, NULL)
+    , fTexture(SkRef(tex))
     , fSampleCountForNewSurfaces(sampleCountForNewSurfaces)
+    , fAlphaType(at)
     , fBudgeted(budgeted)
-{
-    SkASSERT(fBitmap.getTexture());
-}
-
-SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX,
-                                   SkShader::TileMode tileY,
-                                   const SkMatrix* localMatrix) const
-{
-    return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
-}
+    {}
 
 SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
-    GrContext* ctx = this->getTexture()->getContext();
-    // TODO: Change signature of onNewSurface to take a budgeted param.
-    static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
-    return SkSurface::NewRenderTarget(ctx, kBudgeted, info, fSampleCountForNewSurfaces, &props);
-}
-
-GrTexture* SkImage_Gpu::onGetTexture() const {
-    return fBitmap.getTexture();
-}
-
-bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
-    return fBitmap.copyTo(dst, kN32_SkColorType);
-}
-
-bool SkImage_Gpu::isOpaque() const {
-    return fBitmap.isOpaque();
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-SkImage* SkNewImageFromBitmapTexture(const SkBitmap& bitmap, int sampleCountForNewSurfaces,
-                                     SkSurface::Budgeted budgeted) {
-    if (0 == bitmap.width() || 0 == bitmap.height() || NULL == bitmap.getTexture()) {
+    GrTexture* tex = this->getTexture();
+    SkASSERT(tex);
+    GrContext* ctx = tex->getContext();
+    if (!ctx) {
+        // the texture may have been abandoned, so we have to check
         return NULL;
     }
-    return SkNEW_ARGS(SkImage_Gpu, (bitmap, sampleCountForNewSurfaces, budgeted));
-}
-
-GrTexture* SkTextureImageGetTexture(SkImage* image) {
-    return ((SkImage_Gpu*)image)->getTexture();
+    // TODO: Change signature of onNewSurface to take a budgeted param.
+    const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
+    return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSurfaces, &props);
 }
 
 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
-    ((SkImage_Gpu*)image)->applyBudgetDecision();
+    if (image->getTexture()) {
+        ((SkImage_Gpu*)image)->applyBudgetDecision();
+    }
+}
+
+SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
+                                   const SkMatrix* localMatrix) const {
+    SkBitmap bm;
+    GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &bm);
+    return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix);
+}
+
+bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
+    SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
+    if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) {
+        return false;
+    }
+    if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
+                              dst->getPixels(), dst->rowBytes())) {
+        return false;
+    }
+    return true;
+}
+
+bool SkImage_Gpu::isOpaque() const {
+    return GrPixelConfigIsOpaque(fTexture->config());
+}
+
+static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
+    switch (info.colorType()) {
+        case kRGBA_8888_SkColorType:
+        case kBGRA_8888_SkColorType:
+            break;
+        default:
+            return; // nothing to do
+    }
+
+    // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
+    // and in either case, the alpha-byte is always in the same place, so we can safely call
+    // SkPreMultiplyColor()
+    //
+    SkColor* row = (SkColor*)pixels;
+    for (int y = 0; y < info.height(); ++y) {
+        for (int x = 0; x < info.width(); ++x) {
+            row[x] = SkPreMultiplyColor(row[x]);
+        }
+    }
+}
+
+bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
+                               int srcX, int srcY) const {
+    GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(),
+                                                     info.profileType());
+    uint32_t flags = 0;
+    if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
+        // let the GPU perform this transformation for us
+        flags = GrContext::kUnpremul_PixelOpsFlag;
+    }
+    if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
+                              pixels, rowBytes, flags)) {
+        return false;
+    }
+    // do we have to manually fix-up the alpha channel?
+    //      src         dst
+    //      unpremul    premul      fix manually
+    //      premul      unpremul    done by kUnpremul_PixelOpsFlag
+    // all other combos need to change.
+    //
+    // Should this be handled by Ganesh? todo:?
+    //
+    if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
+        apply_premul(info, pixels, rowBytes);
+    }
+    return true;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at) {
+    if (desc.fWidth <= 0 || desc.fHeight <= 0) {
+        return NULL;
+    }
+    SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc));
+    if (!tex) {
+        return NULL;
+    }
+    const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted;
+    return SkNEW_ARGS(SkImage_Gpu, (desc.fWidth, desc.fHeight, at, tex, 0, budgeted));
+}
+
+SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& srcDesc,
+                                     SkAlphaType at) {
+    const bool isBudgeted = true;
+    const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted;
+
+    if (srcDesc.fWidth <= 0 || srcDesc.fHeight <= 0) {
+        return NULL;
+    }
+    SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(srcDesc));
+    if (!src) {
+        return NULL;
+    }
+
+    GrSurfaceDesc dstDesc;
+    // need to be a rendertarget for readpixels to work, instead of kNone_GrSurfaceFlags
+    dstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
+    dstDesc.fOrigin = srcDesc.fOrigin;
+    dstDesc.fWidth = srcDesc.fWidth;
+    dstDesc.fHeight = srcDesc.fHeight;
+    dstDesc.fConfig = srcDesc.fConfig;
+    dstDesc.fSampleCnt = srcDesc.fSampleCnt;
+
+    SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(
+                                                                  dstDesc, isBudgeted, NULL, 0));
+    if (!dst) {
+        return NULL;
+    }
+
+    const SkIRect srcR = SkIRect::MakeWH(dstDesc.fWidth, dstDesc.fHeight);
+    const SkIPoint dstP = SkIPoint::Make(0, 0);
+    ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp);
+
+    const int sampleCount = 0;  // todo: make this an explicit parameter to newSurface()?
+    return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, at, dst, sampleCount,
+                                    budgeted));
 }
diff --git a/src/image/SkImage_Gpu.h b/src/image/SkImage_Gpu.h
index 7b38e60..972cd3c 100644
--- a/src/image/SkImage_Gpu.h
+++ b/src/image/SkImage_Gpu.h
@@ -19,32 +19,38 @@
 public:
     SK_DECLARE_INST_COUNT(SkImage_Gpu)
 
-    SkImage_Gpu(const SkBitmap&, int sampleCountForNewSurfaces, SkSurface::Budgeted);
-
-    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override;
-    GrTexture* onGetTexture() const override;
-    bool getROPixels(SkBitmap*) const override;
-
-    GrTexture* getTexture() const { return fBitmap.getTexture(); }
-
-    SkShader* onNewShader(SkShader::TileMode,
-                                  SkShader::TileMode,
-                                  const SkMatrix* localMatrix) const override;
-
-    bool isOpaque() const override;
+    /**
+     *  An "image" can be a subset/window into a larger texture, so we explicit take the
+     *  width and height.
+     */
+    SkImage_Gpu(int w, int h, SkAlphaType, GrTexture*, int sampleCountForNewSurfaces,
+                SkSurface::Budgeted);
 
     void applyBudgetDecision() const {
+        GrTexture* tex = this->getTexture();
+        SkASSERT(tex);
         if (fBudgeted) {
-            fBitmap.getTexture()->resourcePriv().makeBudgeted();
+            tex->resourcePriv().makeBudgeted();
         } else {
-            fBitmap.getTexture()->resourcePriv().makeUnbudgeted();
+            tex->resourcePriv().makeUnbudgeted();
         }
     }
 
+    bool getROPixels(SkBitmap*) const override;
+    GrTexture* onGetTexture() const override { return fTexture; }
+    SkShader* onNewShader(SkShader::TileMode,
+                          SkShader::TileMode,
+                          const SkMatrix* localMatrix) const override;
+    bool isOpaque() const override;
+    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override;
+    bool onReadPixels(const SkImageInfo&, void* dstPixels, size_t dstRowBytes,
+                      int srcX, int srcY) const override;
+
 private:
-    SkBitmap            fBitmap;
-    const int           fSampleCountForNewSurfaces;   // 0 if we don't know
-    SkSurface::Budgeted fBudgeted;
+    SkAutoTUnref<GrTexture> fTexture;
+    const int               fSampleCountForNewSurfaces;   // 0 if we don't know
+    const SkAlphaType       fAlphaType;
+    SkSurface::Budgeted     fBudgeted;
 
     typedef SkImage_Base INHERITED;
 };
diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp
index 8788e08..fa5daec 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -10,6 +10,7 @@
 #include "SkCanvas.h"
 #include "SkGpuDevice.h"
 #include "SkImage_Base.h"
+#include "SkImage_Gpu.h"
 #include "SkImagePriv.h"
 #include "SkSurface_Base.h"
 
@@ -44,9 +45,15 @@
 }
 
 SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) {
+    const SkImageInfo info = fDevice->imageInfo();
     const int sampleCount = fDevice->accessRenderTarget()->numSamples();
-    SkImage* image = SkNewImageFromBitmapTexture(fDevice->accessBitmap(false), sampleCount,
-                                                 budgeted);
+    SkImage* image = NULL;
+    GrTexture* tex = fDevice->accessRenderTarget()->asTexture();
+    if (tex) {
+        image = SkNEW_ARGS(SkImage_Gpu,
+                           (info.width(), info.height(), info.alphaType(),
+                            tex, sampleCount, budgeted));
+    }
     if (image) {
         as_IB(image)->initWithProps(this->props());
     }
@@ -67,7 +74,7 @@
     // image because onCopyOnWrite is only called when there is a cached image.
     SkImage* image = this->getCachedImage(kNo_Budgeted);
     SkASSERT(image);
-    if (rt->asTexture() == SkTextureImageGetTexture(image)) {
+    if (rt->asTexture() == image->getTexture()) {
         this->fDevice->replaceRenderTarget(SkSurface::kRetain_ContentChangeMode == mode);
         SkTextureImageApplyBudgetedDecision(image);
     } else if (kDiscard_ContentChangeMode == mode) {