blob: c3e2946f766123a4e637b22cc17e312f8903a1e9 [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
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020class GrTexture : public GrResource {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022public:
reed@google.comac10a2d2010-12-22 21:39:39 +000023 /**
robertphillips@google.coma1e57952012-06-04 20:05:28 +000024 * Key generated by client. Should be a unique key on the texture data.
25 * Does not need to consider that width and height of the texture. Two
26 * textures with the same TextureKey but different bounds will not collide.
27 */
28 typedef uint64_t TextureKey;
29
30 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000031 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000032 *
reed@google.comac10a2d2010-12-22 21:39:39 +000033 * @return the width in texels
34 */
robertphillips@google.com32716282012-06-04 12:48:45 +000035 int width() const { return fDesc.fWidth; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036
reed@google.comac10a2d2010-12-22 21:39:39 +000037 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000038 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000039 *
reed@google.comac10a2d2010-12-22 21:39:39 +000040 * @return the height in texels
41 */
robertphillips@google.com32716282012-06-04 12:48:45 +000042 int height() const { return fDesc.fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +000043
44 /**
45 * Convert from texels to normalized texture coords for POT textures
46 * only.
47 */
robertphillips@google.com32716282012-06-04 12:48:45 +000048 GrFixed normalizeFixedX(GrFixed x) const {
49 GrAssert(GrIsPow2(fDesc.fWidth));
50 return x >> fShiftFixedX;
51 }
52 GrFixed normalizeFixedY(GrFixed y) const {
53 GrAssert(GrIsPow2(fDesc.fHeight));
54 return y >> fShiftFixedY;
55 }
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.com1c13c962011-02-14 16:51:21 +000057 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000058 * Retrieves the pixel config specified when the texture was created.
59 */
robertphillips@google.com32716282012-06-04 12:48:45 +000060 GrPixelConfig config() const { return fDesc.fConfig; }
61
62 /**
63 * Return the descriptor describing the texture
64 */
65 const GrTextureDesc& desc() const { return fDesc; }
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000068 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000069 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +000070 virtual size_t sizeInBytes() const {
robertphillips@google.com32716282012-06-04 12:48:45 +000071 return (size_t) fDesc.fWidth *
72 fDesc.fHeight *
73 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000074 }
75
76 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000077 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000078 * @param left left edge of the rectangle to read (inclusive)
79 * @param top top edge of the rectangle to read (inclusive)
80 * @param width width of rectangle to read in pixels.
81 * @param height height of rectangle to read in pixels.
82 * @param config the pixel config of the destination buffer
83 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000084 * @param rowBytes number of bytes bewtween consecutive rows. Zero
85 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000086 *
87 * @return true if the read succeeded, false if not. The read can fail
88 * because of a unsupported pixel config.
89 */
90 bool readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000091 GrPixelConfig config, void* buffer,
92 size_t rowBytes);
93
94 /**
95 * Writes a rectangle of pixels to the texture.
96 * @param left left edge of the rectangle to write (inclusive)
97 * @param top top edge of the rectangle to write (inclusive)
98 * @param width width of rectangle to write in pixels.
99 * @param height height of rectangle to write in pixels.
100 * @param config the pixel config of the source buffer
101 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +0000102 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000103 * means rows are tightly packed.
104 */
105 void writePixels(int left, int top, int width, int height,
106 GrPixelConfig config, const void* buffer,
107 size_t rowBytes);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000108
109 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 * Retrieves the render target underlying this texture that can be passed to
111 * GrGpu::setRenderTarget().
112 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000113 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 * render target
115 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000116 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000117 const GrRenderTarget* asRenderTarget() const { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000118
119 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000120 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000122 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000124 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
126 /**
127 * Return the native ID or handle to the texture, depending on the
128 * platform. e.g. on opengl, return the texture ID.
129 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000130 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
132#if GR_DEBUG
133 void validate() const {
134 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000135
136 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 }
138#else
139 void validate() const {}
140#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000141
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000142 static GrResourceKey ComputeKey(const GrGpu* gpu,
143 const GrSamplerState* sampler,
144 TextureKey clientKey,
145 const GrTextureDesc& desc,
146 bool scratch);
147
148 static bool NeedsResizing(const GrResourceKey& key);
149 static bool IsScratchTexture(const GrResourceKey& key);
150 static bool NeedsFiltering(const GrResourceKey& key);
151
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000152protected:
153 GrRenderTarget* fRenderTarget; // texture refs its rt representation
154 // base class cons sets to NULL
155 // subclass cons can create and set
156
robertphillips@google.com32716282012-06-04 12:48:45 +0000157 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000158 : INHERITED(gpu)
159 , fRenderTarget(NULL)
robertphillips@google.com32716282012-06-04 12:48:45 +0000160 , fDesc(desc) {
161
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000162 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000163 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
164 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000165 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000166
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000167 // GrResource overrides
168 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000169 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000170 }
171
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000173
robertphillips@google.com32716282012-06-04 12:48:45 +0000174 void validateDesc() const;
175
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000176private:
robertphillips@google.com32716282012-06-04 12:48:45 +0000177 GrTextureDesc fDesc;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000178
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 // these two shift a fixed-point value into normalized coordinates
180 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000181 int fShiftFixedX;
182 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000184 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000185};
186
187#endif
188