Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
rendertomipmappedyuvimageplanes.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2024 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// This test only works with Graphite.
9
10#include "gm/gm.h"
12
13#if defined(SK_GRAPHITE)
24#include "tools/DecodeUtils.h"
25
26namespace skiagm {
27class RenderToMipmappedYUVImagePlanes : public GM {
28public:
29 RenderToMipmappedYUVImagePlanes() { this->setBGColor(0xFFFFFFFF); }
30
31protected:
32 SkString getName() const override { return SkString("render_to_mipmapped_yuv_image_planes"); }
33
34 SkISize getISize() override { return {96, 32}; }
35
36 DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override {
37 auto* recorder = canvas->recorder();
38 if (!recorder) {
39 *errorMsg = "graphite-only test";
40 return DrawResult::kSkip;
41 }
42 if (!fSrcImage) {
43 fSrcImage = ToolUtils::GetResourceAsImage("images/mandrill_512.png");
44 if (!fSrcImage) {
45 *errorMsg = "Could not load src image.";
46 return DrawResult::kFail;
47 }
48 }
49
50 return DrawResult::kOk;
51 }
52
53 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
54 auto* recorder = canvas->recorder();
55 if (!recorder) {
56 *errorMsg = "direct to graphite test";
57 return DrawResult::kSkip;
58 }
59 using PlaneConfig = SkYUVAInfo::PlaneConfig;
60 using Subsampling = SkYUVAInfo::Subsampling;
61 struct TestCase {
62 PlaneConfig config;
63 Subsampling subsampling;
64 };
65 for (const auto& tc : {TestCase{PlaneConfig::kY_U_V, Subsampling::k420},
66 TestCase{PlaneConfig::kY_UV, Subsampling::k422},
67 TestCase{PlaneConfig::kYUV, Subsampling::k444}}) {
68 SkYUVAInfo yuvaInfo(fSrcImage->dimensions(),
69 tc.config,
70 tc.subsampling,
72
73 float rgbToYuv[20];
74 SkColorMatrix::RGBtoYUV(yuvaInfo.yuvColorSpace()).getRowMajor(rgbToYuv);
77 int numPlanes = yuvaInfo.planeDimensions(dimensions);
78
80 uint32_t channelFlags[SkYUVAInfo::kMaxPlanes];
81 for (int i = 0; i < numPlanes; ++i) {
82 switch (yuvaInfo.numChannelsInPlane(i)) {
83 case 1: colorTypes[i] = kAlpha_8_SkColorType; break;
84 case 2: colorTypes[i] = kR8G8_unorm_SkColorType; break;
85 case 3: colorTypes[i] = kRGB_888x_SkColorType; break;
86 case 4: colorTypes[i] = kRGBA_8888_SkColorType; break;
87
88 default: SkUNREACHABLE;
89 }
90 channelFlags[i] = SkColorTypeChannelFlags(colorTypes[i]);
91 }
92 SkYUVAInfo::YUVALocations locations = yuvaInfo.toYUVALocations(channelFlags);
93
94 for (int i = 0; i < numPlanes; ++i) {
95 auto info = SkImageInfo::Make(dimensions[i], colorTypes[i], kPremul_SkAlphaType);
96 auto surf = SkSurfaces::RenderTarget(recorder, info, skgpu::Mipmapped::kYes);
97
98 float matrix[20] {
99 1, 0, 0, 0, 0,
100 0, 1, 0, 0, 0,
101 0, 0, 1, 0, 0,
102 0, 0, 0, 1, 0
103 };
104
105 for (int c = 0; c < SkYUVAInfo::kYUVAChannelCount; ++c) {
106 if (locations[c].fPlane == i) {
107 auto d = static_cast<int>(locations[c].fChannel);
108 std::copy_n(rgbToYuv + 5 * c, 5, matrix + 5 * d);
109 }
110 }
111
112 auto cf = SkColorFilters::Matrix(matrix);
114 paint.setColorFilter(SkColorFilters::Matrix(matrix));
115 surf->getCanvas()->drawImageRect(fSrcImage,
116 SkRect::Make(surf->imageInfo().dimensions()),
118 &paint);
119 planes[i] = SkSurfaces::AsImage(std::move(surf));
120 }
121 auto yuvaImage = SkImages::TextureFromYUVAImages(recorder,
122 yuvaInfo,
123 planes,
124 /*imageColorSpace=*/nullptr);
125 auto dstRect = SkRect::MakeWH(yuvaImage->width() / 16.f, yuvaImage->height() / 16.f);
126 canvas->drawImageRect(yuvaImage.get(),
127 dstRect,
129 canvas->translate(std::ceil(dstRect.width()), 0);
130 }
131 return DrawResult::kOk;
132 }
133
134private:
135 sk_sp<SkImage> fSrcImage;
136};
137
138DEF_GM(return new RenderToMipmappedYUVImagePlanes();)
139} // namespace skiagm
140#endif
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkUNREACHABLE
Definition SkAssert.h:135
SkColorType
Definition SkColorType.h:19
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition SkColorType.h:43
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
static uint32_t SkColorTypeChannelFlags(SkColorType ct)
@ kJPEG_Full_SkYUVColorSpace
describes full range
Definition SkImageInfo.h:69
void translate(SkScalar dx, SkScalar dy)
virtual skgpu::graphite::Recorder * recorder() const
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static SkColorMatrix RGBtoYUV(SkYUVColorSpace)
void getRowMajor(float dst[20]) const
static constexpr int kMaxPlanes
Definition SkYUVAInfo.h:98
std::array< YUVALocation, kYUVAChannelCount > YUVALocations
Definition SkYUVAInfo.h:32
static constexpr int kYUVAChannelCount
Definition SkYUVAInfo.h:29
const Paint & paint
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
#define DEF_GM(CODE)
Definition gm.h:40
SK_API sk_sp< SkImage > TextureFromYUVAImages(skgpu::graphite::Recorder *recorder, const SkYUVAInfo &yuvaInfo, SkSpan< const sk_sp< SkImage > > images, sk_sp< SkColorSpace > imageColorSpace)
unsigned useCenter Optional< SkMatrix > matrix
Definition SkRecords.h:258
SK_API sk_sp< SkImage > AsImage(sk_sp< const SkSurface >)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
DrawResult
Definition gm.h:104
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609