blob: d722f38fd338fd19be08ac9db42cb502f0fcc9d2 [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
Brian Osman2c2bc112017-02-28 10:02:49 -05008#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGLTexture.h"
jvanverth39edf762014-12-22 11:44:19 -080010#include "GrGLGpu.h"
Greg Danield85f97d2017-03-07 13:37:21 -050011#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050012#include "GrSemaphore.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050013#include "GrShaderCaps.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050014#include "SkMakeUnique.h"
ericrk0a5fa482015-09-15 14:16:10 -070015#include "SkTraceMemoryDump.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon861e1032014-12-16 07:33:49 -080017#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
19
Brian Salomonbf7b6202016-11-11 16:08:03 -050020static inline GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, GrPixelConfig config,
21 const GrGLGpu* gpu) {
cdalton9c3f1432016-03-11 10:07:37 -080022 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon1edc5b92016-11-29 13:43:46 -050023 SkASSERT(gpu->caps()->shaderCaps()->externalTextureSupport());
Brian Salomonbf7b6202016-11-11 16:08:03 -050024 SkASSERT(!GrPixelConfigIsSint(config));
egdaniel990dbc82016-07-13 14:09:30 -070025 return kTextureExternalSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080026 } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) {
27 SkASSERT(gpu->glCaps().rectangleTextureSupport());
Brian Salomonbf7b6202016-11-11 16:08:03 -050028 SkASSERT(!GrPixelConfigIsSint(config));
egdaniel990dbc82016-07-13 14:09:30 -070029 return kTexture2DRectSampler_GrSLType;
Brian Salomonbf7b6202016-11-11 16:08:03 -050030 } else if (GrPixelConfigIsSint(config)) {
Brian Salomona8f00022016-11-16 12:55:57 -050031 return kITexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080032 } else {
33 SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D);
egdaniel990dbc82016-07-13 14:09:30 -070034 return kTexture2DSampler_GrSLType;
cdalton9c3f1432016-03-11 10:07:37 -080035 }
36}
37
Brian Salomon514baff2016-11-17 15:17:07 -050038static inline GrSamplerParams::FilterMode highest_filter_mode(const GrGLTexture::IDDesc& idDesc,
Brian Salomonbf7b6202016-11-11 16:08:03 -050039 GrPixelConfig config) {
40 if (GrPixelConfigIsSint(config)) {
41 // Integer textures in GL can use GL_NEAREST_MIPMAP_NEAREST. This is a mode we don't support
42 // and don't currently have a use for.
Brian Salomon514baff2016-11-17 15:17:07 -050043 return GrSamplerParams::kNone_FilterMode;
Brian Salomonbf7b6202016-11-11 16:08:03 -050044 }
Brian Salomon739c5bf2016-11-07 09:53:44 -050045 if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE ||
46 idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
Brian Salomon514baff2016-11-17 15:17:07 -050047 return GrSamplerParams::kBilerp_FilterMode;
Brian Salomon739c5bf2016-11-07 09:53:44 -050048 }
Brian Salomon514baff2016-11-17 15:17:07 -050049 return GrSamplerParams::kMipMap_FilterMode;
Brian Salomon739c5bf2016-11-07 09:53:44 -050050}
51
bsalomon37dd3312014-11-03 08:47:23 -080052// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070053GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
54 const IDDesc& idDesc)
55 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050056 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
57 highest_filter_mode(idDesc, desc.fConfig), false) {
cblume55f2d2d2016-02-26 13:20:48 -080058 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070059 this->registerWithCache(budgeted);
cblume55f2d2d2016-02-26 13:20:48 -080060}
61
kkinnunen2e6055b2016-04-22 01:48:29 -070062GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
63 const IDDesc& idDesc,
cblume55f2d2d2016-02-26 13:20:48 -080064 bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070065 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050066 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
67 highest_filter_mode(idDesc, desc.fConfig),
Brian Salomon739c5bf2016-11-07 09:53:44 -050068 wasMipMapDataProvided) {
bsalomon37dd3312014-11-03 08:47:23 -080069 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070070 this->registerWithCache(budgeted);
bsalomon37dd3312014-11-03 08:47:23 -080071}
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000072
kkinnunen2e6055b2016-04-22 01:48:29 -070073GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, const IDDesc& idDesc)
74 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050075 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
76 highest_filter_mode(idDesc, desc.fConfig), false) {
kkinnunen2e6055b2016-04-22 01:48:29 -070077 this->init(desc, idDesc);
78 this->registerWithCacheWrapped();
79}
80
Robert Phillipsd6214d42016-11-07 08:23:48 -050081GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
82 bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070083 : GrSurface(gpu, desc)
Brian Salomonbf7b6202016-11-11 16:08:03 -050084 , INHERITED(gpu, desc, sampler_type(idDesc, desc.fConfig, gpu),
85 highest_filter_mode(idDesc, desc.fConfig),
Brian Salomon739c5bf2016-11-07 09:53:44 -050086 wasMipMapDataProvided) {
bsalomon37dd3312014-11-03 08:47:23 -080087 this->init(desc, idDesc);
88}
89
90void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
bsalomon091f60c2015-11-10 11:54:56 -080091 SkASSERT(0 != idDesc.fInfo.fID);
bsalomon@google.com80d09b92011-11-05 21:21:13 +000092 fTexParams.invalidate();
93 fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
bsalomon091f60c2015-11-10 11:54:56 -080094 fInfo = idDesc.fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070095 fTextureIDOwnership = idDesc.fOwnership;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000096}
97
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098void GrGLTexture::onRelease() {
bsalomon091f60c2015-11-10 11:54:56 -080099 if (fInfo.fID) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700100 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
bsalomon7e68ab72016-04-13 14:29:25 -0700101 GL_CALL(DeleteTextures(1, &fInfo.fID));
bsalomonbcaefb02014-11-03 11:07:12 -0800102 }
bsalomon091f60c2015-11-10 11:54:56 -0800103 fInfo.fID = 0;
bsalomonbcaefb02014-11-03 11:07:12 -0800104 }
Greg Danielcef213c2017-04-21 11:52:27 -0400105 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000106 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +0000107}
108
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109void GrGLTexture::onAbandon() {
bsalomon091f60c2015-11-10 11:54:56 -0800110 fInfo.fTarget = 0;
111 fInfo.fID = 0;
Greg Danielcef213c2017-04-21 11:52:27 -0400112 this->invokeReleaseProc();
robertphillips@google.comd3645542012-09-05 18:37:39 +0000113 INHERITED::onAbandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000114}
115
bsalomon@google.com08afc842012-10-25 18:56:10 +0000116GrBackendObject GrGLTexture::getTextureHandle() const {
bsalomon091f60c2015-11-10 11:54:56 -0800117 return reinterpret_cast<GrBackendObject>(&fInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +0000118}
ericrk0a5fa482015-09-15 14:16:10 -0700119
Brian Osman2c2bc112017-02-28 10:02:49 -0500120std::unique_ptr<GrExternalTextureData> GrGLTexture::detachBackendTexture() {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400121 SkASSERT(!this->hasPendingIO());
Brian Osmanfe3b5162017-03-02 15:09:20 -0500122
123 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Danield85f97d2017-03-07 13:37:21 -0500124 sk_sp<GrSemaphore> semaphore = this->getContext()->resourceProvider()->makeSemaphore();
Brian Osmandc87c952017-04-28 13:57:38 -0400125 this->getGpu()->insertSemaphore(semaphore, true);
Brian Osman2c2bc112017-02-28 10:02:49 -0500126
127 // Make a copy of our GL-specific information
Greg Danield85f97d2017-03-07 13:37:21 -0500128 auto data = skstd::make_unique<GrGLExternalTextureData>(fInfo, std::move(semaphore),
129 this->getContext());
Brian Osman2c2bc112017-02-28 10:02:49 -0500130
131 // Ensure the cache can't reach this texture anymore
132 this->detachFromCache();
133
134 // Detach from the GL object, so we don't use it (or try to delete it when we're freed)
135 fInfo.fTarget = 0;
136 fInfo.fID = 0;
137
138 return std::move(data);
139}
140
ericrk0a5fa482015-09-15 14:16:10 -0700141void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
142 const SkString& dumpName) const {
143 SkString texture_id;
144 texture_id.appendU32(this->textureID());
145 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
146 texture_id.c_str());
147}
kkinnunen2e6055b2016-04-22 01:48:29 -0700148
bungeman6bd52842016-10-27 09:30:08 -0700149sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
150 const IDDesc& idDesc) {
151 return sk_sp<GrGLTexture>(new GrGLTexture(gpu, kWrapped, desc, idDesc));
kkinnunen2e6055b2016-04-22 01:48:29 -0700152}
153