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 GrResourceProvider_DEFINED
9#define GrResourceProvider_DEFINED
10
11#include "GrTextureProvider.h"
12
13class GrIndexBuffer;
14class GrVertexBuffer;
15
16/**
17 * An extension of the texture provider for arbitrary resource types. This class is intended for
18 * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor
19 * derivatives).
20 */
21class GrResourceProvider : public GrTextureProvider {
22public:
23
24    GrResourceProvider(GrGpu* gpu, GrResourceCache* cache);
25
26    template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
27        return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
28    }
29
30    /**
31     * Either finds and refs, or creates an index buffer for instanced drawing with a specific
32     * pattern if the index buffer is not found. If the return is non-null, the caller owns
33     * a ref on the returned GrIndexBuffer.
34     *
35     * @param pattern     the pattern of indices to repeat
36     * @param patternSize size in bytes of the pattern
37     * @param reps        number of times to repeat the pattern
38     * @param vertCount   number of vertices the pattern references
39     * @param key         Key to be assigned to the index buffer.
40     *
41     * @return The index buffer if successful, otherwise NULL.
42     */
43    const GrIndexBuffer* refOrCreateInstancedIndexBuffer(const uint16_t* pattern,
44                                                         int patternSize,
45                                                         int reps,
46                                                         int vertCount,
47                                                         const GrUniqueKey& key) {
48        if (GrIndexBuffer* buffer = this->findAndRefTByUniqueKey<GrIndexBuffer>(key)) {
49            return buffer;
50        }
51        return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
52    }
53
54    /**
55     * Returns an index buffer that can be used to render quads.
56     * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
57     * The max number of quads can be queried using GrIndexBuffer::maxQuads().
58     * Draw with kTriangles_GrPrimitiveType
59     * @ return the quad index buffer
60     */
61    const GrIndexBuffer* refQuadIndexBuffer() {
62        if (GrIndexBuffer* buffer =
63            this->findAndRefTByUniqueKey<GrIndexBuffer>(fQuadIndexBufferKey)) {
64            return buffer;
65        }
66        return this->createQuadIndexBuffer();
67    }
68
69
70    using GrTextureProvider::assignUniqueKeyToResource;
71    using GrTextureProvider::findAndRefResourceByUniqueKey;
72    using GrTextureProvider::abandon;
73
74private:
75    const GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
76                                                    int patternSize,
77                                                    int reps,
78                                                    int vertCount,
79                                                    const GrUniqueKey& key);
80
81    const GrIndexBuffer* createQuadIndexBuffer();
82
83    GrUniqueKey fQuadIndexBufferKey;
84
85    typedef GrTextureProvider INHERITED;
86};
87
88#endif
89