skia2/tests/BackendSurfaceMutableStateTest.cpp
Adlai Holler 14dc791bc4 Reland "Migrate SkImage::MakeFromTexture to GrRecordingContext"
This reverts commit 74b83a4ea9.

Reason for revert: Flutter is updated

Original change's description:
> Revert "Migrate SkImage::MakeFromTexture to GrRecordingContext"
> 
> This reverts commit daa9e7455d.
> 
> Reason for revert: Broke Build-Debian9-Clang-arm-Release-Flutter_Android_Docker
> 
> Original change's description:
> > Migrate SkImage::MakeFromTexture to GrRecordingContext
> > 
> > Android migration landed in Android CL 12234077
> > Chrome migration is landing in Chrome CL 2335812
> > 
> > Note: makeFromCompressedTexture is not used by Chrome.
> > 
> > Bug: skia:104662
> > Change-Id: Ibbe6d412cf22e87188926383d10b21f780208e48
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/305102
> > Commit-Queue: Adlai Holler <adlai@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
> > Auto-Submit: Adlai Holler <adlai@google.com>
> 
> TBR=bsalomon@google.com,robertphillips@google.com,adlai@google.com
> 
> Change-Id: I570945521c6cd78dfeea81e492b7e2b31dd0e6f5
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:104662
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308505
> Reviewed-by: Adlai Holler <adlai@google.com>
> Commit-Queue: Adlai Holler <adlai@google.com>

TBR=bsalomon@google.com,robertphillips@google.com,adlai@google.com

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: skia:104662
Change-Id: Ie1d7fd9e597e6e6e5bd91bec086e83a8c1f45a37
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/309321
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Adlai Holler <adlai@google.com>
Auto-Submit: Adlai Holler <adlai@google.com>
2020-08-12 18:35:51 +00:00

145 lines
6.6 KiB
C++

/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkImage.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/vk/GrVkTypes.h"
#include "src/gpu/GrContextPriv.h"
#include "src/gpu/GrTexture.h"
#include "src/gpu/GrTextureProxy.h"
#include "src/image/SkImage_Base.h"
#include "tests/Test.h"
#ifdef SK_VULKAN
#include "src/gpu/vk/GrVkGpu.h"
#include "src/gpu/vk/GrVkTexture.h"
DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendSurfaceMutableStateTest, reporter, ctxInfo) {
auto dContext = ctxInfo.directContext();
GrBackendFormat format = GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM);
GrBackendTexture backendTex = dContext->createBackendTexture(
32, 32, format, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
REPORTER_ASSERT(reporter, backendTex.isValid());
GrVkImageInfo info;
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
VkImageLayout initLayout = info.fImageLayout;
uint32_t initQueue = info.fCurrentQueueFamily;
GrBackendSurfaceMutableState initState(initLayout, initQueue);
// Verify that setting that state via a copy of a backendTexture is reflected in all the
// backendTextures.
GrBackendTexture backendTexCopy = backendTex;
REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
GrBackendSurfaceMutableState newState(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_QUEUE_FAMILY_IGNORED);
backendTexCopy.setMutableState(newState);
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
// Setting back to the init state since we didn't actually change it
backendTex.setMutableState(initState);
sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(dContext, backendTex,
kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
kPremul_SkAlphaType, nullptr);
const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext);
REPORTER_ASSERT(reporter, view);
REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
GrTexture* texture = view->proxy()->peekTexture();
REPORTER_ASSERT(reporter, texture);
// Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
REPORTER_ASSERT(reporter, initQueue == vkTexture->currentQueueFamilyIndex());
vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
// Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
backendTexImage.setMutableState(newState);
REPORTER_ASSERT(reporter,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == vkTexture->currentLayout());
REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
vkTexture->setQueueFamilyIndex(initQueue);
vkTexture->updateImageLayout(initLayout);
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
// Test using the setBackendTextureStateAPI. Unlike the previous test this will actually add
// real transitions to the image so we need to be careful about doing actual valid transitions.
GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
dContext->setBackendTextureState(backendTex, newState);
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
// To test queue transitions, we don't have any other valid queue available so instead we try
// to transition to external queue.
if (gpu->vkCaps().supportsExternalMemory()) {
GrBackendSurfaceMutableState externalState(VK_IMAGE_LAYOUT_GENERAL,
VK_QUEUE_FAMILY_EXTERNAL);
dContext->setBackendTextureState(backendTex, externalState);
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_EXTERNAL == info.fCurrentQueueFamily);
dContext->submit();
GrBackendSurfaceMutableState externalState2(VK_IMAGE_LAYOUT_GENERAL, initQueue);
dContext->setBackendTextureState(backendTex, externalState2);
REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
}
// We must submit this work before we try to delete the backend texture.
dContext->submit(true);
dContext->deleteBackendTexture(backendTex);
}
#endif