blob: e4cfee2a0ec79f14cf95ca52f416df17389010e5 [file] [log] [blame]
robertphillips@google.comdd743fe2012-04-05 14:40:53 +00001
2/*
3 * Copyright 2012 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 GrBufferObj_DEFINED
10#define GrBufferObj_DEFINED
11
12#include "GrFakeRefObj.h"
13
14////////////////////////////////////////////////////////////////////////////////
15class GrBufferObj : public GrFakeRefObj {
16 GR_DEFINE_CREATOR(GrBufferObj);
17
18public:
19 GrBufferObj()
20 : GrFakeRefObj()
21 , fDataPtr(NULL)
22 , fMapped(false)
23 , fBound(false)
24 , fSize(0)
25 , fUsage(GR_GL_STATIC_DRAW) {
26 }
27 virtual ~GrBufferObj() {
28 delete[] fDataPtr;
29 }
30
31 void access() {
32 // cannot access the buffer if it is currently mapped
33 GrAlwaysAssert(!fMapped);
34 }
35
36 void setMapped() { fMapped = true; }
37 void resetMapped() { fMapped = false; }
38 bool getMapped() const { return fMapped; }
39
40 void setBound() { fBound = true; }
41 void resetBound() { fBound = false; }
42 bool getBound() const { return fBound; }
43
44 void allocate(GrGLint size, const GrGLchar *dataPtr) {
45 GrAlwaysAssert(size >= 0);
46
47 // delete pre-existing data
48 delete[] fDataPtr;
49
50 fSize = size;
51 fDataPtr = new GrGLchar[size];
52 if (dataPtr) {
53 memcpy(fDataPtr, dataPtr, fSize);
54 }
55 // TODO: w/ no dataPtr the data is unitialized - this could be tracked
56 }
57 GrGLint getSize() const { return fSize; }
58 GrGLchar *getDataPtr() { return fDataPtr; }
59
60 GrGLint getUsage() const { return fUsage; }
61 void setUsage(GrGLint usage) { fUsage = usage; }
62
63 virtual void deleteAction() SK_OVERRIDE {
64
65 // buffers are automatically unmapped when deleted
66 this->resetMapped();
67
68 this->INHERITED::deleteAction();
69 }
70
71
72protected:
73private:
74
75 GrGLchar* fDataPtr;
76 bool fMapped; // is the buffer object mapped via "glMapBuffer"?
77 bool fBound; // is the buffer object bound via "glBindBuffer"?
78 GrGLint fSize; // size in bytes
79 GrGLint fUsage; // one of: GL_STREAM_DRAW, GL_STATIC_DRAW, GL_DYNAMIC_DRAW
80
81 typedef GrFakeRefObj INHERITED;
82};
83
84#endif // GrBufferObj_DEFINED