1 /*
2  * Copyright 2015 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 GrYUVProvider_DEFINED
9 #define GrYUVProvider_DEFINED
10 
11 #include "GrTypes.h"
12 #include "SkImageInfo.h"
13 #include "SkYUVSizeInfo.h"
14 
15 class GrContext;
16 class GrTexture;
17 class GrTextureProxy;
18 
19 /**
20  *  There are at least 2 different ways to extract/retrieve YUV planar data...
21  *  - SkPixelRef
22  *  - SkImageGenerator
23  *
24  *  To share common functionality around using the planar data, we use this abstract base-class
25  *  to represent accessing that data.
26  */
27 class GrYUVProvider {
28 public:
29     virtual ~GrYUVProvider() {}
30 
31     /**
32      *  On success, this returns a texture proxy that has converted the YUV data from the provider
33      *  into a form that is supported by the GPU (typically transformed into RGB). The texture will
34      *  automatically have a key added, so it can be retrieved from the cache (assuming it is
35      *  requested by a provider w/ the same genID). If srcColorSpace and dstColorSpace are
36      *  specified, then a color conversion from src to dst will be applied to the pixels.
37      *
38      *  On failure (e.g. the provider had no data), this returns NULL.
39      */
40     sk_sp<GrTextureProxy> refAsTextureProxy(GrContext*, const GrSurfaceDesc&,
41                                             const SkColorSpace* srcColorSpace,
42                                             const SkColorSpace* dstColorSpace);
43 
44     virtual uint32_t onGetID() = 0;
45 
46     // These are not meant to be called by a client, only by the implementation
47 
48     /**
49      *  If decoding to YUV is supported, this returns true.  Otherwise, this
50      *  returns false and does not modify any of the parameters.
51      *
52      *  @param sizeInfo   Output parameter indicating the sizes and required
53      *                    allocation widths of the Y, U, and V planes.
54      *  @param colorSpace Output parameter.
55      */
56     virtual bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const = 0;
57 
58     /**
59      *  Returns true on success and false on failure.
60      *  This always attempts to perform a full decode.  If the client only
61      *  wants size, it should call onQueryYUV8().
62      *
63      *  @param sizeInfo   Needs to exactly match the values returned by the
64      *                    query, except the WidthBytes may be larger than the
65      *                    recommendation (but not smaller).
66      *  @param planes     Memory for each of the Y, U, and V planes.
67      */
68     virtual bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) = 0;
69 
70 private:
71     // This is used as release callback for the YUV data that we capture in an SkImage when
72     // uploading to a gpu. When the upload is complete and we release the SkImage this callback will
73     // release the underlying data.
74     static void YUVGen_DataReleaseProc(const void*, void* data);
75 };
76 
77 #endif
78