1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrGLBufferImpl_DEFINED
9 #define GrGLBufferImpl_DEFINED
10 
11 #include "SkTypes.h"
12 #include "gl/GrGLTypes.h"
13 
14 class GrGLGpu;
15 
16 /**
17  * This class serves as the implementation of GrGL*Buffer classes. It was written to avoid code
18  * duplication in those classes.
19  */
20 class GrGLBufferImpl : SkNoncopyable {
21 public:
22     enum Usage {
23         kStaticDraw_Usage = 0,
24         kDynamicDraw_Usage,
25         kStreamDraw_Usage,
26         kStreamRead_Usage,
27 
28         kLast_Usage = kStreamRead_Usage
29     };
30     static const int kUsageCount = kLast_Usage + 1;
31 
32     struct Desc {
33         GrGLuint    fID;            // set to 0 to indicate buffer is CPU-backed and not a VBO.
34         size_t      fSizeInBytes;
35         Usage       fUsage;
36     };
37 
38     GrGLBufferImpl(GrGLGpu*, const Desc&, GrGLenum bufferType);
~GrGLBufferImpl()39     ~GrGLBufferImpl() {
40         // either release or abandon should have been called by the owner of this object.
41         SkASSERT(0 == fDesc.fID);
42     }
43 
44     void abandon();
45     void release(GrGLGpu* gpu);
46 
bufferID()47     GrGLuint bufferID() const { return fDesc.fID; }
baseOffset()48     size_t baseOffset() const { return reinterpret_cast<size_t>(fCPUData); }
bufferType()49     GrGLenum bufferType() const { return fBufferType; }
50 
51     void* map(GrGLGpu* gpu);
52     void unmap(GrGLGpu* gpu);
53     bool isMapped() const;
54     bool updateData(GrGLGpu* gpu, const void* src, size_t srcSizeInBytes);
55 
56 private:
57     void validate() const;
58 
59     Desc         fDesc;
60     GrGLenum     fBufferType; // GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER, e.g.
61     void*        fCPUData;
62     void*        fMapPtr;
63     size_t       fGLSizeInBytes;     // In certain cases we make the size of the GL buffer object
64                                      // smaller or larger than the size in fDesc.
65 
66     typedef SkNoncopyable INHERITED;
67 };
68 
69 #endif
70