blob: 8bc6d2e368c4b74113ded30349a22304f0128cf7 [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
Brian Salomon4cfae3b2020-07-23 10:33:24 -04008#include "src/gpu/gl/GrGLTexture.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTraceMemoryDump.h"
11#include "src/gpu/GrSemaphore.h"
12#include "src/gpu/GrShaderCaps.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040013#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/gl/GrGLGpu.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
bsalomon861e1032014-12-16 07:33:49 -080016#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
18
Brian Salomon7226c232018-07-30 13:13:17 -040019GrTextureType GrGLTexture::TextureTypeFromTarget(GrGLenum target) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040020 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;
cdalton9c3f1432016-03-11 10:07:37 -080027 }
Brian Salomon60dd8c72018-07-30 10:24:13 -040028 SK_ABORT("Unexpected texture target");
Brian Salomon60dd8c72018-07-30 10:24:13 -040029}
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;
Robert Phillipsf209e882019-06-25 15:59:50 -040039 default:
40 SK_ABORT("Unexpected texture target");
Brian Salomon60dd8c72018-07-30 10:24:13 -040041 }
42 SK_ABORT("Unexpected texture type");
cdalton9c3f1432016-03-11 10:07:37 -080043}
44
bsalomon37dd3312014-11-03 08:47:23 -080045// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
Brian Salomonea4ad302019-08-07 13:04:55 -040046GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const Desc& desc,
Brian Salomona6db5102020-07-21 09:56:23 -040047 GrMipmapStatus mipmapStatus)
Greg Danield51fa2f2020-01-22 16:53:38 -050048 : GrSurface(gpu, desc.fSize, GrProtected::kNo)
49 , INHERITED(gpu, desc.fSize, GrProtected::kNo,
Brian Salomona6db5102020-07-21 09:56:23 -040050 TextureTypeFromTarget(desc.fTarget), mipmapStatus)
Brian Salomone2826ab2019-06-04 15:58:31 -040051 , fParameters(sk_make_sp<GrGLTextureParameters>()) {
Brian Salomonea4ad302019-08-07 13:04:55 -040052 this->init(desc);
kkinnunen2e6055b2016-04-22 01:48:29 -070053 this->registerWithCache(budgeted);
Jim Van Verthe3671012019-09-18 09:53:31 -040054 if (GrGLFormatIsCompressed(desc.fFormat)) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050055 this->setReadOnly();
56 }
bsalomon37dd3312014-11-03 08:47:23 -080057}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000058
Brian Salomona6db5102020-07-21 09:56:23 -040059GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, GrMipmapStatus mipmapStatus,
Brian Salomonea4ad302019-08-07 13:04:55 -040060 sk_sp<GrGLTextureParameters> parameters, GrWrapCacheable cacheable,
61 GrIOType ioType)
Greg Danield51fa2f2020-01-22 16:53:38 -050062 : GrSurface(gpu, desc.fSize, GrProtected::kNo)
63 , INHERITED(gpu, desc.fSize, GrProtected::kNo,
Brian Salomona6db5102020-07-21 09:56:23 -040064 TextureTypeFromTarget(desc.fTarget), mipmapStatus)
Brian Salomone2826ab2019-06-04 15:58:31 -040065 , fParameters(std::move(parameters)) {
66 SkASSERT(fParameters);
Brian Salomonea4ad302019-08-07 13:04:55 -040067 this->init(desc);
Brian Salomonfa2ebea2019-01-24 15:58:58 -050068 this->registerWithCacheWrapped(cacheable);
Brian Salomonc67c31c2018-12-06 10:00:03 -050069 if (ioType == kRead_GrIOType) {
70 this->setReadOnly();
71 }
kkinnunen2e6055b2016-04-22 01:48:29 -070072}
73
Brian Salomonea4ad302019-08-07 13:04:55 -040074GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, sk_sp<GrGLTextureParameters> parameters,
Brian Salomona6db5102020-07-21 09:56:23 -040075 GrMipmapStatus mipmapStatus)
Greg Danield51fa2f2020-01-22 16:53:38 -050076 : GrSurface(gpu, desc.fSize, GrProtected::kNo)
77 , INHERITED(gpu, desc.fSize, GrProtected::kNo,
Brian Salomona6db5102020-07-21 09:56:23 -040078 TextureTypeFromTarget(desc.fTarget), mipmapStatus) {
Brian Salomonea4ad302019-08-07 13:04:55 -040079 SkASSERT(parameters || desc.fOwnership == GrBackendObjectOwnership::kOwned);
Brian Salomone2826ab2019-06-04 15:58:31 -040080 fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>();
Brian Salomonea4ad302019-08-07 13:04:55 -040081 this->init(desc);
bsalomon37dd3312014-11-03 08:47:23 -080082}
83
Brian Salomonea4ad302019-08-07 13:04:55 -040084void 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.com5bfc2172011-07-29 20:29:05 +000090}
91
Brian Salomon4cfae3b2020-07-23 10:33:24 -040092GrGLenum GrGLTexture::target() const { return target_from_texture_type(this->textureType()); }
Brian Salomon60dd8c72018-07-30 10:24:13 -040093
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094void GrGLTexture::onRelease() {
Yuqian Li40aa85f2019-07-02 13:45:00 -070095 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Herb Derby89acfe72021-01-28 10:57:40 -050096 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Texture release(%u)", this->uniqueID().asUInt());
Yuqian Li40aa85f2019-07-02 13:45:00 -070097
Brian Salomon60dd8c72018-07-30 10:24:13 -040098 if (fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070099 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400100 GL_CALL(DeleteTextures(1, &fID));
bsalomonbcaefb02014-11-03 11:07:12 -0800101 }
Brian Salomon60dd8c72018-07-30 10:24:13 -0400102 fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -0800103 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000104 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +0000105}
106
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107void GrGLTexture::onAbandon() {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400108 fID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000109 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000110}
111
Robert Phillipsb67821d2017-12-13 15:00:45 -0500112GrBackendTexture GrGLTexture::getBackendTexture() const {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400113 GrGLTextureInfo info;
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400114 info.fTarget = target_from_texture_type(this->textureType());
Brian Salomon60dd8c72018-07-30 10:24:13 -0400115 info.fID = fID;
Brian Salomonea4ad302019-08-07 13:04:55 -0400116 info.fFormat = GrGLFormatToEnum(fFormat);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400117 return GrBackendTexture(this->width(), this->height(), this->mipmapped(), info, fParameters);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500118}
119
Greg Daniel4065d452018-11-16 15:43:41 -0500120GrBackendFormat GrGLTexture::backendFormat() const {
Brian Salomonea4ad302019-08-07 13:04:55 -0400121 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat),
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400122 target_from_texture_type(this->textureType()));
Greg Daniel4065d452018-11-16 15:43:41 -0500123}
124
Brian Salomonea4ad302019-08-07 13:04:55 -0400125sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
Brian Salomona6db5102020-07-21 09:56:23 -0400126 GrMipmapStatus mipmapStatus,
Brian Salomonea4ad302019-08-07 13:04:55 -0400127 const Desc& desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400128 sk_sp<GrGLTextureParameters> parameters,
Brian Salomonea4ad302019-08-07 13:04:55 -0400129 GrWrapCacheable cacheable,
130 GrIOType ioType) {
131 return sk_sp<GrGLTexture>(
Brian Salomona6db5102020-07-21 09:56:23 -0400132 new GrGLTexture(gpu, desc, mipmapStatus, std::move(parameters), cacheable, ioType));
kkinnunen2e6055b2016-04-22 01:48:29 -0700133}
134
Eric Karl914a36b2017-10-12 12:44:50 -0700135bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
136 SkImage::BackendTextureReleaseProc* releaseProc) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400137 *backendTexture = this->getBackendTexture();
Eric Karl914a36b2017-10-12 12:44:50 -0700138 // 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 Karlaf770022018-03-19 13:04:03 -0700147
148void 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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400161 SkString resourceName = this->getResourceName();
162 resourceName.append("/texture");
Eric Karlaf770022018-03-19 13:04:03 -0700163
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 Sollenbergercf6da8c2018-03-29 13:40:02 -0400167 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
168 GrGLTexture::gpuMemorySize());
Eric Karlaf770022018-03-19 13:04:03 -0700169
170 SkString texture_id;
171 texture_id.appendU32(this->textureID());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400172 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
Eric Karlaf770022018-03-19 13:04:03 -0700173}