blob: 5b70cccd60593132d963ff458982d1199228f684 [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/gpu/GrSurface.h"
17#include "include/private/GrTypesPriv.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 */
Brian Salomon197c5862019-08-21 22:08:36 -040032 SK_API 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
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000045#ifdef SK_DEBUG
46 void validate() const {
47 this->INHERITED::validate();
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000048 }
49#endif
50
Brian Salomone80b8092019-03-08 13:25:19 -050051 /** See addIdleProc. */
52 enum class IdleState {
53 kFlushed,
54 kFinished
55 };
Brian Salomon614c1a82018-12-19 15:42:06 -050056 /**
Brian Salomone80b8092019-03-08 13:25:19 -050057 * Installs a proc on this texture. It will be called when the texture becomes "idle". There
58 * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
59 * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
60 * between the two. They both mean "all work related to the resource has been flushed to the
61 * backend API and the texture is not owned outside the resource cache".
62 *
63 * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
64 * work flushed to the GPU is finished.
Brian Salomon614c1a82018-12-19 15:42:06 -050065 */
Brian Salomone80b8092019-03-08 13:25:19 -050066 virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
67 // This is the default implementation for the managed case where the IdleState can be
68 // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
69 fIdleProcs.push_back(std::move(idleProc));
70 }
71 /** Helper version of addIdleProc that creates the ref-counted wrapper. */
72 void addIdleProc(GrRefCntedCallback::Callback callback,
73 GrRefCntedCallback::Context context,
74 IdleState state) {
75 this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
Brian Salomonb2c5dae2019-03-04 10:25:17 -050076 }
Brian Salomon614c1a82018-12-19 15:42:06 -050077
bsalomonafbf2d62014-09-30 12:18:44 -070078 /** Access methods that are only to be used within Skia code. */
79 inline GrTexturePriv texturePriv();
80 inline const GrTexturePriv texturePriv() const;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000081
82protected:
Brian Salomona9c22572019-08-05 12:57:09 -040083 GrTexture(GrGpu*, const SkISize&, GrPixelConfig, GrProtected, GrTextureType, GrMipMapsStatus);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000084
Eric Karl914a36b2017-10-12 12:44:50 -070085 virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
86
Brian Salomone80b8092019-03-08 13:25:19 -050087 SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
Brian Salomonb2c5dae2019-03-04 10:25:17 -050088
89 void willRemoveLastRefOrPendingIO() override {
Brian Salomone80b8092019-03-08 13:25:19 -050090 // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
91 fIdleProcs.reset();
Brian Salomonb2c5dae2019-03-04 10:25:17 -050092 }
93
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000094private:
kkinnunen2e6055b2016-04-22 01:48:29 -070095 void computeScratchKey(GrScratchKey*) const override;
mtklein36352bf2015-03-25 18:17:31 -070096 size_t onGpuMemorySize() const override;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040097 void markMipMapsDirty();
98 void markMipMapsClean();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000099
Brian Salomon60dd8c72018-07-30 10:24:13 -0400100 GrTextureType fTextureType;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400101 GrMipMapsStatus fMipMapsStatus;
Brian Osman7b8400d2016-11-08 17:08:54 -0500102 int fMaxMipMapLevel;
bsalomonafbf2d62014-09-30 12:18:44 -0700103 friend class GrTexturePriv;
104
105 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000106};
107
108#endif