blob: a5a1a3708bd6fbcd2a853e39b7e1a120e562f9c2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTraceMemoryDump.h"
9#include "src/gpu/GrSemaphore.h"
10#include "src/gpu/GrShaderCaps.h"
11#include "src/gpu/GrTexturePriv.h"
12#include "src/gpu/gl/GrGLGpu.h"
13#include "src/gpu/gl/GrGLTexture.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 Salomon7226c232018-07-30 13:13:17 -040018GrTextureType GrGLTexture::TextureTypeFromTarget(GrGLenum target) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040019 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");
Brian Salomon60dd8c72018-07-30 10:24:13 -040028}
29
30static inline GrGLenum target_from_texture_type(GrTextureType type) {
31 switch (type) {
32 case GrTextureType::k2D:
33 return GR_GL_TEXTURE_2D;
34 case GrTextureType::kRectangle:
35 return GR_GL_TEXTURE_RECTANGLE;
36 case GrTextureType::kExternal:
37 return GR_GL_TEXTURE_EXTERNAL;
Robert Phillipsf209e882019-06-25 15:59:50 -040038 default:
39 SK_ABORT("Unexpected texture target");
Brian Salomon60dd8c72018-07-30 10:24:13 -040040 }
41 SK_ABORT("Unexpected texture type");
cdalton9c3f1432016-03-11 10:07:37 -080042}
43
bsalomon37dd3312014-11-03 08:47:23 -080044// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
Brian Salomonea4ad302019-08-07 13:04:55 -040045GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const Desc& desc,
46 GrMipMapsStatus mipMapsStatus)
47 : GrSurface(gpu, desc.fSize, desc.fConfig, GrProtected::kNo)
48 , INHERITED(gpu, desc.fSize, desc.fConfig, GrProtected::kNo,
49 TextureTypeFromTarget(desc.fTarget), mipMapsStatus)
Brian Salomone2826ab2019-06-04 15:58:31 -040050 , fParameters(sk_make_sp<GrGLTextureParameters>()) {
Brian Salomonea4ad302019-08-07 13:04:55 -040051 this->init(desc);
kkinnunen2e6055b2016-04-22 01:48:29 -070052 this->registerWithCache(budgeted);
Jim Van Verthe3671012019-09-18 09:53:31 -040053 if (GrGLFormatIsCompressed(desc.fFormat)) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050054 this->setReadOnly();
55 }
bsalomon37dd3312014-11-03 08:47:23 -080056}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000057
Brian Salomonea4ad302019-08-07 13:04:55 -040058GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, GrMipMapsStatus mipMapsStatus,
59 sk_sp<GrGLTextureParameters> parameters, GrWrapCacheable cacheable,
60 GrIOType ioType)
61 : GrSurface(gpu, desc.fSize, desc.fConfig, GrProtected::kNo)
62 , INHERITED(gpu, desc.fSize, desc.fConfig, GrProtected::kNo,
63 TextureTypeFromTarget(desc.fTarget), mipMapsStatus)
Brian Salomone2826ab2019-06-04 15:58:31 -040064 , fParameters(std::move(parameters)) {
65 SkASSERT(fParameters);
Brian Salomonea4ad302019-08-07 13:04:55 -040066 this->init(desc);
Brian Salomonfa2ebea2019-01-24 15:58:58 -050067 this->registerWithCacheWrapped(cacheable);
Brian Salomonc67c31c2018-12-06 10:00:03 -050068 if (ioType == kRead_GrIOType) {
69 this->setReadOnly();
70 }
kkinnunen2e6055b2016-04-22 01:48:29 -070071}
72
Brian Salomonea4ad302019-08-07 13:04:55 -040073GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, sk_sp<GrGLTextureParameters> parameters,
74 GrMipMapsStatus mipMapsStatus)
75 : GrSurface(gpu, desc.fSize, desc.fConfig, GrProtected::kNo)
76 , INHERITED(gpu, desc.fSize, desc.fConfig, GrProtected::kNo,
77 TextureTypeFromTarget(desc.fTarget), mipMapsStatus) {
78 SkASSERT(parameters || desc.fOwnership == GrBackendObjectOwnership::kOwned);
Brian Salomone2826ab2019-06-04 15:58:31 -040079 fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>();
Brian Salomonea4ad302019-08-07 13:04:55 -040080 this->init(desc);
bsalomon37dd3312014-11-03 08:47:23 -080081}
82
Brian Salomonea4ad302019-08-07 13:04:55 -040083void GrGLTexture::init(const Desc& desc) {
84 SkASSERT(0 != desc.fID);
85 SkASSERT(GrGLFormat::kUnknown != desc.fFormat);
86 fID = desc.fID;
87 fFormat = desc.fFormat;
88 fTextureIDOwnership = desc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000089}
90
Brian Salomon60dd8c72018-07-30 10:24:13 -040091GrGLenum GrGLTexture::target() const {
92 return target_from_texture_type(this->texturePriv().textureType());
93}
94
bsalomon@google.com8fe72472011-03-30 21:26:44 +000095void GrGLTexture::onRelease() {
Yuqian Li40aa85f2019-07-02 13:45:00 -070096 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
97
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;
114 info.fTarget = target_from_texture_type(this->texturePriv().textureType());
115 info.fID = fID;
Brian Salomonea4ad302019-08-07 13:04:55 -0400116 info.fFormat = GrGLFormatToEnum(fFormat);
Brian Salomone2826ab2019-06-04 15:58:31 -0400117 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), info,
118 fParameters);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500119}
120
Greg Daniel4065d452018-11-16 15:43:41 -0500121GrBackendFormat GrGLTexture::backendFormat() const {
Brian Salomonea4ad302019-08-07 13:04:55 -0400122 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat),
Greg Daniel4065d452018-11-16 15:43:41 -0500123 target_from_texture_type(this->texturePriv().textureType()));
124}
125
Brian Salomonea4ad302019-08-07 13:04:55 -0400126sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
127 GrMipMapsStatus mipMapsStatus,
128 const Desc& desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400129 sk_sp<GrGLTextureParameters> parameters,
Brian Salomonea4ad302019-08-07 13:04:55 -0400130 GrWrapCacheable cacheable,
131 GrIOType ioType) {
132 return sk_sp<GrGLTexture>(
133 new GrGLTexture(gpu, desc, mipMapsStatus, std::move(parameters), cacheable, ioType));
kkinnunen2e6055b2016-04-22 01:48:29 -0700134}
135
Eric Karl914a36b2017-10-12 12:44:50 -0700136bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
137 SkImage::BackendTextureReleaseProc* releaseProc) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400138 *backendTexture = this->getBackendTexture();
Eric Karl914a36b2017-10-12 12:44:50 -0700139 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
140 *releaseProc = [](GrBackendTexture){};
141
142 // It's important that we only abandon this texture's objects, not subclass objects such as
143 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
144 // cleaned up by us.
145 this->GrGLTexture::onAbandon();
146 return true;
147}
Eric Karlaf770022018-03-19 13:04:03 -0700148
149void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
150 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
151 // which is multiply inherited from both ourselves and a texture. In these cases, one part
152 // (texture, rt) may be wrapped, while the other is owned by Skia.
153 bool refsWrappedTextureObjects =
154 this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
155 if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
156 return;
157 }
158
159 // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
160 // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
161 // has no downside in the normal case.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400162 SkString resourceName = this->getResourceName();
163 resourceName.append("/texture");
Eric Karlaf770022018-03-19 13:04:03 -0700164
165 // As we are only dumping our texture memory (not any additional memory tracked by classes
166 // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
167 // hitting an override.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400168 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
169 GrGLTexture::gpuMemorySize());
Eric Karlaf770022018-03-19 13:04:03 -0700170
171 SkString texture_id;
172 texture_id.appendU32(this->textureID());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400173 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
Eric Karlaf770022018-03-19 13:04:03 -0700174}