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" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 9 | #include "GrGLGpu.h" |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 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 | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 19 | GrGLBufferImpl::GrGLBufferImpl(GrGLGpu* gpu, const Desc& desc, GrGLenum bufferType) |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 20 | : fDesc(desc) |
| 21 | , fBufferType(bufferType) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | , fMapPtr(nullptr) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 23 | if (0 == desc.fID) { |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 24 | if (gpu->caps()->mustClearUploadedBufferData()) { |
| 25 | fCPUData = sk_calloc_throw(desc.fSizeInBytes); |
| 26 | } else { |
| 27 | fCPUData = sk_malloc_flags(desc.fSizeInBytes, SK_MALLOC_THROW); |
| 28 | } |
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 { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | fCPUData = nullptr; |
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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | fCPUData = nullptr; |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 44 | } else if (fDesc.fID) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 45 | gpu->releaseBuffer(fDesc.fID, fBufferType); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 46 | fDesc.fID = 0; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 47 | fGLSizeInBytes = 0; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 48 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | fMapPtr = nullptr; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 50 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void GrGLBufferImpl::abandon() { |
| 54 | fDesc.fID = 0; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 55 | fGLSizeInBytes = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | fMapPtr = nullptr; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 57 | sk_free(fCPUData); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | fCPUData = nullptr; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 59 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 62 | void* GrGLBufferImpl::map(GrGLGpu* gpu) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 63 | VALIDATE(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 64 | SkASSERT(!this->isMapped()); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 65 | if (0 == fDesc.fID) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 66 | fMapPtr = fCPUData; |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 67 | } else { |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 68 | fMapPtr = gpu->mapBuffer(fDesc.fID, fBufferType, fDesc.fUsage, fGLSizeInBytes, |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 69 | fDesc.fSizeInBytes); |
| 70 | fGLSizeInBytes = fDesc.fSizeInBytes; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 71 | } |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 72 | VALIDATE(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 73 | return fMapPtr; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 74 | } |
| 75 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 76 | void GrGLBufferImpl::unmap(GrGLGpu* gpu) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 77 | VALIDATE(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 78 | SkASSERT(this->isMapped()); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 79 | if (0 != fDesc.fID) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 80 | gpu->unmapBuffer(fDesc.fID, fBufferType, fMapPtr); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 81 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 82 | fMapPtr = nullptr; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 83 | } |
| 84 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 85 | bool GrGLBufferImpl::isMapped() const { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 86 | VALIDATE(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 87 | return SkToBool(fMapPtr); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 90 | 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] | 91 | SkASSERT(!this->isMapped()); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 92 | SkASSERT(GR_GL_ARRAY_BUFFER == fBufferType || GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 93 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 94 | if (srcSizeInBytes > fDesc.fSizeInBytes) { |
| 95 | return false; |
| 96 | } |
| 97 | if (0 == fDesc.fID) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 98 | memcpy(fCPUData, src, srcSizeInBytes); |
| 99 | return true; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 100 | } |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 101 | gpu->bufferData(fDesc.fID, fBufferType, fDesc.fUsage, fDesc.fSizeInBytes, src, |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 102 | srcSizeInBytes); |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 103 | #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
| 104 | fGLSizeInBytes = fDesc.fSizeInBytes; |
| 105 | #else |
joshualitt | ccdbc1d | 2015-10-23 07:33:54 -0700 | [diff] [blame] | 106 | fGLSizeInBytes = srcSizeInBytes; |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 107 | #endif |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 108 | VALIDATE(); |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 111 | |
| 112 | void GrGLBufferImpl::validate() const { |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 113 | SkASSERT(GR_GL_ARRAY_BUFFER == fBufferType || GR_GL_ELEMENT_ARRAY_BUFFER == fBufferType || |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 114 | GR_GL_PIXEL_PACK_BUFFER == fBufferType || GR_GL_PIXEL_UNPACK_BUFFER == fBufferType || |
| 115 | GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM == fBufferType || |
| 116 | GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM == fBufferType); |
bsalomon@google.com | 1b8014c | 2013-05-08 12:49:49 +0000 | [diff] [blame] | 117 | // The following assert isn't valid when the buffer has been abandoned: |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 118 | // SkASSERT((0 == fDesc.fID) == (fCPUData)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 119 | SkASSERT(nullptr == fCPUData || 0 == fGLSizeInBytes); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 120 | SkASSERT(nullptr == fMapPtr || fCPUData || fGLSizeInBytes <= fDesc.fSizeInBytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 121 | SkASSERT(nullptr == fCPUData || nullptr == fMapPtr || fCPUData == fMapPtr); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 122 | } |