blob: d4a7cc61e060ec13a1d38ee2f7f422c027bb0a75 [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"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014class GrRenderTarget;
robertphillips@google.coma1e57952012-06-04 20:05:28 +000015class GrResourceKey;
bsalomon@google.comb8670992012-07-25 21:27:09 +000016class GrTextureParams;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
robertphillips@google.com75b3c962012-06-07 12:08:45 +000018/*
19 * All uncached textures should have this value as their fClientCacheID
20 */
21static const uint64_t kUncached_CacheID = 0xAAAAAAAA;
22
23/*
24 * Scratch textures should all have this value as their fClientCacheID
25 */
26static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
27
28
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000029class GrTexture : public GrSurface {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000030
reed@google.comac10a2d2010-12-22 21:39:39 +000031public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000032 SK_DECLARE_INST_COUNT(GrTexture)
33
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000034 // from GrResource
reed@google.comac10a2d2010-12-22 21:39:39 +000035 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000036 * Informational texture flags
37 */
38 enum FlagBits {
39 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
40
41 /**
42 * This texture should be returned to the texture cache when
43 * it is no longer reffed
44 */
45 kReturnToCache_FlagBit = kFirstBit,
46 };
47
48 void setFlag(GrTextureFlags flags) {
49 fDesc.fFlags = fDesc.fFlags | flags;
50 }
51 void resetFlag(GrTextureFlags flags) {
52 fDesc.fFlags = fDesc.fFlags & ~flags;
53 }
54 bool isSetFlag(GrTextureFlags flags) const {
55 return 0 != (fDesc.fFlags & flags);
56 }
57
58 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000059 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000060 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000061 virtual size_t sizeInBytes() const SK_OVERRIDE {
robertphillips@google.com32716282012-06-04 12:48:45 +000062 return (size_t) fDesc.fWidth *
63 fDesc.fHeight *
64 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000065 }
66
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000067 // from GrSurface
reed@google.comac10a2d2010-12-22 21:39:39 +000068 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000069 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000070 * @param left left edge of the rectangle to read (inclusive)
71 * @param top top edge of the rectangle to read (inclusive)
72 * @param width width of rectangle to read in pixels.
73 * @param height height of rectangle to read in pixels.
74 * @param config the pixel config of the destination buffer
75 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000076 * @param rowBytes number of bytes bewtween consecutive rows. Zero
77 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000078 *
79 * @return true if the read succeeded, false if not. The read can fail
80 * because of a unsupported pixel config.
81 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000082 virtual bool readPixels(int left, int top, int width, int height,
83 GrPixelConfig config, void* buffer,
84 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000085
86 /**
87 * Writes a rectangle of pixels to the texture.
88 * @param left left edge of the rectangle to write (inclusive)
89 * @param top top edge of the rectangle to write (inclusive)
90 * @param width width of rectangle to write in pixels.
91 * @param height height of rectangle to write in pixels.
92 * @param config the pixel config of the source buffer
93 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +000094 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +000095 * means rows are tightly packed.
96 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000097 virtual void writePixels(int left, int top, int width, int height,
98 GrPixelConfig config, const void* buffer,
99 size_t rowBytes) SK_OVERRIDE;
100
101 /**
102 * @return this texture
103 */
104 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
105 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000106
107 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 * Retrieves the render target underlying this texture that can be passed to
109 * GrGpu::setRenderTarget().
110 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000111 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 * render target
113 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000114 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
115 return fRenderTarget;
116 }
117 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
118 return fRenderTarget;
119 }
120
121 // GrTexture
122 /**
123 * Convert from texels to normalized texture coords for POT textures
124 * only.
125 */
126 GrFixed normalizeFixedX(GrFixed x) const {
127 GrAssert(GrIsPow2(fDesc.fWidth));
128 return x >> fShiftFixedX;
129 }
130 GrFixed normalizeFixedY(GrFixed y) const {
131 GrAssert(GrIsPow2(fDesc.fHeight));
132 return y >> fShiftFixedY;
133 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
135 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000136 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000138 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000140 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000141
142 /**
143 * Return the native ID or handle to the texture, depending on the
144 * platform. e.g. on opengl, return the texture ID.
145 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000146 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000148 /**
149 * Call this when the state of the native API texture object is
150 * altered directly, without being tracked by skia.
151 */
152 virtual void invalidateCachedState() = 0;
153
reed@google.comac10a2d2010-12-22 21:39:39 +0000154#if GR_DEBUG
155 void validate() const {
156 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000157
158 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 }
160#else
161 void validate() const {}
162#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000163
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000164 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000165 const GrTextureParams* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000166 const GrTextureDesc& desc,
167 bool scratch);
168
169 static bool NeedsResizing(const GrResourceKey& key);
170 static bool IsScratchTexture(const GrResourceKey& key);
171 static bool NeedsFiltering(const GrResourceKey& key);
172
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000173protected:
174 GrRenderTarget* fRenderTarget; // texture refs its rt representation
175 // base class cons sets to NULL
176 // subclass cons can create and set
177
robertphillips@google.com32716282012-06-04 12:48:45 +0000178 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000179 : INHERITED(gpu, desc)
180 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000181
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000182 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000183 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
184 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000185 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000186
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000187 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000188 virtual void onRelease() SK_OVERRIDE;
189 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000190
robertphillips@google.com32716282012-06-04 12:48:45 +0000191 void validateDesc() const;
192
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000193private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 // these two shift a fixed-point value into normalized coordinates
195 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000196 int fShiftFixedX;
197 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000198
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000199 virtual void internal_dispose() const SK_OVERRIDE;
200
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000201 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000202};
203
204#endif
205