blob: 7ae57711355a0d659e85f8843d5850bd710d5cd3 [file] [log] [blame]
Robert Phillipsde2bca22020-06-11 12:34:03 -04001/*
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/SkImage.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040018#include "include/gpu/GrDirectContext.h"
Robert Phillipsde2bca22020-06-11 12:34:03 -040019#include "tools/gpu/vk/VkYcbcrSamplerHelper.h"
20
Robert Phillips5f0cda42020-06-15 14:26:58 -040021static void release_ycbcrhelper(void* releaseContext) {
22 VkYcbcrSamplerHelper* ycbcrHelper = reinterpret_cast<VkYcbcrSamplerHelper*>(releaseContext);
23 delete ycbcrHelper;
24}
Robert Phillipsde2bca22020-06-11 12:34:03 -040025
26namespace skiagm {
27
28// This GM exercises the native YCbCr image format on Vulkan
29class YCbCrImageGM : public GpuGM {
30public:
31 YCbCrImageGM() {
32 this->setBGColor(0xFFCCCCCC);
33 }
34
35protected:
36
37 SkString onShortName() override {
38 return SkString("ycbcrimage");
39 }
40
41 SkISize onISize() override {
42 return SkISize::Make(2*kPad+kImageSize, 2*kPad+kImageSize);
43 }
44
Robert Phillips057c33f2020-07-17 11:59:01 -040045 DrawResult createYCbCrImage(GrDirectContext* dContext, SkString* errorMsg) {
46 std::unique_ptr<VkYcbcrSamplerHelper> ycbcrHelper(new VkYcbcrSamplerHelper(dContext));
Robert Phillips5f0cda42020-06-15 14:26:58 -040047
48 if (!ycbcrHelper->isYCbCrSupported()) {
49 *errorMsg = "YCbCr sampling not supported.";
50 return skiagm::DrawResult::kSkip;
51 }
52
53 if (!ycbcrHelper->createBackendTexture(kImageSize, kImageSize)) {
54 *errorMsg = "Failed to create I420 backend texture.";
55 return skiagm::DrawResult::kFail;
56 }
57
58 SkASSERT(!fYCbCrImage);
Robert Phillips057c33f2020-07-17 11:59:01 -040059 fYCbCrImage = SkImage::MakeFromTexture(dContext, ycbcrHelper->backendTexture(),
Robert Phillips5f0cda42020-06-15 14:26:58 -040060 kTopLeft_GrSurfaceOrigin, kRGB_888x_SkColorType,
61 kPremul_SkAlphaType, nullptr,
62 release_ycbcrhelper, ycbcrHelper.get());
63 if (!fYCbCrImage) {
64 *errorMsg = "Failed to create I420 image.";
65 return DrawResult::kFail;
66 }
67
68 ycbcrHelper.release();
69 return DrawResult::kOk;
70 }
71
Robert Phillipsb87b39b2020-07-01 14:45:24 -040072 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipsc869ff72020-06-19 09:50:33 -040073 if (!context || context->abandoned()) {
Robert Phillips889d6132020-06-16 11:11:33 -040074 return DrawResult::kSkip;
75 }
76
Robert Phillips5f0cda42020-06-15 14:26:58 -040077 if (context->backend() != GrBackendApi::kVulkan) {
78 *errorMsg = "This GM requires a Vulkan context.";
79 return DrawResult::kSkip;
80 }
81
82 DrawResult result = this->createYCbCrImage(context, errorMsg);
83 if (result != DrawResult::kOk) {
84 return result;
85 }
86
87 return DrawResult::kOk;
88 }
89
Robert Phillipsb795bea2020-06-25 12:38:53 -040090 void onGpuTeardown() override {
91 fYCbCrImage = nullptr;
92 }
93
Robert Phillips95c250c2020-06-29 15:36:12 -040094 DrawResult onDraw(GrRecordingContext*, GrRenderTargetContext*,
95 SkCanvas* canvas, SkString*) override {
Robert Phillips5f0cda42020-06-15 14:26:58 -040096 SkASSERT(fYCbCrImage);
Robert Phillipsde2bca22020-06-11 12:34:03 -040097
98 SkPaint paint;
99 paint.setFilterQuality(kLow_SkFilterQuality);
100
Robert Phillips5f0cda42020-06-15 14:26:58 -0400101 canvas->drawImage(fYCbCrImage, kPad, kPad, &paint);
Robert Phillipsde2bca22020-06-11 12:34:03 -0400102 return DrawResult::kOk;
103 }
104
105private:
106 static const int kImageSize = 112;
107 static const int kPad = 8;
108
Robert Phillips5f0cda42020-06-15 14:26:58 -0400109 sk_sp<SkImage> fYCbCrImage;
110
John Stiles7571f9e2020-09-02 22:42:33 -0400111 using INHERITED = GpuGM;
Robert Phillipsde2bca22020-06-11 12:34:03 -0400112};
113
114//////////////////////////////////////////////////////////////////////////////
115
116DEF_GM(return new YCbCrImageGM;)
117
John Stilesa6841be2020-08-06 14:11:56 -0400118} // namespace skiagm
Robert Phillipsde2bca22020-06-11 12:34:03 -0400119
120#endif // SK_VULKAN