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#ifndef GrMockTexture_DEFINED
8#define GrMockTexture_DEFINED
9
10#include "GrMockGpu.h"
11#include "GrRenderTarget.h"
12#include "GrTexture.h"
13#include "GrTexturePriv.h"
14#include "mock/GrMockTypes.h"
15
16class GrMockTexture : public GrTexture {
17public:
18    GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
19                  GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info)
20            : GrMockTexture(gpu, desc, mipMapsStatus, info) {
21        this->registerWithCache(budgeted);
22    }
23    ~GrMockTexture() override {}
24
25    GrBackendObject getTextureHandle() const override {
26        return reinterpret_cast<GrBackendObject>(&fInfo);
27    }
28    GrBackendTexture getBackendTexture() const override {
29        return GrBackendTexture(this->width(), this->height(), this->config(),
30                                this->texturePriv().mipMapped(), fInfo);
31    }
32
33    void textureParamsModified() override {}
34    void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {
35        fReleaseHelper = std::move(releaseHelper);
36    }
37
38protected:
39    // constructor for subclasses
40    GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
41                  const GrMockTextureInfo& info)
42            : GrSurface(gpu, desc)
43            , INHERITED(gpu, desc, kITexture2DSampler_GrSLType, GrSamplerState::Filter::kMipMap,
44                        mipMapsStatus)
45            , fInfo(info) {}
46
47    bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
48        return false;
49    }
50
51private:
52    GrMockTextureInfo          fInfo;
53    sk_sp<GrReleaseProcHelper> fReleaseHelper;
54
55    typedef GrTexture INHERITED;
56};
57
58class GrMockTextureRenderTarget : public GrMockTexture, public GrRenderTarget {
59public:
60    GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
61                              GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo)
62            : GrSurface(gpu, desc)
63            , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
64            , GrRenderTarget(gpu, desc) {
65        this->registerWithCache(budgeted);
66    }
67    ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
68    GrBackendObject getRenderTargetHandle() const override { return 0; }
69
70    GrBackendRenderTarget getBackendRenderTarget() const override {
71        return GrBackendRenderTarget(); // invalid
72    }
73
74    bool canAttemptStencilAttachment() const override { return true; }
75    bool completeStencilAttachment() override { return true; }
76    GrTexture* asTexture() override { return this; }
77    GrRenderTarget* asRenderTarget() override { return this; }
78    const GrTexture* asTexture() const override { return this; }
79    const GrRenderTarget* asRenderTarget() const override { return this; }
80
81private:
82    void onAbandon() override {
83        GrRenderTarget::onAbandon();
84        GrMockTexture::onAbandon();
85    }
86
87    void onRelease() override {
88        GrRenderTarget::onRelease();
89        GrMockTexture::onRelease();
90    }
91
92    size_t onGpuMemorySize() const override {
93        int numColorSamples = this->numColorSamples();
94        if (numColorSamples > 1) {
95            // Add one to account for the resolve buffer.
96            ++numColorSamples;
97        }
98        return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
99                                      numColorSamples,
100                                      this->texturePriv().mipMapped());
101    }
102
103    void computeScratchKey(GrScratchKey* key) const override {
104        GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
105                                         true, this->numStencilSamples(),
106                                         this->texturePriv().mipMapped(), key);
107    }
108};
109
110#endif
111