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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #include "GrGLTexture.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 9 | #include "GrGLGpu.h" |
Brian Osman | fe3b516 | 2017-03-02 15:09:20 -0500 | [diff] [blame] | 10 | #include "GrSemaphore.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 11 | #include "GrShaderCaps.h" |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 12 | #include "GrTexturePriv.h" |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 13 | #include "SkTraceMemoryDump.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 | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 18 | static inline GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, GrPixelConfig config, |
| 19 | const GrGLGpu* gpu) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 20 | if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 21 | SkASSERT(gpu->caps()->shaderCaps()->externalTextureSupport()); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 22 | return kTextureExternalSampler_GrSLType; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 23 | } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) { |
| 24 | SkASSERT(gpu->glCaps().rectangleTextureSupport()); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 25 | return kTexture2DRectSampler_GrSLType; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 26 | } else { |
| 27 | SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 28 | return kTexture2DSampler_GrSLType; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 32 | // This method parallels GrTextureProxy::highestFilterMode |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 33 | static inline GrSamplerState::Filter highest_filter_mode(const GrGLTexture::IDDesc& idDesc, |
| 34 | GrPixelConfig config) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 35 | if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE || |
| 36 | idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 37 | return GrSamplerState::Filter::kBilerp; |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 38 | } |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 39 | return GrSamplerState::Filter::kMipMap; |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 40 | } |
| 41 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 42 | // 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] | 43 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 44 | const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 45 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 46 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 47 | highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 48 | this->init(desc, idDesc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 49 | this->registerWithCache(budgeted); |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 50 | } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 51 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 52 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, |
| 53 | GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 54 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 55 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 56 | highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 57 | this->init(desc, idDesc); |
| 58 | this->registerWithCacheWrapped(); |
| 59 | } |
| 60 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 61 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 62 | GrMipMapsStatus mipMapsStatus) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 63 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 64 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 65 | highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 66 | this->init(desc, idDesc); |
| 67 | } |
| 68 | |
| 69 | void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 70 | SkASSERT(0 != idDesc.fInfo.fID); |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 71 | SkASSERT(0 != idDesc.fInfo.fFormat); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 72 | if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE || |
| 73 | idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) { |
Greg Daniel | b73a9f8 | 2018-05-02 15:06:47 -0400 | [diff] [blame] | 74 | this->setIsGLTextureRectangleOrExternal(); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 75 | } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 76 | fTexParams.invalidate(); |
| 77 | fTexParamsTimestamp = GrGpu::kExpiredTimestamp; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 78 | fInfo = idDesc.fInfo; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 79 | fTextureIDOwnership = idDesc.fOwnership; |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 80 | } |
| 81 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 82 | void GrGLTexture::onRelease() { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 83 | if (fInfo.fID) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 84 | if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) { |
bsalomon | 7e68ab7 | 2016-04-13 14:29:25 -0700 | [diff] [blame] | 85 | GL_CALL(DeleteTextures(1, &fInfo.fID)); |
bsalomon | bcaefb0 | 2014-11-03 11:07:12 -0800 | [diff] [blame] | 86 | } |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 87 | fInfo.fID = 0; |
bsalomon | bcaefb0 | 2014-11-03 11:07:12 -0800 | [diff] [blame] | 88 | } |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 89 | this->invokeReleaseProc(); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 90 | INHERITED::onRelease(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 93 | void GrGLTexture::onAbandon() { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 94 | fInfo.fTarget = 0; |
| 95 | fInfo.fID = 0; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 96 | this->invokeReleaseProc(); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 97 | INHERITED::onAbandon(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 100 | GrBackendTexture GrGLTexture::getBackendTexture() const { |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 101 | return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), fInfo); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 102 | } |
| 103 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 104 | sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc, |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 105 | GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc) { |
| 106 | return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, mipMapsStatus, idDesc)); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 109 | bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture, |
| 110 | SkImage::BackendTextureReleaseProc* releaseProc) { |
Brian Salomon | 34df0d3 | 2018-03-23 18:23:23 -0400 | [diff] [blame] | 111 | *backendTexture = GrBackendTexture(this->width(), this->height(), |
| 112 | this->texturePriv().mipMapped(), fInfo); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 113 | // Set the release proc to a no-op function. GL doesn't require any special cleanup. |
| 114 | *releaseProc = [](GrBackendTexture){}; |
| 115 | |
| 116 | // It's important that we only abandon this texture's objects, not subclass objects such as |
| 117 | // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be |
| 118 | // cleaned up by us. |
| 119 | this->GrGLTexture::onAbandon(); |
| 120 | return true; |
| 121 | } |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 122 | |
| 123 | void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 124 | // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget |
| 125 | // which is multiply inherited from both ourselves and a texture. In these cases, one part |
| 126 | // (texture, rt) may be wrapped, while the other is owned by Skia. |
| 127 | bool refsWrappedTextureObjects = |
| 128 | this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed; |
| 129 | if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the |
| 134 | // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This |
| 135 | // has no downside in the normal case. |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 136 | SkString resourceName = this->getResourceName(); |
| 137 | resourceName.append("/texture"); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 138 | |
| 139 | // As we are only dumping our texture memory (not any additional memory tracked by classes |
| 140 | // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid |
| 141 | // hitting an override. |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 142 | this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture", |
| 143 | GrGLTexture::gpuMemorySize()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 144 | |
| 145 | SkString texture_id; |
| 146 | texture_id.appendU32(this->textureID()); |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 147 | traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 148 | } |