xref: /aosp_15_r20/external/skia/gm/imagefromyuvtextures.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 Google Inc.
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 the GPU backend.
9 
10 #include "gm/gm.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkColorFilter.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPixmap.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypes.h"
27 #include "include/gpu/ganesh/GrBackendSurface.h"
28 #include "include/gpu/ganesh/GrDirectContext.h"
29 #include "include/gpu/ganesh/GrTypes.h"
30 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
31 #include "include/private/base/SkTo.h"
32 #include "src/base/SkMathPriv.h"
33 #include "src/core/SkYUVMath.h"
34 #include "src/gpu/ganesh/GrDirectContextPriv.h"
35 #include "tools/DecodeUtils.h"
36 #include "tools/Resources.h"
37 #include "tools/gpu/YUVUtils.h"
38 
39 #if defined(SK_GRAPHITE)
40 #include "include/gpu/graphite/Surface.h"
41 #endif
42 
43 namespace skiagm {
44 class ImageFromYUV : public GM {
45 public:
46     enum class Source {
47         kTextures,
48         kImages,
49     };
50 
ImageFromYUV(Source source)51     ImageFromYUV(Source source) : fSource(source) {
52         this->setBGColor(0xFFFFFFFF);
53     }
54 
55 protected:
getName() const56     SkString getName() const override {
57         switch (fSource) {
58             case Source::kTextures: return SkString("image_from_yuv_textures");
59             case Source::kImages:   return SkString("image_from_yuv_images");
60         }
61         SkUNREACHABLE;
62     }
63 
getISize()64     SkISize getISize() override { return {1950, 800}; }
65 
CreatePlanes(const char * name)66     static std::unique_ptr<sk_gpu_test::LazyYUVImage> CreatePlanes(const char* name) {
67         SkBitmap bmp;
68         if (!ToolUtils::GetResourceAsBitmap(name, &bmp)) {
69             return {};
70         }
71         if (bmp.colorType() != kRGBA_8888_SkColorType) {
72             auto info = bmp.info().makeColorType(kRGBA_8888_SkColorType);
73             SkBitmap copy;
74             copy.allocPixels(info);
75             SkAssertResult(bmp.readPixels(copy.pixmap()));
76             bmp = copy;
77         }
78         SkYUVAPixmapInfo pixmapInfo({bmp.dimensions(),
79                                      SkYUVAInfo::PlaneConfig::kY_U_V_A,
80                                      SkYUVAInfo::Subsampling::k420,
81                                      kJPEG_Full_SkYUVColorSpace},
82                                     SkYUVAPixmapInfo::DataType::kUnorm8,
83                                     nullptr);
84         auto pixmaps = SkYUVAPixmaps::Allocate(pixmapInfo);
85 
86         unsigned char* yuvPixels[] = {
87                 static_cast<unsigned char*>(pixmaps.planes()[0].writable_addr()),
88                 static_cast<unsigned char*>(pixmaps.planes()[1].writable_addr()),
89                 static_cast<unsigned char*>(pixmaps.planes()[2].writable_addr()),
90                 static_cast<unsigned char*>(pixmaps.planes()[3].writable_addr()),
91         };
92 
93         float m[20];
94         SkColorMatrix_RGB2YUV(pixmaps.yuvaInfo().yuvColorSpace(), m);
95         // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
96         // we will draw it with all the supported yuv color spaces when converted back to RGB
97         for (int j = 0; j < pixmaps.planes()[0].height(); ++j) {
98             for (int i = 0; i < pixmaps.planes()[0].width(); ++i) {
99                 auto rgba = *bmp.getAddr32(i, j);
100                 auto r = (rgba & 0x000000ff) >>  0;
101                 auto g = (rgba & 0x0000ff00) >>  8;
102                 auto b = (rgba & 0x00ff0000) >> 16;
103                 auto a = (rgba & 0xff000000) >> 24;
104                 yuvPixels[0][j*pixmaps.planes()[0].width() + i] = SkToU8(
105                         sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4]));
106                 yuvPixels[3][j*pixmaps.planes()[0].width() + i] = SkToU8(sk_float_round2int(
107                         m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19]));
108             }
109         }
110         for (int j = 0; j < pixmaps.planes()[1].height(); ++j) {
111             for (int i = 0; i < pixmaps.planes()[1].width(); ++i) {
112                 // Average together 4 pixels of RGB.
113                 int rgba[] = {0, 0, 0, 0};
114                 int denom = 0;
115                 int ylimit = std::min(2*j + 2, pixmaps.planes()[0].height());
116                 int xlimit = std::min(2*i + 2, pixmaps.planes()[0].width());
117                 for (int y = 2*j; y < ylimit; ++y) {
118                     for (int x = 2*i; x < xlimit; ++x) {
119                         auto src = *bmp.getAddr32(x, y);
120                         rgba[0] += (src & 0x000000ff) >> 0;
121                         rgba[1] += (src & 0x0000ff00) >> 8;
122                         rgba[2] += (src & 0x00ff0000) >> 16;
123                         rgba[3] += (src & 0xff000000) >> 24;
124                         ++denom;
125                     }
126                 }
127                 for (int c = 0; c < 4; ++c) {
128                     rgba[c] /= denom;
129                 }
130                 int uvIndex = j*pixmaps.planes()[1].width() + i;
131                 yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int(
132                         m[5]*rgba[0] + m[6]*rgba[1] + m[7]*rgba[2] + m[8]*rgba[3] + 255*m[9]));
133                 yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int(
134                         m[10]*rgba[0] + m[11]*rgba[1] + m[12]*rgba[2] + m[13]*rgba[3] + 255*m[14]));
135             }
136         }
137         return sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps), skgpu::Mipmapped::kYes);
138     }
139 
makeYUVAImage(GrDirectContext * context,skgpu::graphite::Recorder * recorder)140     sk_sp<SkImage> makeYUVAImage(GrDirectContext* context, skgpu::graphite::Recorder* recorder) {
141         SkASSERT(SkToBool(context) != SkToBool(recorder));
142         sk_gpu_test::LazyYUVImage::Type type;
143         switch (fSource) {
144             case Source::kTextures: type = sk_gpu_test::LazyYUVImage::Type::kFromTextures; break;
145             case Source::kImages:   type = sk_gpu_test::LazyYUVImage::Type::kFromImages;   break;
146         }
147         if (context) {
148             return fLazyYUVImage->refImage(context, type);
149         }
150 #if defined(SK_GRAPHITE)
151         return fLazyYUVImage->refImage(recorder, type);
152 #endif
153         return nullptr;
154     }
155 
createReferenceImage(GrDirectContext * dContext,skgpu::graphite::Recorder * recorder)156     sk_sp<SkImage> createReferenceImage(GrDirectContext* dContext,
157                                         skgpu::graphite::Recorder* recorder) {
158         auto planarImage = this->makeYUVAImage(dContext, recorder);
159         if (!planarImage) {
160             return nullptr;
161         }
162 
163         auto resultInfo = SkImageInfo::Make(fLazyYUVImage->dimensions(),
164                                             kRGBA_8888_SkColorType,
165                                             kPremul_SkAlphaType);
166         sk_sp<SkSurface> resultSurface;
167         if (dContext) {
168             resultSurface = SkSurfaces::RenderTarget(dContext,
169                                                      skgpu::Budgeted::kYes,
170                                                      resultInfo,
171                                                      1,
172                                                      kTopLeft_GrSurfaceOrigin,
173                                                      nullptr,
174                                                      /*shouldCreateWithMips=*/true);
175         }
176 #if defined(SK_GRAPHITE)
177         if (recorder) {
178             resultSurface = SkSurfaces::RenderTarget(recorder, resultInfo, skgpu::Mipmapped::kYes);
179         }
180 #endif
181         if (!resultSurface) {
182             return nullptr;
183         }
184 
185         resultSurface->getCanvas()->drawImage(std::move(planarImage), 0, 0);
186         return resultSurface->makeImageSnapshot();
187     }
188 
onGpuSetup(SkCanvas * canvas,SkString * errorMsg,GraphiteTestContext *)189     DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override {
190         auto dContext = GrAsDirectContext(canvas->recordingContext());
191         auto* recorder = canvas->recorder();
192 
193         if (!recorder && (!dContext || dContext->abandoned())) {
194             *errorMsg = "DirectContext or graphite::Recorder required to create YUV images";
195             return DrawResult::kSkip;
196         }
197 
198         if (dContext && !dContext->priv().caps()->mipmapSupport()) {
199             return DrawResult::kSkip;
200         }
201 
202         if (fSource == Source::kImages && dContext) {
203             *errorMsg = "YUV Image from SkImage planes not supported with Ganesh.";
204             return DrawResult::kSkip;
205         }
206 
207         if (!fLazyYUVImage) {
208             fLazyYUVImage = CreatePlanes("images/mandrill_128.png");
209         }
210 
211         // We make a version of this image for each draw because, if any draw flattens it to
212         // RGBA, then all subsequent draws would use the RGBA texture.
213         for (int i = 0; i < kNumImages; ++i) {
214             fYUVAImages[i] = this->makeYUVAImage(dContext, recorder);
215             if (!fYUVAImages[i]) {
216                 *errorMsg = "Couldn't create src YUVA image.";
217                 return DrawResult::kFail;
218             }
219         }
220 
221         fReferenceImage = this->createReferenceImage(dContext, recorder);
222         if (!fReferenceImage) {
223             *errorMsg = "Couldn't create reference YUVA image.";
224             return DrawResult::kFail;
225         }
226 
227         if (dContext) {
228             // Some backends (e.g., Vulkan) require all work be completed for backend textures
229             // before they are deleted. Since we don't know when we'll next have access to a
230             // direct context, flush all the work now.
231             dContext->flush();
232             dContext->submit(GrSyncCpu::kYes);
233         }
234 
235         return DrawResult::kOk;
236     }
237 
onGpuTeardown()238     void onGpuTeardown() override {
239         fLazyYUVImage.reset();
240         for (sk_sp<SkImage>& image : fYUVAImages) {
241             image.reset();
242         }
243         fReferenceImage.reset();
244     }
245 
getYUVAImage(int index)246     SkImage* getYUVAImage(int index) {
247         SkASSERT(index >= 0 && index < kNumImages);
248         return fYUVAImages[index].get();
249     }
250 
onDraw(SkCanvas * canvas)251     void onDraw(SkCanvas* canvas) override {
252         auto draw_image = [canvas](SkImage* image, const SkSamplingOptions& sampling) -> SkSize {
253             if (!image) {
254                 return {0, 0};
255             }
256             canvas->drawImage(image, 0, 0, sampling, nullptr);
257             return {SkIntToScalar(image->width()), SkIntToScalar(image->height())};
258         };
259 
260         auto draw_image_rect = [canvas](SkImage* image,
261                                         const SkSamplingOptions& sampling) -> SkSize {
262             if (!image) {
263                 return {0, 0};
264             }
265             auto subset = SkRect::Make(image->dimensions());
266             subset.inset(subset.width() * .05f, subset.height() * .1f);
267             auto dst = SkRect::MakeWH(subset.width(), subset.height());
268             canvas->drawImageRect(image, subset, dst, sampling, nullptr,
269                                   SkCanvas::kStrict_SrcRectConstraint);
270             return {dst.width(), dst.height()};
271         };
272 
273         auto draw_image_shader = [canvas](SkImage* image,
274                                           const SkSamplingOptions& sampling) -> SkSize {
275             if (!image) {
276                 return {0, 0};
277             }
278             SkMatrix m;
279             m.setRotate(45, image->width()/2.f, image->height()/2.f);
280             SkPaint paint;
281             paint.setShader(image->makeShader(SkTileMode::kMirror, SkTileMode::kDecal,
282                                               sampling, m));
283             auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height());
284             canvas->drawRect(rect, paint);
285             return {rect.width(), rect.height()};
286         };
287 
288         canvas->translate(kPad, kPad);
289         int imageIndex = 0;
290         using DrawSig = SkSize(SkImage* image, const SkSamplingOptions&);
291         using DF = std::function<DrawSig>;
292         for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) {
293             float wForDrawFunc = 0;
294             canvas->save();
295             for (auto scale : {1.f, 1.5f, 0.3f}) {
296                 float hForScale = 0;
297                 float wForScale = 0;
298                 canvas->save();
299                 // We exercise either bicubic or mipmaps depending on the scale.
300                 SkSamplingOptions samplings[] = {
301                         {SkFilterMode::kNearest},
302                         {SkFilterMode::kLinear},
303                         scale > 1.f
304                                 ? SkSamplingOptions{SkCubicResampler::CatmullRom()}
305                                 : SkSamplingOptions{SkFilterMode::kLinear, SkMipmapMode::kLinear}};
306 
307                 for (const auto& sampling : samplings) {
308                     float yuvAndRefH;
309                     canvas->save();
310                         canvas->scale(scale, scale);
311                         auto s1 = draw(this->getYUVAImage(imageIndex++), sampling);
312                         yuvAndRefH = kPad + std::ceil(scale * s1.height());
313                     canvas->restore();
314                     canvas->save();
315                         canvas->translate(0, yuvAndRefH);
316                         canvas->scale(scale, scale);
317                         auto s2 = draw(fReferenceImage.get(), sampling);
318                         yuvAndRefH += std::ceil(scale * s2.height());
319                     canvas->restore();
320 
321                     float thisW = std::ceil(scale * std::max(s1.width(), s2.width()));
322 
323                     SkPaint outline;
324                     outline.setColor(SK_ColorBLACK);
325                     outline.setStroke(true);
326                     outline.setAntiAlias(false);
327                     canvas->drawRect(SkRect::MakeXYWH(-1, -1, thisW + 1, yuvAndRefH + 1), outline);
328 
329                     thisW += kPad;
330                     yuvAndRefH += kPad;
331 
332                     canvas->translate(thisW, 0);
333 
334                     wForScale += thisW;
335                     hForScale = std::max(hForScale, yuvAndRefH);
336                 }
337                 canvas->restore();
338                 canvas->translate(0, hForScale);
339                 wForDrawFunc = std::max(wForScale, wForDrawFunc);
340             }
341             canvas->restore();
342             canvas->translate(wForDrawFunc, 0);
343         }
344      }
345 
346 private:
347     Source fSource;
348 
349     std::unique_ptr<sk_gpu_test::LazyYUVImage> fLazyYUVImage;
350 
351     // 3 draws x 3 scales x 4 filter qualities
352     inline static constexpr int kNumImages = 3 * 3 * 4;
353     sk_sp<SkImage> fYUVAImages[kNumImages];
354     sk_sp<SkImage> fReferenceImage;
355 
356     inline static constexpr SkScalar kPad = 10.0f;
357 
358     using INHERITED = GM;
359 };
360 
361 DEF_GM(return new ImageFromYUV(ImageFromYUV::Source::kTextures);)
362 DEF_GM(return new ImageFromYUV(ImageFromYUV::Source::kImages);)
363 }  // namespace skiagm
364