blob: a1b7eabd85f3e23b914a8c55c38d697ffc7cd40a [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"
robertphillips@google.com46a86002012-08-08 10:42:44 +000013#include "GrCacheID.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015class GrRenderTarget;
robertphillips@google.coma1e57952012-06-04 20:05:28 +000016class GrResourceKey;
bsalomon@google.comb8670992012-07-25 21:27:09 +000017class GrTextureParams;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000019class GrTexture : public GrSurface {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000022 SK_DECLARE_INST_COUNT(GrTexture)
robertphillips@google.com46a86002012-08-08 10:42:44 +000023 GR_DECLARE_RESOURCE_CACHE_TYPE()
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000024
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000025 // from GrResource
reed@google.comac10a2d2010-12-22 21:39:39 +000026 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000027 * Informational texture flags
28 */
29 enum FlagBits {
30 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
31
32 /**
33 * This texture should be returned to the texture cache when
34 * it is no longer reffed
35 */
36 kReturnToCache_FlagBit = kFirstBit,
37 };
38
39 void setFlag(GrTextureFlags flags) {
40 fDesc.fFlags = fDesc.fFlags | flags;
41 }
42 void resetFlag(GrTextureFlags flags) {
43 fDesc.fFlags = fDesc.fFlags & ~flags;
44 }
45 bool isSetFlag(GrTextureFlags flags) const {
46 return 0 != (fDesc.fFlags & flags);
47 }
48
49 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000050 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000051 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000052 virtual size_t sizeInBytes() const SK_OVERRIDE {
robertphillips@google.com32716282012-06-04 12:48:45 +000053 return (size_t) fDesc.fWidth *
54 fDesc.fHeight *
55 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000056 }
57
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000058 // from GrSurface
reed@google.comac10a2d2010-12-22 21:39:39 +000059 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000060 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000061 * @param left left edge of the rectangle to read (inclusive)
62 * @param top top edge of the rectangle to read (inclusive)
63 * @param width width of rectangle to read in pixels.
64 * @param height height of rectangle to read in pixels.
65 * @param config the pixel config of the destination buffer
66 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000067 * @param rowBytes number of bytes bewtween consecutive rows. Zero
68 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000069 *
70 * @return true if the read succeeded, false if not. The read can fail
71 * because of a unsupported pixel config.
72 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000073 virtual bool readPixels(int left, int top, int width, int height,
74 GrPixelConfig config, void* buffer,
75 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000076
77 /**
78 * Writes a rectangle of pixels to the texture.
79 * @param left left edge of the rectangle to write (inclusive)
80 * @param top top edge of the rectangle to write (inclusive)
81 * @param width width of rectangle to write in pixels.
82 * @param height height of rectangle to write in pixels.
83 * @param config the pixel config of the source buffer
84 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +000085 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +000086 * means rows are tightly packed.
87 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000088 virtual void writePixels(int left, int top, int width, int height,
89 GrPixelConfig config, const void* buffer,
90 size_t rowBytes) SK_OVERRIDE;
91
92 /**
93 * @return this texture
94 */
95 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
96 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000097
98 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000099 * Retrieves the render target underlying this texture that can be passed to
100 * GrGpu::setRenderTarget().
101 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000102 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 * render target
104 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000105 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
106 return fRenderTarget;
107 }
108 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
109 return fRenderTarget;
110 }
111
112 // GrTexture
113 /**
114 * Convert from texels to normalized texture coords for POT textures
115 * only.
116 */
117 GrFixed normalizeFixedX(GrFixed x) const {
118 GrAssert(GrIsPow2(fDesc.fWidth));
119 return x >> fShiftFixedX;
120 }
121 GrFixed normalizeFixedY(GrFixed y) const {
122 GrAssert(GrIsPow2(fDesc.fHeight));
123 return y >> fShiftFixedY;
124 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
126 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000127 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000129 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000131 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
133 /**
134 * Return the native ID or handle to the texture, depending on the
135 * platform. e.g. on opengl, return the texture ID.
136 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000137 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000138
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000139 /**
140 * Call this when the state of the native API texture object is
141 * altered directly, without being tracked by skia.
142 */
143 virtual void invalidateCachedState() = 0;
144
reed@google.comac10a2d2010-12-22 21:39:39 +0000145#if GR_DEBUG
146 void validate() const {
147 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000148
149 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 }
151#else
152 void validate() const {}
153#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000154
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000155 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000156 const GrTextureParams* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000157 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000158 const GrCacheData& cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000159 bool scratch);
160
161 static bool NeedsResizing(const GrResourceKey& key);
162 static bool IsScratchTexture(const GrResourceKey& key);
163 static bool NeedsFiltering(const GrResourceKey& key);
164
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000165protected:
166 GrRenderTarget* fRenderTarget; // texture refs its rt representation
167 // base class cons sets to NULL
168 // subclass cons can create and set
169
robertphillips@google.com32716282012-06-04 12:48:45 +0000170 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000171 : INHERITED(gpu, desc)
172 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000173
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000174 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000175 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
176 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000177 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000178
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000179 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000180 virtual void onRelease() SK_OVERRIDE;
181 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000182
robertphillips@google.com32716282012-06-04 12:48:45 +0000183 void validateDesc() const;
184
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000185private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 // these two shift a fixed-point value into normalized coordinates
187 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000188 int fShiftFixedX;
189 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000190
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000191 virtual void internal_dispose() const SK_OVERRIDE;
192
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000193 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194};
195
196#endif
197