blob: b8497bd840be5ac3a4bcb4c72f46479c34391567 [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 Salomon514baff2016-11-17 15:17:07 -050036static inline GrSamplerParams::FilterMode highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
Brian Salomonbf7b6202016-11-11 16:08:03 -050037 GrPixelConfig config) {
38 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 Salomon514baff2016-11-17 15:17:07 -050041 return GrSamplerParams::kNone_FilterMode;
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 Salomon514baff2016-11-17 15:17:07 -050045 return GrSamplerParams::kBilerp_FilterMode;
Brian Salomon739c5bf2016-11-07 09:53:44 -050046 }
Brian Salomon514baff2016-11-17 15:17:07 -050047 return GrSamplerParams::kMipMap_FilterMode;
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,
52 const IDDesc& idDesc)
53 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050054 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
55 highest_filter_mode(idDesc, desc.fConfig), false) {
cblume55f2d2d2016-02-26 13:20:48 -080056 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070057 this->registerWithCache(budgeted);
cblume55f2d2d2016-02-26 13:20:48 -080058}
59
kkinnunen2e6055b2016-04-22 01:48:29 -070060GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
61 const IDDesc& idDesc,
cblume55f2d2d2016-02-26 13:20:48 -080062 bool wasMipMapDataProvided)
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),
65 highest_filter_mode(idDesc, desc.fConfig),
Brian Salomon739c5bf2016-11-07 09:53:44 -050066 wasMipMapDataProvided) {
bsalomon37dd3312014-11-03 08:47:23 -080067 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070068 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080069}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000070
kkinnunen2e6055b2016-04-22 01:48:29 -070071GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, const IDDesc& idDesc)
72 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050073 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
74 highest_filter_mode(idDesc, desc.fConfig), false) {
kkinnunen2e6055b2016-04-22 01:48:29 -070075 this->init(desc, idDesc);
76 this->registerWithCacheWrapped();
77}
78
Robert Phillipsd6214d42016-11-07 08:23:48 -050079GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
80 bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070081 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050082 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
83 highest_filter_mode(idDesc, desc.fConfig),
Brian Salomon739c5bf2016-11-07 09:53:44 -050084 wasMipMapDataProvided) {
bsalomon37dd3312014-11-03 08:47:23 -080085 this->init(desc, idDesc);
86}
87
88void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080089 SkASSERT(0 != idDesc.fInfo.fID);
bsalomon@google.com80d09b92011-11-05 21:21:13 +000090 fTexParams.invalidate();
91 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080092 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070093 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000094}
95
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080097 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070098 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -070099 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -0800100 }
bsalomon091f60c2015-11-10 11:54:56 -0800101 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -0800102 }
Greg Danielcef213c2017-04-21 11:52:27 -0400103 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000104 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +0000105}
106
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -0800108 fInfo.fTarget = 0;
109 fInfo.fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -0400110 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000111 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000112}
113
bsalomon@google.com08afc842012-10-25 18:56:10 +0000114GrBackendObject GrGLTexture::getTextureHandle() const {
bsalomon091f60c2015-11-10 11:54:56 -0800115 return reinterpret_cast<GrBackendObject>(&fInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +0000116}
ericrk0a5fa482015-09-15 14:16:10 -0700117
118void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
119 const SkString& dumpName) const {
120 SkString texture_id;
121 texture_id.appendU32(this->textureID());
122 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
123 texture_id.c_str());
124}
kkinnunen2e6055b2016-04-22 01:48:29 -0700125
bungeman6bd52842016-10-27 09:30:08 -0700126sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
127 const IDDesc& idDesc) {
128 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700129}
130