1 /*
2  * Copyright 2019 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 #ifndef YUVUtils_DEFINED
9 #define YUVUtils_DEFINED
10 
11 #include "include/core/SkImage.h"
12 #include "include/core/SkYUVAPixmaps.h"
13 #include "include/gpu/GrBackendSurface.h"
14 #include "src/core/SkAutoMalloc.h"
15 
16 class SkData;
17 
18 namespace sk_gpu_test {
19 
20 // Utility that decodes a JPEG but preserves the YUVA8 planes in the image, and uses
21 // MakeFromYUVAPixmaps to create a GPU multiplane YUVA image for a context. It extracts the planar
22 // data once, and lazily creates the actual SkImage when the GrContext is provided (and refreshes
23 // the image if the context has changed, as in Viewer)
24 class LazyYUVImage {
25 public:
26     // Returns null if the data could not be extracted into YUVA planes
27     static std::unique_ptr<LazyYUVImage> Make(sk_sp<SkData> data,
28                                               GrMipmapped = GrMipmapped::kNo,
29                                               sk_sp<SkColorSpace> = nullptr);
30     static std::unique_ptr<LazyYUVImage> Make(SkYUVAPixmaps,
31                                               GrMipmapped = GrMipmapped::kNo,
32                                               sk_sp<SkColorSpace> = nullptr);
33 
34     enum class Type { kFromPixmaps, kFromGenerator, kFromTextures };
35 
dimensions()36     SkISize dimensions() const { return fPixmaps.yuvaInfo().dimensions(); }
37 
38     sk_sp<SkImage> refImage(GrRecordingContext* rContext, Type);
39 
40 private:
41     // Decoded YUV data
42     SkYUVAPixmaps fPixmaps;
43 
44     GrMipmapped fMipmapped;
45 
46     sk_sp<SkColorSpace> fColorSpace;
47 
48     // Memoized SkImages formed with planes, one for each Type.
49     sk_sp<SkImage> fYUVImage[4];
50 
51     LazyYUVImage() = default;
52 
53     bool reset(sk_sp<SkData> data, GrMipmapped, sk_sp<SkColorSpace>);
54     bool reset(SkYUVAPixmaps pixmaps, GrMipmapped, sk_sp<SkColorSpace>);
55 
56     bool ensureYUVImage(GrRecordingContext* rContext, Type type);
57 };
58 
59 } // namespace sk_gpu_test
60 
61 #endif // YUVUtils_DEFINED
62