gfx/gl/GLTextureImage.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef GLTEXTUREIMAGE_H_
michael@0 7 #define GLTEXTUREIMAGE_H_
michael@0 8
michael@0 9 #include "nsAutoPtr.h"
michael@0 10 #include "nsRegion.h"
michael@0 11 #include "nsTArray.h"
michael@0 12 #include "gfxTypes.h"
michael@0 13 #include "GLContextTypes.h"
michael@0 14 #include "GraphicsFilter.h"
michael@0 15 #include "mozilla/gfx/Rect.h"
michael@0 16 #include "mozilla/RefPtr.h"
michael@0 17
michael@0 18 class gfxASurface;
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace gfx {
michael@0 22 class DataSourceSurface;
michael@0 23 class DrawTarget;
michael@0 24 }
michael@0 25 }
michael@0 26
michael@0 27 namespace mozilla {
michael@0 28 namespace gl {
michael@0 29 class GLContext;
michael@0 30
michael@0 31 /**
michael@0 32 * A TextureImage encapsulates a surface that can be drawn to by a
michael@0 33 * Thebes gfxContext and (hopefully efficiently!) synchronized to a
michael@0 34 * texture in the server. TextureImages are associated with one and
michael@0 35 * only one GLContext.
michael@0 36 *
michael@0 37 * Implementation note: TextureImages attempt to unify two categories
michael@0 38 * of backends
michael@0 39 *
michael@0 40 * (1) proxy to server-side object that can be bound to a texture;
michael@0 41 * e.g. Pixmap on X11.
michael@0 42 *
michael@0 43 * (2) efficient manager of texture memory; e.g. by having clients draw
michael@0 44 * into a scratch buffer which is then uploaded with
michael@0 45 * glTexSubImage2D().
michael@0 46 */
michael@0 47 class TextureImage
michael@0 48 {
michael@0 49 NS_INLINE_DECL_REFCOUNTING(TextureImage)
michael@0 50 public:
michael@0 51 enum TextureState
michael@0 52 {
michael@0 53 Created, // Texture created, but has not had glTexImage called to initialize it.
michael@0 54 Allocated, // Texture memory exists, but contents are invalid.
michael@0 55 Valid // Texture fully ready to use.
michael@0 56 };
michael@0 57
michael@0 58 enum Flags {
michael@0 59 NoFlags = 0x0,
michael@0 60 UseNearestFilter = 0x1,
michael@0 61 NeedsYFlip = 0x2,
michael@0 62 DisallowBigImage = 0x4
michael@0 63 };
michael@0 64
michael@0 65 typedef gfxContentType ContentType;
michael@0 66 typedef gfxImageFormat ImageFormat;
michael@0 67
michael@0 68 static already_AddRefed<TextureImage> Create(
michael@0 69 GLContext* gl,
michael@0 70 const nsIntSize& aSize,
michael@0 71 TextureImage::ContentType aContentType,
michael@0 72 GLenum aWrapMode,
michael@0 73 TextureImage::Flags aFlags = TextureImage::NoFlags);
michael@0 74 // Moz2D equivalent...
michael@0 75 static already_AddRefed<TextureImage> Create(
michael@0 76 GLContext* gl,
michael@0 77 const gfx::IntSize& aSize,
michael@0 78 TextureImage::ContentType aContentType,
michael@0 79 GLenum aWrapMode,
michael@0 80 TextureImage::Flags aFlags = TextureImage::NoFlags);
michael@0 81
michael@0 82 /**
michael@0 83 * Returns a gfxASurface for updating |aRegion| of the client's
michael@0 84 * image if successul, nullptr if not. |aRegion|'s bounds must fit
michael@0 85 * within Size(); its coordinate space (if any) is ignored. If
michael@0 86 * the update begins successfully, the returned gfxASurface is
michael@0 87 * owned by this. Otherwise, nullptr is returned.
michael@0 88 *
michael@0 89 * |aRegion| is an inout param: the returned region is what the
michael@0 90 * client must repaint. Category (1) regions above can
michael@0 91 * efficiently handle repaints to "scattered" regions, while (2)
michael@0 92 * can only efficiently handle repaints to rects.
michael@0 93 *
michael@0 94 * Painting the returned surface outside of |aRegion| results
michael@0 95 * in undefined behavior.
michael@0 96 *
michael@0 97 * BeginUpdate() calls cannot be "nested", and each successful
michael@0 98 * BeginUpdate() must be followed by exactly one EndUpdate() (see
michael@0 99 * below). Failure to do so can leave this in a possibly
michael@0 100 * inconsistent state. Unsuccessful BeginUpdate()s must not be
michael@0 101 * followed by EndUpdate().
michael@0 102 */
michael@0 103 virtual gfx::DrawTarget* BeginUpdate(nsIntRegion& aRegion) = 0;
michael@0 104 /**
michael@0 105 * Retrieves the region that will require updating, given a
michael@0 106 * region that needs to be updated. This can be used for
michael@0 107 * making decisions about updating before calling BeginUpdate().
michael@0 108 *
michael@0 109 * |aRegion| is an inout param.
michael@0 110 */
michael@0 111 virtual void GetUpdateRegion(nsIntRegion& aForRegion) {
michael@0 112 }
michael@0 113 /**
michael@0 114 * Finish the active update and synchronize with the server, if
michael@0 115 * necessary.
michael@0 116 *
michael@0 117 * BeginUpdate() must have been called exactly once before
michael@0 118 * EndUpdate().
michael@0 119 */
michael@0 120 virtual void EndUpdate() = 0;
michael@0 121
michael@0 122 /**
michael@0 123 * The Image may contain several textures for different regions (tiles).
michael@0 124 * These functions iterate over each sub texture image tile.
michael@0 125 */
michael@0 126 virtual void BeginTileIteration() {
michael@0 127 }
michael@0 128
michael@0 129 virtual bool NextTile() {
michael@0 130 return false;
michael@0 131 }
michael@0 132
michael@0 133 // Function prototype for a tile iteration callback. Returning false will
michael@0 134 // cause iteration to be interrupted (i.e. the corresponding NextTile call
michael@0 135 // will return false).
michael@0 136 typedef bool (* TileIterationCallback)(TextureImage* aImage,
michael@0 137 int aTileNumber,
michael@0 138 void* aCallbackData);
michael@0 139
michael@0 140 // Sets a callback to be called every time NextTile is called.
michael@0 141 virtual void SetIterationCallback(TileIterationCallback aCallback,
michael@0 142 void* aCallbackData) {
michael@0 143 }
michael@0 144
michael@0 145 virtual gfx::IntRect GetTileRect();
michael@0 146
michael@0 147 virtual GLuint GetTextureID() = 0;
michael@0 148
michael@0 149 virtual uint32_t GetTileCount() {
michael@0 150 return 1;
michael@0 151 }
michael@0 152
michael@0 153 /**
michael@0 154 * Set this TextureImage's size, and ensure a texture has been
michael@0 155 * allocated. Must not be called between BeginUpdate and EndUpdate.
michael@0 156 * After a resize, the contents are undefined.
michael@0 157 *
michael@0 158 * If this isn't implemented by a subclass, it will just perform
michael@0 159 * a dummy BeginUpdate/EndUpdate pair.
michael@0 160 */
michael@0 161 virtual void Resize(const gfx::IntSize& aSize) {
michael@0 162 mSize = aSize;
michael@0 163 nsIntRegion r(nsIntRect(0, 0, aSize.width, aSize.height));
michael@0 164 BeginUpdate(r);
michael@0 165 EndUpdate();
michael@0 166 }
michael@0 167
michael@0 168 /**
michael@0 169 * Mark this texture as having valid contents. Call this after modifying
michael@0 170 * the texture contents externally.
michael@0 171 */
michael@0 172 virtual void MarkValid() {}
michael@0 173
michael@0 174 /**
michael@0 175 * aSurf - the source surface to update from
michael@0 176 * aRegion - the region in this image to update
michael@0 177 * aFrom - offset in the source to update from
michael@0 178 */
michael@0 179 virtual bool DirectUpdate(gfx::DataSourceSurface* aSurf, const nsIntRegion& aRegion, const gfx::IntPoint& aFrom = gfx::IntPoint(0,0)) = 0;
michael@0 180 bool UpdateFromDataSource(gfx::DataSourceSurface *aSurf,
michael@0 181 const nsIntRegion* aDstRegion = nullptr,
michael@0 182 const gfx::IntPoint* aSrcOffset = nullptr);
michael@0 183
michael@0 184 virtual void BindTexture(GLenum aTextureUnit) = 0;
michael@0 185
michael@0 186 /**
michael@0 187 * Returns the image format of the texture. Only valid after a matching
michael@0 188 * BeginUpdate/EndUpdate pair have been called.
michael@0 189 */
michael@0 190 virtual gfx::SurfaceFormat GetTextureFormat() {
michael@0 191 return mTextureFormat;
michael@0 192 }
michael@0 193
michael@0 194 /** Can be called safely at any time. */
michael@0 195
michael@0 196 /**
michael@0 197 * If this TextureImage has a permanent gfxASurface backing,
michael@0 198 * return it. Otherwise return nullptr.
michael@0 199 */
michael@0 200 virtual already_AddRefed<gfxASurface> GetBackingSurface()
michael@0 201 { return nullptr; }
michael@0 202
michael@0 203
michael@0 204 gfx::IntSize GetSize() const;
michael@0 205 ContentType GetContentType() const { return mContentType; }
michael@0 206 ImageFormat GetImageFormat() const { return mImageFormat; }
michael@0 207 virtual bool InUpdate() const = 0;
michael@0 208 GLenum GetWrapMode() const { return mWrapMode; }
michael@0 209
michael@0 210 void SetFilter(GraphicsFilter aFilter) { mFilter = aFilter; }
michael@0 211
michael@0 212 protected:
michael@0 213 friend class GLContext;
michael@0 214
michael@0 215 /**
michael@0 216 * After the ctor, the TextureImage is invalid. Implementations
michael@0 217 * must allocate resources successfully before returning the new
michael@0 218 * TextureImage from GLContext::CreateTextureImage(). That is,
michael@0 219 * clients must not be given partially-constructed TextureImages.
michael@0 220 */
michael@0 221 TextureImage(const nsIntSize& aSize,
michael@0 222 GLenum aWrapMode, ContentType aContentType,
michael@0 223 Flags aFlags = NoFlags,
michael@0 224 ImageFormat aImageFormat = gfxImageFormat::Unknown)
michael@0 225 : mSize(aSize.ToIntSize())
michael@0 226 , mWrapMode(aWrapMode)
michael@0 227 , mContentType(aContentType)
michael@0 228 , mImageFormat(aImageFormat)
michael@0 229 , mFilter(GraphicsFilter::FILTER_GOOD)
michael@0 230 , mFlags(aFlags)
michael@0 231 {}
michael@0 232
michael@0 233 // Moz2D equivalent...
michael@0 234 TextureImage(const gfx::IntSize& aSize,
michael@0 235 GLenum aWrapMode, ContentType aContentType,
michael@0 236 Flags aFlags = NoFlags);
michael@0 237
michael@0 238 // Protected destructor, to discourage deletion outside of Release():
michael@0 239 virtual ~TextureImage() {}
michael@0 240
michael@0 241 virtual gfx::IntRect GetSrcTileRect();
michael@0 242
michael@0 243 gfx::IntSize mSize;
michael@0 244 GLenum mWrapMode;
michael@0 245 ContentType mContentType;
michael@0 246 ImageFormat mImageFormat;
michael@0 247 gfx::SurfaceFormat mTextureFormat;
michael@0 248 GraphicsFilter mFilter;
michael@0 249 Flags mFlags;
michael@0 250 };
michael@0 251
michael@0 252 /**
michael@0 253 * BasicTextureImage is the baseline TextureImage implementation ---
michael@0 254 * it updates its texture by allocating a scratch buffer for the
michael@0 255 * client to draw into, then using glTexSubImage2D() to upload the new
michael@0 256 * pixels. Platforms must provide the code to create a new surface
michael@0 257 * into which the updated pixels will be drawn, and the code to
michael@0 258 * convert the update surface's pixels into an image on which we can
michael@0 259 * glTexSubImage2D().
michael@0 260 */
michael@0 261 class BasicTextureImage
michael@0 262 : public TextureImage
michael@0 263 {
michael@0 264 public:
michael@0 265 virtual ~BasicTextureImage();
michael@0 266
michael@0 267 BasicTextureImage(GLuint aTexture,
michael@0 268 const nsIntSize& aSize,
michael@0 269 GLenum aWrapMode,
michael@0 270 ContentType aContentType,
michael@0 271 GLContext* aContext,
michael@0 272 TextureImage::Flags aFlags = TextureImage::NoFlags,
michael@0 273 TextureImage::ImageFormat aImageFormat = gfxImageFormat::Unknown);
michael@0 274 BasicTextureImage(GLuint aTexture,
michael@0 275 const gfx::IntSize& aSize,
michael@0 276 GLenum aWrapMode,
michael@0 277 ContentType aContentType,
michael@0 278 GLContext* aContext,
michael@0 279 TextureImage::Flags aFlags = TextureImage::NoFlags,
michael@0 280 TextureImage::ImageFormat aImageFormat = gfxImageFormat::Unknown);
michael@0 281
michael@0 282 virtual void BindTexture(GLenum aTextureUnit);
michael@0 283
michael@0 284 virtual gfx::DrawTarget* BeginUpdate(nsIntRegion& aRegion);
michael@0 285 virtual void GetUpdateRegion(nsIntRegion& aForRegion);
michael@0 286 virtual void EndUpdate();
michael@0 287 virtual bool DirectUpdate(gfx::DataSourceSurface* aSurf, const nsIntRegion& aRegion, const gfx::IntPoint& aFrom = gfx::IntPoint(0,0));
michael@0 288 virtual GLuint GetTextureID() { return mTexture; }
michael@0 289 virtual TemporaryRef<gfx::DrawTarget>
michael@0 290 GetDrawTargetForUpdate(const gfx::IntSize& aSize, gfx::SurfaceFormat aFmt);
michael@0 291
michael@0 292 virtual void MarkValid() { mTextureState = Valid; }
michael@0 293
michael@0 294 // Call when drawing into the update surface is complete.
michael@0 295 // Returns true if textures should be upload with a relative
michael@0 296 // offset - See UploadSurfaceToTexture.
michael@0 297 virtual bool FinishedSurfaceUpdate();
michael@0 298
michael@0 299 // Call after surface data has been uploaded to a texture.
michael@0 300 virtual void FinishedSurfaceUpload();
michael@0 301
michael@0 302 virtual bool InUpdate() const { return !!mUpdateDrawTarget; }
michael@0 303
michael@0 304 virtual void Resize(const gfx::IntSize& aSize);
michael@0 305
michael@0 306 protected:
michael@0 307 GLuint mTexture;
michael@0 308 TextureState mTextureState;
michael@0 309 nsRefPtr<GLContext> mGLContext;
michael@0 310 RefPtr<gfx::DrawTarget> mUpdateDrawTarget;
michael@0 311 nsIntRegion mUpdateRegion;
michael@0 312
michael@0 313 // The offset into the update surface at which the update rect is located.
michael@0 314 nsIntPoint mUpdateOffset;
michael@0 315 };
michael@0 316
michael@0 317 /**
michael@0 318 * A container class that complements many sub TextureImages into a big TextureImage.
michael@0 319 * Aims to behave just like the real thing.
michael@0 320 */
michael@0 321
michael@0 322 class TiledTextureImage
michael@0 323 : public TextureImage
michael@0 324 {
michael@0 325 public:
michael@0 326 TiledTextureImage(GLContext* aGL,
michael@0 327 gfx::IntSize aSize,
michael@0 328 TextureImage::ContentType,
michael@0 329 TextureImage::Flags aFlags = TextureImage::NoFlags,
michael@0 330 TextureImage::ImageFormat aImageFormat = gfxImageFormat::Unknown);
michael@0 331 ~TiledTextureImage();
michael@0 332 void DumpDiv();
michael@0 333 virtual gfx::DrawTarget* BeginUpdate(nsIntRegion& aRegion);
michael@0 334 virtual void GetUpdateRegion(nsIntRegion& aForRegion);
michael@0 335 virtual void EndUpdate();
michael@0 336 virtual void Resize(const gfx::IntSize& aSize);
michael@0 337 virtual uint32_t GetTileCount();
michael@0 338 virtual void BeginTileIteration();
michael@0 339 virtual bool NextTile();
michael@0 340 virtual void SetIterationCallback(TileIterationCallback aCallback,
michael@0 341 void* aCallbackData);
michael@0 342 virtual gfx::IntRect GetTileRect();
michael@0 343 virtual GLuint GetTextureID() {
michael@0 344 return mImages[mCurrentImage]->GetTextureID();
michael@0 345 }
michael@0 346 virtual bool DirectUpdate(gfx::DataSourceSurface* aSurf, const nsIntRegion& aRegion, const gfx::IntPoint& aFrom = gfx::IntPoint(0,0));
michael@0 347 virtual bool InUpdate() const { return mInUpdate; }
michael@0 348 virtual void BindTexture(GLenum);
michael@0 349
michael@0 350 protected:
michael@0 351 virtual gfx::IntRect GetSrcTileRect();
michael@0 352
michael@0 353 unsigned int mCurrentImage;
michael@0 354 TileIterationCallback mIterationCallback;
michael@0 355 void* mIterationCallbackData;
michael@0 356 nsTArray< nsRefPtr<TextureImage> > mImages;
michael@0 357 bool mInUpdate;
michael@0 358 gfx::IntSize mSize;
michael@0 359 unsigned int mTileSize;
michael@0 360 unsigned int mRows, mColumns;
michael@0 361 GLContext* mGL;
michael@0 362 // A temporary draw target to faciliate cross-tile updates.
michael@0 363 RefPtr<gfx::DrawTarget> mUpdateDrawTarget;
michael@0 364 // The region of update requested
michael@0 365 nsIntRegion mUpdateRegion;
michael@0 366 TextureState mTextureState;
michael@0 367 TextureImage::ImageFormat mImageFormat;
michael@0 368 };
michael@0 369
michael@0 370 /**
michael@0 371 * Creates a TextureImage of the basic implementation, can be useful in cases
michael@0 372 * where we know we don't want to use platform-specific TextureImage.
michael@0 373 * In doubt, use GLContext::CreateTextureImage instead.
michael@0 374 */
michael@0 375 already_AddRefed<TextureImage>
michael@0 376 CreateBasicTextureImage(GLContext* aGL,
michael@0 377 const gfx::IntSize& aSize,
michael@0 378 TextureImage::ContentType aContentType,
michael@0 379 GLenum aWrapMode,
michael@0 380 TextureImage::Flags aFlags,
michael@0 381 TextureImage::ImageFormat aImageFormat = gfxImageFormat::Unknown);
michael@0 382
michael@0 383 /**
michael@0 384 * Return a valid, allocated TextureImage of |aSize| with
michael@0 385 * |aContentType|. If |aContentType| is COLOR, |aImageFormat| can be used
michael@0 386 * to hint at the preferred RGB format, however it is not necessarily
michael@0 387 * respected. The TextureImage's texture is configured to use
michael@0 388 * |aWrapMode| (usually GL_CLAMP_TO_EDGE or GL_REPEAT) and by
michael@0 389 * default, GL_LINEAR filtering. Specify
michael@0 390 * |aFlags=UseNearestFilter| for GL_NEAREST filtering. Specify
michael@0 391 * |aFlags=NeedsYFlip| if the image is flipped. Return
michael@0 392 * nullptr if creating the TextureImage fails.
michael@0 393 *
michael@0 394 * The returned TextureImage may only be used with this GLContext.
michael@0 395 * Attempting to use the returned TextureImage after this
michael@0 396 * GLContext is destroyed will result in undefined (and likely
michael@0 397 * crashy) behavior.
michael@0 398 */
michael@0 399 already_AddRefed<TextureImage>
michael@0 400 CreateTextureImage(GLContext* gl,
michael@0 401 const gfx::IntSize& aSize,
michael@0 402 TextureImage::ContentType aContentType,
michael@0 403 GLenum aWrapMode,
michael@0 404 TextureImage::Flags aFlags = TextureImage::NoFlags,
michael@0 405 TextureImage::ImageFormat aImageFormat = gfxImageFormat::Unknown);
michael@0 406
michael@0 407 } // namespace gl
michael@0 408 } // namespace mozilla
michael@0 409
michael@0 410 #endif /* GLTEXTUREIMAGE_H_ */

mercurial