Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2008, The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * * Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * * Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * |
michael@0 | 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
michael@0 | 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
michael@0 | 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 24 | */ |
michael@0 | 25 | |
michael@0 | 26 | // must include config.h first for webkit to fiddle with new/delete |
michael@0 | 27 | #include "SkANP.h" |
michael@0 | 28 | |
michael@0 | 29 | static ANPCanvas* anp_newCanvas(const ANPBitmap* bitmap) { |
michael@0 | 30 | SkBitmap bm; |
michael@0 | 31 | return new ANPCanvas(*SkANP::SetBitmap(&bm, *bitmap)); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | static void anp_deleteCanvas(ANPCanvas* canvas) { |
michael@0 | 35 | delete canvas; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | static void anp_save(ANPCanvas* canvas) { |
michael@0 | 39 | canvas->skcanvas->save(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | static void anp_restore(ANPCanvas* canvas) { |
michael@0 | 43 | canvas->skcanvas->restore(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | static void anp_translate(ANPCanvas* canvas, float tx, float ty) { |
michael@0 | 47 | canvas->skcanvas->translate(SkFloatToScalar(tx), SkFloatToScalar(ty)); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | static void anp_scale(ANPCanvas* canvas, float sx, float sy) { |
michael@0 | 51 | canvas->skcanvas->scale(SkFloatToScalar(sx), SkFloatToScalar(sy)); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | static void anp_rotate(ANPCanvas* canvas, float degrees) { |
michael@0 | 55 | canvas->skcanvas->rotate(SkFloatToScalar(degrees)); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | static void anp_skew(ANPCanvas* canvas, float kx, float ky) { |
michael@0 | 59 | canvas->skcanvas->skew(SkFloatToScalar(kx), SkFloatToScalar(ky)); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | static void anp_clipRect(ANPCanvas* canvas, const ANPRectF* rect) { |
michael@0 | 63 | SkRect r; |
michael@0 | 64 | canvas->skcanvas->clipRect(*SkANP::SetRect(&r, *rect)); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | static void anp_clipPath(ANPCanvas* canvas, const ANPPath* path) { |
michael@0 | 68 | canvas->skcanvas->clipPath(*path); |
michael@0 | 69 | } |
michael@0 | 70 | static void anp_concat(ANPCanvas* canvas, const ANPMatrix* matrix) { |
michael@0 | 71 | canvas->skcanvas->concat(*matrix); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | static void anp_getTotalMatrix(ANPCanvas* canvas, ANPMatrix* matrix) { |
michael@0 | 75 | const SkMatrix& src = canvas->skcanvas->getTotalMatrix(); |
michael@0 | 76 | *matrix = *reinterpret_cast<const ANPMatrix*>(&src); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | static bool anp_getLocalClipBounds(ANPCanvas* canvas, ANPRectF* r, |
michael@0 | 80 | bool antialias) { |
michael@0 | 81 | SkRect bounds; |
michael@0 | 82 | if (canvas->skcanvas->getClipBounds(&bounds)) { |
michael@0 | 83 | SkANP::SetRect(r, bounds); |
michael@0 | 84 | return true; |
michael@0 | 85 | } |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | static bool anp_getDeviceClipBounds(ANPCanvas* canvas, ANPRectI* r) { |
michael@0 | 90 | const SkRegion& clip = canvas->skcanvas->getTotalClip(); |
michael@0 | 91 | if (!clip.isEmpty()) { |
michael@0 | 92 | SkANP::SetRect(r, clip.getBounds()); |
michael@0 | 93 | return true; |
michael@0 | 94 | } |
michael@0 | 95 | return false; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | static void anp_drawColor(ANPCanvas* canvas, ANPColor color) { |
michael@0 | 99 | canvas->skcanvas->drawColor(color); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | static void anp_drawPaint(ANPCanvas* canvas, const ANPPaint* paint) { |
michael@0 | 103 | canvas->skcanvas->drawPaint(*paint); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | static void anp_drawLine(ANPCanvas* canvas, float x0, float y0, |
michael@0 | 107 | float x1, float y1, const ANPPaint* paint) { |
michael@0 | 108 | canvas->skcanvas->drawLine(SkFloatToScalar(x0), SkFloatToScalar(y0), |
michael@0 | 109 | SkFloatToScalar(x1), SkFloatToScalar(y1), *paint); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | static void anp_drawRect(ANPCanvas* canvas, const ANPRectF* rect, |
michael@0 | 113 | const ANPPaint* paint) { |
michael@0 | 114 | SkRect r; |
michael@0 | 115 | canvas->skcanvas->drawRect(*SkANP::SetRect(&r, *rect), *paint); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | static void anp_drawOval(ANPCanvas* canvas, const ANPRectF* rect, |
michael@0 | 119 | const ANPPaint* paint) { |
michael@0 | 120 | SkRect r; |
michael@0 | 121 | canvas->skcanvas->drawOval(*SkANP::SetRect(&r, *rect), *paint); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | static void anp_drawPath(ANPCanvas* canvas, const ANPPath* path, |
michael@0 | 125 | const ANPPaint* paint) { |
michael@0 | 126 | canvas->skcanvas->drawPath(*path, *paint); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | static void anp_drawText(ANPCanvas* canvas, const void* text, uint32_t length, |
michael@0 | 130 | float x, float y, const ANPPaint* paint) { |
michael@0 | 131 | canvas->skcanvas->drawText(text, length, |
michael@0 | 132 | SkFloatToScalar(x), SkFloatToScalar(y), |
michael@0 | 133 | *paint); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | static void anp_drawPosText(ANPCanvas* canvas, const void* text, |
michael@0 | 137 | uint32_t byteLength, const float xy[], const ANPPaint* paint) { |
michael@0 | 138 | canvas->skcanvas->drawPosText(text, byteLength, |
michael@0 | 139 | reinterpret_cast<const SkPoint*>(xy), *paint); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | static void anp_drawBitmap(ANPCanvas* canvas, const ANPBitmap* bitmap, |
michael@0 | 143 | float x, float y, const ANPPaint* paint) { |
michael@0 | 144 | SkBitmap bm; |
michael@0 | 145 | canvas->skcanvas->drawBitmap(*SkANP::SetBitmap(&bm, *bitmap), |
michael@0 | 146 | SkFloatToScalar(x), SkFloatToScalar(y), |
michael@0 | 147 | paint); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | static void anp_drawBitmapRect(ANPCanvas* canvas, const ANPBitmap* bitmap, |
michael@0 | 151 | const ANPRectI* src, const ANPRectF* dst, |
michael@0 | 152 | const ANPPaint* paint) { |
michael@0 | 153 | SkBitmap bm; |
michael@0 | 154 | SkRect dstR; |
michael@0 | 155 | SkIRect srcR, *srcPtr = NULL; |
michael@0 | 156 | |
michael@0 | 157 | if (src) { |
michael@0 | 158 | srcPtr = SkANP::SetRect(&srcR, *src); |
michael@0 | 159 | } |
michael@0 | 160 | canvas->skcanvas->drawBitmapRect(*SkANP::SetBitmap(&bm, *bitmap), srcPtr, |
michael@0 | 161 | *SkANP::SetRect(&dstR, *dst), paint); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 165 | |
michael@0 | 166 | #define ASSIGN(obj, name) (obj)->name = anp_##name |
michael@0 | 167 | |
michael@0 | 168 | void InitCanvasInterface(ANPCanvasInterfaceV0* i) { |
michael@0 | 169 | ASSIGN(i, newCanvas); |
michael@0 | 170 | ASSIGN(i, deleteCanvas); |
michael@0 | 171 | ASSIGN(i, save); |
michael@0 | 172 | ASSIGN(i, restore); |
michael@0 | 173 | ASSIGN(i, translate); |
michael@0 | 174 | ASSIGN(i, scale); |
michael@0 | 175 | ASSIGN(i, rotate); |
michael@0 | 176 | ASSIGN(i, skew); |
michael@0 | 177 | ASSIGN(i, clipRect); |
michael@0 | 178 | ASSIGN(i, clipPath); |
michael@0 | 179 | ASSIGN(i, concat); |
michael@0 | 180 | ASSIGN(i, getTotalMatrix); |
michael@0 | 181 | ASSIGN(i, getLocalClipBounds); |
michael@0 | 182 | ASSIGN(i, getDeviceClipBounds); |
michael@0 | 183 | ASSIGN(i, drawColor); |
michael@0 | 184 | ASSIGN(i, drawPaint); |
michael@0 | 185 | ASSIGN(i, drawLine); |
michael@0 | 186 | ASSIGN(i, drawRect); |
michael@0 | 187 | ASSIGN(i, drawOval); |
michael@0 | 188 | ASSIGN(i, drawPath); |
michael@0 | 189 | ASSIGN(i, drawText); |
michael@0 | 190 | ASSIGN(i, drawPosText); |
michael@0 | 191 | ASSIGN(i, drawBitmap); |
michael@0 | 192 | ASSIGN(i, drawBitmapRect); |
michael@0 | 193 | } |