SamplePipeControllers.h revision 80bacfeb4bda06541e8695bd502229727bccfea
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkBitmap.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkChunkAlloc.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkGPipe.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTDArray.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkCanvas;
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkMatrix;
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass PipeController : public SkGPipeController {
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    PipeController(SkCanvas* target);
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~PipeController();
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const void* getData() { return (const char*) fBlock + fBytesWritten; }
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkGPipeReader fReader;
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void* fBlock;
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    size_t fBlockSize;
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    size_t fBytesWritten;
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkGPipeReader::Status fStatus;
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass TiledPipeController : public PipeController {
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    TiledPipeController(const SkBitmap&, const SkMatrix* initialMatrix = NULL);
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~TiledPipeController() {};
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual int numberOfReaders() const SK_OVERRIDE { return NumberOfTiles; }
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        NumberOfTiles = 10
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkGPipeReader fReaders[NumberOfTiles - 1];
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkBitmap fBitmaps[NumberOfTiles];
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef PipeController INHERITED;
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Borrowed (and modified) from SkDeferredCanvas.cpp::DeferredPipeController.
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Allows playing back from multiple threads, but does not do the threading itself.
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass ThreadSafePipeController : public SkGPipeController {
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ThreadSafePipeController(int numberOfReaders);
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual int numberOfReaders() const SK_OVERRIDE { return fNumberOfReaders; }
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Play the stored drawing commands to the specified canvas. If SkGPipeWriter::startRecording
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * used the flag SkGPipeWriter::kSimultaneousReaders_Flag, this can be called from different
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * threads simultaneously.
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void draw(SkCanvas*);
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum {
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kMinBlockSize = 4096
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct PipeBlock {
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        PipeBlock(void* block, size_t bytes) { fBlock = block, fBytes = bytes; }
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Stream of draw commands written by the SkGPipeWriter. Allocated by fAllocator, which will
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // handle freeing it.
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        void* fBlock;
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Number of bytes that were written to fBlock.
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        size_t fBytes;
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void* fBlock;
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    size_t fBytesWritten;
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkChunkAlloc fAllocator;
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDArray<PipeBlock> fBlockList;
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int fNumberOfReaders;
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
86