GrGeometryBuffer.h revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1
2/*
3 * Copyright 2011 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
10#ifndef GrGeometryBuffer_DEFINED
11#define GrGeometryBuffer_DEFINED
12
13#include "GrResource.h"
14
15class GrGpu;
16
17/**
18 * Parent class for vertex and index buffers
19 */
20class GrGeometryBuffer : public GrResource {
21public:
22
23    /**
24     *Retrieves whether the buffer was created with the dynamic flag
25     *
26     * @return true if the buffer was created with the dynamic flag
27     */
28    bool dynamic() const { return fDynamic; }
29
30    /**
31     * Locks the buffer to be written by the CPU.
32     *
33     * The previous content of the buffer is invalidated. It is an error
34     * to draw from the buffer while it is locked. It is an error to call lock
35     * on an already locked buffer.
36     *
37     * @return a pointer to the data or NULL if the lock fails.
38     */
39    virtual void* lock() = 0;
40
41    /**
42     * Returns the same ptr that lock() returned at time of lock or NULL if the
43     * is not locked.
44     *
45     * @return ptr to locked buffer data or undefined if buffer is not locked.
46     */
47    virtual void* lockPtr() const = 0;
48
49    /**
50     * Unlocks the buffer.
51     *
52     * The pointer returned by the previous lock call will no longer be valid.
53     */
54    virtual void unlock() = 0;
55
56    /**
57     Queries whether the buffer has been locked.
58
59     @return true if the buffer is locked, false otherwise.
60     */
61    virtual bool isLocked() const = 0;
62
63    /**
64     * Updates the buffer data.
65     *
66     * The size of the buffer will be preserved. The src data will be
67     * placed at the begining of the buffer and any remaining contents will
68     * be undefined.
69     *
70     * @return returns true if the update succeeds, false otherwise.
71     */
72    virtual bool updateData(const void* src, size_t srcSizeInBytes) = 0;
73
74    /**
75     * Updates a portion of the buffer data.
76     *
77     * The contents of the buffer outside the update region are preserved.
78     *
79     * @return returns true if the update succeeds, false otherwise.
80     */
81    virtual bool updateSubData(const void* src,
82                               size_t srcSizeInBytes,
83                               size_t offset) = 0;
84
85    // GrResource overrides
86    virtual size_t sizeInBytes() const { return fSizeInBytes; }
87
88protected:
89    GrGeometryBuffer(GrGpu* gpu, size_t sizeInBytes, bool dynamic)
90        : INHERITED(gpu)
91        , fSizeInBytes(sizeInBytes)
92        , fDynamic(dynamic) {}
93
94private:
95    size_t   fSizeInBytes;
96    bool     fDynamic;
97
98    typedef GrResource INHERITED;
99};
100
101#endif
102