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 3 // found in the LICENSE file. 4 5 #include "skia/ext/platform_canvas.h" 6 7 #include "skia/ext/bitmap_platform_device.h" 8 #include "third_party/skia/include/core/SkTypes.h" 9 10 namespace skia { 11 12 SkBaseDevice* GetTopDevice(const SkCanvas& canvas) { 13 return canvas.getTopDevice(true); 14 } 15 16 bool SupportsPlatformPaint(const SkCanvas* canvas) { 17 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas)); 18 return platform_device && platform_device->SupportsPlatformPaint(); 19 } 20 21 PlatformSurface BeginPlatformPaint(SkCanvas* canvas) { 22 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas)); 23 if (platform_device) 24 return platform_device->BeginPlatformPaint(); 25 26 return 0; 27 } 28 29 void EndPlatformPaint(SkCanvas* canvas) { 30 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas)); 31 if (platform_device) 32 platform_device->EndPlatformPaint(); 33 } 34 35 void DrawToNativeContext(SkCanvas* canvas, PlatformSurface context, int x, 36 int y, const PlatformRect* src_rect) { 37 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas)); 38 if (platform_device) 39 platform_device->DrawToNativeContext(context, x, y, src_rect); 40 } 41 42 static SkPMColor MakeOpaqueXfermodeProc(SkPMColor src, SkPMColor dst) { 43 return dst | (0xFF << SK_A32_SHIFT); 44 } 45 46 void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height) { 47 if (width <= 0 || height <= 0) 48 return; 49 50 SkRect rect; 51 rect.setXYWH(SkIntToScalar(x), SkIntToScalar(y), 52 SkIntToScalar(width), SkIntToScalar(height)); 53 SkPaint paint; 54 // so we don't draw anything on a device that ignores xfermodes 55 paint.setColor(0); 56 // install our custom mode 57 skia::RefPtr<SkProcXfermode> xfermode = 58 skia::AdoptRef(new SkProcXfermode(MakeOpaqueXfermodeProc)); 59 paint.setXfermode(xfermode.get()); 60 canvas->drawRect(rect, paint); 61 } 62 63 size_t PlatformCanvasStrideForWidth(unsigned width) { 64 return 4 * width; 65 } 66 67 SkCanvas* CreateCanvas(const skia::RefPtr<SkBaseDevice>& device, OnFailureType failureType) { 68 if (!device) { 69 if (CRASH_ON_FAILURE == failureType) 70 SK_CRASH(); 71 return NULL; 72 } 73 return new SkCanvas(device.get()); 74 } 75 76 PlatformBitmap::PlatformBitmap() : surface_(0), platform_extra_(0) {} 77 78 } // namespace skia 79