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