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 GrMtlTexture_DEFINED
9#define GrMtlTexture_DEFINED
10
11#include "GrTexture.h"
12
13#import <Metal/Metal.h>
14
15class GrMtlGpu;
16
17class GrMtlTexture : public GrTexture {
18public:
19    static sk_sp<GrMtlTexture> CreateNewTexture(GrMtlGpu*, SkBudgeted budgeted,
20                                                const GrSurfaceDesc&, int mipLevels);
21
22    static sk_sp<GrMtlTexture> MakeWrappedTexture(GrMtlGpu*, const GrSurfaceDesc&,
23                                                  GrWrapOwnership);
24
25    ~GrMtlTexture() override;
26
27    id<MTLTexture> mtlTexture() const { return fTexture; }
28
29    GrBackendObject getTextureHandle() const override;
30    GrBackendTexture getBackendTexture() const override;
31
32    void textureParamsModified() override {}
33
34    bool reallocForMipmap(GrMtlGpu* gpu, uint32_t mipLevels);
35
36    void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {
37        // Since all MTLResources are inherently ref counted, we can call the Release proc when we
38        // delete the GrMtlTexture without worry of the MTLTexture getting deleted before it is done
39        // on the GPU.
40        fReleaseHelper = std::move(releaseHelper);
41    }
42
43protected:
44    GrMtlTexture(GrMtlGpu*, const GrSurfaceDesc&);
45
46    GrMtlGpu* getMtlGpu() const;
47
48    void onAbandon() override {
49        fTexture = nil;
50    }
51    void onRelease() override {
52        fTexture = nil;
53    }
54
55     bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
56         return false;
57     }
58
59private:
60    enum Wrapped { kWrapped };
61    GrMtlTexture(GrMtlGpu*, SkBudgeted, const GrSurfaceDesc&, id<MTLTexture>, GrMipMapsStatus);
62   // GrMtlTexture(GrMtlGpu*, Wrapped, const GrSurfaceDesc&, GrMtlImage::Wrapped wrapped);
63
64    id<MTLTexture> fTexture;
65
66    sk_sp<GrReleaseProcHelper>        fReleaseHelper;
67
68    typedef GrTexture INHERITED;
69};
70
71#endif
72