blob: 3aa81e57637ad9915cac097242ff86f8108d8fec [file] [log] [blame]
bsalomon@google.come49ad452013-02-20 19:33:20 +00001/*
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.org515dcd32013-08-28 14:17:03 +000013#ifdef SK_DEBUG
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000014#define VALIDATE() this->validate()
15#else
16#define VALIDATE() do {} while(false)
17#endif
18
bsalomon@google.com306b2ce2013-03-13 15:31:11 +000019// 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
bsalomon861e1032014-12-16 07:33:49 -080023GrGLBufferImpl::GrGLBufferImpl(GrGLGpu* gpu, const Desc& desc, GrGLenum bufferType)
bsalomon@google.come49ad452013-02-20 19:33:20 +000024 : fDesc(desc)
25 , fBufferType(bufferType)
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000026 , fMapPtr(NULL) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000027 if (0 == desc.fID) {
28 fCPUData = sk_malloc_flags(desc.fSizeInBytes, SK_MALLOC_THROW);
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000029 fGLSizeInBytes = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000030 } else {
31 fCPUData = NULL;
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000032 // We assume that the GL buffer was created at the desc's size initially.
33 fGLSizeInBytes = fDesc.fSizeInBytes;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000034 }
35 VALIDATE();
bsalomon@google.come49ad452013-02-20 19:33:20 +000036}
37
bsalomon861e1032014-12-16 07:33:49 -080038void GrGLBufferImpl::release(GrGLGpu* gpu) {
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000039 VALIDATE();
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000040 // make sure we've not been abandoned or already released
bsalomon49f085d2014-09-05 13:34:00 -070041 if (fCPUData) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000042 sk_free(fCPUData);
43 fCPUData = NULL;
44 } else if (fDesc.fID && !fDesc.fIsWrapped) {
bsalomon@google.come49ad452013-02-20 19:33:20 +000045 GL_CALL(gpu, DeleteBuffers(1, &fDesc.fID));
46 if (GR_GL_ARRAY_BUFFER == fBufferType) {
47 gpu->notifyVertexBufferDelete(fDesc.fID);
48 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000049 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType);
bsalomon@google.come49ad452013-02-20 19:33:20 +000050 gpu->notifyIndexBufferDelete(fDesc.fID);
51 }
52 fDesc.fID = 0;
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000053 fGLSizeInBytes = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +000054 }
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000055 fMapPtr = NULL;
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000056 VALIDATE();
bsalomon@google.come49ad452013-02-20 19:33:20 +000057}
58
59void GrGLBufferImpl::abandon() {
60 fDesc.fID = 0;
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000061 fGLSizeInBytes = 0;
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000062 fMapPtr = NULL;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000063 sk_free(fCPUData);
64 fCPUData = NULL;
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000065 VALIDATE();
bsalomon@google.come49ad452013-02-20 19:33:20 +000066}
67
bsalomon861e1032014-12-16 07:33:49 -080068void GrGLBufferImpl::bind(GrGLGpu* gpu) const {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000069 VALIDATE();
bsalomon@google.come49ad452013-02-20 19:33:20 +000070 if (GR_GL_ARRAY_BUFFER == fBufferType) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000071 gpu->bindVertexBuffer(fDesc.fID);
bsalomon@google.come49ad452013-02-20 19:33:20 +000072 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000073 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType);
bsalomon@google.com6918d482013-03-07 19:09:11 +000074 gpu->bindIndexBufferAndDefaultVertexArray(fDesc.fID);
bsalomon@google.come49ad452013-02-20 19:33:20 +000075 }
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000076 VALIDATE();
bsalomon@google.come49ad452013-02-20 19:33:20 +000077}
78
bsalomon861e1032014-12-16 07:33:49 -080079void* GrGLBufferImpl::map(GrGLGpu* gpu) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000080 VALIDATE();
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000081 SkASSERT(!this->isMapped());
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000082 if (0 == fDesc.fID) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000083 fMapPtr = fCPUData;
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000084 } 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.org8341eb72014-05-07 20:51:05 +000098 GR_GL_CALL_RET(gpu->glInterface(), fMapPtr,
commit-bot@chromium.org160b4782014-05-05 12:32:37 +000099 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.org8341eb72014-05-07 20:51:05 +0000113 fMapPtr,
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000114 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.org8341eb72014-05-07 20:51:05 +0000127 fMapPtr,
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000128 MapBufferSubData(fBufferType, 0, fGLSizeInBytes, GR_GL_WRITE_ONLY));
129 break;
130 }
bsalomon@google.come49ad452013-02-20 19:33:20 +0000131 }
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000132 VALIDATE();
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000133 return fMapPtr;
bsalomon@google.come49ad452013-02-20 19:33:20 +0000134}
135
bsalomon861e1032014-12-16 07:33:49 -0800136void GrGLBufferImpl::unmap(GrGLGpu* gpu) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000137 VALIDATE();
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000138 SkASSERT(this->isMapped());
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000139 if (0 != fDesc.fID) {
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000140 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.org8341eb72014-05-07 20:51:05 +0000151 GR_GL_CALL(gpu->glInterface(), UnmapBufferSubData(fMapPtr));
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000152 break;
153 }
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000154 }
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000155 fMapPtr = NULL;
bsalomon@google.come49ad452013-02-20 19:33:20 +0000156}
157
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000158bool GrGLBufferImpl::isMapped() const {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000159 VALIDATE();
bsalomon49f085d2014-09-05 13:34:00 -0700160 return SkToBool(fMapPtr);
bsalomon@google.come49ad452013-02-20 19:33:20 +0000161}
162
bsalomon861e1032014-12-16 07:33:49 -0800163bool GrGLBufferImpl::updateData(GrGLGpu* gpu, const void* src, size_t srcSizeInBytes) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000164 SkASSERT(!this->isMapped());
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000165 VALIDATE();
bsalomon@google.come49ad452013-02-20 19:33:20 +0000166 if (srcSizeInBytes > fDesc.fSizeInBytes) {
167 return false;
168 }
169 if (0 == fDesc.fID) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000170 memcpy(fCPUData, src, srcSizeInBytes);
171 return true;
bsalomon@google.come49ad452013-02-20 19:33:20 +0000172 }
173 this->bind(gpu);
bsalomon@google.com306b2ce2013-03-13 15:31:11 +0000174 GrGLenum usage = fDesc.fDynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW;
bsalomon@google.come49ad452013-02-20 19:33:20 +0000175
176#if GR_GL_USE_BUFFER_DATA_NULL_HINT
177 if (fDesc.fSizeInBytes == srcSizeInBytes) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000178 GL_CALL(gpu, BufferData(fBufferType, (GrGLsizeiptr) srcSizeInBytes, src, usage));
bsalomon@google.come49ad452013-02-20 19:33:20 +0000179 } 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.org160b4782014-05-05 12:32:37 +0000186 fGLSizeInBytes = fDesc.fSizeInBytes;
187 GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, NULL, usage));
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000188 GL_CALL(gpu, BufferSubData(fBufferType, 0, (GrGLsizeiptr) srcSizeInBytes, src));
bsalomon@google.come49ad452013-02-20 19:33:20 +0000189 }
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.org8341eb72014-05-07 20:51:05 +0000193 // portions of the buffer (map() does a glBufferData(..size, NULL..))
bsalomon@google.come49ad452013-02-20 19:33:20 +0000194 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.org160b4782014-05-05 12:32:37 +0000207 fGLSizeInBytes = srcSizeInBytes + 1;
208 GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, NULL, usage));
bsalomon@google.come49ad452013-02-20 19:33:20 +0000209 GL_CALL(gpu, BufferSubData(fBufferType, 0, srcSizeInBytes, src));
210 } else {
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000211 fGLSizeInBytes = srcSizeInBytes;
212 GL_CALL(gpu, BufferData(fBufferType, fGLSizeInBytes, src, usage));
bsalomon@google.come49ad452013-02-20 19:33:20 +0000213 }
214#endif
215 return true;
216}
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000217
218void GrGLBufferImpl::validate() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000219 SkASSERT(GR_GL_ARRAY_BUFFER == fBufferType || GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType);
bsalomon@google.com1b8014c2013-05-08 12:49:49 +0000220 // The following assert isn't valid when the buffer has been abandoned:
bsalomon49f085d2014-09-05 13:34:00 -0700221 // SkASSERT((0 == fDesc.fID) == (fCPUData));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000222 SkASSERT(0 != fDesc.fID || !fDesc.fIsWrapped);
commit-bot@chromium.org160b4782014-05-05 12:32:37 +0000223 SkASSERT(NULL == fCPUData || 0 == fGLSizeInBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700224 SkASSERT(NULL == fMapPtr || fCPUData || fGLSizeInBytes == fDesc.fSizeInBytes);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000225 SkASSERT(NULL == fCPUData || NULL == fMapPtr || fCPUData == fMapPtr);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000226}