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/SkColorSpace.h" 12 #include "include/core/SkImage.h" 13 #include "include/core/SkYUVAPixmaps.h" 14 #include "include/gpu/GrBackendSurface.h" 15 #include "src/base/SkAutoMalloc.h" 16 17 #include <tuple> 18 19 class SkData; 20 21 namespace sk_gpu_test { 22 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*, 29 SkYUVColorSpace, 30 SkYUVAInfo::Subsampling, 31 GrRecordingContext*); 32 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) 37 class LazyYUVImage { 38 public: 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); 46 47 enum class Type { kFromPixmaps, kFromGenerator, kFromTextures }; 48 dimensions()49 SkISize dimensions() const { return fPixmaps.yuvaInfo().dimensions(); } 50 51 sk_sp<SkImage> refImage(GrRecordingContext* rContext, Type); 52 53 private: 54 // Decoded YUV data 55 SkYUVAPixmaps fPixmaps; 56 57 GrMipmapped fMipmapped; 58 59 sk_sp<SkColorSpace> fColorSpace; 60 61 // Memoized SkImages formed with planes, one for each Type. 62 sk_sp<SkImage> fYUVImage[4]; 63 64 LazyYUVImage() = default; 65 66 bool reset(sk_sp<SkData> data, GrMipmapped, sk_sp<SkColorSpace>); 67 bool reset(SkYUVAPixmaps pixmaps, GrMipmapped, sk_sp<SkColorSpace>); 68 69 bool ensureYUVImage(GrRecordingContext* rContext, Type type); 70 }; 71 72 } // namespace sk_gpu_test 73 74 #endif // YUVUtils_DEFINED 75