Add SkImage factory method that forces image to be resolved to a texture.
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1631053003

Review URL: https://codereview.chromium.org/1631053003
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
index 575fc1c..1bf56c5 100644
--- a/tests/ImageTest.cpp
+++ b/tests/ImageTest.cpp
@@ -5,6 +5,9 @@
  * found in the LICENSE file.
  */
 
+#include <functional>
+#include "DMGpuSupport.h"
+
 #include "SkBitmap.h"
 #include "SkCanvas.h"
 #include "SkData.h"
@@ -21,12 +24,6 @@
 #include "SkUtils.h"
 #include "Test.h"
 
-#if SK_SUPPORT_GPU
-#include "GrContext.h"
-#include "gl/GrGLInterface.h"
-#include "gl/GrGLUtil.h"
-#endif
-
 static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
                          SkImage* b) {
     const int widthA = subsetA ? subsetA->width() : a->width();
@@ -87,6 +84,15 @@
     SkAutoTUnref<SkData> data(create_image_data(&info));
     return SkImage::NewRasterData(info, data, info.minRowBytes());
 }
+#if SK_SUPPORT_GPU // not gpu-specific but currently only used in GPU tests
+static SkImage* create_picture_image() {
+    SkPictureRecorder recorder;
+    SkCanvas* canvas = recorder.beginRecording(10, 10);
+    canvas->clear(SK_ColorCYAN);
+    SkAutoTUnref<SkPicture> picture(recorder.endRecording());
+    return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullptr, nullptr);
+};
+#endif
 // Want to ensure that our Release is called when the owning image is destroyed
 struct RasterDataHolder {
     RasterDataHolder() : fReleaseCount(0) {}
@@ -376,6 +382,64 @@
         REPORTER_ASSERT(reporter, !SkBitmapCache::Find(uniqueID, &cachedBitmap));
     }
 }
+
+DEF_GPUTEST_FOR_NATIVE_CONTEXT(SkImage_newTextureImage, reporter, context, glContext) {
+    GrContextFactory otherFactory;
+    GrContextFactory::ContextInfo otherContextInfo =
+        otherFactory.getContextInfo(GrContextFactory::kNative_GLContextType);
+    glContext->makeCurrent();
+
+    std::function<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.
+        [glContext, otherContextInfo] {
+            otherContextInfo.fGLContext->makeCurrent();
+            SkImage* otherContextImage = create_gpu_image(otherContextInfo.fGrContext);
+            glContext->makeCurrent();
+            return otherContextImage;
+        }
+    };
+
+    for (auto factory : imageFactories) {
+        SkAutoTUnref<SkImage> image(factory());
+        if (!image) {
+            ERRORF(reporter, "Error creating image.");
+            continue;
+        }
+        GrTexture* origTexture = as_IB(image)->peekTexture();
+
+        SkAutoTUnref<SkImage> texImage(image->newTextureImage(context));
+        if (!texImage) {
+            // We execpt to fail if image comes from a different GrContext.
+            if (!origTexture || origTexture->getContext() == context) {
+                ERRORF(reporter, "newTextureImage failed.");
+            }
+            continue;
+        }
+        GrTexture* copyTexture = as_IB(texImage)->peekTexture();
+        if (!copyTexture) {
+            ERRORF(reporter, "newTextureImage returned non-texture image.");
+            continue;
+        }
+        if (origTexture) {
+            if (origTexture != copyTexture) {
+                ERRORF(reporter, "newTextureImage made unnecessary texture copy.");
+            }
+        }
+        if (image->width() != texImage->width() || image->height() != texImage->height()) {
+            ERRORF(reporter, "newTextureImage changed the image size.");
+        }
+        if (image->isOpaque() != texImage->isOpaque()) {
+            ERRORF(reporter, "newTextureImage changed image opaqueness.");
+        }
+    }
+}
 #endif
 
 // https://bug.skia.org/4390