Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/core/SkDraw.cpp

Issue 207683004: Extract most of the mutable state of SkShader into a separate Context object. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: freeLast reduces fStorageUsed; TriColorShaderContext::setup; totalInverse param to validContext Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkDraw.h" 8 #include "SkDraw.h"
9 #include "SkBlitter.h" 9 #include "SkBlitter.h"
10 #include "SkBounder.h" 10 #include "SkBounder.h"
(...skipping 2337 matching lines...) Expand 10 before | Expand all | Expand 10 after
2348 2348
2349 src[0] = texs[state.f0]; 2349 src[0] = texs[state.f0];
2350 src[1] = texs[state.f1]; 2350 src[1] = texs[state.f1];
2351 src[2] = texs[state.f2]; 2351 src[2] = texs[state.f2];
2352 dst[0] = verts[state.f0]; 2352 dst[0] = verts[state.f0];
2353 dst[1] = verts[state.f1]; 2353 dst[1] = verts[state.f1];
2354 dst[2] = verts[state.f2]; 2354 dst[2] = verts[state.f2];
2355 return matrix->setPolyToPoly(src, dst, 3); 2355 return matrix->setPolyToPoly(src, dst, 3);
2356 } 2356 }
2357 2357
2358 /**
2359 * This class is NOT immutable due to the setup() method being called repeatedl y after
2360 * the shader has been associated with a paint. This is okay because we only ev er
2361 * associate this shader with temporary paints.
2362 */
2358 class SkTriColorShader : public SkShader { 2363 class SkTriColorShader : public SkShader {
2359 public: 2364 public:
2360 SkTriColorShader() {} 2365 SkTriColorShader() {}
2361 2366
2362 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int); 2367 virtual SkShader::Context* createContext(
2368 const SkBitmap&, const SkPaint&, const SkMatrix&, void*) const SK_OV ERRIDE;
2369 virtual size_t contextSize() const SK_OVERRIDE;
2363 2370
2364 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRID E; 2371 class TriColorShaderContext : public SkShader::Context {
2372 public:
2373 TriColorShaderContext(const SkTriColorShader& shader, const SkBitmap& de vice,
2374 const SkPaint& paint, const SkMatrix& matrix);
2375 virtual ~TriColorShaderContext();
2376
2377 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2378
2379 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVE RRIDE;
2380
2381 private:
2382 SkMatrix fDstToUnit;
2383 SkPMColor fColors[3];
2384
2385 typedef SkShader::Context INHERITED;
2386 };
2365 2387
2366 SK_DEVELOPER_TO_STRING() 2388 SK_DEVELOPER_TO_STRING()
2367 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader) 2389 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
2368 2390
2369 protected: 2391 protected:
2370 SkTriColorShader(SkReadBuffer& buffer) : SkShader(buffer) {} 2392 SkTriColorShader(SkReadBuffer& buffer) : SkShader(buffer) {}
2371 2393
2372 private:
2373 SkMatrix fDstToUnit;
2374 SkPMColor fColors[3];
2375
2376 typedef SkShader INHERITED; 2394 typedef SkShader INHERITED;
2377 }; 2395 };
2378 2396
2379 bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[], 2397 SkShader::Context* SkTriColorShader::createContext(const SkBitmap& device, const SkPaint& paint,
2380 int index0, int index1, int index2) { 2398 const SkMatrix& matrix, void* storage) const {
2399 SkMatrix totalInverse;
2400 if (!this->validContext(device, paint, matrix, &totalInverse)) {
2401 return NULL;
2402 }
2403
2404 return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, device, paint, matrix));
2405 }
2406
2407 bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const S kColor colors[],
2408 int index0, int index1, int index2) {
2381 2409
2382 fColors[0] = SkPreMultiplyColor(colors[index0]); 2410 fColors[0] = SkPreMultiplyColor(colors[index0]);
2383 fColors[1] = SkPreMultiplyColor(colors[index1]); 2411 fColors[1] = SkPreMultiplyColor(colors[index1]);
2384 fColors[2] = SkPreMultiplyColor(colors[index2]); 2412 fColors[2] = SkPreMultiplyColor(colors[index2]);
2385 2413
2386 SkMatrix m, im; 2414 SkMatrix m, im;
2387 m.reset(); 2415 m.reset();
2388 m.set(0, pts[index1].fX - pts[index0].fX); 2416 m.set(0, pts[index1].fX - pts[index0].fX);
2389 m.set(1, pts[index2].fX - pts[index0].fX); 2417 m.set(1, pts[index2].fX - pts[index0].fX);
2390 m.set(2, pts[index0].fX); 2418 m.set(2, pts[index0].fX);
(...skipping 13 matching lines...) Expand all
2404 int scale = SkScalarToFixed(v) >> 8; 2432 int scale = SkScalarToFixed(v) >> 8;
2405 if (scale < 0) { 2433 if (scale < 0) {
2406 scale = 0; 2434 scale = 0;
2407 } 2435 }
2408 if (scale > 255) { 2436 if (scale > 255) {
2409 scale = 255; 2437 scale = 255;
2410 } 2438 }
2411 return SkAlpha255To256(scale); 2439 return SkAlpha255To256(scale);
2412 } 2440 }
2413 2441
2414 void SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { 2442
2443 SkTriColorShader::TriColorShaderContext::TriColorShaderContext(
2444 const SkTriColorShader& shader, const SkBitmap& device,
2445 const SkPaint& paint, const SkMatrix& matrix)
2446 : INHERITED(shader, device, paint, matrix) {}
2447
2448 SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
2449
2450 size_t SkTriColorShader::contextSize() const {
2451 return sizeof(TriColorShaderContext);
2452 }
2453 void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
2415 SkPoint src; 2454 SkPoint src;
2416 2455
2417 for (int i = 0; i < count; i++) { 2456 for (int i = 0; i < count; i++) {
2418 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src); 2457 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
2419 x += 1; 2458 x += 1;
2420 2459
2421 int scale1 = ScalarTo256(src.fX); 2460 int scale1 = ScalarTo256(src.fX);
2422 int scale2 = ScalarTo256(src.fY); 2461 int scale2 = ScalarTo256(src.fY);
2423 int scale0 = 256 - scale1 - scale2; 2462 int scale0 = 256 - scale1 - scale2;
2424 if (scale0 < 0) { 2463 if (scale0 < 0) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
2511 SkShader* compose = SkNEW_ARGS(SkComposeShader, 2550 SkShader* compose = SkNEW_ARGS(SkComposeShader,
2512 (&triShader, shader, xmode)); 2551 (&triShader, shader, xmode));
2513 p.setShader(compose)->unref(); 2552 p.setShader(compose)->unref();
2514 if (releaseMode) { 2553 if (releaseMode) {
2515 xmode->unref(); 2554 xmode->unref();
2516 } 2555 }
2517 } 2556 }
2518 } 2557 }
2519 2558
2520 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p); 2559 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
2521 // important that we abort early, as below we may manipulate the shader 2560 // Important that we abort early, as below we may manipulate the shader cont ext
scroggo 2014/03/27 18:05:33 Comment nit: We're replacing the shader context, b
Dominik Grewe 2014/03/28 18:16:32 Updated.
2522 // and that is only valid if the shader returned true from setContext. 2561 // and that is only valid if a context was successfully created from the sha der.
2523 // If it returned false, then our blitter will be the NullBlitter. 2562 // If it returned false, then our blitter will be the NullBlitter.
2524 if (blitter->isNullBlitter()) { 2563 if (blitter->isNullBlitter()) {
2525 return; 2564 return;
2526 } 2565 }
2527 2566
2528 // setup our state and function pointer for iterating triangles 2567 // setup our state and function pointer for iterating triangles
2529 VertState state(count, indices, indexCount); 2568 VertState state(count, indices, indexCount);
2530 VertState::Proc vertProc = state.chooseProc(vmode); 2569 VertState::Proc vertProc = state.chooseProc(vmode);
2531 2570
2532 if (NULL != textures || NULL != colors) { 2571 if (NULL != textures || NULL != colors) {
2533 SkMatrix tempM; 2572 SkMatrix tempM;
2534 SkMatrix savedLocalM; 2573 SkMatrix savedLocalM;
2535 if (shader) { 2574 if (shader) {
2536 savedLocalM = shader->getLocalMatrix(); 2575 savedLocalM = shader->getLocalMatrix();
2537 } 2576 }
2538 2577
2539 // setContext has already been called and verified to return true
2540 // by the constructor of SkAutoBlitterChoose
2541 bool prevContextSuccess = true;
2542 while (vertProc(&state)) { 2578 while (vertProc(&state)) {
2543 if (NULL != textures) { 2579 if (NULL != textures) {
2544 if (texture_to_matrix(state, vertices, textures, &tempM)) { 2580 if (texture_to_matrix(state, vertices, textures, &tempM)) {
2545 tempM.postConcat(savedLocalM); 2581 tempM.postConcat(savedLocalM);
2546 shader->setLocalMatrix(tempM); 2582 shader->setLocalMatrix(tempM);
2547 // Need to recall setContext since we changed the local matr ix. 2583 if (!blitter->resetShaderContext(*fBitmap, p, *fMatrix)) {
2548 // However, we also need to balance the calls this with a
2549 // call to endContext which requires tracking the result of
2550 // the previous call to setContext.
2551 if (prevContextSuccess) {
2552 shader->endContext();
2553 }
2554 prevContextSuccess = shader->setContext(*fBitmap, p, *fMatri x);
2555 if (!prevContextSuccess) {
2556 continue; 2584 continue;
2557 } 2585 }
2558 } 2586 }
2559 } 2587 }
2560 if (NULL != colors) { 2588 if (NULL != colors) {
2561 if (!triShader.setup(vertices, colors, 2589 SkShader::Context* shaderContext = blitter->getShaderContext();
2562 state.f0, state.f1, state.f2)) { 2590 SkASSERT(shaderContext);
2591 SkTriColorShader::TriColorShaderContext* triColorShaderContext =
2592 static_cast<SkTriColorShader::TriColorShaderContext*>(sh aderContext);
2593 if (!triColorShaderContext->setup(vertices, colors,
2594 state.f0, state.f1, state.f2)) {
2563 continue; 2595 continue;
2564 } 2596 }
2565 } 2597 }
2566 2598
2567 SkPoint tmp[] = { 2599 SkPoint tmp[] = {
2568 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2] 2600 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2569 }; 2601 };
2570 SkScan::FillTriangle(tmp, *fRC, blitter.get()); 2602 SkScan::FillTriangle(tmp, *fRC, blitter.get());
2571 } 2603 }
2572 2604
2573 // now restore the shader's original local matrix 2605 // now restore the shader's original local matrix
2574 if (NULL != shader) { 2606 if (NULL != shader) {
2575 shader->setLocalMatrix(savedLocalM); 2607 shader->setLocalMatrix(savedLocalM);
2576 } 2608 }
2577
2578 // If the final call to setContext fails we must make it suceed so that the
2579 // call to endContext in the destructor for SkAutoBlitterChoose is balan ced.
2580 if (!prevContextSuccess) {
2581 prevContextSuccess = shader->setContext(*fBitmap, paint, SkMatrix::I ());
2582 SkASSERT(prevContextSuccess);
2583 }
2584 } else { 2609 } else {
2585 // no colors[] and no texture 2610 // no colors[] and no texture
2586 HairProc hairProc = ChooseHairProc(paint.isAntiAlias()); 2611 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
2587 const SkRasterClip& clip = *fRC; 2612 const SkRasterClip& clip = *fRC;
2588 while (vertProc(&state)) { 2613 while (vertProc(&state)) {
2589 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get() ); 2614 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get() );
2590 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get() ); 2615 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get() );
2591 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get() ); 2616 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get() );
2592 } 2617 }
2593 } 2618 }
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
2805 mask->fImage = SkMask::AllocImage(size); 2830 mask->fImage = SkMask::AllocImage(size);
2806 memset(mask->fImage, 0, mask->computeImageSize()); 2831 memset(mask->fImage, 0, mask->computeImageSize());
2807 } 2832 }
2808 2833
2809 if (SkMask::kJustComputeBounds_CreateMode != mode) { 2834 if (SkMask::kJustComputeBounds_CreateMode != mode) {
2810 draw_into_mask(*mask, devPath, style); 2835 draw_into_mask(*mask, devPath, style);
2811 } 2836 }
2812 2837
2813 return true; 2838 return true;
2814 } 2839 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698