Add protected status to SkSurfaceCharacterization

Change-Id: I27bd051c1fce0239faff7fc073eaf82976fb63ae
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/225940
Reviewed-by: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/core/SkDeferredDisplayListRecorder.cpp b/src/core/SkDeferredDisplayListRecorder.cpp
index a7cf33b..a17f8a4 100644
--- a/src/core/SkDeferredDisplayListRecorder.cpp
+++ b/src/core/SkDeferredDisplayListRecorder.cpp
@@ -142,6 +142,7 @@
     desc.fFlags = kRenderTarget_GrSurfaceFlag;
     desc.fWidth = fCharacterization.width();
     desc.fHeight = fCharacterization.height();
+    desc.fIsProtected = fCharacterization.isProtected();
     desc.fConfig = config;
     desc.fSampleCnt = fCharacterization.sampleCount();
 
diff --git a/src/core/SkSurfaceCharacterization.cpp b/src/core/SkSurfaceCharacterization.cpp
index a97ffc5..1c884af 100644
--- a/src/core/SkSurfaceCharacterization.cpp
+++ b/src/core/SkSurfaceCharacterization.cpp
@@ -42,6 +42,7 @@
            fIsMipMapped == other.fIsMipMapped &&
            fUsesGLFBO0 == other.fUsesGLFBO0 &&
            fVulkanSecondaryCBCompatible == other.fVulkanSecondaryCBCompatible &&
+           fIsProtected == other.fIsProtected &&
            fSurfaceProps == other.fSurfaceProps;
 }
 
@@ -59,7 +60,7 @@
     return SkSurfaceCharacterization(fContextInfo, fCacheMaxResourceBytes,
                                      fImageInfo.makeWH(width, height), fBackendFormat, fOrigin,
                                      fSampleCnt, fIsTextureable, fIsMipMapped, fUsesGLFBO0,
-                                     fVulkanSecondaryCBCompatible, fSurfaceProps);
+                                     fVulkanSecondaryCBCompatible, fIsProtected, fSurfaceProps);
 }
 
 bool SkSurfaceCharacterization::isCompatible(const GrBackendTexture& backendTex) const {
@@ -90,7 +91,10 @@
         return false;
     }
 
-    // TODO: need to check protected status here
+    if (this->isProtected() != GrProtected(backendTex.isProtected())) {
+        return false;
+    }
+
     return true;
 }
 
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index da219a1..a1e3eba 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -394,11 +394,10 @@
         return GrBackendTexture();
     }
 
-    // TODO (PROT-CHAR): pass in protection status once added to characterization
     GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
                                                          GrMipMapped(c.isMipMapped()),
                                                          GrRenderable::kYes,
-                                                         GrProtected::kNo);
+                                                         c.isProtected());
     SkASSERT(c.isCompatible(result));
     return result;
 }
@@ -434,11 +433,10 @@
         return GrBackendTexture();
     }
 
-    // TODO (PROT-CHAR): pass in protection status once added to characterization
     GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
                                                          GrMipMapped(c.isMipMapped()),
                                                          GrRenderable::kYes,
-                                                         GrProtected::kNo);
+                                                         c.isProtected());
     SkASSERT(c.isCompatible(result));
     return result;
 }
diff --git a/src/gpu/GrContextThreadSafeProxy.cpp b/src/gpu/GrContextThreadSafeProxy.cpp
index 9a1a37a..5b00895 100644
--- a/src/gpu/GrContextThreadSafeProxy.cpp
+++ b/src/gpu/GrContextThreadSafeProxy.cpp
@@ -15,6 +15,10 @@
 #include "src/gpu/GrSkSLFPFactoryCache.h"
 #include "src/image/SkSurface_Gpu.h"
 
+#ifdef SK_VULKAN
+#include "src/gpu/vk/GrVkCaps.h"
+#endif
+
 GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
                                                    const GrContextOptions& options,
                                                    uint32_t contextID)
@@ -33,7 +37,8 @@
                                      const SkImageInfo& ii, const GrBackendFormat& backendFormat,
                                      int sampleCnt, GrSurfaceOrigin origin,
                                      const SkSurfaceProps& surfaceProps,
-                                     bool isMipMapped, bool willUseGLFBO0, bool isTextureable) {
+                                     bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
+                                     GrProtected isProtected) {
     if (!backendFormat.isValid()) {
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
@@ -73,6 +78,21 @@
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
+    if (GrBackendApi::kVulkan == backendFormat.backend()) {
+        if (GrBackendApi::kVulkan != this->backend()) {
+            return SkSurfaceCharacterization(); // return an invalid characterization
+        }
+
+#ifdef SK_VULKAN
+        const GrVkCaps* vkCaps = (const GrVkCaps*) this->caps();
+
+        // The protection status of the characterization and the context need to match
+        if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
+            return SkSurfaceCharacterization(); // return an invalid characterization
+        }
+#endif
+    }
+
     return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
                                      cacheMaxResourceBytes, ii, backendFormat,
                                      origin, sampleCnt,
@@ -80,6 +100,7 @@
                                      SkSurfaceCharacterization::MipMapped(isMipMapped),
                                      SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
                                      SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
+                                     isProtected,
                                      surfaceProps);
 }
 
diff --git a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
index f1eff5f..cea1478 100644
--- a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
+++ b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
@@ -105,6 +105,7 @@
                           SkSurfaceCharacterization::MipMapped(false),
                           SkSurfaceCharacterization::UsesGLFBO0(false),
                           SkSurfaceCharacterization::VulkanSecondaryCBCompatible(true),
+                          GrProtected(rtc->asRenderTargetProxy()->isProtected()),
                           this->props());
 
     return true;
@@ -145,6 +146,7 @@
     }
 
     GrBackendFormat rtcFormat = rtc->asRenderTargetProxy()->backendFormat();
+    GrProtected isProtected = GrProtected(rtc->asRenderTargetProxy()->isProtected());
 
     return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) &&
            characterization.cacheMaxResourceBytes() <= maxResourceBytes &&
@@ -156,6 +158,7 @@
            characterization.sampleCount() == rtc->numSamples() &&
            SkColorSpace::Equals(characterization.colorSpace(),
                                 rtc->colorSpaceInfo().colorSpace()) &&
+           characterization.isProtected() == isProtected &&
            characterization.surfaceProps() == rtc->surfaceProps();
 }
 
diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp
index 2459294..860e487 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -216,6 +216,7 @@
                           SkSurfaceCharacterization::MipMapped(mipmapped),
                           SkSurfaceCharacterization::UsesGLFBO0(usesGLFBO0),
                           SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
+                          GrProtected(rtc->asRenderTargetProxy()->isProtected()),
                           this->props());
     return true;
 }
@@ -300,6 +301,8 @@
         return false;
     }
 
+    GrProtected isProtected = GrProtected(rtc->asSurfaceProxy()->isProtected());
+
     return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) &&
            characterization.cacheMaxResourceBytes() <= maxResourceBytes &&
            characterization.origin() == rtc->origin() &&
@@ -310,6 +313,7 @@
            characterization.sampleCount() == rtc->numSamples() &&
            SkColorSpace::Equals(characterization.colorSpace(),
                                 rtc->colorSpaceInfo().colorSpace()) &&
+           characterization.isProtected() == isProtected &&
            characterization.surfaceProps() == rtc->surfaceProps();
 }
 
@@ -368,6 +372,7 @@
     desc.fFlags = kRenderTarget_GrSurfaceFlag;
     desc.fWidth = c.width();
     desc.fHeight = c.height();
+    desc.fIsProtected = c.isProtected();
     desc.fConfig = config;
     desc.fSampleCnt = c.sampleCount();