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