blob: aa918d5ed7c3a31cf215a98c3f790387ca9c418d [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"
ericrk0a5fa482015-09-15 14:16:10 -070012#include "SkTraceMemoryDump.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon861e1032014-12-16 07:33:49 -080014#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
bsalomon@google.com0b77d682011-08-19 13:28:54 +000015#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
16
Brian Salomonbf7b6202016-11-11 16:08:03 -050017static inline GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, GrPixelConfig config,
18 const GrGLGpu* gpu) {
cdalton9c3f1432016-03-11 10:07:37 -080019 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon1edc5b92016-11-29 13:43:46 -050020 SkASSERT(gpu->caps()->shaderCaps()->externalTextureSupport());
Brian Salomonbf7b6202016-11-11 16:08:03 -050021 SkASSERT(!GrPixelConfigIsSint(config));
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());
Brian Salomonbf7b6202016-11-11 16:08:03 -050025 SkASSERT(!GrPixelConfigIsSint(config));
egdaniel990dbc82016-07-13 14:09:30 -070026 return kTexture2DRectSampler_GrSLType;
Brian Salomonbf7b6202016-11-11 16:08:03 -050027 } else if (GrPixelConfigIsSint(config)) {
Brian Salomona8f00022016-11-16 12:55:57 -050028 return kITexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080029 } else {
30 SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D);
egdaniel990dbc82016-07-13 14:09:30 -070031 return kTexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080032 }
33}
34
Robert Phillips18166ee2017-06-01 12:55:44 -040035// This method parallels GrTextureProxy::highestFilterMode
Brian Salomon2bbdcc42017-09-07 12:36:34 -040036static inline GrSamplerState::Filter highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
37 GrPixelConfig config) {
Brian Salomonbf7b6202016-11-11 16:08:03 -050038 if (GrPixelConfigIsSint(config)) {
39 // Integer textures in GL can use GL_NEAREST_MIPMAP_NEAREST. This is a mode we don't support
40 // and don't currently have a use for.
Brian Salomon2bbdcc42017-09-07 12:36:34 -040041 return GrSamplerState::Filter::kNearest;
Brian Salomonbf7b6202016-11-11 16:08:03 -050042 }
Brian Salomon739c5bf2016-11-07 09:53:44 -050043 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
44 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040045 return GrSamplerState::Filter::kBilerp;
Brian Salomon739c5bf2016-11-07 09:53:44 -050046 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040047 return GrSamplerState::Filter::kMipMap;
Brian Salomon739c5bf2016-11-07 09:53:44 -050048}
49
bsalomon37dd3312014-11-03 08:47:23 -080050// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070051GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040052 const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070053 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050054 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040055 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080056 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070057 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080058}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000059
kkinnunen2e6055b2016-04-22 01:48:29 -070060GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, const IDDesc& idDesc)
61 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050062 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040063 highest_filter_mode(idDesc, desc.fConfig), GrMipMapsStatus::kNotAllocated) {
kkinnunen2e6055b2016-04-22 01:48:29 -070064 this->init(desc, idDesc);
65 this->registerWithCacheWrapped();
66}
67
Robert Phillipsd6214d42016-11-07 08:23:48 -050068GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040069 GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070070 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050071 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040072 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080073 this->init(desc, idDesc);
74}
75
76void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080077 SkASSERT(0 != idDesc.fInfo.fID);
bsalomon@google.com80d09b92011-11-05 21:21:13 +000078 fTexParams.invalidate();
79 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080080 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070081 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000082}
83
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080085 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070086 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -070087 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -080088 }
bsalomon091f60c2015-11-10 11:54:56 -080089 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -080090 }
Greg Danielcef213c2017-04-21 11:52:27 -040091 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000092 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000093}
94
bsalomon@google.com8fe72472011-03-30 21:26:44 +000095void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -080096 fInfo.fTarget = 0;
97 fInfo.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
bsalomon@google.com08afc842012-10-25 18:56:10 +0000102GrBackendObject GrGLTexture::getTextureHandle() const {
bsalomon091f60c2015-11-10 11:54:56 -0800103 return reinterpret_cast<GrBackendObject>(&fInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +0000104}
ericrk0a5fa482015-09-15 14:16:10 -0700105
106void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
107 const SkString& dumpName) const {
108 SkString texture_id;
109 texture_id.appendU32(this->textureID());
110 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
111 texture_id.c_str());
112}
kkinnunen2e6055b2016-04-22 01:48:29 -0700113
bungeman6bd52842016-10-27 09:30:08 -0700114sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
115 const IDDesc& idDesc) {
116 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700117}
118