blob: c93ff41ec6d1017bde006b58d0d8439bd340ce5a [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"
Greg Danield85f97d2017-03-07 13:37:21 -050010#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050011#include "GrSemaphore.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050012#include "GrShaderCaps.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());
Brian Salomonbf7b6202016-11-11 16:08:03 -050022 SkASSERT(!GrPixelConfigIsSint(config));
egdaniel990dbc82016-07-13 14:09:30 -070023 return kTextureExternalSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080024 } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) {
25 SkASSERT(gpu->glCaps().rectangleTextureSupport());
Brian Salomonbf7b6202016-11-11 16:08:03 -050026 SkASSERT(!GrPixelConfigIsSint(config));
egdaniel990dbc82016-07-13 14:09:30 -070027 return kTexture2DRectSampler_GrSLType;
Brian Salomonbf7b6202016-11-11 16:08:03 -050028 } else if (GrPixelConfigIsSint(config)) {
Brian Salomona8f00022016-11-16 12:55:57 -050029 return kITexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080030 } else {
31 SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D);
egdaniel990dbc82016-07-13 14:09:30 -070032 return kTexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080033 }
34}
35
Robert Phillips18166ee2017-06-01 12:55:44 -040036// This method parallels GrTextureProxy::highestFilterMode
Brian Salomon514baff2016-11-17 15:17:07 -050037static inline GrSamplerParams::FilterMode highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
Brian Salomonbf7b6202016-11-11 16:08:03 -050038 GrPixelConfig config) {
39 if (GrPixelConfigIsSint(config)) {
40 // Integer textures in GL can use GL_NEAREST_MIPMAP_NEAREST. This is a mode we don't support
41 // and don't currently have a use for.
Brian Salomon514baff2016-11-17 15:17:07 -050042 return GrSamplerParams::kNone_FilterMode;
Brian Salomonbf7b6202016-11-11 16:08:03 -050043 }
Brian Salomon739c5bf2016-11-07 09:53:44 -050044 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
45 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon514baff2016-11-17 15:17:07 -050046 return GrSamplerParams::kBilerp_FilterMode;
Brian Salomon739c5bf2016-11-07 09:53:44 -050047 }
Brian Salomon514baff2016-11-17 15:17:07 -050048 return GrSamplerParams::kMipMap_FilterMode;
Brian Salomon739c5bf2016-11-07 09:53:44 -050049}
50
bsalomon37dd3312014-11-03 08:47:23 -080051// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070052GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
53 const IDDesc& idDesc)
54 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050055 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
56 highest_filter_mode(idDesc, desc.fConfig), false) {
cblume55f2d2d2016-02-26 13:20:48 -080057 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070058 this->registerWithCache(budgeted);
cblume55f2d2d2016-02-26 13:20:48 -080059}
60
kkinnunen2e6055b2016-04-22 01:48:29 -070061GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
62 const IDDesc& idDesc,
cblume55f2d2d2016-02-26 13:20:48 -080063 bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070064 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050065 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
66 highest_filter_mode(idDesc, desc.fConfig),
Brian Salomon739c5bf2016-11-07 09:53:44 -050067 wasMipMapDataProvided) {
bsalomon37dd3312014-11-03 08:47:23 -080068 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070069 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080070}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000071
kkinnunen2e6055b2016-04-22 01:48:29 -070072GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, const IDDesc& idDesc)
73 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050074 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
75 highest_filter_mode(idDesc, desc.fConfig), false) {
kkinnunen2e6055b2016-04-22 01:48:29 -070076 this->init(desc, idDesc);
77 this->registerWithCacheWrapped();
78}
79
Robert Phillipsd6214d42016-11-07 08:23:48 -050080GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
81 bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070082 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050083 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
84 highest_filter_mode(idDesc, desc.fConfig),
Brian Salomon739c5bf2016-11-07 09:53:44 -050085 wasMipMapDataProvided) {
bsalomon37dd3312014-11-03 08:47:23 -080086 this->init(desc, idDesc);
87}
88
89void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080090 SkASSERT(0 != idDesc.fInfo.fID);
bsalomon@google.com80d09b92011-11-05 21:21:13 +000091 fTexParams.invalidate();
92 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080093 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070094 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000095}
96
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080098 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070099 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -0700100 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -0800101 }
bsalomon091f60c2015-11-10 11:54:56 -0800102 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -0800103 }
Greg Danielcef213c2017-04-21 11:52:27 -0400104 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000105 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +0000106}
107
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -0800109 fInfo.fTarget = 0;
110 fInfo.fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -0400111 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000112 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000113}
114
bsalomon@google.com08afc842012-10-25 18:56:10 +0000115GrBackendObject GrGLTexture::getTextureHandle() const {
bsalomon091f60c2015-11-10 11:54:56 -0800116 return reinterpret_cast<GrBackendObject>(&fInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +0000117}
ericrk0a5fa482015-09-15 14:16:10 -0700118
119void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
120 const SkString& dumpName) const {
121 SkString texture_id;
122 texture_id.appendU32(this->textureID());
123 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
124 texture_id.c_str());
125}
kkinnunen2e6055b2016-04-22 01:48:29 -0700126
bungeman6bd52842016-10-27 09:30:08 -0700127sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
128 const IDDesc& idDesc) {
129 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700130}
131