gfx/skia/trunk/src/utils/SkMeshUtils.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2011 Google Inc.
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8 #include "SkMeshUtils.h"
michael@0 9 #include "SkCanvas.h"
michael@0 10 #include "SkPaint.h"
michael@0 11
michael@0 12 SkMeshIndices::SkMeshIndices() {
michael@0 13 sk_bzero(this, sizeof(*this));
michael@0 14 }
michael@0 15
michael@0 16 SkMeshIndices::~SkMeshIndices() {
michael@0 17 sk_free(fStorage);
michael@0 18 }
michael@0 19
michael@0 20 bool SkMeshIndices::init(SkPoint tex[], uint16_t indices[],
michael@0 21 int texW, int texH, int rows, int cols) {
michael@0 22 if (rows < 2 || cols < 2) {
michael@0 23 sk_free(fStorage);
michael@0 24 fStorage = NULL;
michael@0 25 fTex = NULL;
michael@0 26 fIndices = NULL;
michael@0 27 fTexCount = fIndexCount = 0;
michael@0 28 return false;
michael@0 29 }
michael@0 30
michael@0 31 sk_free(fStorage);
michael@0 32 fStorage = NULL;
michael@0 33
michael@0 34 fTexCount = rows * cols;
michael@0 35 rows -= 1;
michael@0 36 cols -= 1;
michael@0 37 fIndexCount = rows * cols * 6;
michael@0 38
michael@0 39 if (tex) {
michael@0 40 fTex = tex;
michael@0 41 fIndices = indices;
michael@0 42 } else {
michael@0 43 fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) +
michael@0 44 fIndexCount * sizeof(uint16_t));
michael@0 45 fTex = (SkPoint*)fStorage;
michael@0 46 fIndices = (uint16_t*)(fTex + fTexCount);
michael@0 47 }
michael@0 48
michael@0 49 // compute the indices
michael@0 50 {
michael@0 51 uint16_t* idx = fIndices;
michael@0 52 int index = 0;
michael@0 53 for (int y = 0; y < cols; y++) {
michael@0 54 for (int x = 0; x < rows; x++) {
michael@0 55 *idx++ = index;
michael@0 56 *idx++ = index + rows + 1;
michael@0 57 *idx++ = index + 1;
michael@0 58
michael@0 59 *idx++ = index + 1;
michael@0 60 *idx++ = index + rows + 1;
michael@0 61 *idx++ = index + rows + 2;
michael@0 62
michael@0 63 index += 1;
michael@0 64 }
michael@0 65 index += 1;
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 // compute texture coordinates
michael@0 70 {
michael@0 71 SkPoint* tex = fTex;
michael@0 72 const SkScalar dx = SkIntToScalar(texW) / rows;
michael@0 73 const SkScalar dy = SkIntToScalar(texH) / cols;
michael@0 74 for (int y = 0; y <= cols; y++) {
michael@0 75 for (int x = 0; x <= rows; x++) {
michael@0 76 tex->set(x*dx, y*dy);
michael@0 77 tex += 1;
michael@0 78 }
michael@0 79 }
michael@0 80 }
michael@0 81 return true;
michael@0 82 }
michael@0 83
michael@0 84 ///////////////////////////////////////////////////////////////////////////////
michael@0 85
michael@0 86 #include "SkShader.h"
michael@0 87
michael@0 88 void SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap,
michael@0 89 int rows, int cols, const SkPoint verts[],
michael@0 90 const SkColor colors[], const SkPaint& paint) {
michael@0 91 SkMeshIndices idx;
michael@0 92
michael@0 93 if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) {
michael@0 94 SkPaint p(paint);
michael@0 95 p.setShader(SkShader::CreateBitmapShader(bitmap,
michael@0 96 SkShader::kClamp_TileMode,
michael@0 97 SkShader::kClamp_TileMode))->unref();
michael@0 98 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
michael@0 99 rows * cols, verts, idx.tex(), colors, NULL,
michael@0 100 idx.indices(), idx.indexCount(), p);
michael@0 101 }
michael@0 102 }

mercurial