2014-08-21 20:12:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-11-04 16:11:07 +00:00
|
|
|
// Need to include something before #if SK_SUPPORT_GPU so that the Android
|
|
|
|
// framework build, which gets its defines from SkTypes rather than a makefile,
|
|
|
|
// has the definition before checking it.
|
2014-08-21 20:12:42 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-11-24 17:49:17 +00:00
|
|
|
#include "SkCanvasPriv.h"
|
2014-08-21 20:12:42 +00:00
|
|
|
#include "SkMultiPictureDraw.h"
|
|
|
|
#include "SkPicture.h"
|
2014-10-29 19:36:45 +00:00
|
|
|
#include "SkTaskGroup.h"
|
|
|
|
|
2014-11-04 16:11:07 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-12-03 15:33:57 +00:00
|
|
|
#include "GrContext.h"
|
2014-11-04 16:11:07 +00:00
|
|
|
#include "GrLayerHoister.h"
|
|
|
|
#include "GrRecordReplaceDraw.h"
|
2014-11-10 16:10:42 +00:00
|
|
|
#include "GrRenderTarget.h"
|
2014-11-04 16:11:07 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
void SkMultiPictureDraw::DrawData::draw() {
|
|
|
|
fCanvas->drawPicture(fPicture, &fMatrix, fPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkMultiPictureDraw::DrawData::init(SkCanvas* canvas, const SkPicture* picture,
|
|
|
|
const SkMatrix* matrix, const SkPaint* paint) {
|
|
|
|
fPicture = SkRef(picture);
|
|
|
|
fCanvas = SkRef(canvas);
|
|
|
|
if (matrix) {
|
|
|
|
fMatrix = *matrix;
|
|
|
|
} else {
|
|
|
|
fMatrix.setIdentity();
|
|
|
|
}
|
|
|
|
if (paint) {
|
2015-08-26 20:07:48 +00:00
|
|
|
fPaint = new SkPaint(*paint);
|
2014-10-29 19:36:45 +00:00
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
fPaint = nullptr;
|
2014-10-29 19:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkMultiPictureDraw::DrawData::Reset(SkTDArray<DrawData>& data) {
|
|
|
|
for (int i = 0; i < data.count(); ++i) {
|
|
|
|
data[i].fPicture->unref();
|
|
|
|
data[i].fCanvas->unref();
|
2015-08-26 20:07:48 +00:00
|
|
|
delete data[i].fPaint;
|
2014-10-29 19:36:45 +00:00
|
|
|
}
|
|
|
|
data.rewind();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
2014-08-21 20:12:42 +00:00
|
|
|
|
|
|
|
SkMultiPictureDraw::SkMultiPictureDraw(int reserve) {
|
|
|
|
if (reserve > 0) {
|
2014-10-29 19:36:45 +00:00
|
|
|
fGPUDrawData.setReserve(reserve);
|
|
|
|
fThreadSafeDrawData.setReserve(reserve);
|
2014-08-21 20:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkMultiPictureDraw::reset() {
|
2014-10-29 19:36:45 +00:00
|
|
|
DrawData::Reset(fGPUDrawData);
|
|
|
|
DrawData::Reset(fThreadSafeDrawData);
|
2014-08-21 20:12:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 21:17:13 +00:00
|
|
|
void SkMultiPictureDraw::add(SkCanvas* canvas,
|
2014-08-21 20:12:42 +00:00
|
|
|
const SkPicture* picture,
|
2014-10-29 21:17:13 +00:00
|
|
|
const SkMatrix* matrix,
|
2014-08-21 20:12:42 +00:00
|
|
|
const SkPaint* paint) {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == canvas || nullptr == picture) {
|
|
|
|
SkDEBUGFAIL("parameters to SkMultiPictureDraw::add should be non-nullptr");
|
2014-08-21 20:12:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
SkTDArray<DrawData>& array = canvas->getGrContext() ? fGPUDrawData : fThreadSafeDrawData;
|
|
|
|
array.append()->init(canvas, picture, matrix, paint);
|
2014-08-21 20:12:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
class AutoMPDReset : SkNoncopyable {
|
|
|
|
SkMultiPictureDraw* fMPD;
|
|
|
|
public:
|
|
|
|
AutoMPDReset(SkMultiPictureDraw* mpd) : fMPD(mpd) {}
|
|
|
|
~AutoMPDReset() { fMPD->reset(); }
|
|
|
|
};
|
|
|
|
|
2014-12-25 21:55:08 +00:00
|
|
|
//#define FORCE_SINGLE_THREAD_DRAWING_FOR_TESTING
|
|
|
|
|
2015-01-28 19:01:06 +00:00
|
|
|
void SkMultiPictureDraw::draw(bool flush) {
|
2014-10-29 19:36:45 +00:00
|
|
|
AutoMPDReset mpdreset(this);
|
2014-12-25 21:55:08 +00:00
|
|
|
|
|
|
|
#ifdef FORCE_SINGLE_THREAD_DRAWING_FOR_TESTING
|
|
|
|
for (int i = 0; i < fThreadSafeDrawData.count(); ++i) {
|
2015-06-17 22:26:15 +00:00
|
|
|
fThreadSafeDrawData[i].draw();
|
2014-12-25 21:55:08 +00:00
|
|
|
}
|
|
|
|
#else
|
2015-06-17 22:26:15 +00:00
|
|
|
sk_parallel_for(fThreadSafeDrawData.count(), [&](int i) {
|
|
|
|
fThreadSafeDrawData[i].draw();
|
|
|
|
});
|
2014-12-25 21:55:08 +00:00
|
|
|
#endif
|
2015-06-17 22:26:15 +00:00
|
|
|
|
|
|
|
// N.B. we could get going on any GPU work from this main thread while the CPU work runs.
|
|
|
|
// But in practice, we've either got GPU work or CPU work, not both.
|
2014-10-29 19:36:45 +00:00
|
|
|
|
|
|
|
const int count = fGPUDrawData.count();
|
|
|
|
if (0 == count) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-08 12:17:02 +00:00
|
|
|
|
2014-11-03 15:19:30 +00:00
|
|
|
#if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
|
2014-10-29 19:36:45 +00:00
|
|
|
GrContext* context = fGPUDrawData[0].fCanvas->getGrContext();
|
|
|
|
SkASSERT(context);
|
2014-10-08 12:17:02 +00:00
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
// Start by collecting all the layers that are going to be atlased and render
|
2014-10-28 14:21:44 +00:00
|
|
|
// them (if necessary). Hoisting the free floating layers is deferred until
|
|
|
|
// drawing the canvas that requires them.
|
|
|
|
SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
|
2014-10-08 12:17:02 +00:00
|
|
|
|
2015-11-09 21:51:06 +00:00
|
|
|
GrLayerHoister::Begin(context);
|
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
const DrawData& data = fGPUDrawData[i];
|
|
|
|
// we only expect 1 context for all the canvases
|
2014-10-30 18:49:37 +00:00
|
|
|
SkASSERT(data.fCanvas->getGrContext() == context);
|
2014-10-08 12:17:02 +00:00
|
|
|
|
2014-11-24 17:49:17 +00:00
|
|
|
if (!data.fPaint) {
|
2014-10-08 12:17:02 +00:00
|
|
|
SkRect clipBounds;
|
2014-10-29 19:36:45 +00:00
|
|
|
if (!data.fCanvas->getClipBounds(&clipBounds)) {
|
2014-10-08 12:17:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:49:17 +00:00
|
|
|
SkMatrix initialMatrix = data.fCanvas->getTotalMatrix();
|
|
|
|
initialMatrix.preConcat(data.fMatrix);
|
|
|
|
|
2014-11-10 16:10:42 +00:00
|
|
|
GrRenderTarget* rt = data.fCanvas->internal_private_accessTopLayerRenderTarget();
|
|
|
|
SkASSERT(rt);
|
|
|
|
|
2014-10-28 14:21:44 +00:00
|
|
|
// TODO: sorting the cacheable layers from smallest to largest
|
|
|
|
// would improve the packing and reduce the number of swaps
|
|
|
|
// TODO: another optimization would be to make a first pass to
|
|
|
|
// lock any required layer that is already in the atlas
|
2014-11-24 17:49:17 +00:00
|
|
|
GrLayerHoister::FindLayersToAtlas(context, data.fPicture, initialMatrix,
|
2014-10-29 21:17:13 +00:00
|
|
|
clipBounds,
|
2014-11-10 16:10:42 +00:00
|
|
|
&atlasedNeedRendering, &atlasedRecycled,
|
2015-06-12 15:59:45 +00:00
|
|
|
rt->numColorSamples());
|
2014-10-08 12:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
GrLayerHoister::DrawLayersToAtlas(context, atlasedNeedRendering);
|
2014-10-28 14:21:44 +00:00
|
|
|
|
|
|
|
SkTDArray<GrHoistedLayer> needRendering, recycled;
|
2014-10-08 12:17:02 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-29 19:36:45 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
const DrawData& data = fGPUDrawData[i];
|
|
|
|
SkCanvas* canvas = data.fCanvas;
|
|
|
|
const SkPicture* picture = data.fPicture;
|
|
|
|
|
2014-11-03 15:19:30 +00:00
|
|
|
#if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
|
2014-11-24 17:49:17 +00:00
|
|
|
if (!data.fPaint) {
|
2014-10-28 14:21:44 +00:00
|
|
|
|
|
|
|
SkRect clipBounds;
|
2014-10-29 19:36:45 +00:00
|
|
|
if (!canvas->getClipBounds(&clipBounds)) {
|
2014-10-28 14:21:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:49:17 +00:00
|
|
|
SkAutoCanvasMatrixPaint acmp(canvas, &data.fMatrix, data.fPaint, picture->cullRect());
|
|
|
|
|
|
|
|
const SkMatrix initialMatrix = canvas->getTotalMatrix();
|
|
|
|
|
2014-11-10 16:10:42 +00:00
|
|
|
GrRenderTarget* rt = data.fCanvas->internal_private_accessTopLayerRenderTarget();
|
|
|
|
SkASSERT(rt);
|
|
|
|
|
2014-10-28 14:21:44 +00:00
|
|
|
// Find the layers required by this canvas. It will return atlased
|
|
|
|
// layers in the 'recycled' list since they have already been drawn.
|
2014-11-24 17:49:17 +00:00
|
|
|
GrLayerHoister::FindLayersToHoist(context, picture, initialMatrix,
|
2014-11-10 16:10:42 +00:00
|
|
|
clipBounds, &needRendering, &recycled,
|
2015-06-12 15:59:45 +00:00
|
|
|
rt->numColorSamples());
|
2014-10-28 14:21:44 +00:00
|
|
|
|
|
|
|
GrLayerHoister::DrawLayers(context, needRendering);
|
|
|
|
|
|
|
|
// Render the entire picture using new layers
|
2014-12-03 15:33:57 +00:00
|
|
|
GrRecordReplaceDraw(picture, canvas, context->getLayerCache(),
|
2015-08-27 14:41:13 +00:00
|
|
|
initialMatrix, nullptr);
|
2014-10-28 14:21:44 +00:00
|
|
|
|
|
|
|
GrLayerHoister::UnlockLayers(context, needRendering);
|
|
|
|
GrLayerHoister::UnlockLayers(context, recycled);
|
|
|
|
|
|
|
|
needRendering.rewind();
|
|
|
|
recycled.rewind();
|
|
|
|
} else
|
2014-10-08 12:17:02 +00:00
|
|
|
#endif
|
|
|
|
{
|
2014-10-29 19:36:45 +00:00
|
|
|
canvas->drawPicture(picture, &data.fMatrix, data.fPaint);
|
2014-10-08 12:17:02 +00:00
|
|
|
}
|
2015-01-28 19:01:06 +00:00
|
|
|
if (flush) {
|
|
|
|
canvas->flush();
|
|
|
|
}
|
2014-10-08 12:17:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 15:19:30 +00:00
|
|
|
#if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
|
2014-10-29 19:36:45 +00:00
|
|
|
GrLayerHoister::UnlockLayers(context, atlasedNeedRendering);
|
|
|
|
GrLayerHoister::UnlockLayers(context, atlasedRecycled);
|
2015-11-09 21:51:06 +00:00
|
|
|
GrLayerHoister::End(context);
|
2014-10-08 12:17:02 +00:00
|
|
|
#endif
|
2014-08-21 20:12:42 +00:00
|
|
|
}
|
|
|
|
|