blob: 704bb28f0b34e0619bfd57c913c984dd4c551564 [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:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000034 SK_DECLARE_INST_COUNT(GrTexture)
35
reed@google.comac10a2d2010-12-22 21:39:39 +000036 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000037 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000038 *
reed@google.comac10a2d2010-12-22 21:39:39 +000039 * @return the width in texels
40 */
robertphillips@google.com32716282012-06-04 12:48:45 +000041 int width() const { return fDesc.fWidth; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000042
reed@google.comac10a2d2010-12-22 21:39:39 +000043 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000044 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000045 *
reed@google.comac10a2d2010-12-22 21:39:39 +000046 * @return the height in texels
47 */
robertphillips@google.com32716282012-06-04 12:48:45 +000048 int height() const { return fDesc.fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +000049
50 /**
51 * Convert from texels to normalized texture coords for POT textures
52 * only.
53 */
robertphillips@google.com32716282012-06-04 12:48:45 +000054 GrFixed normalizeFixedX(GrFixed x) const {
55 GrAssert(GrIsPow2(fDesc.fWidth));
56 return x >> fShiftFixedX;
57 }
58 GrFixed normalizeFixedY(GrFixed y) const {
59 GrAssert(GrIsPow2(fDesc.fHeight));
60 return y >> fShiftFixedY;
61 }
reed@google.comac10a2d2010-12-22 21:39:39 +000062
bsalomon@google.com1c13c962011-02-14 16:51:21 +000063 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000064 * Retrieves the pixel config specified when the texture was created.
65 */
robertphillips@google.com32716282012-06-04 12:48:45 +000066 GrPixelConfig config() const { return fDesc.fConfig; }
67
68 /**
69 * Return the descriptor describing the texture
70 */
71 const GrTextureDesc& desc() const { return fDesc; }
reed@google.comac10a2d2010-12-22 21:39:39 +000072
73 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000074 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000075 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +000076 virtual size_t sizeInBytes() const {
robertphillips@google.com32716282012-06-04 12:48:45 +000077 return (size_t) fDesc.fWidth *
78 fDesc.fHeight *
79 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000080 }
81
82 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000083 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000084 * @param left left edge of the rectangle to read (inclusive)
85 * @param top top edge of the rectangle to read (inclusive)
86 * @param width width of rectangle to read in pixels.
87 * @param height height of rectangle to read in pixels.
88 * @param config the pixel config of the destination buffer
89 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000090 * @param rowBytes number of bytes bewtween consecutive rows. Zero
91 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000092 *
93 * @return true if the read succeeded, false if not. The read can fail
94 * because of a unsupported pixel config.
95 */
96 bool readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000097 GrPixelConfig config, void* buffer,
98 size_t rowBytes);
99
100 /**
101 * Writes a rectangle of pixels to the texture.
102 * @param left left edge of the rectangle to write (inclusive)
103 * @param top top edge of the rectangle to write (inclusive)
104 * @param width width of rectangle to write in pixels.
105 * @param height height of rectangle to write in pixels.
106 * @param config the pixel config of the source buffer
107 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +0000108 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000109 * means rows are tightly packed.
110 */
111 void writePixels(int left, int top, int width, int height,
112 GrPixelConfig config, const void* buffer,
113 size_t rowBytes);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000114
115 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 * Retrieves the render target underlying this texture that can be passed to
117 * GrGpu::setRenderTarget().
118 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000119 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 * render target
121 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000122 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000123 const GrRenderTarget* asRenderTarget() const { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
125 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000126 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000128 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000130 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
132 /**
133 * Return the native ID or handle to the texture, depending on the
134 * platform. e.g. on opengl, return the texture ID.
135 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000136 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000138 /**
139 * Call this when the state of the native API texture object is
140 * altered directly, without being tracked by skia.
141 */
142 virtual void invalidateCachedState() = 0;
143
reed@google.comac10a2d2010-12-22 21:39:39 +0000144#if GR_DEBUG
145 void validate() const {
146 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000147
148 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 }
150#else
151 void validate() const {}
152#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000153
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000154 static GrResourceKey ComputeKey(const GrGpu* gpu,
155 const GrSamplerState* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000156 const GrTextureDesc& desc,
157 bool scratch);
158
159 static bool NeedsResizing(const GrResourceKey& key);
160 static bool IsScratchTexture(const GrResourceKey& key);
161 static bool NeedsFiltering(const GrResourceKey& key);
162
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000163protected:
164 GrRenderTarget* fRenderTarget; // texture refs its rt representation
165 // base class cons sets to NULL
166 // subclass cons can create and set
167
robertphillips@google.com32716282012-06-04 12:48:45 +0000168 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000169 : INHERITED(gpu)
170 , fRenderTarget(NULL)
robertphillips@google.com32716282012-06-04 12:48:45 +0000171 , fDesc(desc) {
172
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000173 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000174 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
175 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000176 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000177
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000178 // GrResource overrides
179 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000180 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000181 }
182
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000183 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000184
robertphillips@google.com32716282012-06-04 12:48:45 +0000185 void validateDesc() const;
186
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000187private:
robertphillips@google.com32716282012-06-04 12:48:45 +0000188 GrTextureDesc fDesc;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000189
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 // these two shift a fixed-point value into normalized coordinates
191 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000192 int fShiftFixedX;
193 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000195 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000196};
197
198#endif
199