reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTraceMemoryDump.h" |
| 9 | #include "src/gpu/GrSemaphore.h" |
| 10 | #include "src/gpu/GrShaderCaps.h" |
| 11 | #include "src/gpu/GrTexturePriv.h" |
| 12 | #include "src/gpu/gl/GrGLGpu.h" |
| 13 | #include "src/gpu/gl/GrGLTexture.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 15 | #define GPUGL static_cast<GrGLGpu*>(this->getGpu()) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 16 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
| 17 | |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 18 | GrTextureType GrGLTexture::TextureTypeFromTarget(GrGLenum target) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 19 | switch (target) { |
| 20 | case GR_GL_TEXTURE_2D: |
| 21 | return GrTextureType::k2D; |
| 22 | case GR_GL_TEXTURE_RECTANGLE: |
| 23 | return GrTextureType::kRectangle; |
| 24 | case GR_GL_TEXTURE_EXTERNAL: |
| 25 | return GrTextureType::kExternal; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 26 | } |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 27 | SK_ABORT("Unexpected texture target"); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static inline GrGLenum target_from_texture_type(GrTextureType type) { |
| 31 | switch (type) { |
| 32 | case GrTextureType::k2D: |
| 33 | return GR_GL_TEXTURE_2D; |
| 34 | case GrTextureType::kRectangle: |
| 35 | return GR_GL_TEXTURE_RECTANGLE; |
| 36 | case GrTextureType::kExternal: |
| 37 | return GR_GL_TEXTURE_EXTERNAL; |
Robert Phillips | f209e88 | 2019-06-25 15:59:50 -0400 | [diff] [blame] | 38 | default: |
| 39 | SK_ABORT("Unexpected texture target"); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 40 | } |
| 41 | SK_ABORT("Unexpected texture type"); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 42 | } |
| 43 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 44 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 45 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const Desc& desc, |
| 46 | GrMipMapsStatus mipMapsStatus) |
| 47 | : GrSurface(gpu, desc.fSize, desc.fConfig, GrProtected::kNo) |
| 48 | , INHERITED(gpu, desc.fSize, desc.fConfig, GrProtected::kNo, |
| 49 | TextureTypeFromTarget(desc.fTarget), mipMapsStatus) |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 50 | , fParameters(sk_make_sp<GrGLTextureParameters>()) { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 51 | this->init(desc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 52 | this->registerWithCache(budgeted); |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 53 | if (GrGLFormatIsCompressed(desc.fFormat)) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 54 | this->setReadOnly(); |
| 55 | } |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 56 | } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 57 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 58 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, GrMipMapsStatus mipMapsStatus, |
| 59 | sk_sp<GrGLTextureParameters> parameters, GrWrapCacheable cacheable, |
| 60 | GrIOType ioType) |
| 61 | : GrSurface(gpu, desc.fSize, desc.fConfig, GrProtected::kNo) |
| 62 | , INHERITED(gpu, desc.fSize, desc.fConfig, GrProtected::kNo, |
| 63 | TextureTypeFromTarget(desc.fTarget), mipMapsStatus) |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 64 | , fParameters(std::move(parameters)) { |
| 65 | SkASSERT(fParameters); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 66 | this->init(desc); |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 67 | this->registerWithCacheWrapped(cacheable); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 68 | if (ioType == kRead_GrIOType) { |
| 69 | this->setReadOnly(); |
| 70 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 73 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, sk_sp<GrGLTextureParameters> parameters, |
| 74 | GrMipMapsStatus mipMapsStatus) |
| 75 | : GrSurface(gpu, desc.fSize, desc.fConfig, GrProtected::kNo) |
| 76 | , INHERITED(gpu, desc.fSize, desc.fConfig, GrProtected::kNo, |
| 77 | TextureTypeFromTarget(desc.fTarget), mipMapsStatus) { |
| 78 | SkASSERT(parameters || desc.fOwnership == GrBackendObjectOwnership::kOwned); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 79 | fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>(); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 80 | this->init(desc); |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 83 | void GrGLTexture::init(const Desc& desc) { |
| 84 | SkASSERT(0 != desc.fID); |
| 85 | SkASSERT(GrGLFormat::kUnknown != desc.fFormat); |
| 86 | fID = desc.fID; |
| 87 | fFormat = desc.fFormat; |
| 88 | fTextureIDOwnership = desc.fOwnership; |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 91 | GrGLenum GrGLTexture::target() const { |
| 92 | return target_from_texture_type(this->texturePriv().textureType()); |
| 93 | } |
| 94 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 95 | void GrGLTexture::onRelease() { |
Yuqian Li | 40aa85f | 2019-07-02 13:45:00 -0700 | [diff] [blame] | 96 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 97 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 98 | if (fID) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 99 | if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 100 | GL_CALL(DeleteTextures(1, &fID)); |
bsalomon | bcaefb0 | 2014-11-03 11:07:12 -0800 | [diff] [blame] | 101 | } |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 102 | fID = 0; |
bsalomon | bcaefb0 | 2014-11-03 11:07:12 -0800 | [diff] [blame] | 103 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 104 | INHERITED::onRelease(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 107 | void GrGLTexture::onAbandon() { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 108 | fID = 0; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 109 | INHERITED::onAbandon(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 112 | GrBackendTexture GrGLTexture::getBackendTexture() const { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 113 | GrGLTextureInfo info; |
| 114 | info.fTarget = target_from_texture_type(this->texturePriv().textureType()); |
| 115 | info.fID = fID; |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 116 | info.fFormat = GrGLFormatToEnum(fFormat); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 117 | return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), info, |
| 118 | fParameters); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 121 | GrBackendFormat GrGLTexture::backendFormat() const { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 122 | return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat), |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 123 | target_from_texture_type(this->texturePriv().textureType())); |
| 124 | } |
| 125 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 126 | sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, |
| 127 | GrMipMapsStatus mipMapsStatus, |
| 128 | const Desc& desc, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 129 | sk_sp<GrGLTextureParameters> parameters, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 130 | GrWrapCacheable cacheable, |
| 131 | GrIOType ioType) { |
| 132 | return sk_sp<GrGLTexture>( |
| 133 | new GrGLTexture(gpu, desc, mipMapsStatus, std::move(parameters), cacheable, ioType)); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 136 | bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture, |
| 137 | SkImage::BackendTextureReleaseProc* releaseProc) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 138 | *backendTexture = this->getBackendTexture(); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 139 | // Set the release proc to a no-op function. GL doesn't require any special cleanup. |
| 140 | *releaseProc = [](GrBackendTexture){}; |
| 141 | |
| 142 | // It's important that we only abandon this texture's objects, not subclass objects such as |
| 143 | // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be |
| 144 | // cleaned up by us. |
| 145 | this->GrGLTexture::onAbandon(); |
| 146 | return true; |
| 147 | } |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 148 | |
| 149 | void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 150 | // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget |
| 151 | // which is multiply inherited from both ourselves and a texture. In these cases, one part |
| 152 | // (texture, rt) may be wrapped, while the other is owned by Skia. |
| 153 | bool refsWrappedTextureObjects = |
| 154 | this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed; |
| 155 | if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the |
| 160 | // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This |
| 161 | // has no downside in the normal case. |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 162 | SkString resourceName = this->getResourceName(); |
| 163 | resourceName.append("/texture"); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 164 | |
| 165 | // As we are only dumping our texture memory (not any additional memory tracked by classes |
| 166 | // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid |
| 167 | // hitting an override. |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 168 | this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture", |
| 169 | GrGLTexture::gpuMemorySize()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 170 | |
| 171 | SkString texture_id; |
| 172 | texture_id.appendU32(this->textureID()); |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 173 | traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 174 | } |