robertphillips@google.com | dd743fe | 2012-04-05 14:40:53 +0000 | [diff] [blame^] | 1 | |
| 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 | //////////////////////////////////////////////////////////////////////////////// |
| 15 | class GrBufferObj : public GrFakeRefObj { |
| 16 | GR_DEFINE_CREATOR(GrBufferObj); |
| 17 | |
| 18 | public: |
| 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 | |
| 72 | protected: |
| 73 | private: |
| 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 |