| 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/platform_canvas.h" | 5 #include "skia/ext/platform_canvas.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "skia/ext/cdl_canvas.h" |
| 10 #include "skia/ext/platform_device.h" | 11 #include "skia/ext/platform_device.h" |
| 11 #include "third_party/skia/include/core/SkMetaData.h" | 12 #include "third_party/skia/include/core/SkMetaData.h" |
| 12 #include "third_party/skia/include/core/SkTypes.h" | 13 #include "third_party/skia/include/core/SkTypes.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 #if defined(OS_MACOSX) | 17 #if defined(OS_MACOSX) |
| 17 const char kIsPreviewMetafileKey[] = "CrIsPreviewMetafile"; | 18 const char kIsPreviewMetafileKey[] = "CrIsPreviewMetafile"; |
| 18 | 19 |
| 19 void SetBoolMetaData(const SkCanvas& canvas, const char* key, bool value) { | 20 void SetBoolMetaData(const CdlCanvas& canvas, const char* key, bool value) { |
| 20 SkMetaData& meta = skia::GetMetaData(canvas); | 21 SkMetaData& meta = skia::GetMetaData(canvas); |
| 21 meta.setBool(key, value); | 22 meta.setBool(key, value); |
| 22 } | 23 } |
| 23 | 24 |
| 24 bool GetBoolMetaData(const SkCanvas& canvas, const char* key) { | 25 bool GetBoolMetaData(const CdlCanvas& canvas, const char* key) { |
| 25 bool value; | 26 bool value; |
| 26 SkMetaData& meta = skia::GetMetaData(canvas); | 27 SkMetaData& meta = skia::GetMetaData(canvas); |
| 27 if (!meta.findBool(key, &value)) | 28 if (!meta.findBool(key, &value)) |
| 28 value = false; | 29 value = false; |
| 29 return value; | 30 return value; |
| 30 } | 31 } |
| 31 #endif | 32 #endif |
| 32 | 33 |
| 33 } // namespace | 34 } // namespace |
| 34 | 35 |
| 35 namespace skia { | 36 namespace skia { |
| 36 | 37 |
| 37 SkBitmap ReadPixels(SkCanvas* canvas) { | 38 SkBitmap ReadPixels(CdlCanvas* canvas) { |
| 38 SkBitmap bitmap; | 39 SkBitmap bitmap; |
| 39 bitmap.setInfo(canvas->imageInfo()); | 40 bitmap.setInfo(GetSkCanvas(canvas)->imageInfo()); |
| 40 canvas->readPixels(&bitmap, 0, 0); | 41 canvas->readPixels(&bitmap, 0, 0); |
| 41 return bitmap; | 42 return bitmap; |
| 42 } | 43 } |
| 43 | 44 |
| 44 bool GetWritablePixels(SkCanvas* canvas, SkPixmap* result) { | 45 bool GetWritablePixels(CdlCanvas* canvas, SkPixmap* result) { |
| 45 if (!canvas || !result) { | 46 if (!canvas || !result) { |
| 46 return false; | 47 return false; |
| 47 } | 48 } |
| 48 | 49 |
| 49 SkImageInfo info; | 50 SkImageInfo info; |
| 50 size_t row_bytes; | 51 size_t row_bytes; |
| 51 void* pixels = canvas->accessTopLayerPixels(&info, &row_bytes); | 52 void* pixels = GetSkCanvas(canvas)->accessTopLayerPixels(&info, &row_bytes); |
| 52 if (!pixels) { | 53 if (!pixels) { |
| 53 result->reset(); | 54 result->reset(); |
| 54 return false; | 55 return false; |
| 55 } | 56 } |
| 56 | 57 |
| 57 result->reset(info, pixels, row_bytes); | 58 result->reset(info, pixels, row_bytes); |
| 58 return true; | 59 return true; |
| 59 } | 60 } |
| 60 | 61 |
| 61 bool SupportsPlatformPaint(const SkCanvas* canvas) { | 62 bool SupportsPlatformPaint(const CdlCanvas* canvas) { |
| 62 return GetPlatformDevice(canvas->getTopDevice(true)) != nullptr; | 63 return GetPlatformDevice(GetSkCanvas(canvas)->getTopDevice(true)) != nullptr; |
| 63 } | 64 } |
| 64 | 65 |
| 65 size_t PlatformCanvasStrideForWidth(unsigned width) { | 66 size_t PlatformCanvasStrideForWidth(unsigned width) { |
| 66 return 4 * width; | 67 return 4 * width; |
| 67 } | 68 } |
| 68 | 69 |
| 69 std::unique_ptr<SkCanvas> CreateCanvas(const sk_sp<SkBaseDevice>& device, | 70 std::unique_ptr<CdlCanvas> CreateCanvas(const sk_sp<SkBaseDevice>& device, |
| 70 OnFailureType failureType) { | 71 OnFailureType failureType) { |
| 71 if (!device) { | 72 if (!device) { |
| 72 if (CRASH_ON_FAILURE == failureType) | 73 if (CRASH_ON_FAILURE == failureType) |
| 73 SK_CRASH(); | 74 SK_CRASH(); |
| 74 return nullptr; | 75 return nullptr; |
| 75 } | 76 } |
| 76 return base::MakeUnique<SkCanvas>(device.get()); | 77 return base::MakeUnique<CdlCanvas>(device.get()); |
| 77 } | 78 } |
| 78 | 79 |
| 79 SkMetaData& GetMetaData(const SkCanvas& canvas) { | 80 SkMetaData& GetMetaData(const CdlCanvas& canvas) { |
| 80 SkBaseDevice* device = canvas.getDevice(); | 81 SkBaseDevice* device = GetSkCanvas(&canvas)->getDevice(); |
| 81 DCHECK(device != nullptr); | 82 DCHECK(device != nullptr); |
| 82 return device->getMetaData(); | 83 return device->getMetaData(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 #if defined(OS_MACOSX) | 86 #if defined(OS_MACOSX) |
| 86 void SetIsPreviewMetafile(const SkCanvas& canvas, bool is_preview) { | 87 void SetIsPreviewMetafile(const CdlCanvas& canvas, bool is_preview) { |
| 87 SetBoolMetaData(canvas, kIsPreviewMetafileKey, is_preview); | 88 SetBoolMetaData(canvas, kIsPreviewMetafileKey, is_preview); |
| 88 } | 89 } |
| 89 | 90 |
| 90 bool IsPreviewMetafile(const SkCanvas& canvas) { | 91 bool IsPreviewMetafile(const CdlCanvas& canvas) { |
| 91 return GetBoolMetaData(canvas, kIsPreviewMetafileKey); | 92 return GetBoolMetaData(canvas, kIsPreviewMetafileKey); |
| 92 } | 93 } |
| 93 | 94 |
| 94 CGContextRef GetBitmapContext(const SkCanvas& canvas) { | 95 CGContextRef GetBitmapContext(const CdlCanvas& canvas) { |
| 95 PlatformDevice* platform_device = | 96 PlatformDevice* platform_device = |
| 96 GetPlatformDevice(canvas.getTopDevice(true)); | 97 GetPlatformDevice(GetSkCanvas(&canvas)->getTopDevice(true)); |
| 97 SkIRect clip_bounds; | 98 SkIRect clip_bounds; |
| 98 canvas.getClipDeviceBounds(&clip_bounds); | 99 canvas.getClipDeviceBounds(&clip_bounds); |
| 99 return platform_device ? | 100 return platform_device ? |
| 100 platform_device->GetBitmapContext( | 101 platform_device->GetBitmapContext( |
| 101 canvas.getTotalMatrix(), clip_bounds) : | 102 canvas.getTotalMatrix(), clip_bounds) : |
| 102 nullptr; | 103 nullptr; |
| 103 } | 104 } |
| 104 | 105 |
| 105 #endif | 106 #endif |
| 106 | 107 |
| 107 ScopedPlatformPaint::ScopedPlatformPaint(SkCanvas* canvas) : | 108 ScopedPlatformPaint::ScopedPlatformPaint(CdlCanvas* canvas) |
| 108 canvas_(canvas), | 109 : canvas_(canvas), native_drawing_context_(nullptr) { |
| 109 native_drawing_context_(nullptr) { | |
| 110 // TODO(tomhudson) we're assuming non-null canvas? | 110 // TODO(tomhudson) we're assuming non-null canvas? |
| 111 PlatformDevice* platform_device = GetPlatformDevice(canvas->getTopDevice(true)
); | 111 PlatformDevice* platform_device = |
| 112 GetPlatformDevice(GetSkCanvas(canvas)->getTopDevice(true)); |
| 112 if (platform_device) { | 113 if (platform_device) { |
| 113 // Compensate for drawing to a layer rather than the entire canvas | 114 // Compensate for drawing to a layer rather than the entire canvas |
| 114 SkMatrix ctm; | 115 SkMatrix ctm; |
| 115 SkIRect clip_bounds; | 116 SkIRect clip_bounds; |
| 116 canvas->temporary_internal_describeTopLayer(&ctm, &clip_bounds); | 117 GetSkCanvas(canvas)->temporary_internal_describeTopLayer(&ctm, |
| 118 &clip_bounds); |
| 117 native_drawing_context_ = platform_device->BeginPlatformPaint(ctm, clip_boun
ds); | 119 native_drawing_context_ = platform_device->BeginPlatformPaint(ctm, clip_boun
ds); |
| 118 } | 120 } |
| 119 } | 121 } |
| 120 | 122 |
| 121 } // namespace skia | 123 } // namespace skia |
| OLD | NEW |