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

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: scroggo's comments Created 6 years, 9 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 // TODO(dominikg): Feels like this should be part of the context, but we don 't have
2368 // access to it where we need it.
2369 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int, const SkMatrix&);
2363 2370
2364 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRID E; 2371 virtual SkShader::Context* createContext(
2372 const SkBitmap&, const SkPaint&, const SkMatrix&, void*) const SK_OV ERRIDE;
2373 virtual size_t contextSize() const SK_OVERRIDE;
2374
2375 class TriColorShaderContext : public SkShader::Context {
2376 public:
2377 TriColorShaderContext(const SkTriColorShader& shader, const SkBitmap& de vice,
2378 const SkPaint& paint, const SkMatrix& matrix);
2379 virtual ~TriColorShaderContext();
2380
2381 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVE RRIDE;
2382
2383 typedef SkShader::Context INHERITED;
2384 };
2365 2385
2366 SK_DEVELOPER_TO_STRING() 2386 SK_DEVELOPER_TO_STRING()
2367 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader) 2387 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
2368 2388
2369 protected: 2389 protected:
2370 SkTriColorShader(SkReadBuffer& buffer) : SkShader(buffer) {} 2390 SkTriColorShader(SkReadBuffer& buffer) : SkShader(buffer) {}
2371 2391
2372 private: 2392 private:
2373 SkMatrix fDstToUnit; 2393 SkMatrix fDstToUnit;
2374 SkPMColor fColors[3]; 2394 SkPMColor fColors[3];
2375 2395
2376 typedef SkShader INHERITED; 2396 typedef SkShader INHERITED;
2377 }; 2397 };
2378 2398
2399 SkShader::Context* SkTriColorShader::createContext(const SkBitmap& device, const SkPaint& paint,
2400 const SkMatrix& matrix, void* storage) const {
2401 if (!this->validContext(device, paint, matrix)) {
2402 return NULL;
2403 }
2404
2405 return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, device, paint, matrix));
2406 }
2407
2379 bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[], 2408 bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
2380 int index0, int index1, int index2) { 2409 int index0, int index1, int index2, const SkMatrix& matrix) {
2381 2410
2382 fColors[0] = SkPreMultiplyColor(colors[index0]); 2411 fColors[0] = SkPreMultiplyColor(colors[index0]);
2383 fColors[1] = SkPreMultiplyColor(colors[index1]); 2412 fColors[1] = SkPreMultiplyColor(colors[index1]);
2384 fColors[2] = SkPreMultiplyColor(colors[index2]); 2413 fColors[2] = SkPreMultiplyColor(colors[index2]);
2385 2414
2386 SkMatrix m, im; 2415 SkMatrix m, im;
2387 m.reset(); 2416 m.reset();
2388 m.set(0, pts[index1].fX - pts[index0].fX); 2417 m.set(0, pts[index1].fX - pts[index0].fX);
2389 m.set(1, pts[index2].fX - pts[index0].fX); 2418 m.set(1, pts[index2].fX - pts[index0].fX);
2390 m.set(2, pts[index0].fX); 2419 m.set(2, pts[index0].fX);
2391 m.set(3, pts[index1].fY - pts[index0].fY); 2420 m.set(3, pts[index1].fY - pts[index0].fY);
2392 m.set(4, pts[index2].fY - pts[index0].fY); 2421 m.set(4, pts[index2].fY - pts[index0].fY);
2393 m.set(5, pts[index0].fY); 2422 m.set(5, pts[index0].fY);
2394 if (!m.invert(&im)) { 2423 if (!m.invert(&im)) {
2395 return false; 2424 return false;
2396 } 2425 }
2397 return fDstToUnit.setConcat(im, this->getTotalInverse()); 2426 SkMatrix inverse;
2427 SkASSERT(matrix.invert(&inverse));
2428 return fDstToUnit.setConcat(im, inverse);
2398 } 2429 }
2399 2430
2400 #include "SkColorPriv.h" 2431 #include "SkColorPriv.h"
2401 #include "SkComposeShader.h" 2432 #include "SkComposeShader.h"
2402 2433
2403 static int ScalarTo256(SkScalar v) { 2434 static int ScalarTo256(SkScalar v) {
2404 int scale = SkScalarToFixed(v) >> 8; 2435 int scale = SkScalarToFixed(v) >> 8;
2405 if (scale < 0) { 2436 if (scale < 0) {
2406 scale = 0; 2437 scale = 0;
2407 } 2438 }
2408 if (scale > 255) { 2439 if (scale > 255) {
2409 scale = 255; 2440 scale = 255;
2410 } 2441 }
2411 return SkAlpha255To256(scale); 2442 return SkAlpha255To256(scale);
2412 } 2443 }
2413 2444
2414 void SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { 2445
2446 SkTriColorShader::TriColorShaderContext::TriColorShaderContext(
2447 const SkTriColorShader& shader, const SkBitmap& device,
2448 const SkPaint& paint, const SkMatrix& matrix)
2449 : INHERITED(shader, device, paint, matrix) {}
2450
2451 SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
2452
2453 size_t SkTriColorShader::contextSize() const {
2454 return sizeof(TriColorShaderContext);
2455 }
2456 void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
2457 const SkTriColorShader& triColorShader = static_cast<const SkTriColorShader& >(fShader);
2458
2415 SkPoint src; 2459 SkPoint src;
2416 2460
2417 for (int i = 0; i < count; i++) { 2461 for (int i = 0; i < count; i++) {
2418 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src); 2462 triColorShader.fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src );
2419 x += 1; 2463 x += 1;
2420 2464
2421 int scale1 = ScalarTo256(src.fX); 2465 int scale1 = ScalarTo256(src.fX);
2422 int scale2 = ScalarTo256(src.fY); 2466 int scale2 = ScalarTo256(src.fY);
2423 int scale0 = 256 - scale1 - scale2; 2467 int scale0 = 256 - scale1 - scale2;
2424 if (scale0 < 0) { 2468 if (scale0 < 0) {
2425 if (scale1 > scale2) { 2469 if (scale1 > scale2) {
2426 scale2 = 256 - scale1; 2470 scale2 = 256 - scale1;
2427 } else { 2471 } else {
2428 scale1 = 256 - scale2; 2472 scale1 = 256 - scale2;
2429 } 2473 }
2430 scale0 = 0; 2474 scale0 = 0;
2431 } 2475 }
2432 2476
2433 dstC[i] = SkAlphaMulQ(fColors[0], scale0) + 2477 dstC[i] = SkAlphaMulQ(triColorShader.fColors[0], scale0) +
2434 SkAlphaMulQ(fColors[1], scale1) + 2478 SkAlphaMulQ(triColorShader.fColors[1], scale1) +
2435 SkAlphaMulQ(fColors[2], scale2); 2479 SkAlphaMulQ(triColorShader.fColors[2], scale2);
2436 } 2480 }
2437 } 2481 }
2438 2482
2439 #ifdef SK_DEVELOPER 2483 #ifdef SK_DEVELOPER
2440 void SkTriColorShader::toString(SkString* str) const { 2484 void SkTriColorShader::toString(SkString* str) const {
2441 str->append("SkTriColorShader: ("); 2485 str->append("SkTriColorShader: (");
2442 2486
2443 this->INHERITED::toString(str); 2487 this->INHERITED::toString(str);
2444 2488
2445 str->append(")"); 2489 str->append(")");
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
2511 SkShader* compose = SkNEW_ARGS(SkComposeShader, 2555 SkShader* compose = SkNEW_ARGS(SkComposeShader,
2512 (&triShader, shader, xmode)); 2556 (&triShader, shader, xmode));
2513 p.setShader(compose)->unref(); 2557 p.setShader(compose)->unref();
2514 if (releaseMode) { 2558 if (releaseMode) {
2515 xmode->unref(); 2559 xmode->unref();
2516 } 2560 }
2517 } 2561 }
2518 } 2562 }
2519 2563
2520 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p); 2564 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
2521 // important that we abort early, as below we may manipulate the shader 2565 // Important that we abort early, as below we may manipulate the shader cont ext
2522 // and that is only valid if the shader returned true from setContext. 2566 // 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. 2567 // If it returned false, then our blitter will be the NullBlitter.
2524 if (blitter->isNullBlitter()) { 2568 if (blitter->isNullBlitter()) {
2525 return; 2569 return;
2526 } 2570 }
2527 2571
2528 // setup our state and function pointer for iterating triangles 2572 // setup our state and function pointer for iterating triangles
2529 VertState state(count, indices, indexCount); 2573 VertState state(count, indices, indexCount);
2530 VertState::Proc vertProc = state.chooseProc(vmode); 2574 VertState::Proc vertProc = state.chooseProc(vmode);
2531 2575
2532 if (NULL != textures || NULL != colors) { 2576 if (NULL != textures || NULL != colors) {
2533 SkMatrix tempM; 2577 SkMatrix tempM;
2534 SkMatrix savedLocalM; 2578 SkMatrix savedLocalM;
2535 if (shader) { 2579 if (shader) {
2536 savedLocalM = shader->getLocalMatrix(); 2580 savedLocalM = shader->getLocalMatrix();
2537 } 2581 }
2538 2582
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)) { 2583 while (vertProc(&state)) {
2543 if (NULL != textures) { 2584 if (NULL != textures) {
2544 if (texture_to_matrix(state, vertices, textures, &tempM)) { 2585 if (texture_to_matrix(state, vertices, textures, &tempM)) {
2545 tempM.postConcat(savedLocalM); 2586 tempM.postConcat(savedLocalM);
2546 shader->setLocalMatrix(tempM); 2587 shader->setLocalMatrix(tempM);
2547 // Need to recall setContext since we changed the local matr ix. 2588 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; 2589 continue;
2557 } 2590 }
2558 } 2591 }
2559 } 2592 }
2560 if (NULL != colors) { 2593 if (NULL != colors) {
2561 if (!triShader.setup(vertices, colors, 2594 if (!triShader.setup(vertices, colors,
2562 state.f0, state.f1, state.f2)) { 2595 state.f0, state.f1, state.f2, *fMatrix)) {
2563 continue; 2596 continue;
2564 } 2597 }
2565 } 2598 }
2566 2599
2567 SkPoint tmp[] = { 2600 SkPoint tmp[] = {
2568 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2] 2601 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2569 }; 2602 };
2570 SkScan::FillTriangle(tmp, *fRC, blitter.get()); 2603 SkScan::FillTriangle(tmp, *fRC, blitter.get());
2571 } 2604 }
2572 2605
2573 // now restore the shader's original local matrix 2606 // now restore the shader's original local matrix
2574 if (NULL != shader) { 2607 if (NULL != shader) {
2575 shader->setLocalMatrix(savedLocalM); 2608 shader->setLocalMatrix(savedLocalM);
2576 } 2609 }
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 { 2610 } else {
2585 // no colors[] and no texture 2611 // no colors[] and no texture
2586 HairProc hairProc = ChooseHairProc(paint.isAntiAlias()); 2612 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
2587 const SkRasterClip& clip = *fRC; 2613 const SkRasterClip& clip = *fRC;
2588 while (vertProc(&state)) { 2614 while (vertProc(&state)) {
2589 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get() ); 2615 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get() );
2590 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get() ); 2616 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get() );
2591 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get() ); 2617 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get() );
2592 } 2618 }
2593 } 2619 }
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
2805 mask->fImage = SkMask::AllocImage(size); 2831 mask->fImage = SkMask::AllocImage(size);
2806 memset(mask->fImage, 0, mask->computeImageSize()); 2832 memset(mask->fImage, 0, mask->computeImageSize());
2807 } 2833 }
2808 2834
2809 if (SkMask::kJustComputeBounds_CreateMode != mode) { 2835 if (SkMask::kJustComputeBounds_CreateMode != mode) {
2810 draw_into_mask(*mask, devPath, style); 2836 draw_into_mask(*mask, devPath, style);
2811 } 2837 }
2812 2838
2813 return true; 2839 return true;
2814 } 2840 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698