bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 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 | #include "GrGLBufferImpl.h" |
| 9 | #include "GrGpuGL.h" |
| 10 | |
| 11 | #define GL_CALL(GPU, X) GR_GL_CALL(GPU->glInterface(), X) |
| 12 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 13 | #ifdef SK_DEBUG |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 14 | #define VALIDATE() this->validate() |
| 15 | #else |
| 16 | #define VALIDATE() do {} while(false) |
| 17 | #endif |
| 18 | |
bsalomon@google.com | 306b2ce | 2013-03-13 15:31:11 +0000 | [diff] [blame] | 19 | // GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer |
| 20 | // objects are implemented as client-side-arrays on tile-deferred architectures. |
| 21 | #define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW |
| 22 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 23 | GrGLBufferImpl::GrGLBufferImpl(GrGLGpu* gpu, const Desc& desc, GrGLenum bufferType) |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 24 | : fDesc(desc) |
| 25 | , fBufferType(bufferType) |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 26 | , fMapPtr(NULL) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 27 | if (0 == desc.fID) { |
| 28 | fCPUData = sk_malloc_flags(desc.fSizeInBytes, SK_MALLOC_THROW); |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 29 | fGLSizeInBytes = 0; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 30 | } else { |
| 31 | fCPUData = NULL; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 32 | // We assume that the GL buffer was created at the desc's size initially. |
| 33 | fGLSizeInBytes = fDesc.fSizeInBytes; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 34 | } |
| 35 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 36 | } |
| 37 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 38 | void GrGLBufferImpl::release(GrGLGpu* gpu) { |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 39 | VALIDATE(); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 40 | // make sure we've not been abandoned or already released |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 41 | if (fCPUData) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 42 | sk_free(fCPUData); |
| 43 | fCPUData = NULL; |
| 44 | } else if (fDesc.fID && !fDesc.fIsWrapped) { |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 45 | GL_CALL(gpu, DeleteBuffers(1, &fDesc.fID)); |
| 46 | if (GR_GL_ARRAY_BUFFER == fBufferType) { |
| 47 | gpu->notifyVertexBufferDelete(fDesc.fID); |
| 48 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 49 | SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 50 | gpu->notifyIndexBufferDelete(fDesc.fID); |
| 51 | } |
| 52 | fDesc.fID = 0; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 53 | fGLSizeInBytes = 0; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 54 | } |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 55 | fMapPtr = NULL; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 56 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void GrGLBufferImpl::abandon() { |
| 60 | fDesc.fID = 0; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 61 | fGLSizeInBytes = 0; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 62 | fMapPtr = NULL; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 63 | sk_free(fCPUData); |
| 64 | fCPUData = NULL; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 65 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 66 | } |
| 67 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 68 | void GrGLBufferImpl::bind(GrGLGpu* gpu) const { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 69 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 70 | if (GR_GL_ARRAY_BUFFER == fBufferType) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 71 | gpu->bindVertexBuffer(fDesc.fID); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 72 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 73 | SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 74 | gpu->bindIndexBufferAndDefaultVertexArray(fDesc.fID); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 75 | } |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 76 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 79 | void* GrGLBufferImpl::map(GrGLGpu* gpu) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 80 | VALIDATE(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 81 | SkASSERT(!this->isMapped()); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 82 | if (0 == fDesc.fID) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 83 | fMapPtr = fCPUData; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 84 | } else { |
| 85 | switch (gpu->glCaps().mapBufferType()) { |
| 86 | case GrGLCaps::kNone_MapBufferType: |
| 87 | VALIDATE(); |
| 88 | return NULL; |
| 89 | case GrGLCaps::kMapBuffer_MapBufferType: |
| 90 | this->bind(gpu); |
| 91 | // Let driver know it can discard the old data |
| 92 | if (GR_GL_USE_BUFFER_DATA_NULL_HINT || fDesc.fSizeInBytes != fGLSizeInBytes) { |
| 93 | fGLSizeInBytes = fDesc.fSizeInBytes; |
| 94 | GL_CALL(gpu, |
| 95 | BufferData(fBufferType, fGLSizeInBytes, NULL, |
| 96 | fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW)); |
| 97 | } |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 98 | GR_GL_CALL_RET(gpu->glInterface(), fMapPtr, |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 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(), |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 113 | fMapPtr, |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 114 | MapBufferRange(fBufferType, 0, fGLSizeInBytes, kAccess)); |
| 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(), |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 127 | fMapPtr, |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 128 | MapBufferSubData(fBufferType, 0, fGLSizeInBytes, GR_GL_WRITE_ONLY)); |
| 129 | break; |
| 130 | } |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 131 | } |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 132 | VALIDATE(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 133 | return fMapPtr; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 134 | } |
| 135 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 136 | void GrGLBufferImpl::unmap(GrGLGpu* gpu) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 137 | VALIDATE(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 138 | SkASSERT(this->isMapped()); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 139 | if (0 != fDesc.fID) { |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 140 | switch (gpu->glCaps().mapBufferType()) { |
| 141 | case GrGLCaps::kNone_MapBufferType: |
| 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); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 151 | GR_GL_CALL(gpu->glInterface(), UnmapBufferSubData(fMapPtr)); |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 152 | break; |
| 153 | } |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 154 | } |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 155 | fMapPtr = NULL; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 156 | } |
| 157 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 158 | bool GrGLBufferImpl::isMapped() const { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 159 | VALIDATE(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 160 | return SkToBool(fMapPtr); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 161 | } |
| 162 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 163 | bool GrGLBufferImpl::updateData(GrGLGpu* gpu, const void* src, size_t srcSizeInBytes) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 164 | SkASSERT(!this->isMapped()); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 165 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 166 | if (srcSizeInBytes > fDesc.fSizeInBytes) { |
| 167 | return false; |
| 168 | } |
| 169 | if (0 == fDesc.fID) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 170 | memcpy(fCPUData, src, srcSizeInBytes); |
| 171 | return true; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 172 | } |
| 173 | this->bind(gpu); |
bsalomon@google.com | 306b2ce | 2013-03-13 15:31:11 +0000 | [diff] [blame] | 174 | GrGLenum usage = fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 175 | |
| 176 | #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
| 177 | if (fDesc.fSizeInBytes == srcSizeInBytes) { |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 178 | GL_CALL(gpu, BufferData(fBufferType, (GrGLsizeiptr) srcSizeInBytes, src, usage)); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 179 | } else { |
| 180 | // Before we call glBufferSubData we give the driver a hint using |
| 181 | // glBufferData with NULL. This makes the old buffer contents |
| 182 | // inaccessible to future draws. The GPU may still be processing |
| 183 | // draws that reference the old contents. With this hint it can |
| 184 | // assign a different allocation for the new contents to avoid |
| 185 | // flushing the gpu past draws consuming the old contents. |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 186 | fGLSizeInBytes = fDesc.fSizeInBytes; |
| 187 | GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, NULL, usage)); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 188 | GL_CALL(gpu, BufferSubData(fBufferType, 0, (GrGLsizeiptr) srcSizeInBytes, src)); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 189 | } |
| 190 | #else |
| 191 | // Note that we're cheating on the size here. Currently no methods |
| 192 | // allow a partial update that preserves contents of non-updated |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 193 | // portions of the buffer (map() does a glBufferData(..size, NULL..)) |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 194 | bool doSubData = false; |
| 195 | #if GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND |
| 196 | static int N = 0; |
| 197 | // 128 was chosen experimentally. At 256 a slight hitchiness was noticed |
| 198 | // when dragging a Chromium window around with a canvas tab backgrounded. |
| 199 | doSubData = 0 == (N % 128); |
| 200 | ++N; |
| 201 | #endif |
| 202 | if (doSubData) { |
| 203 | // The workaround is to do a glBufferData followed by glBufferSubData. |
| 204 | // Chromium's command buffer may turn a glBufferSubData where the size |
| 205 | // exactly matches the buffer size into a glBufferData. So we tack 1 |
| 206 | // extra byte onto the glBufferData. |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 207 | fGLSizeInBytes = srcSizeInBytes + 1; |
| 208 | GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, NULL, usage)); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 209 | GL_CALL(gpu, BufferSubData(fBufferType, 0, srcSizeInBytes, src)); |
| 210 | } else { |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 211 | fGLSizeInBytes = srcSizeInBytes; |
| 212 | GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, src, usage)); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 213 | } |
| 214 | #endif |
| 215 | return true; |
| 216 | } |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 217 | |
| 218 | void GrGLBufferImpl::validate() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 219 | SkASSERT(GR_GL_ARRAY_BUFFER == fBufferType || GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); |
bsalomon@google.com | 1b8014c | 2013-05-08 12:49:49 +0000 | [diff] [blame] | 220 | // The following assert isn't valid when the buffer has been abandoned: |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 221 | // SkASSERT((0 == fDesc.fID) == (fCPUData)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 222 | SkASSERT(0 != fDesc.fID || !fDesc.fIsWrapped); |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 223 | SkASSERT(NULL == fCPUData || 0 == fGLSizeInBytes); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 224 | SkASSERT(NULL == fMapPtr || fCPUData || fGLSizeInBytes == fDesc.fSizeInBytes); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 225 | SkASSERT(NULL == fCPUData || NULL == fMapPtr || fCPUData == fMapPtr); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 226 | } |