blob: 06bf6621adf47f9c7378f21ce67ba42a33352a18 [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 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;
39 }
40 SK_ABORT("Unexpected texture type");
41 return GR_GL_TEXTURE_2D;
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.
kkinnunen2e6055b2016-04-22 01:48:29 -070045GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040046 const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040047 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040048 , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080049 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070050 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080051}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000052
Greg Daniel177e6952017-10-12 12:27:11 -040053GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
Brian Salomonc67c31c2018-12-06 10:00:03 -050054 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc, GrIOType ioType,
Greg Daniel2268ad22018-11-15 09:27:38 -050055 bool purgeImmediately)
Brian Salomon60dd8c72018-07-30 10:24:13 -040056 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040057 , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
kkinnunen2e6055b2016-04-22 01:48:29 -070058 this->init(desc, idDesc);
Greg Daniel2268ad22018-11-15 09:27:38 -050059 this->registerWithCacheWrapped(purgeImmediately);
Brian Salomonc67c31c2018-12-06 10:00:03 -050060 if (ioType == kRead_GrIOType) {
61 this->setReadOnly();
62 }
kkinnunen2e6055b2016-04-22 01:48:29 -070063}
64
Robert Phillipsd6214d42016-11-07 08:23:48 -050065GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040066 GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040067 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040068 , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080069 this->init(desc, idDesc);
70}
71
72void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080073 SkASSERT(0 != idDesc.fInfo.fID);
Greg Daniele7d8da42017-12-04 11:23:19 -050074 SkASSERT(0 != idDesc.fInfo.fFormat);
Brian Salomondc829942018-10-23 16:07:24 -040075 fParamsTimestamp = GrGpu::kExpiredTimestamp;
Brian Salomon60dd8c72018-07-30 10:24:13 -040076 fID = idDesc.fInfo.fID;
77 fFormat = idDesc.fInfo.fFormat;
kkinnunen2e6055b2016-04-22 01:48:29 -070078 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000079}
80
Brian Salomon60dd8c72018-07-30 10:24:13 -040081GrGLenum GrGLTexture::target() const {
82 return target_from_texture_type(this->texturePriv().textureType());
83}
84
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085void GrGLTexture::onRelease() {
Brian Salomon60dd8c72018-07-30 10:24:13 -040086 if (fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070087 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040088 GL_CALL(DeleteTextures(1, &fID));
bsalomonbcaefb02014-11-03 11:07:12 -080089 }
Brian Salomon60dd8c72018-07-30 10:24:13 -040090 fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -080091 }
Greg Danielcef213c2017-04-21 11:52:27 -040092 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000093 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000094}
95
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096void GrGLTexture::onAbandon() {
Brian Salomon60dd8c72018-07-30 10:24:13 -040097 fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -040098 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000099 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000100}
101
Robert Phillipsb67821d2017-12-13 15:00:45 -0500102GrBackendTexture GrGLTexture::getBackendTexture() const {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400103 GrGLTextureInfo info;
104 info.fTarget = target_from_texture_type(this->texturePriv().textureType());
105 info.fID = fID;
106 info.fFormat = fFormat;
107 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), info);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500108}
109
Greg Daniel4065d452018-11-16 15:43:41 -0500110GrBackendFormat GrGLTexture::backendFormat() const {
111 return GrBackendFormat::MakeGL(fFormat,
112 target_from_texture_type(this->texturePriv().textureType()));
113}
114
bungeman6bd52842016-10-27 09:30:08 -0700115sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel2268ad22018-11-15 09:27:38 -0500116 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500117 GrIOType ioType, bool purgeImmediately) {
118 return sk_sp<GrGLTexture>(
119 new GrGLTexture(gpu, kWrapped, desc, mipMapsStatus, idDesc, ioType, purgeImmediately));
kkinnunen2e6055b2016-04-22 01:48:29 -0700120}
121
Eric Karl914a36b2017-10-12 12:44:50 -0700122bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
123 SkImage::BackendTextureReleaseProc* releaseProc) {
Brian Salomon60dd8c72018-07-30 10:24:13 -0400124 *backendTexture = this->getBackendTexture();
Eric Karl914a36b2017-10-12 12:44:50 -0700125 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
126 *releaseProc = [](GrBackendTexture){};
127
128 // It's important that we only abandon this texture's objects, not subclass objects such as
129 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
130 // cleaned up by us.
131 this->GrGLTexture::onAbandon();
132 return true;
133}
Eric Karlaf770022018-03-19 13:04:03 -0700134
135void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
136 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
137 // which is multiply inherited from both ourselves and a texture. In these cases, one part
138 // (texture, rt) may be wrapped, while the other is owned by Skia.
139 bool refsWrappedTextureObjects =
140 this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
141 if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
142 return;
143 }
144
145 // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
146 // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
147 // has no downside in the normal case.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400148 SkString resourceName = this->getResourceName();
149 resourceName.append("/texture");
Eric Karlaf770022018-03-19 13:04:03 -0700150
151 // As we are only dumping our texture memory (not any additional memory tracked by classes
152 // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
153 // hitting an override.
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400154 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
155 GrGLTexture::gpuMemorySize());
Eric Karlaf770022018-03-19 13:04:03 -0700156
157 SkString texture_id;
158 texture_id.appendU32(this->textureID());
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400159 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
Eric Karlaf770022018-03-19 13:04:03 -0700160}