OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrGLBufferImpl.h" | 8 #include "GrGLBufferImpl.h" |
9 #include "GrGpuGL.h" | 9 #include "GrGpuGL.h" |
10 | 10 |
11 #define GL_CALL(GPU, X) GR_GL_CALL(GPU->glInterface(), X) | 11 #define GL_CALL(GPU, X) GR_GL_CALL(GPU->glInterface(), X) |
12 | 12 |
13 #ifdef SK_DEBUG | 13 #ifdef SK_DEBUG |
14 #define VALIDATE() this->validate() | 14 #define VALIDATE() this->validate() |
15 #else | 15 #else |
16 #define VALIDATE() do {} while(false) | 16 #define VALIDATE() do {} while(false) |
17 #endif | 17 #endif |
18 | 18 |
19 // GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a cli
ent's vertex buffer | 19 // GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a cli
ent's vertex buffer |
20 // objects are implemented as client-side-arrays on tile-deferred architectures. | 20 // objects are implemented as client-side-arrays on tile-deferred architectures. |
21 #define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW | 21 #define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW |
22 | 22 |
23 GrGLBufferImpl::GrGLBufferImpl(GrGpuGL* gpu, const Desc& desc, GrGLenum bufferTy
pe) | 23 GrGLBufferImpl::GrGLBufferImpl(GrGpuGL* gpu, const Desc& desc, GrGLenum bufferTy
pe) |
24 : fDesc(desc) | 24 : fDesc(desc) |
25 , fBufferType(bufferType) | 25 , fBufferType(bufferType) |
26 , fLockPtr(NULL) { | 26 , fLockPtr(NULL) { |
27 if (0 == desc.fID) { | 27 if (0 == desc.fID) { |
28 fCPUData = sk_malloc_flags(desc.fSizeInBytes, SK_MALLOC_THROW); | 28 fCPUData = sk_malloc_flags(desc.fSizeInBytes, SK_MALLOC_THROW); |
| 29 fGLSizeInBytes = 0; |
29 } else { | 30 } else { |
30 fCPUData = NULL; | 31 fCPUData = NULL; |
| 32 // We assume that the GL buffer was created at the desc's size initially
. |
| 33 fGLSizeInBytes = fDesc.fSizeInBytes; |
31 } | 34 } |
32 VALIDATE(); | 35 VALIDATE(); |
33 } | 36 } |
34 | 37 |
35 void GrGLBufferImpl::release(GrGpuGL* gpu) { | 38 void GrGLBufferImpl::release(GrGpuGL* gpu) { |
| 39 VALIDATE(); |
36 // make sure we've not been abandoned or already released | 40 // make sure we've not been abandoned or already released |
37 if (NULL != fCPUData) { | 41 if (NULL != fCPUData) { |
38 VALIDATE(); | |
39 sk_free(fCPUData); | 42 sk_free(fCPUData); |
40 fCPUData = NULL; | 43 fCPUData = NULL; |
41 } else if (fDesc.fID && !fDesc.fIsWrapped) { | 44 } else if (fDesc.fID && !fDesc.fIsWrapped) { |
42 VALIDATE(); | |
43 GL_CALL(gpu, DeleteBuffers(1, &fDesc.fID)); | 45 GL_CALL(gpu, DeleteBuffers(1, &fDesc.fID)); |
44 if (GR_GL_ARRAY_BUFFER == fBufferType) { | 46 if (GR_GL_ARRAY_BUFFER == fBufferType) { |
45 gpu->notifyVertexBufferDelete(fDesc.fID); | 47 gpu->notifyVertexBufferDelete(fDesc.fID); |
46 } else { | 48 } else { |
47 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); | 49 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); |
48 gpu->notifyIndexBufferDelete(fDesc.fID); | 50 gpu->notifyIndexBufferDelete(fDesc.fID); |
49 } | 51 } |
50 fDesc.fID = 0; | 52 fDesc.fID = 0; |
| 53 fGLSizeInBytes = 0; |
51 } | 54 } |
52 fLockPtr = NULL; | 55 fLockPtr = NULL; |
| 56 VALIDATE(); |
53 } | 57 } |
54 | 58 |
55 void GrGLBufferImpl::abandon() { | 59 void GrGLBufferImpl::abandon() { |
56 fDesc.fID = 0; | 60 fDesc.fID = 0; |
| 61 fGLSizeInBytes = 0; |
57 fLockPtr = NULL; | 62 fLockPtr = NULL; |
58 sk_free(fCPUData); | 63 sk_free(fCPUData); |
59 fCPUData = NULL; | 64 fCPUData = NULL; |
| 65 VALIDATE(); |
60 } | 66 } |
61 | 67 |
62 void GrGLBufferImpl::bind(GrGpuGL* gpu) const { | 68 void GrGLBufferImpl::bind(GrGpuGL* gpu) const { |
63 VALIDATE(); | 69 VALIDATE(); |
64 if (GR_GL_ARRAY_BUFFER == fBufferType) { | 70 if (GR_GL_ARRAY_BUFFER == fBufferType) { |
65 gpu->bindVertexBuffer(fDesc.fID); | 71 gpu->bindVertexBuffer(fDesc.fID); |
66 } else { | 72 } else { |
67 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); | 73 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); |
68 gpu->bindIndexBufferAndDefaultVertexArray(fDesc.fID); | 74 gpu->bindIndexBufferAndDefaultVertexArray(fDesc.fID); |
69 } | 75 } |
| 76 VALIDATE(); |
70 } | 77 } |
71 | 78 |
72 void* GrGLBufferImpl::lock(GrGpuGL* gpu) { | 79 void* GrGLBufferImpl::lock(GrGpuGL* gpu) { |
73 VALIDATE(); | 80 VALIDATE(); |
74 SkASSERT(!this->isLocked()); | 81 SkASSERT(!this->isLocked()); |
75 if (0 == fDesc.fID) { | 82 if (0 == fDesc.fID) { |
76 fLockPtr = fCPUData; | 83 fLockPtr = fCPUData; |
77 } else if (gpu->caps()->bufferLockSupport()) { | 84 } else { |
78 this->bind(gpu); | 85 switch (gpu->glCaps().mapBufferType()) { |
79 // Let driver know it can discard the old data | 86 case GrGLCaps::kNone_MapBufferType: |
80 GL_CALL(gpu, BufferData(fBufferType, | 87 VALIDATE(); |
81 (GrGLsizeiptr) fDesc.fSizeInBytes, | 88 return NULL; |
82 NULL, | 89 case GrGLCaps::kMapBuffer_MapBufferType: |
83 fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STA
TIC_DRAW)); | 90 this->bind(gpu); |
84 GR_GL_CALL_RET(gpu->glInterface(), | 91 // Let driver know it can discard the old data |
85 fLockPtr, | 92 if (GR_GL_USE_BUFFER_DATA_NULL_HINT || fDesc.fSizeInBytes != fGL
SizeInBytes) { |
86 MapBuffer(fBufferType, GR_GL_WRITE_ONLY)); | 93 fGLSizeInBytes = fDesc.fSizeInBytes; |
| 94 GL_CALL(gpu, |
| 95 BufferData(fBufferType, fGLSizeInBytes, NULL, |
| 96 fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR
_GL_STATIC_DRAW)); |
| 97 } |
| 98 GR_GL_CALL_RET(gpu->glInterface(), fLockPtr, |
| 99 MapBuffer(fBufferType, GR_GL_WRITE_ONLY)); |
| 100 break; |
| 101 case GrGLCaps::kMapBufferRange_MapBufferType: { |
| 102 this->bind(gpu); |
| 103 // Make sure the GL buffer size agrees with fDesc before mapping
. |
| 104 if (fDesc.fSizeInBytes != fGLSizeInBytes) { |
| 105 fGLSizeInBytes = fDesc.fSizeInBytes; |
| 106 GL_CALL(gpu, |
| 107 BufferData(fBufferType, fGLSizeInBytes, NULL, |
| 108 fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR
_GL_STATIC_DRAW)); |
| 109 } |
| 110 static const GrGLbitfield kAccess = GR_GL_MAP_INVALIDATE_BUFFER_
BIT | |
| 111 GR_GL_MAP_WRITE_BIT; |
| 112 GR_GL_CALL_RET(gpu->glInterface(), |
| 113 fLockPtr, |
| 114 MapBufferRange(fBufferType, 0, fGLSizeInBytes, kA
ccess)); |
| 115 break; |
| 116 } |
| 117 case GrGLCaps::kChromium_MapBufferType: |
| 118 this->bind(gpu); |
| 119 // Make sure the GL buffer size agrees with fDesc before mapping
. |
| 120 if (fDesc.fSizeInBytes != fGLSizeInBytes) { |
| 121 fGLSizeInBytes = fDesc.fSizeInBytes; |
| 122 GL_CALL(gpu, |
| 123 BufferData(fBufferType, fGLSizeInBytes, NULL, |
| 124 fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR
_GL_STATIC_DRAW)); |
| 125 } |
| 126 GR_GL_CALL_RET(gpu->glInterface(), |
| 127 fLockPtr, |
| 128 MapBufferSubData(fBufferType, 0, fGLSizeInBytes,
GR_GL_WRITE_ONLY)); |
| 129 break; |
| 130 } |
87 } | 131 } |
| 132 VALIDATE(); |
88 return fLockPtr; | 133 return fLockPtr; |
89 } | 134 } |
90 | 135 |
91 void GrGLBufferImpl::unlock(GrGpuGL* gpu) { | 136 void GrGLBufferImpl::unlock(GrGpuGL* gpu) { |
92 VALIDATE(); | 137 VALIDATE(); |
93 SkASSERT(this->isLocked()); | 138 SkASSERT(this->isLocked()); |
94 if (0 != fDesc.fID) { | 139 if (0 != fDesc.fID) { |
95 SkASSERT(gpu->caps()->bufferLockSupport()); | 140 switch (gpu->glCaps().mapBufferType()) { |
96 this->bind(gpu); | 141 case GrGLCaps::kNone_MapBufferType: |
97 GL_CALL(gpu, UnmapBuffer(fBufferType)); | 142 SkDEBUGFAIL("Shouldn't get here."); |
| 143 return; |
| 144 case GrGLCaps::kMapBuffer_MapBufferType: // fall through |
| 145 case GrGLCaps::kMapBufferRange_MapBufferType: |
| 146 this->bind(gpu); |
| 147 GL_CALL(gpu, UnmapBuffer(fBufferType)); |
| 148 break; |
| 149 case GrGLCaps::kChromium_MapBufferType: |
| 150 this->bind(gpu); |
| 151 GR_GL_CALL(gpu->glInterface(), UnmapBufferSubData(fLockPtr)); |
| 152 break; |
| 153 } |
98 } | 154 } |
99 fLockPtr = NULL; | 155 fLockPtr = NULL; |
100 } | 156 } |
101 | 157 |
102 bool GrGLBufferImpl::isLocked() const { | 158 bool GrGLBufferImpl::isLocked() const { |
103 VALIDATE(); | 159 VALIDATE(); |
104 return NULL != fLockPtr; | 160 return NULL != fLockPtr; |
105 } | 161 } |
106 | 162 |
107 bool GrGLBufferImpl::updateData(GrGpuGL* gpu, const void* src, size_t srcSizeInB
ytes) { | 163 bool GrGLBufferImpl::updateData(GrGpuGL* gpu, const void* src, size_t srcSizeInB
ytes) { |
(...skipping 12 matching lines...) Expand all Loading... |
120 #if GR_GL_USE_BUFFER_DATA_NULL_HINT | 176 #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
121 if (fDesc.fSizeInBytes == srcSizeInBytes) { | 177 if (fDesc.fSizeInBytes == srcSizeInBytes) { |
122 GL_CALL(gpu, BufferData(fBufferType, (GrGLsizeiptr) srcSizeInBytes, src,
usage)); | 178 GL_CALL(gpu, BufferData(fBufferType, (GrGLsizeiptr) srcSizeInBytes, src,
usage)); |
123 } else { | 179 } else { |
124 // Before we call glBufferSubData we give the driver a hint using | 180 // Before we call glBufferSubData we give the driver a hint using |
125 // glBufferData with NULL. This makes the old buffer contents | 181 // glBufferData with NULL. This makes the old buffer contents |
126 // inaccessible to future draws. The GPU may still be processing | 182 // inaccessible to future draws. The GPU may still be processing |
127 // draws that reference the old contents. With this hint it can | 183 // draws that reference the old contents. With this hint it can |
128 // assign a different allocation for the new contents to avoid | 184 // assign a different allocation for the new contents to avoid |
129 // flushing the gpu past draws consuming the old contents. | 185 // flushing the gpu past draws consuming the old contents. |
130 GL_CALL(gpu, BufferData(fBufferType, (GrGLsizeiptr) fDesc.fSizeInBytes,
NULL, usage)); | 186 fGLSizeInBytes = fDesc.fSizeInBytes; |
| 187 GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, NULL, usage)); |
131 GL_CALL(gpu, BufferSubData(fBufferType, 0, (GrGLsizeiptr) srcSizeInBytes
, src)); | 188 GL_CALL(gpu, BufferSubData(fBufferType, 0, (GrGLsizeiptr) srcSizeInBytes
, src)); |
132 } | 189 } |
133 #else | 190 #else |
134 // Note that we're cheating on the size here. Currently no methods | 191 // Note that we're cheating on the size here. Currently no methods |
135 // allow a partial update that preserves contents of non-updated | 192 // allow a partial update that preserves contents of non-updated |
136 // portions of the buffer (lock() does a glBufferData(..size, NULL..)) | 193 // portions of the buffer (lock() does a glBufferData(..size, NULL..)) |
137 bool doSubData = false; | 194 bool doSubData = false; |
138 #if GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND | 195 #if GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND |
139 static int N = 0; | 196 static int N = 0; |
140 // 128 was chosen experimentally. At 256 a slight hitchiness was noticed | 197 // 128 was chosen experimentally. At 256 a slight hitchiness was noticed |
141 // when dragging a Chromium window around with a canvas tab backgrounded. | 198 // when dragging a Chromium window around with a canvas tab backgrounded. |
142 doSubData = 0 == (N % 128); | 199 doSubData = 0 == (N % 128); |
143 ++N; | 200 ++N; |
144 #endif | 201 #endif |
145 if (doSubData) { | 202 if (doSubData) { |
146 // The workaround is to do a glBufferData followed by glBufferSubData. | 203 // The workaround is to do a glBufferData followed by glBufferSubData. |
147 // Chromium's command buffer may turn a glBufferSubData where the size | 204 // Chromium's command buffer may turn a glBufferSubData where the size |
148 // exactly matches the buffer size into a glBufferData. So we tack 1 | 205 // exactly matches the buffer size into a glBufferData. So we tack 1 |
149 // extra byte onto the glBufferData. | 206 // extra byte onto the glBufferData. |
150 GL_CALL(gpu, BufferData(fBufferType, srcSizeInBytes + 1, NULL, usage)); | 207 fGLSizeInBytes = srcSizeInBytes + 1; |
| 208 GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, NULL, usage)); |
151 GL_CALL(gpu, BufferSubData(fBufferType, 0, srcSizeInBytes, src)); | 209 GL_CALL(gpu, BufferSubData(fBufferType, 0, srcSizeInBytes, src)); |
152 } else { | 210 } else { |
153 GL_CALL(gpu, BufferData(fBufferType, srcSizeInBytes, src, usage)); | 211 fGLSizeInBytes = srcSizeInBytes; |
| 212 GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, src, usage)); |
154 } | 213 } |
155 #endif | 214 #endif |
156 return true; | 215 return true; |
157 } | 216 } |
158 | 217 |
159 void GrGLBufferImpl::validate() const { | 218 void GrGLBufferImpl::validate() const { |
160 SkASSERT(GR_GL_ARRAY_BUFFER == fBufferType || GR_GL_ELEMENT_ARRAY_BUFFER ==
fBufferType); | 219 SkASSERT(GR_GL_ARRAY_BUFFER == fBufferType || GR_GL_ELEMENT_ARRAY_BUFFER ==
fBufferType); |
161 // The following assert isn't valid when the buffer has been abandoned: | 220 // The following assert isn't valid when the buffer has been abandoned: |
162 // SkASSERT((0 == fDesc.fID) == (NULL != fCPUData)); | 221 // SkASSERT((0 == fDesc.fID) == (NULL != fCPUData)); |
163 SkASSERT(0 != fDesc.fID || !fDesc.fIsWrapped); | 222 SkASSERT(0 != fDesc.fID || !fDesc.fIsWrapped); |
| 223 SkASSERT(NULL == fCPUData || 0 == fGLSizeInBytes); |
| 224 SkASSERT(NULL == fLockPtr || NULL != fCPUData || fGLSizeInBytes == fDesc.fSi
zeInBytes); |
164 SkASSERT(NULL == fCPUData || NULL == fLockPtr || fCPUData == fLockPtr); | 225 SkASSERT(NULL == fCPUData || NULL == fLockPtr || fCPUData == fLockPtr); |
165 } | 226 } |
OLD | NEW |