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#ifndef SkPromiseImageTexture_DEFINED
9#define SkPromiseImageTexture_DEFINED
10
11#include "include/core/SkTypes.h"
12
13#if SK_SUPPORT_GPU
14#include "include/core/SkRefCnt.h"
15#include "include/gpu/GrBackendSurface.h"
16#include "include/private/GrResourceKey.h"
17/**
18 * This type is used to fulfill textures for PromiseImages. Once an instance is returned from a
19 * PromiseImageTextureFulfillProc the GrBackendTexture it wraps must remain valid until the
20 * corresponding PromiseImageTextureReleaseProc is called. For performance reasons it is
21 * recommended that the client reuse a single PromiseImageTexture each time a given texture
22 * is returned by the PromiseImageTextureFulfillProc rather than creating a new PromiseImageTexture
23 * representing the same underlying backend API texture. If the underlying texture is deleted (after
24 * PromiseImageTextureReleaseProc has been called if this was returned by a
25 * PromiseImageTextureFulfillProc) then this object should be disposed as the texture it represented
26 * cannot be used to fulfill again.
27 */
28class SK_API SkPromiseImageTexture : public SkNVRefCnt<SkPromiseImageTexture> {
29public:
30    SkPromiseImageTexture() = delete;
31    SkPromiseImageTexture(const SkPromiseImageTexture&) = delete;
32    SkPromiseImageTexture(SkPromiseImageTexture&&) = delete;
33    ~SkPromiseImageTexture();
34    SkPromiseImageTexture& operator=(const SkPromiseImageTexture&) = delete;
35    SkPromiseImageTexture& operator=(SkPromiseImageTexture&&) = delete;
36
37    static sk_sp<SkPromiseImageTexture> Make(const GrBackendTexture& backendTexture) {
38        if (!backendTexture.isValid()) {
39            return nullptr;
40        }
41        return sk_sp<SkPromiseImageTexture>(new SkPromiseImageTexture(backendTexture));
42    }
43
44    GrBackendTexture backendTexture() const { return fBackendTexture; }
45
46    void addKeyToInvalidate(uint32_t contextID, const GrUniqueKey& key);
47    uint32_t uniqueID() const { return fUniqueID; }
48
49#if GR_TEST_UTILS
50    SkTArray<GrUniqueKey> testingOnly_uniqueKeysToInvalidate() const;
51#endif
52
53private:
54    explicit SkPromiseImageTexture(const GrBackendTexture& backendTexture);
55
56    SkSTArray<1, GrUniqueKeyInvalidatedMessage> fMessages;
57    GrBackendTexture fBackendTexture;
58    uint32_t fUniqueID = SK_InvalidUniqueID;
59    static std::atomic<uint32_t> gUniqueID;
60};
61#endif // SK_SUPPORT_GPU
62
63#endif // SkPromiseImageTexture_DEFINED
64