blob: 79b39ec9f802036972cd4e47811ba01bd0a0952a [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;
16class GrSamplerState;
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 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000036 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000037 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000038 virtual size_t sizeInBytes() const SK_OVERRIDE {
robertphillips@google.com32716282012-06-04 12:48:45 +000039 return (size_t) fDesc.fWidth *
40 fDesc.fHeight *
41 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000042 }
43
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000044 // from GrSurface
reed@google.comac10a2d2010-12-22 21:39:39 +000045 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000046 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000047 * @param left left edge of the rectangle to read (inclusive)
48 * @param top top edge of the rectangle to read (inclusive)
49 * @param width width of rectangle to read in pixels.
50 * @param height height of rectangle to read in pixels.
51 * @param config the pixel config of the destination buffer
52 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000053 * @param rowBytes number of bytes bewtween consecutive rows. Zero
54 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000055 *
56 * @return true if the read succeeded, false if not. The read can fail
57 * because of a unsupported pixel config.
58 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000059 virtual bool readPixels(int left, int top, int width, int height,
60 GrPixelConfig config, void* buffer,
61 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000062
63 /**
64 * Writes a rectangle of pixels to the texture.
65 * @param left left edge of the rectangle to write (inclusive)
66 * @param top top edge of the rectangle to write (inclusive)
67 * @param width width of rectangle to write in pixels.
68 * @param height height of rectangle to write in pixels.
69 * @param config the pixel config of the source buffer
70 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +000071 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +000072 * means rows are tightly packed.
73 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000074 virtual void writePixels(int left, int top, int width, int height,
75 GrPixelConfig config, const void* buffer,
76 size_t rowBytes) SK_OVERRIDE;
77
78 /**
79 * @return this texture
80 */
81 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
82 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000083
84 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000085 * Retrieves the render target underlying this texture that can be passed to
86 * GrGpu::setRenderTarget().
87 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000088 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +000089 * render target
90 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000091 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
92 return fRenderTarget;
93 }
94 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
95 return fRenderTarget;
96 }
97
98 // GrTexture
99 /**
100 * Convert from texels to normalized texture coords for POT textures
101 * only.
102 */
103 GrFixed normalizeFixedX(GrFixed x) const {
104 GrAssert(GrIsPow2(fDesc.fWidth));
105 return x >> fShiftFixedX;
106 }
107 GrFixed normalizeFixedY(GrFixed y) const {
108 GrAssert(GrIsPow2(fDesc.fHeight));
109 return y >> fShiftFixedY;
110 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000113 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000115 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000117 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000118
119 /**
120 * Return the native ID or handle to the texture, depending on the
121 * platform. e.g. on opengl, return the texture ID.
122 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000123 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000125 /**
126 * Call this when the state of the native API texture object is
127 * altered directly, without being tracked by skia.
128 */
129 virtual void invalidateCachedState() = 0;
130
reed@google.comac10a2d2010-12-22 21:39:39 +0000131#if GR_DEBUG
132 void validate() const {
133 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000134
135 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 }
137#else
138 void validate() const {}
139#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000140
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000141 static GrResourceKey ComputeKey(const GrGpu* gpu,
142 const GrSamplerState* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000143 const GrTextureDesc& desc,
144 bool scratch);
145
146 static bool NeedsResizing(const GrResourceKey& key);
147 static bool IsScratchTexture(const GrResourceKey& key);
148 static bool NeedsFiltering(const GrResourceKey& key);
149
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000150protected:
151 GrRenderTarget* fRenderTarget; // texture refs its rt representation
152 // base class cons sets to NULL
153 // subclass cons can create and set
154
robertphillips@google.com32716282012-06-04 12:48:45 +0000155 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000156 : INHERITED(gpu, desc)
157 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000158
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000159 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000160 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
161 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000162 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000163
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000164 // GrResource overrides
165 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000166 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000167 }
168
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000169 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000170
robertphillips@google.com32716282012-06-04 12:48:45 +0000171 void validateDesc() const;
172
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000173private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 // these two shift a fixed-point value into normalized coordinates
175 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000176 int fShiftFixedX;
177 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000179 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000180};
181
182#endif
183