Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2014 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "CanvasStateHelpers.h"
      9 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
     10 #include "SkCanvas.h"
     11 #include "SkCanvasStateUtils.h"
     12 #include "SkPaint.h"
     13 #include "SkRect.h"
     14 #include "SkRegion.h"
     15 
     16 void complex_layers_draw(SkCanvas* canvas, float left, float top,
     17                          float right, float bottom, int32_t spacer) {
     18     SkPaint bluePaint;
     19     bluePaint.setColor(SK_ColorBLUE);
     20     bluePaint.setStyle(SkPaint::kFill_Style);
     21 
     22     SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
     23     canvas->drawRect(rect, bluePaint);
     24     canvas->translate(0, rect.height() + spacer);
     25     canvas->drawRect(rect, bluePaint);
     26 }
     27 
     28 extern "C" bool complex_layers_draw_from_canvas_state(SkCanvasState* state,
     29         float left, float top, float right, float bottom, int32_t spacer) {
     30     std::unique_ptr<SkCanvas> canvas = SkCanvasStateUtils::MakeFromCanvasState(state);
     31     if (!canvas) {
     32         return false;
     33     }
     34     complex_layers_draw(canvas.get(), left, top, right, bottom, spacer);
     35     return true;
     36 }
     37 
     38 void complex_clips_draw(SkCanvas* canvas, int32_t left, int32_t top,
     39         int32_t right, int32_t bottom, int32_t clipOp, const SkRegion& localRegion) {
     40     canvas->save();
     41     SkRect clipRect = SkRect::MakeLTRB(SkIntToScalar(left), SkIntToScalar(top),
     42                                        SkIntToScalar(right), SkIntToScalar(bottom));
     43     canvas->clipRect(clipRect, (SkRegion::Op) clipOp);
     44     canvas->drawColor(SK_ColorBLUE);
     45     canvas->restore();
     46 
     47     canvas->clipRegion(localRegion, (SkClipOp) clipOp);
     48     canvas->drawColor(SK_ColorBLUE);
     49 }
     50 
     51 extern "C" bool complex_clips_draw_from_canvas_state(SkCanvasState* state,
     52         int32_t left, int32_t top, int32_t right, int32_t bottom, int32_t clipOp,
     53         int32_t regionRects, int32_t* rectCoords) {
     54     std::unique_ptr<SkCanvas> canvas = SkCanvasStateUtils::MakeFromCanvasState(state);
     55     if (!canvas) {
     56         return false;
     57     }
     58 
     59     SkRegion localRegion;
     60     for (int32_t i = 0; i < regionRects; ++i) {
     61         localRegion.op(rectCoords[0], rectCoords[1], rectCoords[2], rectCoords[3],
     62                        SkRegion::kUnion_Op);
     63         rectCoords += 4;
     64     }
     65 
     66     complex_clips_draw(canvas.get(), left, top, right, bottom, clipOp, localRegion);
     67     return true;
     68 }
     69 #endif // SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
     70