1/*
2 * Copyright 2017 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
9#ifndef SkCrossContextImageData_DEFINED
10#define SkCrossContextImageData_DEFINED
11
12#include "SkImage.h"
13
14#if SK_SUPPORT_GPU
15#include "GrExternalTextureData.h"
16#endif
17
18class SK_API SkCrossContextImageData : SkNoncopyable {
19public:
20    /**
21     *  Decodes and uploads the encoded data to a texture using the supplied GrContext, then
22     *  returns an instance of SkCrossContextImageData that can be used to transport that texture
23     *  to a different GrContext, across thread boundaries. The GrContext used here, and the one
24     *  used to reconstruct the texture-backed image later must be in the same GL share group,
25     *  or otherwise be able to share resources. After calling this, you *must* construct exactly
26     *  one SkImage from the returned value, using SkImage::MakeFromCrossContextImageData.
27     *
28     *  The texture will be decoded and uploaded to be suitable for use with surfaces that have the
29     *  supplied destination color space. The color space of the texture itself will be determined
30     *  from the encoded data.
31     */
32    static std::unique_ptr<SkCrossContextImageData> MakeFromEncoded(
33        GrContext*, sk_sp<SkData>, SkColorSpace* dstColorSpace);
34
35private:
36    SkCrossContextImageData(sk_sp<SkImage> image) : fImage(std::move(image)) {
37        SkASSERT(!fImage->isTextureBacked());
38    }
39
40#if SK_SUPPORT_GPU
41    SkCrossContextImageData(const GrBackendTextureDesc& desc,
42                            std::unique_ptr<GrExternalTextureData> textureData,
43                            SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace)
44            : fAlphaType(alphaType)
45            , fColorSpace(std::move(colorSpace))
46            , fDesc(desc)
47            , fTextureData(std::move(textureData)) {
48        // Point our texture desc at our copy of the backend information
49        fDesc.fTextureHandle = fTextureData->getBackendObject();
50    }
51#endif
52
53    // For non-GPU backed images
54    sk_sp<SkImage> fImage;
55
56#if SK_SUPPORT_GPU
57    // GPU-backed images store some generic information (needed to reconstruct the SkImage),
58    // and some backend-specific info (to reconstruct the texture).
59    SkAlphaType fAlphaType;
60    sk_sp<SkColorSpace> fColorSpace;
61    GrBackendTextureDesc fDesc;
62    std::unique_ptr<GrExternalTextureData> fTextureData;
63#endif
64
65    friend class SkImage;
66};
67
68#endif
69