blob: 1a1ebbf0ccae0649525f908b99333f7d469277a9 [file] [log] [blame]
Michael Ludwigd9958f82019-03-21 13:08:36 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkImage.h"
Brian Salomonbe0e42c2020-08-27 11:00:04 -040012#include "include/core/SkYUVAPixmaps.h"
Robert Phillipsf105d382020-06-19 14:27:14 -040013#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkAutoMalloc.h"
Michael Ludwigd9958f82019-03-21 13:08:36 -040015
16class SkData;
17
18namespace 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)
24class LazyYUVImage {
25public:
Brian Salomon7db71392020-10-16 10:05:21 -040026 // 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);
Michael Ludwigd9958f82019-03-21 13:08:36 -040033
Brian Salomon8670c982020-12-08 16:39:32 -050034 enum class Type { kFromPixmaps, kFromGenerator, kFromTextures };
Michael Ludwigd9958f82019-03-21 13:08:36 -040035
Brian Salomon7db71392020-10-16 10:05:21 -040036 SkISize dimensions() const { return fPixmaps.yuvaInfo().dimensions(); }
37
38 sk_sp<SkImage> refImage(GrRecordingContext* rContext, Type);
Michael Ludwigd9958f82019-03-21 13:08:36 -040039
40private:
41 // Decoded YUV data
Brian Salomonbe0e42c2020-08-27 11:00:04 -040042 SkYUVAPixmaps fPixmaps;
Brian Salomon87d42e52020-08-24 09:18:16 -040043
Brian Salomon6db78b82020-07-31 08:57:48 -040044 GrMipmapped fMipmapped;
Michael Ludwigd9958f82019-03-21 13:08:36 -040045
Brian Salomon7db71392020-10-16 10:05:21 -040046 sk_sp<SkColorSpace> fColorSpace;
47
48 // Memoized SkImages formed with planes, one for each Type.
Brian Salomon694ff172020-11-04 16:54:28 -050049 sk_sp<SkImage> fYUVImage[4];
Michael Ludwigd9958f82019-03-21 13:08:36 -040050
Brian Salomonefb5f072020-07-28 21:06:43 -040051 LazyYUVImage() = default;
Michael Ludwigd9958f82019-03-21 13:08:36 -040052
Brian Salomon7db71392020-10-16 10:05:21 -040053 bool reset(sk_sp<SkData> data, GrMipmapped, sk_sp<SkColorSpace>);
54 bool reset(SkYUVAPixmaps pixmaps, GrMipmapped, sk_sp<SkColorSpace>);
Michael Ludwigd9958f82019-03-21 13:08:36 -040055
Brian Salomon7db71392020-10-16 10:05:21 -040056 bool ensureYUVImage(GrRecordingContext* rContext, Type type);
Michael Ludwigd9958f82019-03-21 13:08:36 -040057};
58
Michael Ludwigd9958f82019-03-21 13:08:36 -040059} // namespace sk_gpu_test
60
61#endif // YUVUtils_DEFINED