aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/PictureRenderer.cpp
blob: 85bd194bbe2d9fbcd6dd18504ba6176719128afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "PictureRenderer.h"
#include "picture_utils.h"
#include "SamplePipeControllers.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkGPipe.h"
#if SK_SUPPORT_GPU
#include "SkGpuDevice.h"
#endif
#include "SkGraphics.h"
#include "SkImageEncoder.h"
#include "SkMatrix.h"
#include "SkPicture.h"
#include "SkScalar.h"
#include "SkString.h"
#include "SkTemplates.h"
#include "SkTDArray.h"
#include "SkThreadUtils.h"
#include "SkTypes.h"

namespace sk_tools {

enum {
    kDefaultTileWidth = 256,
    kDefaultTileHeight = 256
};

void PictureRenderer::init(SkPicture* pict) {
    SkASSERT(NULL == fPicture);
    SkASSERT(NULL == fCanvas.get());
    if (fPicture != NULL || NULL != fCanvas.get()) {
        return;
    }

    SkASSERT(pict != NULL);
    if (NULL == pict) {
        return;
    }

    fPicture = pict;
    fCanvas.reset(this->setupCanvas());
}

SkCanvas* PictureRenderer::setupCanvas() {
    return this->setupCanvas(fPicture->width(), fPicture->height());
}

SkCanvas* PictureRenderer::setupCanvas(int width, int height) {
    switch(fDeviceType) {
        case kBitmap_DeviceType: {
            SkBitmap bitmap;
            sk_tools::setup_bitmap(&bitmap, width, height);
            return SkNEW_ARGS(SkCanvas, (bitmap));
            break;
        }
#if SK_SUPPORT_GPU
        case kGPU_DeviceType: {
            SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice,
                                                    (fGrContext, SkBitmap::kARGB_8888_Config,
                                                    width, height)));
            return SkNEW_ARGS(SkCanvas, (device.get()));
            break;
        }
#endif
        default:
            SkASSERT(0);
    }

    return NULL;
}

void PictureRenderer::end() {
    this->resetState();
    fPicture = NULL;
    fCanvas.reset(NULL);
}

void PictureRenderer::resetState() {
#if SK_SUPPORT_GPU
    if (this->isUsingGpuDevice()) {
        SkGLContext* glContext = fGrContextFactory.getGLContext(
            GrContextFactory::kNative_GLContextType);

        SkASSERT(glContext != NULL);
        if (NULL == glContext) {
            return;
        }

        fGrContext->flush();
        SK_GL(*glContext, Finish());
    }
#endif
}

bool PictureRenderer::write(SkCanvas* canvas, SkString path) const {
    SkASSERT(canvas != NULL);
    SkASSERT(fPicture != NULL);
    if (NULL == canvas || NULL == fPicture) {
        return false;
    }

    SkBitmap bitmap;
    SkISize size = canvas->getDeviceSize();
    sk_tools::setup_bitmap(&bitmap, size.width(), size.height());

    canvas->readPixels(&bitmap, 0, 0);
    sk_tools::force_all_opaque(bitmap);

    // Since path is passed in by value, it is okay to modify it.
    path.append(".png");
    return SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
}

///////////////////////////////////////////////////////////////////////////////////////////////

bool RecordPictureRenderer::render(const SkString*) {
    SkPicture replayer;
    SkCanvas* recorder = replayer.beginRecording(fPicture->width(), fPicture->height());
    fPicture->draw(recorder);
    replayer.endRecording();
    // Since this class does not actually render, return false.
    return false;
}

///////////////////////////////////////////////////////////////////////////////////////////////

bool PipePictureRenderer::render(const SkString* path) {
    SkASSERT(fCanvas.get() != NULL);
    SkASSERT(fPicture != NULL);
    if (NULL == fCanvas.get() || NULL == fPicture) {
        return false;
    }

    PipeController pipeController(fCanvas.get());
    SkGPipeWriter writer;
    SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
    pipeCanvas->drawPicture(*fPicture);
    writer.endRecording();
    fCanvas->flush();
    return path != NULL && this->write(fCanvas, *path);
}

///////////////////////////////////////////////////////////////////////////////////////////////

bool SimplePictureRenderer::render(const SkString* path) {
    SkASSERT(fCanvas.get() != NULL);
    SkASSERT(fPicture != NULL);
    if (NULL == fCanvas.get() || NULL == fPicture) {
        return false;
    }

    fCanvas->drawPicture(*fPicture);
    fCanvas->flush();
    return path != NULL && this->write(fCanvas, *path);
}

///////////////////////////////////////////////////////////////////////////////////////////////

TiledPictureRenderer::TiledPictureRenderer()
    : fUsePipe(false)
    , fTileWidth(kDefaultTileWidth)
    , fTileHeight(kDefaultTileHeight)
    , fTileWidthPercentage(0.0)
    , fTileHeightPercentage(0.0)
    , fTileMinPowerOf2Width(0)
    , fTileCounter(0)
    , fNumThreads(1)
    , fPictureClones(NULL)
    , fPipeController(NULL) { }

void TiledPictureRenderer::init(SkPicture* pict) {
    SkASSERT(pict != NULL);
    SkASSERT(0 == fTileRects.count());
    if (NULL == pict || fTileRects.count() != 0) {
        return;
    }

    // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
    // used by bench_pictures.
    fPicture = pict;

    if (fTileWidthPercentage > 0) {
        fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100));
    }
    if (fTileHeightPercentage > 0) {
        fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100));
    }

    if (fTileMinPowerOf2Width > 0) {
        this->setupPowerOf2Tiles();
    } else {
        this->setupTiles();
    }

    if (this->multiThreaded()) {
        for (int i = 0; i < fNumThreads; ++i) {
            *fCanvasPool.append() = this->setupCanvas(fTileWidth, fTileHeight);
        }
        if (!fUsePipe) {
            SkASSERT(NULL == fPictureClones);
            // Only need to create fNumThreads - 1 clones, since one thread will use the base
            // picture.
            int numberOfClones = fNumThreads - 1;
            // This will be deleted in end().
            fPictureClones = SkNEW_ARRAY(SkPicture, numberOfClones);
            fPictureClones->clone(fPictureClones, numberOfClones);
        }
    }
}

void TiledPictureRenderer::end() {
    fTileRects.reset();
    SkDELETE_ARRAY(fPictureClones);
    fPictureClones = NULL;
    fCanvasPool.unrefAll();
    if (fPipeController != NULL) {
        SkASSERT(fUsePipe);
        SkDELETE(fPipeController);
        fPipeController = NULL;
    }
    this->INHERITED::end();
}

TiledPictureRenderer::~TiledPictureRenderer() {
    // end() must be called to delete fPictureClones and fPipeController
    SkASSERT(NULL == fPictureClones);
    SkASSERT(NULL == fPipeController);
}

void TiledPictureRenderer::setupTiles() {
    for (int tile_y_start = 0; tile_y_start < fPicture->height(); tile_y_start += fTileHeight) {
        for (int tile_x_start = 0; tile_x_start < fPicture->width(); tile_x_start += fTileWidth) {
            *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
                                                    SkIntToScalar(tile_y_start),
                                                    SkIntToScalar(fTileWidth),
                                                    SkIntToScalar(fTileHeight));
        }
    }
}

// The goal of the powers of two tiles is to minimize the amount of wasted tile
// space in the width-wise direction and then minimize the number of tiles. The
// constraints are that every tile must have a pixel width that is a power of
// two and also be of some minimal width (that is also a power of two).
//
// This is solved by first taking our picture size and rounding it up to the
// multiple of the minimal width. The binary representation of this rounded
// value gives us the tiles we need: a bit of value one means we need a tile of
// that size.
void TiledPictureRenderer::setupPowerOf2Tiles() {
    int rounded_value = fPicture->width();
    if (fPicture->width() % fTileMinPowerOf2Width != 0) {
        rounded_value = fPicture->width() - (fPicture->width() % fTileMinPowerOf2Width)
            + fTileMinPowerOf2Width;
    }

    int num_bits = SkScalarCeilToInt(SkScalarLog2(SkIntToScalar(fPicture->width())));
    int largest_possible_tile_size = 1 << num_bits;

    // The tile height is constant for a particular picture.
    for (int tile_y_start = 0; tile_y_start < fPicture->height(); tile_y_start += fTileHeight) {
        int tile_x_start = 0;
        int current_width = largest_possible_tile_size;
        // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough
        // to draw each tile.
        fTileWidth = current_width;

        while (current_width >= fTileMinPowerOf2Width) {
            // It is very important this is a bitwise AND.
            if (current_width & rounded_value) {
                *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
                                                        SkIntToScalar(tile_y_start),
                                                        SkIntToScalar(current_width),
                                                        SkIntToScalar(fTileHeight));
                tile_x_start += current_width;
            }

            current_width >>= 1;
        }
    }
}

/**
 * Draw the specified playback to the canvas translated to rectangle provided, so that this mini
 * canvas represents the rectangle's portion of the overall picture.
 * Saves and restores so that the initial clip and matrix return to their state before this function
 * is called.
 */
template<class T>
static void DrawTileToCanvas(SkCanvas* canvas, const SkRect& tileRect, T* playback) {
    int saveCount = canvas->save();
    // Translate so that we draw the correct portion of the picture
    canvas->translate(-tileRect.fLeft, -tileRect.fTop);
    playback->draw(canvas);
    canvas->restoreToCount(saveCount);
    canvas->flush();
}

///////////////////////////////////////////////////////////////////////////////////////////////
// Base class for data used both by pipe and clone picture multi threaded drawing.

struct ThreadData {
    ThreadData(SkCanvas* target, int* tileCounter, SkTDArray<SkRect>* tileRects)
    : fCanvas(target)
    , fTileCounter(tileCounter)
    , fTileRects(tileRects) {
        SkASSERT(target != NULL && tileCounter != NULL && tileRects != NULL);
    }

    const SkRect* nextTile() {
        if (int32_t i = sk_atomic_inc(fTileCounter) < fTileRects->count()) {
            return &fTileRects->operator[](i);
        }
        return NULL;
    }

    // All of these are pointers to objects owned elsewhere
    SkCanvas*                fCanvas;
private:
    // Shared by all threads, this states which is the next tile to be drawn.
    int32_t*                 fTileCounter;
    // Points to the array of rectangles. The array is already created before any threads are
    // started and then it is unmodified, so there is no danger of race conditions.
    const SkTDArray<SkRect>* fTileRects;
};

///////////////////////////////////////////////////////////////////////////////////////////////
// Draw using Pipe

struct TileData : public ThreadData {
    TileData(ThreadSafePipeController* controller, SkCanvas* canvas, int* tileCounter,
             SkTDArray<SkRect>* tileRects)
    : INHERITED(canvas, tileCounter, tileRects)
    , fController(controller) {}

    ThreadSafePipeController* fController;

    typedef ThreadData INHERITED;
};

static void DrawTile(void* data) {
    SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024);
    TileData* tileData = static_cast<TileData*>(data);

    const SkRect* tileRect;
    while ((tileRect = tileData->nextTile()) != NULL) {
        DrawTileToCanvas(tileData->fCanvas, *tileRect, tileData->fController);
    }
    SkDELETE(tileData);
}

///////////////////////////////////////////////////////////////////////////////////////////////
// Draw using Picture

struct CloneData : public ThreadData {
    CloneData(SkPicture* clone, SkCanvas* target, int* tileCounter, SkTDArray<SkRect>* tileRects)
    : INHERITED(target, tileCounter, tileRects)
    , fClone(clone) {}

    SkPicture* fClone;

    typedef ThreadData INHERITED;
};

static void DrawClonedTiles(void* data) {
    SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024);
    CloneData* cloneData = static_cast<CloneData*>(data);

    const SkRect* tileRect;
    while ((tileRect = cloneData->nextTile()) != NULL) {
        DrawTileToCanvas(cloneData->fCanvas, *tileRect, cloneData->fClone);
    }
    SkDELETE(cloneData);
}

///////////////////////////////////////////////////////////////////////////////////////////////

void TiledPictureRenderer::setup() {
    if (this->multiThreaded()) {
        // Reset to zero so we start with the first tile.
        fTileCounter = 0;
        if (fUsePipe) {
            // Record the picture into the pipe controller. It is done here because unlike
            // SkPicture, the pipe is modified (bitmaps can be removed) by drawing.
            // fPipeController is deleted here after each call to render() except the last one and
            // in end() for the last one.
            if (fPipeController != NULL) {
                SkDELETE(fPipeController);
            }
            fPipeController = SkNEW_ARGS(ThreadSafePipeController, (fTileRects.count()));
            SkGPipeWriter writer;
            SkCanvas* pipeCanvas = writer.startRecording(fPipeController,
                                                         SkGPipeWriter::kSimultaneousReaders_Flag);
            SkASSERT(fPicture != NULL);
            fPicture->draw(pipeCanvas);
            writer.endRecording();
        }
    }
}

bool TiledPictureRenderer::render(const SkString* path) {
    SkASSERT(fPicture != NULL);
    if (NULL == fPicture) {
        return false;
    }

    if (this->multiThreaded()) {
        SkASSERT(fCanvasPool.count() == fNumThreads);
        SkTDArray<SkThread*> threads;
        SkThread::entryPointProc proc = fUsePipe ? DrawTile : DrawClonedTiles;
        for (int i = 0; i < fNumThreads; ++i) {
            // data will be deleted by the entryPointProc.
            ThreadData* data;
            if (fUsePipe) {
                data = SkNEW_ARGS(TileData,
                                  (fPipeController, fCanvasPool[i], &fTileCounter, &fTileRects));
            } else {
                SkPicture* pic = (0 == i) ? fPicture : &fPictureClones[i-1];
                data = SkNEW_ARGS(CloneData, (pic, fCanvasPool[i], &fTileCounter, &fTileRects));
            }
            SkThread* thread = SkNEW_ARGS(SkThread, (proc, data));
            if (!thread->start()) {
                SkDebugf("Could not start %s thread %i.\n", (fUsePipe ? "pipe" : "picture"), i);
            }
            *threads.append() = thread;
        }
        SkASSERT(threads.count() == fNumThreads);
        for (int i = 0; i < fNumThreads; ++i) {
            SkThread* thread = threads[i];
            thread->join();
            SkDELETE(thread);
        }
        threads.reset();
        // Currently multithreaded is not an option for render_pictures
        return false;
    } else {
        // For single thread, we really only need one canvas total.
        SkCanvas* canvas = this->setupCanvas(fTileWidth, fTileHeight);
        SkAutoUnref aur(canvas);

        for (int i = 0; i < fTileRects.count(); ++i) {
            DrawTileToCanvas(canvas, fTileRects[i], fPicture);
            if (path != NULL) {
                SkString tilePath(*path);
                tilePath.appendf("%i", i);
                if (!this->write(canvas, tilePath)) {
                    return false;
                }
            }
        }
        return path != NULL;
    }
}

SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
    SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
    SkASSERT(fPicture != NULL);
    // Clip the tile to an area that is completely in what the SkPicture says is the
    // drawn-to area. This is mostly important for tiles on the right and bottom edges
    // as they may go over this area and the picture may have some commands that
    // draw outside of this area and so should not actually be written.
    SkRect clip = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
                                 SkIntToScalar(fPicture->height()));
    canvas->clipRect(clip);
    return canvas;
}

///////////////////////////////////////////////////////////////////////////////////////////////

void PlaybackCreationRenderer::setup() {
    SkCanvas* recorder = fReplayer.beginRecording(fPicture->width(), fPicture->height());
    fPicture->draw(recorder);
}

bool PlaybackCreationRenderer::render(const SkString*) {
    fReplayer.endRecording();
    // Since this class does not actually render, return false.
    return false;
}

}