aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLNameAllocator.h
blob: 1c2c26e59f69e14798b0cc58bddd3c1888cad533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLNameAllocator_DEFINED
#define GrGLNameAllocator_DEFINED

#include "SkRefCnt.h"
#include "gl/GrGLFunctions.h"

/**
 * This class assumes ownership of an explicit range of OpenGL object names and
 * manages allocations within that range. This allows the app to generate new
 * objects on the client side without making round trips to the GL server.
 */
class GrGLNameAllocator {
public:
    /**
     * Constructs a name allocator that produces names within the explicit
     * half-open range [firstName, end). Note that the caller will most likely
     * need to call glGen* beforehand to reserve a range within the GL driver,
     * and then invoke this constructor with that range.
     *
     * @param firstName The first name in the range owned by this class. Must be
                        greater than zero.
     * @param endName   The first past-the-end name beyond the range owned by
                        this class. Must be >= firstName.
     */
    GrGLNameAllocator(GrGLuint firstName, GrGLuint endName);

    /**
     * Destructs the name allocator. The caller is responsible for calling the
     * appropriate glDelete* on the range if necessary.
     */
    ~GrGLNameAllocator();

    /**
     * Return the beginning of this class's range.
     *
     * @return The first name in the range owned by this class.
     */
    GrGLuint firstName() const { return fFirstName; }

    /**
     * Return the end of this class's range. Note that endName() is not owned by
     * this class.
     *
     * @return One plus the final name in the range owned by this class.
     */
    GrGLuint endName() const { return fEndName; }

    /**
     * Allocate an OpenGL object name from within this class's range.
     *
     * @return The name if one was available,
               0 if all the names in the range were already in use.
     */
    GrGLuint allocateName();

    /**
     * Free an OpenGL object name, allowing it to be returned by a future call
     * to allocateName(). Note that the caller should most likely redefine the
     * object as empty to deallocate any underlying GPU memory before calling
     * this method (but not call glDelete*, since that would free up the name
     * within the driver itself).
     *
     * @param name The object name to free. Not-allocated names are silently
     *             ignored the same way they are in the OpenGL spec.
     */
    void free(GrGLuint name);

private:
    class SparseNameRange;
    class SparseNameTree;
    class ContiguousNameRange;

    const GrGLuint fFirstName;
    const GrGLuint fEndName;
    SkAutoTUnref<SparseNameRange> fAllocatedNames;
};

#endif