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