blob: 678ed82cafbc7af1f312d5df7dcbfc9cd3ea211f [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
bsalomonafbf2d62014-09-30 12:18:44 -070019class GrTexturePriv;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
Brian Salomon197c5862019-08-21 22:08:36 -040021class GrTexture : virtual public GrSurface {
reed@google.comac10a2d2010-12-22 21:39:39 +000022public:
mtklein36352bf2015-03-25 18:17:31 -070023 GrTexture* asTexture() override { return this; }
24 const GrTexture* asTexture() const override { return this; }
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000025
Robert Phillipsb67821d2017-12-13 15:00:45 -050026 virtual GrBackendTexture getBackendTexture() const = 0;
27
junov@chromium.org957ebdd2012-06-12 13:58:36 +000028 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000029 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
30 * changed externally to Skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +000031 */
Greg Daniel456f9b52020-03-05 19:14:18 +000032 virtual void textureParamsModified() = 0;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000033
Eric Karl914a36b2017-10-12 12:44:50 -070034 /**
35 * This function steals the backend texture from a uniquely owned GrTexture with no pending
36 * IO, passing it out to the caller. The GrTexture is deleted in the process.
Greg Daniel6a0176b2018-01-30 09:28:44 -050037 *
Eric Karl914a36b2017-10-12 12:44:50 -070038 * Note that if the GrTexture is not uniquely owned (no other refs), or has pending IO, this
39 * function will fail.
40 */
Brian Salomon35ba6142019-01-24 13:08:59 -050041 static bool StealBackendTexture(sk_sp<GrTexture>,
Eric Karl914a36b2017-10-12 12:44:50 -070042 GrBackendTexture*,
43 SkImage::BackendTextureReleaseProc*);
44
Brian Salomone80b8092019-03-08 13:25:19 -050045 /** See addIdleProc. */
46 enum class IdleState {
47 kFlushed,
48 kFinished
49 };
Brian Salomon614c1a82018-12-19 15:42:06 -050050 /**
Brian Salomone80b8092019-03-08 13:25:19 -050051 * Installs a proc on this texture. It will be called when the texture becomes "idle". There
52 * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
53 * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
54 * between the two. They both mean "all work related to the resource has been flushed to the
55 * backend API and the texture is not owned outside the resource cache".
56 *
57 * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
58 * work flushed to the GPU is finished.
Brian Salomon614c1a82018-12-19 15:42:06 -050059 */
Brian Salomone80b8092019-03-08 13:25:19 -050060 virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
61 // This is the default implementation for the managed case where the IdleState can be
62 // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
63 fIdleProcs.push_back(std::move(idleProc));
64 }
65 /** Helper version of addIdleProc that creates the ref-counted wrapper. */
66 void addIdleProc(GrRefCntedCallback::Callback callback,
67 GrRefCntedCallback::Context context,
68 IdleState state) {
69 this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
Brian Salomonb2c5dae2019-03-04 10:25:17 -050070 }
Brian Salomon614c1a82018-12-19 15:42:06 -050071
bsalomonafbf2d62014-09-30 12:18:44 -070072 /** Access methods that are only to be used within Skia code. */
73 inline GrTexturePriv texturePriv();
74 inline const GrTexturePriv texturePriv() const;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000075
76protected:
Brian Salomona6db5102020-07-21 09:56:23 -040077 GrTexture(GrGpu*, const SkISize&, GrProtected, GrTextureType, GrMipmapStatus);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000078
Eric Karl914a36b2017-10-12 12:44:50 -070079 virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
80
Brian Salomone80b8092019-03-08 13:25:19 -050081 SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
Brian Salomonb2c5dae2019-03-04 10:25:17 -050082
Robert Phillipsbf8bf832019-08-30 13:13:44 -040083 void willRemoveLastRef() override {
Brian Salomone80b8092019-03-08 13:25:19 -050084 // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
85 fIdleProcs.reset();
Brian Salomonb2c5dae2019-03-04 10:25:17 -050086 }
Jim Van Verth3e192162020-03-10 16:23:16 -040087 virtual void callIdleProcsOnBehalfOfResource() {}
Brian Salomon14cb4132019-09-16 13:14:47 -040088 void computeScratchKey(GrScratchKey*) const override;
Brian Salomonb2c5dae2019-03-04 10:25:17 -050089
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000090private:
mtklein36352bf2015-03-25 18:17:31 -070091 size_t onGpuMemorySize() const override;
Brian Salomon8c82a872020-07-21 12:09:58 -040092 void markMipmapsDirty();
93 void markMipmapsClean();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000094
Brian Salomon60dd8c72018-07-30 10:24:13 -040095 GrTextureType fTextureType;
Brian Salomon8c82a872020-07-21 12:09:58 -040096 GrMipmapStatus fMipmapStatus;
97 int fMaxMipmapLevel;
bsalomonafbf2d62014-09-30 12:18:44 -070098 friend class GrTexturePriv;
Jim Van Verth3e192162020-03-10 16:23:16 -040099 friend class GrTextureResource;
bsalomonafbf2d62014-09-30 12:18:44 -0700100
101 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000102};
103
104#endif