blob: 2d674f4756e530ae47ee4d87db55cfed69c81411 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "GrGLTexture.h"
jvanverth39edf762014-12-22 11:44:19 -08009#include "GrGLGpu.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050010#include "GrSemaphore.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050011#include "GrShaderCaps.h"
Robert Phillipsb67821d2017-12-13 15:00:45 -050012#include "GrTexturePriv.h"
ericrk0a5fa482015-09-15 14:16:10 -070013#include "SkTraceMemoryDump.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon861e1032014-12-16 07:33:49 -080015#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
bsalomon@google.com0b77d682011-08-19 13:28:54 +000016#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
17
Brian Salomonbf7b6202016-11-11 16:08:03 -050018static inline GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, GrPixelConfig config,
19 const GrGLGpu* gpu) {
cdalton9c3f1432016-03-11 10:07:37 -080020 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon1edc5b92016-11-29 13:43:46 -050021 SkASSERT(gpu->caps()->shaderCaps()->externalTextureSupport());
egdaniel990dbc82016-07-13 14:09:30 -070022 return kTextureExternalSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080023 } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) {
24 SkASSERT(gpu->glCaps().rectangleTextureSupport());
egdaniel990dbc82016-07-13 14:09:30 -070025 return kTexture2DRectSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080026 } else {
27 SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D);
egdaniel990dbc82016-07-13 14:09:30 -070028 return kTexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080029 }
30}
31
Robert Phillips18166ee2017-06-01 12:55:44 -040032// This method parallels GrTextureProxy::highestFilterMode
Brian Salomon2bbdcc42017-09-07 12:36:34 -040033static inline GrSamplerState::Filter highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
34 GrPixelConfig config) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050035 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
36 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040037 return GrSamplerState::Filter::kBilerp;
Brian Salomon739c5bf2016-11-07 09:53:44 -050038 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040039 return GrSamplerState::Filter::kMipMap;
Brian Salomon739c5bf2016-11-07 09:53:44 -050040}
41
bsalomon37dd3312014-11-03 08:47:23 -080042// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070043GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040044 const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070045 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050046 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040047 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080048 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070049 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080050}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000051
Greg Daniel177e6952017-10-12 12:27:11 -040052GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
53 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc)
kkinnunen2e6055b2016-04-22 01:48:29 -070054 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050055 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel177e6952017-10-12 12:27:11 -040056 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
kkinnunen2e6055b2016-04-22 01:48:29 -070057 this->init(desc, idDesc);
58 this->registerWithCacheWrapped();
59}
60
Robert Phillipsd6214d42016-11-07 08:23:48 -050061GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040062 GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070063 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050064 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040065 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080066 this->init(desc, idDesc);
67}
68
69void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080070 SkASSERT(0 != idDesc.fInfo.fID);
Greg Daniele7d8da42017-12-04 11:23:19 -050071 SkASSERT(0 != idDesc.fInfo.fFormat);
Robert Phillipsabf7b762018-03-21 12:13:37 -040072 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
73 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Greg Danielb73a9f82018-05-02 15:06:47 -040074 this->setIsGLTextureRectangleOrExternal();
Robert Phillipsabf7b762018-03-21 12:13:37 -040075 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +000076 fTexParams.invalidate();
77 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080078 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070079 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000080}
81
bsalomon@google.com8fe72472011-03-30 21:26:44 +000082void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080083 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070084 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -070085 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -080086 }
bsalomon091f60c2015-11-10 11:54:56 -080087 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -080088 }
Greg Danielcef213c2017-04-21 11:52:27 -040089 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000090 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000091}
92
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -080094 fInfo.fTarget = 0;
95 fInfo.fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -040096 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000097 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +000098}
99
Robert Phillipsb67821d2017-12-13 15:00:45 -0500100GrBackendTexture GrGLTexture::getBackendTexture() const {
Greg Daniel8a3f55c2018-03-14 17:32:12 +0000101 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), fInfo);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500102}
103
bungeman6bd52842016-10-27 09:30:08 -0700104sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel177e6952017-10-12 12:27:11 -0400105 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc) {
106 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, mipMapsStatus, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700107}
108
Eric Karl914a36b2017-10-12 12:44:50 -0700109bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
110 SkImage::BackendTextureReleaseProc* releaseProc) {
Brian Salomon34df0d32018-03-23 18:23:23 -0400111 *backendTexture = GrBackendTexture(this->width(), this->height(),
112 this->texturePriv().mipMapped(), fInfo);
Eric Karl914a36b2017-10-12 12:44:50 -0700113 // 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 Karlaf770022018-03-19 13:04:03 -0700122
123void 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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400136 SkString resourceName = this->getResourceName();
137 resourceName.append("/texture");
Eric Karlaf770022018-03-19 13:04:03 -0700138
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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400142 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
143 GrGLTexture::gpuMemorySize());
Eric Karlaf770022018-03-19 13:04:03 -0700144
145 SkString texture_id;
146 texture_id.appendU32(this->textureID());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400147 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
Eric Karlaf770022018-03-19 13:04:03 -0700148}