blob: 9349509ddceeb0c5ed7a9c847bfa05adfb2d3d0a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
reed@google.comac10a2d2010-12-22 21:39:39 +00009#ifndef GrTexture_DEFINED
10#define GrTexture_DEFINED
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
13#include "include/core/SkPoint.h"
14#include "include/core/SkRefCnt.h"
15#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/GrTypesPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000017#include "src/gpu/GrSurface.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
Brian Salomon197c5862019-08-21 22:08:36 -040019class GrTexture : virtual public GrSurface {
reed@google.comac10a2d2010-12-22 21:39:39 +000020public:
mtklein36352bf2015-03-25 18:17:31 -070021 GrTexture* asTexture() override { return this; }
22 const GrTexture* asTexture() const override { return this; }
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000023
Robert Phillipsb67821d2017-12-13 15:00:45 -050024 virtual GrBackendTexture getBackendTexture() const = 0;
25
junov@chromium.org957ebdd2012-06-12 13:58:36 +000026 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000027 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
28 * changed externally to Skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +000029 */
Greg Daniel456f9b52020-03-05 19:14:18 +000030 virtual void textureParamsModified() = 0;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000031
Eric Karl914a36b2017-10-12 12:44:50 -070032 /**
33 * This function steals the backend texture from a uniquely owned GrTexture with no pending
34 * IO, passing it out to the caller. The GrTexture is deleted in the process.
Greg Daniel6a0176b2018-01-30 09:28:44 -050035 *
Eric Karl914a36b2017-10-12 12:44:50 -070036 * Note that if the GrTexture is not uniquely owned (no other refs), or has pending IO, this
37 * function will fail.
38 */
Brian Salomon35ba6142019-01-24 13:08:59 -050039 static bool StealBackendTexture(sk_sp<GrTexture>,
Eric Karl914a36b2017-10-12 12:44:50 -070040 GrBackendTexture*,
41 SkImage::BackendTextureReleaseProc*);
42
Brian Salomone80b8092019-03-08 13:25:19 -050043 /** See addIdleProc. */
44 enum class IdleState {
45 kFlushed,
46 kFinished
47 };
Brian Salomon614c1a82018-12-19 15:42:06 -050048 /**
Brian Salomone80b8092019-03-08 13:25:19 -050049 * Installs a proc on this texture. It will be called when the texture becomes "idle". There
50 * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
51 * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
52 * between the two. They both mean "all work related to the resource has been flushed to the
53 * backend API and the texture is not owned outside the resource cache".
54 *
55 * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
56 * work flushed to the GPU is finished.
Brian Salomon614c1a82018-12-19 15:42:06 -050057 */
Brian Salomone80b8092019-03-08 13:25:19 -050058 virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
59 // This is the default implementation for the managed case where the IdleState can be
60 // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
61 fIdleProcs.push_back(std::move(idleProc));
62 }
63 /** Helper version of addIdleProc that creates the ref-counted wrapper. */
64 void addIdleProc(GrRefCntedCallback::Callback callback,
65 GrRefCntedCallback::Context context,
66 IdleState state) {
67 this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
Brian Salomonb2c5dae2019-03-04 10:25:17 -050068 }
Brian Salomon614c1a82018-12-19 15:42:06 -050069
Brian Salomon4cfae3b2020-07-23 10:33:24 -040070 GrTextureType textureType() const { return fTextureType; }
71 bool hasRestrictedSampling() const {
72 return GrTextureTypeHasRestrictedSampling(this->textureType());
73 }
74
75 void markMipmapsDirty();
76 void markMipmapsClean();
77 GrMipmapped mipmapped() const {
78 return GrMipmapped(fMipmapStatus != GrMipmapStatus::kNotAllocated);
79 }
80 bool mipmapsAreDirty() const { return fMipmapStatus != GrMipmapStatus::kValid; }
81 GrMipmapStatus mipmapStatus() const { return fMipmapStatus; }
82 int maxMipmapLevel() const { return fMaxMipmapLevel; }
83
84 static void ComputeScratchKey(const GrCaps& caps,
85 const GrBackendFormat& format,
86 SkISize dimensions,
87 GrRenderable,
88 int sampleCnt,
89 GrMipmapped,
90 GrProtected,
91 GrScratchKey* key);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000092
93protected:
Brian Salomona6db5102020-07-21 09:56:23 -040094 GrTexture(GrGpu*, const SkISize&, GrProtected, GrTextureType, GrMipmapStatus);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000095
Eric Karl914a36b2017-10-12 12:44:50 -070096 virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
97
Brian Salomone80b8092019-03-08 13:25:19 -050098 SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
Brian Salomonb2c5dae2019-03-04 10:25:17 -050099
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400100 void willRemoveLastRef() override {
Brian Salomone80b8092019-03-08 13:25:19 -0500101 // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
102 fIdleProcs.reset();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500103 }
Jim Van Verth3e192162020-03-10 16:23:16 -0400104 virtual void callIdleProcsOnBehalfOfResource() {}
Brian Salomon14cb4132019-09-16 13:14:47 -0400105 void computeScratchKey(GrScratchKey*) const override;
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500106
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000107private:
mtklein36352bf2015-03-25 18:17:31 -0700108 size_t onGpuMemorySize() const override;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000109
Brian Salomon60dd8c72018-07-30 10:24:13 -0400110 GrTextureType fTextureType;
Brian Salomon8c82a872020-07-21 12:09:58 -0400111 GrMipmapStatus fMipmapStatus;
112 int fMaxMipmapLevel;
Jim Van Verth3e192162020-03-10 16:23:16 -0400113 friend class GrTextureResource;
bsalomonafbf2d62014-09-30 12:18:44 -0700114
115 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000116};
117
118#endif