epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * 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. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkGLDevice_DEFINED |
| 9 | #define SkGLDevice_DEFINED |
| 10 | |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkGL.h" |
| 13 | #include "SkRegion.h" |
| 14 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 15 | #ifdef SK_BUILD_FOR_MAC |
| 16 | #include <OpenGL/gl.h> |
| 17 | #elif defined(ANDROID) |
| 18 | #include <GLES/gl.h> |
| 19 | #endif |
| 20 | |
| 21 | class SkGLDeviceFactory : public SkDeviceFactory { |
| 22 | public: |
| 23 | virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height, |
| 24 | bool isOpaque, bool isForLayer); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | struct SkGLDrawProcs; |
| 28 | |
| 29 | class SkGLDevice : public SkDevice { |
| 30 | public: |
| 31 | SkGLDevice(const SkBitmap& bitmap, bool offscreen); |
| 32 | virtual ~SkGLDevice(); |
| 33 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 34 | virtual SkDeviceFactory* getDeviceFactory() { |
| 35 | return SkNEW(SkGLDeviceFactory); |
| 36 | } |
| 37 | |
vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame] | 38 | virtual uint32_t getDeviceCapabilities() { return kGL_Capability; } |
| 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | // used to identify GLTextCache data in the glyphcache |
| 41 | static void GlyphCacheAuxProc(void* data); |
| 42 | |
| 43 | enum TexOrientation { |
| 44 | kNo_TexOrientation, |
| 45 | kTopToBottom_TexOrientation, |
| 46 | kBottomToTop_TexOrientation |
| 47 | }; |
| 48 | |
| 49 | /** Called when this device is no longer a candidate for a render target, |
| 50 | but will instead be used as a texture to be drawn. Be sure to call |
| 51 | the base impl if you override, as it will compute size and max. |
| 52 | */ |
| 53 | virtual TexOrientation bindDeviceAsTexture(); |
| 54 | |
| 55 | // returns true if complex |
| 56 | SkGLClipIter* updateMatrixClip(); |
| 57 | // call to set the clip to the specified rect |
| 58 | void scissor(const SkIRect&); |
| 59 | |
| 60 | // overrides from SkDevice |
| 61 | virtual void gainFocus(SkCanvas*); |
| 62 | virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip); |
| 63 | |
| 64 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 65 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 66 | const SkPoint[], const SkPaint& paint); |
| 67 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 68 | const SkPaint& paint); |
| 69 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 70 | const SkPaint& paint); |
| 71 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 72 | const SkMatrix& matrix, const SkPaint& paint); |
| 73 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 74 | int x, int y, const SkPaint& paint); |
| 75 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 76 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 77 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 78 | const SkScalar pos[], SkScalar constY, |
| 79 | int scalarsPerPos, const SkPaint& paint); |
| 80 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 81 | const SkPath& path, const SkMatrix* matrix, |
| 82 | const SkPaint& paint); |
| 83 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 84 | const SkPoint verts[], const SkPoint texs[], |
| 85 | const SkColor colors[], SkXfermode* xmode, |
| 86 | const uint16_t indices[], int indexCount, |
| 87 | const SkPaint& paint); |
| 88 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 89 | const SkPaint&); |
| 90 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 91 | // settings for the global texture cache |
| 92 | |
| 93 | static size_t GetTextureCacheMaxCount(); |
| 94 | static void SetTextureCacheMaxCount(size_t count); |
| 95 | |
| 96 | static size_t GetTextureCacheMaxSize(); |
| 97 | static void SetTextureCacheMaxSize(size_t size); |
| 98 | |
| 99 | /** Call glDeleteTextures for all textures (including those for text) |
| 100 | This should be called while the gl-context is still valid. Its purpose |
| 101 | is to free up gl resources. Note that if a bitmap or text is drawn after |
| 102 | this call, new caches will be created. |
| 103 | */ |
| 104 | static void DeleteAllTextures(); |
| 105 | |
| 106 | /** Forget all textures without calling delete (including those for text). |
| 107 | This should be called if the gl-context has changed, and the texture |
| 108 | IDs that have been cached are no longer valid. |
| 109 | */ |
| 110 | static void AbandonAllTextures(); |
| 111 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | protected: |
| 113 | /** Return the current glmatrix, from a previous call to setMatrixClip */ |
| 114 | const SkMatrix& matrix() const { return fMatrix; } |
| 115 | /** Return the current clip, from a previous call to setMatrixClip */ |
| 116 | const SkRegion& clip() const { return fClip; } |
| 117 | |
| 118 | private: |
| 119 | SkGLMatrix fGLMatrix; |
| 120 | SkMatrix fMatrix; |
| 121 | SkRegion fClip; |
| 122 | bool fDirty; |
| 123 | |
| 124 | SkGLClipIter fClipIter; |
| 125 | SkGLDrawProcs* fDrawProcs; |
| 126 | |
| 127 | void setupForText(SkDraw* draw, const SkPaint& paint); |
| 128 | |
| 129 | // global texture cache methods |
| 130 | class TexCache; |
| 131 | static TexCache* LockTexCache(const SkBitmap&, GLuint* name, |
| 132 | SkPoint* size); |
| 133 | static void UnlockTexCache(TexCache*); |
| 134 | class SkAutoLockTexCache { |
| 135 | public: |
| 136 | SkAutoLockTexCache(const SkBitmap& bitmap, GLuint* name, |
| 137 | SkPoint* size) { |
| 138 | fTex = SkGLDevice::LockTexCache(bitmap, name, size); |
| 139 | } |
| 140 | ~SkAutoLockTexCache() { |
| 141 | if (fTex) { |
| 142 | SkGLDevice::UnlockTexCache(fTex); |
| 143 | } |
| 144 | } |
| 145 | TexCache* get() const { return fTex; } |
| 146 | private: |
| 147 | TexCache* fTex; |
| 148 | }; |
| 149 | friend class SkAutoTexCache; |
| 150 | |
| 151 | // returns cache if the texture is bound for the shader |
| 152 | TexCache* setupGLPaintShader(const SkPaint& paint); |
| 153 | |
| 154 | class AutoPaintShader { |
| 155 | public: |
| 156 | AutoPaintShader(SkGLDevice*, const SkPaint& paint); |
| 157 | ~AutoPaintShader(); |
| 158 | |
| 159 | bool useTex() const { return fTexCache != 0; } |
| 160 | private: |
| 161 | SkGLDevice* fDevice; |
| 162 | TexCache* fTexCache; |
| 163 | }; |
| 164 | friend class AutoPaintShader; |
| 165 | |
| 166 | typedef SkDevice INHERITED; |
| 167 | }; |
| 168 | |
| 169 | #endif |
| 170 | |