aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/canvasproof/src/main/jni/org_skia_canvasproof_GaneshPictureRenderer.cpp
blob: d1de529674104e39e6853e53c9d50c6e50e2ac84 (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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "org_skia_canvasproof_GaneshPictureRenderer.h"

#include "GrContext.h"
#include "JavaInputStream.h"
#include "SkCanvas.h"
#include "SkMatrix.h"
#include "SkPicture.h"
#include "SkRect.h"
#include "SkStream.h"
#include "SkSurface.h"

#define TAG "GaneshPictureRenderer.cpp: "

static void render_picture(GrContext* grContext,
                           int width,
                           int height,
                           const SkPicture* picture,
                           const SkMatrix& matrix) {
    SkASSERT(grContext);
    if (!picture) {
        SkDebugf(TAG "!picture\n");
        return;
    }
    // Render to the default framebuffer render target.
    GrBackendRenderTargetDesc desc;
    desc.fWidth = width;
    desc.fHeight = height;
    desc.fConfig = kSkia8888_GrPixelConfig;
    desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
    SkSurfaceProps surfaceProps(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
                                kUnknown_SkPixelGeometry);
    // TODO:  Check to see if we can keep the surface between draw calls.
    SkAutoTUnref<SkSurface> surface(
            SkSurface::NewFromBackendRenderTarget(
                    grContext, desc, &surfaceProps));
    if (surface) {
        SkCanvas* canvas = surface->getCanvas();
        SkASSERT(canvas);
        canvas->clear(SK_ColorGRAY);
        canvas->concat(matrix);
        SkRect cullRect = picture->cullRect();
        canvas->clipRect(cullRect);
        picture->playback(canvas);
        canvas->flush();
    }
}

namespace {
struct GaneshPictureRendererImpl {
    SkAutoTUnref<GrContext> fGrContext;
    void render(int w, int h, const SkPicture* p, const SkMatrix& m) {
        if (!fGrContext) {
            // Cache the rendering context between frames.
            fGrContext.reset(GrContext::Create(kOpenGL_GrBackend, 0));
            if (!fGrContext) {
                SkDebugf(TAG "GrContext::Create - failed\n");
                return;
            }
        }
        render_picture(fGrContext, w, h, p, m);
    }
};
}  // namespace

/*
 * Class:     org_skia_canvasproof_GaneshPictureRenderer
 * Method:    DrawThisFrame
 * Signature: (IIFJ)V
 */
JNIEXPORT void JNICALL Java_org_skia_canvasproof_GaneshPictureRenderer_DrawThisFrame(
        JNIEnv*, jclass, jint width, jint height, jfloat scale, jlong ptr, jlong pic) {
    if (!ptr) { return; }
    SkMatrix matrix = SkMatrix::MakeScale((SkScalar)scale);
    GaneshPictureRendererImpl* impl =
        reinterpret_cast<GaneshPictureRendererImpl*>(ptr);
    SkPicture* picture = reinterpret_cast<SkPicture*>(pic);
    impl->render((int)width, (int)height, picture, matrix);
}

/*
 * Class:     org_skia_canvasproof_GaneshPictureRenderer
 * Method:    Ctor
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_org_skia_canvasproof_GaneshPictureRenderer_Ctor
  (JNIEnv *, jclass) {
    return reinterpret_cast<jlong>(new GaneshPictureRendererImpl);
}

/*
 * Class:     org_skia_canvasproof_GaneshPictureRenderer
 * Method:    CleanUp
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_org_skia_canvasproof_GaneshPictureRenderer_CleanUp
  (JNIEnv *, jclass, jlong ptr) {
    delete reinterpret_cast<GaneshPictureRendererImpl*>(ptr);
}

namespace {
struct AndroidRectHelper {
    jfieldID fLeft, fTop, fRight, fBottom;
    AndroidRectHelper()
        : fLeft(nullptr), fTop(nullptr), fRight(nullptr), fBottom(nullptr) {}
    void config(JNIEnv *env) {
        if (!fLeft) {
            jclass rectClass = env->FindClass("android/graphics/Rect");
            SkASSERT(rectClass);
            fLeft = env->GetFieldID(rectClass, "left", "I");
            fTop = env->GetFieldID(rectClass, "top", "I");
            fRight = env->GetFieldID(rectClass, "right", "I");
            fBottom = env->GetFieldID(rectClass, "bottom", "I");
        }
    }
};
} // namespace

/*
 * Class:     org_skia_canvasproof_GaneshPictureRenderer
 * Method:    GetCullRect
 * Signature: (Landroid/graphics/Rect;J)V
 */
JNIEXPORT void JNICALL Java_org_skia_canvasproof_GaneshPictureRenderer_GetCullRect
  (JNIEnv *env, jclass, jobject androidGraphicsRect, jlong picturePtr) {
    SkASSERT(androidGraphicsRect);
    const SkPicture* picture = reinterpret_cast<SkPicture*>(picturePtr);
    SkRect rect = SkRect::MakeEmpty();
    if (picture) {
        rect = picture->cullRect();
    }
    SkIRect iRect;
    rect.roundOut(&iRect);
    static AndroidRectHelper help;
    help.config(env);
    env->SetIntField(androidGraphicsRect, help.fLeft, (jint)(iRect.left()));
    env->SetIntField(androidGraphicsRect, help.fTop, (jint)(iRect.top()));
    env->SetIntField(androidGraphicsRect, help.fRight, (jint)(iRect.right()));
    env->SetIntField(androidGraphicsRect, help.fBottom, (jint)(iRect.bottom()));
}