blob: e8f8035ab785a4df76ec6dea77d58779dad8377e [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
Eric Karl914a36b2017-10-12 12:44:50 -070012#include "GrBackendSurface.h"
Brian Salomon9f7d9a22018-12-11 19:01:32 +000013#include "GrSamplerState.h"
14#include "GrSurface.h"
Eric Karl914a36b2017-10-12 12:44:50 -070015#include "SkImage.h"
bsalomonc44be0e2014-07-25 07:32:33 -070016#include "SkPoint.h"
17#include "SkRefCnt.h"
Brian Salomon9f7d9a22018-12-11 19:01:32 +000018#include "../private/GrTypesPriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomonafbf2d62014-09-30 12:18:44 -070020class GrTexturePriv;
reed@google.comac10a2d2010-12-22 21:39:39 +000021
Brian Salomon3ccaa882018-12-10 13:59:52 -050022class SK_API GrTexture : virtual public GrSurface {
reed@google.comac10a2d2010-12-22 21:39:39 +000023public:
mtklein36352bf2015-03-25 18:17:31 -070024 GrTexture* asTexture() override { return this; }
25 const GrTexture* asTexture() const override { return this; }
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000026
Robert Phillipsb67821d2017-12-13 15:00:45 -050027 virtual GrBackendTexture getBackendTexture() const = 0;
28
junov@chromium.org957ebdd2012-06-12 13:58:36 +000029 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000030 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
31 * changed externally to Skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +000032 */
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000033 virtual void textureParamsModified() = 0;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000034
Eric Karl914a36b2017-10-12 12:44:50 -070035 /**
36 * This function steals the backend texture from a uniquely owned GrTexture with no pending
37 * IO, passing it out to the caller. The GrTexture is deleted in the process.
Greg Daniel6a0176b2018-01-30 09:28:44 -050038 *
Eric Karl914a36b2017-10-12 12:44:50 -070039 * Note that if the GrTexture is not uniquely owned (no other refs), or has pending IO, this
40 * function will fail.
41 */
Brian Salomon35ba6142019-01-24 13:08:59 -050042 static bool StealBackendTexture(sk_sp<GrTexture>,
Eric Karl914a36b2017-10-12 12:44:50 -070043 GrBackendTexture*,
44 SkImage::BackendTextureReleaseProc*);
45
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000046#ifdef SK_DEBUG
47 void validate() const {
48 this->INHERITED::validate();
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000049 }
50#endif
51
Brian Salomone80b8092019-03-08 13:25:19 -050052 /** See addIdleProc. */
53 enum class IdleState {
54 kFlushed,
55 kFinished
56 };
Brian Salomon614c1a82018-12-19 15:42:06 -050057 /**
Brian Salomone80b8092019-03-08 13:25:19 -050058 * Installs a proc on this texture. It will be called when the texture becomes "idle". There
59 * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
60 * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
61 * between the two. They both mean "all work related to the resource has been flushed to the
62 * backend API and the texture is not owned outside the resource cache".
63 *
64 * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
65 * work flushed to the GPU is finished.
Brian Salomon614c1a82018-12-19 15:42:06 -050066 */
Brian Salomone80b8092019-03-08 13:25:19 -050067 virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
68 // This is the default implementation for the managed case where the IdleState can be
69 // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
70 fIdleProcs.push_back(std::move(idleProc));
71 }
72 /** Helper version of addIdleProc that creates the ref-counted wrapper. */
73 void addIdleProc(GrRefCntedCallback::Callback callback,
74 GrRefCntedCallback::Context context,
75 IdleState state) {
76 this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
Brian Salomonb2c5dae2019-03-04 10:25:17 -050077 }
Brian Salomon614c1a82018-12-19 15:42:06 -050078
bsalomonafbf2d62014-09-30 12:18:44 -070079 /** Access methods that are only to be used within Skia code. */
80 inline GrTexturePriv texturePriv();
81 inline const GrTexturePriv texturePriv() const;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000082
83protected:
Brian Salomone632dfc2018-08-01 13:01:16 -040084 GrTexture(GrGpu*, const GrSurfaceDesc&, GrTextureType, GrMipMapsStatus);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000085
Eric Karl914a36b2017-10-12 12:44:50 -070086 virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
87
Brian Salomone80b8092019-03-08 13:25:19 -050088 SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
Brian Salomonb2c5dae2019-03-04 10:25:17 -050089
90 void willRemoveLastRefOrPendingIO() override {
Brian Salomone80b8092019-03-08 13:25:19 -050091 // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
92 fIdleProcs.reset();
Brian Salomonb2c5dae2019-03-04 10:25:17 -050093 }
94
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000095private:
kkinnunen2e6055b2016-04-22 01:48:29 -070096 void computeScratchKey(GrScratchKey*) const override;
mtklein36352bf2015-03-25 18:17:31 -070097 size_t onGpuMemorySize() const override;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040098 void markMipMapsDirty();
99 void markMipMapsClean();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000100
Brian Salomon60dd8c72018-07-30 10:24:13 -0400101 GrTextureType fTextureType;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400102 GrMipMapsStatus fMipMapsStatus;
Brian Osman7b8400d2016-11-08 17:08:54 -0500103 int fMaxMipMapLevel;
bsalomonafbf2d62014-09-30 12:18:44 -0700104 friend class GrTexturePriv;
105
106 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000107};
108
109#endif