reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef SkGpuDevice_DEFINED |
| 19 | #define SkGpuDevice_DEFINED |
| 20 | |
| 21 | #include "SkGr.h" |
| 22 | #include "SkDevice.h" |
| 23 | #include "SkRegion.h" |
| 24 | |
| 25 | struct SkDrawProcs; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | struct GrSkDrawProcs; |
| 27 | class GrTextContext; |
| 28 | |
| 29 | /** |
| 30 | * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the |
| 31 | * canvas. |
| 32 | */ |
| 33 | class SkGpuDevice : public SkDevice { |
| 34 | public: |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 35 | /** |
| 36 | * The SkGpuDevice will render to the GrRenderTarget, or if the paremeter is |
| 37 | * null it will create its own render target and manage that target's |
| 38 | * lifetime. |
| 39 | */ |
| 40 | SkGpuDevice(GrContext*, |
| 41 | const SkBitmap& bitmap, |
| 42 | GrRenderTarget* renderTargetOrNull); |
| 43 | |
| 44 | /** |
| 45 | * Magic value that can be passed to constructor. Causes |
| 46 | * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D). |
| 47 | * This isn't a valid pointer, don't attempt to dereference. |
| 48 | */ |
| 49 | static GrRenderTarget* Current3DApiRenderTarget(); |
| 50 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | virtual ~SkGpuDevice(); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 52 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | GrContext* context() const { return fContext; } |
| 54 | |
| 55 | /** |
| 56 | * If this device was built for rendering as a layer (i.e. offscreen), |
| 57 | * then this will return the platform-specific handle to that GPU resource. |
| 58 | * For example, in OpenGL, this will return the FBO's texture ID. |
| 59 | * If this device was not built for rendering as a layer, then 0 |
| 60 | * is returned. |
| 61 | */ |
| 62 | intptr_t getLayerTextureHandle() const; |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 63 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | // call to set the clip to the specified rect |
| 65 | void scissor(const SkIRect&); |
| 66 | |
| 67 | /** |
| 68 | * Override from SkGpuDevice, so we can set our FBO to be the render target |
| 69 | * The canvas parameter must be a SkGpuCanvas |
| 70 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 71 | virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&, |
| 72 | const SkClipStack& clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | |
| 74 | virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; } |
| 75 | |
| 76 | // overrides from SkDevice |
| 77 | |
| 78 | virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); |
| 79 | virtual void writePixels(const SkBitmap& bitmap, int x, int y); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 80 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 81 | virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 82 | const SkClipStack&); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | |
| 84 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 85 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 86 | const SkPoint[], const SkPaint& paint); |
| 87 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 88 | const SkPaint& paint); |
| 89 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 90 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 91 | bool pathIsMutable); |
| 92 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 93 | const SkIRect* srcRectOrNull, |
| 94 | const SkMatrix& matrix, const SkPaint& paint); |
| 95 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 96 | int x, int y, const SkPaint& paint); |
| 97 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 98 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 99 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 100 | const SkScalar pos[], SkScalar constY, |
| 101 | int scalarsPerPos, const SkPaint& paint); |
| 102 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 103 | const SkPath& path, const SkMatrix* matrix, |
| 104 | const SkPaint& paint); |
| 105 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 106 | const SkPoint verts[], const SkPoint texs[], |
| 107 | const SkColor colors[], SkXfermode* xmode, |
| 108 | const uint16_t indices[], int indexCount, |
| 109 | const SkPaint& paint); |
| 110 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 111 | const SkPaint&); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 112 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 113 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | virtual void flush() { fContext->flush(false); } |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | * Make's this device's rendertarget current in the underlying 3D API. |
| 118 | * Also implicitly flushes. |
| 119 | */ |
| 120 | virtual void makeRenderTargetCurrent(); |
| 121 | |
| 122 | protected: |
| 123 | class TexCache; |
| 124 | TexCache* lockCachedTexture(const SkBitmap& bitmap, |
| 125 | const GrSamplerState& sampler, |
| 126 | GrTexture** texture, |
| 127 | bool forDeviceRenderTarget = false); |
| 128 | void unlockCachedTexture(TexCache*); |
| 129 | |
| 130 | class SkAutoCachedTexture { |
| 131 | public: |
| 132 | SkAutoCachedTexture(); |
| 133 | SkAutoCachedTexture(SkGpuDevice* device, |
| 134 | const SkBitmap& bitmap, |
| 135 | const GrSamplerState& sampler, |
| 136 | GrTexture** texture); |
| 137 | ~SkAutoCachedTexture(); |
| 138 | |
| 139 | GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&); |
| 140 | |
| 141 | private: |
| 142 | SkGpuDevice* fDevice; |
| 143 | TexCache* fTex; |
| 144 | }; |
| 145 | friend class SkAutoTexCache; |
| 146 | |
| 147 | private: |
| 148 | GrContext* fContext; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 149 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | GrSkDrawProcs* fDrawProcs; |
| 151 | |
| 152 | // state for our offscreen render-target |
| 153 | TexCache* fCache; |
| 154 | GrTexture* fTexture; |
| 155 | GrRenderTarget* fRenderTarget; |
| 156 | bool fNeedClear; |
| 157 | bool fNeedPrepareRenderTarget; |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 158 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 159 | // doesn't set the texture/sampler/matrix state |
| 160 | // caller needs to null out GrPaint's texture if |
| 161 | // non-textured drawing is desired. |
| 162 | bool skPaint2GrPaintNoShader(const SkPaint& skPaint, |
| 163 | bool justAlpha, |
| 164 | GrPaint* grPaint); |
| 165 | |
| 166 | // uses the SkShader to setup paint, act used to |
| 167 | // hold lock on cached texture and free it when |
| 168 | // destroyed. |
| 169 | bool skPaint2GrPaintShader(const SkPaint& skPaint, |
| 170 | SkAutoCachedTexture* act, |
| 171 | const SkMatrix& ctm, |
| 172 | GrPaint* grPaint); |
| 173 | |
| 174 | SkDrawProcs* initDrawForText(GrTextContext*); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 175 | bool bindDeviceAsTexture(GrPaint* paint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | |
| 177 | void prepareRenderTarget(const SkDraw&); |
| 178 | void internalDrawBitmap(const SkDraw&, const SkBitmap&, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 179 | const SkIRect&, const SkMatrix&, GrPaint* grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | |
| 181 | typedef SkDevice INHERITED; |
| 182 | }; |
| 183 | |
| 184 | #endif |
| 185 | |