blob: bfca6f41fe33e3c67d82613bc592f5cb459959ca [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"
12#include "include/core/SkYUVAIndex.h"
13#include "include/core/SkYUVASizeInfo.h"
Robert Phillipsf105d382020-06-19 14:27:14 -040014#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkAutoMalloc.h"
Michael Ludwigd9958f82019-03-21 13:08:36 -040016
17class SkData;
18
19namespace sk_gpu_test {
20
21// Utility that decodes a JPEG but preserves the YUVA8 planes in the image, and uses
22// MakeFromYUVAPixmaps to create a GPU multiplane YUVA image for a context. It extracts the planar
23// data once, and lazily creates the actual SkImage when the GrContext is provided (and refreshes
24// the image if the context has changed, as in Viewer)
25class LazyYUVImage {
26public:
27 // Returns null if the data could not be extracted into YUVA8 planes
28 static std::unique_ptr<LazyYUVImage> Make(sk_sp<SkData> data);
29
30 sk_sp<SkImage> refImage(GrContext* context);
31
32 const SkImage* getImage(GrContext* context);
33
34private:
35 // Decoded YUV data
36 SkYUVASizeInfo fSizeInfo;
37 SkYUVColorSpace fColorSpace;
38 SkYUVAIndex fComponents[SkYUVAIndex::kIndexCount];
39 SkAutoMalloc fPlaneData;
40 SkPixmap fPlanes[SkYUVASizeInfo::kMaxCount];
41
42 // Memoized SkImage formed with planes
43 sk_sp<SkImage> fYUVImage;
44 uint32_t fOwningContextID;
45
46 LazyYUVImage() : fOwningContextID(SK_InvalidGenID) {}
47
48 bool reset(sk_sp<SkData> data);
49
50 bool ensureYUVImage(GrContext* context);
51};
52
Robert Phillipsf105d382020-06-19 14:27:14 -040053// A helper for managing the lifetime of backend textures for YUVA images.
54class YUVABackendReleaseContext {
55public:
56 // A stock 'TextureReleaseProc' to use with this class
57 static void Release(void* releaseContext) {
58 auto beContext = reinterpret_cast<YUVABackendReleaseContext*>(releaseContext);
59
60 delete beContext;
61 }
62
63 // Given how and when backend textures are created, just deleting this object often
64 // isn't enough. This helper encapsulates the extra work needed.
65 static void Unwind(GrContext* context, YUVABackendReleaseContext* beContext);
66
67 YUVABackendReleaseContext(GrContext* context);
68 ~YUVABackendReleaseContext();
69
70 void set(int index, const GrBackendTexture& beTex) {
71 SkASSERT(index >= 0 && index < 4);
72 SkASSERT(!fBETextures[index].isValid());
73 SkASSERT(beTex.isValid());
74
75 fBETextures[index] = beTex;
76 }
77
78 const GrBackendTexture* beTextures() const { return fBETextures; }
79
80 const GrBackendTexture& beTexture(int index) {
81 SkASSERT(index >= 0 && index < 4);
82 SkASSERT(fBETextures[index].isValid());
83 return fBETextures[index];
84 }
85
86private:
87 GrContext* fContext;
88 GrBackendTexture fBETextures[4];
89};
90
91
Michael Ludwigd9958f82019-03-21 13:08:36 -040092} // namespace sk_gpu_test
93
94#endif // YUVUtils_DEFINED