blob: 8bdff34df5ee311a84401b4c19f8bca1d20186d4 [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
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000012#include "GrSurface.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000013#include "GrRenderTarget.h"
bsalomonc44be0e2014-07-25 07:32:33 -070014#include "SkPoint.h"
15#include "SkRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
robertphillips@google.coma1e57952012-06-04 20:05:28 +000017class GrResourceKey;
bsalomon@google.comb8670992012-07-25 21:27:09 +000018class GrTextureParams;
bsalomonafbf2d62014-09-30 12:18:44 -070019class GrTexturePriv;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000021class GrTexture : public GrSurface {
reed@google.comac10a2d2010-12-22 21:39:39 +000022public:
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000023 /**
24 * Approximate number of bytes used by the texture
25 */
26 virtual size_t gpuMemorySize() const SK_OVERRIDE;
27
28 // GrSurface overrides
29 virtual bool readPixels(int left, int top, int width, int height,
30 GrPixelConfig config,
31 void* buffer,
32 size_t rowBytes = 0,
33 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
34
35 virtual void writePixels(int left, int top, int width, int height,
36 GrPixelConfig config,
37 const void* buffer,
38 size_t rowBytes = 0,
39 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
40
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000041 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
42 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000043 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.get(); }
44 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fRenderTarget.get(); }
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000045
46 /**
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000047 * Return the native ID or handle to the texture, depending on the
48 * platform. e.g. on OpenGL, return the texture ID.
49 */
50 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000051
junov@chromium.org957ebdd2012-06-12 13:58:36 +000052 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000053 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
54 * changed externally to Skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +000055 */
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000056 virtual void textureParamsModified() = 0;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000057
58 /**
bsalomonafbf2d62014-09-30 12:18:44 -070059 * Informational texture flags. This will be removed soon.
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000060 */
61 enum FlagBits {
62 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
63
64 /**
65 * This texture should be returned to the texture cache when
66 * it is no longer reffed
67 */
68 kReturnToCache_FlagBit = kFirstBit,
69 };
70
71 void resetFlag(GrTextureFlags flags) {
72 fDesc.fFlags = fDesc.fFlags & ~flags;
73 }
junov@chromium.org957ebdd2012-06-12 13:58:36 +000074
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000075#ifdef SK_DEBUG
76 void validate() const {
77 this->INHERITED::validate();
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000078 this->validateDesc();
79 }
80#endif
81
bsalomonafbf2d62014-09-30 12:18:44 -070082 /** Access methods that are only to be used within Skia code. */
83 inline GrTexturePriv texturePriv();
84 inline const GrTexturePriv texturePriv() const;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000085
86protected:
87 // A texture refs its rt representation but not vice-versa. It is up to
88 // the subclass constructor to initialize this pointer.
89 SkAutoTUnref<GrRenderTarget> fRenderTarget;
90
bsalomonafbf2d62014-09-30 12:18:44 -070091 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000092
93 virtual ~GrTexture();
94
95 // GrResource overrides
96 virtual void onRelease() SK_OVERRIDE;
97 virtual void onAbandon() SK_OVERRIDE;
98
99 void validateDesc() const;
100
101private:
robertphillipsdbe60742014-09-30 06:54:17 -0700102 void abandonReleaseCommon();
103 virtual void internal_dispose() const SK_OVERRIDE;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000104 void dirtyMipMaps(bool mipMapsDirty);
105
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000106 enum MipMapsStatus {
107 kNotAllocated_MipMapsStatus,
108 kAllocated_MipMapsStatus,
109 kValid_MipMapsStatus
110 };
111
bsalomonafbf2d62014-09-30 12:18:44 -0700112 MipMapsStatus fMipMapsStatus;
113 // These two shift a fixed-point value into normalized coordinates
114 // for this texture if the texture is power of two sized.
115 int fShiftFixedX;
116 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000117
bsalomonafbf2d62014-09-30 12:18:44 -0700118 friend class GrTexturePriv;
119
120 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000121};
122
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000123/**
124 * Represents a texture that is intended to be accessed in device coords with an offset.
125 */
126class GrDeviceCoordTexture {
127public:
128 GrDeviceCoordTexture() { fOffset.set(0, 0); }
129
130 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
131 *this = other;
132 }
133
134 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
135 : fTexture(SkSafeRef(texture))
136 , fOffset(offset) {
137 }
138
139 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
140 fTexture.reset(SkSafeRef(other.fTexture.get()));
141 fOffset = other.fOffset;
142 return *this;
143 }
144
145 const SkIPoint& offset() const { return fOffset; }
146
147 void setOffset(const SkIPoint& offset) { fOffset = offset; }
148 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
149
150 GrTexture* texture() const { return fTexture.get(); }
151
152 GrTexture* setTexture(GrTexture* texture) {
153 fTexture.reset(SkSafeRef(texture));
154 return texture;
155 }
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000156
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000157private:
158 SkAutoTUnref<GrTexture> fTexture;
159 SkIPoint fOffset;
160};
161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162#endif