gfx/skia/trunk/src/views/animated/SkImageView.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 "SkImageView.h"
michael@0 9 #include "SkAnimator.h"
michael@0 10 #include "SkBitmap.h"
michael@0 11 #include "SkCanvas.h"
michael@0 12 #include "SkImageDecoder.h"
michael@0 13 #include "SkMatrix.h"
michael@0 14 #include "SkSystemEventTypes.h"
michael@0 15 #include "SkTime.h"
michael@0 16
michael@0 17 SkImageView::SkImageView()
michael@0 18 {
michael@0 19 fMatrix = NULL;
michael@0 20 fScaleType = kMatrix_ScaleType;
michael@0 21
michael@0 22 fData.fAnim = NULL; // handles initializing the other union values
michael@0 23 fDataIsAnim = true;
michael@0 24
michael@0 25 fUriIsValid = false; // an empty string is not valid
michael@0 26 }
michael@0 27
michael@0 28 SkImageView::~SkImageView()
michael@0 29 {
michael@0 30 if (fMatrix)
michael@0 31 sk_free(fMatrix);
michael@0 32
michael@0 33 this->freeData();
michael@0 34 }
michael@0 35
michael@0 36 void SkImageView::getUri(SkString* uri) const
michael@0 37 {
michael@0 38 if (uri)
michael@0 39 *uri = fUri;
michael@0 40 }
michael@0 41
michael@0 42 void SkImageView::setUri(const char uri[])
michael@0 43 {
michael@0 44 if (!fUri.equals(uri))
michael@0 45 {
michael@0 46 fUri.set(uri);
michael@0 47 this->onUriChange();
michael@0 48 }
michael@0 49 }
michael@0 50
michael@0 51 void SkImageView::setUri(const SkString& uri)
michael@0 52 {
michael@0 53 if (fUri != uri)
michael@0 54 {
michael@0 55 fUri = uri;
michael@0 56 this->onUriChange();
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 void SkImageView::setScaleType(ScaleType st)
michael@0 61 {
michael@0 62 SkASSERT((unsigned)st <= kFitEnd_ScaleType);
michael@0 63
michael@0 64 if ((ScaleType)fScaleType != st)
michael@0 65 {
michael@0 66 fScaleType = SkToU8(st);
michael@0 67 if (fUriIsValid)
michael@0 68 this->inval(NULL);
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 bool SkImageView::getImageMatrix(SkMatrix* matrix) const
michael@0 73 {
michael@0 74 if (fMatrix)
michael@0 75 {
michael@0 76 SkASSERT(!fMatrix->isIdentity());
michael@0 77 if (matrix)
michael@0 78 *matrix = *fMatrix;
michael@0 79 return true;
michael@0 80 }
michael@0 81 else
michael@0 82 {
michael@0 83 if (matrix)
michael@0 84 matrix->reset();
michael@0 85 return false;
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 void SkImageView::setImageMatrix(const SkMatrix* matrix)
michael@0 90 {
michael@0 91 bool changed = false;
michael@0 92
michael@0 93 if (matrix && !matrix->isIdentity())
michael@0 94 {
michael@0 95 if (fMatrix == NULL)
michael@0 96 fMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix));
michael@0 97 *fMatrix = *matrix;
michael@0 98 changed = true;
michael@0 99 }
michael@0 100 else // set us to identity
michael@0 101 {
michael@0 102 if (fMatrix)
michael@0 103 {
michael@0 104 SkASSERT(!fMatrix->isIdentity());
michael@0 105 sk_free(fMatrix);
michael@0 106 fMatrix = NULL;
michael@0 107 changed = true;
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 // only redraw if we changed our matrix and we're not in scaleToFit mode
michael@0 112 if (changed && this->getScaleType() == kMatrix_ScaleType && fUriIsValid)
michael@0 113 this->inval(NULL);
michael@0 114 }
michael@0 115
michael@0 116 ///////////////////////////////////////////////////////////////////////////////////////////////
michael@0 117
michael@0 118 bool SkImageView::onEvent(const SkEvent& evt)
michael@0 119 {
michael@0 120 if (evt.isType(SK_EventType_Inval))
michael@0 121 {
michael@0 122 if (fUriIsValid)
michael@0 123 this->inval(NULL);
michael@0 124 return true;
michael@0 125 }
michael@0 126 return this->INHERITED::onEvent(evt);
michael@0 127 }
michael@0 128
michael@0 129 static inline SkMatrix::ScaleToFit scaleTypeToScaleToFit(SkImageView::ScaleType st)
michael@0 130 {
michael@0 131 SkASSERT(st != SkImageView::kMatrix_ScaleType);
michael@0 132 SkASSERT((unsigned)st <= SkImageView::kFitEnd_ScaleType);
michael@0 133
michael@0 134 SkASSERT(SkImageView::kFitXY_ScaleType - 1 == SkMatrix::kFill_ScaleToFit);
michael@0 135 SkASSERT(SkImageView::kFitStart_ScaleType - 1 == SkMatrix::kStart_ScaleToFit);
michael@0 136 SkASSERT(SkImageView::kFitCenter_ScaleType - 1 == SkMatrix::kCenter_ScaleToFit);
michael@0 137 SkASSERT(SkImageView::kFitEnd_ScaleType - 1 == SkMatrix::kEnd_ScaleToFit);
michael@0 138
michael@0 139 return (SkMatrix::ScaleToFit)(st - 1);
michael@0 140 }
michael@0 141
michael@0 142 void SkImageView::onDraw(SkCanvas* canvas)
michael@0 143 {
michael@0 144 SkRect src;
michael@0 145 if (!this->getDataBounds(&src))
michael@0 146 {
michael@0 147 SkDEBUGCODE(canvas->drawColor(SK_ColorRED);)
michael@0 148 return; // nothing to draw
michael@0 149 }
michael@0 150
michael@0 151 SkAutoCanvasRestore restore(canvas, true);
michael@0 152 SkMatrix matrix;
michael@0 153
michael@0 154 if (this->getScaleType() == kMatrix_ScaleType)
michael@0 155 (void)this->getImageMatrix(&matrix);
michael@0 156 else
michael@0 157 {
michael@0 158 SkRect dst;
michael@0 159 dst.set(0, 0, this->width(), this->height());
michael@0 160 matrix.setRectToRect(src, dst, scaleTypeToScaleToFit(this->getScaleType()));
michael@0 161 }
michael@0 162 canvas->concat(matrix);
michael@0 163
michael@0 164 SkPaint paint;
michael@0 165
michael@0 166 paint.setAntiAlias(true);
michael@0 167
michael@0 168 if (fDataIsAnim)
michael@0 169 {
michael@0 170 SkMSec now = SkTime::GetMSecs();
michael@0 171
michael@0 172 SkAnimator::DifferenceType diff = fData.fAnim->draw(canvas, &paint, now);
michael@0 173
michael@0 174 SkDEBUGF(("SkImageView : now = %X[%12.3f], diff = %d\n", now, now/1000., diff));
michael@0 175
michael@0 176 if (diff == SkAnimator::kDifferent)
michael@0 177 this->inval(NULL);
michael@0 178 else if (diff == SkAnimator::kPartiallyDifferent)
michael@0 179 {
michael@0 180 SkRect bounds;
michael@0 181 fData.fAnim->getInvalBounds(&bounds);
michael@0 182 matrix.mapRect(&bounds); // get the bounds into view coordinates
michael@0 183 this->inval(&bounds);
michael@0 184 }
michael@0 185 }
michael@0 186 else
michael@0 187 canvas->drawBitmap(*fData.fBitmap, 0, 0, &paint);
michael@0 188 }
michael@0 189
michael@0 190 void SkImageView::onInflate(const SkDOM& dom, const SkDOMNode* node)
michael@0 191 {
michael@0 192 this->INHERITED::onInflate(dom, node);
michael@0 193
michael@0 194 const char* src = dom.findAttr(node, "src");
michael@0 195 if (src)
michael@0 196 this->setUri(src);
michael@0 197
michael@0 198 int index = dom.findList(node, "scaleType", "matrix,fitXY,fitStart,fitCenter,fitEnd");
michael@0 199 if (index >= 0)
michael@0 200 this->setScaleType((ScaleType)index);
michael@0 201
michael@0 202 // need inflate syntax/reader for matrix
michael@0 203 }
michael@0 204
michael@0 205 /////////////////////////////////////////////////////////////////////////////////////
michael@0 206
michael@0 207 void SkImageView::onUriChange()
michael@0 208 {
michael@0 209 if (this->freeData())
michael@0 210 this->inval(NULL);
michael@0 211 fUriIsValid = true; // give ensureUriIsLoaded() a shot at the new uri
michael@0 212 }
michael@0 213
michael@0 214 bool SkImageView::freeData()
michael@0 215 {
michael@0 216 if (fData.fAnim) // test is valid for all union values
michael@0 217 {
michael@0 218 if (fDataIsAnim)
michael@0 219 delete fData.fAnim;
michael@0 220 else
michael@0 221 delete fData.fBitmap;
michael@0 222
michael@0 223 fData.fAnim = NULL; // valid for all union values
michael@0 224 return true;
michael@0 225 }
michael@0 226 return false;
michael@0 227 }
michael@0 228
michael@0 229 bool SkImageView::getDataBounds(SkRect* bounds)
michael@0 230 {
michael@0 231 SkASSERT(bounds);
michael@0 232
michael@0 233 if (this->ensureUriIsLoaded())
michael@0 234 {
michael@0 235 SkScalar width, height;
michael@0 236
michael@0 237 if (fDataIsAnim)
michael@0 238 {
michael@0 239 if (SkScalarIsNaN(width = fData.fAnim->getScalar("dimensions", "x")) ||
michael@0 240 SkScalarIsNaN(height = fData.fAnim->getScalar("dimensions", "y")))
michael@0 241 {
michael@0 242 // cons up fake bounds
michael@0 243 width = this->width();
michael@0 244 height = this->height();
michael@0 245 }
michael@0 246 }
michael@0 247 else
michael@0 248 {
michael@0 249 width = SkIntToScalar(fData.fBitmap->width());
michael@0 250 height = SkIntToScalar(fData.fBitmap->height());
michael@0 251 }
michael@0 252 bounds->set(0, 0, width, height);
michael@0 253 return true;
michael@0 254 }
michael@0 255 return false;
michael@0 256 }
michael@0 257
michael@0 258 bool SkImageView::ensureUriIsLoaded()
michael@0 259 {
michael@0 260 if (fData.fAnim) // test is valid for all union values
michael@0 261 {
michael@0 262 SkASSERT(fUriIsValid);
michael@0 263 return true;
michael@0 264 }
michael@0 265 if (!fUriIsValid)
michael@0 266 return false;
michael@0 267
michael@0 268 // try to load the url
michael@0 269 if (fUri.endsWith(".xml")) // assume it is screenplay
michael@0 270 {
michael@0 271 SkAnimator* anim = new SkAnimator;
michael@0 272
michael@0 273 if (!anim->decodeURI(fUri.c_str()))
michael@0 274 {
michael@0 275 delete anim;
michael@0 276 fUriIsValid = false;
michael@0 277 return false;
michael@0 278 }
michael@0 279 anim->setHostEventSink(this);
michael@0 280
michael@0 281 fData.fAnim = anim;
michael@0 282 fDataIsAnim = true;
michael@0 283 }
michael@0 284 else // assume it is an image format
michael@0 285 {
michael@0 286 #if 0
michael@0 287 SkBitmap* bitmap = new SkBitmap;
michael@0 288
michael@0 289 if (!SkImageDecoder::DecodeURL(fUri.c_str(), bitmap))
michael@0 290 {
michael@0 291 delete bitmap;
michael@0 292 fUriIsValid = false;
michael@0 293 return false;
michael@0 294 }
michael@0 295 fData.fBitmap = bitmap;
michael@0 296 fDataIsAnim = false;
michael@0 297 #else
michael@0 298 return false;
michael@0 299 #endif
michael@0 300 }
michael@0 301 return true;
michael@0 302 }

mercurial