Bring back SkImage::makeTextureImage

Ensures that an image is GPU backed on the passed-in GrContxt. The new
version requires a destination color space (intended usage of the image),
so we can make a proper decision about decoded format.

This reverts commit d263413a2a92cafe3fd3b051c67d00206c9a0e4d.

BUG=skia:

Change-Id: Ibccddbafc301779559592045ed5a5fa9264e7432
Reviewed-on: https://skia-review.googlesource.com/8116
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
index 76ddd96..500efef 100644
--- a/tests/ImageTest.cpp
+++ b/tests/ImageTest.cpp
@@ -457,6 +457,69 @@
     }
 }
 
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
+    GrContext* context = contextInfo.grContext();
+    sk_gpu_test::TestContext* testContext = contextInfo.testContext();
+
+    GrContextFactory otherFactory;
+    GrContextFactory::ContextType otherContextType =
+            GrContextFactory::NativeContextTypeForBackend(testContext->backend());
+    ContextInfo otherContextInfo = otherFactory.getContextInfo(otherContextType);
+    testContext->makeCurrent();
+
+    std::function<sk_sp<SkImage>()> imageFactories[] = {
+        create_image,
+        create_codec_image,
+        create_data_image,
+        // Create an image from a picture.
+        create_picture_image,
+        // Create a texture image.
+        [context] { return create_gpu_image(context); },
+        // Create a texture image in a another GrContext.
+        [testContext, otherContextInfo] {
+            otherContextInfo.testContext()->makeCurrent();
+            sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
+            testContext->makeCurrent();
+            return otherContextImage;
+        }
+    };
+
+    SkColorSpace* legacyColorSpace = nullptr;
+    for (auto factory : imageFactories) {
+        sk_sp<SkImage> image(factory());
+        if (!image) {
+            ERRORF(reporter, "Error creating image.");
+            continue;
+        }
+        GrTexture* origTexture = as_IB(image)->peekTexture();
+
+        sk_sp<SkImage> texImage(image->makeTextureImage(context, legacyColorSpace));
+        if (!texImage) {
+            // We execpt to fail if image comes from a different GrContext.
+            if (!origTexture || origTexture->getContext() == context) {
+                ERRORF(reporter, "makeTextureImage failed.");
+            }
+            continue;
+        }
+        GrTexture* copyTexture = as_IB(texImage)->peekTexture();
+        if (!copyTexture) {
+            ERRORF(reporter, "makeTextureImage returned non-texture image.");
+            continue;
+        }
+        if (origTexture) {
+            if (origTexture != copyTexture) {
+                ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
+            }
+        }
+        if (image->width() != texImage->width() || image->height() != texImage->height()) {
+            ERRORF(reporter, "makeTextureImage changed the image size.");
+        }
+        if (image->alphaType() != texImage->alphaType()) {
+            ERRORF(reporter, "makeTextureImage changed image alpha type.");
+        }
+    }
+}
+
 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
     GrContext* context = contextInfo.grContext();
 
@@ -467,11 +530,14 @@
         create_picture_image,
         [context] { return create_gpu_image(context); },
     };
+    SkColorSpace* legacyColorSpace = nullptr;
     for (auto factory : imageFactories) {
         sk_sp<SkImage> image = factory();
         if (!image->isTextureBacked()) {
             REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
-            continue;
+            if (!(image = image->makeTextureImage(context, legacyColorSpace))) {
+                continue;
+            }
         }
         auto rasterImage = image->makeNonTextureImage();
         if (!rasterImage) {