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 Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 8 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "GrGLTexture.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 10 | #include "GrGLGpu.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 11 | #include "GrResourceProvider.h" |
Brian Osman | fe3b516 | 2017-03-02 15:09:20 -0500 | [diff] [blame] | 12 | #include "GrSemaphore.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 13 | #include "GrShaderCaps.h" |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 14 | #include "SkMakeUnique.h" |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 15 | #include "SkTraceMemoryDump.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 17 | #define GPUGL static_cast<GrGLGpu*>(this->getGpu()) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 18 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
| 19 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 20 | static inline GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, GrPixelConfig config, |
| 21 | const GrGLGpu* gpu) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 22 | if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 23 | SkASSERT(gpu->caps()->shaderCaps()->externalTextureSupport()); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 24 | SkASSERT(!GrPixelConfigIsSint(config)); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 25 | return kTextureExternalSampler_GrSLType; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 26 | } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) { |
| 27 | SkASSERT(gpu->glCaps().rectangleTextureSupport()); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 28 | SkASSERT(!GrPixelConfigIsSint(config)); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 29 | return kTexture2DRectSampler_GrSLType; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 30 | } else if (GrPixelConfigIsSint(config)) { |
Brian Salomon | a8f0002 | 2016-11-16 12:55:57 -0500 | [diff] [blame] | 31 | return kITexture2DSampler_GrSLType; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 32 | } else { |
| 33 | SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 34 | return kTexture2DSampler_GrSLType; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 38 | static inline GrSamplerParams::FilterMode highest_filter_mode(const GrGLTexture::IDDesc& idDesc, |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 39 | GrPixelConfig config) { |
| 40 | if (GrPixelConfigIsSint(config)) { |
| 41 | // Integer textures in GL can use GL_NEAREST_MIPMAP_NEAREST. This is a mode we don't support |
| 42 | // and don't currently have a use for. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 43 | return GrSamplerParams::kNone_FilterMode; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 44 | } |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 45 | if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE || |
| 46 | idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) { |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 47 | return GrSamplerParams::kBilerp_FilterMode; |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 48 | } |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 49 | return GrSamplerParams::kMipMap_FilterMode; |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 52 | // 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] | 53 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 54 | const IDDesc& idDesc) |
| 55 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 56 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
| 57 | highest_filter_mode(idDesc, desc.fConfig), false) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 58 | this->init(desc, idDesc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 59 | this->registerWithCache(budgeted); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 60 | } |
| 61 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 62 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 63 | const IDDesc& idDesc, |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 64 | bool wasMipMapDataProvided) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 65 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 66 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
| 67 | highest_filter_mode(idDesc, desc.fConfig), |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 68 | wasMipMapDataProvided) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 69 | this->init(desc, idDesc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 70 | this->registerWithCache(budgeted); |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 71 | } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 72 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 73 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, const IDDesc& idDesc) |
| 74 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 75 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
| 76 | highest_filter_mode(idDesc, desc.fConfig), false) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 77 | this->init(desc, idDesc); |
| 78 | this->registerWithCacheWrapped(); |
| 79 | } |
| 80 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 81 | GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc, |
| 82 | bool wasMipMapDataProvided) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 83 | : GrSurface(gpu, desc) |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 84 | , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu), |
| 85 | highest_filter_mode(idDesc, desc.fConfig), |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 86 | wasMipMapDataProvided) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 87 | this->init(desc, idDesc); |
| 88 | } |
| 89 | |
| 90 | void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 91 | SkASSERT(0 != idDesc.fInfo.fID); |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 92 | fTexParams.invalidate(); |
| 93 | fTexParamsTimestamp = GrGpu::kExpiredTimestamp; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 94 | fInfo = idDesc.fInfo; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 95 | fTextureIDOwnership = idDesc.fOwnership; |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 96 | } |
| 97 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 98 | void GrGLTexture::onRelease() { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 99 | if (fInfo.fID) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 100 | if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) { |
bsalomon | 7e68ab7 | 2016-04-13 14:29:25 -0700 | [diff] [blame] | 101 | GL_CALL(DeleteTextures(1, &fInfo.fID)); |
bsalomon | bcaefb0 | 2014-11-03 11:07:12 -0800 | [diff] [blame] | 102 | } |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 103 | fInfo.fID = 0; |
bsalomon | bcaefb0 | 2014-11-03 11:07:12 -0800 | [diff] [blame] | 104 | } |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 105 | this->invokeReleaseProc(); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 106 | INHERITED::onRelease(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 109 | void GrGLTexture::onAbandon() { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 110 | fInfo.fTarget = 0; |
| 111 | fInfo.fID = 0; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 112 | this->invokeReleaseProc(); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 113 | INHERITED::onAbandon(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | } |
| 115 | |
bsalomon@google.com | 08afc84 | 2012-10-25 18:56:10 +0000 | [diff] [blame] | 116 | GrBackendObject GrGLTexture::getTextureHandle() const { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 117 | return reinterpret_cast<GrBackendObject>(&fInfo); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 118 | } |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 119 | |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 120 | std::unique_ptr<GrExternalTextureData> GrGLTexture::detachBackendTexture() { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 121 | SkASSERT(!this->hasPendingIO()); |
Brian Osman | fe3b516 | 2017-03-02 15:09:20 -0500 | [diff] [blame] | 122 | |
| 123 | // Set up a semaphore to be signaled once the data is ready, and flush GL |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 124 | sk_sp<GrSemaphore> semaphore = this->getContext()->resourceProvider()->makeSemaphore(); |
Brian Osman | dc87c95 | 2017-04-28 13:57:38 -0400 | [diff] [blame^] | 125 | this->getGpu()->insertSemaphore(semaphore, true); |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 126 | |
| 127 | // Make a copy of our GL-specific information |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 128 | auto data = skstd::make_unique<GrGLExternalTextureData>(fInfo, std::move(semaphore), |
| 129 | this->getContext()); |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 130 | |
| 131 | // Ensure the cache can't reach this texture anymore |
| 132 | this->detachFromCache(); |
| 133 | |
| 134 | // Detach from the GL object, so we don't use it (or try to delete it when we're freed) |
| 135 | fInfo.fTarget = 0; |
| 136 | fInfo.fID = 0; |
| 137 | |
| 138 | return std::move(data); |
| 139 | } |
| 140 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 141 | void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump, |
| 142 | const SkString& dumpName) const { |
| 143 | SkString texture_id; |
| 144 | texture_id.appendU32(this->textureID()); |
| 145 | traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture", |
| 146 | texture_id.c_str()); |
| 147 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 148 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 149 | sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc, |
| 150 | const IDDesc& idDesc) { |
| 151 | return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, idDesc)); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 152 | } |
| 153 | |