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 #include <tuple> 17 18 class SkData; 19 20 namespace sk_gpu_test { 21 22 // Splits an input image into A8 YUV[A] planes using the passed subsampling and YUV color space. If 23 // the src image is opaque there will be three planes (Y, U, and V) and if not there will be a 24 // fourth A plane. The planes are returned along with a SkYUVAInfo describing the resulting planar 25 // image. Images are made as textures if GrRecordingContext is not null, otherwise as cpu images. 26 std::tuple<std::array<sk_sp<SkImage>, SkYUVAInfo::kMaxPlanes>, SkYUVAInfo> 27 MakeYUVAPlanesAsA8(SkImage*, 28 SkYUVColorSpace, 29 SkYUVAInfo::Subsampling, 30 GrRecordingContext*); 31 32 // Utility that decodes a JPEG but preserves the YUVA8 planes in the image, and uses 33 // MakeFromYUVAPixmaps to create a GPU multiplane YUVA image for a context. It extracts the planar 34 // data once, and lazily creates the actual SkImage when the GrContext is provided (and refreshes 35 // the image if the context has changed, as in Viewer) 36 class LazyYUVImage { 37 public: 38 // Returns null if the data could not be extracted into YUVA planes 39 static std::unique_ptr<LazyYUVImage> Make(sk_sp<SkData> data, 40 GrMipmapped = GrMipmapped::kNo, 41 sk_sp<SkColorSpace> = nullptr); 42 static std::unique_ptr<LazyYUVImage> Make(SkYUVAPixmaps, 43 GrMipmapped = GrMipmapped::kNo, 44 sk_sp<SkColorSpace> = nullptr); 45 46 enum class Type { kFromPixmaps, kFromGenerator, kFromTextures }; 47 dimensions()48 SkISize dimensions() const { return fPixmaps.yuvaInfo().dimensions(); } 49 50 sk_sp<SkImage> refImage(GrRecordingContext* rContext, Type); 51 52 private: 53 // Decoded YUV data 54 SkYUVAPixmaps fPixmaps; 55 56 GrMipmapped fMipmapped; 57 58 sk_sp<SkColorSpace> fColorSpace; 59 60 // Memoized SkImages formed with planes, one for each Type. 61 sk_sp<SkImage> fYUVImage[4]; 62 63 LazyYUVImage() = default; 64 65 bool reset(sk_sp<SkData> data, GrMipmapped, sk_sp<SkColorSpace>); 66 bool reset(SkYUVAPixmaps pixmaps, GrMipmapped, sk_sp<SkColorSpace>); 67 68 bool ensureYUVImage(GrRecordingContext* rContext, Type type); 69 }; 70 71 } // namespace sk_gpu_test 72 73 #endif // YUVUtils_DEFINED 74