summaryrefslogtreecommitdiffstats
path: root/awt/com/android/internal/awt/AndroidJavaBlitter.java
blob: 423b534cb988f3a150b5357e41971dd1549f6a25 (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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * Copyright 2007, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package com.android.internal.awt;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import org.apache.harmony.awt.gl.MultiRectArea;
import org.apache.harmony.awt.gl.Surface;
import org.apache.harmony.awt.gl.XORComposite;
import org.apache.harmony.awt.gl.render.Blitter;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;

public class AndroidJavaBlitter implements Blitter {

    private Canvas canvas;
    private Paint paint;
    private int colorCache;
        
    public AndroidJavaBlitter(Canvas c) {
        this.canvas = c;
        this.paint = new Paint();
        this.paint.setStrokeWidth(1);
    }
    
    /**
     * Instead of multiplication and division we are using values from
     * Lookup tables.
     */
    static byte mulLUT[][]; // Lookup table for multiplication
    static byte divLUT[][]; // Lookup table for division

    static{
        mulLUT = new byte[256][256];
        for(int i = 0; i < 256; i++){
            for(int j = 0; j < 256; j++){
                mulLUT[i][j] = (byte)((float)(i * j)/255 + 0.5f);
            }
        }
        divLUT = new byte[256][256];
        for(int i = 1; i < 256; i++){
            for(int j = 0; j < i; j++){
                divLUT[i][j] = (byte)(((float)j / 255) / ((float)i/ 255) * 255 + 0.5f);
            }
            for(int j = i; j < 256; j++){
                divLUT[i][j] = (byte)255;
            }
        }
    }

    final static int AlphaCompositeMode = 1;
    final static int XORMode = 2;

    public void blit(int srcX, int srcY, Surface srcSurf, int dstX, int dstY,
            Surface dstSurf, int width, int height, AffineTransform sysxform,
            AffineTransform xform, Composite comp, Color bgcolor,
            MultiRectArea clip) {
        
        if(xform == null){
            blit(srcX, srcY, srcSurf, dstX, dstY, dstSurf, width, height,
                    sysxform, comp, bgcolor, clip);
        }else{
            double scaleX = xform.getScaleX();
            double scaleY = xform.getScaleY();
            double scaledX = dstX / scaleX;
            double scaledY = dstY / scaleY;
            AffineTransform at = new AffineTransform();
            at.setToTranslation(scaledX, scaledY);
            xform.concatenate(at);
            sysxform.concatenate(xform);
            blit(srcX, srcY, srcSurf, 0, 0, dstSurf, width, height,
                    sysxform, comp, bgcolor, clip);
        }

    }

    public void blit(int srcX, int srcY, Surface srcSurf, int dstX, int dstY,
            Surface dstSurf, int width, int height, AffineTransform sysxform,
            Composite comp, Color bgcolor, MultiRectArea clip) {
        
        if(sysxform == null) {
            sysxform = new AffineTransform();
        }
        int type = sysxform.getType();
        switch(type){
            case AffineTransform.TYPE_TRANSLATION:
                dstX += sysxform.getTranslateX();
                dstY += sysxform.getTranslateY();
            case AffineTransform.TYPE_IDENTITY:
                simpleBlit(srcX, srcY, srcSurf, dstX, dstY, dstSurf,
                        width, height, comp, bgcolor, clip);
                break;
            default:
                int srcW = srcSurf.getWidth();
                int srcH = srcSurf.getHeight();

                int w = srcX + width < srcW ? width : srcW - srcX;
                int h = srcY + height < srcH ? height : srcH - srcY;

                ColorModel srcCM = srcSurf.getColorModel();
                Raster srcR = srcSurf.getRaster().createChild(srcX, srcY,
                        w, h, 0, 0, null);

                ColorModel dstCM = dstSurf.getColorModel();
                WritableRaster dstR = dstSurf.getRaster();

                transformedBlit(srcCM, srcR, 0, 0, dstCM, dstR, dstX, dstY, w, h,
                        sysxform, comp, bgcolor, clip);

        }
    }

    public void simpleBlit(int srcX, int srcY, Surface srcSurf, int dstX, int dstY,
            Surface dstSurf, int width, int height, Composite comp,
            Color bgcolor, MultiRectArea clip) {

        // TODO It's possible, though unlikely that we might encounter non-int[]
        // data buffers. In this case the following code needs to have several
        // branches that take this into account.
        data = (DataBufferInt)srcSurf.getRaster().getDataBuffer();
        int[] pixels = data.getData();
        if (!srcSurf.getColorModel().hasAlpha()) {
            // This wouldn't be necessary if Android supported RGB_888.
            for (int i = 0; i < pixels.length; i++) {
                pixels[i] = pixels[i] | 0xff000000;
            }
        }
        bmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
        canvas.drawBitmap(bmap, dstX, dstY, paint);
    }
    
    public void blit(int srcX, int srcY, Surface srcSurf, int dstX, int dstY,
            Surface dstSurf, int width, int height, Composite comp,
            Color bgcolor, MultiRectArea clip) {

        javaBlt(srcX, srcY, srcSurf.getWidth(), srcSurf.getHeight(),
                srcSurf.getColorModel(), srcSurf.getRaster(), dstX, dstY,
                dstSurf.getWidth(), dstSurf.getHeight(),
                dstSurf.getColorModel(), dstSurf.getRaster(),
                width, height, comp, bgcolor, clip);

    }
    
    public void javaBlt(int srcX, int srcY, int srcW, int srcH,
            ColorModel srcCM, Raster srcRast, int dstX, int dstY,
            int dstW, int dstH, ColorModel dstCM, WritableRaster dstRast,
            int width, int height, Composite comp, Color bgcolor,
            MultiRectArea clip){
        
        int srcX2 = srcW - 1;
        int srcY2 = srcH - 1;
        int dstX2 = dstW - 1;
        int dstY2 = dstH - 1;

        if(srcX < 0){
            width += srcX;
            srcX = 0;
        }
        if(srcY < 0){
            height += srcY;
            srcY = 0;
        }

        if(dstX < 0){
            width += dstX;
            srcX -= dstX;
            dstX = 0;
        }
        if(dstY < 0){
            height += dstY;
            srcY -= dstY;
            dstY = 0;
        }

        if(srcX > srcX2 || srcY > srcY2) {
            return;
        }
        if(dstX > dstX2 || dstY > dstY2) {
            return;
        }

        if(srcX + width > srcX2) {
            width = srcX2 - srcX + 1;
        }
        if(srcY + height > srcY2) {
            height = srcY2 - srcY + 1;
        }
        if(dstX + width > dstX2) {
            width = dstX2 - dstX + 1;
        }
        if(dstY + height > dstY2) {
            height = dstY2 - dstY + 1;
        }

        if(width <= 0 || height <= 0) {
            return;
        }

        int clipRects[];
        if(clip != null) {
            clipRects = clip.rect;
        } else {
            clipRects = new int[]{5, 0, 0, dstW - 1, dstH - 1};
        }

        boolean isAlphaComp = false;
        int rule = 0;
        float alpha = 0;
        boolean isXORComp = false;
        Color xorcolor = null;
        CompositeContext cont = null;

        if(comp instanceof AlphaComposite){
            isAlphaComp = true;
            AlphaComposite ac = (AlphaComposite) comp;
            rule = ac.getRule();
            alpha = ac.getAlpha();
        }else if(comp instanceof XORComposite){
            isXORComp = true;
            XORComposite xcomp = (XORComposite) comp;
            xorcolor = xcomp.getXORColor();
        }else{
            cont = comp.createContext(srcCM, dstCM, null);
        }

        for(int i = 1; i < clipRects[0]; i += 4){
            int _sx = srcX;
            int _sy = srcY;

            int _dx = dstX;
            int _dy = dstY;

            int _w = width;
            int _h = height;

            int cx = clipRects[i];          // Clipping left top X
            int cy = clipRects[i + 1];      // Clipping left top Y
            int cx2 = clipRects[i + 2];     // Clipping right bottom X
            int cy2 = clipRects[i + 3];     // Clipping right bottom Y

            if(_dx > cx2 || _dy > cy2 || dstX2 < cx || dstY2 < cy) {
                continue;
            }

            if(cx > _dx){
                int shx = cx - _dx;
                _w -= shx;
                _dx = cx;
                _sx += shx;
            }

            if(cy > _dy){
                int shy = cy - _dy;
                _h -= shy;
                _dy = cy;
                _sy += shy;
            }

            if(_dx + _w > cx2 + 1){
                _w = cx2 - _dx + 1;
            }

            if(_dy + _h > cy2 + 1){
                _h = cy2 - _dy + 1;
            }

            if(_sx > srcX2 || _sy > srcY2) {
                continue;
            }

            if(isAlphaComp){
                alphaCompose(_sx, _sy, srcCM, srcRast, _dx, _dy,
                        dstCM, dstRast, _w, _h, rule, alpha, bgcolor);
            }else if(isXORComp){
                xorCompose(_sx, _sy, srcCM, srcRast, _dx, _dy,
                        dstCM, dstRast, _w, _h, xorcolor);
            }else{
                Raster sr = srcRast.createChild(_sx, _sy, _w, _h, 0, 0, null);
                WritableRaster dr = dstRast.createWritableChild(_dx, _dy,
                        _w, _h, 0, 0, null);
                cont.compose(sr, dr, dr);
            }
        }
        
    }

    DataBufferInt data;
    Bitmap bmap, bmp;
    
    void alphaCompose(int srcX, int srcY, ColorModel srcCM, Raster srcRast,
            int dstX, int dstY, ColorModel dstCM, WritableRaster dstRast,
            int width, int height, int rule, float alpha, Color bgcolor){
        
        Object srcPixel = getTransferArray(srcRast, 1);
        data = (DataBufferInt)srcRast.getDataBuffer();
        int pix[] = data.getData();
        bmap = Bitmap.createBitmap(pix, width, height, Bitmap.Config.RGB_565);
        canvas.drawBitmap(bmap, dstX, dstY, paint);
    }
    
    void render(int[] img, int x, int y, int width, int height) {
        canvas.drawBitmap(Bitmap.createBitmap(img, width, height, Bitmap.Config.ARGB_8888), x, y, paint);
    }

    void xorCompose(int srcX, int srcY, ColorModel srcCM, Raster srcRast,
            int dstX, int dstY, ColorModel dstCM, WritableRaster dstRast,
            int width, int height, Color xorcolor){

        data = (DataBufferInt)srcRast.getDataBuffer();
        int pix[] = data.getData();
        bmap = Bitmap.createBitmap(pix, width, height, Bitmap.Config.RGB_565);
        canvas.drawBitmap(bmap, dstX, dstY, paint);
    }
    
    private void transformedBlit(ColorModel srcCM, Raster srcR, int srcX, int srcY,
            ColorModel dstCM, WritableRaster dstR, int dstX, int dstY,
            int width, int height, AffineTransform at, Composite comp,
            Color bgcolor, MultiRectArea clip) {
        
        data = (DataBufferInt)srcR.getDataBuffer();
        int[] pixels = data.getData();
        if (!srcCM.hasAlpha()) {
            // This wouldn't be necessary if Android supported RGB_888.
            for (int i = 0; i < pixels.length; i++) {
                pixels[i] = pixels[i] | 0xff000000;
            }
        }
        bmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
        
        Matrix tm = new Matrix();
        tm.setConcat(canvas.getMatrix(), AndroidGraphics2D.createMatrixObj(at));
        if(at.getType() > 1) {
            bmp = Bitmap.createBitmap(bmap, 0, 0, width, height, tm, true);
        } else {
            bmp = Bitmap.createBitmap(bmap, 0, 0, width, height, tm, false);
        }
        canvas.drawBitmap(bmp, dstX + (float)at.getTranslateX(), dstY + (float)at.getTranslateY(), paint);
    }

    private Rectangle2D getBounds2D(AffineTransform at, Rectangle r) {
        int x = r.x;
        int y = r.y;
        int width = r.width;
        int height = r.height;

        float[] corners = {
            x, y,
            x + width, y,
            x + width, y + height,
            x, y + height
        };

        at.transform(corners, 0, corners, 0, 4);

        Rectangle2D.Float bounds = new Rectangle2D.Float(corners[0], corners[1], 0 , 0);
        bounds.add(corners[2], corners[3]);
        bounds.add(corners[4], corners[5]);
        bounds.add(corners[6], corners[7]);

        return bounds;
    }

    private int compose(int srcRGB, boolean isSrcAlphaPre,
            int dstRGB, boolean dstHasAlpha, boolean isDstAlphaPre,
            int rule, int srcConstAlpha){

        int sa, sr, sg, sb, da, dr, dg, db;

        sa = (srcRGB >> 24) & 0xff;
        sr = (srcRGB >> 16) & 0xff;
        sg = (srcRGB >> 8) & 0xff;
        sb = srcRGB & 0xff;

        if(isSrcAlphaPre){
            sa = mulLUT[srcConstAlpha][sa] & 0xff;
            sr = mulLUT[srcConstAlpha][sr] & 0xff;
            sg = mulLUT[srcConstAlpha][sg] & 0xff;
            sb = mulLUT[srcConstAlpha][sb] & 0xff;
        }else{
            sa = mulLUT[srcConstAlpha][sa] & 0xff;
            sr = mulLUT[sa][sr] & 0xff;
            sg = mulLUT[sa][sg] & 0xff;
            sb = mulLUT[sa][sb] & 0xff;
        }

        da = (dstRGB >> 24) & 0xff;
        dr = (dstRGB >> 16) & 0xff;
        dg = (dstRGB >> 8) & 0xff;
        db = dstRGB & 0xff;

        if(!isDstAlphaPre){
            dr = mulLUT[da][dr] & 0xff;
            dg = mulLUT[da][dg] & 0xff;
            db = mulLUT[da][db] & 0xff;
        }

        int Fs = 0;
        int Fd = 0;
        switch(rule){
        case AlphaComposite.CLEAR:
            break;

        case AlphaComposite.DST:
            Fd = 255;
            break;

        case AlphaComposite.DST_ATOP:
            Fs = 255 - da;
            Fd = sa;
            break;

        case AlphaComposite.DST_IN:
            Fd = sa;
            break;

        case AlphaComposite.DST_OUT:
            Fd = 255 - sa;
            break;

        case AlphaComposite.DST_OVER:
            Fs = 255 - da;
            Fd = 255;
            break;

        case AlphaComposite.SRC:
            Fs = 255;
            break;

        case AlphaComposite.SRC_ATOP:
            Fs = da;
            Fd = 255 - sa;
            break;

        case AlphaComposite.SRC_IN:
            Fs = da;
            break;

        case AlphaComposite.SRC_OUT:
            Fs = 255 - da;
            break;

        case AlphaComposite.SRC_OVER:
            Fs = 255;
            Fd = 255 - sa;
            break;

        case AlphaComposite.XOR:
            Fs = 255 - da;
            Fd = 255 - sa;
            break;
        }
        dr = (mulLUT[sr][Fs] & 0xff) + (mulLUT[dr][Fd] & 0xff);
        dg = (mulLUT[sg][Fs] & 0xff) + (mulLUT[dg][Fd] & 0xff);
        db = (mulLUT[sb][Fs] & 0xff) + (mulLUT[db][Fd] & 0xff);

        da = (mulLUT[sa][Fs] & 0xff) + (mulLUT[da][Fd] & 0xff);

        if(!isDstAlphaPre){
            if(da != 255){
                dr = divLUT[da][dr] & 0xff;
                dg = divLUT[da][dg] & 0xff;
                db = divLUT[da][db] & 0xff;
            }
        }
        if(!dstHasAlpha) {
            da = 0xff;
        }
        dstRGB = (da << 24) | (dr << 16) | (dg << 8) | db;

        return dstRGB;

    }
    
    /**
     * Allocate an array that can be use to store the result for a 
     * Raster.getDataElements call.
     * @param raster  Raster (type) where the getDataElements call will be made. 
     * @param nbPixels  How many pixels to store in the array at most
     * @return the result array or null
     */
    private Object getTransferArray(Raster raster, int nbPixels) {
        int transferType = raster.getTransferType();
        int nbDataElements = raster.getSampleModel().getNumDataElements();
        int n = nbDataElements * nbPixels;
        switch (transferType) {
        case DataBuffer.TYPE_BYTE:
            return new byte[n];
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_USHORT:
            return new short[n];
        case DataBuffer.TYPE_INT:
            return new int[n];
        case DataBuffer.TYPE_FLOAT:
            return new float[n];
        case DataBuffer.TYPE_DOUBLE:
            return new double[n];
        case DataBuffer.TYPE_UNDEFINED:
        default:
            return null;
        }
    }
    
    /**
     * Draw a pixel
     */
    private void dot(int x, int y, int clr) {
        if (colorCache != clr) {
            paint.setColor(clr);  
            colorCache = clr;
        }
        canvas.drawLine(x, y, x + 1, y + 1, paint);
    }
}