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