blob: 4a4f085081af0c3c66be9eb7457e90fdd3e2db42 [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
Greg Daniel177e6952017-10-12 12:27:11 -040060GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
61 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc)
kkinnunen2e6055b2016-04-22 01:48:29 -070062 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050063 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel177e6952017-10-12 12:27:11 -040064 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
kkinnunen2e6055b2016-04-22 01:48:29 -070065 this->init(desc, idDesc);
66 this->registerWithCacheWrapped();
67}
68
Robert Phillipsd6214d42016-11-07 08:23:48 -050069GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040070 GrMipMapsStatus mipMapsStatus)
kkinnunen2e6055b2016-04-22 01:48:29 -070071 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050072 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040073 highest_filter_mode(idDesc, desc.fConfig), mipMapsStatus) {
bsalomon37dd3312014-11-03 08:47:23 -080074 this->init(desc, idDesc);
75}
76
77void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080078 SkASSERT(0 != idDesc.fInfo.fID);
Greg Daniele7d8da42017-12-04 11:23:19 -050079 SkASSERT(0 != idDesc.fInfo.fFormat);
bsalomon@google.com80d09b92011-11-05 21:21:13 +000080 fTexParams.invalidate();
81 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080082 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070083 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000084}
85
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080087 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -070088 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -070089 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -080090 }
bsalomon091f60c2015-11-10 11:54:56 -080091 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -080092 }
Greg Danielcef213c2017-04-21 11:52:27 -040093 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +000094 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000095}
96
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -080098 fInfo.fTarget = 0;
99 fInfo.fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -0400100 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000101 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000102}
103
bsalomon@google.com08afc842012-10-25 18:56:10 +0000104GrBackendObject GrGLTexture::getTextureHandle() const {
bsalomon091f60c2015-11-10 11:54:56 -0800105 return reinterpret_cast<GrBackendObject>(&fInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +0000106}
ericrk0a5fa482015-09-15 14:16:10 -0700107
108void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
109 const SkString& dumpName) const {
110 SkString texture_id;
111 texture_id.appendU32(this->textureID());
112 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
113 texture_id.c_str());
114}
kkinnunen2e6055b2016-04-22 01:48:29 -0700115
bungeman6bd52842016-10-27 09:30:08 -0700116sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel177e6952017-10-12 12:27:11 -0400117 GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc) {
118 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, mipMapsStatus, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700119}
120
Eric Karl914a36b2017-10-12 12:44:50 -0700121bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
122 SkImage::BackendTextureReleaseProc* releaseProc) {
123 *backendTexture = GrBackendTexture(width(), height(), config(), fInfo);
124 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
125 *releaseProc = [](GrBackendTexture){};
126
127 // It's important that we only abandon this texture's objects, not subclass objects such as
128 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
129 // cleaned up by us.
130 this->GrGLTexture::onAbandon();
131 return true;
132}