1
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrGLNameAllocator_DEFINED
10#define GrGLNameAllocator_DEFINED
11
12#include "SkRefCnt.h"
13#include "gl/GrGLFunctions.h"
14
15/**
16 * This class assumes ownership of an explicit range of OpenGL object names and
17 * manages allocations within that range. This allows the app to generate new
18 * objects on the client side without making round trips to the GL server.
19 */
20class GrGLNameAllocator {
21public:
22    /**
23     * Constructs a name allocator that produces names within the explicit
24     * half-open range [firstName, end). Note that the caller will most likely
25     * need to call glGen* beforehand to reserve a range within the GL driver,
26     * and then invoke this constructor with that range.
27     *
28     * @param firstName The first name in the range owned by this class. Must be
29                        greater than zero.
30     * @param endName   The first past-the-end name beyond the range owned by
31                        this class. Must be >= firstName.
32     */
33    GrGLNameAllocator(GrGLuint firstName, GrGLuint endName);
34
35    /**
36     * Destructs the name allocator. The caller is responsible for calling the
37     * appropriate glDelete* on the range if necessary.
38     */
39    ~GrGLNameAllocator();
40
41    /**
42     * Return the beginning of this class's range.
43     *
44     * @return The first name in the range owned by this class.
45     */
46    GrGLuint firstName() const { return fFirstName; }
47
48    /**
49     * Return the end of this class's range. Note that endName() is not owned by
50     * this class.
51     *
52     * @return One plus the final name in the range owned by this class.
53     */
54    GrGLuint endName() const { return fEndName; }
55
56    /**
57     * Allocate an OpenGL object name from within this class's range.
58     *
59     * @return The name if one was available,
60               0 if all the names in the range were already in use.
61     */
62    GrGLuint allocateName();
63
64    /**
65     * Free an OpenGL object name, allowing it to be returned by a future call
66     * to allocateName(). Note that the caller should most likely redefine the
67     * object as empty to deallocate any underlying GPU memory before calling
68     * this method (but not call glDelete*, since that would free up the name
69     * within the driver itself).
70     *
71     * @param name The object name to free. Not-allocated names are silently
72     *             ignored the same way they are in the OpenGL spec.
73     */
74    void free(GrGLuint name);
75
76private:
77    class SparseNameRange;
78    class SparseNameTree;
79    class ContiguousNameRange;
80
81    const GrGLuint fFirstName;
82    const GrGLuint fEndName;
83    SkAutoTUnref<SparseNameRange> fAllocatedNames;
84};
85
86#endif
87