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