2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef YUVUtils_DEFINED
9 #define YUVUtils_DEFINED
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkYUVAPixmaps.h"
14 #include "include/gpu/GrBackendSurface.h"
15 #include "src/core/SkAutoMalloc.h"
21 namespace sk_gpu_test {
23 // Splits an input image into A8 YUV[A] planes using the passed subsampling and YUV color space. If
24 // the src image is opaque there will be three planes (Y, U, and V) and if not there will be a
25 // fourth A plane. The planes are returned along with a SkYUVAInfo describing the resulting planar
26 // image. Images are made as textures if GrRecordingContext is not null, otherwise as cpu images.
27 std::tuple<std::array<sk_sp<SkImage>, SkYUVAInfo::kMaxPlanes>, SkYUVAInfo>
28 MakeYUVAPlanesAsA8(SkImage*,
30 SkYUVAInfo::Subsampling,
33 // Utility that decodes a JPEG but preserves the YUVA8 planes in the image, and uses
34 // MakeFromYUVAPixmaps to create a GPU multiplane YUVA image for a context. It extracts the planar
35 // data once, and lazily creates the actual SkImage when the GrContext is provided (and refreshes
36 // the image if the context has changed, as in Viewer)
39 // Returns null if the data could not be extracted into YUVA planes
40 static std::unique_ptr<LazyYUVImage> Make(sk_sp<SkData> data,
41 GrMipmapped = GrMipmapped::kNo,
42 sk_sp<SkColorSpace> = nullptr);
43 static std::unique_ptr<LazyYUVImage> Make(SkYUVAPixmaps,
44 GrMipmapped = GrMipmapped::kNo,
45 sk_sp<SkColorSpace> = nullptr);
47 enum class Type { kFromPixmaps, kFromGenerator, kFromTextures };
49 SkISize dimensions() const { return fPixmaps.yuvaInfo().dimensions(); }
51 sk_sp<SkImage> refImage(GrRecordingContext* rContext, Type);
55 SkYUVAPixmaps fPixmaps;
57 GrMipmapped fMipmapped;
59 sk_sp<SkColorSpace> fColorSpace;
61 // Memoized SkImages formed with planes, one for each Type.
62 sk_sp<SkImage> fYUVImage[4];
64 LazyYUVImage() = default;
66 bool reset(sk_sp<SkData> data, GrMipmapped, sk_sp<SkColorSpace>);
67 bool reset(SkYUVAPixmaps pixmaps, GrMipmapped, sk_sp<SkColorSpace>);
69 bool ensureYUVImage(GrRecordingContext* rContext, Type type);
72 } // namespace sk_gpu_test
74 #endif // YUVUtils_DEFINED