| 1 |
/* |
| 2 |
* Copyright 2020 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
|
| 10 |
// This test only works with the Vulkan backend. |
| 11 |
#ifdef SK_VULKAN |
| 12 |
|
| 13 |
#include "include/core/SkCanvas.h" |
| 14 |
#include "include/core/SkColorSpace.h" |
| 15 |
#include "include/core/SkImage.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "tools/gpu/vk/VkYcbcrSamplerHelper.h" |
| 20 |
|
| 21 |
#if defined(SK_GANESH) |
| 22 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 23 |
#include "include/gpu/ganesh/SkImageGanesh.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#if defined(SK_GRAPHITE) |
| 27 |
#include "include/gpu/graphite/Image.h" |
| 28 |
#include "include/gpu/graphite/Recorder.h" |
| 29 |
#include "include/gpu/vk/VulkanBackendContext.h" |
| 30 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 31 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 32 |
#include "tools/graphite/vk/GraphiteVulkanTestContext.h" |
| 33 |
|
| 34 |
using VulkanTestContext = skiatest::graphite::VulkanTestContext; |
| 35 |
using SharedContext = skgpu::graphite::SharedContext; |
| 36 |
using VulkanSharedContext = skgpu::graphite::VulkanSharedContext; |
| 37 |
#endif |
| 38 |
|
| 39 |
static void release_ycbcrhelper(void* releaseContext) { |
| 40 |
VkYcbcrSamplerHelper* ycbcrHelper = reinterpret_cast<VkYcbcrSamplerHelper*>(releaseContext); |
| 41 |
delete ycbcrHelper; |
| 42 |
} |
| 43 |
|
| 44 |
namespace skiagm { |
| 45 |
|
| 46 |
// This GM exercises the native YCbCr image format on Vulkan |
| 47 |
class YCbCrImageGM : public GM { |
| 48 |
public: |
| 49 |
YCbCrImageGM() { |
| 50 |
this->setBGColor(0xFFCCCCCC); |
| 51 |
} |
| 52 |
|
| 53 |
protected: |
| 54 |
SkString getName() const override { return SkString("ycbcrimage"); } |
| 55 |
|
| 56 |
SkISize getISize() override { |
| 57 |
return SkISize::Make(2*kPad+kImageSize, 2*kPad+kImageSize); |
| 58 |
} |
| 59 |
|
| 60 |
#if defined(SK_GRAPHITE) |
| 61 |
DrawResult createYCbCrImage(skgpu::graphite::Recorder* recorder, |
| 62 |
SkString* errorMsg) { |
| 63 |
if (!recorder) { |
| 64 |
*errorMsg = "Cannot generate a YCbCr image without a valid GraphiteTestContext and " |
| 65 |
"recorder."; |
| 66 |
return skiagm::DrawResult::kSkip; |
| 67 |
} |
| 68 |
|
| 69 |
SkASSERT_RELEASE(recorder->backend() == skgpu::BackendApi::kVulkan); |
| 70 |
|
| 71 |
const VulkanSharedContext* vulkanSharedCtxt = |
| 72 |
static_cast<const VulkanSharedContext*>(recorder->priv().sharedContext()); |
| 73 |
SkASSERT(vulkanSharedCtxt); |
| 74 |
|
| 75 |
std::unique_ptr<VkYcbcrSamplerHelper> ycbcrHelper( |
| 76 |
new VkYcbcrSamplerHelper(vulkanSharedCtxt)); |
| 77 |
if (!ycbcrHelper) { |
| 78 |
*errorMsg = "Failed to create VkYcbcrSamplerHelper."; |
| 79 |
return skiagm::DrawResult::kFail; |
| 80 |
} |
| 81 |
if (!ycbcrHelper->isYCbCrSupported()) { |
| 82 |
*errorMsg = "YCbCr sampling is not supported."; |
| 83 |
return skiagm::DrawResult::kSkip; |
| 84 |
} |
| 85 |
if (!ycbcrHelper->createBackendTexture(kImageSize, kImageSize)) { |
| 86 |
*errorMsg = "Failed to create I420 backend texture."; |
| 87 |
return skiagm::DrawResult::kFail; |
| 88 |
} |
| 89 |
|
| 90 |
SkASSERT(!fYCbCrImage); |
| 91 |
|
| 92 |
fYCbCrImage = SkImages::WrapTexture(recorder, |
| 93 |
ycbcrHelper->backendTexture(), |
| 94 |
kRGB_888x_SkColorType, |
| 95 |
kPremul_SkAlphaType, |
| 96 |
/*colorSpace=*/nullptr, |
| 97 |
release_ycbcrhelper, |
| 98 |
ycbcrHelper.get()); |
| 99 |
SkASSERT(fYCbCrImage); |
| 100 |
ycbcrHelper.release(); |
| 101 |
if (!fYCbCrImage) { |
| 102 |
*errorMsg = "Failed to create I420 SkImage."; |
| 103 |
return DrawResult::kFail; |
| 104 |
} |
| 105 |
return DrawResult::kOk; |
| 106 |
} |
| 107 |
#endif // SK_GRAPHITE |
| 108 |
|
| 109 |
#if defined(SK_GANESH) |
| 110 |
DrawResult createYCbCrImage(GrDirectContext* dContext, SkString* errorMsg) { |
| 111 |
std::unique_ptr<VkYcbcrSamplerHelper> ycbcrHelper(new VkYcbcrSamplerHelper(dContext)); |
| 112 |
|
| 113 |
if (!ycbcrHelper->isYCbCrSupported()) { |
| 114 |
*errorMsg = "YCbCr sampling not supported."; |
| 115 |
return skiagm::DrawResult::kSkip; |
| 116 |
} |
| 117 |
|
| 118 |
if (!ycbcrHelper->createGrBackendTexture(kImageSize, kImageSize)) { |
| 119 |
*errorMsg = "Failed to create I420 backend texture."; |
| 120 |
return skiagm::DrawResult::kFail; |
| 121 |
} |
| 122 |
|
| 123 |
SkASSERT(!fYCbCrImage); |
| 124 |
fYCbCrImage = SkImages::BorrowTextureFrom(dContext, |
| 125 |
ycbcrHelper->grBackendTexture(), |
| 126 |
kTopLeft_GrSurfaceOrigin, |
| 127 |
kRGB_888x_SkColorType, |
| 128 |
kPremul_SkAlphaType, |
| 129 |
nullptr, |
| 130 |
release_ycbcrhelper, |
| 131 |
ycbcrHelper.get()); |
| 132 |
ycbcrHelper.release(); |
| 133 |
if (!fYCbCrImage) { |
| 134 |
*errorMsg = "Failed to create I420 image."; |
| 135 |
return DrawResult::kFail; |
| 136 |
} |
| 137 |
|
| 138 |
return DrawResult::kOk; |
| 139 |
} |
| 140 |
#endif |
| 141 |
|
| 142 |
DrawResult onGpuSetup(SkCanvas* canvas, |
| 143 |
SkString* errorMsg, |
| 144 |
GraphiteTestContext* graphiteTestContext) override { |
| 145 |
#if defined(SK_GRAPHITE) |
| 146 |
skgpu::graphite::Recorder* recorder = canvas->recorder(); |
| 147 |
|
| 148 |
if (recorder) { |
| 149 |
if (recorder->backend() != skgpu::BackendApi::kVulkan) { |
| 150 |
*errorMsg = "This GM requires using Vulkan."; |
| 151 |
return DrawResult::kSkip; |
| 152 |
} |
| 153 |
|
| 154 |
return this->createYCbCrImage(recorder, errorMsg); |
| 155 |
} |
| 156 |
#endif |
| 157 |
#if defined(SK_GANESH) |
| 158 |
if (GrDirectContext* dContext = GrAsDirectContext(canvas->recordingContext())) { |
| 159 |
if (dContext->abandoned()) { |
| 160 |
return DrawResult::kSkip; |
| 161 |
} |
| 162 |
|
| 163 |
if (dContext->backend() != GrBackendApi::kVulkan) { |
| 164 |
*errorMsg = "This GM requires a Vulkan context."; |
| 165 |
return DrawResult::kSkip; |
| 166 |
} |
| 167 |
|
| 168 |
DrawResult result = this->createYCbCrImage(dContext, errorMsg); |
| 169 |
if (result != DrawResult::kOk) { |
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
return DrawResult::kOk; |
| 174 |
} |
| 175 |
#endif |
| 176 |
return DrawResult::kSkip; |
| 177 |
} |
| 178 |
|
| 179 |
void onGpuTeardown() override { |
| 180 |
fYCbCrImage = nullptr; |
| 181 |
} |
| 182 |
|
| 183 |
DrawResult onDraw(SkCanvas* canvas, SkString*) override { |
| 184 |
SkASSERT(fYCbCrImage); |
| 185 |
canvas->drawImage(fYCbCrImage, kPad, kPad, SkSamplingOptions(SkFilterMode::kLinear)); |
| 186 |
return DrawResult::kOk; |
| 187 |
} |
| 188 |
|
| 189 |
private: |
| 190 |
static const int kImageSize = 112; |
| 191 |
static const int kPad = 8; |
| 192 |
|
| 193 |
sk_sp<SkImage> fYCbCrImage; |
| 194 |
|
| 195 |
using INHERITED = GM; |
| 196 |
}; |
| 197 |
|
| 198 |
////////////////////////////////////////////////////////////////////////////// |
| 199 |
|
| 200 |
DEF_GM(return new YCbCrImageGM;) |
| 201 |
|
| 202 |
} // namespace skiagm |
| 203 |
|
| 204 |
#endif // SK_VULKAN |