Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "skia/ext/vector_platform_device_skia.h" | |
| 6 | |
| 7 #include "skia/ext/bitmap_platform_device.h" | |
| 8 #include "third_party/skia/include/core/SkClipStack.h" | |
| 9 #include "third_party/skia/include/core/SkDraw.h" | |
| 10 #include "third_party/skia/include/core/SkScalar.h" | |
| 11 #include "third_party/skia/include/core/SkRegion.h" | |
|
Lei Zhang
2011/04/06 20:43:56
nit: SkRegion before SKScalar.
vandebo (ex-Chrome)
2011/04/06 22:01:37
Done.
| |
| 12 | |
| 13 namespace skia { | |
| 14 | |
| 15 SkDevice* VectorPlatformDeviceSkiaFactory::newDevice(SkCanvas* noUsed, | |
| 16 SkBitmap::Config config, | |
| 17 int width, int height, | |
| 18 bool isOpaque, | |
| 19 bool isForLayer) { | |
| 20 SkASSERT(config == SkBitmap::kARGB_8888_Config); | |
| 21 SkPDFDevice::OriginTransform flip = SkPDFDevice::kFlip_OriginTransform; | |
| 22 if (isForLayer) | |
| 23 flip = SkPDFDevice::kNoFlip_OriginTransform; | |
| 24 return new VectorPlatformDeviceSkia(width, height, flip); | |
| 25 } | |
| 26 | |
| 27 static inline SkBitmap makeABitmap(int width, int height) { | |
| 28 SkBitmap bitmap; | |
| 29 bitmap.setConfig(SkBitmap::kNo_Config, width, height); | |
| 30 return bitmap; | |
| 31 } | |
| 32 | |
| 33 VectorPlatformDeviceSkia::VectorPlatformDeviceSkia( | |
| 34 int width, int height, SkPDFDevice::OriginTransform flip) | |
| 35 : PlatformDevice(makeABitmap(width, height)), | |
| 36 pdf_device_(new SkPDFDevice(width, height, flip)) { | |
| 37 pdf_device_->unref(); // SkRefPtr and new both took a reference. | |
| 38 base_transform_.reset(); | |
| 39 } | |
| 40 | |
| 41 VectorPlatformDeviceSkia::~VectorPlatformDeviceSkia() { | |
| 42 } | |
| 43 | |
| 44 bool VectorPlatformDeviceSkia::IsVectorial() { | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 bool VectorPlatformDeviceSkia::IsNativeFontRenderingAllowed() { | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 PlatformDevice::PlatformSurface VectorPlatformDeviceSkia::beginPlatformPaint() { | |
| 53 // Even when drawing a vector representation of the page, we have to | |
| 54 // provide a raster surface for plugins to render into - they don't have | |
| 55 // a vector interface. Therefore we create a BitmapPlatformDevice here | |
| 56 // and return the context from it, then layer on the raster data as an | |
| 57 // image in endPlatformPaint. | |
| 58 DCHECK(raster_surface_ == NULL); | |
| 59 #if defined(OS_WIN) | |
| 60 raster_surface_ = BitmapPlatformDevice::create(pdf_device_->width(), | |
| 61 pdf_device_->height(), | |
| 62 false, /* not opaque */ | |
| 63 NULL); | |
| 64 #elif defined(OS_LINUX) | |
| 65 raster_surface_ = BitmapPlatformDevice::Create(pdf_device_->width(), | |
| 66 pdf_device_->height(), | |
| 67 false /* not opaque */); | |
| 68 #endif | |
| 69 raster_surface_->unref(); // SkRefPtr and create both took a reference. | |
| 70 | |
| 71 // Set it to be transparent. | |
| 72 SkCanvas canvas(raster_surface_.get()); | |
| 73 SkPaint transparent; | |
| 74 transparent.setColor(SK_ColorBLACK); | |
| 75 canvas.drawPaint(transparent); | |
| 76 return raster_surface_->beginPlatformPaint(); | |
| 77 } | |
| 78 | |
| 79 void VectorPlatformDeviceSkia::endPlatformPaint() { | |
| 80 DCHECK(raster_surface_ != NULL); | |
| 81 SkPaint paint; | |
| 82 pdf_device_->drawSprite(SkDraw(), | |
| 83 raster_surface_->accessBitmap(false), | |
| 84 base_transform_.getTranslateX(), | |
| 85 base_transform_.getTranslateY(), | |
| 86 paint); | |
| 87 raster_surface_ = NULL; | |
| 88 } | |
| 89 | |
| 90 SkDeviceFactory* VectorPlatformDeviceSkia::getDeviceFactory() { | |
| 91 return SkNEW(VectorPlatformDeviceSkiaFactory); | |
| 92 } | |
| 93 | |
| 94 uint32_t VectorPlatformDeviceSkia::getDeviceCapabilities() { | |
| 95 return kVector_Capability; | |
| 96 } | |
| 97 | |
| 98 int VectorPlatformDeviceSkia::width() const { | |
| 99 return pdf_device_->width(); | |
| 100 } | |
| 101 | |
| 102 int VectorPlatformDeviceSkia::height() const { | |
| 103 return pdf_device_->height(); | |
| 104 } | |
| 105 | |
| 106 void VectorPlatformDeviceSkia::setMatrixClip(const SkMatrix& matrix, | |
| 107 const SkRegion& region, | |
| 108 const SkClipStack& stack) { | |
| 109 SkMatrix transform = base_transform_; | |
| 110 transform.preConcat(matrix); | |
| 111 | |
| 112 DCHECK(SkMatrix::kTranslate_Mask == base_transform_.getType() || | |
| 113 SkMatrix::kIdentity_Mask == base_transform_.getType()); | |
| 114 SkRegion clip = region; | |
| 115 clip.translate(base_transform_.getTranslateX(), | |
| 116 base_transform_.getTranslateY()); | |
| 117 | |
| 118 pdf_device_->setMatrixClip(transform, clip, stack); | |
| 119 } | |
| 120 | |
| 121 bool VectorPlatformDeviceSkia::readPixels(const SkIRect& srcRect, | |
| 122 SkBitmap* bitmap) { | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 void VectorPlatformDeviceSkia::drawPaint(const SkDraw& draw, | |
| 127 const SkPaint& paint) { | |
| 128 pdf_device_->drawPaint(draw, paint); | |
| 129 } | |
| 130 | |
| 131 void VectorPlatformDeviceSkia::drawPoints(const SkDraw& draw, | |
| 132 SkCanvas::PointMode mode, | |
| 133 size_t count, const SkPoint pts[], | |
| 134 const SkPaint& paint) { | |
| 135 pdf_device_->drawPoints(draw, mode, count, pts, paint); | |
| 136 } | |
| 137 | |
| 138 void VectorPlatformDeviceSkia::drawRect(const SkDraw& draw, | |
| 139 const SkRect& rect, | |
| 140 const SkPaint& paint) { | |
| 141 pdf_device_->drawRect(draw, rect, paint); | |
| 142 } | |
| 143 | |
| 144 void VectorPlatformDeviceSkia::drawPath(const SkDraw& draw, | |
| 145 const SkPath& path, | |
| 146 const SkPaint& paint, | |
| 147 const SkMatrix* prePathMatrix, | |
| 148 bool pathIsMutable) { | |
| 149 pdf_device_->drawPath(draw, path, paint, prePathMatrix, pathIsMutable); | |
| 150 } | |
| 151 | |
| 152 void VectorPlatformDeviceSkia::drawBitmap(const SkDraw& draw, | |
| 153 const SkBitmap& bitmap, | |
| 154 const SkIRect* srcRectOrNull, | |
| 155 const SkMatrix& matrix, | |
| 156 const SkPaint& paint) { | |
| 157 pdf_device_->drawBitmap(draw, bitmap, srcRectOrNull, matrix, paint); | |
| 158 } | |
| 159 | |
| 160 void VectorPlatformDeviceSkia::drawSprite(const SkDraw& draw, | |
| 161 const SkBitmap& bitmap, | |
| 162 int x, int y, | |
| 163 const SkPaint& paint) { | |
| 164 pdf_device_->drawSprite(draw, bitmap, x, y, paint); | |
| 165 } | |
| 166 | |
| 167 void VectorPlatformDeviceSkia::drawText(const SkDraw& draw, | |
| 168 const void* text, | |
| 169 size_t byteLength, | |
| 170 SkScalar x, | |
| 171 SkScalar y, | |
| 172 const SkPaint& paint) { | |
| 173 pdf_device_->drawText(draw, text, byteLength, x, y, paint); | |
| 174 } | |
| 175 | |
| 176 void VectorPlatformDeviceSkia::drawPosText(const SkDraw& draw, | |
| 177 const void* text, | |
| 178 size_t len, | |
| 179 const SkScalar pos[], | |
| 180 SkScalar constY, | |
| 181 int scalarsPerPos, | |
| 182 const SkPaint& paint) { | |
| 183 pdf_device_->drawPosText(draw, text, len, pos, constY, scalarsPerPos, paint); | |
| 184 } | |
| 185 | |
| 186 void VectorPlatformDeviceSkia::drawTextOnPath(const SkDraw& draw, | |
| 187 const void* text, | |
| 188 size_t len, | |
| 189 const SkPath& path, | |
| 190 const SkMatrix* matrix, | |
| 191 const SkPaint& paint) { | |
| 192 pdf_device_->drawTextOnPath(draw, text, len, path, matrix, paint); | |
| 193 } | |
| 194 | |
| 195 void VectorPlatformDeviceSkia::drawVertices(const SkDraw& draw, | |
| 196 SkCanvas::VertexMode vmode, | |
| 197 int vertexCount, | |
| 198 const SkPoint vertices[], | |
| 199 const SkPoint texs[], | |
| 200 const SkColor colors[], | |
| 201 SkXfermode* xmode, | |
| 202 const uint16_t indices[], | |
| 203 int indexCount, | |
| 204 const SkPaint& paint) { | |
| 205 pdf_device_->drawVertices(draw, vmode, vertexCount, vertices, texs, colors, | |
| 206 xmode, indices, indexCount, paint); | |
| 207 } | |
| 208 | |
| 209 void VectorPlatformDeviceSkia::drawDevice(const SkDraw& draw, | |
| 210 SkDevice* device, | |
| 211 int x, | |
| 212 int y, | |
| 213 const SkPaint& paint) { | |
| 214 SkDevice* real_device = device; | |
| 215 if ((device->getDeviceCapabilities() & kVector_Capability)) { | |
| 216 // Assume that a vectorial device means a VectorPlatformDeviceSkia, we need | |
| 217 // to unwrap the embedded SkPDFDevice. | |
| 218 VectorPlatformDeviceSkia* vector_device = | |
| 219 static_cast<VectorPlatformDeviceSkia*>(device); | |
| 220 real_device = vector_device->pdf_device_.get(); | |
| 221 } | |
| 222 pdf_device_->drawDevice(draw, real_device, x, y, paint); | |
| 223 } | |
| 224 | |
| 225 #if defined(OS_WIN) | |
| 226 void VectorPlatformDeviceSkia::drawToHDC(HDC dc, | |
| 227 int x, | |
| 228 int y, | |
| 229 const RECT* src_rect) { | |
| 230 SkASSERT(false); | |
| 231 } | |
| 232 #endif | |
| 233 | |
| 234 void VectorPlatformDeviceSkia::setInitialTransform(int xOffset, int yOffset, | |
| 235 float scale_factor) { | |
| 236 // TODO(vandebo) Supporting a scale factor is some work because we have to | |
| 237 // transform both matrices and clips that come in, but Region only supports | |
| 238 // translation. Instead, we could change SkPDFDevice to include it in the | |
| 239 // initial transform. Delay that work until we would use it. Also checked | |
| 240 // in setMatrixClip. | |
| 241 DCHECK_EQ(1.0f, scale_factor); | |
| 242 | |
| 243 base_transform_.setTranslate(xOffset, yOffset); | |
| 244 SkScalar scale = SkFloatToScalar(scale_factor); | |
| 245 base_transform_.postScale(scale, scale); | |
| 246 | |
| 247 SkMatrix matrix; | |
| 248 matrix.reset(); | |
| 249 SkRegion region; | |
| 250 SkClipStack stack; | |
| 251 setMatrixClip(matrix, region, stack); | |
| 252 } | |
| 253 | |
| 254 } // namespace skia | |
| OLD | NEW |