blob: 1997f68ddffc0c81d3a0197d494ce08d866be9a2 [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
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTexture_DEFINED
12#define GrTexture_DEFINED
13
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014#include "GrResource.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000016class GrRenderTarget;
robertphillips@google.coma1e57952012-06-04 20:05:28 +000017class GrResourceKey;
18class GrSamplerState;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
robertphillips@google.com75b3c962012-06-07 12:08:45 +000020/*
21 * All uncached textures should have this value as their fClientCacheID
22 */
23static const uint64_t kUncached_CacheID = 0xAAAAAAAA;
24
25/*
26 * Scratch textures should all have this value as their fClientCacheID
27 */
28static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
29
30
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031class GrTexture : public GrResource {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033public:
reed@google.comac10a2d2010-12-22 21:39:39 +000034 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000035 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000036 *
reed@google.comac10a2d2010-12-22 21:39:39 +000037 * @return the width in texels
38 */
robertphillips@google.com32716282012-06-04 12:48:45 +000039 int width() const { return fDesc.fWidth; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000040
reed@google.comac10a2d2010-12-22 21:39:39 +000041 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000042 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043 *
reed@google.comac10a2d2010-12-22 21:39:39 +000044 * @return the height in texels
45 */
robertphillips@google.com32716282012-06-04 12:48:45 +000046 int height() const { return fDesc.fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +000047
48 /**
49 * Convert from texels to normalized texture coords for POT textures
50 * only.
51 */
robertphillips@google.com32716282012-06-04 12:48:45 +000052 GrFixed normalizeFixedX(GrFixed x) const {
53 GrAssert(GrIsPow2(fDesc.fWidth));
54 return x >> fShiftFixedX;
55 }
56 GrFixed normalizeFixedY(GrFixed y) const {
57 GrAssert(GrIsPow2(fDesc.fHeight));
58 return y >> fShiftFixedY;
59 }
reed@google.comac10a2d2010-12-22 21:39:39 +000060
bsalomon@google.com1c13c962011-02-14 16:51:21 +000061 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000062 * Retrieves the pixel config specified when the texture was created.
63 */
robertphillips@google.com32716282012-06-04 12:48:45 +000064 GrPixelConfig config() const { return fDesc.fConfig; }
65
66 /**
67 * Return the descriptor describing the texture
68 */
69 const GrTextureDesc& desc() const { return fDesc; }
reed@google.comac10a2d2010-12-22 21:39:39 +000070
71 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000072 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000073 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +000074 virtual size_t sizeInBytes() const {
robertphillips@google.com32716282012-06-04 12:48:45 +000075 return (size_t) fDesc.fWidth *
76 fDesc.fHeight *
77 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000078 }
79
80 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000081 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000082 * @param left left edge of the rectangle to read (inclusive)
83 * @param top top edge of the rectangle to read (inclusive)
84 * @param width width of rectangle to read in pixels.
85 * @param height height of rectangle to read in pixels.
86 * @param config the pixel config of the destination buffer
87 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000088 * @param rowBytes number of bytes bewtween consecutive rows. Zero
89 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000090 *
91 * @return true if the read succeeded, false if not. The read can fail
92 * because of a unsupported pixel config.
93 */
94 bool readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000095 GrPixelConfig config, void* buffer,
96 size_t rowBytes);
97
98 /**
99 * Writes a rectangle of pixels to the texture.
100 * @param left left edge of the rectangle to write (inclusive)
101 * @param top top edge of the rectangle to write (inclusive)
102 * @param width width of rectangle to write in pixels.
103 * @param height height of rectangle to write in pixels.
104 * @param config the pixel config of the source buffer
105 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +0000106 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000107 * means rows are tightly packed.
108 */
109 void writePixels(int left, int top, int width, int height,
110 GrPixelConfig config, const void* buffer,
111 size_t rowBytes);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000112
113 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 * Retrieves the render target underlying this texture that can be passed to
115 * GrGpu::setRenderTarget().
116 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000117 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 * render target
119 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000120 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000121 const GrRenderTarget* asRenderTarget() const { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000124 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000125 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000126 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000128 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000129
130 /**
131 * Return the native ID or handle to the texture, depending on the
132 * platform. e.g. on opengl, return the texture ID.
133 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000134 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000136 /**
137 * Call this when the state of the native API texture object is
138 * altered directly, without being tracked by skia.
139 */
140 virtual void invalidateCachedState() = 0;
141
reed@google.comac10a2d2010-12-22 21:39:39 +0000142#if GR_DEBUG
143 void validate() const {
144 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000145
146 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 }
148#else
149 void validate() const {}
150#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000151
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000152 static GrResourceKey ComputeKey(const GrGpu* gpu,
153 const GrSamplerState* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000154 const GrTextureDesc& desc,
155 bool scratch);
156
157 static bool NeedsResizing(const GrResourceKey& key);
158 static bool IsScratchTexture(const GrResourceKey& key);
159 static bool NeedsFiltering(const GrResourceKey& key);
160
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000161protected:
162 GrRenderTarget* fRenderTarget; // texture refs its rt representation
163 // base class cons sets to NULL
164 // subclass cons can create and set
165
robertphillips@google.com32716282012-06-04 12:48:45 +0000166 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000167 : INHERITED(gpu)
168 , fRenderTarget(NULL)
robertphillips@google.com32716282012-06-04 12:48:45 +0000169 , fDesc(desc) {
170
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000171 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000172 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
173 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000174 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000175
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000176 // GrResource overrides
177 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000178 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000179 }
180
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000181 virtual void onAbandon();
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:
robertphillips@google.com32716282012-06-04 12:48:45 +0000186 GrTextureDesc fDesc;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000187
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 // these two shift a fixed-point value into normalized coordinates
189 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000190 int fShiftFixedX;
191 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194};
195
196#endif
197