1/*
2 * Copyright (C) 2009, 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#ifndef RenderQueue_h
20#define RenderQueue_h
21
22#include "TileIndex.h"
23
24#include <BlackBerryPlatformIntRectRegion.h>
25#include <BlackBerryPlatformPrimitives.h>
26
27namespace BlackBerry {
28namespace WebKit {
29
30class BackingStorePrivate;
31class BackingStoreGeometry;
32
33enum SortDirection {
34    LeftToRight = 0,
35    RightToLeft,
36    TopToBottom,
37    BottomToTop,
38    NumSortDirections
39};
40
41class RenderQueue {
42public:
43    enum JobType { VisibleZoom, VisibleScroll, RegularRender, NonVisibleScroll };
44
45    enum ClearJobsFlags {
46        ClearRegularRenderJobs = 1 << 0,
47        ClearIncompleteScrollJobs = 1 << 1,
48        ClearIncompleteZoomJobs = 1 << 2,
49        ClearCompletedJobs = 1 << 3,
50
51        ClearAnyJobs = 0xFFFFFFFF,
52
53        DontClearRegularRenderJobs = ClearAnyJobs & ~ClearRegularRenderJobs,
54        DontClearCompletedJobs = ClearAnyJobs & ~ClearCompletedJobs,
55    };
56
57    RenderQueue(BackingStorePrivate*);
58
59    void reset();
60
61    bool isEmpty(bool shouldPerformRegularRenderJobs = true) const;
62
63    bool hasCurrentRegularRenderJob() const;
64    bool hasCurrentVisibleZoomJob() const;
65    bool hasCurrentVisibleScrollJob() const;
66    bool isCurrentVisibleZoomJob(const TileIndex&) const;
67    bool isCurrentVisibleZoomJobCompleted(const TileIndex&) const;
68    bool isCurrentVisibleScrollJob(const TileIndex&) const;
69    bool isCurrentVisibleScrollJobCompleted(const TileIndex&) const;
70    bool isCurrentRegularRenderJob(const TileIndex&, BackingStoreGeometry*) const;
71
72    bool currentRegularRenderJobBatchUnderPressure() const;
73    void setCurrentRegularRenderJobBatchUnderPressure(bool);
74
75    void eventQueueCycled();
76
77    void addToQueue(JobType, const Platform::IntRectRegion&);
78
79    void updateSortDirection(int lastDeltaX, int lastDeltaY);
80    void visibleContentChanged(const Platform::IntRect&);
81    void backingStoreRectChanging(const Platform::IntRect& oldRect, const Platform::IntRect& newRect);
82    void clear(const TileIndexList&, BackingStoreGeometry*, ClearJobsFlags);
83    void clear(const Platform::IntRectRegion&, ClearJobsFlags);
84    bool regularRenderJobsPreviouslyAttemptedButNotRendered(const Platform::IntRect&);
85    Platform::IntRectRegion regularRenderJobsNotRenderedRegion() const { return m_regularRenderJobsNotRenderedRegion; }
86
87    void render(bool shouldPerformRegularRenderJobs = true);
88
89private:
90    TileIndexList tileIndexesIntersectingRegion(const Platform::IntRectRegion&, BackingStoreGeometry*) const;
91    TileIndexList tileIndexesFullyContainedInRegion(const Platform::IntRectRegion&, BackingStoreGeometry*) const;
92    Platform::IntRectRegion tileRegion(const TileIndexList&, BackingStoreGeometry*) const;
93
94    void clearRegions(const Platform::IntRectRegion&, ClearJobsFlags);
95    void clearTileIndexes(const TileIndexList&, ClearJobsFlags);
96
97    // Render items from the queue.
98    void renderRegularRenderJobs(bool allAtOnceIfPossible);
99    void renderScrollZoomJobs(TileIndexList* outstandingJobs, TileIndexList* completedJobs, bool allAtOnceIfPossible, bool shouldBlitWhenCompleted);
100    void scrollZoomJobsCompleted(const TileIndexList& outstandingJobs, TileIndexList* completedJobs, bool shouldBlit);
101
102    // Internal method to add to the various queues.
103    void addToRegularQueue(const Platform::IntRectRegion&);
104    void addToScrollZoomQueue(const TileIndexList&, TileIndexList* queue);
105    void quickSort(TileIndexList*);
106
107    BackingStorePrivate* m_parent;
108
109    // The highest priority queue.
110    TileIndexList m_visibleZoomJobs;
111    TileIndexList m_visibleZoomJobsCompleted;
112    TileIndexList m_visibleScrollJobs;
113    TileIndexList m_visibleScrollJobsCompleted;
114    // The lowest priority queue.
115    TileIndexList m_nonVisibleScrollJobs;
116    TileIndexList m_nonVisibleScrollJobsCompleted;
117    // The regular render jobs are in the middle.
118    Platform::IntRectRegion m_regularRenderJobsRegion;
119    TileIndexList m_currentRegularRenderJobsBatch;
120    Platform::IntRectRegion m_currentRegularRenderJobsBatchRegion;
121    bool m_rectsAddedToRegularRenderJobsInCurrentCycle;
122    bool m_currentRegularRenderJobsBatchUnderPressure;
123
124    // Holds the region of the page that we attempt to render, but the
125    // backingstore was not in the right place at the time. This will
126    // be checked before we try to restore a tile to it's last rendered
127    // place.
128    Platform::IntRectRegion m_regularRenderJobsNotRenderedRegion;
129
130    SortDirection m_primarySortDirection;
131    SortDirection m_secondarySortDirection;
132};
133
134} // namespace WebKit
135} // namespace BlackBerry
136
137#endif // RenderQueue_h
138