17eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips/*
27eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips * Copyright 2014 Google Inc.
37eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips *
47eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips * Use of this source code is governed by a BSD-style license that can be
57eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips * found in the LICENSE file.
67eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips */
77eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
87eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips#ifndef SkMultiPictureDraw_DEFINED
97eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips#define SkMultiPictureDraw_DEFINED
107eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
117eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips#include "SkMatrix.h"
127eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips#include "SkTDArray.h"
137eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
147eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillipsclass SkCanvas;
157eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillipsclass SkPaint;
167eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillipsclass SkPicture;
177eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
187eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips/** \class SkMultiPictureDraw
197eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
207eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    The MultiPictureDraw object accepts several picture/canvas pairs and
217eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    then attempts to optimally draw the pictures into the canvases, sharing
227eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    as many resources as possible.
237eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips*/
247eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillipsclass SK_API SkMultiPictureDraw {
257eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillipspublic:
267eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    /**
277eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  Create an object to optimize the drawing of multiple pictures.
287eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  @param reserve Hint for the number of add calls expected to be issued
297eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     */
307eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    SkMultiPictureDraw(int reserve = 0);
317eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    ~SkMultiPictureDraw() { this->reset(); }
327eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
337eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    /**
347eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  Add a canvas/picture pair for later rendering.
357eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  @param canvas   the canvas in which to draw picture
367eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  @param picture  the picture to draw into canvas
377eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  @param matrix   if non-NULL, applied to the CTM when drawing
387eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  @param paint    if non-NULL, draw picture to a temporary buffer
397eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *                  and then apply the paint when the result is drawn
407eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     */
417eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    void add(SkCanvas* canvas,
427eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips             const SkPicture* picture,
43e71cd54ed4d83310d718490d40643c35b622b9f5mtklein             const SkMatrix* matrix = NULL,
447eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips             const SkPaint* paint = NULL);
457eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
467eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    /**
477eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  Perform all the previously added draws. This will reset the state
48772604c214e8c12ee16d2eb60f4b7acbcdd2129esenorblanco     *  of this object. If flush is true, all canvases are flushed after
49772604c214e8c12ee16d2eb60f4b7acbcdd2129esenorblanco     *  draw.
507eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     */
51772604c214e8c12ee16d2eb60f4b7acbcdd2129esenorblanco    void draw(bool flush = false);
527eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
537eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    /**
547eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     *  Abandon all buffered draws and reset to the initial state.
557eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips     */
567eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    void reset();
577eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
587eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillipsprivate:
597eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    struct DrawData {
6089889b69391a730f0ba2a1efb549864b7762263freed        SkCanvas*        fCanvas;  // reffed
6189889b69391a730f0ba2a1efb549864b7762263freed        const SkPicture* fPicture; // reffed
6289889b69391a730f0ba2a1efb549864b7762263freed        SkMatrix         fMatrix;
6389889b69391a730f0ba2a1efb549864b7762263freed        SkPaint*         fPaint;   // owned
6489889b69391a730f0ba2a1efb549864b7762263freed
6589889b69391a730f0ba2a1efb549864b7762263freed        void init(SkCanvas*, const SkPicture*, const SkMatrix*, const SkPaint*);
6689889b69391a730f0ba2a1efb549864b7762263freed        void draw();
6789889b69391a730f0ba2a1efb549864b7762263freed
6889889b69391a730f0ba2a1efb549864b7762263freed        static void Reset(SkTDArray<DrawData>&);
6989889b69391a730f0ba2a1efb549864b7762263freed
70e71cd54ed4d83310d718490d40643c35b622b9f5mtklein        static void Draw(DrawData* d) { d->draw(); }
717eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips    };
727eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
7389889b69391a730f0ba2a1efb549864b7762263freed    SkTDArray<DrawData> fThreadSafeDrawData;
7489889b69391a730f0ba2a1efb549864b7762263freed    SkTDArray<DrawData> fGPUDrawData;
757eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips};
767eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips
777eacd77ce63abec6c5a0e7be9bf9f40ea4145d11robertphillips#endif
78