blob: dd85ddb87759ee6f971fdae2cd7cda2dfde47f71 [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");
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;
Robert Phillipsf209e882019-06-25 15:59:50 -040039 default:
40 SK_ABORT("Unexpected texture target");
41 return GR_GL_TEXTURE_2D;
Brian Salomon60dd8c72018-07-30 10:24:13 -040042 }
43 SK_ABORT("Unexpected texture type");
44 return GR_GL_TEXTURE_2D;
cdalton9c3f1432016-03-11 10:07:37 -080045}
46
bsalomon37dd3312014-11-03 08:47:23 -080047// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070048GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040049 const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040050 : GrSurface(gpu, desc)
Brian Salomone2826ab2019-06-04 15:58:31 -040051 , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus)
52 , fParameters(sk_make_sp<GrGLTextureParameters>()) {
bsalomon37dd3312014-11-03 08:47:23 -080053 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070054 this->registerWithCache(budgeted);
Jim Van Verth1676cb92019-01-15 13:24:45 -050055 if (GrPixelConfigIsCompressed(desc.fConfig)) {
56 this->setReadOnly();
57 }
bsalomon37dd3312014-11-03 08:47:23 -080058}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000059
Brian Salomonfa2ebea2019-01-24 15:58:58 -050060GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
Brian Salomone2826ab2019-06-04 15:58:31 -040061 const IDDesc& idDesc, sk_sp<GrGLTextureParameters> parameters,
62 GrWrapCacheable cacheable, GrIOType ioType)
Brian Salomon60dd8c72018-07-30 10:24:13 -040063 : GrSurface(gpu, desc)
Brian Salomone2826ab2019-06-04 15:58:31 -040064 , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus)
65 , fParameters(std::move(parameters)) {
66 SkASSERT(fParameters);
kkinnunen2e6055b2016-04-22 01:48:29 -070067 this->init(desc, idDesc);
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
Robert Phillipsd6214d42016-11-07 08:23:48 -050074GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Brian Salomone2826ab2019-06-04 15:58:31 -040075 sk_sp<GrGLTextureParameters> parameters, GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040076 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040077 , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
Brian Salomone2826ab2019-06-04 15:58:31 -040078 SkASSERT(parameters || idDesc.fOwnership == GrBackendObjectOwnership::kOwned);
79 fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>();
bsalomon37dd3312014-11-03 08:47:23 -080080 this->init(desc, idDesc);
81}
82
83void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080084 SkASSERT(0 != idDesc.fInfo.fID);
Greg Daniele7d8da42017-12-04 11:23:19 -050085 SkASSERT(0 != idDesc.fInfo.fFormat);
Brian Salomon60dd8c72018-07-30 10:24:13 -040086 fID = idDesc.fInfo.fID;
87 fFormat = idDesc.fInfo.fFormat;
kkinnunen2e6055b2016-04-22 01:48:29 -070088 fTextureIDOwnership = idDesc.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() {
Brian Salomon60dd8c72018-07-30 10:24:13 -040096 if (fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070097 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040098 GL_CALL(DeleteTextures(1, &fID));
bsalomonbcaefb02014-11-03 11:07:12 -080099 }
Brian Salomon60dd8c72018-07-30 10:24:13 -0400100 fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -0800101 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000102 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +0000103}
104
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000105void GrGLTexture::onAbandon() {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400106 fID = 0;
robertphillips@google.comd3645542012-09-05 18:37:39 +0000107 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000108}
109
Robert Phillipsb67821d2017-12-13 15:00:45 -0500110GrBackendTexture GrGLTexture::getBackendTexture() const {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400111 GrGLTextureInfo info;
112 info.fTarget = target_from_texture_type(this->texturePriv().textureType());
113 info.fID = fID;
114 info.fFormat = fFormat;
Brian Salomone2826ab2019-06-04 15:58:31 -0400115 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), info,
116 fParameters);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500117}
118
Greg Daniel4065d452018-11-16 15:43:41 -0500119GrBackendFormat GrGLTexture::backendFormat() const {
120 return GrBackendFormat::MakeGL(fFormat,
121 target_from_texture_type(this->texturePriv().textureType()));
122}
123
bungeman6bd52842016-10-27 09:30:08 -0700124sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel2268ad22018-11-15 09:27:38 -0500125 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400126 sk_sp<GrGLTextureParameters> parameters,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500127 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomone2826ab2019-06-04 15:58:31 -0400128 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, desc, mipMapsStatus, idDesc,
129 std::move(parameters), cacheable, ioType));
kkinnunen2e6055b2016-04-22 01:48:29 -0700130}
131
Eric Karl914a36b2017-10-12 12:44:50 -0700132bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
133 SkImage::BackendTextureReleaseProc* releaseProc) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400134 *backendTexture = this->getBackendTexture();
Eric Karl914a36b2017-10-12 12:44:50 -0700135 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
136 *releaseProc = [](GrBackendTexture){};
137
138 // It's important that we only abandon this texture's objects, not subclass objects such as
139 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
140 // cleaned up by us.
141 this->GrGLTexture::onAbandon();
142 return true;
143}
Eric Karlaf770022018-03-19 13:04:03 -0700144
145void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
146 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
147 // which is multiply inherited from both ourselves and a texture. In these cases, one part
148 // (texture, rt) may be wrapped, while the other is owned by Skia.
149 bool refsWrappedTextureObjects =
150 this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
151 if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
152 return;
153 }
154
155 // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
156 // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
157 // has no downside in the normal case.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400158 SkString resourceName = this->getResourceName();
159 resourceName.append("/texture");
Eric Karlaf770022018-03-19 13:04:03 -0700160
161 // As we are only dumping our texture memory (not any additional memory tracked by classes
162 // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
163 // hitting an override.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400164 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
165 GrGLTexture::gpuMemorySize());
Eric Karlaf770022018-03-19 13:04:03 -0700166
167 SkString texture_id;
168 texture_id.appendU32(this->textureID());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400169 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
Eric Karlaf770022018-03-19 13:04:03 -0700170}