OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/ext/vector_platform_device_skia.h" | 5 #include "skia/ext/vector_platform_device_skia.h" |
6 | 6 |
7 #include "skia/ext/bitmap_platform_device.h" | 7 #include "skia/ext/bitmap_platform_device.h" |
8 #include "skia/ext/platform_canvas.h" | |
8 #include "third_party/skia/include/core/SkClipStack.h" | 9 #include "third_party/skia/include/core/SkClipStack.h" |
9 #include "third_party/skia/include/core/SkDraw.h" | 10 #include "third_party/skia/include/core/SkDraw.h" |
10 #include "third_party/skia/include/core/SkRect.h" | 11 #include "third_party/skia/include/core/SkRect.h" |
11 #include "third_party/skia/include/core/SkRegion.h" | 12 #include "third_party/skia/include/core/SkRegion.h" |
12 #include "third_party/skia/include/core/SkScalar.h" | 13 #include "third_party/skia/include/core/SkScalar.h" |
13 | 14 |
14 namespace skia { | 15 namespace skia { |
15 | 16 |
16 static inline SkBitmap makeABitmap(int width, int height) { | 17 static inline SkBitmap makeABitmap(int width, int height) { |
17 SkBitmap bitmap; | 18 SkBitmap bitmap; |
18 bitmap.setConfig(SkBitmap::kNo_Config, width, height); | 19 bitmap.setConfig(SkBitmap::kNo_Config, width, height); |
19 return bitmap; | 20 return bitmap; |
20 } | 21 } |
21 | 22 |
22 VectorPlatformDeviceSkia::VectorPlatformDeviceSkia(SkPDFDevice* pdf_device) | 23 VectorPlatformDeviceSkia::VectorPlatformDeviceSkia(SkPDFDevice* pdf_device) |
23 : PlatformDevice(makeABitmap(pdf_device->width(), pdf_device->height())), | 24 : PlatformDevice(pdf_device) { |
24 pdf_device_(pdf_device) { | 25 SetPlatformDevice(pdf_device, this, true); |
vandebo (ex-Chrome)
2011/08/19 21:54:22
This is the only place where ownership is transfer
Jeff Timanus
2011/08/20 02:31:04
Yes, that's a very good suggestion. It clears up
| |
25 } | 26 } |
26 | 27 |
27 VectorPlatformDeviceSkia::~VectorPlatformDeviceSkia() { | 28 VectorPlatformDeviceSkia::~VectorPlatformDeviceSkia() { |
28 } | 29 } |
29 | 30 |
30 bool VectorPlatformDeviceSkia::IsNativeFontRenderingAllowed() { | 31 bool VectorPlatformDeviceSkia::IsNativeFontRenderingAllowed() { |
31 return false; | 32 return false; |
32 } | 33 } |
33 | 34 |
34 PlatformDevice::PlatformSurface VectorPlatformDeviceSkia::BeginPlatformPaint() { | 35 PlatformDevice::PlatformSurface VectorPlatformDeviceSkia::BeginPlatformPaint() { |
35 // Even when drawing a vector representation of the page, we have to | 36 // Even when drawing a vector representation of the page, we have to |
36 // provide a raster surface for plugins to render into - they don't have | 37 // provide a raster surface for plugins to render into - they don't have |
37 // a vector interface. Therefore we create a BitmapPlatformDevice here | 38 // a vector interface. Therefore we create a BitmapPlatformDevice here |
38 // and return the context from it, then layer on the raster data as an | 39 // and return the context from it, then layer on the raster data as an |
39 // image in EndPlatformPaint. | 40 // image in EndPlatformPaint. |
40 DCHECK(raster_surface_ == NULL); | 41 DCHECK(raster_surface_ == NULL); |
41 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
42 raster_surface_ = BitmapPlatformDevice::create(pdf_device_->width(), | 43 raster_surface_ = BitmapPlatformDevice::create(GetOwningDevice()->width(), |
43 pdf_device_->height(), | 44 GetOwningDevice()->height(), |
44 false, /* not opaque */ | 45 false, /* not opaque */ |
45 NULL); | 46 NULL); |
46 #elif defined(OS_POSIX) && !defined(OS_MACOSX) | 47 #elif defined(OS_POSIX) && !defined(OS_MACOSX) |
47 raster_surface_ = BitmapPlatformDevice::Create(pdf_device_->width(), | 48 raster_surface_ = BitmapPlatformDevice::Create(GetOwningDevice()->width(), |
48 pdf_device_->height(), | 49 GetOwningDevice()->height(), |
49 false /* not opaque */); | 50 false /* not opaque */); |
50 #endif | 51 #endif |
51 raster_surface_->unref(); // SkRefPtr and create both took a reference. | 52 raster_surface_->unref(); // SkRefPtr and create both took a reference. |
52 | 53 |
53 SkCanvas canvas(raster_surface_.get()); | 54 raster_canvas_ = new SkCanvas(raster_surface_.get()); |
54 return raster_surface_->BeginPlatformPaint(); | 55 return skia::BeginPlatformPaint(raster_canvas_.get()); |
55 } | 56 } |
56 | 57 |
57 void VectorPlatformDeviceSkia::EndPlatformPaint() { | 58 void VectorPlatformDeviceSkia::EndPlatformPaint() { |
58 DCHECK(raster_surface_ != NULL); | 59 DCHECK(raster_surface_ != NULL); |
60 | |
59 SkPaint paint; | 61 SkPaint paint; |
60 // SkPDFDevice checks the passed SkDraw for an empty clip (only). Fake | 62 // SkPDFDevice checks the passed SkDraw for an empty clip (only). Fake |
61 // it out by setting a non-empty clip. | 63 // it out by setting a non-empty clip. |
62 SkDraw draw; | 64 SkDraw draw; |
63 SkRegion clip(SkIRect::MakeWH(pdf_device_->width(), pdf_device_->height())); | 65 SkRegion clip(SkIRect::MakeWH(GetOwningDevice()->width(), |
66 GetOwningDevice()->height())); | |
64 draw.fClip=&clip; | 67 draw.fClip=&clip; |
65 pdf_device_->drawSprite(draw, raster_surface_->accessBitmap(false), 0, 0, | 68 |
66 paint); | 69 SkCanvas pdf_canvas(GetOwningDevice()); |
70 pdf_canvas.drawSprite(raster_surface_->accessBitmap(false), 0, 0, &paint); | |
71 | |
67 // BitmapPlatformDevice matches begin and end calls. | 72 // BitmapPlatformDevice matches begin and end calls. |
68 raster_surface_->EndPlatformPaint(); | 73 skia::EndPlatformPaint(raster_canvas_.get()); |
74 raster_canvas_ = NULL; | |
69 raster_surface_ = NULL; | 75 raster_surface_ = NULL; |
70 } | 76 } |
71 | 77 |
72 uint32_t VectorPlatformDeviceSkia::getDeviceCapabilities() { | |
73 return SkDevice::getDeviceCapabilities() | kVector_Capability; | |
74 } | |
75 | |
76 int VectorPlatformDeviceSkia::width() const { | |
77 return pdf_device_->width(); | |
78 } | |
79 | |
80 int VectorPlatformDeviceSkia::height() const { | |
81 return pdf_device_->height(); | |
82 } | |
83 | |
84 void VectorPlatformDeviceSkia::setMatrixClip(const SkMatrix& matrix, | |
85 const SkRegion& region, | |
86 const SkClipStack& stack) { | |
87 pdf_device_->setMatrixClip(matrix, region, stack); | |
88 } | |
89 | |
90 bool VectorPlatformDeviceSkia::readPixels(const SkIRect& srcRect, | |
91 SkBitmap* bitmap) { | |
92 return false; | |
93 } | |
94 | |
95 void VectorPlatformDeviceSkia::drawPaint(const SkDraw& draw, | |
96 const SkPaint& paint) { | |
97 pdf_device_->drawPaint(draw, paint); | |
98 } | |
99 | |
100 void VectorPlatformDeviceSkia::drawPoints(const SkDraw& draw, | |
101 SkCanvas::PointMode mode, | |
102 size_t count, const SkPoint pts[], | |
103 const SkPaint& paint) { | |
104 pdf_device_->drawPoints(draw, mode, count, pts, paint); | |
105 } | |
106 | |
107 void VectorPlatformDeviceSkia::drawRect(const SkDraw& draw, | |
108 const SkRect& rect, | |
109 const SkPaint& paint) { | |
110 pdf_device_->drawRect(draw, rect, paint); | |
111 } | |
112 | |
113 void VectorPlatformDeviceSkia::drawPath(const SkDraw& draw, | |
114 const SkPath& path, | |
115 const SkPaint& paint, | |
116 const SkMatrix* prePathMatrix, | |
117 bool pathIsMutable) { | |
118 pdf_device_->drawPath(draw, path, paint, prePathMatrix, pathIsMutable); | |
119 } | |
120 | |
121 void VectorPlatformDeviceSkia::drawBitmap(const SkDraw& draw, | |
122 const SkBitmap& bitmap, | |
123 const SkIRect* srcRectOrNull, | |
124 const SkMatrix& matrix, | |
125 const SkPaint& paint) { | |
126 pdf_device_->drawBitmap(draw, bitmap, srcRectOrNull, matrix, paint); | |
127 } | |
128 | |
129 void VectorPlatformDeviceSkia::drawSprite(const SkDraw& draw, | |
130 const SkBitmap& bitmap, | |
131 int x, int y, | |
132 const SkPaint& paint) { | |
133 pdf_device_->drawSprite(draw, bitmap, x, y, paint); | |
134 } | |
135 | |
136 void VectorPlatformDeviceSkia::drawText(const SkDraw& draw, | |
137 const void* text, | |
138 size_t byteLength, | |
139 SkScalar x, | |
140 SkScalar y, | |
141 const SkPaint& paint) { | |
142 pdf_device_->drawText(draw, text, byteLength, x, y, paint); | |
143 } | |
144 | |
145 void VectorPlatformDeviceSkia::drawPosText(const SkDraw& draw, | |
146 const void* text, | |
147 size_t len, | |
148 const SkScalar pos[], | |
149 SkScalar constY, | |
150 int scalarsPerPos, | |
151 const SkPaint& paint) { | |
152 pdf_device_->drawPosText(draw, text, len, pos, constY, scalarsPerPos, paint); | |
153 } | |
154 | |
155 void VectorPlatformDeviceSkia::drawTextOnPath(const SkDraw& draw, | |
156 const void* text, | |
157 size_t len, | |
158 const SkPath& path, | |
159 const SkMatrix* matrix, | |
160 const SkPaint& paint) { | |
161 pdf_device_->drawTextOnPath(draw, text, len, path, matrix, paint); | |
162 } | |
163 | |
164 void VectorPlatformDeviceSkia::drawVertices(const SkDraw& draw, | |
165 SkCanvas::VertexMode vmode, | |
166 int vertexCount, | |
167 const SkPoint vertices[], | |
168 const SkPoint texs[], | |
169 const SkColor colors[], | |
170 SkXfermode* xmode, | |
171 const uint16_t indices[], | |
172 int indexCount, | |
173 const SkPaint& paint) { | |
174 pdf_device_->drawVertices(draw, vmode, vertexCount, vertices, texs, colors, | |
175 xmode, indices, indexCount, paint); | |
176 } | |
177 | |
178 void VectorPlatformDeviceSkia::drawDevice(const SkDraw& draw, | |
179 SkDevice* device, | |
180 int x, | |
181 int y, | |
182 const SkPaint& paint) { | |
183 SkDevice* real_device = device; | |
184 if ((device->getDeviceCapabilities() & kVector_Capability)) { | |
185 // Assume that a vectorial device means a VectorPlatformDeviceSkia, we need | |
186 // to unwrap the embedded SkPDFDevice. | |
187 VectorPlatformDeviceSkia* vector_device = | |
188 static_cast<VectorPlatformDeviceSkia*>(device); | |
189 vector_device->pdf_device_->setOrigin(vector_device->getOrigin().fX, | |
190 vector_device->getOrigin().fY); | |
191 real_device = vector_device->pdf_device_.get(); | |
192 } | |
193 pdf_device_->drawDevice(draw, real_device, x, y, paint); | |
194 } | |
195 | |
196 void VectorPlatformDeviceSkia::setDrawingArea(SkPDFDevice::DrawingArea area) { | |
197 pdf_device_->setDrawingArea(area); | |
198 } | |
199 | |
200 #if defined(OS_WIN) | 78 #if defined(OS_WIN) |
201 void VectorPlatformDeviceSkia::DrawToNativeContext(HDC dc, | 79 void VectorPlatformDeviceSkia::DrawToNativeContext(HDC dc, |
202 int x, | 80 int x, |
203 int y, | 81 int y, |
204 const RECT* src_rect) { | 82 const RECT* src_rect) { |
205 SkASSERT(false); | 83 SkASSERT(false); |
206 } | 84 } |
207 #elif defined(OS_MACOSX) | 85 #elif defined(OS_MACOSX) |
208 void VectorPlatformDeviceSkia::DrawToNativeContext(CGContext* context, int x, | 86 void VectorPlatformDeviceSkia::DrawToNativeContext(CGContext* context, int x, |
209 int y, const CGRect* src_rect) { | 87 int y, const CGRect* src_rect) { |
210 SkASSERT(false); | 88 SkASSERT(false); |
211 } | 89 } |
212 | 90 |
213 CGContextRef VectorPlatformDeviceSkia::GetBitmapContext() { | 91 CGContextRef VectorPlatformDeviceSkia::GetBitmapContext() { |
214 SkASSERT(false); | 92 SkASSERT(false); |
215 return NULL; | 93 return NULL; |
216 } | 94 } |
217 | 95 |
218 #endif | 96 #endif |
219 | 97 |
220 SkDevice* VectorPlatformDeviceSkia::onCreateCompatibleDevice( | |
221 SkBitmap::Config config, int width, int height, bool isOpaque, | |
222 Usage /*usage*/) { | |
223 SkAutoTUnref<SkDevice> dev(pdf_device_->createCompatibleDevice(config, width, | |
224 height, | |
225 isOpaque)); | |
226 return new VectorPlatformDeviceSkia(static_cast<SkPDFDevice*>(dev.get())); | |
227 } | |
228 | |
229 } // namespace skia | 98 } // namespace skia |
OLD | NEW |