OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "build/build_config.h" |
| 6 #include "skia/ext/layer_allocator_cairo.h" |
| 7 #include "third_party/skia/include/core/SkMatrix.h" |
| 8 #include "third_party/skia/include/core/SkRect.h" |
| 9 |
| 10 #if defined(OS_OPENBSD) |
| 11 #include <cairo.h> |
| 12 #else |
| 13 #include <cairo/cairo.h> |
| 14 #endif |
| 15 |
| 16 typedef struct _cairo_surface cairo_surface_t; |
| 17 |
| 18 namespace { |
| 19 |
| 20 void LoadMatrixToContext(cairo_t* context, const SkMatrix& matrix) { |
| 21 cairo_matrix_t cairo_matrix; |
| 22 cairo_matrix_init(&cairo_matrix, |
| 23 SkScalarToFloat(matrix.getScaleX()), |
| 24 SkScalarToFloat(matrix.getSkewY()), |
| 25 SkScalarToFloat(matrix.getSkewX()), |
| 26 SkScalarToFloat(matrix.getScaleY()), |
| 27 SkScalarToFloat(matrix.getTranslateX()), |
| 28 SkScalarToFloat(matrix.getTranslateY())); |
| 29 cairo_set_matrix(context, &cairo_matrix); |
| 30 } |
| 31 |
| 32 void LoadClipToContext(cairo_t* context, const SkIRect& clip_bounds) { |
| 33 cairo_reset_clip(context); |
| 34 |
| 35 cairo_rectangle(context, clip_bounds.fLeft, clip_bounds.fTop, |
| 36 clip_bounds.width(), clip_bounds.height()); |
| 37 cairo_clip(context); |
| 38 } |
| 39 |
| 40 void FreeCairoBacking(void* context) { |
| 41 if (!context) |
| 42 return; |
| 43 cairo_surface_destroy(cairo_get_target((cairo_t*) context)); |
| 44 cairo_destroy((cairo_t*) context); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 namespace skia { |
| 50 |
| 51 LayerAllocator::LayerAllocator() { } |
| 52 |
| 53 LayerAllocator::~LayerAllocator() { } |
| 54 |
| 55 void* LayerAllocator::getNativeContext(void* buffer, |
| 56 const SkMatrix& transform, |
| 57 const SkIRect& clip_bounds) { |
| 58 if (!buffer) |
| 59 return nullptr; |
| 60 |
| 61 context_map_t::iterator context_iterator = contexts_.find(buffer); |
| 62 if (context_iterator == contexts_.end()) |
| 63 return nullptr; |
| 64 |
| 65 LoadClipToContext(context_iterator->second, clip_bounds); |
| 66 LoadMatrixToContext(context_iterator->second, transform); |
| 67 cairo_surface_t* surface = cairo_get_target(context_iterator->second); |
| 68 cairo_surface_flush(surface); |
| 69 cairo_surface_mark_dirty(surface); |
| 70 return context_iterator->second; |
| 71 } |
| 72 |
| 73 void* LayerAllocator::allocateLayer(const SkImageInfo& info, |
| 74 size_t* rowBytes, |
| 75 void (**deallocator)(void*), |
| 76 void** deallocatorPayload, |
| 77 void* initialData) { |
| 78 SkASSERT(info.colorType() == kN32_SkColorType); |
| 79 |
| 80 cairo_surface_t* surface; |
| 81 if (initialData) |
| 82 surface = cairo_image_surface_create_for_data( |
| 83 (unsigned char*) initialData, |
| 84 CAIRO_FORMAT_ARGB32, |
| 85 info.width(), |
| 86 info.height(), |
| 87 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, info.width())); |
| 88 else |
| 89 surface = cairo_image_surface_create( |
| 90 CAIRO_FORMAT_ARGB32, info.width(), info.height()); |
| 91 if (!surface) |
| 92 return nullptr; |
| 93 |
| 94 if (rowBytes) |
| 95 *rowBytes = cairo_image_surface_get_stride(surface); |
| 96 |
| 97 cairo_t* context = cairo_create(surface); |
| 98 if (!context) { |
| 99 cairo_surface_destroy(surface); |
| 100 return nullptr; |
| 101 } |
| 102 void* buffer = cairo_image_surface_get_data(surface); |
| 103 contexts_.insert({buffer, context}); |
| 104 |
| 105 *deallocator = FreeCairoBacking; |
| 106 *deallocatorPayload = context; |
| 107 |
| 108 return buffer; |
| 109 } |
| 110 |
| 111 } // namespace skia |
| 112 |
OLD | NEW |