summaryrefslogtreecommitdiff
path: root/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitThread.java
blob: bc08b9882b0f4d719cdae89254d3bd9312d15f8c (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
package org.libreoffice;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

import org.mozilla.gecko.gfx.FloatSize;
import org.mozilla.gecko.gfx.GeckoLayerClient;
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
import org.mozilla.gecko.gfx.SubTile;
import org.mozilla.gecko.gfx.ViewportMetrics;

import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;

public class LOKitThread extends Thread {
    private static final String LOGTAG = LOKitThread.class.getSimpleName();

    private static final int TILE_SIZE = 256;
    public LinkedBlockingQueue<LOEvent> mEventQueue = new LinkedBlockingQueue<LOEvent>();
    private LibreOfficeMainActivity mApplication;
    private TileProvider mTileProvider;
    private ViewportMetrics mViewportMetrics;
    private String mInputFile;
    private Rect mOldRect;
    private boolean mCheckboardImageSet = false;

    LOKitThread(String inputFile) {
        mInputFile = inputFile;
    }

    RectF normlizeRect(ImmutableViewportMetrics metrics) {
        RectF rect = metrics.getViewport();
        float zoomFactor = metrics.zoomFactor;
        return new RectF(rect.left / zoomFactor, rect.top / zoomFactor, rect.right / zoomFactor, rect.bottom / zoomFactor);
    }

    Rect roundToTileSize(RectF input, int tileSize) {
        int minX = (Math.round(input.left)    / tileSize) * tileSize;
        int minY = (Math.round(input.top)     / tileSize) * tileSize;
        int maxX = ((Math.round(input.right)  / tileSize) + 1) * tileSize;
        int maxY = ((Math.round(input.bottom) / tileSize) + 1) * tileSize;
        return new Rect(minX, minY, maxX, maxY);
    }

    Rect inflate(Rect rect, int inflateSize) {
        Rect newRect = new Rect(rect);
        newRect.left -= inflateSize;
        newRect.left = newRect.left < 0 ? 0 : newRect.left;

        newRect.top -= inflateSize;
        newRect.top = newRect.top < 0 ? 0 : newRect.top;

        newRect.right += inflateSize;
        newRect.bottom += inflateSize;

        return newRect;
    }

    private boolean draw() throws InterruptedException {
        Log.i(LOGTAG, "tilerender draw");
        int pageWidth = mTileProvider.getPageWidth();
        int pageHeight = mTileProvider.getPageHeight();

        mViewportMetrics = new ViewportMetrics();
        FloatSize size = new FloatSize(pageWidth, pageHeight);
        mViewportMetrics.setPageSize(size, size);

        GeckoLayerClient layerClient = mApplication.getLayerClient();
        layerClient.beginDrawing(mViewportMetrics);

        ImmutableViewportMetrics metrics = mApplication.getLayerController().getViewportMetrics();
        RectF viewport = normlizeRect(metrics);
        Rect rect = inflate(roundToTileSize(viewport, TILE_SIZE), TILE_SIZE);

        mOldRect = rect;

        Log.i(LOGTAG, "tilerender rect: " + rect);

        long start = System.currentTimeMillis();

        ArrayList<SubTile> removeTiles = new ArrayList<SubTile>();
        for (SubTile tile : layerClient.getTiles()) {
            Rect tileRect = new Rect(tile.x, tile.y, tile.x + TILE_SIZE, tile.y + TILE_SIZE);
            if (!Rect.intersects(rect, tileRect)) {
                Log.i(LOGTAG, "tilerender delete " + tileRect);
                removeTiles.add(tile);
            }
        }
        Log.i(LOGTAG, "TileRendering Remove: " + (System.currentTimeMillis() - start));

        layerClient.getTiles().removeAll(removeTiles);

        Log.i(LOGTAG, "TileRendering Clear: " + (System.currentTimeMillis() - start));

        for (int y = rect.top; y < rect.bottom; y += TILE_SIZE) {
            for (int x = rect.left; x < rect.right; x += TILE_SIZE) {
                if (x > pageWidth) {
                    continue;
                }
                if (y > pageHeight) {
                    continue;
                }
                boolean contains = false;
                for (SubTile tile : layerClient.getTiles()) {
                    if (tile.x == x && tile.y == y) {
                        contains = true;
                    }
                }
                if (!contains) {
                    SubTile tile = mTileProvider.createTile(x, y);
                    layerClient.addTile(tile);
                }
            }
        }

        Log.i(LOGTAG, "TileRendering Add: " + (System.currentTimeMillis() - start));

        layerClient.endDrawing();
        Log.i(LOGTAG, "tilerender end draw");

        return true;
    }

    private void changePart(int partIndex) throws InterruptedException {
        mTileProvider.changePart(partIndex);
        GeckoLayerClient layerClient = mApplication.getLayerClient();
        layerClient.getTiles().clear();
        LOKitShell.sendEvent(LOEvent.draw(new Rect()));
    }

    private boolean initialize() {
        mApplication = LibreOfficeMainActivity.mAppContext;
        mTileProvider = new LOKitTileProvider(mApplication.getLayerController(), mInputFile);
        boolean isReady = mTileProvider.isReady();
        if (isReady)
        {
            if (!mCheckboardImageSet) {
                Log.i(LOGTAG, "Generate thumbnail!");
                Bitmap bitmap = mTileProvider.thumbnail();
                Log.i(LOGTAG, "Done generate thumbnail!");
                if (bitmap != null) {
                    Log.i(LOGTAG, "Setting checkboard image!");
                    mApplication.getLayerController().getView().changeCheckerboardBitmap(bitmap);
                    Log.i(LOGTAG, "Done setting checkboard image!!");
                    mCheckboardImageSet = true;
                }
            }
        }
        return isReady;
    }

    public void run() {
        if (initialize()) {
            try {
                boolean drawn = false;
                while (true) {
                    processEvent(mEventQueue.take());
                }
            } catch (InterruptedException ex) {
            }
        }
    }

    private void processEvent(LOEvent event) throws InterruptedException {
        switch (event.mType) {
            case LOEvent.VIEWPORT:
                mViewportMetrics = event.getViewport();
                draw();
                break;
            case LOEvent.DRAW:
                draw();
                break;
            case LOEvent.SIZE_CHANGED:
                break;
            case LOEvent.CHANGE_PART:
                changePart(event.getPartIndex());
                break;
        }
    }

    public void queueEvent(LOEvent event) {
        Log.i(LOGTAG, "Event: " + event.getTypeString());
        mEventQueue.add(event);
    }
}