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" |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame^] | 22 | #include "SkBitmap.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "SkDevice.h" |
| 24 | #include "SkRegion.h" |
| 25 | |
| 26 | struct SkDrawProcs; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | struct GrSkDrawProcs; |
| 28 | class GrTextContext; |
| 29 | |
| 30 | /** |
| 31 | * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the |
| 32 | * canvas. |
| 33 | */ |
bsalomon@google.com | 9182610 | 2011-03-21 19:51:57 +0000 | [diff] [blame] | 34 | class SK_API SkGpuDevice : public SkDevice { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | public: |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 36 | /** |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame^] | 37 | * New device that will create an offscreen renderTarget based on the |
| 38 | * config, width, height. |
| 39 | * |
| 40 | * isForSaveLayer is a special flag that should only be set by SkCanvas |
| 41 | * internally. |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 42 | */ |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame^] | 43 | SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, |
| 44 | bool isForSaveLayer = false); |
| 45 | |
| 46 | /** |
| 47 | * New device that will render to the specified renderTarget. |
| 48 | */ |
| 49 | SkGpuDevice(GrContext*, GrRenderTarget*); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Magic value that can be passed to constructor. Causes |
| 53 | * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D). |
| 54 | * This isn't a valid pointer, don't attempt to dereference. |
| 55 | */ |
| 56 | static GrRenderTarget* Current3DApiRenderTarget(); |
| 57 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | virtual ~SkGpuDevice(); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 59 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | GrContext* context() const { return fContext; } |
| 61 | |
| 62 | /** |
| 63 | * If this device was built for rendering as a layer (i.e. offscreen), |
| 64 | * then this will return the platform-specific handle to that GPU resource. |
| 65 | * For example, in OpenGL, this will return the FBO's texture ID. |
| 66 | * If this device was not built for rendering as a layer, then 0 |
| 67 | * is returned. |
| 68 | */ |
| 69 | intptr_t getLayerTextureHandle() const; |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 70 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | // call to set the clip to the specified rect |
| 72 | void scissor(const SkIRect&); |
| 73 | |
| 74 | /** |
| 75 | * Override from SkGpuDevice, so we can set our FBO to be the render target |
| 76 | * The canvas parameter must be a SkGpuCanvas |
| 77 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 78 | virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&, |
| 79 | const SkClipStack& clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | |
| 81 | virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; } |
| 82 | |
| 83 | // overrides from SkDevice |
| 84 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 85 | virtual void clear(SkColor color); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); |
| 87 | virtual void writePixels(const SkBitmap& bitmap, int x, int y); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 88 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 89 | virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 90 | const SkClipStack&); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | |
| 92 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 93 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 94 | const SkPoint[], const SkPaint& paint); |
| 95 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 96 | const SkPaint& paint); |
| 97 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 98 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 99 | bool pathIsMutable); |
| 100 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 101 | const SkIRect* srcRectOrNull, |
| 102 | const SkMatrix& matrix, const SkPaint& paint); |
| 103 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 104 | int x, int y, const SkPaint& paint); |
| 105 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 106 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 107 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 108 | const SkScalar pos[], SkScalar constY, |
| 109 | int scalarsPerPos, const SkPaint& paint); |
| 110 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 111 | const SkPath& path, const SkMatrix* matrix, |
| 112 | const SkPaint& paint); |
| 113 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 114 | const SkPoint verts[], const SkPoint texs[], |
| 115 | const SkColor colors[], SkXfermode* xmode, |
| 116 | const uint16_t indices[], int indexCount, |
| 117 | const SkPaint& paint); |
| 118 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 119 | const SkPaint&); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 120 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 121 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | virtual void flush() { fContext->flush(false); } |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | * Make's this device's rendertarget current in the underlying 3D API. |
| 126 | * Also implicitly flushes. |
| 127 | */ |
| 128 | virtual void makeRenderTargetCurrent(); |
| 129 | |
| 130 | protected: |
mike@reedtribe.org | ea4ac97 | 2011-04-26 11:48:33 +0000 | [diff] [blame] | 131 | // override |
| 132 | virtual SkDeviceFactory* onNewDeviceFactory(); |
| 133 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | class TexCache; |
| 135 | TexCache* lockCachedTexture(const SkBitmap& bitmap, |
| 136 | const GrSamplerState& sampler, |
| 137 | GrTexture** texture, |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 138 | bool forDeviceRenderTarget = false, |
| 139 | bool isSaveLayer = false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | void unlockCachedTexture(TexCache*); |
| 141 | |
| 142 | class SkAutoCachedTexture { |
| 143 | public: |
| 144 | SkAutoCachedTexture(); |
| 145 | SkAutoCachedTexture(SkGpuDevice* device, |
| 146 | const SkBitmap& bitmap, |
| 147 | const GrSamplerState& sampler, |
| 148 | GrTexture** texture); |
| 149 | ~SkAutoCachedTexture(); |
| 150 | |
| 151 | GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&); |
| 152 | |
| 153 | private: |
| 154 | SkGpuDevice* fDevice; |
| 155 | TexCache* fTex; |
| 156 | }; |
| 157 | friend class SkAutoTexCache; |
| 158 | |
| 159 | private: |
| 160 | GrContext* fContext; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 161 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | GrSkDrawProcs* fDrawProcs; |
| 163 | |
| 164 | // state for our offscreen render-target |
| 165 | TexCache* fCache; |
| 166 | GrTexture* fTexture; |
| 167 | GrRenderTarget* fRenderTarget; |
| 168 | bool fNeedClear; |
| 169 | bool fNeedPrepareRenderTarget; |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 170 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 171 | // doesn't set the texture/sampler/matrix state |
| 172 | // caller needs to null out GrPaint's texture if |
| 173 | // non-textured drawing is desired. |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 174 | // Set constantColor to true if a constant color |
| 175 | // will be used. This is an optimization, and can |
| 176 | // always be set to false. constantColor should |
| 177 | // never be true if justAlpha is true. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 178 | bool skPaint2GrPaintNoShader(const SkPaint& skPaint, |
| 179 | bool justAlpha, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 180 | GrPaint* grPaint, |
| 181 | bool constantColor); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 182 | |
| 183 | // uses the SkShader to setup paint, act used to |
| 184 | // hold lock on cached texture and free it when |
| 185 | // destroyed. |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 186 | // If there is no shader, constantColor will |
| 187 | // be passed to skPaint2GrPaintNoShader. Otherwise |
| 188 | // it is ignored. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 189 | bool skPaint2GrPaintShader(const SkPaint& skPaint, |
| 190 | SkAutoCachedTexture* act, |
| 191 | const SkMatrix& ctm, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 192 | GrPaint* grPaint, |
| 193 | bool constantColor); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 194 | |
| 195 | SkDrawProcs* initDrawForText(GrTextContext*); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 196 | bool bindDeviceAsTexture(GrPaint* paint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 197 | |
| 198 | void prepareRenderTarget(const SkDraw&); |
| 199 | void internalDrawBitmap(const SkDraw&, const SkBitmap&, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 200 | const SkIRect&, const SkMatrix&, GrPaint* grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | |
| 202 | typedef SkDevice INHERITED; |
| 203 | }; |
| 204 | |
| 205 | #endif |
| 206 | |