blob: 771006919b5f9bdfbcbb01aa260d5216c98509f0 [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 Salomonbf7b6202016-11-11 16:08:03 -050018static inline GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, GrPixelConfig config,
19 const GrGLGpu* gpu) {
cdalton9c3f1432016-03-11 10:07:37 -080020 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon1edc5b92016-11-29 13:43:46 -050021 SkASSERT(gpu->caps()->shaderCaps()->externalTextureSupport());
egdaniel990dbc82016-07-13 14:09:30 -070022 return kTextureExternalSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080023 } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) {
24 SkASSERT(gpu->glCaps().rectangleTextureSupport());
egdaniel990dbc82016-07-13 14:09:30 -070025 return kTexture2DRectSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080026 } else {
27 SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D);
egdaniel990dbc82016-07-13 14:09:30 -070028 return kTexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080029 }
30}
31
Robert Phillips18166ee2017-06-01 12:55:44 -040032// This method parallels GrTextureProxy::highestFilterMode
Brian Salomon2bbdcc42017-09-07 12:36:34 -040033static inline GrSamplerState::Filter highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
34 GrPixelConfig config) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050035 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
36 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040037 return GrSamplerState::Filter::kBilerp;
Brian Salomon739c5bf2016-11-07 09:53:44 -050038 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040039 return GrSamplerState::Filter::kMipMap;
Brian Salomon739c5bf2016-11-07 09:53:44 -050040}
41
bsalomon37dd3312014-11-03 08:47:23 -080042// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070043GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040044 const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070045 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050046 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040047 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080048 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070049 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080050}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000051
Greg Daniel177e6952017-10-12 12:27:11 -040052GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
53 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc)
kkinnunen2e6055b2016-04-22 01:48:29 -070054 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050055 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel177e6952017-10-12 12:27:11 -040056 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
kkinnunen2e6055b2016-04-22 01:48:29 -070057 this->init(desc, idDesc);
58 this->registerWithCacheWrapped();
59}
60
Robert Phillipsd6214d42016-11-07 08:23:48 -050061GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040062 GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070063 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050064 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040065 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080066 this->init(desc, idDesc);
67}
68
69void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080070 SkASSERT(0 != idDesc.fInfo.fID);
Greg Daniele7d8da42017-12-04 11:23:19 -050071 SkASSERT(0 != idDesc.fInfo.fFormat);
bsalomon@google.com80d09b92011-11-05 21:21:13 +000072 fTexParams.invalidate();
73 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080074 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070075 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000076}
77
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080079 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070080 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -070081 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -080082 }
bsalomon091f60c2015-11-10 11:54:56 -080083 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -080084 }
Greg Danielcef213c2017-04-21 11:52:27 -040085 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000086 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000087}
88
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -080090 fInfo.fTarget = 0;
91 fInfo.fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -040092 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000093 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +000094}
95
bsalomon@google.com08afc842012-10-25 18:56:10 +000096GrBackendObject GrGLTexture::getTextureHandle() const {
bsalomon091f60c2015-11-10 11:54:56 -080097 return reinterpret_cast<GrBackendObject>(&fInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +000098}
ericrk0a5fa482015-09-15 14:16:10 -070099
Robert Phillipsb67821d2017-12-13 15:00:45 -0500100GrBackendTexture GrGLTexture::getBackendTexture() const {
Greg Daniel7d2b16a2018-03-13 16:44:48 -0400101 GrBackendTexture beTex = GrBackendTexture(this->width(), this->height(),
102 this->texturePriv().mipMapped(), fInfo);
103#if GR_TEST_UTILS
104 // We shouldn't have to set this since the client can't access it and we will handle the config
105 // correctly if we go through our public SkSurface and SkImage APIs. However, some of our tests
106 // bypass the public APIs so we need to set this manually here.
107 beTex.setPixelConfig(this->config());
108#endif
109 return beTex;
Robert Phillipsb67821d2017-12-13 15:00:45 -0500110}
111
ericrk0a5fa482015-09-15 14:16:10 -0700112void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
113 const SkString& dumpName) const {
114 SkString texture_id;
115 texture_id.appendU32(this->textureID());
116 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
117 texture_id.c_str());
118}
kkinnunen2e6055b2016-04-22 01:48:29 -0700119
bungeman6bd52842016-10-27 09:30:08 -0700120sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel177e6952017-10-12 12:27:11 -0400121 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc) {
122 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, mipMapsStatus, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700123}
124
Eric Karl914a36b2017-10-12 12:44:50 -0700125bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
126 SkImage::BackendTextureReleaseProc* releaseProc) {
127 *backendTexture = GrBackendTexture(width(), height(), config(), fInfo);
128 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
129 *releaseProc = [](GrBackendTexture){};
130
131 // It's important that we only abandon this texture's objects, not subclass objects such as
132 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
133 // cleaned up by us.
134 this->GrGLTexture::onAbandon();
135 return true;
136}