blob: f0fafabcba1477c561ee041557615c34a89dfa73 [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 Salomon60dd8c72018-07-30 10:24:13 -040018static inline GrTextureType texture_type_from_target(GrGLenum target) {
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;
cdalton9c3f1432016-03-11 10:07:37 -080026 }
Brian Salomon60dd8c72018-07-30 10:24:13 -040027 SK_ABORT("Unexpected texture target");
28 return GrTextureType::k2D;
29}
30
31static 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;
cdalton9c3f1432016-03-11 10:07:37 -080042}
43
Robert Phillips18166ee2017-06-01 12:55:44 -040044// This method parallels GrTextureProxy::highestFilterMode
Brian Salomon2bbdcc42017-09-07 12:36:34 -040045static inline GrSamplerState::Filter highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
46 GrPixelConfig config) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050047 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
48 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040049 return GrSamplerState::Filter::kBilerp;
Brian Salomon739c5bf2016-11-07 09:53:44 -050050 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040051 return GrSamplerState::Filter::kMipMap;
Brian Salomon739c5bf2016-11-07 09:53:44 -050052}
53
bsalomon37dd3312014-11-03 08:47:23 -080054// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070055GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040056 const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040057 : GrSurface(gpu, desc)
58 , INHERITED(gpu, desc, texture_type_from_target(idDesc.fInfo.fTarget),
59 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080060 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070061 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080062}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000063
Greg Daniel177e6952017-10-12 12:27:11 -040064GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
65 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc)
Brian Salomon60dd8c72018-07-30 10:24:13 -040066 : GrSurface(gpu, desc)
67 , INHERITED(gpu, desc, texture_type_from_target(idDesc.fInfo.fTarget),
68 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
kkinnunen2e6055b2016-04-22 01:48:29 -070069 this->init(desc, idDesc);
70 this->registerWithCacheWrapped();
71}
72
Robert Phillipsd6214d42016-11-07 08:23:48 -050073GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040074 GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040075 : GrSurface(gpu, desc)
76 , INHERITED(gpu, desc, texture_type_from_target(idDesc.fInfo.fTarget),
77 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080078 this->init(desc, idDesc);
79}
80
81void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080082 SkASSERT(0 != idDesc.fInfo.fID);
Greg Daniele7d8da42017-12-04 11:23:19 -050083 SkASSERT(0 != idDesc.fInfo.fFormat);
Robert Phillipsabf7b762018-03-21 12:13:37 -040084 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
85 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Greg Danielb73a9f82018-05-02 15:06:47 -040086 this->setIsGLTextureRectangleOrExternal();
Robert Phillipsabf7b762018-03-21 12:13:37 -040087 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +000088 fTexParams.invalidate();
89 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
Brian Salomon60dd8c72018-07-30 10:24:13 -040090 fID = idDesc.fInfo.fID;
91 fFormat = idDesc.fInfo.fFormat;
kkinnunen2e6055b2016-04-22 01:48:29 -070092 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000093}
94
Brian Salomon60dd8c72018-07-30 10:24:13 -040095GrGLenum GrGLTexture::target() const {
96 return target_from_texture_type(this->texturePriv().textureType());
97}
98
bsalomon@google.com8fe72472011-03-30 21:26:44 +000099void GrGLTexture::onRelease() {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400100 if (fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700101 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400102 GL_CALL(DeleteTextures(1, &fID));
bsalomonbcaefb02014-11-03 11:07:12 -0800103 }
Brian Salomon60dd8c72018-07-30 10:24:13 -0400104 fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -0800105 }
Greg Danielcef213c2017-04-21 11:52:27 -0400106 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000107 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +0000108}
109
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110void GrGLTexture::onAbandon() {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400111 fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -0400112 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000113 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000114}
115
Robert Phillipsb67821d2017-12-13 15:00:45 -0500116GrBackendTexture GrGLTexture::getBackendTexture() const {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400117 GrGLTextureInfo info;
118 info.fTarget = target_from_texture_type(this->texturePriv().textureType());
119 info.fID = fID;
120 info.fFormat = fFormat;
121 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), info);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500122}
123
bungeman6bd52842016-10-27 09:30:08 -0700124sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel177e6952017-10-12 12:27:11 -0400125 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc) {
126 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, mipMapsStatus, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700127}
128
Eric Karl914a36b2017-10-12 12:44:50 -0700129bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
130 SkImage::BackendTextureReleaseProc* releaseProc) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400131 *backendTexture = this->getBackendTexture();
Eric Karl914a36b2017-10-12 12:44:50 -0700132 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
133 *releaseProc = [](GrBackendTexture){};
134
135 // It's important that we only abandon this texture's objects, not subclass objects such as
136 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
137 // cleaned up by us.
138 this->GrGLTexture::onAbandon();
139 return true;
140}
Eric Karlaf770022018-03-19 13:04:03 -0700141
142void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
143 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
144 // which is multiply inherited from both ourselves and a texture. In these cases, one part
145 // (texture, rt) may be wrapped, while the other is owned by Skia.
146 bool refsWrappedTextureObjects =
147 this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
148 if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
149 return;
150 }
151
152 // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
153 // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
154 // has no downside in the normal case.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400155 SkString resourceName = this->getResourceName();
156 resourceName.append("/texture");
Eric Karlaf770022018-03-19 13:04:03 -0700157
158 // As we are only dumping our texture memory (not any additional memory tracked by classes
159 // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
160 // hitting an override.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400161 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
162 GrGLTexture::gpuMemorySize());
Eric Karlaf770022018-03-19 13:04:03 -0700163
164 SkString texture_id;
165 texture_id.appendU32(this->textureID());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400166 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
Eric Karlaf770022018-03-19 13:04:03 -0700167}