| 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 "third_party/skia/include/core/SkClipStack.h" | 8 #include "third_party/skia/include/core/SkClipStack.h" |
| 9 #include "third_party/skia/include/core/SkDraw.h" | 9 #include "third_party/skia/include/core/SkDraw.h" |
| 10 #include "third_party/skia/include/core/SkRect.h" | 10 #include "third_party/skia/include/core/SkRect.h" |
| 11 #include "third_party/skia/include/core/SkRegion.h" | 11 #include "third_party/skia/include/core/SkRegion.h" |
| 12 #include "third_party/skia/include/core/SkScalar.h" | 12 #include "third_party/skia/include/core/SkScalar.h" |
| 13 | 13 |
| 14 namespace skia { | 14 namespace skia { |
| 15 | 15 |
| 16 static inline SkBitmap makeABitmap(int width, int height) { | 16 static inline SkBitmap makeABitmap(int width, int height) { |
| 17 SkBitmap bitmap; | 17 SkBitmap bitmap; |
| 18 bitmap.setConfig(SkBitmap::kNo_Config, width, height); | 18 bitmap.setConfig(SkBitmap::kNo_Config, width, height); |
| 19 return bitmap; | 19 return bitmap; |
| 20 } | 20 } |
| 21 | 21 |
| 22 VectorPlatformDeviceSkia::VectorPlatformDeviceSkia( | 22 VectorPlatformDeviceSkia::VectorPlatformDeviceSkia(SkPDFDevice* pdf_device) |
| 23 const SkISize& pageSize, | 23 : PlatformDevice(makeABitmap(pdf_device->width(), pdf_device->height())), |
| 24 const SkISize& contentSize, | 24 pdf_device_(pdf_device) { |
| 25 const SkMatrix& initialTransform) | |
| 26 : SkPDFDevice(pageSize, contentSize, initialTransform) { | |
| 27 SetPlatformDevice(this, this); | |
| 28 } | 25 } |
| 29 | 26 |
| 30 VectorPlatformDeviceSkia::~VectorPlatformDeviceSkia() { | 27 VectorPlatformDeviceSkia::~VectorPlatformDeviceSkia() { |
| 31 } | 28 } |
| 32 | 29 |
| 33 bool VectorPlatformDeviceSkia::IsNativeFontRenderingAllowed() { | 30 bool VectorPlatformDeviceSkia::IsNativeFontRenderingAllowed() { |
| 34 return false; | 31 return false; |
| 35 } | 32 } |
| 36 | 33 |
| 37 PlatformSurface VectorPlatformDeviceSkia::BeginPlatformPaint() { | 34 PlatformDevice::PlatformSurface VectorPlatformDeviceSkia::BeginPlatformPaint() { |
| 38 // Even when drawing a vector representation of the page, we have to | 35 // Even when drawing a vector representation of the page, we have to |
| 39 // provide a raster surface for plugins to render into - they don't have | 36 // provide a raster surface for plugins to render into - they don't have |
| 40 // a vector interface. Therefore we create a BitmapPlatformDevice here | 37 // a vector interface. Therefore we create a BitmapPlatformDevice here |
| 41 // and return the context from it, then layer on the raster data as an | 38 // and return the context from it, then layer on the raster data as an |
| 42 // image in EndPlatformPaint. | 39 // image in EndPlatformPaint. |
| 43 DCHECK(raster_surface_ == NULL); | 40 DCHECK(raster_surface_ == NULL); |
| 44 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
| 45 raster_surface_ = BitmapPlatformDevice::create(width(), | 42 raster_surface_ = BitmapPlatformDevice::create(pdf_device_->width(), |
| 46 height(), | 43 pdf_device_->height(), |
| 47 false, /* not opaque */ | 44 false, /* not opaque */ |
| 48 NULL); | 45 NULL); |
| 49 #elif defined(OS_POSIX) && !defined(OS_MACOSX) | 46 #elif defined(OS_POSIX) && !defined(OS_MACOSX) |
| 50 raster_surface_ = BitmapPlatformDevice::Create(width(), | 47 raster_surface_ = BitmapPlatformDevice::Create(pdf_device_->width(), |
| 51 height(), | 48 pdf_device_->height(), |
| 52 false /* not opaque */); | 49 false /* not opaque */); |
| 53 #endif | 50 #endif |
| 54 raster_surface_->unref(); // SkRefPtr and create both took a reference. | 51 raster_surface_->unref(); // SkRefPtr and create both took a reference. |
| 55 | 52 |
| 56 SkCanvas canvas(raster_surface_.get()); | 53 SkCanvas canvas(raster_surface_.get()); |
| 57 return raster_surface_->BeginPlatformPaint(); | 54 return raster_surface_->BeginPlatformPaint(); |
| 58 } | 55 } |
| 59 | 56 |
| 60 void VectorPlatformDeviceSkia::EndPlatformPaint() { | 57 void VectorPlatformDeviceSkia::EndPlatformPaint() { |
| 61 DCHECK(raster_surface_ != NULL); | 58 DCHECK(raster_surface_ != NULL); |
| 62 SkPaint paint; | 59 SkPaint paint; |
| 63 // SkPDFDevice checks the passed SkDraw for an empty clip (only). Fake | 60 // SkPDFDevice checks the passed SkDraw for an empty clip (only). Fake |
| 64 // it out by setting a non-empty clip. | 61 // it out by setting a non-empty clip. |
| 65 SkDraw draw; | 62 SkDraw draw; |
| 66 SkRegion clip(SkIRect::MakeWH(width(), height())); | 63 SkRegion clip(SkIRect::MakeWH(pdf_device_->width(), pdf_device_->height())); |
| 67 draw.fClip=&clip; | 64 draw.fClip=&clip; |
| 68 drawSprite(draw, raster_surface_->accessBitmap(false), 0, 0, paint); | 65 pdf_device_->drawSprite(draw, raster_surface_->accessBitmap(false), 0, 0, |
| 66 paint); |
| 69 // BitmapPlatformDevice matches begin and end calls. | 67 // BitmapPlatformDevice matches begin and end calls. |
| 70 raster_surface_->EndPlatformPaint(); | 68 raster_surface_->EndPlatformPaint(); |
| 71 raster_surface_ = NULL; | 69 raster_surface_ = NULL; |
| 72 } | 70 } |
| 73 | 71 |
| 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 |
| 74 #if defined(OS_WIN) | 200 #if defined(OS_WIN) |
| 75 void VectorPlatformDeviceSkia::DrawToNativeContext(HDC dc, | 201 void VectorPlatformDeviceSkia::DrawToNativeContext(HDC dc, |
| 76 int x, | 202 int x, |
| 77 int y, | 203 int y, |
| 78 const RECT* src_rect) { | 204 const RECT* src_rect) { |
| 79 SkASSERT(false); | 205 SkASSERT(false); |
| 80 } | 206 } |
| 81 #elif defined(OS_MACOSX) | 207 #elif defined(OS_MACOSX) |
| 82 void VectorPlatformDeviceSkia::DrawToNativeContext(CGContext* context, int x, | 208 void VectorPlatformDeviceSkia::DrawToNativeContext(CGContext* context, int x, |
| 83 int y, const CGRect* src_rect) { | 209 int y, const CGRect* src_rect) { |
| 84 SkASSERT(false); | 210 SkASSERT(false); |
| 85 } | 211 } |
| 86 | 212 |
| 87 CGContextRef VectorPlatformDeviceSkia::GetBitmapContext() { | 213 CGContextRef VectorPlatformDeviceSkia::GetBitmapContext() { |
| 88 SkASSERT(false); | 214 SkASSERT(false); |
| 89 return NULL; | 215 return NULL; |
| 90 } | 216 } |
| 91 #elif defined(OS_LINUX) | 217 |
| 92 void VectorPlatformDeviceSkia::DrawToNativeContext( | |
| 93 PlatformSurface surface, int x, int y, const PlatformRect* src_rect) { | |
| 94 // Should never be called on Linux. | |
| 95 SkASSERT(false); | |
| 96 } | |
| 97 #endif | 218 #endif |
| 98 | 219 |
| 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 |
| 99 } // namespace skia | 229 } // namespace skia |
| OLD | NEW |