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