Revert of Make stencils be attachable to render targets created via SkSurface::MakeFromBackendTextureAsRender… (patchset #9 id:150001 of https://codereview.chromium.org/1924183003/ )

Reason for revert:
Appears to be breaking the roll.

Original issue's description:
> Make stencils be attachable to render targets created via SkSurface::MakeFromBackendTextureAsRenderTarget
>
> This is a regression from "Refactor to separate backend object lifecycle
> and GpuResource budget decision".
>
> GrGLRenderTarget::CreateWrapped creates only render targets that wrap
> the FBO.
> GrGLRenderTargetTexture::CreateWrapped creates render targets that
> wrap the texture.
>
> Use the latter as the implementation for
> SkSurface::MakeFromBackendTextureAsRenderTarget.
>
>
> BUG=608238
>
> GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1924183003
>
> Committed: https://skia.googlesource.com/skia/+/fe635fd76bbc375d527d1884af23cc617ca364ee
>
> Committed: https://skia.googlesource.com/skia/+/1cb00e42bff455e23f6883bf09680438fa631224

TBR=bsalomon@google.com,kkinnunen@nvidia.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=608238

Review-Url: https://codereview.chromium.org/1943843002
diff --git a/include/core/SkSurface.h b/include/core/SkSurface.h
index b73ccf2..a092a81 100644
--- a/include/core/SkSurface.h
+++ b/include/core/SkSurface.h
@@ -85,8 +85,6 @@
      *  must be set on GrBackendTextureDesc for this to succeed. Skia will not assume ownership
      *  of the texture and the client must ensure the texture is valid for the lifetime of the
      *  SkSurface.
-     *
-     *  If the requested sample count is greater than 0, the contents of the surface are undefined.
      */
     static sk_sp<SkSurface> MakeFromBackendTexture(GrContext*, const GrBackendTextureDesc&,
                                                    const SkSurfaceProps*);
@@ -107,8 +105,6 @@
      *  must be set on GrBackendTextureDesc for this to succeed. Skia will not assume ownership
      *  of the texture and the client must ensure the texture is valid for the lifetime of the
      *  SkSurface.
-     *
-     *  If the requested sample count is greater than 0, the contents of the surface are undefined.
      */
     static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(
             GrContext*, const GrBackendTextureDesc&, const SkSurfaceProps*);
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index d24734f..027d952 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -723,20 +723,19 @@
     }
 #endif
 
-    GrGLTexture::IDDesc idDesc;
-    idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
+    GrGLTextureInfo texInfo;
     GrSurfaceDesc surfDesc;
 
 #ifdef SK_IGNORE_GL_TEXTURE_TARGET
-    idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
+    texInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
     // We only support GL_TEXTURE_2D at the moment.
-    idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
+    texInfo.fTarget = GR_GL_TEXTURE_2D;
 #else
-    idDesc.fInfo = *info;
+    texInfo = *info;
 #endif
 
-    if (GR_GL_TEXTURE_RECTANGLE != idDesc.fInfo.fTarget &&
-        GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
+    if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
+        GR_GL_TEXTURE_2D != texInfo.fTarget) {
         // Only texture rectangle and texture 2d are supported. We do not check whether texture
         // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
         // we assume the necessary support exists.
@@ -759,10 +758,10 @@
     }
 
     GrGLRenderTarget::IDDesc rtIDDesc;
-    if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
+    if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
         return nullptr;
     }
-    return GrGLTextureRenderTarget::CreateWrapped(this, surfDesc, idDesc, rtIDDesc);
+    return GrGLRenderTarget::CreateWrapped(this, surfDesc, rtIDDesc, 0);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/tests/SurfaceTest.cpp b/tests/SurfaceTest.cpp
index a3e0f8f..878aded 100644
--- a/tests/SurfaceTest.cpp
+++ b/tests/SurfaceTest.cpp
@@ -83,6 +83,75 @@
 }
 #endif
 
+#if SK_SUPPORT_GPU
+DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceWrappedTexture, reporter, ctxInfo) {
+    GrGpu* gpu = ctxInfo.fGrContext->getGpu();
+    if (!gpu) {
+        return;
+    }
+
+    // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
+    // a SkSurface.
+    static const int kW = 100;
+    static const int kH = 100;
+    static const uint32_t kOrigColor = 0xFFAABBCC;
+    SkAutoTArray<uint32_t> pixels(kW * kH);
+    sk_memset32(pixels.get(), kOrigColor, kW * kH);
+    GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
+                                                                     kRGBA_8888_GrPixelConfig);
+
+    GrBackendTextureDesc wrappedDesc;
+    wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
+    wrappedDesc.fWidth = kW;
+    wrappedDesc.fHeight = kH;
+    wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
+    wrappedDesc.fSampleCnt = 0;
+    wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
+    wrappedDesc.fTextureHandle = texHandle;
+
+    auto surface(SkSurface::MakeFromBackendTexture(ctxInfo.fGrContext, wrappedDesc, nullptr));
+    REPORTER_ASSERT(reporter, surface);
+    if (surface) {
+        // Validate that we can draw to the canvas and that the original texture color is preserved
+        // in pixels that aren't rendered to via the surface.
+        SkPaint paint;
+        static const SkColor kRectColor = ~kOrigColor | 0xFF000000;
+        paint.setColor(kRectColor);
+        surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
+                                       paint);
+        SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
+        surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0);
+        bool stop = false;
+        SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF),
+                                             (kOrigColor >>  0 & 0xFF),
+                                             (kOrigColor >>  8 & 0xFF),
+                                             (kOrigColor >> 16 & 0xFF));
+        SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
+                                             (kRectColor >> 16 & 0xFF),
+                                             (kRectColor >>  8 & 0xFF),
+                                             (kRectColor >>  0 & 0xFF));
+        for (int y = 0; y < kH/2 && !stop; ++y) {
+            for (int x = 0; x < kW && !stop; ++x) {
+                REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
+                if (rectColorPM != pixels[x + y * kW]) {
+                    stop = true;
+                }
+            }
+        }
+        stop = false;
+        for (int y = kH/2; y < kH && !stop; ++y) {
+            for (int x = 0; x < kW && !stop; ++x) {
+                REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
+                if (origColorPM != pixels[x + y * kW]) {
+                    stop = true;
+                }
+            }
+        }
+    }
+    gpu->deleteTestingOnlyBackendTexture(texHandle);
+}
+#endif
+
 static void test_canvas_peek(skiatest::Reporter* reporter,
                              sk_sp<SkSurface>& surface,
                              const SkImageInfo& requestInfo,
@@ -751,54 +820,10 @@
 }
 
 #if SK_SUPPORT_GPU
-static sk_sp<SkSurface> create_gpu_surface_backend_texture(
-    GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
-    const int kWidth = 10;
-    const int kHeight = 10;
-    SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
-    sk_memset32(pixels.get(), color, kWidth * kHeight);
-    GrBackendTextureDesc desc;
-    desc.fConfig = kRGBA_8888_GrPixelConfig;
-    desc.fWidth = kWidth;
-    desc.fHeight = kHeight;
-    desc.fFlags = kRenderTarget_GrBackendTextureFlag;
-    desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
-        pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig);
-    sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
-    if (!surface) {
-        context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
-        return nullptr;
-    }
-    *outTexture = desc.fTextureHandle;
-    return surface;
-}
 
-static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
-    GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
-    const int kWidth = 10;
-    const int kHeight = 10;
-    SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
-    sk_memset32(pixels.get(), color, kWidth * kHeight);
-    GrBackendTextureDesc desc;
-    desc.fConfig = kRGBA_8888_GrPixelConfig;
-    desc.fWidth = kWidth;
-    desc.fHeight = kHeight;
-    desc.fFlags = kRenderTarget_GrBackendTextureFlag;
-    desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
-        pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig);
-    sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
-                                                                               nullptr);
-    if (!surface) {
-        context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
-        return nullptr;
-    }
-    *outTexture = desc.fTextureHandle;
-    return surface;
-}
-
-static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
-                               std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
-                               uint32_t expectedValue) {
+void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
+                        std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
+                        uint32_t expectedValue) {
     if (!surface) {
         ERRORF(reporter, "Could not create GPU SkSurface.");
         return;
@@ -806,7 +831,7 @@
     int w = surface->width();
     int h = surface->height();
     SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
-    sk_memset32(pixels.get(), ~expectedValue, w * h);
+    memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
 
     SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
     if (!grSurface) {
@@ -847,127 +872,31 @@
         [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
                            return as_IB(i)->peekTexture(); },
     };
-
     for (auto grSurfaceGetter : grSurfaceGetters) {
-        // Test that non-wrapped RTs are created clear.
         for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
             auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
             test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
         }
         // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
-        // This is applicable only when sample count is 0. Otherwise contents are undefined.
-        const uint32_t kOrigColor = 0xABABABAB;
-        for (auto& surface_func : {&create_gpu_surface_backend_texture,
-                                   &create_gpu_surface_backend_texture_as_render_target}) {
-            GrBackendObject textureObject;
-            int sampleCnt = 0;
-            auto surface = surface_func(context, sampleCnt, kOrigColor, &textureObject);
-            test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
-            surface.reset();
-            context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
-        }
-    }
-}
-#endif
+        static const int kWidth = 10;
+        static const int kHeight = 10;
+        SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
+        memset(pixels.get(), 0xAB, sizeof(uint32_t) * kWidth * kHeight);
 
-#if SK_SUPPORT_GPU
-static void test_surface_draw_path_triggering_stencil(
-    skiatest::Reporter* reporter, int sampleCnt, sk_sp<SkSurface> surface,
-    uint32_t origColor) {
-    const int kW = surface->width();
-    const int kH = surface->height();
-    SkPaint paint;
-    paint.setAntiAlias(true);
-    paint.setColor(SK_ColorGREEN);
-    SkPath clipPath;
-    clipPath.quadTo(SkIntToScalar(kW), SkIntToScalar(0), SkIntToScalar(kW), SkIntToScalar(kH));
-    clipPath.lineTo(SkIntToScalar(0), SkIntToScalar(kH));
-    clipPath.lineTo(SkIntToScalar(0), SkIntToScalar(0));
-    clipPath.close();
-    SkPath path;
-    path.quadTo(SkIntToScalar(0), SkIntToScalar(kH), SkIntToScalar(kW), SkIntToScalar(kH));
-    path.lineTo(SkIntToScalar(kW), SkIntToScalar(0));
-    path.lineTo(SkIntToScalar(0), SkIntToScalar(0));
-    path.close();
-    surface->getCanvas()->clipPath(clipPath, SkRegion::kIntersect_Op, true);
-    surface->getCanvas()->drawPath(path, paint);
-    // TODO: verify the drawing in a way that passes all platforms.
-}
+        GrBackendObject textureObject =
+                context->getGpu()->createTestingOnlyBackendTexture(pixels.get(), kWidth, kHeight,
+                                                                   kRGBA_8888_GrPixelConfig);
 
-static void test_surface_draw_partially(
-    skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
-    const int kW = surface->width();
-    const int kH = surface->height();
-    SkPaint paint;
-    const SkColor kRectColor = ~origColor | 0xFF000000;
-    paint.setColor(kRectColor);
-    surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
-                                   paint);
-    SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kW * kH]);
-    sk_memset32(pixels.get(), ~origColor, kW * kH);
-    SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
-    SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
-    bool stop = false;
-    SkPMColor origColorPM = SkPackARGB32((origColor >> 24 & 0xFF),
-                                         (origColor >>  0 & 0xFF),
-                                         (origColor >>  8 & 0xFF),
-                                         (origColor >> 16 & 0xFF));
-    SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
-                                         (kRectColor >> 16 & 0xFF),
-                                         (kRectColor >>  8 & 0xFF),
-                                         (kRectColor >>  0 & 0xFF));
-    for (int y = 0; y < kH/2 && !stop; ++y) {
-        for (int x = 0; x < kW && !stop; ++x) {
-            REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
-            if (rectColorPM != pixels[x + y * kW]) {
-                stop = true;
-            }
-        }
-    }
-    stop = false;
-    for (int y = kH/2; y < kH && !stop; ++y) {
-        for (int x = 0; x < kW && !stop; ++x) {
-            REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
-            if (origColorPM != pixels[x + y * kW]) {
-                stop = true;
-            }
-        }
-    }
-}
+        GrBackendTextureDesc desc;
+        desc.fConfig = kRGBA_8888_GrPixelConfig;
+        desc.fWidth = kWidth;
+        desc.fHeight = kHeight;
+        desc.fFlags = kRenderTarget_GrBackendTextureFlag;
+        desc.fTextureHandle = textureObject;
 
-DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceWrappedTextureDrawTests, reporter, ctxInfo) {
-    GrGpu* gpu = ctxInfo.fGrContext->getGpu();
-    if (!gpu) {
-        return;
-    }
-    static const uint32_t kOrigColor = 0xFFAABBCC;
-
-    for (auto& surface_func : {&create_gpu_surface_backend_texture,
-                               &create_gpu_surface_backend_texture_as_render_target}) {
-        for (int sampleCnt : {0, 4, 8}) {
-            GrBackendObject textureObject;
-            auto surface = surface_func(ctxInfo.fGrContext, sampleCnt, kOrigColor, &textureObject);
-            if (!surface) {
-                continue;
-            }
-            // Validate that we can draw paths to a canvas of a surface created with
-            // SkSurface::MakeFromBackendTextureAsRenderTarget. The code intends to enforce the use
-            // of stencil buffer. The original bug prevented the creation of stencil buffer, causing
-            // an assert while drawing paths.
-            test_surface_draw_path_triggering_stencil(reporter, sampleCnt, surface, kOrigColor);
-            surface.reset();
-            gpu->deleteTestingOnlyBackendTexture(textureObject);
-        }
-        // Validate that we can draw to the canvas and that the original texture color is
-        // preserved in pixels that aren't rendered to via the surface.
-        // This works only for non-multisampled case.
-        GrBackendObject textureObject;
-        auto surface = surface_func(ctxInfo.fGrContext, 0, kOrigColor, &textureObject);
-        if (surface) {
-            test_surface_draw_partially(reporter, surface, kOrigColor);
-            surface.reset();
-            gpu->deleteTestingOnlyBackendTexture(textureObject);
-        }
+        auto surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
+        test_surface_clear(reporter, surface, grSurfaceGetter, 0xABABABAB);
+        context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
     }
 }
 #endif